Shop.php:
PHP Code:
<?php
class ShopController extends AppController{
const PARAM = "shop"; private $view; private $subController;
public function __construct(){ parent::__construct("member"); $mysidia = Registry::get("mysidia"); $mysidia->user->getstatus(); if($mysidia->user->status->canshop == "no"){ throw new NoPermissionException($mysidia->lang->denied); } if($mysidia->input->action() != "index" and !$mysidia->input->get("shop")){ throw new InvalidIDException($mysidia->lang->global_id); } } public function index(){ $mysidia = Registry::get("mysidia"); $document = $mysidia->frame->getDocument(); $document->setTitle($mysidia->lang->access); $typeForm = new Form("shoptypes", "shop", "post"); $typeSelection = new DropdownList("shoptype"); $typeSelection->add(new Option("Itemshop", "itemshop")); $typeSelection->add(new Option("Adoptshop", "adoptshop")); $typeForm->add($typeSelection); $typeForm->add(new Button("Go", "submit", "submit")); $document->add($typeForm); $shoplist = new Shoplist($mysidia->input->post("shoptype")); $shoplist->display(); } public function browse(){ $mysidia = Registry::get("mysidia"); $document = $mysidia->frame->getDocument(); $document->setTitle($mysidia->lang->welcome); $shoptype = $mysidia->db->select("shops", array("shoptype"), "shopname = '{$mysidia->input->get("shop")}'")->fetchColumn(); $shoplist = new Shoplist($shoptype); $shop = $shoplist->createshop($mysidia->input->get("shop")); $shop->display(); } public function purchase(){ $mysidia = Registry::get("mysidia"); $document = $mysidia->frame->getDocument(); if(!$mysidia->input->post("buy")) throw new InvalidIDException($mysidia->lang->global_id); if($mysidia->input->post("shoptype") == "itemshop"){ $shop = new Itemshop($mysidia->input->get("shop")); $item = $shop->getitem($mysidia->input->post("itemname")); $item->assign($mysidia->user->username); $oldquantity = $item->getoldquantity(); $newquantity = $oldquantity + $mysidia->input->post("quantity"); if(!is_numeric($mysidia->input->post("quantity"))){ throw new InvalidActionException($mysidia->lang->invalid_quantity); } elseif($newquantity > $item->cap){ throw new InvalidActionException($mysidia->lang->full_quantity); } else{ $shop->purchase($item); $document->setTitle($mysidia->lang->global_transaction_complete); $document->addLangvar("{$mysidia->lang->purchase_item}{$item->getcost($shop->salestax)} {$mysidia->settings->cost} and {$item->getcost2($shop->salestax)} {$mysidia->settings->cost2}."); } } elseif($mysidia->input->post("shoptype") == "adoptshop"){ $adoptobedience = rand(0,100); $shop = new Adoptshop($mysidia->input->get("shop")); $adopt = $shop->getadopt($mysidia->input->post("adopttype")); $adopt->assign($mysidia->user->username); $shop->purchase($adopt); $num = 1; $adoptid = $mysidia->db->select("owned_adoptables", array("aid"), "1 ORDER BY aid DESC LIMIT 1")->fetchColumn() - $num + 1; $personalities = array('Shy', 'Friendly', 'Loyal', 'Bold', 'Courageous', 'Witty', 'Agressive', 'Docile', 'Mischievious', 'Serious', 'Quirky', 'Playful', 'Lazy'); shuffle($personalities); foreach ($personalities as $personality) { $adoptablepersonality = "$personality"; } $adoptobedience = rand(0, 100); $adoptintellect = rand(0,100); $adoptspeed = rand(0,100); $adoptstrength = rand(0,100); $mysidia->db->update("owned_adoptables",array("obedience" => $adoptobedience, "personality" => $adoptablepersonality, "intellect" => $adoptintellect, "speed" => $adoptspeed, "strength" => $adoptstrength),"aid = $adoptid"); $document->setTitle($mysidia->lang->global_transaction_complete); $document->addLangvar("{$mysidia->lang->purchase_adopt}{$adopt->getcost($shop->salestax)} {$mysidia->settings->cost} and {$adopt->getcost2($shop->salestax)} {$mysidia->settings->cost2}."); } else throw new InvalidActionException($mysidia->lang->global_action); } } ?>
class_adoptshop:
PHP Code:
<?php
class Adoptshop extends AdoptContainer{ public $sid; public $category; public $shopname; public $shoptype; public $description; public $imageurl; public $status; public $restriction; public $salestax; public $adopts; 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; }
$this->adopts = $this->getadopttypes(); $this->total = (is_array($this->adopts))?count($this->adopts):0; }
public function getcategory(){ // This method checks if the shop category exists in adoptables database or not $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(){ // This method check if a shop exists in database or not $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 getadopttypes(){ if(!$this->adopts){ // The iids have yet to be loaded, lets acquire their info from database $mysidia = Registry::get("mysidia"); $stmt = $mysidia->db->join("adoptables_conditions", "adoptables_conditions.id = adoptables.id") ->select("adoptables", array(), constant("PREFIX")."adoptables.shop ='{$this->shopname}'"); $adopts = array(); while($adopt = $stmt->fetchObject()){ $aid = $row->id; $promocode = ""; if(canadopt($aid, "showing", $promocode, $row)) $adopts[] = $adopt; } return $adopts; } else return $this->adopts; } public function gettotal(){ // This method returns the total number of adoptables available return $this->total; } public function display(){ // This method displays the shoplist to users. It may look messy at this moment, but not after we create a table class $mysidia = Registry::get("mysidia"); $document = $mysidia->frame->getDocument(); $document->addLangvar($mysidia->lang->select_adopt); if($this->gettotal() == 0){ $document->addLangvar($mysidia->lang->empty); return FALSE; } $adoptList = new TableBuilder("shop"); $adoptList->setAlign(new Align("center", "middle")); $adoptList->buildHeaders("Image", "Class", "Type", "Description", "Fuzz Puff Price", "Dream Price", "Buy"); $adoptList->setHelper(new ShopTableHelper); $this->adopts = $this->getadopttypes(); foreach($this->adopts as $stockadopt){ $adopt = $this->getadopt($stockadopt->type); $image = new TCell($this->getadoptimage($adopt->eggimage)); $class = new TCell($adopt->class); $type = new TCell($adopt->type); $description = new TCell($adopt->description); $price = new TCell($adopt->cost); $price2 = new TCell($adopt->cost2); $buy = new TCell($adoptList->getHelper()->getAdoptPurchaseForm($this, $adopt)); $adoptList->buildRow(array($image, $class, $type, $description, $price, $price2, $buy)); } $document->add($adoptList); } public function getadopt($id){ // This method retrieves a new Stock adoptable return new StockAdopt($id); } public function purchase($adopt){ // This method carries out the purchase transaction of an adoptable object $mysidia = Registry::get("mysidia"); if($adopt->owner != $mysidia->user->username) Throw new NoPermissionException('Something is very very wrong, please contact an admin asap.'); else{ $cost = $adopt->getcost($this->salestax); $cost2 = $adopt->getcost2($this->salestax); $moneyleft = $mysidia->user->money - $cost; $money2left = $mysidia->user->money2 - $cost2; if($moneyleft >= 0 && $money2left>= 0){ $purchase = $adopt->append($adopt->owner); $mysidia->db->update("users", array("money" => $moneyleft, "money2" => $money2left), "username = '{$adopt->owner}'"); $status = TRUE; } else throw new InvalidActionException($mysidia->lang->money); } return $status; } public function rent($adopt, $period){ // This method will be added in future } public function execute($action){ // This method will be added in future } } ?>
class_itemshop:
PHP Code:
<?php
class Itemshop extends ItemContainer{ public $sid; public $category; public $shopname; public $shoptype; public $description; public $imageurl; public $status; public $restriction; public $salestax; 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; }
$this->items = $this->getitemnames(); $this->total = (is_array($this->items))?count($this->items):0; }
public function getcategory(){ // This method checks if the shop category exists in items database or not $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(){ // This method check if a shop exists in database or not $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){ // The iids have yet to be loaded, lets acquire their info from database $mysidia = Registry::get("mysidia"); $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(){ // This method returns the total number of item types available return $this->total; } public function display(){ // This method displays the shoplist to users. It may look messy at this moment, but not after we create a table class $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", "Fuzz Puff Price", "Dream Price", "Buy"); $itemList->setHelper(new ShopTableHelper); foreach($this->items as $stockitem){ $item = $this->getitem($stockitem); $image = new TCell($this->getitemimage($item->imageurl)); $category = new TCell($item->category); $itemname = new TCell($item->itemname); $description = new TCell($item->description); $price = new TCell($item->price); $price2 = new TCell($item->price2); $buy = new TCell($itemList->getHelper()->getItemPurchaseForm($this, $item)); $itemList->buildRow(array($image, $category, $itemname, $description, $price, $price2, $buy)); } $document->add($itemList); } public function getitem($itemname){ // This method retrieves a new Stock item return new StockItem($itemname); } public function purchase(Item $item){ // This method carries out the purchase transaction of an item object $mysidia = Registry::get("mysidia"); if($item->owner != $mysidia->user->username) Throw new NoPermissionException('Something is very very wrong, please contact an admin asap.'); else{ $item->quantity = $mysidia->input->post("quantity"); $cost = $item->getcost($this->salestax, $item->quantity); $cost2 = $item->getcost2($this->salestax, $item->quantity); $moneyleft = $mysidia->user->money - $cost; $money2left = $mysidia->user->money2 - $cost2; if($moneyleft >= 0 and $money2left >= 0 and $item->quantity > 0){ $purchase = $item->append($item->quantity, $item->owner); $mysidia->db->update("users", array("money" => $moneyleft, "money2" => $money2left), "username = '{$item->owner}'"); $status = TRUE; } else throw new InvalidActionException($mysidia->lang->money); } return $status; } public function rent($item, $period){ // This method will be added in future } public function execute($action){ // This method will be added in future } } ?>
__________________
|