Skip to content

Instantly share code, notes, and snippets.

@hakunin
Created December 12, 2010 17:15
Show Gist options
  • Save hakunin/738189 to your computer and use it in GitHub Desktop.
Save hakunin/738189 to your computer and use it in GitHub Desktop.
<?php
class BasketWidget {
public static function view(Perspective $perspective) {
$b = Basket::current();
if ($b->isEmpty()) {
$items = '<a href="'.Basket::url().'">Košík</a> je prázdný';
} else {
$items = 'V <a href="'.Basket::url().'">košíku</a> máte<br> <strong>'.$b->itemCount().'</strong> položky
<p>za <span class="cena_overall">'.$b->sumItems().'&nbsp;€</span></p>';
}
return '
<div id="kosik">
<div id="kosik_text">
'.$items.'
</div>
</div>';
}
}
MainController::$directUrls['kosik'] = 'BasketController';
class BasketController {
public function execute($controller) {
$controller->handlePost(Perspective::getActual());
$b = Basket::current();
if (array_key_exists('remove', $_GET)) {
$bi = new BasketItem();
$bi->productId = $_GET['remove'];
$b->removeItem($bi);
}
if ($_GET['step'] === "2") {
$html = $this->chooseOrderDetails();
} else if ($_GET['step'] === "3") {
$html = $this->confirmOrder($b);
} else {
$html = $this->showBasket($b);
}
$html = '<div class="textPage">'.$html.'</div>';
Response::set('content', $html);
return $controller->output();
}
public function showBasket(Basket $b) {
$items = '';
foreach ($b->getItems() as $item) {
if (array_key_exists('amount'.$item->productId, $_POST)) {
$amount = $_POST['amount'.$item->productId];
$b->setAmount($item, $amount);
}
}
foreach ($b->getItems() as $item) {
// můžeš psát, můžeš stylovat :), ale stránky netrhat
$items .= "<tr><td class=\"name\">".$item->title.
"&nbsp;(<a href=\"?remove=".$item->productId."\" class=\"remove\">x</a>)".
"<img src=\"".$item->image."\" /></td><td>".
$item->description."</td><td>".($item->price * $item->amount)."&nbsp;€</td><td>".
"<input type=\"text\" name=\"amount".$item->productId."\" size=\"2\" value=\"".$item->amount."\" /></td></tr>";
}
$b->persist();
return '<form method="post">
<h1>Košík</h1>
<input type="submit" value="Přepočítat" style="display:none"/>
<table class=\"basket\">
'.$items.'
</table>
<input type="submit" value="Přepočítat" class="recount"/>
</form>
<form action="'.Basket::url().'" method="get">
<input type="hidden" value="2" name="step" />
<input type="submit" value="Vybrat způsob platby a dopravu &raquo;" class="purchase"/>
</form>
';
}
public function chooseOrderDetails() {
return '<form action="'.Basket::url().'" method="get">
<input type="hidden" value="3" name="step" />
<select name="shipment">
<option>A</option>
<option>B</option>
<option>C</option>
</select>
<input type="submit" value="Potvrzuji objednávku &raquo;" class="purchase"/>
</form>';
}
public function confirmOrder(Basket $b) {
$b->shipment = $_GET['shipment'];
$b->save();
return '<h1>Děkujeme!</h1>
<p>.. sem přijde info o tom, co se bude dít dál ..</p>
';
}
}
class Basket extends Node
{
private $items = array();
protected $items_json;
public $shipment;
public function getItems() {
return $this->items;
}
public static function url() {
return 'http://'.Perspective::getActual()->getSiteUrl().'kosik/';
}
public static function current() {
if ($id = Session::get('basket_id')) {
$b = Tree::getNodeById($id, 'basket');
} else {
$b = Basket::fromSession($session);
}
return $b;
}
public function toJson() {
return json_encode($this->toArray());
}
public function toArray() {
$array = array(
'items' => array() // itemy strcim sem
);
foreach ($this->items as $item) {
$array['items'][]= $item->toArray();
}
$array['shipment'] = $this->shipment; // pridal jsem do pole, ktere reprezentuje kosik shipment
return $array;
}
public static function fromJson($json) {
return Basket::fromArray(json_decode($json, true));
}
public static function fromArray($array) {
$b = new Basket();
foreach ($array['items'] as $itemArray) { // itemy jsou ted v tehle casti pole
$item = BasketItem::fromArray($itemArray);
$b->addItem($item, $item->amount);
}
$this->shipment = $array['shipment']; // shipment je zas tu
// je videt o co jde? jj obecne to chapu, mohl bys mi to prosim zkopirovat
return $b;
}
public function persist() {
$this->persistToSession();
}
public function persistToSession() {
Session::store('basket', $this->toJson());
}
public static function fromSession() {
if ($json = Session::get('basket')) {
return Basket::fromJson($json);
} else {
return new Basket();
}
}
public function save() {
if ($this->items) {
$this->items_json = $this->toJson();
}
return parent::save();
}
public function addItem(BasketItem $item, $amount) {
if ($amount < 1) {
return;
}
if ($bi = $this->item($item)) {
$this->items[$item->productId] = $item;
$item->amount = $bi->amount + $amount;
return $bi;
} else {
$this->items[$item->productId] = $item;
$item->amount = $amount;
return $item;
}
}
public function setAmount(BasketItem $item, $amount) {
if ($bi = $this->item($item)) {
if ($amount > 0) {
$bi->amount = $amount;
} else {
$this->removeItem($bi);
}
} else {
throw new Exception('Item ');
}
}
public function removeItem(BasketItem $item) {
if ($this->contains($item)) {
$this->items = Basket::array_remove_key($this->items, $item->productId);
}
}
private static function array_remove_key() {
$args = func_get_args();
return array_diff_key($args[0],array_flip(array_slice($args,1)));
}
public function contains(BasketItem $item) {
return array_key_exists($item->productId, $this->items);
}
public function item(BasketItem $item) {
if ($this->contains($item)) {
return $this->items[$item->productId];
}
return null;
}
public function itemCount() {
$amount = 0;
foreach ($this->items as $item) {
$amount += $item->amount;
}
return $amount;
}
public function isEmpty() {
return $this->itemCount() == 0;
}
public function sumItems() {
$sum = 0;
foreach($this->items as $item) {
$sum += $item->price * $item->amount;
}
return $sum;
}
public function typeName() { return "Nákupní košík"; }
public function isManagable() { return true; }
static function createMapping() {
$m = new ClassMapping('basket');
$m->add(new IntegerMapping('id'));
$m->add(new IntegerMapping('customerId'));
$m->add(new StringMapping('items_json'));
$m->add(new StringMapping('finalPrice'));
return $m;
}
function onInstall() {
}
function beforeSave() {
}
}
include('basket_insert_action.php');
class BasketForm extends NodeForm
{
function __construct()
{
parent::__construct();
$name = $this->add(new StringField('Označení košíku','node/name'));
$name->addFilter(new NotEmptyFilter('Název musí být vyplňěn.'));
$this->add(new LocationField('Umístění','node/parentId'));
$this->add(new StringField('ID zákazníka','node/customerId'));
//$this->add(new StringField('ID Session','node/sessionId'));
//$this->add(new StringField('Počet položek v košíku','node/items'));
$this->add(new IntegerField('Konečná cena','node/finalPrice'));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment