BxTasksModule.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  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. public function serviceGetBlockMenuContext($iProfileId)
  235. {
  236. $CNF = &$this->_oConfig->CNF;
  237. $oMenu = BxDolMenu::getObjectInstance($CNF['OBJECT_MENU_SUBMENU_VIEW_CONTEXT']);
  238. if(!$oMenu)
  239. return '';
  240. $oMenu->addMarkers(['profile_id' => $iProfileId]);
  241. return $oMenu->getCode();
  242. }
  243. public function serviceGetBlockManageTimeContext($iContextPid)
  244. {
  245. $CNF = &$this->_oConfig->CNF;
  246. return $this->_getBlockManageTimeContext($CNF['OBJECT_GRID_TIME'], $iContextPid);
  247. }
  248. public function serviceGetBlockAdministrateTimeContext($iContextPid)
  249. {
  250. $CNF = &$this->_oConfig->CNF;
  251. return $this->_getBlockManageTimeContext($CNF['OBJECT_GRID_TIME_ADMINISTRATION'], $iContextPid);
  252. }
  253. protected function _getBlockManageTimeContext($sGridObject, $iContextPid)
  254. {
  255. $oGrid = BxDolGrid::getObjectInstance($sGridObject);
  256. if(!$oGrid)
  257. return $this->_bIsApi ? [] : '';
  258. $oGrid->setContextPid($iContextPid);
  259. if($this->_bIsApi)
  260. return [
  261. bx_api_get_block('grid', $oGrid->getCodeAPI())
  262. ];
  263. $this->_oTemplate->addCss(['manage_tools.css']);
  264. $this->_oTemplate->addJs(['modules/base/text/js/|manage_tools.js', 'manage_tools.js']);
  265. $this->_oTemplate->addJsTranslation(['_sys_grid_search']);
  266. return $this->_oTemplate->getJsCode('manage_tools', [
  267. 'sObjNameGrid' => $sGridObject
  268. ]) . $oGrid->getCode();
  269. }
  270. /**
  271. * Data for Timeline module
  272. */
  273. public function serviceGetTimelineData()
  274. {
  275. $sModule = $this->_aModule['name'];
  276. return array(
  277. 'handlers' => array(
  278. 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' => ''),
  279. 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' => ''),
  280. 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' => ''),
  281. array('group' => $sModule . '_object', 'type' => 'update', 'alert_unit' => $sModule, 'alert_action' => 'edited'),
  282. array('group' => $sModule . '_object', 'type' => 'delete', 'alert_unit' => $sModule, 'alert_action' => 'deleted'),
  283. ),
  284. 'alerts' => array(
  285. array('unit' => $sModule, 'action' => 'added'),
  286. array('unit' => $sModule, 'action' => 'completed'),
  287. array('unit' => $sModule, 'action' => 'reopened'),
  288. array('unit' => $sModule, 'action' => 'edited'),
  289. array('unit' => $sModule, 'action' => 'deleted'),
  290. )
  291. );
  292. }
  293. /**
  294. * Entry task for Timeline module
  295. */
  296. public function serviceGetTimelinePost($aEvent, $aBrowseParams = array())
  297. {
  298. $CNF = &$this->_oConfig->CNF;
  299. $aResult = parent::serviceGetTimelinePost($aEvent, $aBrowseParams);
  300. if(empty($aResult) || !is_array($aResult) || empty($aResult['date']))
  301. return $aResult;
  302. $aContentInfo = $this->_oDb->getContentInfoById($aEvent['object_id']);
  303. if($aContentInfo[$CNF['FIELD_PUBLISHED']] > $aResult['date'])
  304. $aResult['date'] = $aContentInfo[$CNF['FIELD_PUBLISHED']];
  305. return $aResult;
  306. }
  307. public function serviceGetTimelineCompleted($aEvent, $aBrowseParams = array())
  308. {
  309. $CNF = &$this->_oConfig->CNF;
  310. $aResult = parent::serviceGetTimelinePost($aEvent, $aBrowseParams);
  311. if(empty($aResult) || !is_array($aResult) || empty($aResult['date']))
  312. return $aResult;
  313. $aContentInfo = $this->_oDb->getContentInfoById($aEvent['object_id']);
  314. if($aContentInfo[$CNF['FIELD_PUBLISHED']] > $aResult['date'])
  315. $aResult['date'] = $aContentInfo[$CNF['FIELD_PUBLISHED']];
  316. $aResult['sample_action'] = $aResult['content']['sample_action'] = _t('_bx_tasks_txt_action_completed');
  317. return $aResult;
  318. }
  319. public function serviceGetTimelineReopened($aEvent, $aBrowseParams = array())
  320. {
  321. $CNF = &$this->_oConfig->CNF;
  322. $aResult = parent::serviceGetTimelinePost($aEvent, $aBrowseParams);
  323. if(empty($aResult) || !is_array($aResult) || empty($aResult['date']))
  324. return $aResult;
  325. $aContentInfo = $this->_oDb->getContentInfoById($aEvent['object_id']);
  326. if($aContentInfo[$CNF['FIELD_PUBLISHED']] > $aResult['date'])
  327. $aResult['date'] = $aContentInfo[$CNF['FIELD_PUBLISHED']];
  328. $aResult['sample_action'] = $aResult['content']['sample_action'] = _t('_bx_tasks_txt_action_reopened');
  329. return $aResult;
  330. }
  331. public function serviceGetNotificationsData()
  332. {
  333. $sModule = $this->_aModule['name'];
  334. $sEventPrivacy = $sModule . '_allow_view_event_to';
  335. if(BxDolPrivacy::getObjectInstance($sEventPrivacy) === false)
  336. $sEventPrivacy = '';
  337. $aResult = parent::serviceGetNotificationsData();
  338. $aResult['handlers'] = array_merge($aResult['handlers'], array(
  339. 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),
  340. 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),
  341. 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),
  342. 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),
  343. array('group' => $sModule . '_assign', 'type' => 'delete', 'alert_unit' => $sModule, 'alert_action' => 'unassigned'),
  344. ));
  345. $aResult['settings'] = array_merge($aResult['settings'], array(
  346. array('group' => 'content', 'unit' => $sModule, 'action' => 'completed', 'types' => array('personal', 'follow_member', 'follow_context')),
  347. array('group' => 'content', 'unit' => $sModule, 'action' => 'reopened', 'types' => array('personal', 'follow_member', 'follow_context')),
  348. array('group' => 'content', 'unit' => $sModule, 'action' => 'expired', 'types' => array('personal')),
  349. array('group' => 'content', 'unit' => $sModule, 'action' => 'assigned', 'types' => array('personal')),
  350. ));
  351. $aResult['alerts'] = array_merge($aResult['alerts'], array(
  352. array('unit' => $sModule, 'action' => 'completed'),
  353. array('unit' => $sModule, 'action' => 'reopened'),
  354. array('unit' => $sModule, 'action' => 'expired'),
  355. array('unit' => $sModule, 'action' => 'assigned'),
  356. array('unit' => $sModule, 'action' => 'unassigned'),
  357. ));
  358. return $aResult;
  359. }
  360. public function serviceGetNotificationsCompleted($aEvent)
  361. {
  362. return $this->_serviceGetNotificationsByAction($aEvent, 'completed');
  363. }
  364. public function serviceGetNotificationsReopened($aEvent)
  365. {
  366. return $this->_serviceGetNotificationsByAction($aEvent, 'reopened');
  367. }
  368. public function serviceGetNotificationsExpired($aEvent)
  369. {
  370. return $this->_serviceGetNotificationsByAction($aEvent, 'expired');
  371. }
  372. public function serviceGetNotificationsAssigned($aEvent)
  373. {
  374. return $this->_serviceGetNotificationsByAction($aEvent, 'assigned');
  375. }
  376. protected function _serviceGetNotificationsByAction($aEvent, $sAction)
  377. {
  378. $CNF = &$this->_oConfig->CNF;
  379. $aResult = parent::serviceGetNotificationsPost($aEvent);
  380. if(empty($aResult) || !is_array($aResult))
  381. return $aResult;
  382. $aResult['entry_author'] = $aEvent['object_owner_id'];
  383. $aResult['entry_author_name'] = '';
  384. if(($oAuthor = BxDolProfile::getInstance($aResult['entry_author'])) !== false)
  385. $aResult['entry_author_name'] = $oAuthor->getDisplayName();
  386. $sLangKey = '_bx_tasks_txt_notification_' . $sAction;
  387. if((int)$aEvent['object_privacy_view'] < 0)
  388. $sLangKey .= '_in_context';
  389. $aResult['lang_key'] = _t($sLangKey);
  390. return $aResult;
  391. }
  392. public function serviceCheckAllowedManageInContext($iContextPid)
  393. {
  394. if(!$this->isAllowManageByContext($iContextPid))
  395. return false;
  396. return true;
  397. }
  398. public function serviceCheckAllowedManage($iContentId)
  399. {
  400. if(!$this->isAllowManage($iContentId))
  401. return false;
  402. return true;
  403. }
  404. public function serviceCheckAllowedComplete($iContentId)
  405. {
  406. if(!$this->serviceCheckAllowedManage($iContentId))
  407. return false;
  408. return !$this->isCompleted($iContentId);
  409. }
  410. public function serviceCheckAllowedUncomplete($iContentId)
  411. {
  412. if(!$this->serviceCheckAllowedManage($iContentId))
  413. return false;
  414. return $this->isCompleted($iContentId);
  415. }
  416. public function serviceIsCompleted($iContentId)
  417. {
  418. return $this->isCompleted($iContentId);
  419. }
  420. public function serviceIsUncompleted($iContentId)
  421. {
  422. return !$this->isCompleted($iContentId);
  423. }
  424. public function serviceIsAllowBadges($iContentId)
  425. {
  426. if (!$this->isAllowManage($iContentId))
  427. return false;
  428. if (!$this->serviceIsBadgesAvaliable())
  429. return false;
  430. return true;
  431. }
  432. public function serviceEntityAssignments($iContentId = 0, $bAsArray = false)
  433. {
  434. if(!$iContentId)
  435. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  436. if(!$iContentId)
  437. return false;
  438. $CNF = &$this->_oConfig->CNF;
  439. $aProfiles = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTION'])->getConnectedInitiators($iContentId);
  440. if($bAsArray)
  441. return $aProfiles;
  442. $sResult = '';
  443. foreach ($aProfiles as $mixedProfile) {
  444. $bProfile = is_array($mixedProfile);
  445. $oProfile = BxDolProfile::getInstance($bProfile ? (int)$mixedProfile['id'] : (int)$mixedProfile);
  446. if(!$oProfile)
  447. continue;
  448. $aUnitParams = array('template' => array('name' => 'unit', 'size' => 'thumb'));
  449. if($bProfile && is_array($mixedProfile['info']))
  450. $aUnitParams['template']['vars'] = $mixedProfile['info'];
  451. $sResult .= $oProfile->getUnit(0, $aUnitParams);
  452. }
  453. if(!$sResult)
  454. $sResult = MsgBox(_t('_sys_txt_empty'));
  455. return $sResult;
  456. }
  457. public function serviceCheckAllowedCommentsTask($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::serviceCheckAllowedCommentsTask($iContentId, $sObjectComments);
  464. }
  465. public function serviceCheckAllowedCommentsView($iContentId, $sObjectComments)
  466. {
  467. $CNF = &$this->_oConfig->CNF;
  468. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  469. if ($aContentInfo[$CNF['FIELD_ALLOW_COMMENTS']] == 0)
  470. return false;
  471. return parent::serviceCheckAllowedCommentsView($iContentId, $sObjectComments);
  472. }
  473. /**
  474. * @page service Service Calls
  475. * @section bx_tasks Tasks
  476. * @subsection bx_tasks-page_blocks Page Blocks
  477. * @subsubsection bx_tasks-calendar calendar
  478. *
  479. * @code bx_srv('bx_tasks', 'calendar', [...]); @endcode
  480. *
  481. * Shows tasks calendar baced on die date
  482. *
  483. * @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.
  484. * @param $sTemplate template to use to show calendar, or leave empty for default template, possible options: calendar.html, calendar_compact.html
  485. * @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.
  486. *
  487. * @see BxTasksModule::serviceCalendar
  488. */
  489. /**
  490. * @ref bx_tasks-calendar "calendar"
  491. */
  492. public function serviceCalendar($aData = array(), $sTemplate = 'calendar.html')
  493. {
  494. if (!$this->isAllowView($aData['context_id']))
  495. return;
  496. $o = new BxTemplCalendar(array(
  497. 'eventSources' => array (
  498. bx_append_url_params(BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'calendar_data', $aData),
  499. ),
  500. ), $this->_oTemplate);
  501. return $o->display($sTemplate);
  502. }
  503. public function serviceGetCalendarEntries($iProfileId)
  504. {
  505. $CNF = &$this->_oConfig->CNF;
  506. $oConn = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTION']);
  507. $aData = $oConn->getConnectedInitiators($iProfileId);
  508. $aData2 = array(0);
  509. foreach($aData as $iProfileId2) {
  510. $oProfile = BxDolProfile::getInstance($iProfileId2);
  511. array_push($aData2, $oProfile->getContentId());
  512. }
  513. $aSQLPart['where'] = " AND " . $CNF['TABLE_ENTRIES'] . ".`" . $CNF['FIELD_ID'] . "` IN(" . implode(',', $aData2) . ")";
  514. return $this->_oDb->getEntriesByDate(bx_get('start'), bx_get('end'), null, $aSQLPart);
  515. }
  516. public function serviceBrowseContext ($iProfileId = 0, $aParams = array())
  517. {
  518. if(!$iProfileId)
  519. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  520. if(!$iProfileId)
  521. return '';
  522. return $this->serviceBrowseTasks (-$iProfileId, $aParams);
  523. }
  524. public function serviceBrowseTasks ($iContextId = 0, $aParams = array())
  525. {
  526. $_iContextId = abs($iContextId);
  527. if(!$this->isAllowView($_iContextId))
  528. return;
  529. if(!($oProfileContext = BxDolProfile::getInstance($_iContextId)) || $oProfileContext->checkAllowedProfileView($_iContextId) !== CHECK_ACTION_RESULT_ALLOWED)
  530. return false;
  531. return $this->_oTemplate->getEntriesList($iContextId);
  532. }
  533. /**
  534. * Common methods
  535. */
  536. public function onExpired($iContentId)
  537. {
  538. $CNF = &$this->_oConfig->CNF;
  539. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  540. if(!$aContentInfo)
  541. return;
  542. $oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTION']);
  543. if($oConnection) {
  544. $aProfileIds = $oConnection->getConnectedContent($iContentId);
  545. if(!empty($aProfileIds) && is_array($aProfileIds))
  546. foreach($aProfileIds as $iProfileId){
  547. /**
  548. * @hooks
  549. * @hookdef hook-bx_tasks-expired 'bx_tasks', 'expired' - hook on task unassigned to profile
  550. * - $unit_name - equals `bx_tasks`
  551. * - $action - equals `expired`
  552. * - $object_id - task id
  553. * - $sender_id - not used
  554. * - $extra_params - array of additional params with the following array keys:
  555. * - `object_author_id` - [int] profile_id for task's author
  556. * - `privacy_view` - [string] privacy view value
  557. * @hook @ref hook-bx_tasks-expired
  558. */
  559. bx_alert($this->getName(), 'expired', $iContentId, false, array(
  560. 'object_author_id' => $iProfileId,
  561. 'privacy_view' => $aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]
  562. ));
  563. }
  564. }
  565. }
  566. public function isAllowView($iContextId)
  567. {
  568. if(!($oContext = BxDolProfile::getInstance($iContextId)) || $oContext->checkAllowedProfileView($iContextId) !== CHECK_ACTION_RESULT_ALLOWED)
  569. return false;
  570. return true;
  571. }
  572. public function isAllowAdd($iContextId)
  573. {
  574. if(!($oContext = BxDolProfile::getInstance($iContextId)) || $oContext->checkAllowedPostInProfile($iContextId) !== CHECK_ACTION_RESULT_ALLOWED)
  575. return false;
  576. return true;
  577. }
  578. public function isAllowManageByContext($iContextId)
  579. {
  580. if(isAdmin())
  581. return true;
  582. $oProfileContext = BxDolProfile::getInstance($iContextId);
  583. if(BxDolService::call($oProfileContext->getModule(), 'is_admin', array($iContextId)))
  584. return true;
  585. return false;
  586. }
  587. public function isAllowManage($mixedContent)
  588. {
  589. $CNF = &$this->_oConfig->CNF;
  590. $aContentInfo = !is_array($mixedContent) ? $this->_oDb->getContentInfoById((int)$mixedContent) : $mixedContent;
  591. if($this->checkAllowedEdit($aContentInfo) === CHECK_ACTION_RESULT_ALLOWED)
  592. return true;
  593. if($this->isAllowManageByContext(abs($aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']])))
  594. return true;
  595. if(($oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTION'])) !== false) {
  596. $iLoggedId = bx_get_logged_profile_id();
  597. $aProfileIds = $oConnection->getConnectedInitiators($aContentInfo[$CNF['FIELD_ID']]);
  598. if(!empty($aProfileIds) && is_array($aProfileIds) && in_array($iLoggedId, $aProfileIds))
  599. return true;
  600. }
  601. return false;
  602. }
  603. /**
  604. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden. So make sure to make strict(===) checking.
  605. */
  606. public function checkAllowedManage ($aDataEntry, $isPerformAction = false)
  607. {
  608. return $this->isAllowManage($aDataEntry) ? CHECK_ACTION_RESULT_ALLOWED : _t('_sys_txt_access_denied');
  609. }
  610. public function isCompleted($iContentId)
  611. {
  612. $CNF = &$this->_oConfig->CNF;
  613. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  614. return (bool)$aContentInfo[$CNF['FIELD_COMPLETED']];
  615. }
  616. }
  617. /** @} */