BxTasksModule.php 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066
  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 actionSetFilterValue($iListId, $sValue)
  55. {
  56. $CNF = &$this->_oConfig->CNF;
  57. $aTmp = array();
  58. if (isset($_COOKIE[$CNF['COOKIE_SETTING_KEY']]))
  59. $aTmp = json_decode($_COOKIE[$CNF['COOKIE_SETTING_KEY']], true);
  60. if ($sValue != '')
  61. $aTmp[$iListId] = $sValue;
  62. else
  63. unset($aTmp[$iListId]);
  64. bx_setcookie($CNF['COOKIE_SETTING_KEY'], json_encode($aTmp), time() + 60*60*24*365);
  65. }
  66. public function actionProcessTaskListForm($iContextId, $iId)
  67. {
  68. if (!$this->isAllowAdd(-$iContextId))
  69. return;
  70. $CNF = &$this->_oConfig->CNF;
  71. $oForm = null;
  72. $sPopupTitle = "";
  73. $aContentInfo = array();
  74. if ($iId == 0){
  75. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_LIST_ENTRY'], $CNF['OBJECT_FORM_LIST_ENTRY_DISPLAY_ADD']);
  76. $sPopupTitle = _t('_bx_tasks_form_list_entry_display_add');
  77. }
  78. else {
  79. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_LIST_ENTRY'], $CNF['OBJECT_FORM_LIST_ENTRY_DISPLAY_EDIT']);
  80. $aContentInfo = $this->_oDb->getList($iId);
  81. $sPopupTitle = _t('_bx_tasks_form_list_entry_display_edit');
  82. }
  83. $oForm->aFormAttrs['action'] = BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'process_task_list_form/' . $iContextId . '/' . $iId . '/';
  84. if (!$oForm)
  85. return '';
  86. $oForm->initChecker($aContentInfo, array());
  87. if($oForm->isSubmittedAndValid()) {
  88. if ($iId == 0){
  89. $aValsToAdd['context_id'] = $iContextId;
  90. $iId = $oForm->insert($aValsToAdd);
  91. }
  92. else {
  93. $iId = $oForm->update($iId);
  94. }
  95. return echoJson(array(
  96. 'eval' => $this->_oConfig->getJsObject('tasks') . '.reloadData(oData, ' . $iContextId . ')',
  97. ));
  98. }
  99. else {
  100. $sContent = $this->_oTemplate->parseHtmlByName('popup_form.html', array(
  101. 'form_id' => $oForm->getId(),
  102. 'form' => $oForm->getCode(true)
  103. ));
  104. if (!$oForm->isSubmitted()) {
  105. echo $sContent;
  106. return;
  107. }
  108. return echoJson(array('form' => $sContent, 'form_id' => $oForm->getId()));
  109. }
  110. }
  111. public function actionDeleteTaskList($iId, $iContextId)
  112. {
  113. if (!$this->isAllowManageByContext($iContextId))
  114. return;
  115. $CNF = &$this->_oConfig->CNF;
  116. $aTasks = $this->_oDb->getTasks($iContextId, $iId);
  117. $this->_oDb->deleteList($iId);
  118. if (!empty($aTasks) && ($oConn = BxDolConnection::getObjectInstance($this->_oConfig->CNF['OBJECT_CONNECTION']))) {
  119. foreach ($aTasks as &$aTask)
  120. $oConn->onDeleteContent($aTask[$CNF['FIELD_ID']]);
  121. }
  122. echoJson(array(
  123. 'context_id' => $iContextId,
  124. ));
  125. }
  126. public function actionProcessTaskForm($iContextId, $iListId)
  127. {
  128. $_iContextId = abs($iContextId);
  129. if(!$this->isAllowAdd($_iContextId))
  130. return echoJson([]);
  131. $CNF = &$this->_oConfig->CNF;
  132. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_ENTRY'], $CNF['OBJECT_FORM_ENTRY_DISPLAY_ADD']);
  133. if(!$oForm)
  134. return echoJson([]);
  135. $oForm->setAction(BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'process_task_form/' . $iContextId . '/' . $iListId . '/');
  136. $oForm->setContextId($_iContextId);
  137. $oForm->initChecker();
  138. if($oForm->isSubmittedAndValid()) {
  139. $iContentId = $oForm->insert([$CNF['FIELD_ALLOW_VIEW_TO'] => $iContextId, $CNF['FIELD_TASKLIST'] => $iListId]);
  140. if($iContentId)
  141. $this->onPublished($iContentId);
  142. return echoJson([
  143. 'eval' => $this->_oConfig->getJsObject('tasks') . '.reloadData(oData, ' . $iContextId . ')',
  144. ]);
  145. }
  146. else {
  147. $sContent = $this->_oTemplate->parseHtmlByName('popup_form.html', [
  148. 'form_id' => $oForm->getId(),
  149. 'form' => $oForm->getCode(true)
  150. ]);
  151. if($oForm->isSubmitted())
  152. return echoJson(['form' => $sContent, 'form_id' => $oForm->getId()]);
  153. echo $sContent;
  154. }
  155. }
  156. public function actionProcessTaskFormEditProperty($iContentId, $sProperty)
  157. {
  158. $CNF = &$this->_oConfig->CNF;
  159. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  160. if(!$this->isAllowAdd(abs($aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']])))
  161. return '';
  162. $sForm = '';
  163. if(($sKey = 'OBJECT_FORM_ENTRY_DISPLAY_EDIT_' . strtoupper($sProperty)) && !empty($CNF[$sKey]))
  164. $sForm = $CNF[$sKey];
  165. else
  166. return '';
  167. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_ENTRY'], $sForm);
  168. $oForm->setId($sForm);
  169. $oForm->setName($sForm);
  170. $oForm->setAction(BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'process_task_form_edit_property/' . $iContentId . '/' . $sProperty . '/');
  171. if(!$oForm)
  172. return '';
  173. $oForm->initChecker($aContentInfo);
  174. if($oForm->isSubmittedAndValid()) {
  175. if(!$oForm->update($iContentId))
  176. return echoJson(['msg' => _t('_bx_tasks_txt_err_cannot_perform_action')]);
  177. if(($sMethod = '_onEdit' . bx_gen_method_name($sProperty)) && method_exists($this, $sMethod))
  178. $this->$sMethod($aContentInfo, $oForm);
  179. else
  180. $this->_onEditProperty($aContentInfo, $sProperty, $oForm);
  181. return echoJson([
  182. 'eval' => $this->_oConfig->getJsObject('tasks') . '.reload(oData)',
  183. ]);
  184. }
  185. else {
  186. $sContent = $this->_oTemplate->parseHtmlByName('popup_form.html', [
  187. 'form_id' => $oForm->getId(),
  188. 'form' => $oForm->getCode(true)
  189. ]);
  190. if (!$oForm->isSubmitted()) {
  191. echo $sContent;
  192. return;
  193. }
  194. return echoJson(['form' => $sContent, 'form_id' => $oForm->getId()]);
  195. }
  196. }
  197. public function actionCalendarData()
  198. {
  199. // check permissions
  200. $aSQLPart = array();
  201. $iContextId = (int)bx_get('context_id');
  202. if(!$this->isAllowView($iContextId))
  203. return;
  204. $oPrivacy = BxDolPrivacy::getObjectInstance($this->_oConfig->CNF['OBJECT_PRIVACY_VIEW']);
  205. if($iContextId) {
  206. $aSQLPart = $oPrivacy ? $oPrivacy->getContentByGroupAsSQLPart(- $iContextId) : array();
  207. }
  208. // get entries
  209. $aEntries = $this->_oDb->getEntriesByDate(bx_get('start'), bx_get('end'), bx_get('event'), $aSQLPart);
  210. header('Content-Type: application/json; charset=utf-8');
  211. echo json_encode($aEntries);
  212. }
  213. public function actionProcessTimer($sAction, $iContentId, $iProfileId)
  214. {
  215. if(!$this->_iProfileId || $this->_iProfileId != $iProfileId)
  216. return echoJson(['msg' => _t('_sys_txt_access_denied')]);
  217. $aResult = $this->serviceProcessTimer($sAction, $iContentId, $iProfileId);
  218. if($aResult['code'] == 0)
  219. $aResult = array_merge($aResult, [
  220. 'content_id' => $iContentId,
  221. 'profile_id' => $iProfileId,
  222. 'content' => $this->_oTemplate->getTimer($iContentId, $iProfileId),
  223. 'eval' => $this->_oConfig->getJsObject('timer') . '.onPerformAction' . bx_gen_method_name($sAction) . '(oData)'
  224. ]);
  225. return echoJson($aResult);
  226. }
  227. public function serviceGetSafeServices()
  228. {
  229. return array_merge(parent::serviceGetSafeServices(), [
  230. 'ProcessTimer' => '',
  231. ]);
  232. }
  233. public function serviceProcessTimer($sAction, $iContentId, $iProfileId)
  234. {
  235. $CNF = &$this->_oConfig->CNF;
  236. $aResult = ['code' => 1, 'msg' => _t('_sys_txt_error_occured')];
  237. $iNow = time();
  238. switch($sAction) {
  239. case 'start':
  240. $this->stopTimerByAuthor($iProfileId);
  241. $iTimer = false;
  242. $aTimer = $this->getTimer($iContentId, $iProfileId);
  243. if($aTimer && is_array($aTimer)) {
  244. if($this->updateTimerById($aTimer['id'], ['started' => $iNow]))
  245. $iTimer = $aTimer['id'];
  246. }
  247. else
  248. $iTimer = $this->_oDb->insertTimer(['content_id' => $iContentId, 'profile_id' => $iProfileId, 'started' => $iNow]);
  249. if($iTimer !== false)
  250. $aResult = ['code' => 0, 'id' => $iTimer];
  251. break;
  252. case 'stop':
  253. if($this->stopTimerByAuthor($iProfileId))
  254. $aResult = ['code' => 0];
  255. break;
  256. case 'resume':
  257. $this->stopTimerByAuthor($iProfileId);
  258. $aTimer = $this->getTimer($iContentId, $iProfileId);
  259. if($aTimer && is_array($aTimer) && $this->updateTimerById($aTimer['id'], ['started' => $iNow]))
  260. $aResult = ['code' => 0];
  261. break;
  262. case 'reload':
  263. $aTimer = $this->getTimer($iContentId, $iProfileId);
  264. $aResult = [
  265. 'code' => 0,
  266. 'started' => $aTimer && ($aTimer['started'] ?? 0) != 0
  267. ];
  268. break;
  269. case 'log':
  270. $aTimer = $this->getTimer($iContentId, $iProfileId);
  271. if(!$aTimer || !is_array($aTimer) || !$aTimer['duration'])
  272. break;
  273. list($iH, $iM) = $this->_oConfig->timeI2A($aTimer['duration'], true);
  274. $iNow = time();
  275. $iTrackId = $this->_oDb->insertTimeTrack([
  276. 'object_id' => $iContentId,
  277. 'author_id' => $iProfileId,
  278. 'author_nip' => $iProfileId == $this->_iProfileId ? bx_get_ip_hash(getVisitorIP()) : 0,
  279. 'value' => $this->_oConfig->timeA2I([(int)$iH, (int)$iM + 1]),
  280. 'value_date' => $iNow,
  281. 'date' => $iNow
  282. ]);
  283. if(!$iTrackId)
  284. break;
  285. $oTime = BxDolReport::getObjectInstance($CNF['OBJECT_REPORTS_TIME'], $iContentId);
  286. if(!$oTime || !$oTime->isEnabled())
  287. break;
  288. if(!$oTime->putReport($iContentId, $iProfileId, $iTrackId))
  289. break;
  290. $this->_oDb->deleteTimer([
  291. 'content_id' => $iContentId,
  292. 'profile_id' => $iProfileId
  293. ]);
  294. $aResult = ['code' => 0];
  295. break;
  296. case 'clear':
  297. if($this->_oDb->deleteTimer(['content_id' => $iContentId, 'profile_id' => $iProfileId]) !== false)
  298. $aResult = ['code' => 0];
  299. break;
  300. }
  301. return $aResult;
  302. }
  303. public function serviceManageTools($sType = 'common')
  304. {
  305. $CNF = &$this->_oConfig->CNF;
  306. $mixedResults = '';
  307. if(!($mixedResults = parent::serviceManageTools($sType)))
  308. return $mixedResults;
  309. return array_merge(parent::serviceManageTools($sType), [
  310. 'menu' => BxDolMenu::getObjectInstance($CNF['OBJECT_MENU_MANAGE_TOOLS_SUBMENU'])
  311. ]);
  312. }
  313. public function serviceGetBlockMenuBrowse()
  314. {
  315. $CNF = &$this->_oConfig->CNF;
  316. $oMenu = BxDolMenu::getObjectInstance($CNF['OBJECT_MENU_BROWSE']);
  317. if(!$oMenu)
  318. return '';
  319. return $oMenu->getCode();
  320. }
  321. public function serviceGetBlockMenuContext($iProfileId)
  322. {
  323. $CNF = &$this->_oConfig->CNF;
  324. $oMenu = BxDolMenu::getObjectInstance($CNF['OBJECT_MENU_SUBMENU_VIEW_CONTEXT']);
  325. if(!$oMenu)
  326. return '';
  327. $oMenu->addMarkers(['profile_id' => $iProfileId]);
  328. return $oMenu->getCode();
  329. }
  330. public function serviceGetBlockManageTime($sType = 'common', $iContextPid = 0)
  331. {
  332. $CNF = &$this->_oConfig->CNF;
  333. $bContextPid = !empty($iContextPid);
  334. $sGrid = $CNF['OBJECT_GRID_TIME_' . ($bContextPid ? 'CONTEXT_' : '') . strtoupper($sType)];
  335. $oGrid = BxDolGrid::getObjectInstance($sGrid);
  336. if(!$oGrid)
  337. return $this->_bIsApi ? [] : '';
  338. if($bContextPid)
  339. $oGrid->setContextPid($iContextPid);
  340. if($this->_bIsApi)
  341. return [
  342. bx_api_get_block('grid', $oGrid->getCodeAPI())
  343. ];
  344. list($aCssCalendar, $aJsCalendar) = BxBaseFormView::getCssJsCalendar();
  345. $this->_oTemplate->addCss(array_merge($aCssCalendar, ['manage_tools.css', 'time.css']));
  346. $this->_oTemplate->addJs(array_merge($aJsCalendar, ['modules/base/text/js/|manage_tools.js', 'time.js']));
  347. $this->_oTemplate->addJsTranslation(['_sys_grid_search']);
  348. $aResult = [
  349. 'content' => $this->_oTemplate->getJsCode('time', [
  350. 'sObjNameGrid' => $sGrid
  351. ]) . $oGrid->getCode()
  352. ];
  353. if(!$bContextPid)
  354. $aResult['menu'] = BxDolMenu::getObjectInstance($CNF['OBJECT_MENU_MANAGE_TOOLS_SUBMENU']);
  355. return $aResult;
  356. }
  357. public function serviceGetBlockTimers()
  358. {
  359. $CNF = &$this->_oConfig->CNF;
  360. return [
  361. 'content' => $this->_oTemplate->getBlockTimers($this->_iProfileId),
  362. 'menu' => BxDolMenu::getObjectInstance($CNF['OBJECT_MENU_MANAGE_TOOLS_SUBMENU'])
  363. ];
  364. }
  365. /**
  366. * Data for Timeline module
  367. */
  368. public function serviceGetTimelineData()
  369. {
  370. $sModule = $this->_aModule['name'];
  371. return array(
  372. 'handlers' => array(
  373. 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' => ''),
  374. 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' => ''),
  375. 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' => ''),
  376. array('group' => $sModule . '_object', 'type' => 'update', 'alert_unit' => $sModule, 'alert_action' => 'edited'),
  377. array('group' => $sModule . '_object', 'type' => 'delete', 'alert_unit' => $sModule, 'alert_action' => 'deleted'),
  378. ),
  379. 'alerts' => array(
  380. array('unit' => $sModule, 'action' => 'added'),
  381. array('unit' => $sModule, 'action' => 'completed'),
  382. array('unit' => $sModule, 'action' => 'reopened'),
  383. array('unit' => $sModule, 'action' => 'edited'),
  384. array('unit' => $sModule, 'action' => 'deleted'),
  385. )
  386. );
  387. }
  388. /**
  389. * Entry task for Timeline module
  390. */
  391. public function serviceGetTimelinePost($aEvent, $aBrowseParams = array())
  392. {
  393. $CNF = &$this->_oConfig->CNF;
  394. $aResult = parent::serviceGetTimelinePost($aEvent, $aBrowseParams);
  395. if(empty($aResult) || !is_array($aResult) || empty($aResult['date']))
  396. return $aResult;
  397. $aContentInfo = $this->_oDb->getContentInfoById($aEvent['object_id']);
  398. if($aContentInfo[$CNF['FIELD_PUBLISHED']] > $aResult['date'])
  399. $aResult['date'] = $aContentInfo[$CNF['FIELD_PUBLISHED']];
  400. return $aResult;
  401. }
  402. public function serviceGetTimelineCompleted($aEvent, $aBrowseParams = array())
  403. {
  404. $CNF = &$this->_oConfig->CNF;
  405. $aResult = parent::serviceGetTimelinePost($aEvent, $aBrowseParams);
  406. if(empty($aResult) || !is_array($aResult) || empty($aResult['date']))
  407. return $aResult;
  408. $aContentInfo = $this->_oDb->getContentInfoById($aEvent['object_id']);
  409. if($aContentInfo[$CNF['FIELD_PUBLISHED']] > $aResult['date'])
  410. $aResult['date'] = $aContentInfo[$CNF['FIELD_PUBLISHED']];
  411. $aResult['sample_action'] = $aResult['content']['sample_action'] = _t('_bx_tasks_txt_action_completed');
  412. return $aResult;
  413. }
  414. public function serviceGetTimelineReopened($aEvent, $aBrowseParams = array())
  415. {
  416. $CNF = &$this->_oConfig->CNF;
  417. $aResult = parent::serviceGetTimelinePost($aEvent, $aBrowseParams);
  418. if(empty($aResult) || !is_array($aResult) || empty($aResult['date']))
  419. return $aResult;
  420. $aContentInfo = $this->_oDb->getContentInfoById($aEvent['object_id']);
  421. if($aContentInfo[$CNF['FIELD_PUBLISHED']] > $aResult['date'])
  422. $aResult['date'] = $aContentInfo[$CNF['FIELD_PUBLISHED']];
  423. $aResult['sample_action'] = $aResult['content']['sample_action'] = _t('_bx_tasks_txt_action_reopened');
  424. return $aResult;
  425. }
  426. public function serviceGetNotificationsData()
  427. {
  428. $sModule = $this->_aModule['name'];
  429. $sEventPrivacy = $sModule . '_allow_view_event_to';
  430. if(BxDolPrivacy::getObjectInstance($sEventPrivacy) === false)
  431. $sEventPrivacy = '';
  432. $aResult = parent::serviceGetNotificationsData();
  433. $aResult['handlers'] = array_merge($aResult['handlers'], array(
  434. 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),
  435. 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),
  436. 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),
  437. 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),
  438. array('group' => $sModule . '_assign', 'type' => 'delete', 'alert_unit' => $sModule, 'alert_action' => 'unassigned'),
  439. ));
  440. $aResult['settings'] = array_merge($aResult['settings'], array(
  441. array('group' => 'content', 'unit' => $sModule, 'action' => 'completed', 'types' => array('personal', 'follow_member', 'follow_context')),
  442. array('group' => 'content', 'unit' => $sModule, 'action' => 'reopened', 'types' => array('personal', 'follow_member', 'follow_context')),
  443. array('group' => 'content', 'unit' => $sModule, 'action' => 'expired', 'types' => array('personal')),
  444. array('group' => 'content', 'unit' => $sModule, 'action' => 'assigned', 'types' => array('personal')),
  445. ));
  446. $aResult['alerts'] = array_merge($aResult['alerts'], array(
  447. array('unit' => $sModule, 'action' => 'completed'),
  448. array('unit' => $sModule, 'action' => 'reopened'),
  449. array('unit' => $sModule, 'action' => 'expired'),
  450. array('unit' => $sModule, 'action' => 'assigned'),
  451. array('unit' => $sModule, 'action' => 'unassigned'),
  452. ));
  453. return $aResult;
  454. }
  455. public function serviceGetNotificationsCompleted($aEvent)
  456. {
  457. return $this->_serviceGetNotificationsByAction($aEvent, 'completed');
  458. }
  459. public function serviceGetNotificationsReopened($aEvent)
  460. {
  461. return $this->_serviceGetNotificationsByAction($aEvent, 'reopened');
  462. }
  463. public function serviceGetNotificationsExpired($aEvent)
  464. {
  465. return $this->_serviceGetNotificationsByAction($aEvent, 'expired');
  466. }
  467. public function serviceGetNotificationsAssigned($aEvent)
  468. {
  469. return $this->_serviceGetNotificationsByAction($aEvent, 'assigned');
  470. }
  471. protected function _serviceGetNotificationsByAction($aEvent, $sAction)
  472. {
  473. $CNF = &$this->_oConfig->CNF;
  474. $aResult = parent::serviceGetNotificationsPost($aEvent);
  475. if(empty($aResult) || !is_array($aResult))
  476. return $aResult;
  477. $aResult['entry_author'] = $aEvent['object_owner_id'];
  478. $aResult['entry_author_name'] = '';
  479. if(($oAuthor = BxDolProfile::getInstance($aResult['entry_author'])) !== false)
  480. $aResult['entry_author_name'] = $oAuthor->getDisplayName();
  481. $sLangKey = '_bx_tasks_txt_notification_' . $sAction;
  482. if((int)$aEvent['object_privacy_view'] < 0)
  483. $sLangKey .= '_in_context';
  484. $aResult['lang_key'] = _t($sLangKey);
  485. return $aResult;
  486. }
  487. public function serviceCheckAllowedManageInContext($iContextPid)
  488. {
  489. if(!$this->isAllowManageByContext($iContextPid))
  490. return false;
  491. return true;
  492. }
  493. public function serviceCheckAllowedManage($iContentId)
  494. {
  495. if(!$this->isAllowManage($iContentId))
  496. return false;
  497. return true;
  498. }
  499. public function serviceCheckAllowedComplete($iContentId)
  500. {
  501. if(!$this->serviceCheckAllowedManage($iContentId))
  502. return false;
  503. return !$this->isCompleted($iContentId);
  504. }
  505. public function serviceCheckAllowedUncomplete($iContentId)
  506. {
  507. if(!$this->serviceCheckAllowedManage($iContentId))
  508. return false;
  509. return $this->isCompleted($iContentId);
  510. }
  511. public function serviceIsCompleted($iContentId)
  512. {
  513. return $this->isCompleted($iContentId);
  514. }
  515. public function serviceIsUncompleted($iContentId)
  516. {
  517. return !$this->isCompleted($iContentId);
  518. }
  519. public function serviceIsAllowBadges($iContentId)
  520. {
  521. if (!$this->isAllowManage($iContentId))
  522. return false;
  523. if (!$this->serviceIsBadgesAvaliable())
  524. return false;
  525. return true;
  526. }
  527. public function serviceEntityAssignments($iContentId = 0, $bAsArray = false)
  528. {
  529. if(!$iContentId)
  530. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  531. if(!$iContentId)
  532. return false;
  533. $CNF = &$this->_oConfig->CNF;
  534. $aProfiles = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTION'])->getConnectedInitiators($iContentId);
  535. if($bAsArray)
  536. return $aProfiles;
  537. return $this->_oTemplate->entryAssignments($aProfiles);
  538. }
  539. public function serviceEntityTimer($iContentId = 0)
  540. {
  541. if(!$iContentId)
  542. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  543. if(!$iContentId)
  544. return false;
  545. return $this->_oTemplate->entryTimer($iContentId, $this->_iProfileId);
  546. }
  547. public function serviceCheckAllowedCommentsTask($iContentId, $sObjectComments)
  548. {
  549. $CNF = &$this->_oConfig->CNF;
  550. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  551. if($aContentInfo[$CNF['FIELD_ALLOW_COMMENTS']] == 0)
  552. return false;
  553. return parent::serviceCheckAllowedCommentsTask($iContentId, $sObjectComments);
  554. }
  555. public function serviceCheckAllowedCommentsView($iContentId, $sObjectComments)
  556. {
  557. $CNF = &$this->_oConfig->CNF;
  558. //negative id used in comments for reports
  559. if($iContentId < 0)
  560. return CHECK_ACTION_RESULT_ALLOWED;
  561. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  562. if($aContentInfo[$CNF['FIELD_ALLOW_COMMENTS']] == 0)
  563. return false;
  564. return parent::serviceCheckAllowedCommentsView($iContentId, $sObjectComments);
  565. }
  566. /**
  567. * @page service Service Calls
  568. * @section bx_tasks Tasks
  569. * @subsection bx_tasks-page_blocks Page Blocks
  570. * @subsubsection bx_tasks-calendar calendar
  571. *
  572. * @code bx_srv('bx_tasks', 'calendar', [...]); @endcode
  573. *
  574. * Shows tasks calendar baced on die date
  575. *
  576. * @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.
  577. * @param $sTemplate template to use to show calendar, or leave empty for default template, possible options: calendar.html, calendar_compact.html
  578. * @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.
  579. *
  580. * @see BxTasksModule::serviceCalendar
  581. */
  582. /**
  583. * @ref bx_tasks-calendar "calendar"
  584. */
  585. public function serviceCalendar($aData = array(), $sTemplate = 'calendar.html')
  586. {
  587. if (!$this->isAllowView($aData['context_id']))
  588. return;
  589. $o = new BxTemplCalendar(array(
  590. 'eventSources' => array (
  591. bx_append_url_params(BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'calendar_data', $aData),
  592. ),
  593. ), $this->_oTemplate);
  594. return $o->display($sTemplate);
  595. }
  596. public function serviceGetCalendarEntries($iProfileId)
  597. {
  598. $CNF = &$this->_oConfig->CNF;
  599. $oConn = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTION']);
  600. $aData = $oConn->getConnectedInitiators($iProfileId);
  601. $aData2 = array(0);
  602. foreach($aData as $iProfileId2) {
  603. $oProfile = BxDolProfile::getInstance($iProfileId2);
  604. array_push($aData2, $oProfile->getContentId());
  605. }
  606. $aSQLPart['where'] = " AND " . $CNF['TABLE_ENTRIES'] . ".`" . $CNF['FIELD_ID'] . "` IN(" . implode(',', $aData2) . ")";
  607. return $this->_oDb->getEntriesByDate(bx_get('start'), bx_get('end'), null, $aSQLPart);
  608. }
  609. public function serviceBrowseContext ($iProfileId = 0, $aParams = array())
  610. {
  611. if(!$iProfileId)
  612. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  613. if(!$iProfileId)
  614. return '';
  615. return $this->serviceBrowseTasks (-$iProfileId, $aParams);
  616. }
  617. public function serviceBrowseTasks ($iContextId = 0, $aParams = array())
  618. {
  619. $_iContextId = abs($iContextId);
  620. if(!$this->isAllowView($_iContextId))
  621. return;
  622. if(!($oProfileContext = BxDolProfile::getInstance($_iContextId)) || $oProfileContext->checkAllowedProfileView($_iContextId) !== CHECK_ACTION_RESULT_ALLOWED)
  623. return false;
  624. return $this->_oTemplate->getEntriesList($iContextId);
  625. }
  626. /**
  627. * Common methods
  628. */
  629. public function getModuleTitle($sName)
  630. {
  631. $sResult = $sName;
  632. if(($_sTitle = '_' . $sName) && ($sTitle = _t($_sTitle)) && strcmp($_sTitle, $sTitle) != 0)
  633. $sResult = $sTitle;
  634. else if(($aModule = $this->_oModule->_oDb->getModuleByName($sName)) && is_array($aModule))
  635. $sResult = $aModule['title'] ?? _t('_undefined');
  636. return $sResult;
  637. }
  638. public function getAssignees()
  639. {
  640. $aResults = [];
  641. if(($oProfile = BxDolProfile::getInstance()) !== false && ($aIds = $this->_oDb->getTimeTracks(['sample' => 'assignees'])))
  642. foreach($aIds as $iId)
  643. $aResults[] = ['key' => $iId, 'value' => $oProfile->getDisplayName($iId)];
  644. return $aResults;
  645. }
  646. public function getContexts()
  647. {
  648. $aContexts = [];
  649. $aModules = bx_srv('system', 'get_modules_by_type', ['context', ['name_as_key' => true]]);
  650. foreach($aModules as $sModule => $aModule)
  651. $aContexts[$sModule] = ($_sTitle = '_' . $sModule) && ($sTitle = _t($_sTitle)) && strcmp($_sTitle, $sTitle) != 0 ? $sTitle : $aModule['title'];
  652. return $aContexts;
  653. }
  654. public function getContextMembers($iContextPid)
  655. {
  656. $aMembers = [];
  657. if(($oContext = BxDolProfile::getInstance($iContextPid)) !== false) {
  658. $aPids = bx_srv($oContext->getModule(), 'fans', [$oContext->getContentId(), true]);
  659. foreach($aPids as $iPid)
  660. $aMembers[$iPid] = $oContext->getDisplayName($iPid);
  661. }
  662. return $aMembers;
  663. }
  664. public function getContextEntries($iContextPid)
  665. {
  666. $CNF = &$this->_oConfig->CNF;
  667. $aEntries = [];
  668. if(($aTasks = $this->_oDb->getTasks(-$iContextPid)) && is_array($aTasks))
  669. foreach($aTasks as $aTask)
  670. $aEntries[$aTask[$CNF['FIELD_ID']]] = $aTask[$CNF['FIELD_TITLE']];
  671. return $aEntries;
  672. }
  673. public function getTimer($iContentId, $iProfileId)
  674. {
  675. return $this->_oDb->getTimers(['sample' => 'content_profile_ids', 'content_id' => $iContentId, 'profile_id' => $iProfileId]);
  676. }
  677. public function getTimersByAuthor($iProfileId, $bActive = false)
  678. {
  679. return $this->_oDb->getTimers(['sample' => 'profile_id', 'profile_id' => $iProfileId, 'active' => $bActive]);
  680. }
  681. public function stopTimerByAuthor($iProfileId)
  682. {
  683. $aTimer = $this->getTimersByAuthor($iProfileId, true);
  684. if(!$aTimer || !is_array($aTimer))
  685. return false;
  686. return $this->updateTimerById($aTimer['id'], [
  687. 'started' => 0,
  688. 'duration' => $aTimer['duration'] + (time() - $aTimer['started']),
  689. ]);
  690. }
  691. public function updateTimerById($iId, $aSet)
  692. {
  693. return $this->_oDb->updateTimer($aSet, ['id' => (int)$iId]) !== false;
  694. }
  695. public function onExpired($iContentId)
  696. {
  697. $CNF = &$this->_oConfig->CNF;
  698. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  699. if(!$aContentInfo)
  700. return;
  701. $oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTION']);
  702. if($oConnection) {
  703. $aProfileIds = $oConnection->getConnectedContent($iContentId);
  704. if(!empty($aProfileIds) && is_array($aProfileIds))
  705. foreach($aProfileIds as $iProfileId){
  706. /**
  707. * @hooks
  708. * @hookdef hook-bx_tasks-expired 'bx_tasks', 'expired' - hook on task unassigned to profile
  709. * - $unit_name - equals `bx_tasks`
  710. * - $action - equals `expired`
  711. * - $object_id - task id
  712. * - $sender_id - not used
  713. * - $extra_params - array of additional params with the following array keys:
  714. * - `object_author_id` - [int] profile_id for task's author
  715. * - `privacy_view` - [string] privacy view value
  716. * @hook @ref hook-bx_tasks-expired
  717. */
  718. bx_alert($this->getName(), 'expired', $iContentId, false, array(
  719. 'object_author_id' => $iProfileId,
  720. 'privacy_view' => $aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]
  721. ));
  722. }
  723. }
  724. }
  725. public function isAllowView($iContextId)
  726. {
  727. if(!($oContext = BxDolProfile::getInstance($iContextId)) || $oContext->checkAllowedProfileView($iContextId) !== CHECK_ACTION_RESULT_ALLOWED)
  728. return false;
  729. return true;
  730. }
  731. public function isAllowAdd($iContextId)
  732. {
  733. if(!($oContext = BxDolProfile::getInstance($iContextId)) || $oContext->checkAllowedPostInProfile($iContextId) !== CHECK_ACTION_RESULT_ALLOWED)
  734. return false;
  735. return true;
  736. }
  737. public function isAllowManageByContext($iContextId)
  738. {
  739. if(isAdmin())
  740. return true;
  741. $oProfileContext = BxDolProfile::getInstance($iContextId);
  742. if(BxDolService::call($oProfileContext->getModule(), 'is_admin', array($iContextId)))
  743. return true;
  744. return false;
  745. }
  746. public function isAllowManage($mixedContent)
  747. {
  748. $CNF = &$this->_oConfig->CNF;
  749. $aContentInfo = !is_array($mixedContent) ? $this->_oDb->getContentInfoById((int)$mixedContent) : $mixedContent;
  750. if($this->checkAllowedEdit($aContentInfo) === CHECK_ACTION_RESULT_ALLOWED)
  751. return true;
  752. if($this->isAllowManageByContext(abs($aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']])))
  753. return true;
  754. if(($oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTION'])) !== false) {
  755. $iLoggedId = bx_get_logged_profile_id();
  756. $aProfileIds = $oConnection->getConnectedInitiators($aContentInfo[$CNF['FIELD_ID']]);
  757. if(!empty($aProfileIds) && is_array($aProfileIds) && in_array($iLoggedId, $aProfileIds))
  758. return true;
  759. }
  760. return false;
  761. }
  762. /**
  763. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden. So make sure to make strict(===) checking.
  764. */
  765. public function checkAllowedManage ($aDataEntry, $isPerformAction = false)
  766. {
  767. return $this->isAllowManage($aDataEntry) ? CHECK_ACTION_RESULT_ALLOWED : _t('_sys_txt_access_denied');
  768. }
  769. public function isCompleted($iContentId)
  770. {
  771. $CNF = &$this->_oConfig->CNF;
  772. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  773. return (bool)$aContentInfo[$CNF['FIELD_COMPLETED']];
  774. }
  775. public function complete($iContentId, $iCompleted)
  776. {
  777. $CNF = &$this->_oConfig->CNF;
  778. $iCompleted = (int)$iCompleted;
  779. $this->_oDb->updateEntriesBy([$CNF['FIELD_COMPLETED'] => $iCompleted], [$CNF['FIELD_ID'] => (int)$iContentId]);
  780. $sModule = $this->getName();
  781. $sAction = !$iCompleted ? 'reopened' : 'completed';
  782. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  783. $iContentAuthor = (int)$aContentInfo[$CNF['FIELD_AUTHOR']];
  784. /**
  785. * @hooks
  786. * @hookdef hook-bx_tasks-completed 'bx_tasks', 'completed' - hook on task unassigned to profile
  787. * - $unit_name - equals `bx_tasks`
  788. * - $action - can be `completed` or `reopened`
  789. * - $object_id - task id
  790. * - $sender_id - not used
  791. * - $extra_params - array of additional params with the following array keys:
  792. * - `object_author_id` - [int] profile_id for task's author
  793. * - `privacy_view` - [string] privacy view value
  794. * @hook @ref hook-bx_tasks-completed
  795. */
  796. bx_alert($sModule, $sAction, $iContentId, false, array(
  797. 'object_author_id' => $iContentAuthor,
  798. 'privacy_view' => $aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]
  799. ));
  800. if(($oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTION'])) !== false) {
  801. $aProfileIds = $oConnection->getConnectedContent($iContentId);
  802. if(!empty($aProfileIds) && is_array($aProfileIds))
  803. foreach($aProfileIds as $iProfileId) {
  804. if($iProfileId == $iContentAuthor)
  805. continue;
  806. bx_alert($sModule, $sAction, $iContentId, false, array(
  807. 'object_author_id' => $iProfileId,
  808. 'privacy_view' => $aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]
  809. ));
  810. }
  811. }
  812. $this->logActivity($iContentId, ['key' => '_bx_tasks_txt_msg_' . ($iCompleted ? '' : 'un') . 'completed']);
  813. return true;
  814. }
  815. public function logActivity($iContentId, $aMessage)
  816. {
  817. $CNF = &$this->_oConfig->CNF;
  818. $oCmts = BxDolCmts::getObjectInstance($CNF['OBJECT_COMMENTS'], $iContentId);
  819. if(!$oCmts || !$oCmts->isEnabled())
  820. return false;
  821. if(($oAuthor = BxDolProfile::getInstance()) !== false)
  822. $aMessage['markers'] = array_merge($aMessage['markers'] ?? [], [
  823. 'author_link' => $oAuthor->getUrl(),
  824. 'author_name' => $oAuthor->getDisplayName(),
  825. ]);
  826. return $oCmts->addAuto($aMessage);
  827. }
  828. /**
  829. * Internal methods
  830. */
  831. protected function _onEditProperty($aContentInfo, $sProperty, &$oForm)
  832. {
  833. $CNF = &$this->_oConfig->CNF;
  834. $iContentId = (int)$aContentInfo[$CNF['FIELD_ID']];
  835. $mixedValue = $oForm->getCleanValue($CNF['FIELD_' . strtoupper($sProperty)]);
  836. if(($sMethod = 'get' . bx_gen_method_name($sProperty) . 'Title') && method_exists($this->_oConfig, $sMethod))
  837. $mixedValue = $this->_oConfig->$sMethod($mixedValue);
  838. $this->logActivity($iContentId, ['key' => '_bx_tasks_txt_msg_edit_' . $sProperty, 'markers' => ['value' => $mixedValue]]);
  839. }
  840. protected function _onEditState($aContentInfo, &$oForm)
  841. {
  842. $CNF = &$this->_oConfig->CNF;
  843. $iContentId = (int)$aContentInfo[$CNF['FIELD_ID']];
  844. $iState = $oForm->getCleanValue($CNF['FIELD_STATE']);
  845. $this->logActivity($iContentId, ['key' => '_bx_tasks_txt_msg_edit_state', 'markers' => ['value' => $this->_oConfig->getStateTitle($iState)]]);
  846. $iCompleted = (int)$this->_oConfig->isCompleted($iState);
  847. if($iCompleted != (int)$this->_oConfig->isCompleted($aContentInfo[$CNF['FIELD_STATE']]))
  848. $this->complete($iContentId, $iCompleted);
  849. }
  850. }
  851. /** @} */