BxTasksModule.php 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080
  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. return $this->_oTemplate->getBlockMenuBrowse();
  316. }
  317. public function serviceGetBlockMenuContext($iProfileId)
  318. {
  319. $CNF = &$this->_oConfig->CNF;
  320. if(!$this->isAllowView($iProfileId))
  321. return '';
  322. $oMenu = BxDolMenu::getObjectInstance($CNF['OBJECT_MENU_SUBMENU_VIEW_CONTEXT']);
  323. if(!$oMenu)
  324. return '';
  325. $oMenu->addMarkers(['profile_id' => $iProfileId]);
  326. return $oMenu->getCode();
  327. }
  328. public function serviceGetBlockManageTime($sType = 'common', $iContextPid = 0)
  329. {
  330. $CNF = &$this->_oConfig->CNF;
  331. $bContextPid = !empty($iContextPid);
  332. $sGrid = $CNF['OBJECT_GRID_TIME_' . ($bContextPid ? 'CONTEXT_' : '') . strtoupper($sType)];
  333. $oGrid = BxDolGrid::getObjectInstance($sGrid);
  334. if(!$oGrid)
  335. return $this->_bIsApi ? [] : '';
  336. if($bContextPid)
  337. $oGrid->setContextPid($iContextPid);
  338. if($this->_bIsApi)
  339. return [
  340. bx_api_get_block('grid', $oGrid->getCodeAPI())
  341. ];
  342. list($aCssCalendar, $aJsCalendar) = BxBaseFormView::getCssJsCalendar();
  343. $this->_oTemplate->addCss(array_merge($aCssCalendar, ['manage_tools.css', 'time.css']));
  344. $this->_oTemplate->addJs(array_merge($aJsCalendar, ['modules/base/text/js/|manage_tools.js', 'time.js']));
  345. $this->_oTemplate->addJsTranslation(['_sys_grid_search']);
  346. $aResult = [
  347. 'content' => $this->_oTemplate->getJsCode('time', [
  348. 'sObjNameGrid' => $sGrid
  349. ]) . $oGrid->getCode()
  350. ];
  351. if(!$bContextPid)
  352. $aResult['menu'] = BxDolMenu::getObjectInstance($CNF['OBJECT_MENU_MANAGE_TOOLS_SUBMENU']);
  353. return $aResult;
  354. }
  355. public function serviceGetBlockTimers()
  356. {
  357. $CNF = &$this->_oConfig->CNF;
  358. return [
  359. 'content' => $this->_oTemplate->getBlockTimers($this->_iProfileId),
  360. 'menu' => BxDolMenu::getObjectInstance($CNF['OBJECT_MENU_MANAGE_TOOLS_SUBMENU'])
  361. ];
  362. }
  363. /**
  364. * Data for Timeline module
  365. */
  366. public function serviceGetTimelineData()
  367. {
  368. $sModule = $this->_aModule['name'];
  369. return array(
  370. 'handlers' => array(
  371. 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' => ''),
  372. 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' => ''),
  373. 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' => ''),
  374. array('group' => $sModule . '_object', 'type' => 'update', 'alert_unit' => $sModule, 'alert_action' => 'edited'),
  375. array('group' => $sModule . '_object', 'type' => 'delete', 'alert_unit' => $sModule, 'alert_action' => 'deleted'),
  376. ),
  377. 'alerts' => array(
  378. array('unit' => $sModule, 'action' => 'added'),
  379. array('unit' => $sModule, 'action' => 'completed'),
  380. array('unit' => $sModule, 'action' => 'reopened'),
  381. array('unit' => $sModule, 'action' => 'edited'),
  382. array('unit' => $sModule, 'action' => 'deleted'),
  383. )
  384. );
  385. }
  386. /**
  387. * Entry task for Timeline module
  388. */
  389. public function serviceGetTimelinePost($aEvent, $aBrowseParams = array())
  390. {
  391. $CNF = &$this->_oConfig->CNF;
  392. $aResult = parent::serviceGetTimelinePost($aEvent, $aBrowseParams);
  393. if(empty($aResult) || !is_array($aResult) || empty($aResult['date']))
  394. return $aResult;
  395. $aContentInfo = $this->_oDb->getContentInfoById($aEvent['object_id']);
  396. if($aContentInfo[$CNF['FIELD_PUBLISHED']] > $aResult['date'])
  397. $aResult['date'] = $aContentInfo[$CNF['FIELD_PUBLISHED']];
  398. return $aResult;
  399. }
  400. public function serviceGetTimelineCompleted($aEvent, $aBrowseParams = array())
  401. {
  402. $CNF = &$this->_oConfig->CNF;
  403. $aResult = parent::serviceGetTimelinePost($aEvent, $aBrowseParams);
  404. if(empty($aResult) || !is_array($aResult) || empty($aResult['date']))
  405. return $aResult;
  406. $aContentInfo = $this->_oDb->getContentInfoById($aEvent['object_id']);
  407. if($aContentInfo[$CNF['FIELD_PUBLISHED']] > $aResult['date'])
  408. $aResult['date'] = $aContentInfo[$CNF['FIELD_PUBLISHED']];
  409. $aResult['sample_action'] = $aResult['content']['sample_action'] = _t('_bx_tasks_txt_action_completed');
  410. return $aResult;
  411. }
  412. public function serviceGetTimelineReopened($aEvent, $aBrowseParams = array())
  413. {
  414. $CNF = &$this->_oConfig->CNF;
  415. $aResult = parent::serviceGetTimelinePost($aEvent, $aBrowseParams);
  416. if(empty($aResult) || !is_array($aResult) || empty($aResult['date']))
  417. return $aResult;
  418. $aContentInfo = $this->_oDb->getContentInfoById($aEvent['object_id']);
  419. if($aContentInfo[$CNF['FIELD_PUBLISHED']] > $aResult['date'])
  420. $aResult['date'] = $aContentInfo[$CNF['FIELD_PUBLISHED']];
  421. $aResult['sample_action'] = $aResult['content']['sample_action'] = _t('_bx_tasks_txt_action_reopened');
  422. return $aResult;
  423. }
  424. public function serviceGetNotificationsData()
  425. {
  426. $sModule = $this->_aModule['name'];
  427. $sEventPrivacy = $sModule . '_allow_view_event_to';
  428. if(BxDolPrivacy::getObjectInstance($sEventPrivacy) === false)
  429. $sEventPrivacy = '';
  430. $aResult = parent::serviceGetNotificationsData();
  431. $aResult['handlers'] = array_merge($aResult['handlers'], array(
  432. 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),
  433. 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),
  434. 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),
  435. 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),
  436. array('group' => $sModule . '_assign', 'type' => 'delete', 'alert_unit' => $sModule, 'alert_action' => 'unassigned'),
  437. ));
  438. $aResult['settings'] = array_merge($aResult['settings'], array(
  439. array('group' => 'content', 'unit' => $sModule, 'action' => 'completed', 'types' => array('personal', 'follow_member', 'follow_context')),
  440. array('group' => 'content', 'unit' => $sModule, 'action' => 'reopened', 'types' => array('personal', 'follow_member', 'follow_context')),
  441. array('group' => 'content', 'unit' => $sModule, 'action' => 'expired', 'types' => array('personal')),
  442. array('group' => 'content', 'unit' => $sModule, 'action' => 'assigned', 'types' => array('personal')),
  443. ));
  444. $aResult['alerts'] = array_merge($aResult['alerts'], array(
  445. array('unit' => $sModule, 'action' => 'completed'),
  446. array('unit' => $sModule, 'action' => 'reopened'),
  447. array('unit' => $sModule, 'action' => 'expired'),
  448. array('unit' => $sModule, 'action' => 'assigned'),
  449. array('unit' => $sModule, 'action' => 'unassigned'),
  450. ));
  451. return $aResult;
  452. }
  453. public function serviceGetNotificationsCompleted($aEvent)
  454. {
  455. return $this->_serviceGetNotificationsByAction($aEvent, 'completed');
  456. }
  457. public function serviceGetNotificationsReopened($aEvent)
  458. {
  459. return $this->_serviceGetNotificationsByAction($aEvent, 'reopened');
  460. }
  461. public function serviceGetNotificationsExpired($aEvent)
  462. {
  463. return $this->_serviceGetNotificationsByAction($aEvent, 'expired');
  464. }
  465. public function serviceGetNotificationsAssigned($aEvent)
  466. {
  467. return $this->_serviceGetNotificationsByAction($aEvent, 'assigned');
  468. }
  469. protected function _serviceGetNotificationsByAction($aEvent, $sAction)
  470. {
  471. $CNF = &$this->_oConfig->CNF;
  472. $aResult = parent::serviceGetNotificationsPost($aEvent);
  473. if(empty($aResult) || !is_array($aResult))
  474. return $aResult;
  475. $aResult['entry_author'] = $aEvent['object_owner_id'];
  476. $aResult['entry_author_name'] = '';
  477. if(($oAuthor = BxDolProfile::getInstance($aResult['entry_author'])) !== false)
  478. $aResult['entry_author_name'] = $oAuthor->getDisplayName();
  479. $sLangKey = '_bx_tasks_txt_notification_' . $sAction;
  480. if((int)$aEvent['object_privacy_view'] < 0)
  481. $sLangKey .= '_in_context';
  482. $aResult['lang_key'] = _t($sLangKey);
  483. return $aResult;
  484. }
  485. public function serviceCheckAllowedManageInContext($iContextPid)
  486. {
  487. if(!$this->isAllowManageByContext($iContextPid))
  488. return false;
  489. return true;
  490. }
  491. public function serviceCheckAllowedManage($iContentId)
  492. {
  493. if(!$this->isAllowManage($iContentId))
  494. return false;
  495. return true;
  496. }
  497. public function serviceCheckAllowedComplete($iContentId)
  498. {
  499. if(!$this->serviceCheckAllowedManage($iContentId))
  500. return false;
  501. return !$this->isCompleted($iContentId);
  502. }
  503. public function serviceCheckAllowedUncomplete($iContentId)
  504. {
  505. if(!$this->serviceCheckAllowedManage($iContentId))
  506. return false;
  507. return $this->isCompleted($iContentId);
  508. }
  509. public function serviceIsCompleted($iContentId)
  510. {
  511. return $this->isCompleted($iContentId);
  512. }
  513. public function serviceIsUncompleted($iContentId)
  514. {
  515. return !$this->isCompleted($iContentId);
  516. }
  517. public function serviceIsAllowBadges($iContentId)
  518. {
  519. if (!$this->isAllowManage($iContentId))
  520. return false;
  521. if (!$this->serviceIsBadgesAvaliable())
  522. return false;
  523. return true;
  524. }
  525. public function serviceEntityAssignments($iContentId = 0, $bAsArray = false)
  526. {
  527. if(!$iContentId)
  528. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  529. if(!$iContentId)
  530. return false;
  531. $CNF = &$this->_oConfig->CNF;
  532. $aProfiles = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTION'])->getConnectedInitiators($iContentId);
  533. if($bAsArray)
  534. return $aProfiles;
  535. return $this->_oTemplate->entryAssignments($aProfiles);
  536. }
  537. public function serviceEntityTimer($iContentId = 0)
  538. {
  539. if(!$iContentId)
  540. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  541. if(!$iContentId)
  542. return false;
  543. return $this->_oTemplate->entryTimer($iContentId, $this->_iProfileId);
  544. }
  545. public function serviceCheckAllowedCommentsTask($iContentId, $sObjectComments)
  546. {
  547. $CNF = &$this->_oConfig->CNF;
  548. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  549. if($aContentInfo[$CNF['FIELD_ALLOW_COMMENTS']] == 0)
  550. return false;
  551. return parent::serviceCheckAllowedCommentsTask($iContentId, $sObjectComments);
  552. }
  553. public function serviceCheckAllowedCommentsView($iContentId, $sObjectComments)
  554. {
  555. $CNF = &$this->_oConfig->CNF;
  556. //negative id used in comments for reports
  557. if($iContentId < 0)
  558. return CHECK_ACTION_RESULT_ALLOWED;
  559. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  560. if($aContentInfo[$CNF['FIELD_ALLOW_COMMENTS']] == 0)
  561. return false;
  562. return parent::serviceCheckAllowedCommentsView($iContentId, $sObjectComments);
  563. }
  564. /**
  565. * @page service Service Calls
  566. * @section bx_tasks Tasks
  567. * @subsection bx_tasks-page_blocks Page Blocks
  568. * @subsubsection bx_tasks-calendar calendar
  569. *
  570. * @code bx_srv('bx_tasks', 'calendar', [...]); @endcode
  571. *
  572. * Shows tasks calendar baced on die date
  573. *
  574. * @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.
  575. * @param $sTemplate template to use to show calendar, or leave empty for default template, possible options: calendar.html, calendar_compact.html
  576. * @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.
  577. *
  578. * @see BxTasksModule::serviceCalendar
  579. */
  580. /**
  581. * @ref bx_tasks-calendar "calendar"
  582. */
  583. public function serviceCalendar($aData = array(), $sTemplate = 'calendar.html')
  584. {
  585. if (!$this->isAllowView($aData['context_id']))
  586. return;
  587. $o = new BxTemplCalendar(array(
  588. 'eventSources' => array (
  589. bx_append_url_params(BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'calendar_data', $aData),
  590. ),
  591. ), $this->_oTemplate);
  592. return $o->display($sTemplate);
  593. }
  594. public function serviceGetCalendarEntries($iProfileId)
  595. {
  596. $CNF = &$this->_oConfig->CNF;
  597. $oConn = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTION']);
  598. $aData = $oConn->getConnectedInitiators($iProfileId);
  599. $aData2 = array(0);
  600. foreach($aData as $iProfileId2) {
  601. $oProfile = BxDolProfile::getInstance($iProfileId2);
  602. array_push($aData2, $oProfile->getContentId());
  603. }
  604. $aSQLPart['where'] = " AND " . $CNF['TABLE_ENTRIES'] . ".`" . $CNF['FIELD_ID'] . "` IN(" . implode(',', $aData2) . ")";
  605. return $this->_oDb->getEntriesByDate(bx_get('start'), bx_get('end'), null, $aSQLPart);
  606. }
  607. public function serviceBrowseHome($aParams = [])
  608. {
  609. $iContextPid = bx_process_input(bx_get('context_pid'), BX_DATA_INT);
  610. if(!$iContextPid)
  611. return MsgBox(_t('_Empty'));
  612. $sContent = $this->serviceBrowseTasks(-$iContextPid, array_merge($aParams, [
  613. 'for_profile' => bx_get_logged_profile_id()
  614. ]));
  615. if($sContent && ($oProfile = BxDolProfile::getInstance($iContextPid)) !== false)
  616. return [
  617. 'title' => bx_replace_markers(_t('_bx_tasks_page_block_title_entries_in_context'), [
  618. 'display_name' => $oProfile->getDisplayName(),
  619. 'profile_link' => $oProfile->getUrl()
  620. ]),
  621. 'content' => $sContent
  622. ];
  623. return $sContent;
  624. }
  625. public function serviceBrowseContext($iProfileId = 0, $aParams = [])
  626. {
  627. if(!$iProfileId)
  628. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  629. if(!$iProfileId)
  630. return '';
  631. return $this->serviceBrowseTasks (-$iProfileId, $aParams);
  632. }
  633. public function serviceBrowseTasks($iContextId = 0, $aParams = [])
  634. {
  635. if(!$this->isAllowView(abs($iContextId)))
  636. return '';
  637. return $this->_oTemplate->getEntriesList($iContextId, $aParams);
  638. }
  639. /**
  640. * Common methods
  641. */
  642. public function getModuleTitle($sName)
  643. {
  644. $sResult = $sName;
  645. if(($_sTitle = '_' . $sName) && ($sTitle = _t($_sTitle)) && strcmp($_sTitle, $sTitle) != 0)
  646. $sResult = $sTitle;
  647. else if(($aModule = $this->_oModule->_oDb->getModuleByName($sName)) && is_array($aModule))
  648. $sResult = $aModule['title'] ?? _t('_undefined');
  649. return $sResult;
  650. }
  651. public function getAssignees()
  652. {
  653. $aResults = [];
  654. if(($oProfile = BxDolProfile::getInstance()) !== false && ($aIds = $this->_oDb->getTimeTracks(['sample' => 'assignees'])))
  655. foreach($aIds as $iId)
  656. $aResults[] = ['key' => $iId, 'value' => $oProfile->getDisplayName($iId)];
  657. return $aResults;
  658. }
  659. public function getContexts()
  660. {
  661. $aContexts = [];
  662. $aModules = bx_srv('system', 'get_modules_by_type', ['context', ['name_as_key' => true]]);
  663. foreach($aModules as $sModule => $aModule)
  664. $aContexts[$sModule] = ($_sTitle = '_' . $sModule) && ($sTitle = _t($_sTitle)) && strcmp($_sTitle, $sTitle) != 0 ? $sTitle : $aModule['title'];
  665. return $aContexts;
  666. }
  667. public function getContextMembers($iContextPid)
  668. {
  669. $aMembers = [];
  670. if(($oContext = BxDolProfile::getInstance($iContextPid)) !== false) {
  671. $aPids = bx_srv($oContext->getModule(), 'fans', [$oContext->getContentId(), true]);
  672. foreach($aPids as $iPid)
  673. $aMembers[$iPid] = $oContext->getDisplayName($iPid);
  674. }
  675. return $aMembers;
  676. }
  677. public function getContextEntries($iContextPid)
  678. {
  679. $CNF = &$this->_oConfig->CNF;
  680. $aEntries = [];
  681. if(($aTasks = $this->_oDb->getTasks(-$iContextPid)) && is_array($aTasks))
  682. foreach($aTasks as $aTask)
  683. $aEntries[$aTask[$CNF['FIELD_ID']]] = $aTask[$CNF['FIELD_TITLE']];
  684. return $aEntries;
  685. }
  686. public function getTimer($iContentId, $iProfileId)
  687. {
  688. return $this->_oDb->getTimers(['sample' => 'content_profile_ids', 'content_id' => $iContentId, 'profile_id' => $iProfileId]);
  689. }
  690. public function getTimersByAuthor($iProfileId, $bActive = false)
  691. {
  692. return $this->_oDb->getTimers(['sample' => 'profile_id', 'profile_id' => $iProfileId, 'active' => $bActive]);
  693. }
  694. public function stopTimerByAuthor($iProfileId)
  695. {
  696. $aTimer = $this->getTimersByAuthor($iProfileId, true);
  697. if(!$aTimer || !is_array($aTimer))
  698. return false;
  699. return $this->updateTimerById($aTimer['id'], [
  700. 'started' => 0,
  701. 'duration' => $aTimer['duration'] + (time() - $aTimer['started']),
  702. ]);
  703. }
  704. public function updateTimerById($iId, $aSet)
  705. {
  706. return $this->_oDb->updateTimer($aSet, ['id' => (int)$iId]) !== false;
  707. }
  708. public function onExpired($iContentId)
  709. {
  710. $CNF = &$this->_oConfig->CNF;
  711. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  712. if(!$aContentInfo)
  713. return;
  714. $oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTION']);
  715. if($oConnection) {
  716. $aProfileIds = $oConnection->getConnectedContent($iContentId);
  717. if(!empty($aProfileIds) && is_array($aProfileIds))
  718. foreach($aProfileIds as $iProfileId){
  719. /**
  720. * @hooks
  721. * @hookdef hook-bx_tasks-expired 'bx_tasks', 'expired' - hook on task unassigned to profile
  722. * - $unit_name - equals `bx_tasks`
  723. * - $action - equals `expired`
  724. * - $object_id - task id
  725. * - $sender_id - not used
  726. * - $extra_params - array of additional params with the following array keys:
  727. * - `object_author_id` - [int] profile_id for task's author
  728. * - `privacy_view` - [string] privacy view value
  729. * @hook @ref hook-bx_tasks-expired
  730. */
  731. bx_alert($this->getName(), 'expired', $iContentId, false, array(
  732. 'object_author_id' => $iProfileId,
  733. 'privacy_view' => $aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]
  734. ));
  735. }
  736. }
  737. }
  738. public function isAllowView($iContextId)
  739. {
  740. if(!($oContext = BxDolProfile::getInstance($iContextId)) || $oContext->checkAllowedProfileView($iContextId) !== CHECK_ACTION_RESULT_ALLOWED)
  741. return false;
  742. return true;
  743. }
  744. public function isAllowAdd($iContextId)
  745. {
  746. if(!($oContext = BxDolProfile::getInstance($iContextId)) || $oContext->checkAllowedPostInProfile($iContextId) !== CHECK_ACTION_RESULT_ALLOWED)
  747. return false;
  748. return true;
  749. }
  750. public function isAllowManageByContext($iContextId)
  751. {
  752. if(isAdmin())
  753. return true;
  754. $oProfileContext = BxDolProfile::getInstance($iContextId);
  755. if(BxDolService::call($oProfileContext->getModule(), 'is_admin', array($iContextId)))
  756. return true;
  757. return false;
  758. }
  759. public function isAllowManage($mixedContent)
  760. {
  761. $CNF = &$this->_oConfig->CNF;
  762. $aContentInfo = !is_array($mixedContent) ? $this->_oDb->getContentInfoById((int)$mixedContent) : $mixedContent;
  763. if($this->checkAllowedEdit($aContentInfo) === CHECK_ACTION_RESULT_ALLOWED)
  764. return true;
  765. if($this->isAllowManageByContext(abs($aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']])))
  766. return true;
  767. if(($oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTION'])) !== false) {
  768. $iLoggedId = bx_get_logged_profile_id();
  769. $aProfileIds = $oConnection->getConnectedInitiators($aContentInfo[$CNF['FIELD_ID']]);
  770. if(!empty($aProfileIds) && is_array($aProfileIds) && in_array($iLoggedId, $aProfileIds))
  771. return true;
  772. }
  773. return false;
  774. }
  775. /**
  776. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden. So make sure to make strict(===) checking.
  777. */
  778. public function checkAllowedManage ($aDataEntry, $isPerformAction = false)
  779. {
  780. return $this->isAllowManage($aDataEntry) ? CHECK_ACTION_RESULT_ALLOWED : _t('_sys_txt_access_denied');
  781. }
  782. public function isCompleted($iContentId)
  783. {
  784. $CNF = &$this->_oConfig->CNF;
  785. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  786. return (bool)$aContentInfo[$CNF['FIELD_COMPLETED']];
  787. }
  788. public function complete($iContentId, $iCompleted)
  789. {
  790. $CNF = &$this->_oConfig->CNF;
  791. $iCompleted = (int)$iCompleted;
  792. $this->_oDb->updateEntriesBy([$CNF['FIELD_COMPLETED'] => $iCompleted], [$CNF['FIELD_ID'] => (int)$iContentId]);
  793. $sModule = $this->getName();
  794. $sAction = !$iCompleted ? 'reopened' : 'completed';
  795. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  796. $iContentAuthor = (int)$aContentInfo[$CNF['FIELD_AUTHOR']];
  797. /**
  798. * @hooks
  799. * @hookdef hook-bx_tasks-completed 'bx_tasks', 'completed' - hook on task unassigned to profile
  800. * - $unit_name - equals `bx_tasks`
  801. * - $action - can be `completed` or `reopened`
  802. * - $object_id - task id
  803. * - $sender_id - not used
  804. * - $extra_params - array of additional params with the following array keys:
  805. * - `object_author_id` - [int] profile_id for task's author
  806. * - `privacy_view` - [string] privacy view value
  807. * @hook @ref hook-bx_tasks-completed
  808. */
  809. bx_alert($sModule, $sAction, $iContentId, false, array(
  810. 'object_author_id' => $iContentAuthor,
  811. 'privacy_view' => $aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]
  812. ));
  813. if(($oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTION'])) !== false) {
  814. $aProfileIds = $oConnection->getConnectedContent($iContentId);
  815. if(!empty($aProfileIds) && is_array($aProfileIds))
  816. foreach($aProfileIds as $iProfileId) {
  817. if($iProfileId == $iContentAuthor)
  818. continue;
  819. bx_alert($sModule, $sAction, $iContentId, false, array(
  820. 'object_author_id' => $iProfileId,
  821. 'privacy_view' => $aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]
  822. ));
  823. }
  824. }
  825. $this->logActivity($iContentId, ['key' => '_bx_tasks_txt_msg_' . ($iCompleted ? '' : 'un') . 'completed']);
  826. return true;
  827. }
  828. public function logActivity($iContentId, $aMessage)
  829. {
  830. $CNF = &$this->_oConfig->CNF;
  831. $oCmts = BxDolCmts::getObjectInstance($CNF['OBJECT_COMMENTS'], $iContentId);
  832. if(!$oCmts || !$oCmts->isEnabled())
  833. return false;
  834. if(($oAuthor = BxDolProfile::getInstance()) !== false)
  835. $aMessage['markers'] = array_merge($aMessage['markers'] ?? [], [
  836. 'author_link' => $oAuthor->getUrl(),
  837. 'author_name' => $oAuthor->getDisplayName(),
  838. ]);
  839. return $oCmts->addAuto($aMessage);
  840. }
  841. /**
  842. * Internal methods
  843. */
  844. protected function _onEditProperty($aContentInfo, $sProperty, &$oForm)
  845. {
  846. $CNF = &$this->_oConfig->CNF;
  847. $iContentId = (int)$aContentInfo[$CNF['FIELD_ID']];
  848. $mixedValue = $oForm->getCleanValue($CNF['FIELD_' . strtoupper($sProperty)]);
  849. if(($sMethod = 'get' . bx_gen_method_name($sProperty) . 'Title') && method_exists($this->_oConfig, $sMethod))
  850. $mixedValue = $this->_oConfig->$sMethod($mixedValue);
  851. $this->logActivity($iContentId, ['key' => '_bx_tasks_txt_msg_edit_' . $sProperty, 'markers' => ['value' => $mixedValue]]);
  852. }
  853. protected function _onEditState($aContentInfo, &$oForm)
  854. {
  855. $CNF = &$this->_oConfig->CNF;
  856. $iContentId = (int)$aContentInfo[$CNF['FIELD_ID']];
  857. $iState = $oForm->getCleanValue($CNF['FIELD_STATE']);
  858. $this->logActivity($iContentId, ['key' => '_bx_tasks_txt_msg_edit_state', 'markers' => ['value' => $this->_oConfig->getStateTitle($iState)]]);
  859. $iCompleted = (int)$this->_oConfig->isCompleted($iState);
  860. if($iCompleted != (int)$this->_oConfig->isCompleted($aContentInfo[$CNF['FIELD_STATE']]))
  861. $this->complete($iContentId, $iCompleted);
  862. }
  863. }
  864. /** @} */