Source for file DefaultWidget.php

Documentation is available at DefaultWidget.php

  1. <?php
  2. class DefaultWidget extends WidgetGenerator{
  3.  
  4.     public function generate($columns){
  5.     $buffer='<?php
  6. /**
  7.  * This file was generated by the Alia Toolkit. For more information, please see alia.sourceforge.net
  8.  */
  9.  
  10.  
  11.  
  12. /**
  13.  * '.CRUD_WIDGET.' class
  14.  * 
  15.  * A widget for manipulating a '.CRUD_CLASS.' object.
  16.  * @uses AWidget
  17.  */
  18. class '.CRUD_WIDGET.' extends AWidget{
  19.     static $recordClass=\''.CRUD_CLASS.'\';
  20.     
  21.     /**
  22.      * The records primary key. This is used to repopulate the widget on wakeup 
  23.      * 
  24.      * @var mixed
  25.      * @access private
  26.      */
  27.     private $primaryKey=null;
  28.  
  29.     
  30.     /**
  31.      * the AValidator object used for validation 
  32.      * 
  33.      * @var AValidator
  34.      * @access private
  35.      */
  36.     private $validator;
  37.  
  38.  
  39.     /**
  40.      * The actual doctrine record object that will be viewed/edited by this widget 
  41.      * 
  42.      * @var mixed
  43.      */
  44.     private $record=null;
  45.  
  46.  
  47.  
  48.  
  49.     function __construct($_record=null){
  50.         
  51.         $this->defineSignal("saveSucess");
  52.         $this->defineSignal("saveFail",1);
  53.  
  54.         if(!$_record){
  55.             $_record = new '.CRUD_CLASS.'();
  56.         }
  57.         $this->record =$_record;
  58.         $this->primaryKey=$_record->identifier();
  59.         parent::__construct();
  60.         
  61.         //define our saveClicked signal
  62.         $this->defineSignal("saveClicked");
  63.         
  64.         //instantiate the validator
  65.         $this->validator = new AValidator();
  66.  
  67.  
  68.         $this->buildLayout();
  69.         $this->setupConnections();
  70.         $this->validator->sendJScript();
  71.     }
  72.  
  73.  
  74.  
  75.     /**
  76.      * Magic method is executed whenever the object is unserialized. 
  77.      * 
  78.      * @access protected
  79.      * @return void
  80.      */
  81.     function __wakeup(){
  82.         if($this->primaryKey){
  83.             //we have to re-fetch the record on wakeup because doctrine doesn\'t serialize relationships
  84.             $this->record = Doctrine::getTable("'.CRUD_CLASS.'")->find($this->primaryKey);
  85.         }
  86.         if(!$this->record){
  87.             $this->record = new '.CRUD_CLASS.'();
  88.         }
  89.     }
  90.     
  91.     function __sleep(){
  92.         return array("validator","primaryKey");
  93.     }
  94.     
  95.     
  96.     /**
  97.      * builds the layout and widgets that go in it. 
  98.      * 
  99.      * @access public
  100.      * @return void
  101.      */
  102.     function buildLayout(){
  103.         $layout = new AHTMLLayout($this,"/templates/'.CRUD_WIDGET.'Edit.tpl");
  104.         $this->setLayout($layout);
  105.         $x=0;
  106. ';
  107.         
  108. foreach($columns as $column=>$vals){
  109.     $buffer.="\n\n".'        //'."The '$column' widget";
  110.     switch($vals['type']){
  111.         case 'integer':
  112.             $buffer.="\n".'        $widget=new ALineEdit($this->record->'.$column.');';
  113.             $buffer.="\n".'        $widget->setAttribute("maxlength",'.$vals['length'].');';
  114.             $buffer.="\n".'        $widget->addAttribute("name")->setAttribute("name","'.$column.'");';
  115.             $buffer.="\n".'        $layout->addWidget($widget, "'.$column.'");';
  116.             $buffer.="\n".'        $this->validator->add($x++,"int",null,$widget->getAttribute("id"));';
  117.             break;
  118.         case 'string':
  119.             $buffer.="\n".'        $widget=new ALineEdit($this->record->'.$column.');';
  120.             $buffer.="\n".'        $widget->setAttribute("maxlength",'.$vals['length'].');';
  121.             $buffer.="\n".'        $widget->addAttribute("name")->setAttribute("name","'.$column.'");';
  122.             $buffer.="\n".'        $layout->addWidget($widget, "'.$column.'");';
  123.             $buffer.="\n".'        $this->validator->add($x++,"string",array("maxLength"=>'.$vals['length'].'), $widget->getAttribute("id"));';
  124.  
  125.             
  126.             break;
  127.         default:
  128.             $buffer.="\n\n".'        $layout->addWidget(new ALineEdit($this->record->'.$column.'), "'.$column.'");';
  129.             break;
  130.     }
  131. }
  132.  
  133. $buffer.='
  134.         
  135.         //add a save button
  136.         $saveButton = new APushButton("Save");
  137.         $layout->addWidget($saveButton,"_saveButton");
  138.     }
  139.     
  140.  
  141.  
  142.  
  143.     /**
  144.      * sets up the signal and default connection for saving the form. 
  145.      * 
  146.      * @access public
  147.      * @return void
  148.      */
  149.     function setupConnections(){
  150.         $button=$this->getLayout()->getWidget("_saveButton");
  151.         
  152.         //emit a saveClicked signal when the user hits save. The signal passes all form information to all slots.
  153.         Alia::connect($button,"clicked", null, null, "" . AJScript::emit("saveClicked",$this,array(';
  154.     $x=0;
  155.     foreach($columns as $column=>$vals){
  156.     if($x++)$buffer.=',';
  157. $buffer.="\n\t\t\t".'AJScript::formElementValue($this->getLayout()->getWidget("'.$column.'"))';
  158. }
  159. $buffer.=')));
  160.         
  161.         //connect the save() method to the saveClicked signal
  162.         Alia::connect($this,"saveClicked",$this,"save");
  163.         
  164.     }
  165.  
  166.  
  167.  
  168.     /**
  169.      * Saves the record to the database
  170.      *
  171.      * Called when the user hits save button by default.
  172.      */
  173.     function save(';
  174.     $x=0;
  175. foreach($columns as $column=>$vals){
  176.     if($x++)$buffer.=',';
  177.     $buffer.='$'.$column;
  178. }
  179. $buffer.='){
  180.         $args = func_get_args();
  181.         $result=$this->validator->validate($args);
  182.         if(is_array($result)){
  183.             $this->emit("saveFail", $result);
  184.             return false;
  185.         }
  186.         if($this->record){
  187.             ';
  188. foreach($columns as $column=>$vals){
  189.     switch($vals['type']){
  190.         case 'integer':
  191.             $buffer.="\n".'            $this->record->'.$column.'=(int)$'.$column.';';
  192.             break;
  193.         default:
  194.             $buffer.="\n".'            $this->record->'.$column.'=$'.$column.';';
  195.     }
  196. }
  197.  
  198. $buffer.='
  199.             $this->record->save();';
  200.             if($key=getPrimaryKey($columns)){
  201.                 $buffer.="\n".'            $this->primaryKey = $this->record->'.$key.';'."\n";
  202.             }
  203. $buffer.='            $this->emit("saveSuccess");
  204.         }
  205.     }
  206.  
  207.  
  208. }
  209. ';
  210.  
  211.         return $buffer;    
  212.     
  213.     }
  214. }

Documentation generated on Fri, 11 Jul 2008 13:28:45 -0400 by phpDocumentor 1.4.2