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. foreach($aResult['inputs'] as $aInput)
  118. if(($sName = $aInput['name'] ?? false) && in_array($sName, $this->_aProperties))
  119. $aResult['inputs'][$sName]['editable'] = true;
  120. }
  121. return $aResult;
  122. }
  123. public function initChecker ($aValues = array (), $aSpecificValues = array())
  124. {
  125. $CNF = &$this->_oModule->_oConfig->CNF;
  126. $bValues = $aValues && !empty($aValues['id']);
  127. $aContentInfo = $bValues ? $this->_oModule->_oDb->getContentInfoById($aValues['id']) : false;
  128. if(!empty($aContentInfo) && is_array($aContentInfo))
  129. $this->setContextId(abs($aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]));
  130. if($this->aParams['display'] == $CNF['OBJECT_FORM_ENTRY_DISPLAY_EDIT'] && isset($CNF['FIELD_PUBLISHED']) && isset($this->aInputs[$CNF['FIELD_PUBLISHED']]))
  131. if($bValues && in_array($aValues[$CNF['FIELD_STATUS']], array('active', 'hidden')))
  132. unset($this->aInputs[$CNF['FIELD_PUBLISHED']]);
  133. if(($sKey = 'FIELD_COVER') && isset($CNF[$sKey], $this->aInputs[$CNF[$sKey]])) {
  134. if($bValues)
  135. $this->aInputs[$CNF['FIELD_COVER']]['content_id'] = $aValues['id'];
  136. $this->aInputs[$CNF['FIELD_COVER']]['ghost_template'] = $this->_oModule->_oTemplate->parseHtmlByName($this->_sGhostTemplateCover, $this->_getCoverGhostTmplVars($aContentInfo));
  137. }
  138. if(($sKey = 'FIELD_INITIAL_MEMBERS') && isset($CNF[$sKey], $this->aInputs[$CNF[$sKey]])) {
  139. if($bValues)
  140. $this->aInputs[$CNF[$sKey]]['value'] = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTION'])->getConnectedInitiators($aValues['id']);
  141. else
  142. $this->aInputs[$CNF[$sKey]]['value'] = [bx_get_logged_profile_id()];
  143. }
  144. parent::initChecker ($aValues, $aSpecificValues);
  145. }
  146. public function insert ($aValsToAdd = array(), $isIgnore = false)
  147. {
  148. $CNF = &$this->_oModule->_oConfig->CNF;
  149. $aValsToAdd[$CNF['FIELD_STATE']] ??= 1;
  150. if(isset($CNF['FIELD_ADDED']) && empty($aValsToAdd[$CNF['FIELD_ADDED']])) {
  151. $iAdded = 0;
  152. if(isset($this->aInputs[$CNF['FIELD_ADDED']]))
  153. $iAdded = $this->getCleanValue($CNF['FIELD_ADDED']);
  154. if(empty($iAdded))
  155. $iAdded = time();
  156. $aValsToAdd[$CNF['FIELD_ADDED']] = $iAdded;
  157. }
  158. if(empty($aValsToAdd[$CNF['FIELD_PUBLISHED']])) {
  159. $iPublished = 0;
  160. if(isset($this->aInputs[$CNF['FIELD_PUBLISHED']]))
  161. $iPublished = $this->getCleanValue($CNF['FIELD_PUBLISHED']);
  162. if(empty($iPublished))
  163. $iPublished = time();
  164. $aValsToAdd[$CNF['FIELD_PUBLISHED']] = $iPublished;
  165. }
  166. $aValsToAdd[$CNF['FIELD_STATUS']] = $aValsToAdd[$CNF['FIELD_PUBLISHED']] > $aValsToAdd[$CNF['FIELD_ADDED']] ? 'awaiting' : 'active';
  167. $iContentId = parent::insert ($aValsToAdd, $isIgnore);
  168. if(!empty($iContentId)) {
  169. $this->processFiles($CNF['FIELD_COVER'], $iContentId, true);
  170. if(isset($this->aInputs['initial_members']))
  171. $this->_setAssignments($iContentId, $this->aInputs['initial_members']['value']);
  172. }
  173. return $iContentId;
  174. }
  175. public function update ($iContentId, $aValsToAdd = array(), &$aTrackTextFieldsChanges = null)
  176. {
  177. $CNF = &$this->_oModule->_oConfig->CNF;
  178. if(empty($aValsToAdd[$CNF['FIELD_PUBLISHED']]) && isset($this->aInputs[$CNF['FIELD_PUBLISHED']])) {
  179. $iPublished = $this->getCleanValue($CNF['FIELD_PUBLISHED']);
  180. if(empty($iPublished))
  181. $iPublished = time();
  182. $aValsToAdd[$CNF['FIELD_PUBLISHED']] = $iPublished;
  183. }
  184. if(isset($this->aInputs['initial_members'])) {
  185. $this->_setAssignments($iContentId, $this->aInputs['initial_members']['value']);
  186. }
  187. $aContentInfo = $this->_oModule->_oDb->getContentInfoById($iContentId);
  188. $aValsToAdd[$CNF['FIELD_ALLOW_VIEW_TO']] = $aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']];
  189. $iResult = parent::update ($iContentId, $aValsToAdd, $aTrackTextFieldsChanges);
  190. $this->processFiles($CNF['FIELD_COVER'], $iContentId, false);
  191. return $iResult;
  192. }
  193. public function delete ($iContentId, $aContentInfo = [])
  194. {
  195. $CNF = &$this->_oModule->_oConfig->CNF;
  196. if(($sKey = 'OBJECT_REPORTS_TIME') && !empty($CNF[$sKey]) && ($o = BxDolReport::getObjectInstance($CNF[$sKey], $iContentId)))
  197. $o->onObjectDelete();
  198. $this->_oModule->_oDb->deleteTimer(['content_id' => $iContentId]);
  199. return parent::delete($iContentId, $aContentInfo);
  200. }
  201. protected function genCustomViewRowValueStickers (&$aInput)
  202. {
  203. if(empty($aInput['value']) || !is_array($aInput['value']))
  204. return null;
  205. $aStickers = $this->_oModule->getStickers($aInput['value'], $this->_iContextId);
  206. if(!$aStickers)
  207. return null;
  208. return $this->_oModule->_oTemplate->getStickers($aStickers);
  209. }
  210. protected function genCustomViewRowValueGhIssueUrl(&$aInput)
  211. {
  212. return $aInput['value'] ? bx_linkify(bx_process_output($aInput['value'])) : null;
  213. }
  214. protected function _getCoverGhostTmplVars($aContentInfo = [])
  215. {
  216. $CNF = &$this->_oModule->_oConfig->CNF;
  217. return array (
  218. 'name' => $this->aInputs[$CNF['FIELD_COVER']]['name'],
  219. 'content_id' => $this->aInputs[$CNF['FIELD_COVER']]['content_id'],
  220. 'editor_id' => isset($CNF['FIELD_TEXT_ID']) ? $CNF['FIELD_TEXT_ID'] : '',
  221. 'thumb_id' => isset($CNF['FIELD_THUMB']) && isset($aContentInfo[$CNF['FIELD_THUMB']]) ? $aContentInfo[$CNF['FIELD_THUMB']] : 0,
  222. 'name_thumb' => isset($CNF['FIELD_THUMB']) ? $CNF['FIELD_THUMB'] : ''
  223. );
  224. }
  225. protected function _getPhotoGhostTmplVars($aContentInfo = array())
  226. {
  227. $CNF = &$this->_oModule->_oConfig->CNF;
  228. return [
  229. 'name' => $this->aInputs[$CNF['FIELD_PHOTO']]['name'],
  230. 'content_id' => (int)$this->aInputs[$CNF['FIELD_PHOTO']]['content_id'],
  231. 'editor_id' => isset($CNF['FIELD_TEXT_ID']) ? $CNF['FIELD_TEXT_ID'] : '',
  232. 'bx_if:set_thumb' => [
  233. 'condition' => false,
  234. 'content' => []
  235. ]
  236. ];
  237. }
  238. protected function _setAssignments($iContentId, $aMembers)
  239. {
  240. $CNF = &$this->_oModule->_oConfig->CNF;
  241. $oConn = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTION']);
  242. if($this->_bIsApi && is_string($aMembers))
  243. $aMembers = explode(',', $aMembers);
  244. $aMembersCurrent = $oConn->getConnectedInitiators($iContentId);
  245. $aMembersToAdd = [];
  246. $aMembersToRemove = $aMembersCurrent;
  247. if (is_array($aMembers)){
  248. $aMembersToAdd = array_diff($aMembers, $aMembersCurrent);
  249. $aMembersToRemove = array_diff($aMembersCurrent, $aMembers);
  250. }
  251. $aContentInfo = $this->_oModule->_oDb->getContentInfoById($iContentId);
  252. foreach($aMembersToAdd as $iProfileId){
  253. $oConn->addConnection($iProfileId, $iContentId);
  254. /**
  255. * @hooks
  256. * @hookdef hook-bx_tasks-assigned 'bx_tasks', 'assigned' - hook on task assigned to profile
  257. * - $unit_name - equals `bx_tasks`
  258. * - $action - equals `assigned`
  259. * - $object_id - task id
  260. * - $sender_id - not used
  261. * - $extra_params - array of additional params with the following array keys:
  262. * - `object_author_id` - [int] id for assigned profile
  263. * - `privacy_view` - [string] privacy view value
  264. * @hook @ref hook-bx_tasks-assigned
  265. */
  266. bx_alert($this->MODULE, 'assigned', $iContentId, false, array(
  267. 'object_author_id' => $iProfileId,
  268. 'privacy_view' => $aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]
  269. ));
  270. }
  271. foreach($aMembersToRemove as $iProfileId){
  272. $oConn->removeConnection($iProfileId, $iContentId);
  273. /**
  274. * @hooks
  275. * @hookdef hook-bx_tasks-unassigned 'bx_tasks', 'unassigned' - hook on task unassigned to profile
  276. * - $unit_name - equals `bx_tasks`
  277. * - $action - equals `unassigned`
  278. * - $object_id - task id
  279. * - $sender_id - not used
  280. * - $extra_params - array of additional params with the following array keys:
  281. * - `object_author_id` - [int] id for unassigned profile
  282. * - `privacy_view` - [string] privacy view value
  283. * @hook @ref hook-bx_tasks-unassigned
  284. */
  285. bx_alert($this->MODULE, 'unassigned', $iContentId, false, array(
  286. 'object_author_id' => $iProfileId,
  287. 'privacy_view' => $aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]
  288. ));
  289. }
  290. }
  291. protected function genCustomInputInitialMembers ($aInput)
  292. {
  293. if($this->_bIsApi) {
  294. $aInput['ajax_get_suggestions'] = $this->MODULE . '/get_initial_members&params[]=' . $this->_iContextId . '&params[]=';
  295. $aInput['value_data'] = [];
  296. foreach($aInput['value'] as $iProfileId)
  297. $aInput['value_data'][] = BxDolProfile::getData($iProfileId);
  298. }
  299. else
  300. $aInput['ajax_get_suggestions'] = BX_DOL_URL_ROOT . "modules/?r=" . $this->_oModule->_oConfig->getUri() . "/ajax_get_initial_members/" . $this->_iContextId;
  301. return $this->genCustomInputUsernamesSuggestions($aInput);
  302. }
  303. }
  304. /** @} */