PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/src/site/components/com_base/domains/behaviors/taggable.php

https://github.com/bhar1red/anahita
PHP | 190 lines | 77 code | 16 blank | 97 comment | 4 complexity | 83dbac7d4caf3d19662b1f1d19bc729d MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * LICENSE: ##LICENSE##
  4. *
  5. * @category Anahita
  6. * @package Com_Base
  7. * @subpackage Domain_Behavior
  8. * @author Arash Sanieyan <ash@anahitapolis.com>
  9. * @author Rastin Mehr <rastin@anahitapolis.com>
  10. * @copyright 2008 - 2010 rmdStudio Inc./Peerglobe Technology Inc
  11. * @license GNU GPLv3 <http://www.gnu.org/licenses/gpl-3.0.html>
  12. * @version SVN: $Id: resource.php 11985 2012-01-12 10:53:20Z asanieyan $
  13. * @link http://www.anahitapolis.com
  14. */
  15. /**
  16. * Taggable Behavior
  17. *
  18. * @category Anahita
  19. * @package Com_Base
  20. * @subpackage Domain_Behavior
  21. * @author Arash Sanieyan <ash@anahitapolis.com>
  22. * @author Rastin Mehr <rastin@anahitapolis.com>
  23. * @license GNU GPLv3 <http://www.gnu.org/licenses/gpl-3.0.html>
  24. * @link http://www.anahitapolis.com
  25. */
  26. class ComBaseDomainBehaviorTaggable extends AnDomainBehaviorAbstract
  27. {
  28. /**
  29. * Initializes the default configuration for the object
  30. *
  31. * Called from {@link __construct()} as a first step of object instantiation.
  32. *
  33. * @param KConfig $config An optional KConfig object with configuration options.
  34. *
  35. * @return void
  36. */
  37. protected function _initialize(KConfig $config)
  38. {
  39. $config->append(array(
  40. 'attributes' => array(
  41. 'tagCount' => array('default'=>0,'write'=>'private'),
  42. 'tagIds' => array('type'=>'set', 'default'=>'set','write'=>'private')
  43. ),
  44. 'relationships' => array(
  45. 'tags' => array(
  46. 'parent_delete' => 'ignore',
  47. 'through' => 'com:tags.domain.entity.association',
  48. 'child_key' => 'taggable',
  49. 'target' => 'com:base.domain.entity.node',
  50. 'target_child_key' => 'tag'
  51. )
  52. ),
  53. ));
  54. parent::_initialize($config);
  55. }
  56. /**
  57. * Seaches for hashtags to add/remove a tag
  58. *
  59. * @param KCommandContext $context Context parameter
  60. *
  61. * @return void
  62. */
  63. protected function _beforeEntityInsert(KCommandContext $context)
  64. {
  65. $this->_mixer->addHashtagsAsTagsFrom($this->_mixer->body);
  66. }
  67. /**
  68. * Seaches for hashtags to add/remove a tag
  69. *
  70. * @param KCommandContext $context Context parameter
  71. *
  72. * @return void
  73. */
  74. protected function _beforeEntityUpdate(KCommandContext $context)
  75. {
  76. $this->_mixer->addHashtagsAsTagsFrom($this->_mixer->body);
  77. }
  78. /**
  79. * Add an entity hash tags as tags
  80. *
  81. * @param string Extract hashtags from a text and add them as tags to
  82. * the entity
  83. *
  84. * @return void
  85. */
  86. public function addHashtagsAsTagsFrom($text)
  87. {
  88. $regx = '/#(\w+)/';
  89. $matches = array();
  90. if ( preg_match_all($regx, $text, $matches) ) {
  91. $hashtags = array_unique($matches[1]);
  92. foreach($matches[1] as $tag) {
  93. $this->_mixer->addTag($tag);
  94. }
  95. }
  96. }
  97. /**
  98. * Seaches for hashtags to add/remove a tag
  99. *
  100. * @param KCommandContext $context Context parameter
  101. *
  102. * @return void
  103. */
  104. protected function _afterEntityDelete(KCommandContext $context)
  105. {
  106. }
  107. /**
  108. * Adds a tag object to the mixer entity. A tag object can be a simple
  109. * string or another node object
  110. *
  111. * @param mixed tag A tag. Can be a simple string or an object
  112. *
  113. * @return void
  114. */
  115. public function addTag($tag)
  116. {
  117. if ( is_string($tag) ) {
  118. //find a text tag
  119. $tag = $this->getService('repos://site/tags.text')->findOrAddNew(array('name'=>$tag));
  120. }
  121. //will not insert the tag if already inserted
  122. $this->tags->insert($tag);
  123. return $this;
  124. }
  125. /**
  126. * Change the query to include name.
  127. *
  128. * Since the target is a simple node. The name field is not included. By ovewriting the
  129. * tags method we can change the query to include name in the $taggable->tags query
  130. *
  131. * @return AnDomainEntitySet
  132. */
  133. public function getTags()
  134. {
  135. $this->get('tags')->getQuery()->select('name');
  136. return $this->get('tags');
  137. }
  138. /**
  139. * Removes a tag object to the mixer entity. A tag object can be a simple
  140. * string or another node object
  141. *
  142. * @param mixed tag A tag. Can be a simple string or an object
  143. *
  144. * @return void
  145. */
  146. public function removeTag($tag)
  147. {
  148. if ( is_string($tag) ) {
  149. //find a text tag
  150. $tag = $this->getService('repos://site/tags.text')->find(array('name'=>$tag));
  151. }
  152. //removes the tag
  153. if ( $tag ) {
  154. $this->tags->extract($tag);
  155. }
  156. return $this;
  157. }
  158. /**
  159. * Reset subscriptions stats
  160. *
  161. * @param array $entities
  162. *
  163. * @return void
  164. */
  165. public function resetStats(array $entities)
  166. {
  167. foreach($entities as $entity)
  168. {
  169. $ids = $entity->tags->getQuery()->disableChain()->fetchValues('id');
  170. $entity->set('tagCount', count($ids));
  171. $entity->set('tagIds', AnDomainAttribute::getInstance('set')->setData($ids));
  172. }
  173. }
  174. }