BxCoursesGridCntDataManage.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 Courses Courses
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. class BxCoursesGridCntDataManage extends BxTemplGrid
  12. {
  13. protected $_sModule;
  14. protected $_oModule;
  15. protected $_sPageUrl;
  16. protected $_iEntryId;
  17. protected $_iEntryPid;
  18. protected $_iNodeId;
  19. protected $_aNodeInfo;
  20. protected $_iLevelMax;
  21. public function __construct ($aOptions, $oTemplate = false)
  22. {
  23. $this->_sModule = 'bx_courses';
  24. $this->_oModule = BxDolModule::getInstance($this->_sModule);
  25. parent::__construct ($aOptions, $oTemplate);
  26. $CNF = &$this->_oModule->_oConfig->CNF;
  27. $this->_iEntryId = 0;
  28. if(($iEntryId = bx_get('entry_id')) !== false)
  29. $this->setEntryId($iEntryId);
  30. $this->setNodeId(($iNodeId = bx_get('parent_id')) !== false ? $iNodeId : 0);
  31. $this->_iLevelMax = $this->_oModule->_oConfig->getContentLevelMax();
  32. }
  33. public function setEntryId($iEntryId)
  34. {
  35. $CNF = &$this->_oModule->_oConfig->CNF;
  36. $this->_iEntryId = (int)$iEntryId;
  37. $this->_iEntryPid = ($oProfile = BxDolProfile::getInstanceByContentAndType($this->_iEntryId, $this->_sModule)) !== false ? $oProfile->id() : 0;
  38. $this->_sPageUrl = BxDolPermalinks::getInstance()->permalink($CNF['URL_MANAGE_STRUCTURE'], ['profile_id' => $this->_iEntryPid]);
  39. $this->_aQueryAppend['entry_id'] = $this->_iEntryId;
  40. }
  41. public function setNodeId($iNodeId)
  42. {
  43. $this->_iNodeId = (int)$iNodeId;
  44. $this->_aNodeInfo = [];
  45. if($this->_iNodeId)
  46. $this->_aNodeInfo = $this->_oModule->_oDb->getContentNodes(['sample' => 'id_full', 'id' => $this->_iNodeId]);
  47. $this->_aQueryAppend['parent_id'] = $this->_iNodeId;
  48. }
  49. public function getCode ($isDisplayHeader = true)
  50. {
  51. if(empty($this->_aNodeInfo) || !is_array($this->_aNodeInfo) || $this->_aNodeInfo['level'] != $this->_iLevelMax)
  52. return '';
  53. return parent::getCode($isDisplayHeader);
  54. }
  55. public function getCodeAPI($bForceReturn = false)
  56. {
  57. if(empty($this->_aNodeInfo) || !is_array($this->_aNodeInfo) || $this->_aNodeInfo['level'] != $this->_iLevelMax)
  58. return [];
  59. return parent::getCodeAPI($bForceReturn);
  60. }
  61. protected function _getCellContentId($mixedValue, $sKey, $aField, $aRow)
  62. {
  63. $iContentId = (int)$mixedValue;
  64. $mixedValue = '';
  65. $sModule = $aRow['content_type'];
  66. if(($sMethod = 'get_title') && bx_is_srv($sModule, $sMethod))
  67. $mixedValue = bx_srv($sModule, $sMethod, [$iContentId]);
  68. if(!$mixedValue && ($sMethod = 'get_text') && bx_is_srv($sModule, $sMethod))
  69. $mixedValue = bx_srv($sModule, $sMethod, [$iContentId]);
  70. if(!$mixedValue)
  71. $mixedValue = _t('_undefined');
  72. if(($sMethod = 'get_link') && bx_is_srv($sModule, $sMethod))
  73. $mixedValue = $this->_oModule->_oTemplate->parseHtmlByName('name_link.html', [
  74. 'href' => bx_srv($sModule, $sMethod, [$iContentId]),
  75. 'title' => bx_html_attribute($mixedValue),
  76. 'content' => $mixedValue
  77. ]);
  78. return parent::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
  79. }
  80. protected function _getCellContentType($mixedValue, $sKey, $aField, $aRow)
  81. {
  82. return parent::_getCellDefault(_t('_' . $mixedValue), $sKey, $aField, $aRow);
  83. }
  84. protected function _getCellUsage($mixedValue, $sKey, $aField, $aRow)
  85. {
  86. return parent::_getCellDefault(_t('_bx_courses_txt_data_usage_' . $mixedValue), $sKey, $aField, $aRow);
  87. }
  88. protected function _getCellAdded($mixedValue, $sKey, $aField, $aRow)
  89. {
  90. if($this->_bIsApi)
  91. return ['type' => 'time', 'data' => $mixedValue];
  92. return parent::_getCellDefault(bx_time_js($mixedValue), $sKey, $aField, $aRow);
  93. }
  94. protected function _getActionBack($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = [])
  95. {
  96. if(empty($this->_aNodeInfo))
  97. return $this->_bIsApi ? [] : '';
  98. $sUrl = $this->_sPageUrl;
  99. if(!empty($this->_aNodeInfo['parent_id']))
  100. $sUrl = bx_append_url_params($sUrl, ['parent_id' => $this->_aNodeInfo['parent_id']]);
  101. if($this->_bIsApi)
  102. return array_merge($a, ['name' => $sKey, 'type' => 'link', 'link' => bx_api_get_relative_url($sUrl)]);
  103. $a['attr'] = array_merge($a['attr'], [
  104. "onclick" => "window.open('" . $sUrl . "', '_self');"
  105. ]);
  106. return $this->_getActionDefault ($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  107. }
  108. protected function _getActionAddSt($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = [])
  109. {
  110. if($this->_bIsApi)
  111. return array_merge($a, ['name' => $sKey, 'type' => 'menu', 'action' => $sKey, 'values' => $this->_getValues(BX_COURSES_CND_USAGE_ST)]);
  112. $CNF = &$this->_oModule->_oConfig->CNF;
  113. $a['attr'] = array_merge($a['attr'], [
  114. "onclick" => "$(this).off('click'); bx_menu_popup('" . $CNF['OBJECT_MENU_CONTENT_ADD'] . "', this, {removeOnClose: 1}, {entry_pid: " . $this->_iEntryPid . ", node_id: " . $this->_iNodeId . ", usage:0});"
  115. ]);
  116. return $this->_getActionDefault ($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  117. }
  118. protected function _getActionAddAt($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = [])
  119. {
  120. if($this->_bIsApi)
  121. return array_merge($a, ['name' => $sKey, 'type' => 'menu', 'action' => $sKey, 'values' => $this->_getValues(BX_COURSES_CND_USAGE_AT)]);
  122. $CNF = &$this->_oModule->_oConfig->CNF;
  123. $a['attr'] = array_merge($a['attr'], [
  124. "onclick" => "$(this).off('click'); bx_menu_popup('" . $CNF['OBJECT_MENU_CONTENT_ADD'] . "', this, {removeOnClose: 1}, {entry_pid: " . $this->_iEntryPid . ", node_id: " . $this->_iNodeId . ", usage:1});"
  125. ]);
  126. return $this->_getActionDefault ($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  127. }
  128. protected function _getActionEdit($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = [])
  129. {
  130. $oModule = BxDolModule::getInstance($aRow['content_type']);
  131. if(!$oModule)
  132. return '';
  133. $CNF_MODULE = &$oModule->_oConfig->CNF;
  134. if(!isset($CNF_MODULE['URI_EDIT_ENTRY']))
  135. return '';
  136. $sUrl = bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF_MODULE['URI_EDIT_ENTRY'] . '&id=' . $aRow['content_id']));
  137. if($this->_bIsApi)
  138. return array_merge($a, [
  139. 'name' => $sKey,
  140. 'type' => 'modal',
  141. 'callback' => $aRow['content_type'] . '/entity_edit/&params[]=' . $aRow['content_id'],
  142. 'content_type' => $aRow['content_type']
  143. ]);
  144. $a['attr'] = array_merge($a['attr'], [
  145. "onclick" => "window.open('" . $sUrl . "', '_self');"
  146. ]);
  147. return $this->_getActionDefault ($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  148. }
  149. protected function _delete($mixedId)
  150. {
  151. $aData = $this->_oModule->_oDb->getContentData(['sample' => 'id', 'id' => (int)$mixedId]);
  152. if(!empty($aData) && is_array($aData)) {
  153. $sModule = $aData['content_type'];
  154. $sMethod = 'delete_entity';
  155. if(bx_is_srv($sModule, $sMethod) && bx_srv($sModule, $sMethod, [$aData['content_id']]) != '')
  156. return false;
  157. }
  158. return parent::_delete($mixedId) !== false;
  159. }
  160. protected function _getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage)
  161. {
  162. if(!$this->_iEntryId)
  163. return [];
  164. $this->_aOptions['source'] .= $this->_oModule->_oDb->prepareAsString(" AND `entry_id`=? AND `node_id`=?", $this->_iEntryId, $this->_iNodeId);
  165. return parent::_getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage);
  166. }
  167. protected function _getIds()
  168. {
  169. $aIds = bx_get('ids');
  170. if(!$aIds || !is_array($aIds)) {
  171. $iId = (int)bx_get('id');
  172. if(!$iId)
  173. return false;
  174. $aIds = [$iId];
  175. }
  176. return $aIds;
  177. }
  178. protected function _getValues($iUsage)
  179. {
  180. $aValues = [];
  181. $oPermalink = BxDolPermalinks::getInstance();
  182. $aModules = $this->_oModule->_oConfig->getContentModules($iUsage);
  183. foreach($aModules as $sModule) {
  184. $oModule = BxDolModule::getInstance($sModule);
  185. if(!$oModule)
  186. continue;
  187. $CNF = &$oModule->_oConfig->CNF;
  188. if(!isset($CNF['URI_ADD_ENTRY']))
  189. continue;
  190. $aParams = [
  191. 'context_pid' => $this->_iEntryPid,
  192. 'context_nid' => $this->_iNodeId,
  193. 'context_usage' => $iUsage
  194. ];
  195. $aValues[] = [
  196. 'key' => $sModule,
  197. 'title' => _t('_' . $sModule),
  198. 'link' => bx_api_get_relative_url(BX_DOL_URL_ROOT . $oPermalink->permalink('page.php?i=' . $CNF['URI_ADD_ENTRY'], $aParams)),
  199. 'callback' => $sModule . '/entity_create/&params[]=' . json_encode($aParams)
  200. ];
  201. }
  202. return $aValues;
  203. }
  204. }
  205. /** @} */