move from array rendering to more advanced object rendering
authorkonrad <konrad@6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33>
Mon, 5 Jul 2010 15:52:30 +0000 (15:52 +0000)
committerkonrad <konrad@6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33>
Mon, 5 Jul 2010 15:52:30 +0000 (15:52 +0000)
git-svn-id: https://silmor.de/svn/softmagic/smoke/trunk@527 6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33

12 files changed:
www/inc/classes/language_manager.php
www/inc/rendering/autoload.php
www/inc/rendering/cart_listing.php
www/inc/rendering/event_listing.php
www/inc/rendering/twig_extensions.php [new file with mode: 0644]
www/inc/wext/event.php
www/inc/wext/webcart.php
www/index.php
www/template/en/cart.html
www/template/en/eventdetails.html
www/template/en/index.html
www/template/en/layout.html

index ee15a7d..3474172 100644 (file)
@@ -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)
        {
index bc9e1c6..256c4e4 100644 (file)
@@ -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');
 
 ?>
index d159da9..7610d96 100644 (file)
@@ -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);
index ec39adc..de5cb35 100644 (file)
@@ -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 (file)
index 0000000..d10644b
--- /dev/null
@@ -0,0 +1,41 @@
+<?
+//
+// PHP Implementation: additional Twig Filters
+//
+// Description: 
+//
+//
+// Author: Konrad Rosenbaum <konrad@silmor.de>, (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
index 9eead34..92688a7 100644 (file)
@@ -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
index b6eeb7c..e7809f8 100644 (file)
 
 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");
        }
 };
 
index 8cc6930..f072ca9 100644 (file)
@@ -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)";
index 0b8c674..8934ac4 100644 (file)
   <table frame="1" border="1">
   <tr><td><b>Item</b></td><td><b>Item Price</b></td><td><b>Amount</b></td><td><b>Sum</b></td></tr>
   {% for ticket in cart.tickets %}
-  <tr><td>Ticket: {{ticket.event.title}}</td><td>{{ticket.price}}</td><td>{{ticket.amount}}</td><td>{{ticket.pricesum}}</td></tr>
+  {% set pricesum=ticket.price * ticket.amount %}
+  <tr><td>Ticket: {{ticket.event.title}}</td><td align="right">{{ticket.price|asMoney}}</td><td align="right">{{ticket.amount}}</td><td align="right">{{pricesum|asMoney}}</td></tr>
   {% endfor %}
   {% for voucher in cart.vouchers %}
-  <tr><td>Voucher</td><td>{{voucher.value}}</td><td>1</td><td>{{voucher.value}}</td></tr>
+  <tr><td>Voucher</td><td align="right">{{voucher.value}}</td><td align="right">1</td><td align="right">{{voucher.value}}</td></tr>
   {% endfor %}
   
-  <tr><td>Shipping: {{cart.shipping.name}}</td><td>{{cart.shipping.price}}</td><td>1</td><td>{{cart.shippingprice}}</td></tr>
-  <tr><td colspan="3" align="right"><b>Sum:</b></td><td>{{cart.sum}}</td></tr>
+  <tr><td>Shipping: {{cart.shipping.description}}</td><td align="right">{{cart.shipping.cost|asMoney}}</td><td align="right">1</td><td align="right">{{cart.shipping.cost|asMoney}}</td></tr>
+  <tr><td colspan="3" align="right"><b>Sum:</b></td><td>{{cart.totalsum|asMoney}}</td></tr>
   </table>
   <p><a href="{{script.checkout}}">Checkout</a></p>
  {% endif %}
index e30708d..8bec38f 100644 (file)
@@ -5,24 +5,26 @@
 
 {% extends 'layout.html' %}
 
-{% block title %}{{event.name}}{% endblock %}
+{% block title %}{{event.title}}{% endblock %}
 
 {% block page %}
 
-  <h2>{{event.name}}</h2>
-  {{event.date}} {{event.time}}, {{event.place}}<br/>
-  Artist: {{event.artistname}}<br/>
+  {{event.start|asDateTime}}, {{event.room|e}}<br/>
+  Artist: {{event.artist.name|e}}<br/>
   {{event.description}}<br/>
-  <form action="{{script.eventOrder}}{{event.ID}}" method="POST">
+  <form action="{{script.eventOrder|safe}}{{event.ID|safe}}" method="POST">
   <table>
   <tr><td>Price:</td><td>Amount:</td></tr>
-  {% for price in event.prices %}
-   <tr><td>{{price.price}} ({{price.categoryname}})</td>
+  {% for price in event.price %}
+   {% if price.canuse %}
+   <tr><td>{{price.price|asMoney}} ({{price.pricecategory.name}})</td>
    <td><select name="{{price.amountinputfield}}">
-    {% for i in 0 .. price.ticketsavailable %}
+    {% set ticketsavailable=price.maxavailable - price.amountticketsblock %}
+    {% for i in 0 .. ticketsavailable %}
       <option>{{i}}</option>
     {% endfor %}
     </select></td></tr>
+   {% endif %}
   {% endfor %}</table><br/>
   <input type="submit" value="add to cart" />
   </form>
index cc56a30..f699354 100644 (file)
 
  {% for event in events %}
   <h2>{{event.name}}</h2>
-  {{event.date}} {{event.time}}, {{event.place}}<br/>
-  Artist: {{event.artistname}}<br/>
+  {{event.start|asDate}} {{event.start|asTime}}, {{event.room}}<br/>
+  Artist: {{event.artist.name}}<br/>
   {{event.description}}<br/>
   Price: 
-  {% for price in event.prices %}
-   {{price.price}} ({{price.categoryname}})
+  {% for price in event.price %}
+   {{price.price|asMoney}} ({{price.pricecategory.name}})
   {% endfor %}<br/>
   <a href="{{script.eventDetails}}{{event.ID}}">details/order tickets</a>
  {% endfor %}
index ef8dc34..be1e036 100644 (file)
@@ -9,11 +9,13 @@
 <!-- Begin Menu -->
 <p align="right">
 <a href="{{script.root}}">Ticket Shop</a> |
-<a href="{{script.mycart}}">My Shoping Cart</a> |
-Language:
-{% for lang in languages %}
- <a href="{{script.setlanguage}}{{lang}}"><img src="images/{{lang}}.png" alt="{{lang}}"/></a>
+<a href="{{script.mycart}}">My Shoping Cart</a>
+{% if lang.languages|length > 1 %}
+| Language:
+{% for lng in lang.languages %}
+ <a href="{{script.setlanguage}}{{lng}}"><img src="images/{{lng}}.png" alt="{{lng}}"/></a>
 {% endfor %}
+{% endif %}
 </p>
 <!-- End Menu -->