BxDolLabel.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaCore UNA Core
  7. * @{
  8. */
  9. class BxDolLabel extends BxDolFactory implements iBxDolSingleton
  10. {
  11. protected $_oDb;
  12. protected $_sForm;
  13. protected $_sFormDisplaySelect;
  14. protected function __construct()
  15. {
  16. parent::__construct();
  17. $this->_oDb = new BxDolLabelQuery();
  18. $this->_sForm= 'sys_labels';
  19. $this->_sFormDisplaySelect = 'sys_labels_select';
  20. }
  21. public function __clone()
  22. {
  23. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  24. trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
  25. }
  26. public static function getInstance($oTemplate = false)
  27. {
  28. if(!isset($GLOBALS['bxDolClasses'][__CLASS__]))
  29. $GLOBALS['bxDolClasses'][__CLASS__] = new BxTemplLabel($oTemplate);
  30. return $GLOBALS['bxDolClasses'][__CLASS__];
  31. }
  32. public function actionSelectLabels()
  33. {
  34. $sName = bx_process_input(bx_get('name'));
  35. $aValue = bx_process_input(bx_get('value'));
  36. return echoJson($this->selectLabels([
  37. 'name' => $sName,
  38. 'list' => $aValue,
  39. 'list_context' => $aValue,
  40. ]));
  41. }
  42. public function actionLabelsList()
  43. {
  44. $sTerm = bx_process_input(bx_get('term'));
  45. $aLabels = $this->getLabels(array('type' => 'term', 'term' => $sTerm));
  46. $aResult = array();
  47. foreach($aLabels as $aLabel)
  48. $aResult[] = array (
  49. 'label' => $aLabel['value'],
  50. 'value' => $aLabel['value'],
  51. );
  52. echoJson($aResult);
  53. }
  54. public function getLabels($aParams = [])
  55. {
  56. return $this->_oDb->getLabels($aParams);
  57. }
  58. public function getLabelsSystem($iParentId = 0)
  59. {
  60. $iLevel = 0;
  61. return $this->_getLabelsSystem($iParentId, $iLevel);
  62. }
  63. public function getLabelsContext($iProfileId = 0)
  64. {
  65. if(!$iProfileId)
  66. $iProfileId = bx_get_logged_profile_id();
  67. $aModules = bx_srv('system', 'get_modules_by_type', ['context']);
  68. $aResults = [];
  69. foreach($aModules as $aModule) {
  70. if(bx_srv($aModule['name'], 'act_as_profile'))
  71. continue;
  72. $aContextPids = bx_srv($aModule['name'], 'get_participating_profiles', [$iProfileId]);
  73. if(empty($aContextPids) || !is_array($aContextPids))
  74. continue;
  75. foreach($aContextPids as $iContextPid) {
  76. $aContextInfo = bx_srv($aModule['name'], 'get_content_info_by_profile_id', [$iContextPid]);
  77. if(empty($aContextInfo['hashtag']))
  78. continue;
  79. $aResults[] = [
  80. 'value' => $aContextInfo['hashtag']
  81. ];
  82. }
  83. }
  84. return $aResults;
  85. }
  86. public function getLabelUrl($sKeyword, $mixedSection = false)
  87. {
  88. $sSectionPart = '';
  89. if(!empty($mixedSection)) {
  90. if (is_array($mixedSection))
  91. $sSectionPart = '&section[]=' . implode('&section[]=', $mixedSection);
  92. elseif (is_string($mixedSection))
  93. $sSectionPart = '&section[]=' . $mixedSection;
  94. }
  95. $sUrl = BX_DOL_URL_ROOT . 'searchKeyword.php?type=keyword&keyword=' . rawurlencode($sKeyword) . $sSectionPart;
  96. bx_alert('meta_keyword', 'url', 0, false, array(
  97. 'url' => &$sUrl,
  98. 'keyword' => $sKeyword,
  99. 'section' => $mixedSection,
  100. ));
  101. return $sUrl;
  102. }
  103. public function getFormFieldId($aInput = array())
  104. {
  105. return !empty($aInput['name']) ? $this->_aHtmlIds['labels_element'] . $aInput['name'] : 'bx-form-element-labels';
  106. }
  107. public function onAdd($iId)
  108. {
  109. $aLabel = $this->_oDb->getLabels(array('type' => 'id', 'id' => $iId));
  110. if(empty($aLabel) || !is_array($aLabel))
  111. return;
  112. /**
  113. * @hooks
  114. * @hookdef hook-label-edited 'label', 'added' - hook on new label added
  115. * - $unit_name - equals `label`
  116. * - $action - equals `added`
  117. * - $object_id - label id
  118. * - $sender_id - not used
  119. * - $extra_params - array of additional params with the following array keys:
  120. * - `label` - [array] array with label info
  121. * @hook @ref hook-label-added
  122. */
  123. bx_alert('label', 'added', $iId, false, array('label' => $aLabel));
  124. }
  125. public function onEdit($iId)
  126. {
  127. $aLabel = $this->_oDb->getLabels(array('type' => 'id', 'id' => $iId));
  128. if(empty($aLabel) || !is_array($aLabel))
  129. return;
  130. /**
  131. * @hooks
  132. * @hookdef hook-label-edited 'label', 'edited' - hook on new label edited
  133. * - $unit_name - equals `label`
  134. * - $action - equals `edited`
  135. * - $object_id - label id
  136. * - $sender_id - not used
  137. * - $extra_params - array of additional params with the following array keys:
  138. * - `label` - [array] array with label info
  139. * @hook @ref hook-label-edited
  140. */
  141. bx_alert('label', 'edited', $iId, false, array('label' => $aLabel));
  142. }
  143. public function onDelete($iId)
  144. {
  145. $aLabel = $this->_oDb->getLabels(array('type' => 'id', 'id' => $iId));
  146. if(empty($aLabel) || !is_array($aLabel))
  147. return;
  148. /**
  149. * @hooks
  150. * @hookdef hook-label-deleted 'label', 'deleted' - hook on new label deleted
  151. * - $unit_name - equals `label`
  152. * - $action - equals `deleted`
  153. * - $object_id - label id
  154. * - $sender_id - not used
  155. * - $extra_params - array of additional params with the following array keys:
  156. * - `label` - [array] array with label info
  157. * @hook @ref hook-label-deleted
  158. */
  159. bx_alert('label', 'deleted', $iId, false, array('label' => $aLabel));
  160. }
  161. protected function _getLabelsSystem($iParentId, $iLevel)
  162. {
  163. $aLabels = $this->getLabels(['type' => 'parent', 'parent' => $iParentId]);
  164. if(empty($aLabels) || !is_array($aLabels))
  165. return [];
  166. $aResults = [];
  167. foreach($aLabels as $aLabel) {
  168. $aResult = [
  169. 'value' => $aLabel['value']
  170. ];
  171. $aSubitems = $this->_getLabelsSystem($aLabel['id'], $iLevel + 1);
  172. if(!empty($aSubitems) && is_array($aSubitems))
  173. $aResult['subitems'] = $aSubitems;
  174. $aResults[] = $aResult;
  175. }
  176. return $aResults;
  177. }
  178. }
  179. /** @} */