BxTasksGridPreValues.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. protected function _getActionAdd($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = [])
  87. {
  88. if(!$this->_sList)
  89. $isDisabled = true;
  90. return parent::_getActionDefault($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  91. }
  92. protected function _getFilterOnChange()
  93. {
  94. return $this->_oModule->_oConfig->getJsObject('pre_values') . '.onChangeList(this)';
  95. }
  96. protected function _getFilterControls()
  97. {
  98. parent::_getFilterControls();
  99. $sContent = $this->_getFilterSelectOne('list', $this->_sList, $this->_oModule->getPreLists(), '_bx_tasks_grid_filter_item_title_pv_select_one_list');
  100. $sContent .= $this->_getSearchInput();
  101. return $sContent;
  102. }
  103. protected function _getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage)
  104. {
  105. if(!$this->_iContextPid || !$this->_sList)
  106. return [];
  107. $this->_aOptions['source'] .= $this->_oModule->_oDb->prepareAsString(' AND `context_id`=? AND `list`=?', $this->_iContextPid, $this->_sList);
  108. return parent::_getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage);
  109. }
  110. protected function _getForm($sAction, $aItem = [])
  111. {
  112. $CNF = &$this->_oModule->_oConfig->CNF;
  113. $aActionParams = [
  114. 'o' => $this->_sObject,
  115. 'a' => $sAction,
  116. 'profile_id' => $this->_iContextPid,
  117. 'list' => $this->_sList
  118. ];
  119. if($aItem)
  120. $aActionParams['id'] = $aItem['id'];
  121. $aForm = [
  122. 'form_attrs' => [
  123. 'id' => 'bx-tasks-pre-value-' . $sAction,
  124. 'action' => BX_DOL_URL_ROOT . bx_append_url_params('grid.php', $aActionParams),
  125. 'method' => BX_DOL_FORM_METHOD_DEFAULT
  126. ],
  127. 'params' => [
  128. 'db' => [
  129. 'table' => $CNF['TABLE_PRE_VALUES'],
  130. 'key' => 'id',
  131. 'uri' => '',
  132. 'uri_title' => '',
  133. 'submit_name' => 'do_submit'
  134. ],
  135. ],
  136. 'inputs' => [
  137. 'value' => [
  138. 'type' => 'hidden',
  139. 'name' => 'value',
  140. 'value' => (int)$this->_oModule->_oDb->getPreValues([
  141. 'sample' => 'value_to_use',
  142. 'context_id' => $this->_iContextPid,
  143. 'list' => $this->_sList
  144. ]),
  145. 'db' => [
  146. 'pass' => 'Int',
  147. ]
  148. ],
  149. 'color' => [
  150. 'type' => 'rgb',
  151. 'name' => 'color',
  152. 'caption' => _t('_bx_tasks_form_pv_input_color'),
  153. 'value' => '',
  154. 'required' => '0',
  155. 'db' => [
  156. 'pass' => 'Xss',
  157. ]
  158. ],
  159. 'title' => [
  160. 'type' => 'text',
  161. 'name' => 'title',
  162. 'caption' => _t('_bx_tasks_form_pv_input_title'),
  163. 'value' => '',
  164. 'required' => '1',
  165. 'db' => [
  166. 'pass' => 'Xss',
  167. ],
  168. 'checker' => [
  169. 'func' => 'Avail',
  170. 'params' => [],
  171. 'error' => _t('_bx_tasks_form_pv_input_err'),
  172. ],
  173. ],
  174. 'controls' => [
  175. 'name' => 'controls',
  176. 'type' => 'input_set',
  177. [
  178. 'type' => 'submit',
  179. 'name' => 'do_submit',
  180. 'value' => _t('_bx_tasks_form_pv_input_submit'),
  181. ], [
  182. 'type' => 'reset',
  183. 'name' => 'do_cancel',
  184. 'value' => _t('_bx_tasks_form_pv_input_cancel'),
  185. 'attrs' => [
  186. 'onclick' => "$('.bx-popup-applied:visible').dolPopupHide()",
  187. 'class' => 'bx-def-margin-sec-left',
  188. ],
  189. ]
  190. ]
  191. ]
  192. ];
  193. if($this->_aListInfo && (int)$this->_aListInfo['use_color'] == 0)
  194. unset($aForm['inputs']['color']);
  195. return new BxTemplFormView($aForm);
  196. }
  197. protected function _getIds()
  198. {
  199. if(($aIds = bx_get('ids')) && is_array($aIds))
  200. return reset($aIds);
  201. if(($iId = bx_get('id')) !== false)
  202. return (int)$iId;
  203. return false;
  204. }
  205. }
  206. /** @} */