Orion PHP  0.11.12
The PHP5.3 framework
link.php
Go to the documentation of this file.
00001 <?php
00002 namespace Orion\Core\Model;
00003 
00004 use \Orion\Core;
00005 
00006 class Link extends Field
00007 {
00008     protected $model;
00009     protected $rightfield;
00010     protected $rightfieldlabel;
00011 
00012     /**
00013      * Link model field
00014      * @param string $bind
00015      * @param string $label
00016      * @param boolean $primary
00017      */
00018     public function __construct($bind='category', $label='Category', $model='CategoryHandler', $rightfield='id', $rightfieldlabel='name', $required=true, $primary=false)
00019     {
00020         $this->type = 'link';
00021         $this->linked = true;
00022         $this->bind = $bind;
00023         $this->label = $label;
00024         $this->model = $model;
00025         $this->rightfield = $rightfield;
00026         $this->rightfieldlabel = $rightfieldlabel;
00027         $this->required = $required;
00028         $this->primary = $primary;
00029     }
00030 
00031     public function toHtml($XHTML=true)
00032     {
00033         if($XHTML)
00034             $tag = ' /';
00035         else
00036             $tag = '';
00037 
00038         try {
00039             $linkedmodel = $this->model;
00040             $data = $linkedmodel::get($this->rightfield, $this->rightfieldlabel)
00041                                 ->order($this->rightfieldlabel, Core\Query::ASCENDING)
00042                                 ->fetchAll();
00043             
00044             $tmp = '<div class="form-row"><label for="'.$this->bind.'">'.$this->label.'</label><div class="form-container"><select class="form-element" name="'.$this->bind.'">'."\n";
00045             foreach($data as $item)
00046                 $tmp .= '<option value="'.$item->{$this->rightfield}.'"'. ($this->value == $item->{$this->rightfield} ? ' selected="selected"' : '') .'>'.$item->{$this->rightfieldlabel}.'</option>'."\n";
00047             $tmp .= '</select></div></div>';
00048         }
00049         catch(Core\Exception $e)
00050         {
00051             $tmp = $e->toString();
00052         }
00053         
00054         return $tmp;
00055     }
00056     
00057     public function getLinkedTable()
00058     {
00059         $model = $this->model;
00060         return $model::getTable();
00061     }
00062 
00063     public function getModel()
00064     {
00065         return $this->model;
00066     }
00067 
00068     public function getRightfield()
00069     {
00070         return $this->rightfield;
00071     }
00072 
00073     public function getRightfieldlabel()
00074     {
00075         return $this->rightfieldlabel;
00076     }
00077 
00078 }
00079 
00080 ?>