BxTasksModule.php 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333
  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. * Task states
  12. */
  13. define('BX_TASKS_STATE_BACBLOG', 1);
  14. define('BX_TASKS_STATE_TODO', 2);
  15. define('BX_TASKS_STATE_IN_PROGRESS', 3);
  16. define('BX_TASKS_STATE_IN_REVIEW', 4);
  17. define('BX_TASKS_STATE_CANCELLED', 5);
  18. define('BX_TASKS_STATE_DUPLICATE', 6);
  19. define('BX_TASKS_STATE_DONE', 7);
  20. class BxTasksModule extends BxBaseModTextModule implements iBxDolCalendarService
  21. {
  22. function __construct(&$aModule)
  23. {
  24. parent::__construct($aModule);
  25. $CNF = &$this->_oConfig->CNF;
  26. $this->_aSearchableNamesExcept = array_merge($this->_aSearchableNamesExcept, array(
  27. $CNF['FIELD_PUBLISHED'],
  28. $CNF['FIELD_ALLOW_COMMENTS']
  29. ));
  30. }
  31. /**
  32. * Action methods
  33. */
  34. /**
  35. * Get possible recipients for start conversation form
  36. */
  37. public function actionAjaxGetInitialMembers($iContextPid)
  38. {
  39. $sTerm = bx_get('term');
  40. $sModule = $this->_oConfig->getName();
  41. $a = bx_srv('system', 'profiles_search', [$sTerm, [
  42. 'module' => $sModule,
  43. 'search_params' => ['name' => $sModule . '_initial_members', 'context_pid' => $iContextPid]
  44. ]], 'TemplServiceProfiles');
  45. header('Content-Type:text/javascript; charset=utf-8');
  46. echo(json_encode($a));
  47. }
  48. public function actionSetCompleted($iContentId, $iCompleted)
  49. {
  50. if(!$this->isAllowManage($iContentId) || !$this->complete($iContentId, $iCompleted))
  51. return echoJson(['code' => 1]);
  52. return echoJson(['code' => 0, 'reload' => 1]);
  53. }
  54. public function actionApplyFilter($iContextId, $iFilterId)
  55. {
  56. $CNF = &$this->_oConfig->CNF;
  57. $sCookieKey = $CNF['COOKIE_SETTING_KEY'];
  58. $aFilters = [];
  59. if(isset($_COOKIE[$sCookieKey]))
  60. $aFilters = json_decode($_COOKIE[$sCookieKey], true);
  61. if(($iFilterId = (int)$iFilterId) != 0)
  62. $aFilters[$iContextId] = $iFilterId;
  63. else
  64. unset($aFilters[$iContextId]);
  65. bx_setcookie($sCookieKey, json_encode($aFilters), time() + 60 * 60 * 24 * 365);
  66. return echoJson([
  67. 'content' => $this->_oTemplate->getEntries($iContextId, ['filter' => $iFilterId]),
  68. 'eval' => $this->_oConfig->getJsObject('tasks') . '.onApplyFilter(oData)'
  69. ]);
  70. }
  71. public function actionProcessContextForm($iContextId)
  72. {
  73. if(!$this->isAllowManageByContext($iContextId))
  74. return;
  75. $CNF = &$this->_oConfig->CNF;
  76. $aContext = $this->_oDb->getContexts(['sample' => 'id', 'id' => $iContextId]);
  77. $bContext = !empty($aContext) && is_array($aContext);
  78. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_CONTEXT'], $CNF['OBJECT_FORM_CONTEXT_DISPLAY_EDIT']);
  79. $oForm->setAction(BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'process_context_form/' . $iContextId . '/');
  80. $oForm->initChecker($aContext);
  81. if($oForm->isSubmittedAndValid()) {
  82. if(!$bContext)
  83. $oForm->insert(['id' => $iContextId]);
  84. else
  85. $oForm->update($iContextId);
  86. echoJson([
  87. 'eval' => $this->_oConfig->getJsObject('tasks') . '.hidePopup(oData, ' . $iContextId . ')',
  88. ]);
  89. }
  90. else {
  91. $sContent = $this->_oTemplate->parseHtmlByName('popup_form.html', [
  92. 'form_id' => $oForm->getId(),
  93. 'form' => $oForm->getCode(true)
  94. ]);
  95. if($oForm->isSubmitted())
  96. return echoJson([
  97. 'form' => $sContent,
  98. 'form_id' => $oForm->getId()
  99. ]);
  100. echo $sContent;
  101. }
  102. }
  103. public function actionProcessTaskListForm($iContextId, $iId)
  104. {
  105. if (!$this->isAllowAdd(abs($iContextId)))
  106. return;
  107. $CNF = &$this->_oConfig->CNF;
  108. $oForm = null;
  109. $sPopupTitle = "";
  110. $aContentInfo = array();
  111. if ($iId == 0){
  112. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_LIST_ENTRY'], $CNF['OBJECT_FORM_LIST_ENTRY_DISPLAY_ADD']);
  113. $sPopupTitle = _t('_bx_tasks_form_list_entry_display_add');
  114. }
  115. else {
  116. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_LIST_ENTRY'], $CNF['OBJECT_FORM_LIST_ENTRY_DISPLAY_EDIT']);
  117. $aContentInfo = $this->_oDb->getList($iId);
  118. $sPopupTitle = _t('_bx_tasks_form_list_entry_display_edit');
  119. }
  120. $oForm->aFormAttrs['action'] = BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'process_task_list_form/' . $iContextId . '/' . $iId . '/';
  121. if (!$oForm)
  122. return '';
  123. $oForm->initChecker($aContentInfo, array());
  124. if($oForm->isSubmittedAndValid()) {
  125. if ($iId == 0){
  126. $aValsToAdd['context_id'] = $iContextId;
  127. $iId = $oForm->insert($aValsToAdd);
  128. }
  129. else {
  130. $iId = $oForm->update($iId);
  131. }
  132. return echoJson(array(
  133. 'eval' => $this->_oConfig->getJsObject('tasks') . '.reloadData(oData, ' . $iContextId . ')',
  134. ));
  135. }
  136. else {
  137. $sContent = $this->_oTemplate->parseHtmlByName('popup_form.html', array(
  138. 'form_id' => $oForm->getId(),
  139. 'form' => $oForm->getCode(true)
  140. ));
  141. if (!$oForm->isSubmitted()) {
  142. echo $sContent;
  143. return;
  144. }
  145. return echoJson(array('form' => $sContent, 'form_id' => $oForm->getId()));
  146. }
  147. }
  148. public function actionDeleteTaskList($iId, $iContextId)
  149. {
  150. if (!$this->isAllowManageByContext($iContextId))
  151. return;
  152. $CNF = &$this->_oConfig->CNF;
  153. $aTasks = $this->_oDb->getTasks($iContextId, $iId);
  154. $this->_oDb->deleteList($iId);
  155. if (!empty($aTasks) && ($oConn = BxDolConnection::getObjectInstance($this->_oConfig->CNF['OBJECT_CONNECTION']))) {
  156. foreach ($aTasks as &$aTask)
  157. $oConn->onDeleteContent($aTask[$CNF['FIELD_ID']]);
  158. }
  159. echoJson(array(
  160. 'context_id' => $iContextId,
  161. ));
  162. }
  163. public function actionProcessTaskForm($iContextId, $iListId)
  164. {
  165. if(!$this->isAllowAdd($iContextId))
  166. return echoJson([]);
  167. $CNF = &$this->_oConfig->CNF;
  168. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_ENTRY'], $CNF['OBJECT_FORM_ENTRY_DISPLAY_ADD']);
  169. if(!$oForm)
  170. return echoJson([]);
  171. $oForm->setAction(BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'process_task_form/' . $iContextId . '/' . $iListId . '/');
  172. $oForm->setContextId($iContextId);
  173. $oForm->initChecker();
  174. if($oForm->isSubmittedAndValid()) {
  175. $iContentId = $oForm->insert([$CNF['FIELD_ALLOW_VIEW_TO'] => -$iContextId, $CNF['FIELD_TASKLIST'] => $iListId]);
  176. if($iContentId)
  177. $this->onPublished($iContentId);
  178. return echoJson([
  179. 'eval' => $this->_oConfig->getJsObject('tasks') . '.reloadData(oData, ' . $iContextId . ')',
  180. ]);
  181. }
  182. else {
  183. $sContent = $this->_oTemplate->parseHtmlByName('popup_form.html', [
  184. 'form_id' => $oForm->getId(),
  185. 'form' => $oForm->getCode(true)
  186. ]);
  187. if($oForm->isSubmitted())
  188. return echoJson(['form' => $sContent, 'form_id' => $oForm->getId()]);
  189. echo $sContent;
  190. }
  191. }
  192. public function actionProcessTaskFormEditProperty($iContentId, $sProperty)
  193. {
  194. $CNF = &$this->_oConfig->CNF;
  195. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  196. if(!$this->isAllowAdd(abs($aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']])))
  197. return '';
  198. $sForm = '';
  199. if(($sKey = 'OBJECT_FORM_ENTRY_DISPLAY_EDIT_' . strtoupper($sProperty)) && !empty($CNF[$sKey]))
  200. $sForm = $CNF[$sKey];
  201. else
  202. return '';
  203. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_ENTRY'], $sForm);
  204. $oForm->setId($sForm);
  205. $oForm->setName($sForm);
  206. $oForm->setAction(BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'process_task_form_edit_property/' . $iContentId . '/' . $sProperty . '/');
  207. if(!$oForm)
  208. return '';
  209. $oForm->initChecker($aContentInfo);
  210. if($oForm->isSubmittedAndValid()) {
  211. if(!$oForm->update($iContentId))
  212. return echoJson(['msg' => _t('_bx_tasks_txt_err_cannot_perform_action')]);
  213. if(($sMethod = '_onEdit' . bx_gen_method_name($sProperty)) && method_exists($this, $sMethod))
  214. $this->$sMethod($aContentInfo, $oForm);
  215. else
  216. $this->_onEditProperty($aContentInfo, $sProperty, $oForm);
  217. return echoJson([
  218. 'eval' => $this->_oConfig->getJsObject('tasks') . '.reload(oData)',
  219. ]);
  220. }
  221. else {
  222. $sContent = $this->_oTemplate->parseHtmlByName('popup_form.html', [
  223. 'form_id' => $oForm->getId(),
  224. 'form' => $oForm->getCode(true)
  225. ]);
  226. if (!$oForm->isSubmitted()) {
  227. echo $sContent;
  228. return;
  229. }
  230. return echoJson(['form' => $sContent, 'form_id' => $oForm->getId()]);
  231. }
  232. }
  233. public function actionCalendarData()
  234. {
  235. // check permissions
  236. $aSQLPart = array();
  237. $iContextId = (int)bx_get('context_id');
  238. if(!$this->isAllowView($iContextId))
  239. return;
  240. $oPrivacy = BxDolPrivacy::getObjectInstance($this->_oConfig->CNF['OBJECT_PRIVACY_VIEW']);
  241. if($iContextId) {
  242. $aSQLPart = $oPrivacy ? $oPrivacy->getContentByGroupAsSQLPart(- $iContextId) : array();
  243. }
  244. // get entries
  245. $aEntries = $this->_oDb->getEntriesByDate(bx_get('start'), bx_get('end'), bx_get('event'), $aSQLPart);
  246. header('Content-Type: application/json; charset=utf-8');
  247. echo json_encode($aEntries);
  248. }
  249. public function actionProcessTimer($sAction, $iContentId, $iProfileId)
  250. {
  251. if(!$this->_iProfileId || $this->_iProfileId != $iProfileId)
  252. return echoJson(['msg' => _t('_sys_txt_access_denied')]);
  253. $aResult = $this->serviceProcessTimer($sAction, $iContentId, $iProfileId);
  254. if($aResult['code'] == 0)
  255. $aResult = array_merge($aResult, [
  256. 'content_id' => $iContentId,
  257. 'profile_id' => $iProfileId,
  258. 'content' => $this->_oTemplate->getTimer($iContentId, $iProfileId),
  259. 'eval' => $this->_oConfig->getJsObject('timer') . '.onPerformAction' . bx_gen_method_name($sAction) . '(oData)'
  260. ]);
  261. return echoJson($aResult);
  262. }
  263. public function serviceGetSafeServices()
  264. {
  265. return array_merge(parent::serviceGetSafeServices(), [
  266. 'ProcessTimer' => '',
  267. ]);
  268. }
  269. public function serviceProcessTimer($sAction, $iContentId, $iProfileId)
  270. {
  271. $CNF = &$this->_oConfig->CNF;
  272. $aResult = ['code' => 1, 'msg' => _t('_sys_txt_error_occured')];
  273. $iNow = time();
  274. switch($sAction) {
  275. case 'start':
  276. $this->stopTimerByAuthor($iProfileId);
  277. $iTimer = false;
  278. $aTimer = $this->getTimer($iContentId, $iProfileId);
  279. if($aTimer && is_array($aTimer)) {
  280. if($this->updateTimerById($aTimer['id'], ['started' => $iNow]))
  281. $iTimer = $aTimer['id'];
  282. }
  283. else
  284. $iTimer = $this->_oDb->insertTimer(['content_id' => $iContentId, 'profile_id' => $iProfileId, 'started' => $iNow]);
  285. if($iTimer !== false)
  286. $aResult = ['code' => 0, 'id' => $iTimer];
  287. break;
  288. case 'stop':
  289. if($this->stopTimerByAuthor($iProfileId))
  290. $aResult = ['code' => 0];
  291. break;
  292. case 'resume':
  293. $this->stopTimerByAuthor($iProfileId);
  294. $aTimer = $this->getTimer($iContentId, $iProfileId);
  295. if($aTimer && is_array($aTimer) && $this->updateTimerById($aTimer['id'], ['started' => $iNow]))
  296. $aResult = ['code' => 0];
  297. break;
  298. case 'reload':
  299. $aTimer = $this->getTimer($iContentId, $iProfileId);
  300. $aResult = [
  301. 'code' => 0,
  302. 'started' => $aTimer && ($aTimer['started'] ?? 0) != 0
  303. ];
  304. break;
  305. case 'log':
  306. $aTimer = $this->getTimer($iContentId, $iProfileId);
  307. if(!$aTimer || !is_array($aTimer) || !$aTimer['duration'])
  308. break;
  309. list($iH, $iM) = $this->_oConfig->timeI2A($aTimer['duration'], true);
  310. $iNow = time();
  311. $iTrackId = $this->_oDb->insertTimeTrack([
  312. 'object_id' => $iContentId,
  313. 'author_id' => $iProfileId,
  314. 'author_nip' => $iProfileId == $this->_iProfileId ? bx_get_ip_hash(getVisitorIP()) : 0,
  315. 'value' => $this->_oConfig->timeA2I([(int)$iH, (int)$iM + 1]),
  316. 'value_date' => $iNow,
  317. 'date' => $iNow
  318. ]);
  319. if(!$iTrackId)
  320. break;
  321. $oTime = BxDolReport::getObjectInstance($CNF['OBJECT_REPORTS_TIME'], $iContentId);
  322. if(!$oTime || !$oTime->isEnabled())
  323. break;
  324. if(!$oTime->putReport($iContentId, $iProfileId, $iTrackId))
  325. break;
  326. $this->_oDb->deleteTimer([
  327. 'content_id' => $iContentId,
  328. 'profile_id' => $iProfileId
  329. ]);
  330. $aResult = ['code' => 0];
  331. break;
  332. case 'clear':
  333. if($this->_oDb->deleteTimer(['content_id' => $iContentId, 'profile_id' => $iProfileId]) !== false)
  334. $aResult = ['code' => 0];
  335. break;
  336. }
  337. return $aResult;
  338. }
  339. public function serviceManageTools($sType = 'common')
  340. {
  341. $CNF = &$this->_oConfig->CNF;
  342. $mixedResults = '';
  343. if(!($mixedResults = parent::serviceManageTools($sType)))
  344. return $mixedResults;
  345. return array_merge(parent::serviceManageTools($sType), [
  346. 'menu' => BxDolMenu::getObjectInstance($CNF['OBJECT_MENU_MANAGE_TOOLS_SUBMENU'])
  347. ]);
  348. }
  349. public function serviceGetBlockMenuBrowse()
  350. {
  351. return $this->_oTemplate->getBlockMenuBrowse();
  352. }
  353. public function serviceGetBlockMenuContext($iProfileId)
  354. {
  355. $CNF = &$this->_oConfig->CNF;
  356. if(!$this->isAllowView($iProfileId))
  357. return '';
  358. $oMenu = BxDolMenu::getObjectInstance($CNF['OBJECT_MENU_SUBMENU_VIEW_CONTEXT']);
  359. if(!$oMenu)
  360. return '';
  361. $oMenu->addMarkers(['profile_id' => $iProfileId]);
  362. return $oMenu->getCode();
  363. }
  364. public function serviceGetBlockPreValues($iContextPid = 0)
  365. {
  366. $CNF = &$this->_oConfig->CNF;
  367. if(!$this->isAllowManageByContext($iContextPid))
  368. return $this->_bIsApi ? [] : '';
  369. $bContextPid = !empty($iContextPid);
  370. $sGrid = $CNF['OBJECT_GRID_PRE_VALUES'];
  371. $oGrid = BxDolGrid::getObjectInstance($sGrid);
  372. if(!$oGrid)
  373. return $this->_bIsApi ? [] : '';
  374. if($bContextPid)
  375. $oGrid->setContextPid($iContextPid);
  376. if($this->_bIsApi)
  377. return [
  378. bx_api_get_block('grid', $oGrid->getCodeAPI())
  379. ];
  380. $this->_oTemplate->addCss(['manage_tools.css']);
  381. $this->_oTemplate->addJs(['modules/base/text/js/|manage_tools.js', 'pre_values.js']);
  382. $this->_oTemplate->addJsTranslation(['_sys_grid_search']);
  383. $aResult = [
  384. 'content' => $this->_oTemplate->getJsCode('pre_values', [
  385. 'sObjNameGrid' => $sGrid,
  386. 'sPageUrl' => BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink($CNF['URL_CONTEXT_VALUES'], ['profile_id' => $iContextPid])
  387. ]) . $oGrid->getCode()
  388. ];
  389. return $aResult;
  390. }
  391. public function serviceGetBlockManageTime($sType = 'common', $iContextPid = 0)
  392. {
  393. $CNF = &$this->_oConfig->CNF;
  394. $bContextPid = !empty($iContextPid);
  395. $sGrid = $CNF['OBJECT_GRID_TIME_' . ($bContextPid ? 'CONTEXT_' : '') . strtoupper($sType)];
  396. $oGrid = BxDolGrid::getObjectInstance($sGrid);
  397. if(!$oGrid)
  398. return $this->_bIsApi ? [] : '';
  399. if($bContextPid)
  400. $oGrid->setContextPid($iContextPid);
  401. if($this->_bIsApi)
  402. return [
  403. bx_api_get_block('grid', $oGrid->getCodeAPI())
  404. ];
  405. list($aCssCalendar, $aJsCalendar) = BxBaseFormView::getCssJsCalendar();
  406. $this->_oTemplate->addCss(array_merge($aCssCalendar, ['manage_tools.css', 'time.css']));
  407. $this->_oTemplate->addJs(array_merge($aJsCalendar, ['modules/base/text/js/|manage_tools.js', 'time.js']));
  408. $this->_oTemplate->addJsTranslation(['_sys_grid_search']);
  409. $aResult = [
  410. 'content' => $this->_oTemplate->getJsCode('time', [
  411. 'sObjNameGrid' => $sGrid
  412. ]) . $oGrid->getCode()
  413. ];
  414. if(!$bContextPid)
  415. $aResult['menu'] = BxDolMenu::getObjectInstance($CNF['OBJECT_MENU_MANAGE_TOOLS_SUBMENU']);
  416. return $aResult;
  417. }
  418. public function serviceGetBlockTimers()
  419. {
  420. $CNF = &$this->_oConfig->CNF;
  421. return [
  422. 'content' => $this->_oTemplate->getBlockTimers($this->_iProfileId),
  423. 'menu' => BxDolMenu::getObjectInstance($CNF['OBJECT_MENU_MANAGE_TOOLS_SUBMENU'])
  424. ];
  425. }
  426. /**
  427. * Data for Timeline module
  428. */
  429. public function serviceGetTimelineData()
  430. {
  431. $sModule = $this->_aModule['name'];
  432. return array(
  433. 'handlers' => array(
  434. array('group' => $sModule . '_object', 'type' => 'insert', 'alert_unit' => $sModule, 'alert_action' => 'added', 'module_name' => $sModule, 'module_method' => 'get_timeline_post', 'module_class' => 'Module', 'groupable' => 0, 'group_by' => ''),
  435. array('group' => $sModule . '_completed', 'type' => 'insert', 'alert_unit' => $sModule, 'alert_action' => 'completed', 'module_name' => $sModule, 'module_method' => 'get_timeline_completed', 'module_class' => 'Module', 'groupable' => 0, 'group_by' => ''),
  436. array('group' => $sModule . '_reopened', 'type' => 'insert', 'alert_unit' => $sModule, 'alert_action' => 'reopened', 'module_name' => $sModule, 'module_method' => 'get_timeline_reopened', 'module_class' => 'Module', 'groupable' => 0, 'group_by' => ''),
  437. array('group' => $sModule . '_object', 'type' => 'update', 'alert_unit' => $sModule, 'alert_action' => 'edited'),
  438. array('group' => $sModule . '_object', 'type' => 'delete', 'alert_unit' => $sModule, 'alert_action' => 'deleted'),
  439. ),
  440. 'alerts' => array(
  441. array('unit' => $sModule, 'action' => 'added'),
  442. array('unit' => $sModule, 'action' => 'completed'),
  443. array('unit' => $sModule, 'action' => 'reopened'),
  444. array('unit' => $sModule, 'action' => 'edited'),
  445. array('unit' => $sModule, 'action' => 'deleted'),
  446. )
  447. );
  448. }
  449. /**
  450. * Entry task for Timeline module
  451. */
  452. public function serviceGetTimelinePost($aEvent, $aBrowseParams = array())
  453. {
  454. $CNF = &$this->_oConfig->CNF;
  455. $aResult = parent::serviceGetTimelinePost($aEvent, $aBrowseParams);
  456. if(empty($aResult) || !is_array($aResult) || empty($aResult['date']))
  457. return $aResult;
  458. $aContentInfo = $this->_oDb->getContentInfoById($aEvent['object_id']);
  459. if($aContentInfo[$CNF['FIELD_PUBLISHED']] > $aResult['date'])
  460. $aResult['date'] = $aContentInfo[$CNF['FIELD_PUBLISHED']];
  461. return $aResult;
  462. }
  463. public function serviceGetTimelineCompleted($aEvent, $aBrowseParams = array())
  464. {
  465. $CNF = &$this->_oConfig->CNF;
  466. $aResult = parent::serviceGetTimelinePost($aEvent, $aBrowseParams);
  467. if(empty($aResult) || !is_array($aResult) || empty($aResult['date']))
  468. return $aResult;
  469. $aContentInfo = $this->_oDb->getContentInfoById($aEvent['object_id']);
  470. if($aContentInfo[$CNF['FIELD_PUBLISHED']] > $aResult['date'])
  471. $aResult['date'] = $aContentInfo[$CNF['FIELD_PUBLISHED']];
  472. $aResult['sample_action'] = $aResult['content']['sample_action'] = _t('_bx_tasks_txt_action_completed');
  473. return $aResult;
  474. }
  475. public function serviceGetTimelineReopened($aEvent, $aBrowseParams = array())
  476. {
  477. $CNF = &$this->_oConfig->CNF;
  478. $aResult = parent::serviceGetTimelinePost($aEvent, $aBrowseParams);
  479. if(empty($aResult) || !is_array($aResult) || empty($aResult['date']))
  480. return $aResult;
  481. $aContentInfo = $this->_oDb->getContentInfoById($aEvent['object_id']);
  482. if($aContentInfo[$CNF['FIELD_PUBLISHED']] > $aResult['date'])
  483. $aResult['date'] = $aContentInfo[$CNF['FIELD_PUBLISHED']];
  484. $aResult['sample_action'] = $aResult['content']['sample_action'] = _t('_bx_tasks_txt_action_reopened');
  485. return $aResult;
  486. }
  487. public function serviceGetNotificationsData()
  488. {
  489. $sModule = $this->_aModule['name'];
  490. $sEventPrivacy = $sModule . '_allow_view_event_to';
  491. if(BxDolPrivacy::getObjectInstance($sEventPrivacy) === false)
  492. $sEventPrivacy = '';
  493. $aResult = parent::serviceGetNotificationsData();
  494. $aResult['handlers'] = array_merge($aResult['handlers'], array(
  495. array('group' => $sModule . '_completed', 'type' => 'insert', 'alert_unit' => $sModule, 'alert_action' => 'completed', 'module_name' => $sModule, 'module_method' => 'get_notifications_completed', 'module_class' => 'Module', 'module_event_privacy' => $sEventPrivacy),
  496. array('group' => $sModule . '_reopened', 'type' => 'insert', 'alert_unit' => $sModule, 'alert_action' => 'reopened', 'module_name' => $sModule, 'module_method' => 'get_notifications_reopened', 'module_class' => 'Module', 'module_event_privacy' => $sEventPrivacy),
  497. array('group' => $sModule . '_expired', 'type' => 'insert', 'alert_unit' => $sModule, 'alert_action' => 'expired', 'module_name' => $sModule, 'module_method' => 'get_notifications_expired', 'module_class' => 'Module', 'module_event_privacy' => $sEventPrivacy),
  498. array('group' => $sModule . '_assign', 'type' => 'insert', 'alert_unit' => $sModule, 'alert_action' => 'assigned', 'module_name' => $sModule, 'module_method' => 'get_notifications_assigned', 'module_class' => 'Module', 'module_event_privacy' => $sEventPrivacy),
  499. array('group' => $sModule . '_assign', 'type' => 'delete', 'alert_unit' => $sModule, 'alert_action' => 'unassigned'),
  500. ));
  501. $aResult['settings'] = array_merge($aResult['settings'], array(
  502. array('group' => 'content', 'unit' => $sModule, 'action' => 'completed', 'types' => array('personal', 'follow_member', 'follow_context')),
  503. array('group' => 'content', 'unit' => $sModule, 'action' => 'reopened', 'types' => array('personal', 'follow_member', 'follow_context')),
  504. array('group' => 'content', 'unit' => $sModule, 'action' => 'expired', 'types' => array('personal')),
  505. array('group' => 'content', 'unit' => $sModule, 'action' => 'assigned', 'types' => array('personal')),
  506. ));
  507. $aResult['alerts'] = array_merge($aResult['alerts'], array(
  508. array('unit' => $sModule, 'action' => 'completed'),
  509. array('unit' => $sModule, 'action' => 'reopened'),
  510. array('unit' => $sModule, 'action' => 'expired'),
  511. array('unit' => $sModule, 'action' => 'assigned'),
  512. array('unit' => $sModule, 'action' => 'unassigned'),
  513. ));
  514. return $aResult;
  515. }
  516. public function serviceGetNotificationsCompleted($aEvent)
  517. {
  518. return $this->_serviceGetNotificationsByAction($aEvent, 'completed');
  519. }
  520. public function serviceGetNotificationsReopened($aEvent)
  521. {
  522. return $this->_serviceGetNotificationsByAction($aEvent, 'reopened');
  523. }
  524. public function serviceGetNotificationsExpired($aEvent)
  525. {
  526. return $this->_serviceGetNotificationsByAction($aEvent, 'expired');
  527. }
  528. public function serviceGetNotificationsAssigned($aEvent)
  529. {
  530. return $this->_serviceGetNotificationsByAction($aEvent, 'assigned');
  531. }
  532. protected function _serviceGetNotificationsByAction($aEvent, $sAction)
  533. {
  534. $CNF = &$this->_oConfig->CNF;
  535. $aResult = parent::serviceGetNotificationsPost($aEvent);
  536. if(empty($aResult) || !is_array($aResult))
  537. return $aResult;
  538. $aResult['entry_author'] = $aEvent['object_owner_id'];
  539. $aResult['entry_author_name'] = '';
  540. if(($oAuthor = BxDolProfile::getInstance($aResult['entry_author'])) !== false)
  541. $aResult['entry_author_name'] = $oAuthor->getDisplayName();
  542. $sLangKey = '_bx_tasks_txt_notification_' . $sAction;
  543. if((int)$aEvent['object_privacy_view'] < 0)
  544. $sLangKey .= '_in_context';
  545. $aResult['lang_key'] = _t($sLangKey);
  546. return $aResult;
  547. }
  548. public function serviceCheckAllowedManageInContext($iContextPid)
  549. {
  550. if(!$this->isAllowManageByContext($iContextPid))
  551. return false;
  552. return true;
  553. }
  554. public function serviceCheckAllowedManage($iContentId)
  555. {
  556. if(!$this->isAllowManage($iContentId))
  557. return false;
  558. return true;
  559. }
  560. public function serviceCheckAllowedComplete($iContentId)
  561. {
  562. if(!$this->serviceCheckAllowedManage($iContentId))
  563. return false;
  564. return !$this->isCompleted($iContentId);
  565. }
  566. public function serviceCheckAllowedUncomplete($iContentId)
  567. {
  568. if(!$this->serviceCheckAllowedManage($iContentId))
  569. return false;
  570. return $this->isCompleted($iContentId);
  571. }
  572. public function serviceIsCompleted($iContentId)
  573. {
  574. return $this->isCompleted($iContentId);
  575. }
  576. public function serviceIsUncompleted($iContentId)
  577. {
  578. return !$this->isCompleted($iContentId);
  579. }
  580. public function serviceIsAllowBadges($iContentId)
  581. {
  582. if (!$this->isAllowManage($iContentId))
  583. return false;
  584. if (!$this->serviceIsBadgesAvaliable())
  585. return false;
  586. return true;
  587. }
  588. public function serviceEntityAssignments($iContentId = 0, $bAsArray = false)
  589. {
  590. if(!$iContentId)
  591. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  592. if(!$iContentId)
  593. return false;
  594. $CNF = &$this->_oConfig->CNF;
  595. $aProfiles = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTION'])->getConnectedInitiators($iContentId);
  596. if($bAsArray)
  597. return $aProfiles;
  598. return $this->_oTemplate->entryAssignments($aProfiles);
  599. }
  600. public function serviceEntityTimer($iContentId = 0)
  601. {
  602. if(!$iContentId)
  603. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  604. if(!$iContentId)
  605. return false;
  606. return $this->_oTemplate->entryTimer($iContentId, $this->_iProfileId);
  607. }
  608. public function serviceCheckAllowedCommentsTask($iContentId, $sObjectComments)
  609. {
  610. $CNF = &$this->_oConfig->CNF;
  611. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  612. if($aContentInfo[$CNF['FIELD_ALLOW_COMMENTS']] == 0)
  613. return false;
  614. return parent::serviceCheckAllowedCommentsTask($iContentId, $sObjectComments);
  615. }
  616. public function serviceCheckAllowedCommentsView($iContentId, $sObjectComments)
  617. {
  618. $CNF = &$this->_oConfig->CNF;
  619. //negative id used in comments for reports
  620. if($iContentId < 0)
  621. return CHECK_ACTION_RESULT_ALLOWED;
  622. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  623. if($aContentInfo[$CNF['FIELD_ALLOW_COMMENTS']] == 0)
  624. return false;
  625. return parent::serviceCheckAllowedCommentsView($iContentId, $sObjectComments);
  626. }
  627. /**
  628. * @page service Service Calls
  629. * @section bx_tasks Tasks
  630. * @subsection bx_tasks-page_blocks Page Blocks
  631. * @subsubsection bx_tasks-calendar calendar
  632. *
  633. * @code bx_srv('bx_tasks', 'calendar', [...]); @endcode
  634. *
  635. * Shows tasks calendar baced on die date
  636. *
  637. * @param $aData additional data to point which events to show, leave empty to show all events, specify event's ID in 'event' array key to show calendar for one event only, specify context's ID in 'context_id' array key to show calendar for one context events only. If only one event is specified then it will show calendar only if it's repeating event.
  638. * @param $sTemplate template to use to show calendar, or leave empty for default template, possible options: calendar.html, calendar_compact.html
  639. * @return HTML string with calendar to display on the site, all necessary CSS and JS files are automatically added to the HEAD section of the site HTML. On error empty string is returned.
  640. *
  641. * @see BxTasksModule::serviceCalendar
  642. */
  643. /**
  644. * @ref bx_tasks-calendar "calendar"
  645. */
  646. public function serviceCalendar($aData = array(), $sTemplate = 'calendar.html')
  647. {
  648. if (!$this->isAllowView($aData['context_id']))
  649. return;
  650. $o = new BxTemplCalendar(array(
  651. 'eventSources' => array (
  652. bx_append_url_params(BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'calendar_data', $aData),
  653. ),
  654. ), $this->_oTemplate);
  655. return $o->display($sTemplate);
  656. }
  657. public function serviceGetCalendarEntries($iProfileId)
  658. {
  659. $CNF = &$this->_oConfig->CNF;
  660. $oConn = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTION']);
  661. $aData = $oConn->getConnectedInitiators($iProfileId);
  662. $aData2 = array(0);
  663. foreach($aData as $iProfileId2) {
  664. $oProfile = BxDolProfile::getInstance($iProfileId2);
  665. array_push($aData2, $oProfile->getContentId());
  666. }
  667. $aSQLPart['where'] = " AND " . $CNF['TABLE_ENTRIES'] . ".`" . $CNF['FIELD_ID'] . "` IN(" . implode(',', $aData2) . ")";
  668. return $this->_oDb->getEntriesByDate(bx_get('start'), bx_get('end'), null, $aSQLPart);
  669. }
  670. public function serviceBrowseHome($aParams = [])
  671. {
  672. $iContextPid = bx_process_input(bx_get('context_pid'), BX_DATA_INT);
  673. if(!$iContextPid)
  674. return MsgBox(_t('_Empty'));
  675. $sContent = $this->serviceBrowseTasks($iContextPid);
  676. if($sContent && ($oProfile = BxDolProfile::getInstance($iContextPid)) !== false)
  677. return [
  678. 'title' => bx_replace_markers(_t('_bx_tasks_page_block_title_entries_in_context'), [
  679. 'display_name' => $oProfile->getDisplayName(),
  680. 'profile_link' => $oProfile->getUrl()
  681. ]),
  682. 'content' => $sContent
  683. ];
  684. return $sContent;
  685. }
  686. public function serviceBrowseContext($iProfileId = 0, $aParams = [])
  687. {
  688. if(!$iProfileId)
  689. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  690. if(!$iProfileId)
  691. return '';
  692. return $this->serviceBrowseTasks ($iProfileId, $aParams);
  693. }
  694. public function serviceBrowseTasks($iContextId = 0, $aParams = [])
  695. {
  696. if(!$this->isAllowView($iContextId))
  697. return '';
  698. return $this->_oTemplate->getEntriesList($iContextId, $aParams);
  699. }
  700. /**
  701. * Common methods
  702. */
  703. public function getModuleTitle($sName)
  704. {
  705. $sResult = $sName;
  706. if(($_sTitle = '_' . $sName) && ($sTitle = _t($_sTitle)) && strcmp($_sTitle, $sTitle) != 0)
  707. $sResult = $sTitle;
  708. else if(($aModule = $this->_oModule->_oDb->getModuleByName($sName)) && is_array($aModule))
  709. $sResult = $aModule['title'] ?? _t('_undefined');
  710. return $sResult;
  711. }
  712. public function getAssignees()
  713. {
  714. $aResults = [];
  715. if(($oProfile = BxDolProfile::getInstance()) !== false && ($aIds = $this->_oDb->getTimeTracks(['sample' => 'assignees'])))
  716. foreach($aIds as $iId)
  717. $aResults[] = ['key' => $iId, 'value' => $oProfile->getDisplayName($iId)];
  718. return $aResults;
  719. }
  720. public function getPreLists()
  721. {
  722. $CNF = &$this->_oConfig->CNF;
  723. $aResults = [];
  724. if(($aPreLists = $this->_oDb->getPreLists(['sample' => 'all'])) && is_array($aPreLists))
  725. foreach($aPreLists as $aPreList)
  726. $aResults[$aPreList['name']] = _t($aPreList['title']);
  727. return $aResults;
  728. }
  729. public function getContexts()
  730. {
  731. $aContexts = [];
  732. $aModules = bx_srv('system', 'get_modules_by_type', ['context', ['name_as_key' => true]]);
  733. foreach($aModules as $sModule => $aModule)
  734. $aContexts[$sModule] = ($_sTitle = '_' . $sModule) && ($sTitle = _t($_sTitle)) && strcmp($_sTitle, $sTitle) != 0 ? $sTitle : $aModule['title'];
  735. return $aContexts;
  736. }
  737. public function getContextMembers($iContextPid)
  738. {
  739. $aMembers = [];
  740. if(($oContext = BxDolProfile::getInstance($iContextPid)) !== false) {
  741. $aPids = bx_srv($oContext->getModule(), 'fans', [$oContext->getContentId(), true]);
  742. foreach($aPids as $iPid)
  743. $aMembers[$iPid] = $oContext->getDisplayName($iPid);
  744. }
  745. return $aMembers;
  746. }
  747. public function getContextEntries($iContextPid)
  748. {
  749. $CNF = &$this->_oConfig->CNF;
  750. $aEntries = [];
  751. if(($aTasks = $this->_oDb->getTasks($iContextPid)) && is_array($aTasks))
  752. foreach($aTasks as $aTask)
  753. $aEntries[$aTask[$CNF['FIELD_ID']]] = $aTask[$CNF['FIELD_TITLE']];
  754. return $aEntries;
  755. }
  756. public function getTimer($iContentId, $iProfileId)
  757. {
  758. return $this->_oDb->getTimers(['sample' => 'content_profile_ids', 'content_id' => $iContentId, 'profile_id' => $iProfileId]);
  759. }
  760. public function getTimersByAuthor($iProfileId, $bActive = false)
  761. {
  762. return $this->_oDb->getTimers(['sample' => 'profile_id', 'profile_id' => $iProfileId, 'active' => $bActive]);
  763. }
  764. public function stopTimerByAuthor($iProfileId)
  765. {
  766. $aTimer = $this->getTimersByAuthor($iProfileId, true);
  767. if(!$aTimer || !is_array($aTimer))
  768. return false;
  769. return $this->updateTimerById($aTimer['id'], [
  770. 'started' => 0,
  771. 'duration' => $aTimer['duration'] + (time() - $aTimer['started']),
  772. ]);
  773. }
  774. public function updateTimerById($iId, $aSet)
  775. {
  776. return $this->_oDb->updateTimer($aSet, ['id' => (int)$iId]) !== false;
  777. }
  778. public function onPublished($iContentId)
  779. {
  780. parent::onPublished($iContentId);
  781. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  782. if(!$aContentInfo)
  783. return;
  784. $CNF = &$this->_oConfig->CNF;
  785. if(($iContextId = (int)$aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]) < 0) {
  786. $iContextId = abs($iContextId);
  787. $mixedRepo = $this->_oDb->getContextRepository($iContextId);
  788. if($mixedRepo !== false && ($sMl = 'bx_github') && ($sMd = 'create_issue') && bx_is_srv($sMl, $sMd)) {
  789. list($sGhUsername, $sGhRepository) = $mixedRepo;
  790. $aFldLoc2Repo = [
  791. $CNF['FIELD_STICKERS'] => 'labels',
  792. $CNF['FIELD_TYPE'] => 'type',
  793. ];
  794. $aGhFields = [];
  795. foreach($aFldLoc2Repo as $sLocField => $sRepoField)
  796. if(($sLocValue = $aContentInfo[$sLocField] ?? false))
  797. switch($sLocField) {
  798. case $CNF['FIELD_STICKERS']:
  799. $aStickers = $this->getStickers($sLocValue, $iContextId);
  800. if($aStickers && is_array($aStickers)) {
  801. $aGhFields[$sRepoField] = [];
  802. foreach($aStickers as $aSticker)
  803. $aGhFields[$sRepoField][] = $aSticker['title'];
  804. }
  805. break;
  806. case $CNF['FIELD_TYPE']:
  807. $aGhFields[$sRepoField] = $this->getType($sLocValue, $iContextId);
  808. break;
  809. default:
  810. $aGhFields[$sRepoField] = $sLocValue;
  811. }
  812. $aIssue = bx_srv($sMl, $sMd, [$sGhUsername, $sGhRepository, $aContentInfo[$CNF['FIELD_TITLE']], $aContentInfo[$CNF['FIELD_TEXT']], $aGhFields]);
  813. if($aIssue && is_array($aIssue) && ($iNumber = (int)($aIssue['number'] ?? 0)) != 0 && ($sUrl = $aIssue['html_url'] ?? '') != '') {
  814. $this->_oDb->updateEntriesBy([
  815. $CNF['FIELD_GH_ISSUE'] => $iNumber,
  816. $CNF['FIELD_GH_ISSUE_URL'] => $sUrl,
  817. ], [$CNF['FIELD_ID'] => $iContentId]);
  818. $this->logActivity($iContentId, ['key' => '_bx_tasks_txt_msg_synced', 'markers' => [
  819. 'ghi_link' => $sUrl,
  820. 'ghi_number' => $iNumber
  821. ]]);
  822. }
  823. }
  824. }
  825. }
  826. public function onExpired($iContentId)
  827. {
  828. $CNF = &$this->_oConfig->CNF;
  829. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  830. if(!$aContentInfo)
  831. return;
  832. $oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTION']);
  833. if($oConnection) {
  834. $aProfileIds = $oConnection->getConnectedContent($iContentId);
  835. if(!empty($aProfileIds) && is_array($aProfileIds))
  836. foreach($aProfileIds as $iProfileId){
  837. /**
  838. * @hooks
  839. * @hookdef hook-bx_tasks-expired 'bx_tasks', 'expired' - hook on task unassigned to profile
  840. * - $unit_name - equals `bx_tasks`
  841. * - $action - equals `expired`
  842. * - $object_id - task id
  843. * - $sender_id - not used
  844. * - $extra_params - array of additional params with the following array keys:
  845. * - `object_author_id` - [int] profile_id for task's author
  846. * - `privacy_view` - [string] privacy view value
  847. * @hook @ref hook-bx_tasks-expired
  848. */
  849. bx_alert($this->getName(), 'expired', $iContentId, false, array(
  850. 'object_author_id' => $iProfileId,
  851. 'privacy_view' => $aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]
  852. ));
  853. }
  854. }
  855. }
  856. public function isAllowView($iContextId)
  857. {
  858. if(!($oContext = BxDolProfile::getInstance($iContextId)) || $oContext->checkAllowedProfileView($iContextId) !== CHECK_ACTION_RESULT_ALLOWED)
  859. return false;
  860. return true;
  861. }
  862. public function isAllowAdd($iContextId)
  863. {
  864. if(!($oContext = BxDolProfile::getInstance($iContextId)) || $oContext->checkAllowedPostInProfile($iContextId) !== CHECK_ACTION_RESULT_ALLOWED)
  865. return false;
  866. return true;
  867. }
  868. public function isAllowManageByContext($iContextId)
  869. {
  870. if(isAdmin())
  871. return true;
  872. $oProfileContext = BxDolProfile::getInstance($iContextId);
  873. if(BxDolService::call($oProfileContext->getModule(), 'is_admin', array($iContextId)))
  874. return true;
  875. return false;
  876. }
  877. public function isAllowManage($mixedContent)
  878. {
  879. $CNF = &$this->_oConfig->CNF;
  880. $aContentInfo = !is_array($mixedContent) ? $this->_oDb->getContentInfoById((int)$mixedContent) : $mixedContent;
  881. if($this->checkAllowedEdit($aContentInfo) === CHECK_ACTION_RESULT_ALLOWED)
  882. return true;
  883. if($this->isAllowManageByContext(abs($aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']])))
  884. return true;
  885. if(($oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTION'])) !== false) {
  886. $iLoggedId = bx_get_logged_profile_id();
  887. $aProfileIds = $oConnection->getConnectedInitiators($aContentInfo[$CNF['FIELD_ID']]);
  888. if(!empty($aProfileIds) && is_array($aProfileIds) && in_array($iLoggedId, $aProfileIds))
  889. return true;
  890. }
  891. return false;
  892. }
  893. /**
  894. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden. So make sure to make strict(===) checking.
  895. */
  896. public function checkAllowedManage ($aDataEntry, $isPerformAction = false)
  897. {
  898. return $this->isAllowManage($aDataEntry) ? CHECK_ACTION_RESULT_ALLOWED : _t('_sys_txt_access_denied');
  899. }
  900. public function isCompleted($iContentId)
  901. {
  902. $CNF = &$this->_oConfig->CNF;
  903. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  904. return (bool)$aContentInfo[$CNF['FIELD_COMPLETED']];
  905. }
  906. public function complete($iContentId, $iCompleted)
  907. {
  908. $CNF = &$this->_oConfig->CNF;
  909. $iCompleted = (int)$iCompleted;
  910. $this->_oDb->updateEntriesBy([$CNF['FIELD_COMPLETED'] => $iCompleted], [$CNF['FIELD_ID'] => (int)$iContentId]);
  911. $sModule = $this->getName();
  912. $sAction = !$iCompleted ? 'reopened' : 'completed';
  913. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  914. $iContentAuthor = (int)$aContentInfo[$CNF['FIELD_AUTHOR']];
  915. /**
  916. * @hooks
  917. * @hookdef hook-bx_tasks-completed 'bx_tasks', 'completed' - hook on task unassigned to profile
  918. * - $unit_name - equals `bx_tasks`
  919. * - $action - can be `completed` or `reopened`
  920. * - $object_id - task id
  921. * - $sender_id - not used
  922. * - $extra_params - array of additional params with the following array keys:
  923. * - `object_author_id` - [int] profile_id for task's author
  924. * - `privacy_view` - [string] privacy view value
  925. * @hook @ref hook-bx_tasks-completed
  926. */
  927. bx_alert($sModule, $sAction, $iContentId, false, array(
  928. 'object_author_id' => $iContentAuthor,
  929. 'privacy_view' => $aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]
  930. ));
  931. if(($oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTION'])) !== false) {
  932. $aProfileIds = $oConnection->getConnectedContent($iContentId);
  933. if(!empty($aProfileIds) && is_array($aProfileIds))
  934. foreach($aProfileIds as $iProfileId) {
  935. if($iProfileId == $iContentAuthor)
  936. continue;
  937. bx_alert($sModule, $sAction, $iContentId, false, array(
  938. 'object_author_id' => $iProfileId,
  939. 'privacy_view' => $aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]
  940. ));
  941. }
  942. }
  943. $this->logActivity($iContentId, ['key' => '_bx_tasks_txt_msg_' . ($iCompleted ? '' : 'un') . 'completed']);
  944. if(($iContextId = (int)$aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]) < 0 && ($mixedRepository = $this->_oDb->getContextRepository(abs($iContextId))) !== false) {
  945. $sModule = 'bx_github';
  946. $sMethod = ($iCompleted ? 'close' : 'reopen') . '_issue';
  947. if(bx_is_srv($sModule, $sMethod) && ($iGhIssue = (int)$aContentInfo[$CNF['FIELD_GH_ISSUE']]) != 0) {
  948. list($sGhUsername, $sGhRepository) = $mixedRepository;
  949. bx_srv($sModule, $sMethod, [$sGhUsername, $sGhRepository, $iGhIssue]);
  950. }
  951. }
  952. return true;
  953. }
  954. public function logActivity($iContentId, $aMessage)
  955. {
  956. $CNF = &$this->_oConfig->CNF;
  957. $oCmts = BxDolCmts::getObjectInstance($CNF['OBJECT_COMMENTS'], $iContentId);
  958. if(!$oCmts || !$oCmts->isEnabled())
  959. return false;
  960. if(($oAuthor = BxDolProfile::getInstance()) !== false)
  961. $aMessage['markers'] = array_merge($aMessage['markers'] ?? [], [
  962. 'author_link' => $oAuthor->getUrl(),
  963. 'author_name' => $oAuthor->getDisplayName(),
  964. ]);
  965. return $oCmts->addAuto($aMessage);
  966. }
  967. public function ghUpdateIssue($mixedContent, $aGhFields = [])
  968. {
  969. $aContentInfo = is_array($mixedContent) ? $mixedContent : $this->_oDb->getContentInfoById((int)$mixedContent);
  970. if(!$aContentInfo || !is_array($aContentInfo))
  971. return false;
  972. $CNF = &$this->_oConfig->CNF;
  973. $iContentId = (int)$aContentInfo[$CNF['FIELD_ID']];
  974. $iContextId = (int)$aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']];
  975. if($iContextId >= 0)
  976. return false;
  977. $mixedRepo = $this->_oDb->getContextRepository(abs($iContextId));
  978. if($mixedRepo === false)
  979. return false;
  980. list($sGhUsername, $sGhRepository) = $mixedRepo;
  981. $iGhIssue = (int)$aContentInfo[$CNF['FIELD_GH_ISSUE']];
  982. $sMl = 'bx_github';
  983. if($aGhFields && ($sMd = 'update_issue') && bx_is_srv($sMl, $sMd))
  984. if(bx_srv($sMl, $sMd, [$sGhUsername, $sGhRepository, $iGhIssue, $aGhFields]) !== false)
  985. $this->logActivity($iContentId, ['key' => '_bx_tasks_txt_msg_synced', 'markers' => [
  986. 'ghi_link' => $aContentInfo[$CNF['FIELD_GH_ISSUE_URL']],
  987. 'ghi_number' => $iGhIssue
  988. ]]);
  989. return true;
  990. }
  991. public function getStickers($mixedValues, $iContextId = 0)
  992. {
  993. $CNF = &$this->_oConfig->CNF;
  994. $aValues = is_array($mixedValues) ? $mixedValues : BxDolFormCheckerHelper::displaySet($mixedValues);
  995. $aResults = [];
  996. if($iContextId) {
  997. $aStickers = $this->_oDb->getPreValues([
  998. 'sample' => 'context_list',
  999. 'context_id' => $iContextId,
  1000. 'list' => 'sticker'
  1001. ]);
  1002. foreach($aValues as $sValue)
  1003. if(($aSticker = $aStickers[$sValue] ?? false))
  1004. $aResults[] = [
  1005. 'title' => $aSticker['title'],
  1006. 'color' => $aSticker['color']
  1007. ];
  1008. }
  1009. if(!$aResults) {
  1010. $aStickers = BxDolForm::getDataItems($CNF['OBJECT_PRE_LIST_STICKERS']);
  1011. foreach($aValues as $sValue)
  1012. $aResults[] = [
  1013. 'title' => $aStickers[$sValue],
  1014. 'color' => ''
  1015. ];
  1016. }
  1017. return $aResults;
  1018. }
  1019. public function getType($mixedValue, $iContextId = 0)
  1020. {
  1021. $sResult = '';
  1022. if($iContextId) {
  1023. $aType = $this->_oDb->getPreValues([
  1024. 'sample' => 'context_list_value',
  1025. 'context_id' => $iContextId,
  1026. 'list' => 'type',
  1027. 'value' => $mixedValue
  1028. ]);
  1029. if($aType && is_array($aType))
  1030. $sResult = $aType['title'];
  1031. }
  1032. if(!$sResult && ($sTypeTitle = $this->_oConfig->getTypeTitle($mixedValue)) != '')
  1033. $sResult = _t($sTypeTitle);
  1034. return $sResult;
  1035. }
  1036. /**
  1037. * Internal methods
  1038. */
  1039. protected function _onEditProperty($aContentInfo, $sProperty, &$oForm)
  1040. {
  1041. $CNF = &$this->_oConfig->CNF;
  1042. $aPropLoc2Repo = [
  1043. $CNF['FIELD_TYPE'] => 'type',
  1044. ];
  1045. $iContentId = (int)$aContentInfo[$CNF['FIELD_ID']];
  1046. $mixedValue = $oForm->getCleanValue($CNF['FIELD_' . strtoupper($sProperty)]);
  1047. if(($sMethod = 'get' . bx_gen_method_name($sProperty) . 'Title') && method_exists($this->_oConfig, $sMethod))
  1048. $mixedValue = $this->_oConfig->$sMethod($mixedValue);
  1049. $this->logActivity($iContentId, ['key' => '_bx_tasks_txt_msg_edit_' . $sProperty, 'markers' => ['value' => $mixedValue]]);
  1050. if(($sPropRepo = $aPropLoc2Repo[$sProperty] ?? false))
  1051. $this->ghUpdateIssue($aContentInfo, [
  1052. $sPropRepo => _t($mixedValue)
  1053. ]);
  1054. }
  1055. protected function _onEditState($aContentInfo, &$oForm)
  1056. {
  1057. $CNF = &$this->_oConfig->CNF;
  1058. $iContentId = (int)$aContentInfo[$CNF['FIELD_ID']];
  1059. $iState = $oForm->getCleanValue($CNF['FIELD_STATE']);
  1060. $this->logActivity($iContentId, ['key' => '_bx_tasks_txt_msg_edit_state', 'markers' => ['value' => $this->_oConfig->getStateTitle($iState)]]);
  1061. $iCompleted = (int)$this->_oConfig->isCompleted($iState);
  1062. if($iCompleted != (int)$this->_oConfig->isCompleted($aContentInfo[$CNF['FIELD_STATE']]))
  1063. $this->complete($iContentId, $iCompleted);
  1064. }
  1065. }
  1066. /** @} */