PageRenderTime 56ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/application/modules/Core/controllers/TagController.php

https://github.com/shopaholiccompany/shopaholic
PHP | 199 lines | 139 code | 23 blank | 37 comment | 24 complexity | f38cd2e87a6f09788bc94b7b71e84882 MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. * SocialEngine
  4. *
  5. * @category Application_Core
  6. * @package Core
  7. * @copyright Copyright 2006-2010 Webligo Developments
  8. * @license http://www.socialengine.net/license/
  9. * @version $Id: TagController.php 7244 2010-09-01 01:49:53Z john $
  10. * @author John
  11. */
  12. /**
  13. * @category Application_Core
  14. * @package Core
  15. * @copyright Copyright 2006-2010 Webligo Developments
  16. * @license http://www.socialengine.net/license/
  17. */
  18. class Core_TagController extends Core_Controller_Action_Standard
  19. {
  20. public function init()
  21. {
  22. /*
  23. $subject = $this->_getParam('subject');
  24. if( is_string($subject) )
  25. {
  26. $subject = Engine_Api::_()->getItemByGuid($guid);
  27. }
  28. if( $subject instanceof Core_Model_Item_Abstract && $subject->getIdentity() )
  29. {
  30. Engine_Api::_()->core()->setSubject($subject);
  31. }
  32. */
  33. }
  34. public function addAction()
  35. {
  36. if( !$this->_helper->requireUser()->isValid() ) return;
  37. if( !$this->_helper->requireSubject()->isValid() ) return;
  38. //if( !$this->_helper->requireAuth()->setAuthParams(null, null, 'tag')->isValid() ) return;
  39. $subject = Engine_Api::_()->core()->getSubject();
  40. $viewer = Engine_Api::_()->user()->getViewer();
  41. if( !method_exists($subject, 'tags') ) {
  42. throw new Engine_Exception('whoops! doesn\'t support tagging');
  43. }
  44. // GUID tagging
  45. if( null !== ($guid = $this->_getParam('guid')) )
  46. {
  47. $tag = Engine_Api::_()->getItemByGuid($this->_getParam('guid'));
  48. }
  49. // STRING tagging
  50. else if( null !== ($text = $this->_getParam('label')) )
  51. {
  52. $tag = $text;
  53. }
  54. $tagmap = $subject->tags()->addTagMap($viewer, $tag, $this->_getParam('extra'));
  55. if( !$tagmap instanceof Core_Model_TagMap ) {
  56. throw new Engine_Exception('uh oh');
  57. }
  58. // Do stuff when users are tagged
  59. if( $tag instanceof User_Model_User && !$subject->isOwner($tag) && !$viewer->isSelf($tag) )
  60. {
  61. // Add activity
  62. $action = Engine_Api::_()->getDbtable('actions', 'activity')->addActivity(
  63. $viewer,
  64. $tag,
  65. 'tagged',
  66. '',
  67. array(
  68. 'label' => str_replace('_', ' ', $subject->getShortType())
  69. )
  70. );
  71. if( $action ) $action->attach($subject);
  72. // Add notification
  73. Engine_Api::_()->getDbtable('notifications', 'activity')->addNotification(
  74. $tag,
  75. $viewer,
  76. $subject,
  77. 'tagged',
  78. array(
  79. 'label' => str_replace('_', ' ', $subject->getShortType())
  80. )
  81. );
  82. }
  83. $this->view->id = $tagmap->getIdentity();
  84. $this->view->guid = $tagmap->tag_type . '_' . $tagmap->tag_id;
  85. $this->view->text = $tagmap->getTitle();
  86. $this->view->href = $tagmap->getHref();
  87. $this->view->extra = $tagmap->extra;
  88. }
  89. public function removeAction()
  90. {
  91. if( !$this->_helper->requireUser()->isValid() ) return;
  92. if( !$this->_helper->requireSubject()->isValid() ) return;
  93. //if( !$this->_helper->requireAuth()->setAuthParams(null, null, 'tag')->isValid() ) return;
  94. $subject = Engine_Api::_()->core()->getSubject();
  95. $viewer = Engine_Api::_()->user()->getViewer();
  96. // Subject doesn't have tagging
  97. if( !method_exists($subject, 'tags') ) {
  98. throw new Engine_Exception('Subject doesn\'t support tagging');
  99. }
  100. // Get tagmao
  101. $tagmap_id = $this->_getParam('tagmap_id');
  102. $tagmap = $subject->tags()->getTagMapById($tagmap_id);
  103. if( !($tagmap instanceof Core_Model_TagMap) ) {
  104. throw new Engine_Exception('Tagmap missing');
  105. }
  106. // Can remove if: is tagger, is tagged, is owner of resource, has tag permission
  107. if( $viewer->getGuid() != $tagmap->tagger_type . '_' . $tagmap->tagger_id &&
  108. $viewer->getGuid() != $tagmap->tag_type . '_' . $tagmap->tag_id &&
  109. !$subject->isOwner($viewer) /* &&
  110. !$subject->authorization()->isAllowed($viewer, 'tag') */ ) {
  111. throw new Engine_Exception('Not authorized');
  112. }
  113. $tagmap->delete();
  114. }
  115. public function suggestAction()
  116. {
  117. $tags = Engine_Api::_()->getDbtable('tags', 'core')->getTagsByText($this->_getParam('text'), $this->_getParam('limit', 40));
  118. $data = array();
  119. $mode = $this->_getParam('struct');
  120. if( $mode == 'text' )
  121. {
  122. foreach( $tags as $tag )
  123. {
  124. $data[] = $tag->text;
  125. }
  126. }
  127. else
  128. {
  129. foreach( $tags as $tag )
  130. {
  131. $data[] = array(
  132. 'id' => $tag->tag_id,
  133. 'label' => $tag->text
  134. );
  135. }
  136. }
  137. if( $this->_getParam('sendNow', true) )
  138. {
  139. return $this->_helper->json($data);
  140. }
  141. else
  142. {
  143. $this->_helper->viewRenderer->setNoRender(true);
  144. $data = Zend_Json::encode($data);
  145. $this->getResponse()->setBody($data);
  146. }
  147. }
  148. public function retrieveAction()
  149. {
  150. if( !$this->_helper->requireSubject()->checkRequire() ) return;
  151. $subject = Engine_Api::_()->core()->getSubject();
  152. $viewer = Engine_Api::_()->user()->getViewer();
  153. if( !method_exists($subject, 'tags') ) {
  154. throw new Engine_Exception('whoops! doesn\'t support tagging');
  155. }
  156. $data = array();
  157. foreach( $subject->tags()->getTagMaps() as $tagmap ) {
  158. $data[] = array_merge($tagmap->toArray(), array(
  159. 'id' => $tagmap->getIdentity(),
  160. 'text' => $tagmap->getTitle(),
  161. 'href' => $tagmap->getHref(),
  162. 'guid' => $tagmap->tag_type . '_' . $tagmap->tag_id
  163. ));
  164. }
  165. if( $this->_getParam('sendNow', true) )
  166. {
  167. return $this->_helper->json($data);
  168. }
  169. else
  170. {
  171. $this->_helper->viewRenderer->setNoRender(true);
  172. $data = Zend_Json::encode($data);
  173. $this->getResponse()->setBody($data);
  174. }
  175. }
  176. }