BxTasksModule.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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($iTaskId, $iValue)
  38. {
  39. $CNF = &$this->_oConfig->CNF;
  40. $this->_oDb->updateEntriesBy(array($CNF['FIELD_COMPLETED'] => $iValue), array($CNF['FIELD_ID'] => (int)$iTaskId));
  41. echo 'ok';
  42. }
  43. public function actionSetFilterValue($iListId, $sValue)
  44. {
  45. $CNF = &$this->_oConfig->CNF;
  46. $aTmp = array();
  47. if (isset($_COOKIE[$CNF['COOKIE_SETTING_KEY']]))
  48. $aTmp = json_decode($_COOKIE[$CNF['COOKIE_SETTING_KEY']], true);
  49. $aUrl = parse_url(BX_DOL_URL_ROOT);
  50. $sPath = isset($aUrl['path']) && !empty($aUrl['path']) ? $aUrl['path'] : '/';
  51. if ($sValue != '')
  52. $aTmp[$iListId] = $sValue;
  53. else
  54. unset($aTmp[$iListId]);
  55. setcookie($CNF['COOKIE_SETTING_KEY'], json_encode($aTmp), time() + 60*60*24*365, $sPath);
  56. }
  57. public function actionProcessTaskListForm($iContextId, $iId)
  58. {
  59. $CNF = &$this->_oConfig->CNF;
  60. $oForm = null;
  61. $sPopupTitle = "";
  62. $aContentInfo = array();
  63. if ($iId == 0){
  64. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_LIST_ENTRY'], $CNF['OBJECT_FORM_LIST_ENTRY_DISPLAY_ADD']);
  65. $sPopupTitle = _t('_bx_tasks_form_list_entry_display_add');
  66. }
  67. else{
  68. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_LIST_ENTRY'], $CNF['OBJECT_FORM_LIST_ENTRY_DISPLAY_EDIT']);
  69. $aContentInfo = $this->_oDb->getList($iId);
  70. $sPopupTitle = _t('_bx_tasks_form_list_entry_display_edit');
  71. }
  72. $oForm->aFormAttrs['action'] = BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'process_task_list_form/' . $iContextId . '/' . $iId . '/';
  73. if (!$oForm)
  74. return '';
  75. $oForm->initChecker($aContentInfo, array());
  76. if($oForm->isSubmittedAndValid()) {
  77. if ($iId == 0){
  78. $aValsToAdd['context_id'] = $iContextId;
  79. $iId = $oForm->insert($aValsToAdd);
  80. }
  81. else{
  82. $iId = $oForm->update($iId);
  83. }
  84. return echoJson(array(
  85. 'eval' => $this->_oConfig->getJsObject('tasks') . '.reloadData(oData)',
  86. ));
  87. }
  88. else {
  89. $sContent = $this->_oTemplate->parseHtmlByName('popup_form.html', array(
  90. 'form_id' => $oForm->getId(),
  91. 'form' => $oForm->getCode(true)
  92. ));
  93. if (!$oForm->isSubmitted()){
  94. echo $sContent;
  95. return;
  96. }
  97. return echoJson(array('form' => $sContent, 'form_id' => $oForm->getId()));;
  98. }
  99. }
  100. public function actionProcessTaskForm($iContextId, $iListId)
  101. {
  102. $CNF = &$this->_oConfig->CNF;
  103. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_ENTRY'], $CNF['OBJECT_FORM_ENTRY_DISPLAY_ADD']);
  104. $oForm->aFormAttrs['action'] = BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'process_task_form/' . $iContextId . '/' . $iListId . '/';
  105. if (!$oForm)
  106. return '';
  107. $oForm->initChecker();
  108. if($oForm->isSubmittedAndValid()) {
  109. $aValsToAdd[$CNF['FIELD_ALLOW_VIEW_TO']] = $iContextId;
  110. $aValsToAdd[$CNF['FIELD_TASKLIST']] = $iListId;
  111. $iId = $oForm->insert($aValsToAdd);
  112. return echoJson(array(
  113. 'eval' => $this->_oConfig->getJsObject('tasks') . '.reloadData(oData)',
  114. ));
  115. }
  116. else {
  117. $sContent = $this->_oTemplate->parseHtmlByName('popup_form.html', array(
  118. 'form_id' => $oForm->getId(),
  119. 'form' => $oForm->getCode(true)
  120. ));
  121. if (!$oForm->isSubmitted()){
  122. echo $sContent;
  123. return;
  124. }
  125. return echoJson(array('form' => $sContent, 'form_id' => $oForm->getId()));;
  126. }
  127. }
  128. public function actionCalendarData()
  129. {
  130. // check permissions
  131. $aSQLPart = array();
  132. $iContextId = (int)bx_get('context_id');
  133. $oPrivacy = BxDolPrivacy::getObjectInstance($this->_oConfig->CNF['OBJECT_PRIVACY_VIEW']);
  134. if($iContextId){
  135. $aSQLPart = $oPrivacy ? $oPrivacy->getContentByGroupAsSQLPart(- $iContextId) : array();
  136. }
  137. // get entries
  138. $aEntries = $this->_oDb->getEntriesByDate(bx_get('start'), bx_get('end'), bx_get('event'), $aSQLPart);
  139. header('Content-Type: application/json; charset=utf-8');
  140. echo json_encode($aEntries);
  141. }
  142. /**
  143. * Entry task for Timeline module
  144. */
  145. public function serviceGetTimelinePost($aEvent, $aBrowseParams = array())
  146. {
  147. $CNF = &$this->_oConfig->CNF;
  148. $aResult = parent::serviceGetTimelinePost($aEvent, $aBrowseParams);
  149. if(empty($aResult) || !is_array($aResult) || empty($aResult['date']))
  150. return $aResult;
  151. $aContentInfo = $this->_oDb->getContentInfoById($aEvent['object_id']);
  152. if($aContentInfo[$CNF['FIELD_PUBLISHED']] > $aResult['date'])
  153. $aResult['date'] = $aContentInfo[$CNF['FIELD_PUBLISHED']];
  154. return $aResult;
  155. }
  156. public function serviceIsCompleted($iContentId)
  157. {
  158. $CNF = &$this->_oConfig->CNF;
  159. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  160. return $aContentInfo[$CNF['FIELD_COMPLETED']] ? false: true;
  161. }
  162. public function serviceEntityAssignments($iContentId = 0, $bAsArray = false)
  163. {
  164. if (!$iContentId)
  165. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  166. if (!$iContentId)
  167. return false;
  168. $mixedResult = BxDolConnection::getObjectInstance($this->_oConfig->CNF['OBJECT_CONNECTION'])->getConnectedContent($iContentId);
  169. if(!$bAsArray) {
  170. $s = '';
  171. foreach ($mixedResult as $mixedProfile) {
  172. $bProfile = is_array($mixedProfile);
  173. $oProfile = BxDolProfile::getInstance($bProfile ? (int)$mixedProfile['id'] : (int)$mixedProfile);
  174. if(!$oProfile)
  175. continue;
  176. $aUnitParams = array('template' => array('name' => 'unit', 'size' => 'thumb'));
  177. if($bProfile && is_array($mixedProfile['info']))
  178. $aUnitParams['template']['vars'] = $mixedProfile['info'];
  179. $s .= $oProfile->getUnit(0, $aUnitParams);
  180. }
  181. $mixedResult = $s;
  182. if (!$mixedResult)
  183. return MsgBox(_t('_sys_txt_empty'));
  184. }
  185. return $mixedResult;
  186. }
  187. public function serviceIsUncompleted($iContentId)
  188. {
  189. $CNF = &$this->_oConfig->CNF;
  190. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  191. return $aContentInfo[$CNF['FIELD_COMPLETED']] ? true : false;
  192. }
  193. public function serviceCheckAllowedCommentsTask($iContentId, $sObjectComments)
  194. {
  195. $CNF = &$this->_oConfig->CNF;
  196. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  197. if ($aContentInfo[$CNF['FIELD_DISABLE_COMMENTS']] == 1)
  198. return false;
  199. return parent::serviceCheckAllowedCommentsTask($iContentId, $sObjectComments);
  200. }
  201. public function serviceCheckAllowedCommentsView($iContentId, $sObjectComments)
  202. {
  203. $CNF = &$this->_oConfig->CNF;
  204. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  205. if ($aContentInfo[$CNF['FIELD_DISABLE_COMMENTS']] == 1)
  206. return false;
  207. return parent::serviceCheckAllowedCommentsView($iContentId, $sObjectComments);
  208. }
  209. /**
  210. * @page service Service Calls
  211. * @section bx_tasks Tasks
  212. * @subsection bx_tasks-page_blocks Page Blocks
  213. * @subsubsection bx_tasks-calendar calendar
  214. *
  215. * @code bx_srv('bx_tasks', 'calendar', [...]); @endcode
  216. *
  217. * Shows tasks calendar baced on die date
  218. *
  219. * @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.
  220. * @param $sTemplate template to use to show calendar, or leave empty for default template, possible options: calendar.html, calendar_compact.html
  221. * @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.
  222. *
  223. * @see BxTasksModule::serviceCalendar
  224. */
  225. /**
  226. * @ref bx_tasks-calendar "calendar"
  227. */
  228. public function serviceCalendar($aData = array(), $sTemplate = 'calendar.html')
  229. {
  230. $o = new BxTemplCalendar(array(
  231. 'eventSources' => array (
  232. bx_append_url_params(BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'calendar_data', $aData),
  233. ),
  234. ), $this->_oTemplate);
  235. return $o->display($sTemplate);
  236. }
  237. public function serviceGetCalendarEntries($iProfileId)
  238. {
  239. $CNF = &$this->_oConfig->CNF;
  240. $oConn = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTION']);
  241. $aData = $oConn->getConnectedInitiators($iProfileId);
  242. $aData2 = array(0);
  243. foreach($aData as $iProfileId2){
  244. $oProfile = BxDolProfile::getInstance($iProfileId2);
  245. array_push($aData2, $oProfile->getContentId());
  246. }
  247. $aSQLPart['where'] = " AND " . $CNF['TABLE_ENTRIES'] . ".`" . $CNF['FIELD_ID'] . "` IN(" . implode(',', $aData2) . ")";
  248. return $this->_oDb->getEntriesByDate(bx_get('start'), bx_get('end'), null, $aSQLPart);
  249. }
  250. public function serviceBrowseContext ($iProfileId = 0, $aParams = array())
  251. {
  252. if(!$iProfileId)
  253. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  254. if(!$iProfileId)
  255. return '';
  256. return $this->serviceBrowseTasks (-$iProfileId, $aParams);
  257. }
  258. public function serviceBrowseTasks ($iContextId = 0, $aParams = array())
  259. {
  260. $CNF = &$this->_oConfig->CNF;
  261. $this->_oTemplate->addCssJs();
  262. $aVars = array();
  263. $aLists = $this->_oDb->getLists($iContextId);
  264. $aListsVars = array();
  265. $oConn = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTION']);
  266. $aFilterValues = array();
  267. if (isset($_COOKIE[$CNF['COOKIE_SETTING_KEY']]))
  268. $aFilterValues = json_decode($_COOKIE[$CNF['COOKIE_SETTING_KEY']], true);
  269. foreach($aLists as $aList) {
  270. $aTasks = $this->_oDb->getTasks($iContextId, $aList['id']);
  271. $aTasksVars = array();
  272. foreach($aTasks as $aTask) {
  273. $aMembers = $oConn->getConnectedContent($aTask[$CNF['FIELD_ID']]);
  274. $aMembersVars = array();
  275. foreach($aMembers as $iMember) {
  276. $oProfile = BxDolProfile::getInstance($iMember);
  277. $aMembersVars[] = array('info' => $oProfile->getUnit(0, array('template' => 'unit_wo_info')));
  278. }
  279. $aTasksVars[] = array(
  280. 'id' => $aTask[$CNF['FIELD_ID']],
  281. 'title' => $aTask[$CNF['FIELD_TITLE']],
  282. 'created' => bx_time_js($aTask[$CNF['FIELD_ADDED']]),
  283. 'checked' => $aTask[$CNF['FIELD_COMPLETED']] == 1 ? 'checked' : '',
  284. 'class' => $aTask[$CNF['FIELD_COMPLETED']] == 1 ? 'completed' : 'uncompleted',
  285. 'due' => $aTask[$CNF['FIELD_DUEDATE']] > 0 ? bx_time_js($aTask[$CNF['FIELD_DUEDATE']]) : '',
  286. 'bx_repeat:members' => $aMembersVars,
  287. 'badges' => $this->serviceGetBadges($aTask[$CNF['FIELD_ID']], true),
  288. 'url' => BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $aTask[$CNF['FIELD_ID']]),
  289. 'object' => $this->_oConfig->getJsObject('tasks')
  290. );
  291. }
  292. $sClass = $sCompleted = $sUncompleted = "";
  293. if (isset($aFilterValues[$aList[$CNF['FIELD_ID']]])){
  294. $sClass = $aFilterValues[$aList[$CNF['FIELD_ID']]];
  295. if ($sClass == 'completed')
  296. $sCompleted= 'selected';
  297. if ($sClass == 'uncompleted')
  298. $sUncompleted= 'selected';
  299. }
  300. $aListsVars[] = array(
  301. 'title' => $aList[$CNF['FIELD_TITLE']],
  302. 'id' => $aList['id'],
  303. 'bx_repeat:tasks' => $aTasksVars,
  304. 'context_id' => $iContextId,
  305. 'list_id' => $aList[$CNF['FIELD_ID']],
  306. 'object' => $this->_oConfig->getJsObject('tasks'),
  307. 'class' => $sClass,
  308. 'completed' => $sCompleted,
  309. 'uncompleted' => $sUncompleted,
  310. );
  311. }
  312. $aVars = array(
  313. 'bx_repeat:task_lists' => $aListsVars,
  314. 'context_id' => $iContextId,
  315. 'object' => $this->_oConfig->getJsObject('tasks')
  316. );
  317. $this->_oTemplate->addJs(array(
  318. 'jquery-ui/jquery.ui.widget.min.js',
  319. 'jquery-ui/jquery.ui.menu.min.js',
  320. 'jquery-ui/jquery.ui.autocomplete.min.js',
  321. 'jquery-ui/jquery.ui.core.min.js',
  322. 'tasks.js',
  323. ));
  324. return $this->_oTemplate->getJsCode('tasks') . $this->_oTemplate->parseHtmlByName('browse_tasks.html', $aVars);
  325. }
  326. }
  327. /** @} */