BxTasksModule.php 28 KB

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