BxTasksFormEntry.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. /**
  11. * Create/Edit entry form
  12. */
  13. class BxTasksFormEntry extends BxBaseModTextFormEntry
  14. {
  15. protected $_iContextId;
  16. protected $_sGhostTemplateCover = 'form_ghost_template_cover.html';
  17. protected $_aProperties;
  18. public function __construct($aInfo, $oTemplate = false)
  19. {
  20. $this->MODULE = 'bx_tasks';
  21. parent::__construct($aInfo, $oTemplate);
  22. $this->_aProperties = $this->_oModule->_oConfig->getProperties();
  23. }
  24. public function setContextId($iContextId)
  25. {
  26. $this->_iContextId = $iContextId;
  27. }
  28. public function genViewRowValue(&$aInput)
  29. {
  30. $sValue = parent::genViewRowValue($aInput);
  31. if($this->_oModule->isAllowManage($this->_iContentId) && !empty($aInput['name']) && in_array($aInput['name'], $this->_aProperties))
  32. $sValue = $this->_oModule->_oTemplate->parseLink('javascript:void(0)', $sValue ?: _t('_undefined'), [
  33. 'onclick' => 'javascript:' . $this->_oModule->_oConfig->getJsObject('tasks') . '.processTaskEdit' . bx_gen_method_name($aInput['name']) . '(' . $this->_iContentId . ', this)'
  34. ]);
  35. return $sValue;
  36. }
  37. public function getCode($bDynamicMode = false)
  38. {
  39. $this->_replaceMarkersInControls('controls_edit');
  40. $sResult = parent::getCode($bDynamicMode);
  41. $sInclude = $this->_oModule->_oTemplate->addJs(array('tasks.js'), $bDynamicMode);
  42. $sResult .= ($bDynamicMode ? $sInclude : '') . $this->_oModule->_oTemplate->getJsCode('tasks');
  43. return $sResult;
  44. }
  45. public function initChecker ($aValues = array (), $aSpecificValues = array())
  46. {
  47. $CNF = &$this->_oModule->_oConfig->CNF;
  48. $bValues = $aValues && !empty($aValues['id']);
  49. $aContentInfo = $bValues ? $this->_oModule->_oDb->getContentInfoById($aValues['id']) : false;
  50. if(!empty($aContentInfo) && is_array($aContentInfo))
  51. $this->setContextId(abs($aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]));
  52. if($this->aParams['display'] == $CNF['OBJECT_FORM_ENTRY_DISPLAY_EDIT'] && isset($CNF['FIELD_PUBLISHED']) && isset($this->aInputs[$CNF['FIELD_PUBLISHED']]))
  53. if($bValues && in_array($aValues[$CNF['FIELD_STATUS']], array('active', 'hidden')))
  54. unset($this->aInputs[$CNF['FIELD_PUBLISHED']]);
  55. if(($sKey = 'FIELD_COVER') && isset($CNF[$sKey], $this->aInputs[$CNF[$sKey]])) {
  56. if($bValues)
  57. $this->aInputs[$CNF['FIELD_COVER']]['content_id'] = $aValues['id'];
  58. $this->aInputs[$CNF['FIELD_COVER']]['ghost_template'] = $this->_oModule->_oTemplate->parseHtmlByName($this->_sGhostTemplateCover, $this->_getCoverGhostTmplVars($aContentInfo));
  59. }
  60. if(($sKey = 'FIELD_INITIAL_MEMBERS') && isset($CNF[$sKey], $this->aInputs[$CNF[$sKey]])) {
  61. if($bValues)
  62. $this->aInputs[$CNF[$sKey]]['value'] = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTION'])->getConnectedInitiators($aValues['id']);
  63. else
  64. $this->aInputs[$CNF[$sKey]]['value'] = [bx_get_logged_profile_id()];
  65. }
  66. parent::initChecker ($aValues, $aSpecificValues);
  67. }
  68. public function insert ($aValsToAdd = array(), $isIgnore = false)
  69. {
  70. $CNF = &$this->_oModule->_oConfig->CNF;
  71. if(isset($CNF['FIELD_ADDED']) && empty($aValsToAdd[$CNF['FIELD_ADDED']])) {
  72. $iAdded = 0;
  73. if(isset($this->aInputs[$CNF['FIELD_ADDED']]))
  74. $iAdded = $this->getCleanValue($CNF['FIELD_ADDED']);
  75. if(empty($iAdded))
  76. $iAdded = time();
  77. $aValsToAdd[$CNF['FIELD_ADDED']] = $iAdded;
  78. }
  79. if(empty($aValsToAdd[$CNF['FIELD_PUBLISHED']])) {
  80. $iPublished = 0;
  81. if(isset($this->aInputs[$CNF['FIELD_PUBLISHED']]))
  82. $iPublished = $this->getCleanValue($CNF['FIELD_PUBLISHED']);
  83. if(empty($iPublished))
  84. $iPublished = time();
  85. $aValsToAdd[$CNF['FIELD_PUBLISHED']] = $iPublished;
  86. }
  87. $aValsToAdd[$CNF['FIELD_STATUS']] = $aValsToAdd[$CNF['FIELD_PUBLISHED']] > $aValsToAdd[$CNF['FIELD_ADDED']] ? 'awaiting' : 'active';
  88. $iContentId = parent::insert ($aValsToAdd, $isIgnore);
  89. if(!empty($iContentId)) {
  90. $this->processFiles($CNF['FIELD_COVER'], $iContentId, true);
  91. if(isset($this->aInputs['initial_members']))
  92. $this->_setAssignments($iContentId, $this->aInputs['initial_members']['value']);
  93. }
  94. return $iContentId;
  95. }
  96. public function update ($iContentId, $aValsToAdd = array(), &$aTrackTextFieldsChanges = null)
  97. {
  98. $CNF = &$this->_oModule->_oConfig->CNF;
  99. if(empty($aValsToAdd[$CNF['FIELD_PUBLISHED']]) && isset($this->aInputs[$CNF['FIELD_PUBLISHED']])) {
  100. $iPublished = $this->getCleanValue($CNF['FIELD_PUBLISHED']);
  101. if(empty($iPublished))
  102. $iPublished = time();
  103. $aValsToAdd[$CNF['FIELD_PUBLISHED']] = $iPublished;
  104. }
  105. if(isset($this->aInputs['initial_members'])) {
  106. $this->_setAssignments($iContentId, $this->aInputs['initial_members']['value']);
  107. }
  108. $aContentInfo = $this->_oModule->_oDb->getContentInfoById($iContentId);
  109. $aValsToAdd[$CNF['FIELD_ALLOW_VIEW_TO']] = $aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']];
  110. $iResult = parent::update ($iContentId, $aValsToAdd, $aTrackTextFieldsChanges);
  111. $this->processFiles($CNF['FIELD_COVER'], $iContentId, false);
  112. return $iResult;
  113. }
  114. public function delete ($iContentId, $aContentInfo = [])
  115. {
  116. $CNF = &$this->_oModule->_oConfig->CNF;
  117. if(($sKey = 'OBJECT_REPORTS_TIME') && !empty($CNF[$sKey]) && ($o = BxDolReport::getObjectInstance($CNF[$sKey], $iContentId)))
  118. $o->onObjectDelete();
  119. return parent::delete($iContentId);
  120. }
  121. protected function _getCoverGhostTmplVars($aContentInfo = [])
  122. {
  123. $CNF = &$this->_oModule->_oConfig->CNF;
  124. return array (
  125. 'name' => $this->aInputs[$CNF['FIELD_COVER']]['name'],
  126. 'content_id' => $this->aInputs[$CNF['FIELD_COVER']]['content_id'],
  127. 'editor_id' => isset($CNF['FIELD_TEXT_ID']) ? $CNF['FIELD_TEXT_ID'] : '',
  128. 'thumb_id' => isset($CNF['FIELD_THUMB']) && isset($aContentInfo[$CNF['FIELD_THUMB']]) ? $aContentInfo[$CNF['FIELD_THUMB']] : 0,
  129. 'name_thumb' => isset($CNF['FIELD_THUMB']) ? $CNF['FIELD_THUMB'] : ''
  130. );
  131. }
  132. protected function _getPhotoGhostTmplVars($aContentInfo = array())
  133. {
  134. $CNF = &$this->_oModule->_oConfig->CNF;
  135. return [
  136. 'name' => $this->aInputs[$CNF['FIELD_PHOTO']]['name'],
  137. 'content_id' => (int)$this->aInputs[$CNF['FIELD_PHOTO']]['content_id'],
  138. 'editor_id' => isset($CNF['FIELD_TEXT_ID']) ? $CNF['FIELD_TEXT_ID'] : '',
  139. 'bx_if:set_thumb' => [
  140. 'condition' => false,
  141. 'content' => []
  142. ]
  143. ];
  144. }
  145. protected function _setAssignments($iContentId, $aMembers)
  146. {
  147. $CNF = &$this->_oModule->_oConfig->CNF;
  148. $oConn = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTION']);
  149. $aMembers2 = $oConn->getConnectedInitiators($iContentId);
  150. $aMembersToAdd = [];
  151. $aMembersToRemove = $aMembers2;
  152. if (is_array($aMembers)){
  153. $aMembersToAdd = array_diff($aMembers, $aMembers2);
  154. $aMembersToRemove = array_diff($aMembers2, $aMembers);
  155. }
  156. $aContentInfo = $this->_oModule->_oDb->getContentInfoById($iContentId);
  157. foreach($aMembersToAdd as $iProfileId){
  158. $oConn->addConnection($iProfileId, $iContentId);
  159. /**
  160. * @hooks
  161. * @hookdef hook-bx_tasks-assigned 'bx_tasks', 'assigned' - hook on task assigned to profile
  162. * - $unit_name - equals `bx_tasks`
  163. * - $action - equals `assigned`
  164. * - $object_id - task id
  165. * - $sender_id - not used
  166. * - $extra_params - array of additional params with the following array keys:
  167. * - `object_author_id` - [int] id for assigned profile
  168. * - `privacy_view` - [string] privacy view value
  169. * @hook @ref hook-bx_tasks-assigned
  170. */
  171. bx_alert($this->MODULE, 'assigned', $iContentId, false, array(
  172. 'object_author_id' => $iProfileId,
  173. 'privacy_view' => $aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]
  174. ));
  175. }
  176. foreach($aMembersToRemove as $iProfileId){
  177. $oConn->removeConnection($iProfileId, $iContentId);
  178. /**
  179. * @hooks
  180. * @hookdef hook-bx_tasks-unassigned 'bx_tasks', 'unassigned' - hook on task unassigned to profile
  181. * - $unit_name - equals `bx_tasks`
  182. * - $action - equals `unassigned`
  183. * - $object_id - task id
  184. * - $sender_id - not used
  185. * - $extra_params - array of additional params with the following array keys:
  186. * - `object_author_id` - [int] id for unassigned profile
  187. * - `privacy_view` - [string] privacy view value
  188. * @hook @ref hook-bx_tasks-unassigned
  189. */
  190. bx_alert($this->MODULE, 'unassigned', $iContentId, false, array(
  191. 'object_author_id' => $iProfileId,
  192. 'privacy_view' => $aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]
  193. ));
  194. }
  195. }
  196. protected function genCustomInputInitialMembers ($aInput)
  197. {
  198. return $this->genCustomInputUsernamesSuggestions(array_merge($aInput , [
  199. 'ajax_get_suggestions' => BX_DOL_URL_ROOT . "modules/?r=" . $this->_oModule->_oConfig->getUri() . "/ajax_get_initial_members/" . $this->_iContextId
  200. ]));
  201. }
  202. protected function genCustomInputTasksList(&$aInput)
  203. {
  204. return $this->genInputStandard($aInput);
  205. }
  206. }
  207. /** @} */