00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 class aliroTagHandler extends cachedSingleton {
00021 protected static $instance = __CLASS__;
00022 private $alltags = array();
00023 private $tagsByTypeOrder = array();
00024 private $typeByID = array();
00025
00026 protected function __construct () {
00027 $this->alltags = aliroDatabase::getInstance()->doSQLget("SELECT id, ordering, frequency, published, hidden, type, name FROM #__tags ORDER BY ordering", 'stdClass', 'id');
00028 foreach ($this->alltags as $id=>$tag) {
00029 $this->tagsByTypeOrder[$tag->type][] = $id;
00030 $this->typeByID[$id] = $tag->type;
00031 }
00032 }
00033
00034 public static function getInstance () {
00035 return is_object(self::$instance) ? self::$instance : (self::$instance = parent::getCachedSingleton(self::$instance));
00036 }
00037
00038 public function getTypes () {
00039 return array_keys($this->tagsByTypeOrder);
00040 }
00041
00042 public function getTagsOrder ($type) {
00043 $ids = empty($this->tagsByTypeOrder[$type]) ? array() : $this->tagsByTypeOrder[$type];
00044 return $this->findTagObjects($ids);
00045 }
00046
00047 public function getTagsFreq ($type) {
00048 trigger_error('This does not work');
00049 $ids = empty($this->tagsByTypeFreq[$type]) ? array() : $this->tagsByTypeFreq[$type];
00050 return $this->findTagObjects($ids);
00051 }
00052
00053 private function findTagObjects ($ids) {
00054 $result = array();
00055 foreach ($ids as $id) $result[] = isset($this->alltags[$id]) ? $this->alltags[$id] : null;
00056 return $result;
00057 }
00058
00059 public function findTagNames ($ids) {
00060 $objects = $this->findTagObjects($ids);
00061 foreach ($objects as $object) $result[] = is_object($object) ? $object->name : '';
00062 return (isset($result)) ? $result : array();
00063 }
00064
00065 public function namesToIds ($nameList) {
00066 $names = explode(',', $nameList);
00067 $ids = array();
00068 foreach ($names as &$name) $name = trim($name);
00069 foreach ($this->alltags as $id=>$tag) if (in_array($tag->name, $names)) $ids[] = $id;
00070 return implode(',', $ids);
00071 }
00072
00073 public function makeSelectList ($type, $name, $addnone=false, $multiple=false, $nulltext='') {
00074 if (isset($this->tagsByTypeOrder[$type])) {
00075 if ($multiple) {
00076 $multitext = 'multiple="multiple"';
00077 if ('[]' != substr($name,-2)) $name .='[]';
00078 }
00079 else $multitext = '';
00080 if (empty($nulltext)) $nulltext = T_('None of these');
00081 if ($addnone) $optionlist = <<<NULL_OPTION
00082
00083 <option value="0">$nulltext</option>
00084
00085 NULL_OPTION;
00086
00087 else $optionlist = '';
00088 foreach ($this->tagsByTypeOrder as $sub) $optionslist .= <<<TAG_OPTION
00089
00090 <option value="{$this->alltags[$sub]->id}">{$this->alltags[$sub]->name}</option>
00091
00092 TAG_OPTION;
00093
00094 if ($optionlist) return <<<TAG_SELECT
00095
00096 <select name="$name" $multitext>
00097 $optionlist
00098 </select>
00099
00100 TAG_SELECT;
00101
00102 }
00103 return '';
00104 }
00105
00106 public function getAdjacentByOrder ($id, $up=true) {
00107 $keys = $this->getKeysArray($id, $this->tagsByTypeOrder);
00108 $sub = array_search($id, $keys);
00109 return $this->findAdjacent($sub, $keys, $up);
00110 }
00111
00112 public function getAdjacentByFreq ($id, $up=true) {
00113 $keys = $this->getKeysArray($id, $this->tagsByTypeFreq);
00114 $sub = array_search($id, $keys);
00115 return $this->findAdjacent($sub, $keys, $up);
00116 }
00117
00118 private function getKeysArray ($id, $tagsArray) {
00119 if (!isset($this->typeByID[$id]) OR empty($tagsArray[$this->typeByID[$id]])) return array();
00120 return array_keys($tagsArray[$this->typeByID[$id]]);
00121 }
00122
00123 private function findAdjacent ($sub, $keys, $up) {
00124 $adjacent = $sub + ($up ? 1 : -1);
00125 return isset($keys[$adjacent]) ? $adjacent : 0;
00126 }
00127
00128 }