1
0

BxTasksGridPreValues.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. * @defgroup Tasks Tasks
  6. * @ingroup UnaModules
  7. *
  8. * @{
  9. */
  10. class BxTasksGridPreValues extends BxBaseModGeneralGrid
  11. {
  12. protected $_iContextPid;
  13. protected $_sList;
  14. protected $_aListInfo;
  15. public function __construct ($aOptions, $oTemplate = false)
  16. {
  17. $this->_sModule = 'bx_tasks';
  18. parent::__construct ($aOptions, $oTemplate);
  19. $this->_aConfirmMessages = [
  20. 'delete' => _t('_bx_tasks_grid_action_confirm_pv_delete')
  21. ];
  22. if(($sFfName = 'profile_id') && ($iValue = bx_get($sFfName)) !== false)
  23. $this->setContextPid($iValue);
  24. if(($sFfName = 'list') && ($sValue = bx_get($sFfName)) !== false)
  25. $this->setList($sValue);
  26. }
  27. public function setContextPid($iContextPid)
  28. {
  29. $this->_iContextPid = (int)$iContextPid;
  30. $this->_aQueryAppend['profile_id'] = $this->_iContextPid;
  31. }
  32. public function setList($sList)
  33. {
  34. $this->_sList = bx_process_input($sList);
  35. $this->_aQueryAppend['list'] = $this->_sList;
  36. $this->_aListInfo = $this->_oModule->_oDb->getPreLists([
  37. 'sample' => 'name',
  38. 'name' => $this->_sList
  39. ]);
  40. }
  41. public function getFormCallBackUrlAPI($sAction, $iId = 0)
  42. {
  43. return '/api.php?r=system/perfom_action_api/TemplServiceGrid/&params[]=&o=' . $this->_sObject . '&a=' . $sAction . '&profile_id=' . $this->_iContextPid . '&list=' . $this->_sList . '&id=' . $iId;
  44. }
  45. public function performActionAdd()
  46. {
  47. if(!$this->_sList)
  48. return $this->_getActionResult(($sMsg = _t('_bx_tasks_txt_err_empty_pre_list')) && $this->_bIsApi ? bx_api_get_msg($sMsg) : ['msg' => $sMsg]);
  49. $sAction = 'add';
  50. $oForm = $this->_getForm($sAction);
  51. $oForm->initChecker();
  52. if($oForm->isSubmittedAndValid()) {
  53. $iId = (int)$oForm->insert(['context_id' => $this->_iContextPid, 'list' => $this->_sList]);
  54. if(!$iId)
  55. return $this->_getActionResult(['msg' => _t('_bx_tasks_txt_err_cannot_perform_action')]);
  56. return $this->_bIsApi ? [] : echoJson(['grid' => $this->getCode(false), 'blink' => $iId]);
  57. }
  58. if($this->_bIsApi)
  59. return $this->getFormBlockAPI($oForm, $sAction);
  60. $sContent = BxTemplFunctions::getInstance()->popupBox($this->_oModule->_oConfig->getHtmlIds('pre_values_popup') . $sAction, _t('_bx_tasks_popup_pv_title_' . $sAction), $this->_oModule->_oTemplate->parseHtmlByName('popup_pre_values.html', [
  61. 'form_id' => $oForm->aFormAttrs['id'],
  62. 'form' => $oForm->getCode(true),
  63. 'object' => $this->_sObject,
  64. 'action' => $sAction
  65. ]));
  66. return echoJson(['popup' => ['html' => $sContent, 'options' => ['closeOnOuterClick' => false]]]);
  67. }
  68. public function performActionEdit()
  69. {
  70. $sAction = 'edit';
  71. $iItemId = $this->_getIds();
  72. $aItem = $this->_oModule->_oDb->getPreValues(['sample' => 'id', 'id' => $iItemId]);
  73. if(!$aItem)
  74. return $this->_bIsApi ? [] : echoJson([]);
  75. $oForm = $this->_getForm($sAction, $aItem);
  76. $oForm->initChecker($aItem);
  77. if($oForm->isSubmittedAndValid()) {
  78. if(!$oForm->update($aItem['id']))
  79. return $this->_getActionResult(['msg' => _t('_bx_tasks_txt_err_cannot_perform_action')]);
  80. return $this->_bIsApi ? [] : echoJson(['grid' => $this->getCode(false), 'blink' => $iItemId]);
  81. }
  82. if($this->_bIsApi)
  83. return $this->getFormBlockAPI($oForm, $sAction, $iItemId);
  84. $sContent = BxTemplFunctions::getInstance()->popupBox($this->_oModule->_oConfig->getHtmlIds('pre_values_popup') . $sAction, _t('_bx_tasks_popup_pv_title_' . $sAction), $this->_oModule->_oTemplate->parseHtmlByName('popup_pre_values.html', [
  85. 'form_id' => $oForm->aFormAttrs['id'],
  86. 'form' => $oForm->getCode(true),
  87. 'object' => $this->_sObject,
  88. 'action' => $sAction
  89. ]));
  90. echoJson(['popup' => ['html' => $sContent, 'options' => ['closeOnOuterClick' => false]]]);
  91. }
  92. public function performActionActivate()
  93. {
  94. parent::performActionEnable(1);
  95. }
  96. public function performActionDeactivate()
  97. {
  98. parent::performActionEnable(0);
  99. }
  100. protected function _getActionAdd($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = [])
  101. {
  102. if(!$this->_sList)
  103. $isDisabled = true;
  104. return parent::_getActionDefault($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  105. }
  106. protected function _getFilterOnChange()
  107. {
  108. return $this->_oModule->_oConfig->getJsObject('pre_values') . '.onChangeList(this)';
  109. }
  110. protected function _getFilterControls()
  111. {
  112. parent::_getFilterControls();
  113. $sContent = $this->_getFilterSelectOne('list', $this->_sList, $this->_oModule->getPreLists(), '_bx_tasks_grid_filter_item_title_pv_select_one_list');
  114. $sContent .= $this->_getSearchInput();
  115. return $sContent;
  116. }
  117. protected function _getFilterControlsAPI($aFilters = [])
  118. {
  119. $aFilters = [
  120. 'list' => [
  121. ['value' => '', 'title' => _t('_bx_tasks_grid_filter_item_title_pv_select_one_list')]
  122. ]
  123. ];
  124. $aLists = $this->_oModule->getPreLists();
  125. foreach($aLists as $sKey => $sValue)
  126. $aFilters['list'][] = ['value' => $sKey, 'title' => $sValue];
  127. return parent::_getFilterControlsAPI($aFilters);
  128. }
  129. protected function _getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage)
  130. {
  131. if(!$this->_iContextPid || !$this->_sList)
  132. return [];
  133. $this->_aOptions['source'] .= $this->_oModule->_oDb->prepareAsString(' AND `context_id`=? AND `list`=?', $this->_iContextPid, $this->_sList);
  134. return parent::_getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage);
  135. }
  136. protected function _getForm($sAction, $aItem = [])
  137. {
  138. $CNF = &$this->_oModule->_oConfig->CNF;
  139. $aActionParams = [
  140. 'o' => $this->_sObject,
  141. 'a' => $sAction,
  142. 'profile_id' => $this->_iContextPid,
  143. 'list' => $this->_sList
  144. ];
  145. if($aItem)
  146. $aActionParams['id'] = $aItem['id'];
  147. $aForm = [
  148. 'form_attrs' => [
  149. 'id' => 'bx-tasks-pre-value-' . $sAction,
  150. 'action' => BX_DOL_URL_ROOT . bx_append_url_params('grid.php', $aActionParams),
  151. 'method' => BX_DOL_FORM_METHOD_DEFAULT
  152. ],
  153. 'params' => [
  154. 'db' => [
  155. 'table' => $CNF['TABLE_PRE_VALUES'],
  156. 'key' => 'id',
  157. 'uri' => '',
  158. 'uri_title' => '',
  159. 'submit_name' => 'do_submit'
  160. ],
  161. ],
  162. 'inputs' => [
  163. 'value' => [
  164. 'type' => 'hidden',
  165. 'name' => 'value',
  166. 'value' => (int)$this->_oModule->_oDb->getPreValues([
  167. 'sample' => 'value_to_use',
  168. 'context_id' => $this->_iContextPid,
  169. 'list' => $this->_sList
  170. ]),
  171. 'db' => [
  172. 'pass' => 'Int',
  173. ]
  174. ],
  175. 'color' => [
  176. 'type' => 'rgb',
  177. 'name' => 'color',
  178. 'caption' => _t('_bx_tasks_form_pv_input_color'),
  179. 'value' => '',
  180. 'required' => '0',
  181. 'db' => [
  182. 'pass' => 'Xss',
  183. ]
  184. ],
  185. 'title' => [
  186. 'type' => 'text',
  187. 'name' => 'title',
  188. 'caption' => _t('_bx_tasks_form_pv_input_title'),
  189. 'value' => '',
  190. 'required' => '1',
  191. 'db' => [
  192. 'pass' => 'Xss',
  193. ],
  194. 'checker' => [
  195. 'func' => 'Avail',
  196. 'params' => [],
  197. 'error' => _t('_bx_tasks_form_pv_input_err'),
  198. ],
  199. ],
  200. 'controls' => [
  201. 'name' => 'controls',
  202. 'type' => 'input_set',
  203. [
  204. 'type' => 'submit',
  205. 'name' => 'do_submit',
  206. 'value' => _t('_bx_tasks_form_pv_input_submit'),
  207. ], [
  208. 'type' => 'reset',
  209. 'name' => 'do_cancel',
  210. 'value' => _t('_bx_tasks_form_pv_input_cancel'),
  211. 'attrs' => [
  212. 'onclick' => "$('.bx-popup-applied:visible').dolPopupHide()",
  213. 'class' => 'bx-def-margin-sec-left',
  214. ],
  215. ]
  216. ]
  217. ]
  218. ];
  219. if($this->_aListInfo && (int)$this->_aListInfo['use_color'] == 0)
  220. unset($aForm['inputs']['color']);
  221. return new BxTemplFormView($aForm);
  222. }
  223. protected function _getIds()
  224. {
  225. if(($aIds = bx_get('ids')) && is_array($aIds))
  226. return reset($aIds);
  227. if(($iId = bx_get('id')) !== false)
  228. return (int)$iId;
  229. return false;
  230. }
  231. }
  232. /** @} */