From 1b0d3851975cdec4f50623205f7398e231b18afc Mon Sep 17 00:00:00 2001 From: konrad Date: Mon, 5 Jul 2010 15:52:30 +0000 Subject: [PATCH] move from array rendering to more advanced object rendering git-svn-id: https://silmor.de/svn/softmagic/smoke/trunk@527 6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33 --- www/inc/classes/language_manager.php | 6 ++++ www/inc/rendering/autoload.php | 1 + www/inc/rendering/cart_listing.php | 13 ++++++-- www/inc/rendering/event_listing.php | 4 +- www/inc/rendering/twig_extensions.php | 41 ++++++++++++++++++++++++++ www/inc/wext/event.php | 43 ++++------------------------ www/inc/wext/webcart.php | 51 ++++++++++++++------------------ www/index.php | 8 ++++- www/template/en/cart.html | 9 +++-- www/template/en/eventdetails.html | 18 ++++++----- www/template/en/index.html | 8 ++-- www/template/en/layout.html | 10 ++++-- 12 files changed, 118 insertions(+), 94 deletions(-) create mode 100644 www/inc/rendering/twig_extensions.php diff --git a/www/inc/classes/language_manager.php b/www/inc/classes/language_manager.php index ee15a7d..3474172 100644 --- a/www/inc/classes/language_manager.php +++ b/www/inc/classes/language_manager.php @@ -111,6 +111,12 @@ class LanguageManager return date(i18n("Y-m-d"), $date); } + /** returns date in current language, default: ISO-date */ + public function getDateTime($date) + { + return date(i18n("Y-m-d h:i a"), $date); + } + /** returns time in current language */ public function getTime($time) { diff --git a/www/inc/rendering/autoload.php b/www/inc/rendering/autoload.php index bc9e1c6..256c4e4 100644 --- a/www/inc/rendering/autoload.php +++ b/www/inc/rendering/autoload.php @@ -11,5 +11,6 @@ $d=dirname(__FILE__); wob_autoclass("EventRender",$d.'/event_listing.php'); wob_autoclass("WebCart",$d.'/cart_listing.php'); +wob_autoclass("LangFilterExtension",$d.'/twig_extensions.php'); ?> diff --git a/www/inc/rendering/cart_listing.php b/www/inc/rendering/cart_listing.php index d159da9..7610d96 100644 --- a/www/inc/rendering/cart_listing.php +++ b/www/inc/rendering/cart_listing.php @@ -16,17 +16,20 @@ static private $cartid=false; /**contains the name of the cookie and the HTTP parameter for the cart ID*/ const cartIdName = "smoke_cartid"; +/**contains the name for the web UI field that is supposed to contain the amount of tickets*/ +const TicketAmountField = "amountTickets"; + /**called from index.php - add tickets to cart*/ static public function addTickets(){ global $HTTPARGS; //get the cart $cartid=self::getOrCreateCart(); //find event - if(!isset($HTTPARGS['event']) || !isset($HTTPARGS['amountTickets'])) + if(!isset($HTTPARGS['event']) || !isset($HTTPARGS[self::TicketAmountField])) redirectHome(); $evid=$HTTPARGS['event']+0; //find price categories - $pcs=$HTTPARGS['amountTickets']; + $pcs=$HTTPARGS[self::TicketAmountField]; if(!is_array($pcs) || count($pcs)==0) redirectHome(); //go through them @@ -85,7 +88,7 @@ static protected function findCart(){ /**return existing cart or create a new one*/ static public function getOrCreateCart(){ - global $CartTimeout; + global $CartTimeout,$WebDefaultShipping; //try to find it, if found update it self::getCart(); //found? @@ -94,6 +97,8 @@ static public function getOrCreateCart(){ $c=WTcart::newRow(); $exp=time()+$CartTimeout; $c->timeout=$exp; + if($WebDefaultShipping>=0) + $c->shippingtype=$WebDefaultShipping; $c->insert(); //set cookies setCookie(self::cartIdName,$c->cartid,0); @@ -140,7 +145,7 @@ static public function createCartOverview() $p = $twig->loadTemplate("cart.html"); $list=$basevars; - $list["cart"]=$cart->getParserData(); + $list["cart"]=$cart; // create page return $p->render($list); diff --git a/www/inc/rendering/event_listing.php b/www/inc/rendering/event_listing.php index ec39adc..de5cb35 100644 --- a/www/inc/rendering/event_listing.php +++ b/www/inc/rendering/event_listing.php @@ -30,7 +30,7 @@ public static function createEventList() //only show those available via web if(!$session->checkFlags($event->getflags()))continue; //encode as array - $list['events'][]=$event->getParserData(); + $list['events'][]=$event; } //pass 2: create page @@ -56,7 +56,7 @@ public static function createEventDetails() // set event details $list=$basevars; - $list['event']=$event->getParserData(); + $list['event']=$event; // create page return $p->render($list); diff --git a/www/inc/rendering/twig_extensions.php b/www/inc/rendering/twig_extensions.php new file mode 100644 index 0000000..d10644b --- /dev/null +++ b/www/inc/rendering/twig_extensions.php @@ -0,0 +1,41 @@ +, (C) 2010 +// +// Copyright: See README/COPYING files that come with this distribution +// +// + +class LangFilterExtension extends Twig_Extension +{ + private $lang; + + public function getFilters() + { + $this->lang=LanguageManager::singleton(); + return array( + 'asMoney' => new Twig_Filter_Method($this, 'getPrice'), + 'asDate' => new Twig_Filter_Method($this, 'getDate'), + 'asTime' => new Twig_Filter_Method($this, 'getTime'), + 'asDateTime' => new Twig_Filter_Method($this, 'getDateTime'), + ); + } + + public function getPrice($i){return $this->lang->getPrice($i);} + public function getDate($i){return $this->lang->getDate($i);} + public function getTime($i){return $this->lang->getTime($i);} + public function getDateTime($i){return $this->lang->getDateTime($i);} + + public function getName() + { + return 'LangFilter'; + } + +} + +?> \ No newline at end of file diff --git a/www/inc/wext/event.php b/www/inc/wext/event.php index 9eead34..92688a7 100644 --- a/www/inc/wext/event.php +++ b/www/inc/wext/event.php @@ -33,6 +33,12 @@ class WOEventPrice extends WOEventPriceAbstract $cnt++; return $cnt; } + + /**helper for web UI: returns the name of the field for this event price*/ + public function getAmountinputfield() + { + return WebCart::TicketAmountField."[".$this->prop_pricecategoryid."]"; + } }; class WOEvent extends WOEventAbstract @@ -230,43 +236,6 @@ class WOEvent extends WOEventAbstract global $db; $trans->setevents(WOEvent::fromTableArrayevent(WTevent::selectFromDB("starttime<=".$db->escapeInt($start)." AND endtime>=".$db->escapeInt($end)))); } - - /**returns the data in an array suitable for the web-page-renderer*/ - public function getParserData() - { - global $session,$basevars; - $lang = LanguageManager::singleton(); - $ret= $this->propertyArray(); - $ret["date"]=$lang->getDate($this->getstart()); - $ret["time"]=$lang->getTime($this->getstart()); - $ret["place"]=$this->getroom(); - $ret["name"]=$this->gettitle(); - $ret["artistname"]=$this->getartist()->getname(); - $ret["ID"]=$this->getid(); - $ret["availabletickets"]=$this->getamountFree(); - //list all available prices - $ret["prices"] = array(); - foreach($this->getprice() as $price){ - //not those unavailable via web - if(!$session->checkFlags($price->getflags()))continue; - //fill in data - $p=$price->propertyArray(); - $p["price"]=$lang->getPrice($price->getprice()); - $p["pricecents"]=$price->getprice(); - $p["categoryid"]=$price->getpricecategoryid(); - $p["categoryname"]=$price->getpricecategory()->getname(); - //get max tickets, current amount, amount free - $p["maxtickets"]=$price->getmaxavailable(); - $p["ticketsblock"]=$price->getamountticketsblock(); - $p["ticketsavailable"]=($price->getmaxavailable()-$price->getamountticketsblock()); - //little helper for forms - $p["amountinputfield"]=($basevars['inputnames']['amountTickets']."[".$price->getpricecategoryid()."]"); - //copy to my own array - $ret['prices'][]=$p; - } - //return result - return $ret; - } }; ?> \ No newline at end of file diff --git a/www/inc/wext/webcart.php b/www/inc/wext/webcart.php index b6eeb7c..e7809f8 100644 --- a/www/inc/wext/webcart.php +++ b/www/inc/wext/webcart.php @@ -13,42 +13,32 @@ class WOWebCart extends WOWebCartAbstract { - /**returns array data for twig*/ - public function getParserData() + /**returns is empty attribute for twig*/ + public function getIsempty() { - $ret=$this->propertyArray(); - //check whether it is empty - $ret['isempty']=count($this->prop_tickets)==0 && count($this->prop_vouchers)==0 && count($this->prop_items)==0; + return count($this->prop_tickets)==0 && count($this->prop_vouchers)==0 && count($this->prop_items)==0; $prc=0; - //go through tickets and add some data - $lang=LanguageManager::singleton(); - foreach($ret['tickets'] as $id=>$tck){ - $ev=WOEvent::fromTableevent(WTevent::getFromDB($tck['eventid'])); - $ret['tickets'][$id]['event']=$ev->propertyArray(); - $pc=WOEventPrice::fromTableeventprice(WTeventprice::getFromDB($tck['eventid'],$tck['pricecategoryid'])); - $ret['tickets'][$id]['pricecategory']=$pc->propertyArray(); - $p=$pc->getprice(); - $ret['tickets'][$id]['price']=$lang->getPrice($p); - $ret['tickets'][$id]['pricecents']=$p; - $p*=$tck['amount']; - $ret['tickets'][$id]['pricesum']=$lang->getPrice($p); - $ret['tickets'][$id]['pricesumcents']=$p; - $prc+=$p; + } + + /**returns the overall sum for twig*/ + public function getTotalsum() + { + $prc=0; + //go through tickets + foreach($this->gettickets() as $tck){ + $prc+=$tck->getprice()*$tck->getamount(); } //go through vouchers add to sum - foreach($ret['vouchers'] as $id=>$vou){ - $prc+=$vou['value']; - $ret['vouchers'][$id]['valuecents']=$vou['value']; - $ret['vouchers'][$id]['value']=$lang->getPrice($vou['value']); + foreach($this->getvouchers() as $id=>$vou){ + $prc+=$vou->getvalue(); } //TODO: go through items add to sum - //TODO: add shipping option - $ret["shippingid"]=false; - //add sum to return value - $ret['sumcents']=$prc; - $ret['sum']=$lang->getPrice($prc); + //add shipping option + $ship=$this->getshipping(); + if(is_a($ship,"WOShipping")) + $prc+=$ship->getcost(); //return... - return $ret; + return $prc; } /**transaction to add tickets to cart*/ @@ -72,10 +62,12 @@ class WOWebCart extends WOWebCartAbstract $itm=WTcartticket::getFromDB($cartid,$evid,$pcid); if(is_a($itm,"WTcartticket")){ //yes: add to it +// echo "add".$amount; $itm->amount+=$amount; $itm->update(); }else{ //no: add new item +// echo "ins".$amount; $itm=WTcartticket::newRow(); $itm->cartid=$cartid; $itm->eventid=$evid; @@ -83,6 +75,7 @@ class WOWebCart extends WOWebCartAbstract $itm->amount=$amount; $itm->insert(); } +// die ("hallo"); } }; diff --git a/www/index.php b/www/index.php index 8cc6930..f072ca9 100644 --- a/www/index.php +++ b/www/index.php @@ -27,7 +27,11 @@ Session::setWebSession(); //initialize TWIG $loader = new Twig_Loader_Filesystem(LanguageManager::singleton()->templateFolder()); $twig = new Twig_Environment($loader, $twigoptions ); -foreach($twigextensions as $te)$twig->addExtension($te); +foreach($twigextensions as $te){ + $t='Twig_Extension_'.$te; + $twig->addExtension(new $t()); +} +$twig->addExtension(new LangFilterExtension); //basic variables shared by all templates // script URLs @@ -52,7 +56,7 @@ $basevars['inputnames']['mode']="mode"; $basevars['inputnames']['cartid']=WebCart::cartIdName; $basevars['cartcookie']=WebCart::cartIdName; // other info -$basevars['languages']=LanguageManager::getLanguages(); +$basevars['lang']=LanguageManager::singleton(); //strings that are used to compose the overall layout $page="(internal error: no page text yet, probably no template defined)"; diff --git a/www/template/en/cart.html b/www/template/en/cart.html index 0b8c674..8934ac4 100644 --- a/www/template/en/cart.html +++ b/www/template/en/cart.html @@ -16,14 +16,15 @@ {% for ticket in cart.tickets %} - + {% set pricesum=ticket.price * ticket.amount %} + {% endfor %} {% for voucher in cart.vouchers %} - + {% endfor %} - - + +
ItemItem PriceAmountSum
Ticket: {{ticket.event.title}}{{ticket.price}}{{ticket.amount}}{{ticket.pricesum}}
Ticket: {{ticket.event.title}}{{ticket.price|asMoney}}{{ticket.amount}}{{pricesum|asMoney}}
Voucher{{voucher.value}}1{{voucher.value}}
Voucher{{voucher.value}}1{{voucher.value}}
Shipping: {{cart.shipping.name}}{{cart.shipping.price}}1{{cart.shippingprice}}
Sum:{{cart.sum}}
Shipping: {{cart.shipping.description}}{{cart.shipping.cost|asMoney}}1{{cart.shipping.cost|asMoney}}
Sum:{{cart.totalsum|asMoney}}

Checkout

{% endif %} diff --git a/www/template/en/eventdetails.html b/www/template/en/eventdetails.html index e30708d..8bec38f 100644 --- a/www/template/en/eventdetails.html +++ b/www/template/en/eventdetails.html @@ -5,24 +5,26 @@ {% extends 'layout.html' %} -{% block title %}{{event.name}}{% endblock %} +{% block title %}{{event.title}}{% endblock %} {% block page %} -

{{event.name}}

- {{event.date}} {{event.time}}, {{event.place}}
- Artist: {{event.artistname}}
+ {{event.start|asDateTime}}, {{event.room|e}}
+ Artist: {{event.artist.name|e}}
{{event.description}}
-
+ - {% for price in event.prices %} - + {% for price in event.price %} + {% if price.canuse %} + + {% endif %} {% endfor %}
Price:Amount:
{{price.price}} ({{price.categoryname}})
{{price.price|asMoney}} ({{price.pricecategory.name}})

diff --git a/www/template/en/index.html b/www/template/en/index.html index cc56a30..f699354 100644 --- a/www/template/en/index.html +++ b/www/template/en/index.html @@ -11,12 +11,12 @@ {% for event in events %}

{{event.name}}

- {{event.date}} {{event.time}}, {{event.place}}
- Artist: {{event.artistname}}
+ {{event.start|asDate}} {{event.start|asTime}}, {{event.room}}
+ Artist: {{event.artist.name}}
{{event.description}}
Price: - {% for price in event.prices %} - {{price.price}} ({{price.categoryname}}) + {% for price in event.price %} + {{price.price|asMoney}} ({{price.pricecategory.name}}) {% endfor %}
details/order tickets {% endfor %} diff --git a/www/template/en/layout.html b/www/template/en/layout.html index ef8dc34..be1e036 100644 --- a/www/template/en/layout.html +++ b/www/template/en/layout.html @@ -9,11 +9,13 @@

Ticket Shop | -My Shoping Cart | -Language: -{% for lang in languages %} - {{lang}} +My Shoping Cart +{% if lang.languages|length > 1 %} +| Language: +{% for lng in lang.languages %} + {{lng}} {% endfor %} +{% endif %}

-- 1.7.2.5