/installer/files/app/models/tag_list.php

https://github.com/akelos/acts-as-taggable · PHP · 248 lines · 233 code · 13 blank · 2 comment · 39 complexity · fba4584b2afe5647bd1264f13bbf513b MD5 · raw file

  1. <?php
  2. class TagList extends AkObject
  3. {
  4. var $_tags = array();
  5. var $_cachedTagNames = array();
  6. var $_cachedSafeTagNames = array();
  7. var $_cacheId = null;
  8. var $_cachedTagString = '';
  9. var $_newTagNames = array();
  10. var $_removeTagNames = array();
  11. var $_separator = ',';
  12. function __construct()
  13. {
  14. $args = func_get_args();
  15. if(count($args)>0) {
  16. call_user_func_array(array(&$this,'addTags'),$args);
  17. }
  18. }
  19. function setSeparator($sep)
  20. {
  21. $this->_separator = $sep;
  22. }
  23. function _generateTagNames()
  24. {
  25. $tagHelper = new Tag();
  26. $safeTagNames = array();
  27. $tagNames = array();
  28. foreach ($this->_tags as $tag) {
  29. $name = $tag->name;
  30. $tagNames[] = $name;
  31. }
  32. foreach ($this->_newTagNames as $newTag) {
  33. //$name = $this->_sanitizeTagName($newTag);
  34. $tagNames[] = $newTag;
  35. }
  36. sort($tagNames);
  37. $tagNames = array_unique($tagNames);
  38. $tagNames = array_diff($tagNames, $this->_removeTagNames);
  39. $slugHelper = new ActsAsSluggable(&$tagHelper,array('slug_source'=>'name','slug_target'=>'slug'));
  40. $tagHelper->sluggable = &$slugHelper;
  41. foreach ($tagNames as $tname) {
  42. $tagHelper->name = $tname;
  43. $safeTagNames[]=$slugHelper->_generateSlug(&$tagHelper);
  44. }
  45. $this->_cachedTagNames = array_values($tagNames);
  46. $this->_cachedSafeTagNames = array_values($safeTagNames);
  47. $this->_cacheId = md5(implode(',',$tagNames));
  48. }
  49. function _generateCachedTagString()
  50. {
  51. $items = array();
  52. foreach ($this->_cachedTagNames as $tag) {
  53. $items[] = $this->_sanitizeTagName($tag);
  54. }
  55. $this->_cachedTagString = implode($this->_separator,$items);
  56. }
  57. function _generateCache()
  58. {
  59. $this->_generateTagNames();
  60. $this->_generateCachedTagString();
  61. }
  62. function toString()
  63. {
  64. if (null == $this->_cacheId) {
  65. $this->_generateCache();
  66. }
  67. return $this->_cachedTagString;
  68. }
  69. function _sanitizeTagName($tag) {
  70. if (strstr($tag,$this->_separator) && !preg_match('/^".*?"$/',$tag)) {
  71. $tag = '"'.str_replace('"','\"',$tag).'"';
  72. }
  73. return $tag;
  74. }
  75. function getTags()
  76. {
  77. //var_dump($this);
  78. if (null == $this->_cacheId) {
  79. $this->_generateCache();
  80. }
  81. return $this->_cachedTagNames;
  82. }
  83. function getSafeTags()
  84. {
  85. if (null == $this->_cacheId) {
  86. $this->_generateCache();
  87. }
  88. return $this->_cachedSafeTagNames;
  89. }
  90. function __toString()
  91. {
  92. return $this->toString();
  93. }
  94. function addTag($tag)
  95. {
  96. if (is_string($tag)) {
  97. $this->_newTagNames[] = $tag;
  98. $this->_decache();
  99. return true;
  100. } else if (is_object($tag) && is_a($tag,'Tag')) {
  101. if ($tag->isNewRecord() || ($tag->id<1 && !empty($tag->name))) {
  102. $this->_newTagNames[] = $tag->name;
  103. $this->_decache();
  104. return true;
  105. } else if ($tag->id>0 && !in_array($this->_sanitizeTagName($tag->name),$this->_cachedTagNames)) {
  106. $this->_tags[]=$tag;
  107. $this->_decache();
  108. return true;
  109. }
  110. }
  111. return false;
  112. }
  113. function removeTag($tag) {
  114. if (is_string($tag)) {
  115. if (in_array($tag,$this->_newTagNames)) {
  116. $this->_newTagNames = array_diff($this->_newTagNames,array($tag));
  117. $this->_decache();
  118. return true;
  119. } else if (!in_array($tag,$this->_removeTagNames)) {
  120. $this->_removeTagNames[] = $tag;
  121. $this->_decache();
  122. return true;
  123. }
  124. } else if (is_object($tag) && is_a($tag,'Tag')) {
  125. if ($tag->isNewRecord()) {
  126. $this->_newTagNames = array_diff($this->_newTagNames,array($tag->name));
  127. $this->_decache();
  128. return true;
  129. } else if ($tag->id>0 && !in_array($tag->name,$this->_removeTagNames)) {
  130. $this->_removeTagNames[] = $tag;
  131. $this->_decache();
  132. return true;
  133. }
  134. }
  135. return false;
  136. }
  137. function setTags($tags)
  138. {
  139. $args = func_get_args();
  140. if (count($args)>1) {
  141. $tags = $args;
  142. }
  143. $this->_generateTagNames();
  144. $tags = is_array($tags)? $tags: (is_string($tags)?$this->_parseTagString($tags):array());
  145. $removeTags = array_diff($this->_cachedTagNames, $tags);
  146. $this->_removeTagNames = $removeTags;
  147. $newTags = array_diff($tags,$this->_cachedTagNames);
  148. $this->_newTagNames = array_merge($this->_newTagNames,$newTags);
  149. $this->_decache();
  150. }
  151. function _decache()
  152. {
  153. $this->_cacheId=null;
  154. $this->_cachedTagNames=array();
  155. $this->_cachedTagString='';
  156. }
  157. function addTags()
  158. {
  159. $args = func_get_args();
  160. foreach ($args as $arg) {
  161. if (is_array($arg)) {
  162. call_user_func_array(array(&$this,'addTags'),$arg);
  163. } else {
  164. $this->addTag($arg);
  165. }
  166. }
  167. }
  168. function removeTags()
  169. {
  170. $args = func_get_args();
  171. foreach ($args as $arg) {
  172. if (is_array($arg)) {
  173. call_user_func_array(array(&$this,'removeTags'),$arg);
  174. } else {
  175. $this->removeTag($arg);
  176. }
  177. }
  178. }
  179. function getRemovedTags()
  180. {
  181. return $this->_removeTagNames;
  182. }
  183. function getNewTags()
  184. {
  185. return $this->_newTagNames;
  186. }
  187. function parseTagString($tagString)
  188. {
  189. return $this->_parseTagString($tagString);
  190. }
  191. function _parseTagString($tagString)
  192. {
  193. $tags = array();
  194. $replaceEscaped = md5(time());
  195. $oldLength = strlen($tagString);
  196. $tagString = str_replace('\"',$replaceEscaped,$tagString);
  197. $reEscape = false;
  198. if (strlen($tagString)!=$oldLength) {
  199. $reEscape = true;
  200. }
  201. preg_match_all('/"([^"]+)"/',$tagString, $matches);
  202. if (isset($matches[0]) && count($matches[0])>0) {
  203. $tagString = str_replace($matches[0],'',$tagString);
  204. $tags = array_merge($tags,$matches[1]);
  205. }
  206. $restTags = split($this->_separator,$tagString);
  207. $tags = array_merge($tags,$restTags);
  208. $tags = array_unique($tags);
  209. $tags = array_diff($tags,array(''));
  210. $newTags = array();
  211. foreach ($tags as $idx=>$tag) {
  212. $tag = trim($tag);
  213. if ($reEscape) {
  214. $tag = str_replace($replaceEscaped,'\"',$tag);
  215. }
  216. $newTags[] = $tag;
  217. }
  218. $tags = $newTags;
  219. return $tags;
  220. }
  221. function size()
  222. {
  223. return count($this->getTags());
  224. }
  225. function reset()
  226. {
  227. $this->_tags = array();
  228. $this->_newTagNames = array();
  229. $this->_removeTagNames = array();
  230. $this->_cachedTagNames = array();
  231. $this->_cachedTagString = '';
  232. $this->_cacheId = null;
  233. }
  234. }
  235. ?>