BxCoursesGridCntStructureManage.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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 BxCoursesGridCntStructureManage extends BxTemplGrid
  12. {
  13. protected $_sModule;
  14. protected $_oModule;
  15. protected $_sPageUrl;
  16. protected $_iEntryId;
  17. protected $_iEntryPid;
  18. protected $_iParentId;
  19. protected $_aParentInfo;
  20. protected $_iLevel;
  21. protected $_iLevelMax;
  22. protected $_aLevelToNode;
  23. protected $_aLevelToNodePl;
  24. public function __construct ($aOptions, $oTemplate = false)
  25. {
  26. $this->_sModule = 'bx_courses';
  27. $this->_oModule = BxDolModule::getInstance($this->_sModule);
  28. parent::__construct ($aOptions, $oTemplate);
  29. $CNF = &$this->_oModule->_oConfig->CNF;
  30. $this->_iEntryId = 0;
  31. if(($iEntryId = bx_get('entry_id')) !== false)
  32. $this->setEntryId($iEntryId);
  33. $this->setParentId(($iParentId = bx_get('parent_id')) !== false ? $iParentId : 0);
  34. $this->_iLevel = $this->_getNodeLevel();
  35. $this->_iLevelMax = $this->_oModule->_oConfig->getContentLevelMax();
  36. $this->_aLevelToNode = $this->_oModule->_oConfig->getContentLevel2Node();
  37. $this->_aLevelToNodePl = $this->_oModule->_oConfig->getContentLevel2Node(false);
  38. }
  39. public function setEntryId($iEntryId)
  40. {
  41. $CNF = &$this->_oModule->_oConfig->CNF;
  42. $this->_iEntryId = (int)$iEntryId;
  43. $this->_iEntryPid = ($oProfile = BxDolProfile::getInstanceByContentAndType($this->_iEntryId, $this->_sModule)) !== false ? $oProfile->id() : 0;
  44. $this->_sPageUrl = BxDolPermalinks::getInstance()->permalink($CNF['URL_MANAGE_STRUCTURE'], ['profile_id' => $this->_iEntryPid]);
  45. $this->_aQueryAppend['entry_id'] = $this->_iEntryId;
  46. }
  47. public function setParentId($iParentId)
  48. {
  49. $this->_iParentId = (int)$iParentId;
  50. $this->_aParentInfo = [];
  51. if($this->_iParentId)
  52. $this->_aParentInfo = $this->_oModule->_oDb->getContentNodes(['sample' => 'id_full', 'id' => $this->_iParentId]);
  53. $this->_aQueryAppend['parent_id'] = $this->_iParentId;
  54. }
  55. public function getCode ($isDisplayHeader = true)
  56. {
  57. if($this->_iLevel > $this->_iLevelMax)
  58. return '';
  59. return parent::getCode($isDisplayHeader);
  60. }
  61. //TODO: Need to check level and return correct title.
  62. public function getFormBlockTitleAPI($sAction, $iId = 0)
  63. {
  64. $CNF = &$this->_oModule->_oConfig->CNF;
  65. $sResult = '';
  66. switch($sAction) {
  67. case 'add':
  68. $sResult = $CNF['T']['popup_title_content_node_add'];
  69. break;
  70. case 'edit':
  71. $sResult = $CNF['T']['popup_title_content_node_edit'];
  72. break;
  73. }
  74. return $this->_parseNodeName(_t($sResult));
  75. }
  76. public function getFormCallBackUrlAPI($sAction, $iId = 0)
  77. {
  78. return '/api.php?r=system/perfom_action_api/TemplServiceGrid/&params[]=&o=' . $this->_sObject . '&a=' . $sAction . '&entry_id=' . $this->_iEntryId . '&parent_id=' . $this->_iParentId . '&id=' . $iId;
  79. }
  80. public function getCodeAPI($bForceReturn = false)
  81. {
  82. if($this->_iLevel > $this->_iLevelMax)
  83. return [];
  84. return parent::getCodeAPI($bForceReturn);
  85. }
  86. protected function _getActionsAPI ($sType)
  87. {
  88. $aResult = parent::_getActionsAPI($sType);
  89. if($aResult && $sType == 'independent') {
  90. if(!empty($aResult['add']))
  91. $aResult['add']['title'] = $this->_parseNodeName($aResult['add']['title']);
  92. }
  93. return $aResult;
  94. }
  95. public function performActionAdd()
  96. {
  97. $CNF = &$this->_oModule->_oConfig->CNF;
  98. $sAction = 'add';
  99. $aEntryInfo = $this->_oModule->_oDb->getContentInfoById($this->_iEntryId);
  100. if(($mixedResult = $this->_oModule->checkAllowedEdit($aEntryInfo)) !== CHECK_ACTION_RESULT_ALLOWED)
  101. return $this->_getActionResult(['msg' => $mixedResult]);
  102. $sForm = $CNF['OBJECT_FORM_CNT_NODE_DISPLAY_ADD'];
  103. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_CNT_NODE'], $CNF['OBJECT_FORM_CNT_NODE_DISPLAY_ADD']);
  104. $oForm->setId($sForm);
  105. $oForm->setName($sForm);
  106. $oForm->setAction(BX_DOL_URL_ROOT . bx_append_url_params('grid.php', ['o' => $this->_sObject, 'a' => $sAction, 'entry_id' => $this->_iEntryId, 'parent_id' => $this->_iParentId]));
  107. $oForm->setData($this->_iParentId, $this->_iLevel);
  108. $oForm->initChecker();
  109. if($oForm->isSubmittedAndValid()) {
  110. $iId = (int)$oForm->insert(['entry_id' => $this->_iEntryId, 'added' => time()]);
  111. if($iId != 0) {
  112. $this->_oModule->_oDb->insertContentStructureNode([
  113. 'entry_id' => $this->_iEntryId,
  114. 'parent_id' => $this->_iParentId,
  115. 'node_id' => $iId,
  116. 'level' => $this->_iLevel,
  117. 'order' => $this->_oModule->_oDb->getContentStructureOrderMax($this->_iEntryId, $this->_iParentId) + 1
  118. ]);
  119. if($this->_iLevel > 1)
  120. $this->_oModule->_oDb->updateContentStructureCounters($this->_iParentId, $this->_iLevel, 1);
  121. $aResult = $this->_bIsApi ? [] : ['grid' => $this->getCode(false), 'blink' => $iId];
  122. }
  123. else
  124. $aResult = ['msg' => _t($CNF['T']['err_cannot_perform'])];
  125. return $this->_getActionResult($aResult);
  126. }
  127. if($this->_bIsApi)
  128. return $this->getFormBlockAPI($oForm, $sAction);
  129. bx_import('BxTemplFunctions');
  130. $sContent = BxTemplFunctions::getInstance()->popupBox($this->_oModule->_oConfig->getHtmlIds('popup_content_node'), $this->_parseNodeName(_t($CNF['T']['popup_title_content_node_add'])), $this->_oModule->_oTemplate->parseHtmlByName('popup_content_node.html', [
  131. 'form_id' => $oForm->getId(),
  132. 'form' => $oForm->getCode(true),
  133. 'object' => $this->_sObject,
  134. 'action' => $sAction
  135. ]));
  136. echoJson(['popup' => ['html' => $sContent, 'options' => ['closeOnOuterClick' => false]]]);
  137. }
  138. public function performActionEdit()
  139. {
  140. $CNF = &$this->_oModule->_oConfig->CNF;
  141. $sAction = 'edit';
  142. $aEntryInfo = $this->_oModule->_oDb->getContentInfoById($this->_iEntryId);
  143. if(($mixedResult = $this->_oModule->checkAllowedEdit($aEntryInfo)) !== CHECK_ACTION_RESULT_ALLOWED)
  144. return $this->_getActionResult(['msg' => $mixedResult]);
  145. $aIds = $this->_getIds();
  146. if($aIds === false)
  147. return $this->_getActionResult([]);
  148. $aNode = $this->_oModule->_oDb->getContentNodes(['sample' => 'id', 'id' => array_shift($aIds)]);
  149. if(!is_array($aNode) || empty($aNode))
  150. return $this->_getActionResult([]);
  151. $sForm = $CNF['OBJECT_FORM_CNT_NODE_DISPLAY_EDIT'];
  152. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_CNT_NODE'], $CNF['OBJECT_FORM_CNT_NODE_DISPLAY_EDIT']);
  153. $oForm->setId($sForm);
  154. $oForm->setName($sForm);
  155. $oForm->setAction(BX_DOL_URL_ROOT . bx_append_url_params('grid.php', ['o' => $this->_sObject, 'a' => $sAction, 'entry_id' => $this->_iEntryId, 'parent_id' => $this->_iParentId, 'id' => $aNode['id']]));
  156. $oForm->setData($this->_iParentId);
  157. $oForm->initChecker($aNode);
  158. if($oForm->isSubmittedAndValid()) {
  159. if($oForm->update($aNode['id']) !== false)
  160. $aResult = $this->_bIsApi ? [] : ['grid' => $this->getCode(false), 'blink' => $aNode['id']];
  161. else
  162. $aResult = ['msg' => _t($CNF['T']['err_cannot_perform'])];
  163. return $this->_getActionResult($aResult);
  164. }
  165. if($this->_bIsApi)
  166. return $this->getFormBlockAPI($oForm, $sAction, $aNode['id']);
  167. bx_import('BxTemplFunctions');
  168. $sContent = BxTemplFunctions::getInstance()->popupBox($this->_oModule->_oConfig->getHtmlIds('popup_content_node'), $this->_parseNodeName(_t($CNF['T']['popup_title_content_node_edit'])), $this->_oModule->_oTemplate->parseHtmlByName('popup_content_node.html', [
  169. 'form_id' => $oForm->getId(),
  170. 'form' => $oForm->getCode(true),
  171. 'object' => $this->_sObject,
  172. 'action' => $sAction
  173. ]));
  174. return echoJson(['popup' => ['html' => $sContent, 'options' => ['closeOnOuterClick' => false]]]);
  175. }
  176. protected function _getCellHeaderCnL2($sKey, $aField)
  177. {
  178. if($this->_iLevelMax < 2 || $this->_iLevel >= 2)
  179. return $this->_bIsApi ? [] : '';
  180. $aField['title'] = ucfirst($this->_aLevelToNodePl[2]);
  181. return parent::_getCellHeaderDefault($sKey, $aField);
  182. }
  183. protected function _getCellHeaderCnL3($sKey, $aField)
  184. {
  185. if($this->_iLevelMax < 3 || $this->_iLevel >= 3)
  186. return $this->_bIsApi ? [] : '';
  187. $aField['title'] = ucfirst($this->_aLevelToNodePl[3]);
  188. return parent::_getCellHeaderDefault($sKey, $aField);
  189. }
  190. protected function _getCellHeaderCounters($sKey, $aField)
  191. {
  192. if($this->_iLevel != $this->_iLevelMax)
  193. return $this->_bIsApi ? [] : '';
  194. return parent::_getCellHeaderDefault($sKey, $aField);
  195. }
  196. protected function _getCellTitle($mixedValue, $sKey, $aField, $aRow)
  197. {
  198. $sLink = bx_append_url_params($this->_sPageUrl, ['parent_id' => $aRow['id']]);
  199. if($this->_bIsApi)
  200. return ['type' => 'link', 'data' => [
  201. 'text' => $mixedValue,
  202. 'url' => bx_api_get_relative_url($sLink)
  203. ]];
  204. $mixedValue = $this->_oModule->_oTemplate->parseHtmlByName('name_link.html', [
  205. 'href' => $sLink,
  206. 'title' => bx_html_attribute($mixedValue),
  207. 'content' => $mixedValue
  208. ]);
  209. return parent::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
  210. }
  211. protected function _getCellCnL2($mixedValue, $sKey, $aField, $aRow)
  212. {
  213. if($this->_iLevelMax < 2 || $this->_iLevel >= 2)
  214. return $this->_bIsApi ? [] : '';
  215. return parent::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
  216. }
  217. protected function _getCellCnL3($mixedValue, $sKey, $aField, $aRow)
  218. {
  219. if($this->_iLevelMax < 3 || $this->_iLevel >= 3)
  220. return $this->_bIsApi ? [] : '';
  221. return parent::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
  222. }
  223. protected function _getCellCounters($mixedValue, $sKey, $aField, $aRow)
  224. {
  225. if($this->_iLevel != $this->_iLevelMax)
  226. return $this->_bIsApi ? [] : '';
  227. $mixedCounters = '';
  228. if(!empty($mixedValue) && ($aCounters = json_decode(html_entity_decode($mixedValue), true)))
  229. $mixedCounters = $this->_oModule->_oTemplate->getCounters($aCounters);
  230. if($this->_bIsApi) {
  231. $sResult = '';
  232. foreach($mixedCounters as $sUsage => $aCounters) {
  233. foreach($aCounters as $iKey => $aCounter)
  234. $aCounters[$iKey] = $aCounter['value'] . ' ' . $aCounter['title'];
  235. $sResult .= ucfirst(_t('_bx_courses_txt_' . $sUsage)) . ': ' . implode(', ', $aCounters) . ' ';
  236. }
  237. return ['type' => 'text', 'value' => trim($sResult)];
  238. }
  239. return parent::_getCellDefault($mixedCounters, $sKey, $aField, $aRow);
  240. }
  241. protected function _getCellAdded($mixedValue, $sKey, $aField, $aRow)
  242. {
  243. if($this->_bIsApi)
  244. return ['type' => 'time', 'data' => $mixedValue];
  245. return parent::_getCellDefault(bx_time_js($mixedValue), $sKey, $aField, $aRow);
  246. }
  247. protected function _getActionBack($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = [])
  248. {
  249. if(empty($this->_aParentInfo))
  250. return $this->_bIsApi ? [] : '';
  251. $sUrl = $this->_sPageUrl;
  252. if(!empty($this->_aParentInfo['parent_id']))
  253. $sUrl = bx_append_url_params($sUrl, ['parent_id' => $this->_aParentInfo['parent_id']]);
  254. if($this->_bIsApi)
  255. return array_merge($a, ['name' => $sKey, 'type' => 'link', 'link' => bx_api_get_relative_url($sUrl)]);
  256. $a['attr'] = array_merge($a['attr'], [
  257. "onclick" => "window.open('" . $sUrl . "','_self');"
  258. ]);
  259. return $this->_getActionDefault ($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  260. }
  261. protected function _getActionAdd($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = [])
  262. {
  263. if($this->_iLevel > $this->_iLevelMax)
  264. return '';
  265. $a['title'] = $this->_parseNodeName($a['title']);
  266. return $this->_getActionDefault ($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  267. }
  268. protected function _delete($mixedId)
  269. {
  270. if($this->_iLevel > 1)
  271. $this->_oModule->_oDb->updateContentStructureCounters($this->_iParentId, $this->_iLevel, -1);
  272. $this->_oModule->_oDb->deleteContentStructureNode(['node_id' => (int)$mixedId]);
  273. return parent::_delete($mixedId);
  274. }
  275. protected function _getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage)
  276. {
  277. if(!$this->_iEntryId)
  278. return [];
  279. $this->_aOptions['source'] .= $this->_oModule->_oDb->prepareAsString(" AND `tcn`.`entry_id`=? AND `tcs`.`parent_id`=?", $this->_iEntryId, $this->_iParentId);
  280. return parent::_getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage);
  281. }
  282. protected function _updateOrder($mixedId, $iOrder)
  283. {
  284. $CNF = &$this->_oModule->_oConfig->CNF;
  285. $sTable = $CNF['TABLE_CNT_STRUCTURE'];
  286. $sFieldId = 'node_id';
  287. $sFieldOrder = $this->_aOptions['field_order'];
  288. return BxDolDb::getInstance()->query("UPDATE `{$sTable}` SET `{$sFieldOrder}` = :order WHERE `{$sFieldId}` = :id", [
  289. 'id' => $mixedId,
  290. 'order' => $iOrder
  291. ]);
  292. }
  293. protected function _switcherChecked2State($isChecked)
  294. {
  295. return $isChecked ? 'active' : 'hidden';
  296. }
  297. protected function _switcherState2Checked($mixedState)
  298. {
  299. return 'active' == $mixedState ? true : false;
  300. }
  301. protected function _getNodeLevel()
  302. {
  303. return $this->_oModule->getNodeLevelByParent($this->_aParentInfo);
  304. }
  305. protected function _parseNodeName($s)
  306. {
  307. return bx_replace_markers($s, [
  308. 'node' => isset($this->_aLevelToNode[$this->_iLevel]) ? $this->_aLevelToNode[$this->_iLevel]: _t('_undefined')
  309. ]);
  310. }
  311. protected function _getIds()
  312. {
  313. $aIds = bx_get('ids');
  314. if(!$aIds || !is_array($aIds)) {
  315. $iId = (int)bx_get('id');
  316. if(!$iId)
  317. return false;
  318. $aIds = [$iId];
  319. }
  320. return $aIds;
  321. }
  322. }
  323. /** @} */