Thread: Holiday Shops
View Single Post
  #4  
Old 12-25-2014, 04:32 PM
IntoRain's Avatar
IntoRain IntoRain is offline
Moderator
 
Join Date: Jul 2013
Location: Portugal
Posts: 461
Gender: Female
Credits: 22,417
IntoRain is on a distinguished road
Default

I coded something similar to #2, it should work properly but if you find a bug tell me.

How it works:

You add a new table to your database with the events your site has. These events have a start and end date. The good thing to this table is that you can use these events for other things you want to change in your site during holidays!
Then you associate your "holiday" shops with the various events they are open in, in the database. Then you associate your holiday items with a holiday shop and an event as well.

When the shops are displayed, the server checks if any of the events they are associated with, if any, are active. If not, the shop is closed. If one is active, the shop is open. Shops without any events associated work like normal shops. Entering the shop, the server loads the items that belong to that shop and to the current event.

Important: I only did this to the Itemshop so far, not Adoptshop

Important: I did no Admincp option for this, so you will have to manually edit the database with the stuff you want. I will try to add this mod to the Admincp sometime later

Also guys feel free to add in the stuff missing yourself or make any changes at all to fit your site and your ideas.


How to do this:

Database

1) First, add a new table to your database called [prefix]_events. Please note [prefix] is your database prefix, what all your tables begin with. For example, all my tables begin with 'adopts', because it's mysidia's default upon installation.

Fields:
  • eid -> it's an int, a primary key, can't be null, set it to auto_increment (A_I)
  • name -> The name of your event. It's a varchar, can be null and default is "NULL"
  • startson -> When your event starts. It's a varchar, at least size 5
  • endson -> When your event ends. It's a varchar, at least size 5

Note: Both startson and endson will be in the format Day-Month. If you switch to Month-Day some functions won't work anymore.

Note2: If your startson is 24-12 (24 December) and endson is 28-12 (28 December), that means the shop will run on the 24, 25, 26 and 27. Once it's 28, the shop will be inactive.

Example:
Below is the structure and contents of my table adopts_events.


2) Now go to your table [prefix]_items and add the following column:
  • eventid -> it's an int, can be null and default is -1

Items with an eventid of -1 don't belong in any event.
Items with an eventid > 0 belong in an event.

Example:
For example, in the image above, you can see the eid of my Christmas event is 1. So I added an item to a store called "Holliday Items" with an eventid of 1. That means that item will be sold in the store Holliday Items only when it's Christmas (eventid = 1). If I had put an eventid of 3, it would appear on Valentine's Day, etc. It depends on the events you added to your [prefix]_events table and their eid.



3) Now go to your [prefix]_shops table and add in this new column:
  • events -> varchar, can be null and default is 'none'

In this column, you will put event IDs (eid from [prefix]_events table) separated by commas (no spaces between them). If the shop is not event-only, put 'none' instead.

Example:

For example, if I wanted a shop open on Christmas and Halloween, since the eid for those events are 1 and 2 in my events table, I have to put "1,2" on that column. If I just wanted Christmas, I'd put "1". If I wanted all my 3 events I created I have to put "1,2,3".




Files

1) Create the file class_event.php (inside classes folder) with these contents:

PHP Code:
<?php

use Resource\Native\String;
use 
Resource\Native\Object;

class 
Event extends Object{

    public 
$eid;
    public 
$name;
    public 
$startson;
    public 
$endson;
  
    public function 
__construct($eid){      
    
$mysidia Registry::get("mysidia");
    
$row $mysidia->db->select("events", array(), "eid ='{$eid}'")->fetchObject();

    if(!
is_object($row)) throw new Exception("Invalid Event specified");
        
// loop through the anonymous object created to assign properties
        
foreach($row as $key => $val){
            
$this->$key $val;         
        }
    }

  public function 
isActive() {

     
$isActive false;
     
$startson_s explode('-'$this->startson);
     
$endson_s explode('-'$this->endson);
     
$today date('d-m');     
     
$today_s explode('-'$today);
          
      if(
$startson_s[1] == $endson_s[1]){
         if(
$today_s[1] == $startson_s[1] && $today_s[0] >= $startson_s[0] && $today_s[0] < $endson_s[0]){
                 
$isActive true;
         }
      }
      else if(
$startson_s[1] < $endson_s[1]){
          
          if(
$today_s[1] >= $startson_s[1] && $today_s[1] <= $endson_s[1]){
              if(
$today_s[1] == $startson_s[1]){
                  if(
$today_s[0] >= $startson_s[0])
                      
$isActive true;
              }
              else if(
$today_s[1] == $endson_s[1]){
                  if(
$today_s[0] < $endson_s[0])
                      
$isActive true;
              }
             else
                    
$isActive true;          
          }
                    
      }    
      else {
      
          if(
$today_s[1] >= $startson_s[1] || $today_s[1] <= $endson_s[1]){
              if(
$today_s[1] == $startson_s[1]){
                  if(
$today_s[0] >= $startson_s[0])
                      
$isActive true;
              }
              else if(
$today_s[1] == $endson_s[1]){
                  if(
$today_s[0] < $endson_s[0])
                      
$isActive true;
              }
             else
                    
$isActive true;          
          }      
      
      }
         
     return 
$isActive;
  }
  
}
?>
The isActive() function is pretty messy, if anyone wants to reduce it, I'd appreciate it xD

2) Edit the file class_item.php like this:

class_item.php

PHP Code:
<?php

use Resource\Native\Object;

class 
Item extends Model{
  
// The item class.

    
public $id;
    public 
$category;
    public 
$itemname;
    public 
$description;
    public 
$imageurl;
    public 
$function;
    public 
$target;
    public 
$shop;
    public 
$price;
    public 
$chance;
    public 
$cap;
    public 
$tradable;
    public 
$consumable;

    public 
$eventid;//add this line here!
(...)
3) Edit the file class_itemshop.php like below. In case you can't copy the whole code, all changes are commented with "add this", so look for them and compare to your existing file to add them in.

class_itemshop.php
PHP Code:
<?php

use Resource\Collection\LinkedList;

class 
Itemshop extends Model{

    public 
$sid;
    public 
$category;
    public 
$shopname;
    public 
$shoptype;
    public 
$description;
    public 
$imageurl;
    public 
$status;
    public 
$restriction;
    public 
$salestax;
    public 
$events;//add this
    
public $events_array;//add this
    
public $active_event;//add this
    
public $items;
    protected 
$total 0;
  
    public function 
__construct($shopname){
        
// Fetch the database info into object property      
        
$mysidia Registry::get("mysidia");
        
$row $mysidia->db->select("shops", array(), "shopname ='{$shopname}'")->fetchObject();
        if(!
is_object($row)) throw new Exception("Invalid Shopname specified");
      
        
// loop through the anonymous object created to assign properties
        
foreach($row as $key => $val){
            
$this->$key $val;     
        }

    
//add this here, this has to be before the getitemnames()!
    
$this->active_event = -1;    
    if(
$this->events != 'none') {
           
$this->fillEventsArray();
        
$this->checkForEvent();
    }
    
        
$this->items $this->getitemnames();
        
$this->total = (is_array($this->items))?count($this->items):0;
        
    }

    public function 
getcategory(){
        
$mysidia Registry::get("mysidia");
        
$stmt $mysidia->db->select("shops", array(), "category ='{$this->category}'");
        
$cate_exist = ($row $stmt->fetchObject())?TRUE:FALSE;     
        return 
$cate_exist;
    }
  
    public function 
getshop(){  
        
$mysidia Registry::get("mysidia");
        if(empty(
$this->shopname)) $shop_exist FALSE;
        else{
            
$stmt $mysidia->db->select("shops", array(), "shopname ='{$this->shopname}'");
            
$shop_exist = ($row $stmt->fetchObject())?TRUE:FALSE;    
        }
        return 
$shop_exist;
    }
  
    public function 
getitemnames(){
          if(!
$this->items){
            
$mysidia Registry::get("mysidia");
                
            
////add this 
            
if($this->events != 'none'
                
$stmt $mysidia->db->select("items", array("itemname"), "shop ='{$this->shopname}' and eventid='{$this->active_event}'");
            else
                
$stmt $mysidia->db->select("items", array("itemname"), "shop ='{$this->shopname}'");
            
            
$items = array();
        
            while(
$item $stmt->fetchColumn()){
                
$items[] = $item;
            }
            return 
$items;
        }
        else return 
$this->items;
    }
  
    public function 
gettotal(){  
        return 
$this->total;
    }
  
    public function 
display(){
        
$mysidia Registry::get("mysidia");
        
$document $mysidia->frame->getDocument();              
        
$document->addLangvar($mysidia->lang->select_item);
       
      
        if(
$this->gettotal() == 0){
            
$document->addLangvar($mysidia->lang->empty);
            return 
FALSE;
        }     
          
        
$itemList = new TableBuilder("shop");
        
$itemList->setAlign(new Align("center""middle"));
        
$itemList->buildHeaders("Image""Category""Name""Description""Price""Buy");    
        
$itemList->setHelper(new ShopTableHelper);
      
        foreach(
$this->items as $stockitem){
              
$item $this->getitem($stockitem);
            
$cells = new LinkedList;         
            
$cells->add(new TCell(new Image($item->imageurl)));
            
$cells->add(new TCell($item->category));
            
$cells->add(new TCell($item->itemname));
            
$cells->add(new TCell($item->description));
            
$cells->add(new TCell($item->price));
            
$cells->add(new TCell($itemList->getHelper()->getItemPurchaseForm($this$item)));
            
$itemList->buildRow($cells);
        }      
        
$document->add($itemList);  
    }
  
    public function 
getitem($itemname){
      return new 
StockItem($itemname);
    }
  
    public function 
purchase(Item $item){
        
$mysidia Registry::get("mysidia");
        echo 
$item->eventid;
        if(
$item->owner != $mysidia->user->username) Throw new NoPermissionException('Something is very very wrong, please contact an admin asap.');
        else if((
$this->events != 'none') && ($item->eventid != $this->active_event)) throw new Exception("This item doesn't exist.");//add this
        
else{
            
$item->quantity $mysidia->input->post("quantity");
            
$cost $item->getcost($this->salestax$item->quantity);
            
$moneyleft $mysidia->user->money $cost;
            if(
$moneyleft >= and $item->quantity 0){    
                
$purchase $item->append($item->quantity$item->owner);
                
$mysidia->db->update("users", array("money" => $moneyleft), "username = '{$item->owner}'");            
                
$status TRUE;
            }            
            else throw new 
InvalidActionException($mysidia->lang->money);
        }
        return 
$status;
    }
  
    public function 
rent($item$period){

    }
  
    public function 
execute($action){
    
    }
  
      protected function 
save($field$value){
        
$mysidia Registry::get("mysidia");
        
$mysidia->db->update("shops", array($field => $value), "sid='{$this->sid}' and shoptype = 'adoptshop'");
    }  

   
//add this
   
public function fillEventsArray(){       
       
$i 0;
       
$events explode(','$this->events);
       foreach(
$events as $event_id){
           
$event = new Event($event_id);
           
$this->events_array[$i] = $event;
           
$i++;
       }

   }
   
   
//add this
   
public function checkForEvent(){

       foreach(
$this->events_array as $event){
           if(
$event->isActive()){
               
$this->status 'open';           
               
$this->active_event $event->eid;
               return 
true;
           }
       }
           
    
$this->status 'closed';       
       
$this->active_event = -1;
       return 
false;
   }
   
}
?>
Please note that, with this mod, one shop can have multiple event IDs associated (separated by commas like 1,2,3), but items cannot. Items can only have one event associated or -1 in case they aren't associated with events.

I tested with a few different dates, but if you detect any bugs please tell me.
__________________


asp.net stole my soul.

Last edited by IntoRain; 12-25-2014 at 04:41 PM.
Reply With Quote