BxTasksGridBudgetContextAdministration.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. require_once(BX_DOL_DIR_STUDIO_INC . 'utils.inc.php');
  11. class BxTasksGridBudgetContextAdministration extends BxBaseModGeneralGrid
  12. {
  13. protected $_iLogged;
  14. protected $_iContextPid;
  15. public function __construct ($aOptions, $oTemplate = false)
  16. {
  17. $this->_sModule = 'bx_tasks';
  18. parent::__construct ($aOptions, $oTemplate);
  19. $this->_iLogged = bx_get_logged_profile_id();
  20. if(($iContextPid = bx_get('context_pid')) !== false)
  21. $this->setContextPid($iContextPid);
  22. }
  23. public function setContextPid($iContextPid)
  24. {
  25. $this->_iContextPid = (int)$iContextPid;
  26. $this->_aQueryAppend['context_pid'] = $this->_iContextPid;
  27. }
  28. public function getFormCallBackUrlAPI($sAction, $iId = 0)
  29. {
  30. return '/api.php?r=system/perfom_action_api/TemplServiceGrid/&params[]=&o=' . $this->_sObject . '&a=' . $sAction . '&context_pid=' . $this->_iContextPid . '&id=' . $iId;
  31. }
  32. public function getFormBlockTitleAPI($sAction, $iId = 0)
  33. {
  34. $sResult = '';
  35. switch($sAction) {
  36. case 'add':
  37. $sResult = _t('_bx_tasks_grid_popup_title_bdt_add');
  38. break;
  39. case 'edit':
  40. $sResult = _t('_bx_tasks_grid_popup_title_bdt_edit');
  41. break;
  42. default:
  43. parent::getFormBlockTitleAPI($sAction, $iId);
  44. }
  45. return $sResult;
  46. }
  47. public function performActionAdd()
  48. {
  49. if(!$this->_isAllowedByConext())
  50. return $this->_getActionResult([]);
  51. $sAction = 'add';
  52. $oForm = $this->_getFormObject($sAction);
  53. $oForm->initChecker();
  54. if($oForm->isSubmittedAndValid()) {
  55. $iNow = time();
  56. $aValsToAdd = [
  57. 'context_id' => $this->_iContextPid,
  58. 'profile_id' => $this->_iLogged,
  59. 'date' => $iNow
  60. ];
  61. $iTrackId = $oForm->insert($aValsToAdd);
  62. if(!$iTrackId)
  63. return $this->_getActionResult(['msg' => _t('_bx_tasks_txt_err_cannot_perform_action')]);
  64. $this->_oModule->_oDb->insertBudget($this->_iContextPid, 60 * $oForm->getCleanValue('value'));
  65. return $this->_bIsApi ? [] : echoJson(['grid' => $this->getCode(false), 'blink' => $iTrackId]);
  66. }
  67. if($this->_bIsApi)
  68. return $this->getFormBlockAPI($oForm, $sAction);
  69. $sContent = BxTemplFunctions::getInstance()->popupBox($this->_oModule->_oConfig->getHtmlIds('budget_popup'), _t('_bx_tasks_grid_popup_title_bdt_add'), $this->_oModule->_oTemplate->parseHtmlByName('popup_budget.html', [
  70. 'form_id' => $oForm->getId(),
  71. 'form' => $oForm->getCode(true),
  72. 'object' => $this->_sObject,
  73. 'action' => $sAction
  74. ]));
  75. return echoJson(['popup' => ['html' => $sContent, 'options' => ['closeOnOuterClick' => false]]]);
  76. }
  77. public function performActionEdit()
  78. {
  79. if(!$this->_isAllowedByConext())
  80. return $this->_getActionResult([]);
  81. $sAction = 'edit';
  82. $iTrackId = $this->_getId();
  83. $aTrack = $this->_oModule->_oDb->getBudgetTracks(['sample' => 'id', 'id' => $iTrackId]);
  84. if(empty($aTrack) || !is_array($aTrack) || (int)$aTrack['context_id'] !== $this->_iContextPid)
  85. return $this->_getActionResult([]);
  86. $oForm = $this->_getFormObject($sAction, $aTrack);
  87. $oForm->initChecker($aTrack);
  88. if($oForm->isSubmittedAndValid()) {
  89. if(!$oForm->update($iTrackId))
  90. return $this->_getActionResult(['msg' => _t('_bx_tasks_txt_err_cannot_perform_action')]);
  91. $this->_oModule->_oDb->updateBudgetTotal($this->_iContextPid, 60 * ($oForm->getCleanValue('value') - (int)$aTrack['value']));
  92. return $this->_bIsApi ? [] : echoJson(['grid' => $this->getCode(false), 'blink' => $iTrackId]);
  93. }
  94. if($this->_bIsApi)
  95. return $this->getFormBlockAPI($oForm, $sAction, $iTrackId);
  96. $sContent = BxTemplFunctions::getInstance()->popupBox($this->_oModule->_oConfig->getHtmlIds('budget_popup'), _t('_bx_tasks_grid_popup_title_bdt_edit'), $this->_oModule->_oTemplate->parseHtmlByName('popup_budget.html', [
  97. 'form_id' => $oForm->getId(),
  98. 'form' => $oForm->getCode(true),
  99. 'object' => $this->_sObject,
  100. 'action' => $sAction
  101. ]));
  102. return echoJson(['popup' => ['html' => $sContent, 'options' => ['closeOnOuterClick' => false]]]);
  103. }
  104. public function performActionDelete()
  105. {
  106. if(!$this->_isAllowedByConext())
  107. return $this->_getActionResult([]);
  108. return parent::performActionDelete();
  109. }
  110. public function getCode ($isDisplayHeader = true)
  111. {
  112. if(!$this->_isAllowedByConext())
  113. return $this->_getActionResult([]);
  114. return parent::getCode($isDisplayHeader);
  115. }
  116. protected function _isAllowedByConext($iContextPid = 0)
  117. {
  118. if(!$iContextPid)
  119. $iContextPid = $this->_iContextPid;
  120. if(!$iContextPid)
  121. return false;
  122. return $this->_oModule->isAllowManageByContext($this->_iContextPid);
  123. }
  124. protected function _delete ($mixedId)
  125. {
  126. $aTrack = $this->_oModule->_oDb->getBudgetTracks(['sample' => 'id', 'id' => (int)$mixedId]);
  127. if(empty($aTrack) || !is_array($aTrack) || (int)$aTrack['context_id'] !== $this->_iContextPid)
  128. return false;
  129. $bResult = parent::_delete($mixedId);
  130. if($bResult)
  131. $this->_oModule->_oDb->updateBudgetTotal($this->_iContextPid, -(int)$aTrack['value']);
  132. return $bResult;
  133. }
  134. protected function _getCellProfileId($mixedValue, $sKey, $aField, $aRow)
  135. {
  136. if($this->_bIsApi)
  137. return ['type' => 'profile', 'data' => BxDolProfile::getData($mixedValue)];
  138. return parent::_getCellDefault(BxDolProfile::getInstanceMagic($mixedValue)->getUnit(), $sKey, $aField, $aRow);
  139. }
  140. protected function _getCellValue($mixedValue, $sKey, $aField, $aRow)
  141. {
  142. if (!$this->_bIsApi)
  143. $mixedValue = $this->_oModule->_oConfig->timeI2S(60 * (int)$mixedValue);
  144. return parent::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
  145. }
  146. protected function _getCellDate($mixedValue, $sKey, $aField, $aRow)
  147. {
  148. if ($this->_bIsApi)
  149. return ['type' => 'time', 'data' => $mixedValue];
  150. return parent::_getCellDefault(bx_time_js($mixedValue), $sKey, $aField, $aRow);
  151. }
  152. protected function _getFormObject($sAction, $aTrack = [])
  153. {
  154. $CNF = &$this->_oModule->_oConfig->CNF;
  155. $bActionEdit = $sAction == 'edit';
  156. $bTrack = !empty($aTrack) && is_array($aTrack);
  157. $aActionParams = [
  158. 'o' => $this->_sObject,
  159. 'a' => $sAction,
  160. 'context_pid' => $this->_iContextPid
  161. ];
  162. if($bActionEdit && $bTrack)
  163. $aActionParams['id'] = (int)$aTrack['id'];
  164. $sForm = $CNF['OBJECT_FORM_BUDGET_DISPLAY_' . strtoupper($sAction)];
  165. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_BUDGET'], $sForm);
  166. $oForm->setId($sForm);
  167. $oForm->setName($sForm);
  168. $oForm->setAction(BX_DOL_URL_ROOT . bx_append_url_params('grid.php', $aActionParams));
  169. return $oForm;
  170. }
  171. protected function _getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage)
  172. {
  173. $this->_aOptions['source'] .= $this->_oModule->_oDb->prepareAsString(" AND `context_id`=?", $this->_iContextPid);
  174. return parent::_getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage);
  175. }
  176. protected function _getId()
  177. {
  178. if(($aIds = bx_get('ids')) && is_array($aIds))
  179. return reset($aIds);
  180. if(($iId = bx_get('id')) !== false)
  181. return (int)$iId;
  182. return false;
  183. }
  184. }
  185. /** @} */