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 $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(){
$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");
$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();
if($this->gettotal() == 0){
$document->addLangvar($mysidia->lang->empty);
return FALSE;
}
# Choose the NPC image based on the name of the shop
switch ($this->shopname) {
case "Mais Stand":
$npc_img = "http://i1346.photobucket.com/albums/p696/parayna/MaiForShop_zpse0665685.png";
#Based on the random number, sets a quote for $npc_text
$num = Rand (1,6);
switch ($num) {
case 1: $npc_text = "Why hello there!"; break;
case 2: $npc_text = "Fruits and vegetables are my expertise."; break;
case 3: $npc_text = "Welcome to {$this->shopname}!"; break;
case 4: $npc_text = "Have you checked out all the features yet? I sure hope so!"; break;
case 5: $npc_text = "My name is Mai! How can I help you?"; break;
case 6: $npc_text = "'Tis the season to be jolly, falalalalaaaaa lala lala~"; }
break;
case "Spectrum":
$npc_img = "http://fc00.deviantart.net/fs70/f/2014/313/a/c/base_npc_2_60_by_kyttias-d85ww5k.png";
#Based on the random number, sets a quote for $npc_text
$num = Rand (1,6);
switch ($num) {
case 1: $npc_text = "Time is money."; break;
case 2: $npc_text = "Isn't icecream super?"; break;
case 3: $npc_text = "Welcome to {$this->shopname}!"; break;
case 4: $npc_text = "Off to see the wizard?"; break;
case 5: $npc_text = "Tomorrow is another day..."; break;
case 6: $npc_text = "PHP is cool!"; }
break;
default;
$npc_img = "http://placekitten.com/g/200/500";
$npc_text = "Welcome to {$this->shopname}!";
break;
}
# let's begin rendering the page
$document->add(new Comment("
<style>
.s_top {
overflow:hidden;
display: block;
}
.sc_npc_text{
width: 40%;
float: left;
height: 100%;
padding: 10px;
margin: 10px;
font-family: 'Trebuchet MS', Helvetica, sans-serif;
}
.sc_npc_img{
width: 40%;
float: left;
}
.sc_item {
display: inline-block;
padding: 5px;
text-align: center;
font-family: 'Trebuchet MS', Helvetica, sans-serif;
font-size: 14px;
margin-bottom: 1px;
}
.s_panel {
border-radius: 4px;
border: 1px solid #466ec3;
background-color: #ccf3f6;
}
.s_input { width: 30px; }
</style>
<!-- START Container for Text and NPC -->
<div class='s_top s_container'>
<div class='s_panel sc_npc_text'><p>{$npc_text}</p></div>
<div class='sc_npc_img'><img src='{$npc_img}' height='450' border='5'></div>
</div>
<!-- END Container for Text and NPC -->
<!-- START Container for Items -->
<div class='s_container'>
", FALSE));
# Now render each item the store has
foreach($this->items as $stockitem){
$item = $this->getitem($stockitem);
#descriptions of the item functions
switch ($item->function) {
case "Click1": $usage = "<b>use:</b> Feed a pet to give them {$item->value} EXP.";
break;
case "Click2": $usage = "<b>use:</b> Feed a pet to set their EXP to {$item->value}.";
break;
case "Click3": $usage = "<b>use:</b> Resets today's earned EXP to 0.";
break;
case "Level1": $usage = "<b>use:</b> Raises a pet's Level by {$item->value}.";
break;
case "Level2": $usage = "<b>use:</b> Sets your pet's Level to {$item->value}.";
break;
case "Level3": $usage = "<b>use:</b> Makes your pet Level 0 again!";
break;
case "Gender": $usage = "<b>use:</b> Swaps your pet's gender to its opposite!";
break;
default;
$usage = "";
break;
}
# Now let's render each item icon, name, price and tooltip
$document->add(new Comment("
<div class='s_panel sc_item'>
<!--
// This is where I would get the tooltip working, but it's not currently
<abbr rel='tooltip' title=\"{$item->description} <em>{$usage}</em>.\"><img src='{$item->imageurl}'></abbr>
-->
<img src='{$item->imageurl}'>
<br/>
<b>{$item->itemname}</b>
<br/>
{$item->price} Credits<br/>
", FALSE));
# Building the Buy form
$buyForm = new FormBuilder("buyform", "../purchase/{$mysidia->input->get("shop")}", "post");
$buyForm->setLineBreak(FALSE);
$buyForm->buildPasswordField("hidden", "action", "purchase")
->buildPasswordField("hidden", "itemname", $item->itemname)
->buildPasswordField("hidden", "shopname", $shop->shopname)
->buildPasswordField("hidden", "shoptype", "itemshop")
->buildPasswordField("hidden", "salestax", $shop->salestax);
$quantity = new TextField("quantity");
$quantity->setSize(3);
$quantity->setMaxLength(3);
$quantity->setLineBreak(FALSE);
$buy = new Button("Buy", "buy", "buy");
$buy->setLineBreak(FALSE);
$buyForm->add($quantity);
$buyForm->add($buy);
# Actually adding in the Quantity field Buy button now
$document->add($buyForm);
# Now we finish off the item by closing its div
$document->add(new Comment("</div>", FALSE));
}
# And that's a wrap
$document->add(new Comment("</div><!-- END Container for Items -->", FALSE));
}
public function getitem($itemname){
return new StockItem($itemname);
}
public function purchase(Item $item){
$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);
$moneyleft = $mysidia->user->money - $cost;
if($moneyleft >= 0 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'");
}
}
?>
I will be removing the CSS part though and adding them to my themes as then I can see about making the colours fit each different coloured theme I have ^_^