| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 |
- <?php defined('BX_DOL') or die('hack attempt');
- /**
- * Copyright (c) UNA, Inc - https://una.io
- * MIT License - https://opensource.org/licenses/MIT
- * @defgroup Tasks Tasks
- * @ingroup UnaModules
- *
- * @{
- */
- require_once(BX_DOL_DIR_STUDIO_INC . 'utils.inc.php');
- class BxTasksGridBudgetContextAdministration extends BxBaseModGeneralGrid
- {
- protected $_iLogged;
- protected $_iContextPid;
- public function __construct ($aOptions, $oTemplate = false)
- {
- $this->_sModule = 'bx_tasks';
- parent::__construct ($aOptions, $oTemplate);
- $this->_iLogged = bx_get_logged_profile_id();
- if(($iContextPid = bx_get('context_pid')) !== false)
- $this->setContextPid($iContextPid);
- }
- public function setContextPid($iContextPid)
- {
- $this->_iContextPid = (int)$iContextPid;
- $this->_aQueryAppend['context_pid'] = $this->_iContextPid;
- }
- public function getFormCallBackUrlAPI($sAction, $iId = 0)
- {
- return '/api.php?r=system/perfom_action_api/TemplServiceGrid/¶ms[]=&o=' . $this->_sObject . '&a=' . $sAction . '&context_pid=' . $this->_iContextPid . '&id=' . $iId;
- }
- public function getFormBlockTitleAPI($sAction, $iId = 0)
- {
- $sResult = '';
- switch($sAction) {
- case 'add':
- $sResult = _t('_bx_tasks_grid_popup_title_bdt_add');
- break;
- case 'edit':
- $sResult = _t('_bx_tasks_grid_popup_title_bdt_edit');
- break;
- default:
- parent::getFormBlockTitleAPI($sAction, $iId);
- }
- return $sResult;
- }
-
- public function performActionAdd()
- {
- if(!$this->_isAllowedByConext())
- return $this->_getActionResult([]);
- $sAction = 'add';
- $oForm = $this->_getFormObject($sAction);
- $oForm->initChecker();
- if($oForm->isSubmittedAndValid()) {
- $iNow = time();
- $aValsToAdd = [
- 'context_id' => $this->_iContextPid,
- 'profile_id' => $this->_iLogged,
- 'date' => $iNow
- ];
- $iTrackId = $oForm->insert($aValsToAdd);
- if(!$iTrackId)
- return $this->_getActionResult(['msg' => _t('_bx_tasks_txt_err_cannot_perform_action')]);
- $this->_oModule->_oDb->insertBudget($this->_iContextPid, 60 * $oForm->getCleanValue('value'));
- return $this->_bIsApi ? [] : echoJson(['grid' => $this->getCode(false), 'blink' => $iTrackId]);
- }
- if($this->_bIsApi)
- return $this->getFormBlockAPI($oForm, $sAction);
- $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', [
- 'form_id' => $oForm->getId(),
- 'form' => $oForm->getCode(true),
- 'object' => $this->_sObject,
- 'action' => $sAction
- ]));
- return echoJson(['popup' => ['html' => $sContent, 'options' => ['closeOnOuterClick' => false]]]);
- }
- public function performActionEdit()
- {
- if(!$this->_isAllowedByConext())
- return $this->_getActionResult([]);
- $sAction = 'edit';
- $iTrackId = $this->_getId();
- $aTrack = $this->_oModule->_oDb->getBudgetTracks(['sample' => 'id', 'id' => $iTrackId]);
- if(empty($aTrack) || !is_array($aTrack) || (int)$aTrack['context_id'] !== $this->_iContextPid)
- return $this->_getActionResult([]);
- $oForm = $this->_getFormObject($sAction, $aTrack);
- $oForm->initChecker($aTrack);
- if($oForm->isSubmittedAndValid()) {
- if(!$oForm->update($iTrackId))
- return $this->_getActionResult(['msg' => _t('_bx_tasks_txt_err_cannot_perform_action')]);
- $this->_oModule->_oDb->updateBudgetTotal($this->_iContextPid, 60 * ($oForm->getCleanValue('value') - (int)$aTrack['value']));
- return $this->_bIsApi ? [] : echoJson(['grid' => $this->getCode(false), 'blink' => $iTrackId]);
- }
- if($this->_bIsApi)
- return $this->getFormBlockAPI($oForm, $sAction, $iTrackId);
- $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', [
- 'form_id' => $oForm->getId(),
- 'form' => $oForm->getCode(true),
- 'object' => $this->_sObject,
- 'action' => $sAction
- ]));
- return echoJson(['popup' => ['html' => $sContent, 'options' => ['closeOnOuterClick' => false]]]);
- }
- public function performActionDelete()
- {
- if(!$this->_isAllowedByConext())
- return $this->_getActionResult([]);
- return parent::performActionDelete();
- }
- public function getCode ($isDisplayHeader = true)
- {
- if(!$this->_isAllowedByConext())
- return $this->_getActionResult([]);
- return parent::getCode($isDisplayHeader);
- }
- protected function _isAllowedByConext($iContextPid = 0)
- {
- if(!$iContextPid)
- $iContextPid = $this->_iContextPid;
- if(!$iContextPid)
- return false;
- return $this->_oModule->isAllowManageByContext($this->_iContextPid);
- }
- protected function _delete ($mixedId)
- {
- $aTrack = $this->_oModule->_oDb->getBudgetTracks(['sample' => 'id', 'id' => (int)$mixedId]);
- if(empty($aTrack) || !is_array($aTrack) || (int)$aTrack['context_id'] !== $this->_iContextPid)
- return false;
- $bResult = parent::_delete($mixedId);
- if($bResult)
- $this->_oModule->_oDb->updateBudgetTotal($this->_iContextPid, -(int)$aTrack['value']);
- return $bResult;
- }
- protected function _getCellProfileId($mixedValue, $sKey, $aField, $aRow)
- {
- if($this->_bIsApi)
- return ['type' => 'profile', 'data' => BxDolProfile::getData($mixedValue)];
- return parent::_getCellDefault(BxDolProfile::getInstanceMagic($mixedValue)->getUnit(), $sKey, $aField, $aRow);
- }
- protected function _getCellValue($mixedValue, $sKey, $aField, $aRow)
- {
- if (!$this->_bIsApi)
- $mixedValue = $this->_oModule->_oConfig->timeI2S(60 * (int)$mixedValue);
- return parent::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
- }
- protected function _getCellDate($mixedValue, $sKey, $aField, $aRow)
- {
- if ($this->_bIsApi)
- return ['type' => 'time', 'data' => $mixedValue];
- return parent::_getCellDefault(bx_time_js($mixedValue), $sKey, $aField, $aRow);
- }
- protected function _getFormObject($sAction, $aTrack = [])
- {
- $CNF = &$this->_oModule->_oConfig->CNF;
- $bActionEdit = $sAction == 'edit';
- $bTrack = !empty($aTrack) && is_array($aTrack);
- $aActionParams = [
- 'o' => $this->_sObject,
- 'a' => $sAction,
- 'context_pid' => $this->_iContextPid
- ];
- if($bActionEdit && $bTrack)
- $aActionParams['id'] = (int)$aTrack['id'];
- $sForm = $CNF['OBJECT_FORM_BUDGET_DISPLAY_' . strtoupper($sAction)];
- $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_BUDGET'], $sForm);
- $oForm->setId($sForm);
- $oForm->setName($sForm);
- $oForm->setAction(BX_DOL_URL_ROOT . bx_append_url_params('grid.php', $aActionParams));
- return $oForm;
- }
- protected function _getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage)
- {
- $this->_aOptions['source'] .= $this->_oModule->_oDb->prepareAsString(" AND `context_id`=?", $this->_iContextPid);
- return parent::_getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage);
- }
- protected function _getId()
- {
- if(($aIds = bx_get('ids')) && is_array($aIds))
- return reset($aIds);
- if(($iId = bx_get('id')) !== false)
- return (int)$iId;
- return false;
- }
- }
- /** @} */
|