BxDirModule.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 Directory Directory
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. /**
  12. * Directory module
  13. */
  14. class BxDirModule extends BxBaseModTextModule
  15. {
  16. function __construct(&$aModule)
  17. {
  18. parent::__construct($aModule);
  19. $CNF = &$this->_oConfig->CNF;
  20. $this->_aSearchableNamesExcept = array_merge($this->_aSearchableNamesExcept, array(
  21. $CNF['FIELD_CATEGORY_VIEW'],
  22. $CNF['FIELD_CATEGORY_SELECT']
  23. ));
  24. }
  25. public function serviceUpdateCategoriesStats()
  26. {
  27. $aStats = $this->_oDb->getCategories(array('type' => 'collect_stats'));
  28. if(empty($aStats) || !is_array($aStats))
  29. return true;
  30. $iUpdated = 0;
  31. foreach($aStats as $aStat)
  32. if($this->_oDb->updateCategory(array('items' => $aStat['count']), array('id' => $aStat['id'])))
  33. $iUpdated++;
  34. return count($aStats) == $iUpdated;
  35. }
  36. public function serviceGetCategoryOptions($iParentId, $bPleaseSelect = false)
  37. {
  38. $aValues = array();
  39. if($bPleaseSelect)
  40. $aValues[] = array('key' => '', 'value' => _t('_sys_please_select'));
  41. $this->_getCategoryOptions($iParentId, $aValues);
  42. return $aValues;
  43. }
  44. public function serviceGetSearchableFields($aInputsAdd = array())
  45. {
  46. $aInputsAdd = array_merge($aInputsAdd, $this->_getSearchableFields());
  47. return parent::serviceGetSearchableFields($aInputsAdd);
  48. }
  49. public function serviceGetSearchableFieldsExtended($aInputsAdd = array())
  50. {
  51. $CNF = &$this->_oConfig->CNF;
  52. $aInputsAdd = array_merge($aInputsAdd, $this->_getSearchableFields());
  53. if(isset($aInputsAdd[$CNF['FIELD_CATEGORY']])) {
  54. $aInputsAdd[$CNF['FIELD_CATEGORY']]['type'] = 'select';
  55. $aInputsAdd[$CNF['FIELD_CATEGORY']]['values_src'] = BxDolService::getSerializedService($this->_oConfig->getName(), 'get_category_options', array(0));
  56. }
  57. return parent::serviceGetSearchableFieldsExtended($aInputsAdd);
  58. }
  59. public function serviceEntityCreate ($sParams = false)
  60. {
  61. if(($sDisplay = $this->_getCategoryDisplay('add')) !== false) {
  62. if(empty($sParams) || !is_array($sParams))
  63. $sParams = array();
  64. $sParams['display'] = $sDisplay;
  65. }
  66. return parent::serviceEntityCreate($sParams);
  67. }
  68. public function serviceCategoriesList($aParams = array())
  69. {
  70. return $this->_oTemplate->categoriesList();
  71. }
  72. public function serviceBrowseCategory($iCategoryId = 0, $aParams = array())
  73. {
  74. $sParamGet = 'category';
  75. $bEmptyMessage = isset($aParams['empty_message']) ? (bool)$aParams['empty_message'] : true;
  76. if(!$iCategoryId)
  77. $iCategoryId = bx_process_input(bx_get($sParamGet), BX_DATA_INT);
  78. if(!$iCategoryId)
  79. return $bEmptyMessage ? MsgBox(_t('_Empty')) : '';
  80. $aBlock = $this->_serviceBrowseWithParam('category', $sParamGet, $iCategoryId, $aParams);
  81. if(!empty($aBlock['content'])) {
  82. $aCategory = $this->_oDb->getCategories(array('type' => 'id', 'id' => $iCategoryId));
  83. if(!empty($aCategory['title']))
  84. $aBlock['title'] = _t('_bx_directory_page_block_title_entries_by_category_mask', _t($aCategory['title']));
  85. }
  86. return $aBlock;
  87. }
  88. /**
  89. * Common methods.
  90. */
  91. /**
  92. * Internal methods.
  93. */
  94. protected function _serviceEntityForm ($sFormMethod, $iContentId = 0, $sDisplay = false, $sCheckFunction = false, $bErrorMsg = true)
  95. {
  96. $CNF = &$this->_oConfig->CNF;
  97. $mixedContent = $this->_getContent($iContentId, true);
  98. if($mixedContent === false)
  99. return false;
  100. list($iContentId, $aContentInfo) = $mixedContent;
  101. $sDisplayType = false;
  102. switch($sFormMethod) {
  103. case 'editDataForm':
  104. $sDisplayType = 'edit';
  105. break;
  106. case 'viewDataForm':
  107. case 'viewDataEntry':
  108. $sDisplayType = 'view';
  109. break;
  110. }
  111. if($sDisplayType !== false && ($sDisplayNew = $this->_getCategoryDisplay($sDisplayType, $aContentInfo[$CNF['FIELD_CATEGORY']])) !== false)
  112. $sDisplay = $sDisplayNew;
  113. return parent::_serviceEntityForm ($sFormMethod, $iContentId, $sDisplay, $sCheckFunction, $bErrorMsg);
  114. }
  115. protected function _getCategoryDisplay($sDisplayType, $iCategory = 0)
  116. {
  117. if(empty($iCategory) && bx_get('category') !== false)
  118. $iCategory = (int)bx_get('category');
  119. if(empty($iCategory))
  120. return false;
  121. $aCategory = $this->_oDb->getCategories(array('type' => 'id_full', 'id' => $iCategory));
  122. $sKey = 'type_display_' . $sDisplayType;
  123. if(empty($aCategory[$sKey]))
  124. return false;
  125. return $aCategory[$sKey];
  126. }
  127. protected function _getCategoryOptions($iParentId, &$aValues)
  128. {
  129. $aCategories = $this->_oDb->getCategories(array('type' => 'parent_id', 'parent_id' => $iParentId));
  130. foreach($aCategories as $aCategory) {
  131. $aValues[] = array('key' => $aCategory['id'], 'value' => str_repeat('--', (int)$aCategory['level']) . ' ' . _t($aCategory['title']));
  132. $this->_getCategoryOptions($aCategory['id'], $aValues);
  133. }
  134. }
  135. protected function _getSearchableFields($mixedDisplayType = '')
  136. {
  137. $CNF = &$this->_oConfig->CNF;
  138. if(empty($mixedDisplayType))
  139. $mixedDisplayType = array('add', 'edit');
  140. $aResult = array();
  141. $aDisplays = $this->_oDb->getDisplays($this->_oConfig->getName() . '_entry', $mixedDisplayType);
  142. foreach($aDisplays as $aDisplay) {
  143. if($aDisplay['display_name'] == $CNF['OBJECT_FORM_ENTRY_DISPLAY_ADD'])
  144. continue;
  145. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_ENTRY'], $aDisplay['display_name'], $this->_oTemplate);
  146. if(!$oForm)
  147. continue;
  148. $aResult = array_merge($aResult, $oForm->aInputs);
  149. }
  150. return $aResult;
  151. }
  152. }
  153. /** @} */