Author Topic: Multiple Shops! A brute-force guide.  (Read 5092 times)

Offline Basil Beard

Multiple Shops! A brute-force guide.
« on: September 11, 2006, 05:14:45 am »
Only follow this if you feel like manually editing alot of code and also editing your database by hand.. I don't feel like actually making adding shops nice and easy.  :P.
Also BACK UP ALL FILES BEFORE YOU START! incase I messed up.  :coolsmiley:. Also when I sya "line 201" that means 201 of the unedited file. If you have made other changes, including the ones you make while editing the file, these line numbers may change. Sorry about that.

Your first task it to edit the Shop-Buy.php file.

After line 24 add
Code: [Select]
if (!isset($_GET['store'])) {
die("Please select a store!");
}

if ($_GET['store'] < 1 || $_GET['store'] > 5) {
die("Please select a valid store");
}

At line 117 find this part of the code:

Code: [Select]
$result = db_query("SELECT name, `desc`, price, stock, id, image, shop
FROM {$db_prefix}shop_items
ORDER BY {$sortQuery} {$sortDirQuery}", __FILE__, __LINE__);

and replace it with

Code: [Select]
$result = db_query("SELECT name, `desc`, price, stock, id, image
FROM {$db_prefix}shop_items
WHERE shopid = {$_GET['store']}
ORDER BY {$sortQuery} {$sortDirQuery}",
__FILE__, __LINE__);

Next, line 149

Code: [Select]
$context['shop_items_list'] .= "<a href='$scripturl?action=shop;do=buy2;id={$itemRows[$x]['id']}'>{$txt['shop_buynow']}</a>";

Make that
Code: [Select]
$context['shop_items_list'] .= "<a href='$scripturl?action=shop;do=buy2;store={$_GET['store']};id={$itemRows[$x]['id']};'>{$txt['shop_buynow']}</a>";

Line 169:
Code: [Select]
$context['shop_page_link'] = $scripturl."?action=shop;do=buy;sort=".$_GET['sort'].";sortDir=".$_GET['sortDir'];
replace it with
Code: [Select]
$context['shop_page_link'] = $scripturl."?action=shop;do=buy;store=".$_GET['store'].";sort=".$_GET['sort'].";sortDir=".$_GET['sortDir'];

after line 177 add:
Code: [Select]
if (!isset($_GET['store'])) {
die("Please select a store!");
}

if ($_GET['store'] < 1 || $_GET['store'] > 5) {
die("Please select a valid store");
}

Line 190. Find
Code: [Select]
$result = db_query("SELECT price, stock, name
FROM {$db_prefix}shop_items
WHERE id = {$_GET['id']}
LIMIT 1", __FILE__, __LINE__);
$row = mysql_fetch_array($result, MYSQL_ASSOC);
replace it with
Code: [Select]
$result = db_query("SELECT price, stock, name, shopid
FROM {$db_prefix}shop_items
WHERE id = {$_GET['id']}
LIMIT 1", __FILE__, __LINE__);
$row = mysql_fetch_array($result, MYSQL_ASSOC);

if ($row['shopid'] != $_GET['store']) {
die ("That item cannot be purchased in this store");
}

And yay! That file is done!

While you are at it; lets make our shops restock themselves as well. No more annoying by hand stuff.


add this after line 42.

Code: [Select]
    $time = date("zH");
$result = db_query("UPDATE {$db_prefix}shop_items
        SET stock = stock + restock
        WHERE laststock <= $time - timer AND stock <= maxrestock", __FILE__, __LINE__);
$result = db_query("UPDATE {$db_prefix}shop_items
        SET laststock = $time
        WHERE laststock <= $time - timer", __FILE__, __LINE__);
$times = date("z");
if ($modSettings['rateday'] != $times) {
$newrate = mt_rand(100,300);
$newrate= $newrate / 100;
$result = db_query("UPDATE {$db_prefix}settings
SET value = {$newrate}
WHERE variable = 'rate'", __FILE__, __LINE__);
$result = db_query("UPDATE {$db_prefix}settings
SET value = {$times}
WHERE variable = 'rateday'", __FILE__, __LINE__);
}

Ok. So those two files are done! Time to play with the templetes!

Line 29 of Shop.templete.php

Code: [Select]
<a href="$scripturl?action=shop;do=buy">{$txt['shop_buy']}</a><br />

replace that with

Code: [Select]
<a href="$scripturl?action=shop;do=buy;store=1">[i]Store 1 desp.[/i]</a><br />
<a href="$scripturl?action=shop;do=buy;store=2">[i]Store 2 desp.[/i]</a><br />
<a href="$scripturl?action=shop;do=buy;store=3">[i]Store 3 desp.[/i]</a><br />
.
.
.
<a href="$scripturl?action=shop;do=buy;store=n">[i]Store n desp.[/i]</a><br />


Where n is the number of stores. Rename "store 1 desp" whatever you want to call that store.

Ok. Now we get to play with the admin part of the store!
in shopAdmin.templete.php instert after line  179

Code: [Select]
Amount to stock per restock: <input name="restock" type="text" value="{$context['shop_items_item']['restock']}" size="5"<br />
Time between restocks: <input name="timer" type="text" value="{$context['shop_items_item']['timer']}" size="5"<br />
Max amount to stock: <input name="maxstock" type="text" value="{$context['shop_items_item']['maxstock']}" size="5"<br />
ID of shop to stock in: <input name="shopid" type="text" value="{$context['shop_items_item']['shopid']}" size="5"<br />

and then do the SAME THING after line 220.

Ok, last file. ShopAdmin.php

line 201. Instert this after it

Code: [Select]
                                            'restock' => 1,
                                            'timer' => 6,
                                            'maxstock' => 10,
                                            'shopid' => 1

line 225. Fine and replace
Code: [Select]
        $sql = "INSERT
                INTO {$db_prefix}shop_items
                (name, `desc`, price, module, stock, input_needed, can_use_item, info1, info2, info3, info4, image)
                VALUES (
                        '{$_POST['itemname']}',
                        '{$_POST['itemdesc']}',
                        {$_POST['itemprice']},
                        '{$_POST['item']}',
                        {$_POST['itemstock']},
                        {$_POST['require_input']},
                        {$_POST['can_use_item']},
                        '{$_POST['info1']}',
                        '{$_POST['info2']}',
                        '{$_POST['info3']}',
                        '{$_POST['info4']}',
                        '{$_POST['icon']}'
                        )";
replace with

Code: [Select]
        $sql = "INSERT
                INTO {$db_prefix}shop_items
                (name, `desc`, price, module, stock, input_needed, can_use_item, info1, info2, info3, info4, image, restock, timer, maxrestock, shopid)
                VALUES (
                        '{$_POST['itemname']}',
                        '{$_POST['itemdesc']}',
                        {$_POST['itemprice']},
                        '{$_POST['item']}',
                        {$_POST['itemstock']},
                        {$_POST['require_input']},
                        {$_POST['can_use_item']},
                        '{$_POST['info1']}',
                        '{$_POST['info2']}',
                        '{$_POST['info3']}',
                        '{$_POST['info4']}',
                        '{$_POST['icon']}',
                        '{$_POST['restock']}',
                        '{$_POST['timer']}',
                        '{$_POST['maxstock']}',
                        '{$_POST['shopid']}'
                        )";

line 262. Find and replace

Code: [Select]
        $result = db_query("SELECT name, `desc`, price, stock, image
                            FROM {$db_prefix}shop_items
                            WHERE id = {$_GET['id']}"
                            , __FILE__, __LINE__);
        $row = mysql_fetch_array($result, MYSQL_ASSOC);
        $context['shop_edit'] = array(
                                      'id' => $_GET['id'],
                                      'name' => $row['name'],
                                      'desc' => $row['desc'],
                                      'price' => $row['price'],
                                      'stock' => $row['stock'],
                                      'image' => $row['image'],
                                       );
    } elseif ($_GET['do'] == "edit2") {
        $context['shop_items_edit'] = 2;
        $result = db_query("UPDATE {$db_prefix}shop_items
                            SET name = '{$_POST['itemname']}',
                                `desc` = '{$_POST['itemdesc']}',
                                price = {$_POST['itemprice']},
                                stock = {$_POST['itemstock']},
                                image = '{$_POST['icon']}'
                            WHERE id = {$_POST['id']}
                            LIMIT 1"
                            , __FILE__, __LINE__);
with 
Code: [Select]
       $result = db_query("SELECT name, `desc`, price, stock, image, restock, timer, maxrestock, shopid
                            FROM {$db_prefix}shop_items
                            WHERE id = {$_GET['id']}"
                            , __FILE__, __LINE__);
        $row = mysql_fetch_array($result, MYSQL_ASSOC);
        $context['shop_edit'] = array(
                                      'id' => $_GET['id'],
                                      'name' => $row['name'],
                                      'desc' => $row['desc'],
                                      'price' => $row['price'],
                                      'stock' => $row['stock'],
                                      'image' => $row['image'],
                                      'restock' => $row['restock'],
                                      'timer' => $row['timer'],
                                      'maxstock' => $row['maxrestock'],
                                      'shopid' => $row['shopid']
                                       );
    } elseif ($_GET['do'] == "edit2") {
        $context['shop_items_edit'] = 2;
        $result = db_query("UPDATE {$db_prefix}shop_items
                            SET name = '{$_POST['itemname']}',
                                `desc` = '{$_POST['itemdesc']}',
                                price = {$_POST['itemprice']},
                                stock = {$_POST['itemstock']},
                                image = '{$_POST['icon']}',
                                restock = {$_POST['restock']},
                                timer = {$_POST['timer']},
                                maxrestock = {$_POST['maxstock']},
                                shopid = {$_POST['shopid']}
                            WHERE id = {$_POST['id']}
                            LIMIT 1", __FILE__, __LINE__);

And there you go. You have a shop.

But wait, there is still the database to edit! Silly us  :D

Anyways, you need to add these four cols to the shop_shop_items database:

        Field      Type     Attributes     Null     Default
      timer      int(10)     UNSIGNED     No      6
    laststock     int(10)    UNSIGNED    No     0
    used     int(10)    UNSIGNED    No     0
    maxrestock     int(10)    UNSIGNED    No     15     

And after you do that, upload everything, cross your fingers, and hope it works. It probably won't, but you can post whatever error you get here and I can try to help you fix it.  :police:
Arrrrr!

Offline okapi

Re: Multiple Shops! A brute-force guide.
« Reply #1 on: September 14, 2006, 03:41:55 am »
Hi, when i try to add an item i get this error:

Unknown column 'restock' in 'field list'
Archivo: /home/shamans/public_html/foro/Sources/shop/ShopAdmin.php
Línea: 251

Column restock... but in the sql changes -->

      timer      int(10)     UNSIGNED     No      6
    laststock     int(10)    UNSIGNED    No     0
    used     int(10)    UNSIGNED    No     0
    maxrestock     int(10)    UNSIGNED    No     15

Where is the column restock? and de colum shopid? :S

Offline okapi

Re: Multiple Shops! A brute-force guide.
« Reply #2 on: September 14, 2006, 03:58:37 am »
I add this 2 tables, but now i have others erros:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '
timer = ,
max' at line 7
Archivo: /home/shamans/public_html/foro/Sources/shop/ShopAdmin.php
Línea: 300

Offline okapi

Re: Multiple Shops! A brute-force guide.
« Reply #3 on: September 14, 2006, 05:37:26 am »
Ok, i had an error updating the files, now works. thanks!

Offline Basil Beard

Re: Multiple Shops! A brute-force guide.
« Reply #4 on: September 14, 2006, 12:20:20 pm »
Glad to know it works. I was worried I had missed something =P
Arrrrr!

Offline Daniel15

Re: Multiple Shops! A brute-force guide.
« Reply #5 on: September 16, 2006, 01:16:09 pm »
Nice work, Basil Beard!
I'd suggest to you to make this into a 'patch'. To do this, first get the latest testing version, as shown at http://www.daniel15.com/forum/index.php?topic=198.0 . Then, do all the code changes to that file. Finally, you can right-click on the folder, and go TortoiseSVN --> Create Patch. That will generate a single file with all the changed data contained in it. Once you have the patch, you can just give that file to people, and if they have the testing version, they can right-click the folder and go TortoiseSVN --> Apply Patch.

There's lots of other ways to apply patches, with the patch program being the most common. Someone could simply get all their SMFShop files, use the 'Patch' utility to patch them, and then upload the patched files :)

Offline Basil Beard

Re: Multiple Shops! A brute-force guide.
« Reply #6 on: September 16, 2006, 11:26:52 pm »
Wow cool! I'll work on that sometime soon. That should also help a whole lot with the pet mod when I finally code that. Thanks!
Arrrrr!

Offline Daniel15

Re: Multiple Shops! A brute-force guide.
« Reply #7 on: September 17, 2006, 09:41:28 am »
Just another thing... If you'd like to patch a specific version of SMFShop, rather than the latest development version, then follow the same instructions at http://www.daniel15.com/forum/index.php?topic=198.0, except when it says to use:
http://server.daniel15.com/svn/smfshop/trunk/
use:
http://server.daniel15.com/svn/smfshop/tags/release-[version]/
instead. For example, if you'd like to work on the SMFShop 2.2 code, use http://server.daniel15.com/svn/smfshop/tags/release-2.2/ . Basically, the 'trunk' is the latest development code, whereas the 'tags' are specific version which have been tagged. In this case, I'm tagging all the releases of SMFShop.

Note that at this time, the development code, and the latest SMFShop release, are literally identical (I haven't done much development).

EDIT: If you'd like to learn about SVN, there's a good (free) online book available at http://svnbook.red-bean.com/ ;)
« Last Edit: September 17, 2006, 09:50:21 am by daniel15 »

Offline Basil Beard

Re: Multiple Shops! A brute-force guide.
« Reply #8 on: September 18, 2006, 03:34:22 am »
Do I need linux or something to use that program? It gave me an error when I tried to download then install o_0
Arrrrr!

Offline Daniel15

Re: Multiple Shops! A brute-force guide.
« Reply #9 on: September 19, 2006, 04:35:01 pm »
Nope, TortoiseSVN is for Windows. :) The 'Patch' utility was originally for Linux, although the one I linked to is a Windows one.
Note that in that book, they'll talk about the 'svn' utility. All of the commands they refer to can be performed using the 'TortoiseSVN' menu :D

What errors were you getting?