BxTasksGridPreValues.php 8.2 KB

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