BxTasksModule.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  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 ()
  38. {
  39. $sTerm = bx_get('term');
  40. $a = BxDolService::call('system', 'profiles_search', array($sTerm), 'TemplServiceProfiles');
  41. header('Content-Type:text/javascript; charset=utf-8');
  42. echo(json_encode($a));
  43. }
  44. public function actionSetCompleted($iContentId, $iValue)
  45. {
  46. if (!$this->isAllowManage($iContentId))
  47. return;
  48. $CNF = &$this->_oConfig->CNF;
  49. $this->_oDb->updateEntriesBy(array($CNF['FIELD_COMPLETED'] => $iValue), array($CNF['FIELD_ID'] => (int)$iContentId));
  50. $sActionName = 'completed';
  51. if($iValue == '0')
  52. $sActionName = 'reopened';
  53. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  54. $iContentAuthor = (int)$aContentInfo[$CNF['FIELD_AUTHOR']];
  55. /**
  56. * @hooks
  57. * @hookdef hook-bx_tasks-completed 'bx_tasks', 'completed' - hook on task unassigned to profile
  58. * - $unit_name - equals `bx_tasks`
  59. * - $action - can be `completed` or `reopened`
  60. * - $object_id - task id
  61. * - $sender_id - not used
  62. * - $extra_params - array of additional params with the following array keys:
  63. * - `object_author_id` - [int] profile_id for task's author
  64. * - `privacy_view` - [string] privacy view value
  65. * @hook @ref hook-bx_tasks-completed
  66. */
  67. bx_alert($this->getName(), $sActionName, $iContentId, false, array(
  68. 'object_author_id' => $iContentAuthor,
  69. 'privacy_view' => $aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]
  70. ));
  71. $oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTION']);
  72. if($oConnection) {
  73. $aProfileIds = $oConnection->getConnectedContent($iContentId);
  74. if(!empty($aProfileIds) && is_array($aProfileIds))
  75. foreach($aProfileIds as $iProfileId) {
  76. if($iProfileId == $iContentAuthor)
  77. continue;
  78. bx_alert($this->getName(), $sActionName, $iContentId, false, array(
  79. 'object_author_id' => $iProfileId,
  80. 'privacy_view' => $aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]
  81. ));
  82. }
  83. }
  84. echo 'ok';
  85. }
  86. public function actionSetFilterValue($iListId, $sValue)
  87. {
  88. $CNF = &$this->_oConfig->CNF;
  89. $aTmp = array();
  90. if (isset($_COOKIE[$CNF['COOKIE_SETTING_KEY']]))
  91. $aTmp = json_decode($_COOKIE[$CNF['COOKIE_SETTING_KEY']], true);
  92. if ($sValue != '')
  93. $aTmp[$iListId] = $sValue;
  94. else
  95. unset($aTmp[$iListId]);
  96. bx_setcookie($CNF['COOKIE_SETTING_KEY'], json_encode($aTmp), time() + 60*60*24*365);
  97. }
  98. public function actionProcessTaskListForm($iContextId, $iId)
  99. {
  100. if (!$this->isAllowAdd(-$iContextId))
  101. return;
  102. $CNF = &$this->_oConfig->CNF;
  103. $oForm = null;
  104. $sPopupTitle = "";
  105. $aContentInfo = array();
  106. if ($iId == 0){
  107. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_LIST_ENTRY'], $CNF['OBJECT_FORM_LIST_ENTRY_DISPLAY_ADD']);
  108. $sPopupTitle = _t('_bx_tasks_form_list_entry_display_add');
  109. }
  110. else{
  111. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_LIST_ENTRY'], $CNF['OBJECT_FORM_LIST_ENTRY_DISPLAY_EDIT']);
  112. $aContentInfo = $this->_oDb->getList($iId);
  113. $sPopupTitle = _t('_bx_tasks_form_list_entry_display_edit');
  114. }
  115. $oForm->aFormAttrs['action'] = BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'process_task_list_form/' . $iContextId . '/' . $iId . '/';
  116. if (!$oForm)
  117. return '';
  118. $oForm->initChecker($aContentInfo, array());
  119. if($oForm->isSubmittedAndValid()) {
  120. if ($iId == 0){
  121. $aValsToAdd['context_id'] = $iContextId;
  122. $iId = $oForm->insert($aValsToAdd);
  123. }
  124. else{
  125. $iId = $oForm->update($iId);
  126. }
  127. return echoJson(array(
  128. 'eval' => $this->_oConfig->getJsObject('tasks') . '.reloadData(oData, ' . $iContextId . ')',
  129. ));
  130. }
  131. else {
  132. $sContent = $this->_oTemplate->parseHtmlByName('popup_form.html', array(
  133. 'form_id' => $oForm->getId(),
  134. 'form' => $oForm->getCode(true)
  135. ));
  136. if (!$oForm->isSubmitted()){
  137. echo $sContent;
  138. return;
  139. }
  140. return echoJson(array('form' => $sContent, 'form_id' => $oForm->getId()));;
  141. }
  142. }
  143. public function actionDeleteTaskList($iId, $iContextId)
  144. {
  145. if (!$this->isAllowManageByContext($iContextId))
  146. return;
  147. $CNF = &$this->_oConfig->CNF;
  148. $aTasks = $this->_oDb->getTasks($iContextId, $iId);
  149. $this->_oDb->deleteList($iId);
  150. if (!empty($aTasks) && ($oConn = BxDolConnection::getObjectInstance($this->_oConfig->CNF['OBJECT_CONNECTION']))) {
  151. foreach ($aTasks as &$aTask)
  152. $oConn->onDeleteContent($aTask[$CNF['FIELD_ID']]);
  153. }
  154. echoJson(array(
  155. 'context_id' => $iContextId,
  156. ));
  157. }
  158. public function actionProcessTaskForm($iContextId, $iListId)
  159. {
  160. if(!$this->isAllowAdd(-$iContextId))
  161. return;
  162. $CNF = &$this->_oConfig->CNF;
  163. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_ENTRY'], $CNF['OBJECT_FORM_ENTRY_DISPLAY_ADD']);
  164. $oForm->aFormAttrs['action'] = BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'process_task_form/' . $iContextId . '/' . $iListId . '/';
  165. if(!$oForm)
  166. return '';
  167. $oForm->initChecker();
  168. if($oForm->isSubmittedAndValid()) {
  169. $iContentId = $oForm->insert([$CNF['FIELD_ALLOW_VIEW_TO'] => $iContextId, $CNF['FIELD_TASKLIST'] => $iListId]);
  170. $this->onPublished($iContentId);
  171. return echoJson([
  172. 'eval' => $this->_oConfig->getJsObject('tasks') . '.reloadData(oData, ' . $iContextId . ')',
  173. ]);
  174. }
  175. else {
  176. $sContent = $this->_oTemplate->parseHtmlByName('popup_form.html', [
  177. 'form_id' => $oForm->getId(),
  178. 'form' => $oForm->getCode(true)
  179. ]);
  180. if (!$oForm->isSubmitted()) {
  181. echo $sContent;
  182. return;
  183. }
  184. return echoJson(['form' => $sContent, 'form_id' => $oForm->getId()]);
  185. }
  186. }
  187. public function actionProcessTaskFormEditState($iContentId)
  188. {
  189. $CNF = &$this->_oConfig->CNF;
  190. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  191. if(!$this->isAllowAdd(abs($aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']])))
  192. return '';
  193. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_ENTRY'], $CNF['OBJECT_FORM_ENTRY_DISPLAY_EDIT_STATE']);
  194. $oForm->aFormAttrs['action'] = BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'process_task_form_edit_state/' . $iContentId . '/';
  195. if(!$oForm)
  196. return '';
  197. $oForm->initChecker($aContentInfo);
  198. if($oForm->isSubmittedAndValid()) {
  199. $iState = $oForm->getCleanValue($CNF['FIELD_STATE']);
  200. if(!$oForm->update($iContentId, [$CNF['FIELD_COMPLETED'] => (int)$this->_oConfig->isCompleted($iState)]))
  201. return echoJson(['msg' => _t('_bx_tasks_txt_err_cannot_perform_action')]);
  202. return echoJson([
  203. 'eval' => $this->_oConfig->getJsObject('tasks') . '.reload(oData)',
  204. ]);
  205. }
  206. else {
  207. $sContent = $this->_oTemplate->parseHtmlByName('popup_form.html', [
  208. 'form_id' => $oForm->getId(),
  209. 'form' => $oForm->getCode(true)
  210. ]);
  211. if (!$oForm->isSubmitted()) {
  212. echo $sContent;
  213. return;
  214. }
  215. return echoJson(['form' => $sContent, 'form_id' => $oForm->getId()]);
  216. }
  217. }
  218. public function actionCalendarData()
  219. {
  220. // check permissions
  221. $aSQLPart = array();
  222. $iContextId = (int)bx_get('context_id');
  223. if (!$this->isAllowView($iContextId))
  224. return;
  225. $oPrivacy = BxDolPrivacy::getObjectInstance($this->_oConfig->CNF['OBJECT_PRIVACY_VIEW']);
  226. if($iContextId){
  227. $aSQLPart = $oPrivacy ? $oPrivacy->getContentByGroupAsSQLPart(- $iContextId) : array();
  228. }
  229. // get entries
  230. $aEntries = $this->_oDb->getEntriesByDate(bx_get('start'), bx_get('end'), bx_get('event'), $aSQLPart);
  231. header('Content-Type: application/json; charset=utf-8');
  232. echo json_encode($aEntries);
  233. }
  234. /**
  235. * Data for Timeline module
  236. */
  237. public function serviceGetTimelineData()
  238. {
  239. $sModule = $this->_aModule['name'];
  240. return array(
  241. 'handlers' => array(
  242. 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' => ''),
  243. 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' => ''),
  244. 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' => ''),
  245. array('group' => $sModule . '_object', 'type' => 'update', 'alert_unit' => $sModule, 'alert_action' => 'edited'),
  246. array('group' => $sModule . '_object', 'type' => 'delete', 'alert_unit' => $sModule, 'alert_action' => 'deleted'),
  247. ),
  248. 'alerts' => array(
  249. array('unit' => $sModule, 'action' => 'added'),
  250. array('unit' => $sModule, 'action' => 'completed'),
  251. array('unit' => $sModule, 'action' => 'reopened'),
  252. array('unit' => $sModule, 'action' => 'edited'),
  253. array('unit' => $sModule, 'action' => 'deleted'),
  254. )
  255. );
  256. }
  257. /**
  258. * Entry task for Timeline module
  259. */
  260. public function serviceGetTimelinePost($aEvent, $aBrowseParams = array())
  261. {
  262. $CNF = &$this->_oConfig->CNF;
  263. $aResult = parent::serviceGetTimelinePost($aEvent, $aBrowseParams);
  264. if(empty($aResult) || !is_array($aResult) || empty($aResult['date']))
  265. return $aResult;
  266. $aContentInfo = $this->_oDb->getContentInfoById($aEvent['object_id']);
  267. if($aContentInfo[$CNF['FIELD_PUBLISHED']] > $aResult['date'])
  268. $aResult['date'] = $aContentInfo[$CNF['FIELD_PUBLISHED']];
  269. return $aResult;
  270. }
  271. public function serviceGetTimelineCompleted($aEvent, $aBrowseParams = array())
  272. {
  273. $CNF = &$this->_oConfig->CNF;
  274. $aResult = parent::serviceGetTimelinePost($aEvent, $aBrowseParams);
  275. if(empty($aResult) || !is_array($aResult) || empty($aResult['date']))
  276. return $aResult;
  277. $aContentInfo = $this->_oDb->getContentInfoById($aEvent['object_id']);
  278. if($aContentInfo[$CNF['FIELD_PUBLISHED']] > $aResult['date'])
  279. $aResult['date'] = $aContentInfo[$CNF['FIELD_PUBLISHED']];
  280. $aResult['sample_action'] = $aResult['content']['sample_action'] = _t('_bx_tasks_txt_action_completed');
  281. return $aResult;
  282. }
  283. public function serviceGetTimelineReopened($aEvent, $aBrowseParams = array())
  284. {
  285. $CNF = &$this->_oConfig->CNF;
  286. $aResult = parent::serviceGetTimelinePost($aEvent, $aBrowseParams);
  287. if(empty($aResult) || !is_array($aResult) || empty($aResult['date']))
  288. return $aResult;
  289. $aContentInfo = $this->_oDb->getContentInfoById($aEvent['object_id']);
  290. if($aContentInfo[$CNF['FIELD_PUBLISHED']] > $aResult['date'])
  291. $aResult['date'] = $aContentInfo[$CNF['FIELD_PUBLISHED']];
  292. $aResult['sample_action'] = $aResult['content']['sample_action'] = _t('_bx_tasks_txt_action_reopened');
  293. return $aResult;
  294. }
  295. public function serviceGetNotificationsData()
  296. {
  297. $sModule = $this->_aModule['name'];
  298. $sEventPrivacy = $sModule . '_allow_view_event_to';
  299. if(BxDolPrivacy::getObjectInstance($sEventPrivacy) === false)
  300. $sEventPrivacy = '';
  301. $aResult = parent::serviceGetNotificationsData();
  302. $aResult['handlers'] = array_merge($aResult['handlers'], array(
  303. 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),
  304. 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),
  305. 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),
  306. 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),
  307. array('group' => $sModule . '_assign', 'type' => 'delete', 'alert_unit' => $sModule, 'alert_action' => 'unassigned'),
  308. ));
  309. $aResult['settings'] = array_merge($aResult['settings'], array(
  310. array('group' => 'content', 'unit' => $sModule, 'action' => 'completed', 'types' => array('personal', 'follow_member', 'follow_context')),
  311. array('group' => 'content', 'unit' => $sModule, 'action' => 'reopened', 'types' => array('personal', 'follow_member', 'follow_context')),
  312. array('group' => 'content', 'unit' => $sModule, 'action' => 'expired', 'types' => array('personal')),
  313. array('group' => 'content', 'unit' => $sModule, 'action' => 'assigned', 'types' => array('personal')),
  314. ));
  315. $aResult['alerts'] = array_merge($aResult['alerts'], array(
  316. array('unit' => $sModule, 'action' => 'completed'),
  317. array('unit' => $sModule, 'action' => 'reopened'),
  318. array('unit' => $sModule, 'action' => 'expired'),
  319. array('unit' => $sModule, 'action' => 'assigned'),
  320. array('unit' => $sModule, 'action' => 'unassigned'),
  321. ));
  322. return $aResult;
  323. }
  324. public function serviceGetNotificationsCompleted($aEvent)
  325. {
  326. return $this->_serviceGetNotificationsByAction($aEvent, 'completed');
  327. }
  328. public function serviceGetNotificationsReopened($aEvent)
  329. {
  330. return $this->_serviceGetNotificationsByAction($aEvent, 'reopened');
  331. }
  332. public function serviceGetNotificationsExpired($aEvent)
  333. {
  334. return $this->_serviceGetNotificationsByAction($aEvent, 'expired');
  335. }
  336. public function serviceGetNotificationsAssigned($aEvent)
  337. {
  338. return $this->_serviceGetNotificationsByAction($aEvent, 'assigned');
  339. }
  340. protected function _serviceGetNotificationsByAction($aEvent, $sAction)
  341. {
  342. $CNF = &$this->_oConfig->CNF;
  343. $aResult = parent::serviceGetNotificationsPost($aEvent);
  344. if(empty($aResult) || !is_array($aResult))
  345. return $aResult;
  346. $aResult['entry_author'] = $aEvent['object_owner_id'];
  347. $aResult['entry_author_name'] = '';
  348. if(($oAuthor = BxDolProfile::getInstance($aResult['entry_author'])) !== false)
  349. $aResult['entry_author_name'] = $oAuthor->getDisplayName();
  350. $sLangKey = '_bx_tasks_txt_notification_' . $sAction;
  351. if((int)$aEvent['object_privacy_view'] < 0)
  352. $sLangKey .= '_in_context';
  353. $aResult['lang_key'] = _t($sLangKey);
  354. return $aResult;
  355. }
  356. public function serviceCheckAllowedManage($iContentId)
  357. {
  358. if(!$this->isAllowManage($iContentId))
  359. return false;
  360. return true;
  361. }
  362. public function serviceCheckAllowedComplete($iContentId)
  363. {
  364. if(!$this->serviceCheckAllowedManage($iContentId))
  365. return false;
  366. return !$this->isCompleted($iContentId);
  367. }
  368. public function serviceCheckAllowedUncomplete($iContentId)
  369. {
  370. if(!$this->serviceCheckAllowedManage($iContentId))
  371. return false;
  372. return $this->isCompleted($iContentId);
  373. }
  374. public function serviceIsCompleted($iContentId)
  375. {
  376. return $this->isCompleted($iContentId);
  377. }
  378. public function serviceIsUncompleted($iContentId)
  379. {
  380. return !$this->isCompleted($iContentId);
  381. }
  382. public function serviceIsAllowBadges($iContentId)
  383. {
  384. if (!$this->isAllowManage($iContentId))
  385. return false;
  386. if (!$this->serviceIsBadgesAvaliable())
  387. return false;
  388. return true;
  389. }
  390. public function serviceEntityAssignments($iContentId = 0, $bAsArray = false)
  391. {
  392. if(!$iContentId)
  393. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  394. if(!$iContentId)
  395. return false;
  396. $CNF = &$this->_oConfig->CNF;
  397. $aProfiles = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTION'])->getConnectedInitiators($iContentId);
  398. if($bAsArray)
  399. return $aProfiles;
  400. $sResult = '';
  401. foreach ($aProfiles as $mixedProfile) {
  402. $bProfile = is_array($mixedProfile);
  403. $oProfile = BxDolProfile::getInstance($bProfile ? (int)$mixedProfile['id'] : (int)$mixedProfile);
  404. if(!$oProfile)
  405. continue;
  406. $aUnitParams = array('template' => array('name' => 'unit', 'size' => 'thumb'));
  407. if($bProfile && is_array($mixedProfile['info']))
  408. $aUnitParams['template']['vars'] = $mixedProfile['info'];
  409. $sResult .= $oProfile->getUnit(0, $aUnitParams);
  410. }
  411. if(!$sResult)
  412. $sResult = MsgBox(_t('_sys_txt_empty'));
  413. return $sResult;
  414. }
  415. public function serviceCheckAllowedCommentsTask($iContentId, $sObjectComments)
  416. {
  417. $CNF = &$this->_oConfig->CNF;
  418. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  419. if ($aContentInfo[$CNF['FIELD_ALLOW_COMMENTS']] == 0)
  420. return false;
  421. return parent::serviceCheckAllowedCommentsTask($iContentId, $sObjectComments);
  422. }
  423. public function serviceCheckAllowedCommentsView($iContentId, $sObjectComments)
  424. {
  425. $CNF = &$this->_oConfig->CNF;
  426. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  427. if ($aContentInfo[$CNF['FIELD_ALLOW_COMMENTS']] == 0)
  428. return false;
  429. return parent::serviceCheckAllowedCommentsView($iContentId, $sObjectComments);
  430. }
  431. /**
  432. * @page service Service Calls
  433. * @section bx_tasks Tasks
  434. * @subsection bx_tasks-page_blocks Page Blocks
  435. * @subsubsection bx_tasks-calendar calendar
  436. *
  437. * @code bx_srv('bx_tasks', 'calendar', [...]); @endcode
  438. *
  439. * Shows tasks calendar baced on die date
  440. *
  441. * @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.
  442. * @param $sTemplate template to use to show calendar, or leave empty for default template, possible options: calendar.html, calendar_compact.html
  443. * @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.
  444. *
  445. * @see BxTasksModule::serviceCalendar
  446. */
  447. /**
  448. * @ref bx_tasks-calendar "calendar"
  449. */
  450. public function serviceCalendar($aData = array(), $sTemplate = 'calendar.html')
  451. {
  452. if (!$this->isAllowView($aData['context_id']))
  453. return;
  454. $o = new BxTemplCalendar(array(
  455. 'eventSources' => array (
  456. bx_append_url_params(BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'calendar_data', $aData),
  457. ),
  458. ), $this->_oTemplate);
  459. return $o->display($sTemplate);
  460. }
  461. public function serviceGetCalendarEntries($iProfileId)
  462. {
  463. $CNF = &$this->_oConfig->CNF;
  464. $oConn = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTION']);
  465. $aData = $oConn->getConnectedInitiators($iProfileId);
  466. $aData2 = array(0);
  467. foreach($aData as $iProfileId2) {
  468. $oProfile = BxDolProfile::getInstance($iProfileId2);
  469. array_push($aData2, $oProfile->getContentId());
  470. }
  471. $aSQLPart['where'] = " AND " . $CNF['TABLE_ENTRIES'] . ".`" . $CNF['FIELD_ID'] . "` IN(" . implode(',', $aData2) . ")";
  472. return $this->_oDb->getEntriesByDate(bx_get('start'), bx_get('end'), null, $aSQLPart);
  473. }
  474. public function serviceBrowseContext ($iProfileId = 0, $aParams = array())
  475. {
  476. if(!$iProfileId)
  477. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  478. if(!$iProfileId)
  479. return '';
  480. return $this->serviceBrowseTasks (-$iProfileId, $aParams);
  481. }
  482. public function serviceBrowseTasks ($iContextId = 0, $aParams = array())
  483. {
  484. $_iContextId = abs($iContextId);
  485. if(!$this->isAllowView($_iContextId))
  486. return;
  487. if(!($oProfileContext = BxDolProfile::getInstance($_iContextId)) || $oProfileContext->checkAllowedProfileView($_iContextId) !== CHECK_ACTION_RESULT_ALLOWED)
  488. return false;
  489. return $this->_oTemplate->getEntriesList($iContextId);
  490. }
  491. /**
  492. * Common methods
  493. */
  494. public function onExpired($iContentId)
  495. {
  496. $CNF = &$this->_oConfig->CNF;
  497. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  498. if(!$aContentInfo)
  499. return;
  500. $oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTION']);
  501. if($oConnection) {
  502. $aProfileIds = $oConnection->getConnectedContent($iContentId);
  503. if(!empty($aProfileIds) && is_array($aProfileIds))
  504. foreach($aProfileIds as $iProfileId){
  505. /**
  506. * @hooks
  507. * @hookdef hook-bx_tasks-expired 'bx_tasks', 'expired' - hook on task unassigned to profile
  508. * - $unit_name - equals `bx_tasks`
  509. * - $action - equals `expired`
  510. * - $object_id - task id
  511. * - $sender_id - not used
  512. * - $extra_params - array of additional params with the following array keys:
  513. * - `object_author_id` - [int] profile_id for task's author
  514. * - `privacy_view` - [string] privacy view value
  515. * @hook @ref hook-bx_tasks-expired
  516. */
  517. bx_alert($this->getName(), 'expired', $iContentId, false, array(
  518. 'object_author_id' => $iProfileId,
  519. 'privacy_view' => $aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]
  520. ));
  521. }
  522. }
  523. }
  524. public function isAllowView($iContextId)
  525. {
  526. if(!($oContext = BxDolProfile::getInstance($iContextId)) || $oContext->checkAllowedProfileView($iContextId) !== CHECK_ACTION_RESULT_ALLOWED)
  527. return false;
  528. return true;
  529. }
  530. public function isAllowAdd($iContextId)
  531. {
  532. if(!($oContext = BxDolProfile::getInstance($iContextId)) || $oContext->checkAllowedPostInProfile($iContextId) !== CHECK_ACTION_RESULT_ALLOWED)
  533. return false;
  534. return true;
  535. }
  536. public function isAllowManageByContext($iContextId)
  537. {
  538. if(isAdmin())
  539. return true;
  540. $oProfileContext = BxDolProfile::getInstance($iContextId);
  541. if(BxDolService::call($oProfileContext->getModule(), 'is_admin', array($iContextId)))
  542. return true;
  543. return false;
  544. }
  545. public function isAllowManage($mixedContent)
  546. {
  547. $CNF = &$this->_oConfig->CNF;
  548. $aContentInfo = !is_array($mixedContent) ? $this->_oDb->getContentInfoById((int)$mixedContent) : $mixedContent;
  549. if($this->checkAllowedEdit($aContentInfo) === CHECK_ACTION_RESULT_ALLOWED)
  550. return true;
  551. if($this->isAllowManageByContext(abs($aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']])))
  552. return true;
  553. if(($oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTION'])) !== false) {
  554. $iLoggedId = bx_get_logged_profile_id();
  555. $aProfileIds = $oConnection->getConnectedInitiators($aContentInfo[$CNF['FIELD_ID']]);
  556. if(!empty($aProfileIds) && is_array($aProfileIds) && in_array($iLoggedId, $aProfileIds))
  557. return true;
  558. }
  559. return false;
  560. }
  561. /**
  562. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden. So make sure to make strict(===) checking.
  563. */
  564. public function checkAllowedManage ($aDataEntry, $isPerformAction = false)
  565. {
  566. return $this->isAllowManage($aDataEntry) ? CHECK_ACTION_RESULT_ALLOWED : _t('_sys_txt_access_denied');
  567. }
  568. public function isCompleted($iContentId)
  569. {
  570. $CNF = &$this->_oConfig->CNF;
  571. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  572. return (bool)$aContentInfo[$CNF['FIELD_COMPLETED']];
  573. }
  574. }
  575. /** @} */