BxTasksModule.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  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. * Tasks module
  12. */
  13. class BxTasksModule extends BxBaseModTextModule implements iBxDolCalendarService
  14. {
  15. function __construct(&$aModule)
  16. {
  17. parent::__construct($aModule);
  18. $CNF = &$this->_oConfig->CNF;
  19. $this->_aSearchableNamesExcept = array_merge($this->_aSearchableNamesExcept, array(
  20. $CNF['FIELD_PUBLISHED'],
  21. $CNF['FIELD_DISABLE_COMMENTS']
  22. ));
  23. }
  24. /**
  25. * Action methods
  26. */
  27. /**
  28. * Get possible recipients for start conversation form
  29. */
  30. public function actionAjaxGetInitialMembers ()
  31. {
  32. $sTerm = bx_get('term');
  33. $a = BxDolService::call('system', 'profiles_search', array($sTerm), 'TemplServiceProfiles');
  34. header('Content-Type:text/javascript; charset=utf-8');
  35. echo(json_encode($a));
  36. }
  37. public function actionSetCompleted($iContentId, $iValue)
  38. {
  39. if (!$this->isAllowManage($iContentId))
  40. return;
  41. $CNF = &$this->_oConfig->CNF;
  42. $this->_oDb->updateEntriesBy(array($CNF['FIELD_COMPLETED'] => $iValue), array($CNF['FIELD_ID'] => (int)$iContentId));
  43. $sActionName = 'completed';
  44. if($iValue == '0')
  45. $sActionName = 'reopened';
  46. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  47. $iContentAuthor = (int)$aContentInfo[$CNF['FIELD_AUTHOR']];
  48. bx_alert($this->getName(), $sActionName, $iContentId, false, array(
  49. 'object_author_id' => $iContentAuthor,
  50. 'privacy_view' => $aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]
  51. ));
  52. $oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTION']);
  53. if($oConnection) {
  54. $aProfileIds = $oConnection->getConnectedContent($iContentId);
  55. if(!empty($aProfileIds) && is_array($aProfileIds))
  56. foreach($aProfileIds as $iProfileId) {
  57. if($iProfileId == $iContentAuthor)
  58. continue;
  59. bx_alert($this->getName(), $sActionName, $iContentId, false, array(
  60. 'object_author_id' => $iProfileId,
  61. 'privacy_view' => $aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]
  62. ));
  63. }
  64. }
  65. echo 'ok';
  66. }
  67. public function actionSetFilterValue($iListId, $sValue)
  68. {
  69. $CNF = &$this->_oConfig->CNF;
  70. $aTmp = array();
  71. if (isset($_COOKIE[$CNF['COOKIE_SETTING_KEY']]))
  72. $aTmp = json_decode($_COOKIE[$CNF['COOKIE_SETTING_KEY']], true);
  73. $aUrl = parse_url(BX_DOL_URL_ROOT);
  74. $sPath = isset($aUrl['path']) && !empty($aUrl['path']) ? $aUrl['path'] : '/';
  75. if ($sValue != '')
  76. $aTmp[$iListId] = $sValue;
  77. else
  78. unset($aTmp[$iListId]);
  79. setcookie($CNF['COOKIE_SETTING_KEY'], json_encode($aTmp), time() + 60*60*24*365, $sPath);
  80. }
  81. public function actionProcessTaskListForm($iContextId, $iId)
  82. {
  83. if (!$this->isAllowAdd(-$iContextId))
  84. return;
  85. $CNF = &$this->_oConfig->CNF;
  86. $oForm = null;
  87. $sPopupTitle = "";
  88. $aContentInfo = array();
  89. if ($iId == 0){
  90. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_LIST_ENTRY'], $CNF['OBJECT_FORM_LIST_ENTRY_DISPLAY_ADD']);
  91. $sPopupTitle = _t('_bx_tasks_form_list_entry_display_add');
  92. }
  93. else{
  94. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_LIST_ENTRY'], $CNF['OBJECT_FORM_LIST_ENTRY_DISPLAY_EDIT']);
  95. $aContentInfo = $this->_oDb->getList($iId);
  96. $sPopupTitle = _t('_bx_tasks_form_list_entry_display_edit');
  97. }
  98. $oForm->aFormAttrs['action'] = BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'process_task_list_form/' . $iContextId . '/' . $iId . '/';
  99. if (!$oForm)
  100. return '';
  101. $oForm->initChecker($aContentInfo, array());
  102. if($oForm->isSubmittedAndValid()) {
  103. if ($iId == 0){
  104. $aValsToAdd['context_id'] = $iContextId;
  105. $iId = $oForm->insert($aValsToAdd);
  106. }
  107. else{
  108. $iId = $oForm->update($iId);
  109. }
  110. return echoJson(array(
  111. 'eval' => $this->_oConfig->getJsObject('tasks') . '.reloadData(oData, ' . $iContextId . ')',
  112. ));
  113. }
  114. else {
  115. $sContent = $this->_oTemplate->parseHtmlByName('popup_form.html', array(
  116. 'form_id' => $oForm->getId(),
  117. 'form' => $oForm->getCode(true)
  118. ));
  119. if (!$oForm->isSubmitted()){
  120. echo $sContent;
  121. return;
  122. }
  123. return echoJson(array('form' => $sContent, 'form_id' => $oForm->getId()));;
  124. }
  125. }
  126. public function actionDeleteTaskList($iId, $iContextId)
  127. {
  128. if (!$this->isAllowManageByContext($iContextId))
  129. return;
  130. $this->_oDb->deleteList($iId);
  131. echoJson(array(
  132. 'context_id' => $iContextId,
  133. ));
  134. }
  135. public function actionProcessTaskForm($iContextId, $iListId)
  136. {
  137. if (!$this->isAllowAdd(-$iContextId))
  138. return;
  139. $CNF = &$this->_oConfig->CNF;
  140. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_ENTRY'], $CNF['OBJECT_FORM_ENTRY_DISPLAY_ADD']);
  141. $oForm->aFormAttrs['action'] = BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'process_task_form/' . $iContextId . '/' . $iListId . '/';
  142. if (!$oForm)
  143. return '';
  144. $oForm->initChecker();
  145. if($oForm->isSubmittedAndValid()) {
  146. $aValsToAdd[$CNF['FIELD_ALLOW_VIEW_TO']] = $iContextId;
  147. $aValsToAdd[$CNF['FIELD_TASKLIST']] = $iListId;
  148. $iContentId = $oForm->insert($aValsToAdd);
  149. $this->onPublished($iContentId);
  150. return echoJson(array(
  151. 'eval' => $this->_oConfig->getJsObject('tasks') . '.reloadData(oData, ' . $iContextId . ')',
  152. ));
  153. }
  154. else {
  155. $sContent = $this->_oTemplate->parseHtmlByName('popup_form.html', array(
  156. 'form_id' => $oForm->getId(),
  157. 'form' => $oForm->getCode(true)
  158. ));
  159. if (!$oForm->isSubmitted()){
  160. echo $sContent;
  161. return;
  162. }
  163. return echoJson(array('form' => $sContent, 'form_id' => $oForm->getId()));;
  164. }
  165. }
  166. public function actionCalendarData()
  167. {
  168. // check permissions
  169. $aSQLPart = array();
  170. $iContextId = (int)bx_get('context_id');
  171. if (!$this->isAllowView($iContextId))
  172. return;
  173. $oPrivacy = BxDolPrivacy::getObjectInstance($this->_oConfig->CNF['OBJECT_PRIVACY_VIEW']);
  174. if($iContextId){
  175. $aSQLPart = $oPrivacy ? $oPrivacy->getContentByGroupAsSQLPart(- $iContextId) : array();
  176. }
  177. // get entries
  178. $aEntries = $this->_oDb->getEntriesByDate(bx_get('start'), bx_get('end'), bx_get('event'), $aSQLPart);
  179. header('Content-Type: application/json; charset=utf-8');
  180. echo json_encode($aEntries);
  181. }
  182. /**
  183. * Data for Timeline module
  184. */
  185. public function serviceGetTimelineData()
  186. {
  187. $sModule = $this->_aModule['name'];
  188. return array(
  189. 'handlers' => array(
  190. 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' => ''),
  191. 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' => ''),
  192. 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' => ''),
  193. array('group' => $sModule . '_object', 'type' => 'update', 'alert_unit' => $sModule, 'alert_action' => 'edited'),
  194. array('group' => $sModule . '_object', 'type' => 'delete', 'alert_unit' => $sModule, 'alert_action' => 'deleted'),
  195. ),
  196. 'alerts' => array(
  197. array('unit' => $sModule, 'action' => 'added'),
  198. array('unit' => $sModule, 'action' => 'completed'),
  199. array('unit' => $sModule, 'action' => 'reopened'),
  200. array('unit' => $sModule, 'action' => 'edited'),
  201. array('unit' => $sModule, 'action' => 'deleted'),
  202. )
  203. );
  204. }
  205. /**
  206. * Entry task for Timeline module
  207. */
  208. public function serviceGetTimelinePost($aEvent, $aBrowseParams = array())
  209. {
  210. $CNF = &$this->_oConfig->CNF;
  211. $aResult = parent::serviceGetTimelinePost($aEvent, $aBrowseParams);
  212. if(empty($aResult) || !is_array($aResult) || empty($aResult['date']))
  213. return $aResult;
  214. $aContentInfo = $this->_oDb->getContentInfoById($aEvent['object_id']);
  215. if($aContentInfo[$CNF['FIELD_PUBLISHED']] > $aResult['date'])
  216. $aResult['date'] = $aContentInfo[$CNF['FIELD_PUBLISHED']];
  217. return $aResult;
  218. }
  219. public function serviceGetTimelineCompleted($aEvent, $aBrowseParams = array())
  220. {
  221. $CNF = &$this->_oConfig->CNF;
  222. $aResult = parent::serviceGetTimelinePost($aEvent, $aBrowseParams);
  223. $aResult['sample_action'] = $aResult['content']['sample_action'] = _t('_bx_tasks_txt_action_completed');
  224. if(empty($aResult) || !is_array($aResult) || empty($aResult['date']))
  225. return $aResult;
  226. $aContentInfo = $this->_oDb->getContentInfoById($aEvent['object_id']);
  227. if($aContentInfo[$CNF['FIELD_PUBLISHED']] > $aResult['date'])
  228. $aResult['date'] = $aContentInfo[$CNF['FIELD_PUBLISHED']];
  229. return $aResult;
  230. }
  231. public function serviceGetTimelineReopened($aEvent, $aBrowseParams = array())
  232. {
  233. $CNF = &$this->_oConfig->CNF;
  234. $aResult = parent::serviceGetTimelinePost($aEvent, $aBrowseParams);
  235. $aResult['sample_action'] = $aResult['content']['sample_action'] = _t('_bx_tasks_txt_action_reopened');
  236. if(empty($aResult) || !is_array($aResult) || empty($aResult['date']))
  237. return $aResult;
  238. $aContentInfo = $this->_oDb->getContentInfoById($aEvent['object_id']);
  239. if($aContentInfo[$CNF['FIELD_PUBLISHED']] > $aResult['date'])
  240. $aResult['date'] = $aContentInfo[$CNF['FIELD_PUBLISHED']];
  241. return $aResult;
  242. }
  243. public function serviceGetNotificationsData()
  244. {
  245. $sModule = $this->_aModule['name'];
  246. $sEventPrivacy = $sModule . '_allow_view_event_to';
  247. if(BxDolPrivacy::getObjectInstance($sEventPrivacy) === false)
  248. $sEventPrivacy = '';
  249. $aResult = parent::serviceGetNotificationsData();
  250. $aResult['handlers'] = array_merge($aResult['handlers'], array(
  251. 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),
  252. 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),
  253. 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),
  254. 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),
  255. array('group' => $sModule . '_assign', 'type' => 'delete', 'alert_unit' => $sModule, 'alert_action' => 'unassigned'),
  256. ));
  257. $aResult['settings'] = array_merge($aResult['settings'], array(
  258. array('group' => 'content', 'unit' => $sModule, 'action' => 'completed', 'types' => array('personal', 'follow_member', 'follow_context')),
  259. array('group' => 'content', 'unit' => $sModule, 'action' => 'reopened', 'types' => array('personal', 'follow_member', 'follow_context')),
  260. array('group' => 'content', 'unit' => $sModule, 'action' => 'expired', 'types' => array('personal')),
  261. array('group' => 'content', 'unit' => $sModule, 'action' => 'assigned', 'types' => array('personal')),
  262. ));
  263. $aResult['alerts'] = array_merge($aResult['alerts'], array(
  264. array('unit' => $sModule, 'action' => 'completed'),
  265. array('unit' => $sModule, 'action' => 'reopened'),
  266. array('unit' => $sModule, 'action' => 'expired'),
  267. array('unit' => $sModule, 'action' => 'assigned'),
  268. array('unit' => $sModule, 'action' => 'unassigned'),
  269. ));
  270. return $aResult;
  271. }
  272. public function serviceGetNotificationsCompleted($aEvent)
  273. {
  274. return $this->_serviceGetNotificationsByAction($aEvent, 'completed');
  275. }
  276. public function serviceGetNotificationsReopened($aEvent)
  277. {
  278. return $this->_serviceGetNotificationsByAction($aEvent, 'reopened');
  279. }
  280. public function serviceGetNotificationsExpired($aEvent)
  281. {
  282. return $this->_serviceGetNotificationsByAction($aEvent, 'expired');
  283. }
  284. public function serviceGetNotificationsAssigned($aEvent)
  285. {
  286. return $this->_serviceGetNotificationsByAction($aEvent, 'assigned');
  287. }
  288. protected function _serviceGetNotificationsByAction($aEvent, $sAction)
  289. {
  290. $CNF = &$this->_oConfig->CNF;
  291. $aResult = parent::serviceGetNotificationsPost($aEvent);
  292. if(empty($aResult) || !is_array($aResult))
  293. return $aResult;
  294. $aResult['entry_author'] = $aEvent['object_owner_id'];
  295. $aResult['entry_author_name'] = '';
  296. if(($oAuthor = BxDolProfile::getInstance($aResult['entry_author'])) !== false)
  297. $aResult['entry_author_name'] = $oAuthor->getDisplayName();
  298. $sLangKey = '_bx_tasks_txt_notification_' . $sAction;
  299. if((int)$aEvent['object_privacy_view'] < 0)
  300. $sLangKey .= '_in_context';
  301. $aResult['lang_key'] = _t($sLangKey);
  302. return $aResult;
  303. }
  304. public function serviceIsCompleted($iContentId)
  305. {
  306. if (!$this->serviceIsAllowManage($iContentId))
  307. return false;
  308. $CNF = &$this->_oConfig->CNF;
  309. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  310. return $aContentInfo[$CNF['FIELD_COMPLETED']] ? false: true;
  311. }
  312. public function serviceIsUncompleted($iContentId)
  313. {
  314. if (!$this->serviceIsAllowManage($iContentId))
  315. return false;
  316. $CNF = &$this->_oConfig->CNF;
  317. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  318. return $aContentInfo[$CNF['FIELD_COMPLETED']] ? true : false;
  319. }
  320. public function serviceIsAllowManage($iContentId)
  321. {
  322. if (!$this->isAllowManage($iContentId))
  323. return false;
  324. return true;
  325. }
  326. public function serviceIsAllowBadges($iContentId)
  327. {
  328. if (!$this->isAllowManage($iContentId))
  329. return false;
  330. if (!$this->serviceIsBadgesAvaliable())
  331. return false;
  332. return true;
  333. }
  334. public function serviceEntityAssignments($iContentId = 0, $bAsArray = false)
  335. {
  336. if (!$iContentId)
  337. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  338. if (!$iContentId)
  339. return false;
  340. $mixedResult = BxDolConnection::getObjectInstance($this->_oConfig->CNF['OBJECT_CONNECTION'])->getConnectedContent($iContentId);
  341. if(!$bAsArray) {
  342. $s = '';
  343. foreach ($mixedResult as $mixedProfile) {
  344. $bProfile = is_array($mixedProfile);
  345. $oProfile = BxDolProfile::getInstance($bProfile ? (int)$mixedProfile['id'] : (int)$mixedProfile);
  346. if(!$oProfile)
  347. continue;
  348. $aUnitParams = array('template' => array('name' => 'unit', 'size' => 'thumb'));
  349. if($bProfile && is_array($mixedProfile['info']))
  350. $aUnitParams['template']['vars'] = $mixedProfile['info'];
  351. $s .= $oProfile->getUnit(0, $aUnitParams);
  352. }
  353. $mixedResult = $s;
  354. if (!$mixedResult)
  355. return MsgBox(_t('_sys_txt_empty'));
  356. }
  357. return $mixedResult;
  358. }
  359. public function serviceCheckAllowedCommentsTask($iContentId, $sObjectComments)
  360. {
  361. $CNF = &$this->_oConfig->CNF;
  362. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  363. if ($aContentInfo[$CNF['FIELD_DISABLE_COMMENTS']] == 1)
  364. return false;
  365. return parent::serviceCheckAllowedCommentsTask($iContentId, $sObjectComments);
  366. }
  367. public function serviceCheckAllowedCommentsView($iContentId, $sObjectComments)
  368. {
  369. $CNF = &$this->_oConfig->CNF;
  370. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  371. if ($aContentInfo[$CNF['FIELD_DISABLE_COMMENTS']] == 1)
  372. return false;
  373. return parent::serviceCheckAllowedCommentsView($iContentId, $sObjectComments);
  374. }
  375. /**
  376. * @page service Service Calls
  377. * @section bx_tasks Tasks
  378. * @subsection bx_tasks-page_blocks Page Blocks
  379. * @subsubsection bx_tasks-calendar calendar
  380. *
  381. * @code bx_srv('bx_tasks', 'calendar', [...]); @endcode
  382. *
  383. * Shows tasks calendar baced on die date
  384. *
  385. * @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.
  386. * @param $sTemplate template to use to show calendar, or leave empty for default template, possible options: calendar.html, calendar_compact.html
  387. * @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.
  388. *
  389. * @see BxTasksModule::serviceCalendar
  390. */
  391. /**
  392. * @ref bx_tasks-calendar "calendar"
  393. */
  394. public function serviceCalendar($aData = array(), $sTemplate = 'calendar.html')
  395. {
  396. if (!$this->isAllowView($aData['context_id']))
  397. return;
  398. $o = new BxTemplCalendar(array(
  399. 'eventSources' => array (
  400. bx_append_url_params(BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'calendar_data', $aData),
  401. ),
  402. ), $this->_oTemplate);
  403. return $o->display($sTemplate);
  404. }
  405. public function serviceGetCalendarEntries($iProfileId)
  406. {
  407. $CNF = &$this->_oConfig->CNF;
  408. $oConn = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTION']);
  409. $aData = $oConn->getConnectedInitiators($iProfileId);
  410. $aData2 = array(0);
  411. foreach($aData as $iProfileId2){
  412. $oProfile = BxDolProfile::getInstance($iProfileId2);
  413. array_push($aData2, $oProfile->getContentId());
  414. }
  415. $aSQLPart['where'] = " AND " . $CNF['TABLE_ENTRIES'] . ".`" . $CNF['FIELD_ID'] . "` IN(" . implode(',', $aData2) . ")";
  416. return $this->_oDb->getEntriesByDate(bx_get('start'), bx_get('end'), null, $aSQLPart);
  417. }
  418. public function serviceBrowseContext ($iProfileId = 0, $aParams = array())
  419. {
  420. if(!$iProfileId)
  421. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  422. if(!$iProfileId)
  423. return '';
  424. return $this->serviceBrowseTasks (-$iProfileId, $aParams);
  425. }
  426. public function serviceBrowseTasks ($iContextId = 0, $aParams = array())
  427. {
  428. if (!$this->isAllowView(-$iContextId))
  429. return;
  430. $oProfileContext = BxDolProfile::getInstance(-$iContextId);
  431. $mixedResult = $oProfileContext->checkAllowedProfileView(-$iContextId);
  432. if($mixedResult !== CHECK_ACTION_RESULT_ALLOWED)
  433. return false;
  434. $CNF = &$this->_oConfig->CNF;
  435. $this->_oTemplate->addCssJs();
  436. $aVars = array();
  437. $aLists = $this->_oDb->getLists($iContextId);
  438. $aListsVars = array();
  439. $oConn = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTION']);
  440. $aFilterValues = array();
  441. if (isset($_COOKIE[$CNF['COOKIE_SETTING_KEY']]))
  442. $aFilterValues = json_decode($_COOKIE[$CNF['COOKIE_SETTING_KEY']], true);
  443. $bAllowAdd = $this->isAllowAdd(-$iContextId);
  444. $bAllowManage = $this->isAllowManageByContext(-$iContextId);
  445. foreach($aLists as $aList) {
  446. $aTasks = $this->_oDb->getTasks($iContextId, $aList['id']);
  447. $aTasksVars = array();
  448. foreach($aTasks as $aTask) {
  449. $aMembers = $oConn->getConnectedContent($aTask[$CNF['FIELD_ID']]);
  450. $aMembersVars = array();
  451. foreach($aMembers as $iMember) {
  452. $oProfile = BxDolProfile::getInstance($iMember);
  453. $aMembersVars[] = array('info' => $oProfile->getUnit(0, array('template' => 'unit_wo_info')));
  454. }
  455. $aTasksVars[] = array(
  456. 'id' => $aTask[$CNF['FIELD_ID']],
  457. 'title' => $aTask[$CNF['FIELD_TITLE']],
  458. 'created' => bx_time_js($aTask[$CNF['FIELD_ADDED']]),
  459. 'class' => $aTask[$CNF['FIELD_COMPLETED']] == 1 ? 'completed' : 'uncompleted',
  460. 'due' => $aTask[$CNF['FIELD_DUEDATE']] > 0 ? bx_time_js($aTask[$CNF['FIELD_DUEDATE']]) : '',
  461. 'bx_repeat:members' => $aMembersVars,
  462. 'badges' => $this->serviceGetBadges($aTask[$CNF['FIELD_ID']], true),
  463. 'url' => BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $aTask[$CNF['FIELD_ID']]),
  464. 'object' => $this->_oConfig->getJsObject('tasks'),
  465. 'bx_if:allow_manage' => array(
  466. 'condition' => $bAllowManage,
  467. 'content' => array(
  468. 'id' => $aTask[$CNF['FIELD_ID']],
  469. 'object' => $this->_oConfig->getJsObject('tasks'),
  470. 'checked' => $aTask[$CNF['FIELD_COMPLETED']] == 1 ? 'checked' : '',
  471. )
  472. ),
  473. 'bx_if:deny_manage' => array(
  474. 'condition' => !$bAllowManage,
  475. 'content' => array(
  476. 'id' => $aTask[$CNF['FIELD_ID']],
  477. 'checked' => $aTask[$CNF['FIELD_COMPLETED']] == 1 ? 'checked' : '',
  478. )
  479. ),
  480. );
  481. }
  482. $sClass = $sCompleted = $sAll = "";
  483. if (isset($aFilterValues[$aList[$CNF['FIELD_ID']]])){
  484. $sClass = $aFilterValues[$aList[$CNF['FIELD_ID']]];
  485. if ($sClass == 'completed')
  486. $sCompleted= 'selected';
  487. if ($sClass == 'all')
  488. $sAll = 'selected';
  489. }
  490. $aListsVars[] = array(
  491. 'bx_if:allow_edit_list' => array(
  492. 'condition' => $bAllowAdd,
  493. 'content' => array(
  494. 'title' => $aList[$CNF['FIELD_TITLE']],
  495. 'context_id' => $iContextId,
  496. 'list_id' => $aList[$CNF['FIELD_ID']],
  497. 'object' => $this->_oConfig->getJsObject('tasks'),
  498. )
  499. ),
  500. 'bx_if:allow_add' => array(
  501. 'condition' => $bAllowAdd,
  502. 'content' => array(
  503. 'context_id' => $iContextId,
  504. 'list_id' => $aList[$CNF['FIELD_ID']],
  505. 'object' => $this->_oConfig->getJsObject('tasks'),
  506. )
  507. ),
  508. 'bx_if:allow_delete_list' => array(
  509. 'condition' => $bAllowManage,
  510. 'content' => array(
  511. 'context_id' => $iContextId,
  512. 'list_id' => $aList[$CNF['FIELD_ID']],
  513. 'object' => $this->_oConfig->getJsObject('tasks'),
  514. )
  515. ),
  516. 'bx_if:deny_edit_list' => array(
  517. 'condition' => !$bAllowAdd,
  518. 'content' => array(
  519. 'title' => $aList[$CNF['FIELD_TITLE']],
  520. )
  521. ),
  522. 'id' => $aList['id'],
  523. 'bx_repeat:tasks' => $aTasksVars,
  524. 'context_id' => $iContextId,
  525. 'list_id' => $aList[$CNF['FIELD_ID']],
  526. 'object' => $this->_oConfig->getJsObject('tasks'),
  527. 'class' => $sClass,
  528. 'completed' => $sCompleted,
  529. 'all' => $sAll,
  530. );
  531. }
  532. $aVars = array(
  533. 'bx_repeat:task_lists' => $aListsVars,
  534. 'bx_if:allow_add_list' => array(
  535. 'condition' => $bAllowAdd,
  536. 'content' => array(
  537. 'context_id' => $iContextId,
  538. 'object' => $this->_oConfig->getJsObject('tasks'),
  539. )
  540. ),
  541. );
  542. $this->_oTemplate->addJs(array(
  543. 'jquery-ui/jquery.ui.widget.min.js',
  544. 'jquery-ui/jquery.ui.menu.min.js',
  545. 'jquery-ui/jquery.ui.autocomplete.min.js',
  546. 'jquery-ui/jquery.ui.core.min.js',
  547. 'tasks.js',
  548. 'modules/base/general/js/|forms.js'
  549. ));
  550. return $this->_oTemplate->getJsCode('tasks', array('t_confirm_block_deletion' => _t('_bx_tasks_confirm_tasklist_deletion'))) . $this->_oTemplate->parseHtmlByName('browse_tasks.html', $aVars);
  551. }
  552. /**
  553. * Common methods
  554. */
  555. public function onExpired($iContentId)
  556. {
  557. $CNF = &$this->_oConfig->CNF;
  558. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  559. if(!$aContentInfo)
  560. return;
  561. $oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTION']);
  562. if($oConnection) {
  563. $aProfileIds = $oConnection->getConnectedContent($iContentId);
  564. if(!empty($aProfileIds) && is_array($aProfileIds))
  565. foreach($aProfileIds as $iProfileId)
  566. bx_alert($this->getName(), 'expired', $iContentId, false, array(
  567. 'object_author_id' => $iProfileId,
  568. 'privacy_view' => $aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]
  569. ));
  570. }
  571. }
  572. function isAllowView($iContextId)
  573. {
  574. $oProfileContext = BxDolProfile::getInstance($iContextId);
  575. $mixedResult = $oProfileContext->checkAllowedProfileView($iContextId);
  576. if($mixedResult !== CHECK_ACTION_RESULT_ALLOWED)
  577. return false;
  578. return true;
  579. }
  580. function isAllowAdd($iContextId)
  581. {
  582. $oProfileContext = BxDolProfile::getInstance($iContextId);
  583. $mixedResult = $oProfileContext->checkAllowedPostInProfile($iContextId);
  584. if($mixedResult !== CHECK_ACTION_RESULT_ALLOWED)
  585. return false;
  586. return true;
  587. }
  588. function isAllowManageByContext($iContextId)
  589. {
  590. if(isAdmin())
  591. return true;
  592. $oProfileContext = BxDolProfile::getInstance($iContextId);
  593. if(BxDolService::call($oProfileContext->getModule(), 'is_admin', array($iContextId)))
  594. return true;
  595. }
  596. function isAllowManage($iContentId)
  597. {
  598. $CNF = &$this->_oConfig->CNF;
  599. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  600. $bRv = $this->isAllowManageByContext(-$aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]);
  601. if ($bRv)
  602. return $bRv;
  603. if ($aContentInfo[$CNF['FIELD_AUTHOR']] == bx_get_logged_profile_id())
  604. return true;
  605. $oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTION']);
  606. if($oConnection) {
  607. $aProfileIds = $oConnection->getConnectedContent($iContentId);
  608. if(!empty($aProfileIds) && is_array($aProfileIds)){
  609. if (in_array(bx_get_logged_profile_id(), $aProfileIds))
  610. return true;
  611. }
  612. }
  613. return false;
  614. }
  615. }
  616. /** @} */