Orion PHP  0.11.12
The PHP5.3 framework
tags.php
Go to the documentation of this file.
00001 <?php
00002 namespace Orion\Core\Model;
00003 
00004 use \Orion\Core;
00005 
00006 class Tags extends LinkOneMany
00007 {
00008     protected $separator;
00009     protected $namefield;
00010     protected $required;
00011 
00012     //' ', 'TagHandler', 'name', 'counter'
00013 
00014     public function __construct($bind, $label, $model, $separator=',', $namefield='name', $required=false)
00015     {
00016         parent::__construct($bind, $label, $model, 'id');
00017         $this->type = 'tags';
00018         $this->separator = $separator;
00019         $this->namefield = $namefield;
00020         $this->required = $required;
00021     }
00022 
00023     public function onDelete($value)
00024     {
00025     }
00026 
00027     public function onSave($value)
00028     {
00029     }
00030 
00031     public function onUpdate($newvalue, $oldvalue)
00032     {
00033     }
00034 
00035     public function toHtml($XHTML=true)
00036     {
00037         if($XHTML)
00038             $tag = ' /';
00039         else
00040             $tag = '';
00041 
00042         return '<div class="form-row"><label for="'.$this->bind.'">'.$this->label.'</label><div class="form-container"><input name="'.$this->bind.'" type="text" class="form-element form-tags" value="'.$this->value.'"'.$tag.'></div></div>';
00043     }
00044 
00045     /**
00046      * Parse and save/update tags of model into their respective table (defined with PARAM_TAGS)
00047      * @param $data the tag list as a string
00048      * @return boolean success
00049      */
00050     protected function saveTags($data)
00051     {
00052         $pdo = Core\DB::getConnection();
00053 
00054         $tags = explode($this->separator, $data);
00055         if(empty($tags)) return false;
00056         $thClass = $this->model;
00057         $th = new $thClass();
00058         $values = "(".implode('),(', $th->formatArray($th->escapeArray($tags))).")";
00059         $query = "INSERT INTO ".$th->escape($th->getTable())." (".$th->escape($this->namefield).") VALUES ".$values." ON DUPLICATE KEY UPDATE ".$th->escape($this->counterfield)."=".$th->escape($this->counterfield)."+1;";
00060 
00061         try {
00062             $result = $pdo->exec($query);
00063         }
00064         catch(\PDOException $e)
00065         {
00066             throw new Core\Exception($e->getMessage(), $e->getCode(), $this->CLASS_NAME);
00067         }
00068     }
00069 
00070     /**
00071      * Decrease tags counters and delete tag when deleted entry was the latest one using it
00072      * @param $data the tag list as a string
00073      */
00074     protected function removeTags($data)
00075     {
00076         $pdo = Core\Sql::getConnection();
00077 
00078         $tags = explode($this->separator, $data);
00079         if(empty($tags)) return false;
00080         $thClass = $this->model;
00081         $th = new $thClass();
00082         $wstart = $th->escape($this->namefield)."=";
00083         $values = $wstart.implode(' OR '.$wstart, $th->formatArray($th->escapeArray($tags)));
00084         $queryUpd = "UPDATE ".$th->escape($th->getTable())." SET ".$th->escape($this->counterfield)."=".$th->escape($this->counterfield)."-1 WHERE ".$values." ;";
00085         $queryDel = "DELETE FROM ".$th->escape($th->getTable())." WHERE ".$th->escape($this->counterfield)."<1;";
00086 
00087         try {
00088             $resultUpd = $pdo->exec($queryUpd);
00089             $resultDel = $pdo->exec($queryDel);
00090         }
00091         catch(\PDOException $e)
00092         {
00093             throw new Core\Exception($e->getMessage(), $e->getCode(), $this->CLASS_NAME);
00094         }
00095     }
00096 }
00097 
00098 ?>