BxTasksModule.php 29 KB

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