BxTasksFormEntry.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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 BxTasksFormEntryChecker extends BxDolFormChecker
  11. {
  12. public function isTrackableField($aInput)
  13. {
  14. return parent::isTrackableField($aInput) || in_array($aInput['name'], ['stickers', 'type']);
  15. }
  16. }
  17. /**
  18. * Create/Edit entry form
  19. */
  20. class BxTasksFormEntry extends BxBaseModTextFormEntry
  21. {
  22. protected $_iContextId;
  23. protected $_sGhostTemplateCover = 'form_ghost_template_cover.html';
  24. protected $_aProperties;
  25. public function __construct($aInfo, $oTemplate = false)
  26. {
  27. $this->MODULE = 'bx_tasks';
  28. $aInfo['params'] ??= [];
  29. $aInfo['params']['checker'] = 'BxTasksFormEntryChecker';
  30. parent::__construct($aInfo, $oTemplate);
  31. $this->_aProperties = $this->_oModule->_oConfig->getProperties();
  32. }
  33. public function setContextId($iContextId)
  34. {
  35. if($this->_iContextId == $iContextId)
  36. return;
  37. $CNF = &$this->_oModule->_oConfig->CNF;
  38. $this->_iContextId = $iContextId;
  39. if(($sKf = 'FIELD_TASKLIST') && isset($CNF[$sKf]) && isset($this->aInputs[$CNF[$sKf]])) {
  40. $aLists = [['id' => 0, 'title' => _t('_bx_tasks_txt_list_inbox')]];
  41. if(($aListsAdd = $this->_oModule->_oDb->getLists($this->_iContextId)) && is_array($aListsAdd))
  42. $aLists = array_merge($aLists, $aListsAdd);
  43. foreach($aLists as $aList)
  44. $this->aInputs[$CNF[$sKf]]['values'][$aList['id']] = $aList['title'];
  45. }
  46. if(($sKf = 'FIELD_TYPE') && isset($CNF[$sKf]) && isset($this->aInputs[$CNF[$sKf]])) {
  47. $aItems = $this->_oModule->_oDb->getPreValues([
  48. 'sample' => 'context_list',
  49. 'context_id' => $this->_iContextId,
  50. 'list' => 'type',
  51. 'active' => 1
  52. ]);
  53. if($aItems && is_array($aItems)) {
  54. $this->aInputs[$CNF[$sKf]]['values'] = [
  55. '' => _t('_sys_please_select')
  56. ];
  57. foreach($aItems as $aItem)
  58. $this->aInputs[$CNF[$sKf]]['values'][$aItem['value']] = $aItem['title'];
  59. }
  60. }
  61. if(($sKf = 'FIELD_STICKERS') && isset($CNF[$sKf]) && isset($this->aInputs[$CNF[$sKf]])) {
  62. $aItems = $this->_oModule->_oDb->getPreValues([
  63. 'sample' => 'context_list',
  64. 'context_id' => $this->_iContextId,
  65. 'list' => 'sticker'
  66. ]);
  67. if($aItems && is_array($aItems)) {
  68. $this->aInputs[$CNF[$sKf]]['values'] = [];
  69. foreach($aItems as $aItem)
  70. $this->aInputs[$CNF[$sKf]]['values'][$aItem['value']] = $aItem['title'];
  71. }
  72. }
  73. }
  74. public function genViewRowValue(&$aInput)
  75. {
  76. $sValue = parent::genViewRowValue($aInput);
  77. if($this->_oModule->isAllowManage($this->_iContentId) && !empty($aInput['name']) && in_array($aInput['name'], $this->_aProperties))
  78. $sValue = $this->_oModule->_oTemplate->parseLink('javascript:void(0)', $sValue ?: _t('_bx_tasks_txt_set'), [
  79. 'onclick' => 'javascript:' . $this->_oModule->_oConfig->getJsObject('tasks') . '.processTaskEdit' . bx_gen_method_name($aInput['name']) . '(' . $this->_iContentId . ', this)'
  80. ]);
  81. return $sValue;
  82. }
  83. protected function genCustomViewRowValueTasksList(&$aInput)
  84. {
  85. if(!isset($aInput['value']))
  86. return null;
  87. return isset($aInput['value']) && isset($aInput['values'][$aInput['value']]) ? $aInput['values'][$aInput['value']] : null;
  88. }
  89. public function getCode($bDynamicMode = false)
  90. {
  91. $this->_replaceMarkersInControls('controls_edit');
  92. $sResult = parent::getCode($bDynamicMode);
  93. $sInclude = $this->_oModule->_oTemplate->addJs(array('tasks.js'), $bDynamicMode);
  94. $sResult .= ($bDynamicMode ? $sInclude : '') . $this->_oModule->_oTemplate->getJsCode('tasks');
  95. return $sResult;
  96. }
  97. public function getCodeAPI()
  98. {
  99. $aResult = parent::getCodeAPI();
  100. $aResult['params'] ??= [];
  101. if(($sK = 'view_mode') && isset($this->aParams[$sK]) && $this->aParams[$sK]) {
  102. $aResult['params']['request_url'] = $this->MODULE . '/set_property/&params[]=' . $this->_iContentId . '&params[]=';
  103. foreach($aResult['inputs'] as $aInput)
  104. if(($sName = $aInput['name'] ?? false) && in_array($sName, $this->_aProperties))
  105. $aResult['inputs'][$sName]['editable'] = true;
  106. }
  107. return $aResult;
  108. }
  109. public function initChecker ($aValues = array (), $aSpecificValues = array())
  110. {
  111. $CNF = &$this->_oModule->_oConfig->CNF;
  112. $bValues = $aValues && !empty($aValues['id']);
  113. $aContentInfo = $bValues ? $this->_oModule->_oDb->getContentInfoById($aValues['id']) : false;
  114. if(!empty($aContentInfo) && is_array($aContentInfo))
  115. $this->setContextId(abs($aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]));
  116. if($this->aParams['display'] == $CNF['OBJECT_FORM_ENTRY_DISPLAY_EDIT'] && isset($CNF['FIELD_PUBLISHED']) && isset($this->aInputs[$CNF['FIELD_PUBLISHED']]))
  117. if($bValues && in_array($aValues[$CNF['FIELD_STATUS']], array('active', 'hidden')))
  118. unset($this->aInputs[$CNF['FIELD_PUBLISHED']]);
  119. if(($sKey = 'FIELD_COVER') && isset($CNF[$sKey], $this->aInputs[$CNF[$sKey]])) {
  120. if($bValues)
  121. $this->aInputs[$CNF['FIELD_COVER']]['content_id'] = $aValues['id'];
  122. $this->aInputs[$CNF['FIELD_COVER']]['ghost_template'] = $this->_oModule->_oTemplate->parseHtmlByName($this->_sGhostTemplateCover, $this->_getCoverGhostTmplVars($aContentInfo));
  123. }
  124. if(($sKey = 'FIELD_INITIAL_MEMBERS') && isset($CNF[$sKey], $this->aInputs[$CNF[$sKey]])) {
  125. if($bValues)
  126. $this->aInputs[$CNF[$sKey]]['value'] = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTION'])->getConnectedInitiators($aValues['id']);
  127. else
  128. $this->aInputs[$CNF[$sKey]]['value'] = [bx_get_logged_profile_id()];
  129. }
  130. parent::initChecker ($aValues, $aSpecificValues);
  131. }
  132. public function insert ($aValsToAdd = array(), $isIgnore = false)
  133. {
  134. $CNF = &$this->_oModule->_oConfig->CNF;
  135. $aValsToAdd[$CNF['FIELD_STATE']] ??= 1;
  136. if(isset($CNF['FIELD_ADDED']) && empty($aValsToAdd[$CNF['FIELD_ADDED']])) {
  137. $iAdded = 0;
  138. if(isset($this->aInputs[$CNF['FIELD_ADDED']]))
  139. $iAdded = $this->getCleanValue($CNF['FIELD_ADDED']);
  140. if(empty($iAdded))
  141. $iAdded = time();
  142. $aValsToAdd[$CNF['FIELD_ADDED']] = $iAdded;
  143. }
  144. if(empty($aValsToAdd[$CNF['FIELD_PUBLISHED']])) {
  145. $iPublished = 0;
  146. if(isset($this->aInputs[$CNF['FIELD_PUBLISHED']]))
  147. $iPublished = $this->getCleanValue($CNF['FIELD_PUBLISHED']);
  148. if(empty($iPublished))
  149. $iPublished = time();
  150. $aValsToAdd[$CNF['FIELD_PUBLISHED']] = $iPublished;
  151. }
  152. $aValsToAdd[$CNF['FIELD_STATUS']] = $aValsToAdd[$CNF['FIELD_PUBLISHED']] > $aValsToAdd[$CNF['FIELD_ADDED']] ? 'awaiting' : 'active';
  153. $iContentId = parent::insert ($aValsToAdd, $isIgnore);
  154. if(!empty($iContentId)) {
  155. $this->processFiles($CNF['FIELD_COVER'], $iContentId, true);
  156. if(isset($this->aInputs['initial_members']))
  157. $this->_setAssignments($iContentId, $this->aInputs['initial_members']['value']);
  158. }
  159. return $iContentId;
  160. }
  161. public function update ($iContentId, $aValsToAdd = array(), &$aTrackTextFieldsChanges = null)
  162. {
  163. $CNF = &$this->_oModule->_oConfig->CNF;
  164. if(empty($aValsToAdd[$CNF['FIELD_PUBLISHED']]) && isset($this->aInputs[$CNF['FIELD_PUBLISHED']])) {
  165. $iPublished = $this->getCleanValue($CNF['FIELD_PUBLISHED']);
  166. if(empty($iPublished))
  167. $iPublished = time();
  168. $aValsToAdd[$CNF['FIELD_PUBLISHED']] = $iPublished;
  169. }
  170. if(isset($this->aInputs['initial_members'])) {
  171. $this->_setAssignments($iContentId, $this->aInputs['initial_members']['value']);
  172. }
  173. $aContentInfo = $this->_oModule->_oDb->getContentInfoById($iContentId);
  174. $aValsToAdd[$CNF['FIELD_ALLOW_VIEW_TO']] = $aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']];
  175. $iResult = parent::update ($iContentId, $aValsToAdd, $aTrackTextFieldsChanges);
  176. $this->processFiles($CNF['FIELD_COVER'], $iContentId, false);
  177. return $iResult;
  178. }
  179. public function delete ($iContentId, $aContentInfo = [])
  180. {
  181. $CNF = &$this->_oModule->_oConfig->CNF;
  182. if(($sKey = 'OBJECT_REPORTS_TIME') && !empty($CNF[$sKey]) && ($o = BxDolReport::getObjectInstance($CNF[$sKey], $iContentId)))
  183. $o->onObjectDelete();
  184. $this->_oModule->_oDb->deleteTimer(['content_id' => $iContentId]);
  185. return parent::delete($iContentId, $aContentInfo);
  186. }
  187. protected function genCustomViewRowValueStickers (&$aInput)
  188. {
  189. if(empty($aInput['value']) || !is_array($aInput['value']))
  190. return null;
  191. $aStickers = $this->_oModule->getStickers($aInput['value'], $this->_iContextId);
  192. if(!$aStickers)
  193. return null;
  194. return $this->_oModule->_oTemplate->getStickers($aStickers);
  195. }
  196. protected function genCustomViewRowValueGhIssueUrl(&$aInput)
  197. {
  198. return $aInput['value'] ? bx_linkify(bx_process_output($aInput['value'])) : null;
  199. }
  200. protected function _getCoverGhostTmplVars($aContentInfo = [])
  201. {
  202. $CNF = &$this->_oModule->_oConfig->CNF;
  203. return array (
  204. 'name' => $this->aInputs[$CNF['FIELD_COVER']]['name'],
  205. 'content_id' => $this->aInputs[$CNF['FIELD_COVER']]['content_id'],
  206. 'editor_id' => isset($CNF['FIELD_TEXT_ID']) ? $CNF['FIELD_TEXT_ID'] : '',
  207. 'thumb_id' => isset($CNF['FIELD_THUMB']) && isset($aContentInfo[$CNF['FIELD_THUMB']]) ? $aContentInfo[$CNF['FIELD_THUMB']] : 0,
  208. 'name_thumb' => isset($CNF['FIELD_THUMB']) ? $CNF['FIELD_THUMB'] : ''
  209. );
  210. }
  211. protected function _getPhotoGhostTmplVars($aContentInfo = array())
  212. {
  213. $CNF = &$this->_oModule->_oConfig->CNF;
  214. return [
  215. 'name' => $this->aInputs[$CNF['FIELD_PHOTO']]['name'],
  216. 'content_id' => (int)$this->aInputs[$CNF['FIELD_PHOTO']]['content_id'],
  217. 'editor_id' => isset($CNF['FIELD_TEXT_ID']) ? $CNF['FIELD_TEXT_ID'] : '',
  218. 'bx_if:set_thumb' => [
  219. 'condition' => false,
  220. 'content' => []
  221. ]
  222. ];
  223. }
  224. protected function _setAssignments($iContentId, $aMembers)
  225. {
  226. $CNF = &$this->_oModule->_oConfig->CNF;
  227. $oConn = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTION']);
  228. if($this->_bIsApi && is_string($aMembers))
  229. $aMembers = explode(',', $aMembers);
  230. $aMembersCurrent = $oConn->getConnectedInitiators($iContentId);
  231. $aMembersToAdd = [];
  232. $aMembersToRemove = $aMembersCurrent;
  233. if (is_array($aMembers)){
  234. $aMembersToAdd = array_diff($aMembers, $aMembersCurrent);
  235. $aMembersToRemove = array_diff($aMembersCurrent, $aMembers);
  236. }
  237. $aContentInfo = $this->_oModule->_oDb->getContentInfoById($iContentId);
  238. foreach($aMembersToAdd as $iProfileId){
  239. $oConn->addConnection($iProfileId, $iContentId);
  240. /**
  241. * @hooks
  242. * @hookdef hook-bx_tasks-assigned 'bx_tasks', 'assigned' - hook on task assigned to profile
  243. * - $unit_name - equals `bx_tasks`
  244. * - $action - equals `assigned`
  245. * - $object_id - task id
  246. * - $sender_id - not used
  247. * - $extra_params - array of additional params with the following array keys:
  248. * - `object_author_id` - [int] id for assigned profile
  249. * - `privacy_view` - [string] privacy view value
  250. * @hook @ref hook-bx_tasks-assigned
  251. */
  252. bx_alert($this->MODULE, 'assigned', $iContentId, false, array(
  253. 'object_author_id' => $iProfileId,
  254. 'privacy_view' => $aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]
  255. ));
  256. }
  257. foreach($aMembersToRemove as $iProfileId){
  258. $oConn->removeConnection($iProfileId, $iContentId);
  259. /**
  260. * @hooks
  261. * @hookdef hook-bx_tasks-unassigned 'bx_tasks', 'unassigned' - hook on task unassigned to profile
  262. * - $unit_name - equals `bx_tasks`
  263. * - $action - equals `unassigned`
  264. * - $object_id - task id
  265. * - $sender_id - not used
  266. * - $extra_params - array of additional params with the following array keys:
  267. * - `object_author_id` - [int] id for unassigned profile
  268. * - `privacy_view` - [string] privacy view value
  269. * @hook @ref hook-bx_tasks-unassigned
  270. */
  271. bx_alert($this->MODULE, 'unassigned', $iContentId, false, array(
  272. 'object_author_id' => $iProfileId,
  273. 'privacy_view' => $aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]
  274. ));
  275. }
  276. }
  277. protected function genCustomInputInitialMembers ($aInput)
  278. {
  279. if($this->_bIsApi) {
  280. $aInput['ajax_get_suggestions'] = $this->MODULE . '/get_initial_members&params[]=' . $this->_iContextId . '&params[]=';
  281. $aInput['value_data'] = [];
  282. foreach($aInput['value'] as $iProfileId)
  283. $aInput['value_data'][] = BxDolProfile::getData($iProfileId);
  284. }
  285. else
  286. $aInput['ajax_get_suggestions'] = BX_DOL_URL_ROOT . "modules/?r=" . $this->_oModule->_oConfig->getUri() . "/ajax_get_initial_members/" . $this->_iContextId;
  287. return $this->genCustomInputUsernamesSuggestions($aInput);
  288. }
  289. }
  290. /** @} */