BxTasksGridPreValues.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 performActionAdd()
  42. {
  43. if(!$this->_sList)
  44. return echoJson(['msg' => _t('_bx_tasks_txt_err_empty_pre_list')]);
  45. $sAction = 'add';
  46. $oForm = $this->_getForm($sAction);
  47. $oForm->initChecker();
  48. if($oForm->isSubmittedAndValid()) {
  49. if(($iId = (int)$oForm->insert(['context_id' => $this->_iContextPid, 'list' => $this->_sList])) != 0)
  50. $aRes = ['grid' => $this->getCode(false), 'blink' => $iId];
  51. else
  52. $aRes = ['msg' => _t('_bx_tasks_txt_err_cannot_perform_action')];
  53. return echoJson($aRes);
  54. }
  55. $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', [
  56. 'form_id' => $oForm->aFormAttrs['id'],
  57. 'form' => $oForm->getCode(true),
  58. 'object' => $this->_sObject,
  59. 'action' => $sAction
  60. ]));
  61. return echoJson(['popup' => ['html' => $sContent, 'options' => ['closeOnOuterClick' => false]]]);
  62. }
  63. public function performActionEdit()
  64. {
  65. $sAction = 'edit';
  66. $aItem = $this->_oModule->_oDb->getPreValues(['sample' => 'id', 'id' => $this->_getIds()]);
  67. if(!$aItem)
  68. return echoJson([]);
  69. $oForm = $this->_getForm($sAction, $aItem);
  70. $oForm->initChecker($aItem);
  71. if($oForm->isSubmittedAndValid()) {
  72. if($oForm->update($aItem['id']) !== false)
  73. $aRes = ['grid' => $this->getCode(false), 'blink' => $aItem['id']];
  74. else
  75. $aRes = ['msg' => _t('_bx_tasks_txt_err_cannot_perform_action')];
  76. return echoJson($aRes);
  77. }
  78. $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', [
  79. 'form_id' => $oForm->aFormAttrs['id'],
  80. 'form' => $oForm->getCode(true),
  81. 'object' => $this->_sObject,
  82. 'action' => $sAction
  83. ]));
  84. echoJson(['popup' => ['html' => $sContent, 'options' => ['closeOnOuterClick' => false]]]);
  85. }
  86. public function performActionActivate()
  87. {
  88. parent::performActionEnable(1);
  89. }
  90. public function performActionDeactivate()
  91. {
  92. parent::performActionEnable(0);
  93. }
  94. protected function _getActionAdd($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = [])
  95. {
  96. if(!$this->_sList)
  97. $isDisabled = true;
  98. return parent::_getActionDefault($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  99. }
  100. protected function _getFilterOnChange()
  101. {
  102. return $this->_oModule->_oConfig->getJsObject('pre_values') . '.onChangeList(this)';
  103. }
  104. protected function _getFilterControls()
  105. {
  106. parent::_getFilterControls();
  107. $sContent = $this->_getFilterSelectOne('list', $this->_sList, $this->_oModule->getPreLists(), '_bx_tasks_grid_filter_item_title_pv_select_one_list');
  108. $sContent .= $this->_getSearchInput();
  109. return $sContent;
  110. }
  111. protected function _getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage)
  112. {
  113. if(!$this->_iContextPid || !$this->_sList)
  114. return [];
  115. $this->_aOptions['source'] .= $this->_oModule->_oDb->prepareAsString(' AND `context_id`=? AND `list`=?', $this->_iContextPid, $this->_sList);
  116. return parent::_getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage);
  117. }
  118. protected function _getForm($sAction, $aItem = [])
  119. {
  120. $CNF = &$this->_oModule->_oConfig->CNF;
  121. $aActionParams = [
  122. 'o' => $this->_sObject,
  123. 'a' => $sAction,
  124. 'profile_id' => $this->_iContextPid,
  125. 'list' => $this->_sList
  126. ];
  127. if($aItem)
  128. $aActionParams['id'] = $aItem['id'];
  129. $aForm = [
  130. 'form_attrs' => [
  131. 'id' => 'bx-tasks-pre-value-' . $sAction,
  132. 'action' => BX_DOL_URL_ROOT . bx_append_url_params('grid.php', $aActionParams),
  133. 'method' => BX_DOL_FORM_METHOD_DEFAULT
  134. ],
  135. 'params' => [
  136. 'db' => [
  137. 'table' => $CNF['TABLE_PRE_VALUES'],
  138. 'key' => 'id',
  139. 'uri' => '',
  140. 'uri_title' => '',
  141. 'submit_name' => 'do_submit'
  142. ],
  143. ],
  144. 'inputs' => [
  145. 'value' => [
  146. 'type' => 'hidden',
  147. 'name' => 'value',
  148. 'value' => (int)$this->_oModule->_oDb->getPreValues([
  149. 'sample' => 'value_to_use',
  150. 'context_id' => $this->_iContextPid,
  151. 'list' => $this->_sList
  152. ]),
  153. 'db' => [
  154. 'pass' => 'Int',
  155. ]
  156. ],
  157. 'color' => [
  158. 'type' => 'rgb',
  159. 'name' => 'color',
  160. 'caption' => _t('_bx_tasks_form_pv_input_color'),
  161. 'value' => '',
  162. 'required' => '0',
  163. 'db' => [
  164. 'pass' => 'Xss',
  165. ]
  166. ],
  167. 'title' => [
  168. 'type' => 'text',
  169. 'name' => 'title',
  170. 'caption' => _t('_bx_tasks_form_pv_input_title'),
  171. 'value' => '',
  172. 'required' => '1',
  173. 'db' => [
  174. 'pass' => 'Xss',
  175. ],
  176. 'checker' => [
  177. 'func' => 'Avail',
  178. 'params' => [],
  179. 'error' => _t('_bx_tasks_form_pv_input_err'),
  180. ],
  181. ],
  182. 'controls' => [
  183. 'name' => 'controls',
  184. 'type' => 'input_set',
  185. [
  186. 'type' => 'submit',
  187. 'name' => 'do_submit',
  188. 'value' => _t('_bx_tasks_form_pv_input_submit'),
  189. ], [
  190. 'type' => 'reset',
  191. 'name' => 'do_cancel',
  192. 'value' => _t('_bx_tasks_form_pv_input_cancel'),
  193. 'attrs' => [
  194. 'onclick' => "$('.bx-popup-applied:visible').dolPopupHide()",
  195. 'class' => 'bx-def-margin-sec-left',
  196. ],
  197. ]
  198. ]
  199. ]
  200. ];
  201. if($this->_aListInfo && (int)$this->_aListInfo['use_color'] == 0)
  202. unset($aForm['inputs']['color']);
  203. return new BxTemplFormView($aForm);
  204. }
  205. protected function _getIds()
  206. {
  207. if(($aIds = bx_get('ids')) && is_array($aIds))
  208. return reset($aIds);
  209. if(($iId = bx_get('id')) !== false)
  210. return (int)$iId;
  211. return false;
  212. }
  213. }
  214. /** @} */