BxTasksFormEntry.php 15 KB

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