BxTasksModule.php 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. * @defgroup Tasks Tasks
  6. * @ingroup UnaModules
  7. *
  8. * @{
  9. */
  10. /**
  11. * Task states
  12. */
  13. define('BX_TASKS_STATE_BACBLOG', 1);
  14. define('BX_TASKS_STATE_TODO', 2);
  15. define('BX_TASKS_STATE_IN_PROGRESS', 3);
  16. define('BX_TASKS_STATE_IN_REVIEW', 4);
  17. define('BX_TASKS_STATE_CANCELLED', 5);
  18. define('BX_TASKS_STATE_DUPLICATE', 6);
  19. define('BX_TASKS_STATE_DONE', 7);
  20. class BxTasksModule extends BxBaseModTextModule implements iBxDolCalendarService
  21. {
  22. function __construct(&$aModule)
  23. {
  24. parent::__construct($aModule);
  25. $CNF = &$this->_oConfig->CNF;
  26. $this->_aSearchableNamesExcept = array_merge($this->_aSearchableNamesExcept, array(
  27. $CNF['FIELD_PUBLISHED'],
  28. $CNF['FIELD_ALLOW_COMMENTS']
  29. ));
  30. }
  31. /**
  32. * Action methods
  33. */
  34. /**
  35. * Get possible recipients for start conversation form
  36. */
  37. public function actionAjaxGetInitialMembers($iContextPid)
  38. {
  39. $sTerm = bx_get('term');
  40. $sModule = $this->_oConfig->getName();
  41. $a = bx_srv('system', 'profiles_search', [$sTerm, [
  42. 'module' => $sModule,
  43. 'search_params' => ['name' => $sModule . '_initial_members', 'context_pid' => $iContextPid]
  44. ]], 'TemplServiceProfiles');
  45. header('Content-Type:text/javascript; charset=utf-8');
  46. echo(json_encode($a));
  47. }
  48. public function actionSetCompleted($iContentId, $iCompleted)
  49. {
  50. if(!$this->isAllowManage($iContentId) || !$this->complete($iContentId, $iCompleted))
  51. return echoJson(['code' => 1]);
  52. return echoJson(['code' => 0, 'reload' => 1]);
  53. }
  54. public function actionApplyFilter($iContextId, $iFilterId)
  55. {
  56. $CNF = &$this->_oConfig->CNF;
  57. $sCookieKey = $CNF['COOKIE_SETTING_KEY'];
  58. $aFilters = [];
  59. if(isset($_COOKIE[$sCookieKey]))
  60. $aFilters = json_decode($_COOKIE[$sCookieKey], true);
  61. if(($iFilterId = (int)$iFilterId) != 0)
  62. $aFilters[$iContextId] = $iFilterId;
  63. else
  64. unset($aFilters[$iContextId]);
  65. bx_setcookie($sCookieKey, json_encode($aFilters), time() + 60 * 60 * 24 * 365);
  66. return echoJson([
  67. 'content' => $this->_oTemplate->getEntries($iContextId, ['filter' => $iFilterId]),
  68. 'eval' => $this->_oConfig->getJsObject('tasks') . '.onApplyFilter(oData)'
  69. ]);
  70. }
  71. public function actionProcessContextForm($iContextId)
  72. {
  73. if(!$this->isAllowManageByContext($iContextId))
  74. return;
  75. $CNF = &$this->_oConfig->CNF;
  76. $aContext = $this->_oDb->getContexts(['sample' => 'id', 'id' => $iContextId]);
  77. $bContext = !empty($aContext) && is_array($aContext);
  78. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_CONTEXT'], $CNF['OBJECT_FORM_CONTEXT_DISPLAY_EDIT']);
  79. $oForm->setAction(BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'process_context_form/' . $iContextId . '/');
  80. $oForm->initChecker($aContext);
  81. if($oForm->isSubmittedAndValid()) {
  82. if(!$bContext)
  83. $oForm->insert(['id' => $iContextId]);
  84. else
  85. $oForm->update($iContextId);
  86. echoJson([
  87. 'eval' => $this->_oConfig->getJsObject('tasks') . '.hidePopup(oData, ' . $iContextId . ')',
  88. ]);
  89. }
  90. else {
  91. $sContent = $this->_oTemplate->parseHtmlByName('popup_form.html', [
  92. 'form_id' => $oForm->getId(),
  93. 'form' => $oForm->getCode(true)
  94. ]);
  95. if($oForm->isSubmitted())
  96. return echoJson([
  97. 'form' => $sContent,
  98. 'form_id' => $oForm->getId()
  99. ]);
  100. echo $sContent;
  101. }
  102. }
  103. public function actionProcessTaskListForm($iContextId, $iId)
  104. {
  105. if (!$this->isAllowAdd(abs($iContextId)))
  106. return;
  107. $CNF = &$this->_oConfig->CNF;
  108. $oForm = null;
  109. $sPopupTitle = "";
  110. $aContentInfo = array();
  111. if ($iId == 0){
  112. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_LIST_ENTRY'], $CNF['OBJECT_FORM_LIST_ENTRY_DISPLAY_ADD']);
  113. $sPopupTitle = _t('_bx_tasks_form_list_entry_display_add');
  114. }
  115. else {
  116. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_LIST_ENTRY'], $CNF['OBJECT_FORM_LIST_ENTRY_DISPLAY_EDIT']);
  117. $aContentInfo = $this->_oDb->getList($iId);
  118. $sPopupTitle = _t('_bx_tasks_form_list_entry_display_edit');
  119. }
  120. $oForm->aFormAttrs['action'] = BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'process_task_list_form/' . $iContextId . '/' . $iId . '/';
  121. if (!$oForm)
  122. return '';
  123. $oForm->initChecker($aContentInfo, array());
  124. if($oForm->isSubmittedAndValid()) {
  125. if ($iId == 0){
  126. $aValsToAdd['context_id'] = $iContextId;
  127. $iId = $oForm->insert($aValsToAdd);
  128. }
  129. else {
  130. $iId = $oForm->update($iId);
  131. }
  132. return echoJson(array(
  133. 'eval' => $this->_oConfig->getJsObject('tasks') . '.reloadData(oData, ' . $iContextId . ')',
  134. ));
  135. }
  136. else {
  137. $sContent = $this->_oTemplate->parseHtmlByName('popup_form.html', array(
  138. 'form_id' => $oForm->getId(),
  139. 'form' => $oForm->getCode(true)
  140. ));
  141. if (!$oForm->isSubmitted()) {
  142. echo $sContent;
  143. return;
  144. }
  145. return echoJson(array('form' => $sContent, 'form_id' => $oForm->getId()));
  146. }
  147. }
  148. public function actionDeleteTaskList($iId, $iContextId)
  149. {
  150. if (!$this->isAllowManageByContext($iContextId))
  151. return;
  152. $CNF = &$this->_oConfig->CNF;
  153. $aTasks = $this->_oDb->getTasks($iContextId, $iId);
  154. $this->_oDb->deleteList($iId);
  155. if (!empty($aTasks) && ($oConn = BxDolConnection::getObjectInstance($this->_oConfig->CNF['OBJECT_CONNECTION']))) {
  156. foreach ($aTasks as &$aTask)
  157. $oConn->onDeleteContent($aTask[$CNF['FIELD_ID']]);
  158. }
  159. echoJson(array(
  160. 'context_id' => $iContextId,
  161. ));
  162. }
  163. public function actionProcessTaskForm($iContextId, $iListId)
  164. {
  165. if(!$this->isAllowAdd($iContextId))
  166. return echoJson([]);
  167. $CNF = &$this->_oConfig->CNF;
  168. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_ENTRY'], $CNF['OBJECT_FORM_ENTRY_DISPLAY_ADD']);
  169. if(!$oForm)
  170. return echoJson([]);
  171. $oForm->setAction(BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'process_task_form/' . $iContextId . '/' . $iListId . '/');
  172. $oForm->setContextId($iContextId);
  173. $oForm->initChecker();
  174. if($oForm->isSubmittedAndValid()) {
  175. $iContentId = $oForm->insert([$CNF['FIELD_ALLOW_VIEW_TO'] => -$iContextId, $CNF['FIELD_TASKLIST'] => $iListId]);
  176. if($iContentId)
  177. $this->onPublished($iContentId);
  178. return echoJson([
  179. 'eval' => $this->_oConfig->getJsObject('tasks') . '.reloadData(oData, ' . $iContextId . ')',
  180. ]);
  181. }
  182. else {
  183. $sContent = $this->_oTemplate->parseHtmlByName('popup_form.html', [
  184. 'form_id' => $oForm->getId(),
  185. 'form' => $oForm->getCode(true)
  186. ]);
  187. if($oForm->isSubmitted())
  188. return echoJson(['form' => $sContent, 'form_id' => $oForm->getId()]);
  189. echo $sContent;
  190. }
  191. }
  192. public function actionProcessTaskFormEditProperty($iContentId, $sProperty)
  193. {
  194. $CNF = &$this->_oConfig->CNF;
  195. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  196. if(!$this->isAllowAdd(abs($aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']])))
  197. return '';
  198. $sForm = '';
  199. if(($sKey = 'OBJECT_FORM_ENTRY_DISPLAY_EDIT_' . strtoupper($sProperty)) && !empty($CNF[$sKey]))
  200. $sForm = $CNF[$sKey];
  201. else
  202. return '';
  203. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_ENTRY'], $sForm);
  204. $oForm->setId($sForm);
  205. $oForm->setName($sForm);
  206. $oForm->setAction(BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'process_task_form_edit_property/' . $iContentId . '/' . $sProperty . '/');
  207. if(!$oForm)
  208. return '';
  209. $oForm->initChecker($aContentInfo);
  210. if($oForm->isSubmittedAndValid()) {
  211. if(!$oForm->update($iContentId))
  212. return echoJson(['msg' => _t('_bx_tasks_txt_err_cannot_perform_action')]);
  213. if(($sMethod = '_onEdit' . bx_gen_method_name($sProperty)) && method_exists($this, $sMethod))
  214. $this->$sMethod($aContentInfo, $oForm);
  215. else
  216. $this->_onEditProperty($aContentInfo, $sProperty, $oForm);
  217. return echoJson([
  218. 'eval' => $this->_oConfig->getJsObject('tasks') . '.reload(oData)',
  219. ]);
  220. }
  221. else {
  222. $sContent = $this->_oTemplate->parseHtmlByName('popup_form.html', [
  223. 'form_id' => $oForm->getId(),
  224. 'form' => $oForm->getCode(true)
  225. ]);
  226. if (!$oForm->isSubmitted()) {
  227. echo $sContent;
  228. return;
  229. }
  230. return echoJson(['form' => $sContent, 'form_id' => $oForm->getId()]);
  231. }
  232. }
  233. public function actionCalendarData()
  234. {
  235. // check permissions
  236. $aSQLPart = array();
  237. $iContextId = (int)bx_get('context_id');
  238. if(!$this->isAllowView($iContextId))
  239. return;
  240. $oPrivacy = BxDolPrivacy::getObjectInstance($this->_oConfig->CNF['OBJECT_PRIVACY_VIEW']);
  241. if($iContextId) {
  242. $aSQLPart = $oPrivacy ? $oPrivacy->getContentByGroupAsSQLPart(- $iContextId) : array();
  243. }
  244. // get entries
  245. $aEntries = $this->_oDb->getEntriesByDate(bx_get('start'), bx_get('end'), bx_get('event'), $aSQLPart);
  246. header('Content-Type: application/json; charset=utf-8');
  247. echo json_encode($aEntries);
  248. }
  249. public function actionProcessTimer($sAction, $iContentId, $iProfileId)
  250. {
  251. if(!$this->_iProfileId || $this->_iProfileId != $iProfileId)
  252. return echoJson(['msg' => _t('_sys_txt_access_denied')]);
  253. $aResult = $this->serviceProcessTimer($sAction, $iContentId, $iProfileId);
  254. if($aResult['code'] == 0)
  255. $aResult = array_merge($aResult, [
  256. 'content_id' => $iContentId,
  257. 'profile_id' => $iProfileId,
  258. 'content' => $this->_oTemplate->getTimer($iContentId, $iProfileId),
  259. 'eval' => $this->_oConfig->getJsObject('timer') . '.onPerformAction' . bx_gen_method_name($sAction) . '(oData)'
  260. ]);
  261. return echoJson($aResult);
  262. }
  263. public function serviceGetSafeServices()
  264. {
  265. return array_merge(parent::serviceGetSafeServices(), [
  266. 'ProcessTimer' => '',
  267. ]);
  268. }
  269. public function serviceProcessTimer($sAction, $iContentId, $iProfileId)
  270. {
  271. $CNF = &$this->_oConfig->CNF;
  272. $aResult = ['code' => 1, 'msg' => _t('_sys_txt_error_occured')];
  273. $iNow = time();
  274. switch($sAction) {
  275. case 'start':
  276. $this->stopTimerByAuthor($iProfileId);
  277. $iTimer = false;
  278. $aTimer = $this->getTimer($iContentId, $iProfileId);
  279. if($aTimer && is_array($aTimer)) {
  280. if($this->updateTimerById($aTimer['id'], ['started' => $iNow]))
  281. $iTimer = $aTimer['id'];
  282. }
  283. else
  284. $iTimer = $this->_oDb->insertTimer(['content_id' => $iContentId, 'profile_id' => $iProfileId, 'started' => $iNow]);
  285. if($iTimer !== false)
  286. $aResult = ['code' => 0, 'id' => $iTimer];
  287. break;
  288. case 'stop':
  289. if($this->stopTimerByAuthor($iProfileId))
  290. $aResult = ['code' => 0];
  291. break;
  292. case 'resume':
  293. $this->stopTimerByAuthor($iProfileId);
  294. $aTimer = $this->getTimer($iContentId, $iProfileId);
  295. if($aTimer && is_array($aTimer) && $this->updateTimerById($aTimer['id'], ['started' => $iNow]))
  296. $aResult = ['code' => 0];
  297. break;
  298. case 'reload':
  299. $aTimer = $this->getTimer($iContentId, $iProfileId);
  300. $aResult = [
  301. 'code' => 0,
  302. 'started' => $aTimer && ($aTimer['started'] ?? 0) != 0
  303. ];
  304. break;
  305. case 'log':
  306. $aTimer = $this->getTimer($iContentId, $iProfileId);
  307. if(!$aTimer || !is_array($aTimer) || !$aTimer['duration'])
  308. break;
  309. list($iH, $iM) = $this->_oConfig->timeI2A($aTimer['duration'], true);
  310. $iNow = time();
  311. $iTrackId = $this->_oDb->insertTimeTrack([
  312. 'object_id' => $iContentId,
  313. 'author_id' => $iProfileId,
  314. 'author_nip' => $iProfileId == $this->_iProfileId ? bx_get_ip_hash(getVisitorIP()) : 0,
  315. 'value' => $this->_oConfig->timeA2I([(int)$iH, (int)$iM + 1]),
  316. 'value_date' => $iNow,
  317. 'date' => $iNow
  318. ]);
  319. if(!$iTrackId)
  320. break;
  321. $oTime = BxDolReport::getObjectInstance($CNF['OBJECT_REPORTS_TIME'], $iContentId);
  322. if(!$oTime || !$oTime->isEnabled())
  323. break;
  324. if(!$oTime->putReport($iContentId, $iProfileId, $iTrackId))
  325. break;
  326. $this->_oDb->deleteTimer([
  327. 'content_id' => $iContentId,
  328. 'profile_id' => $iProfileId
  329. ]);
  330. $aResult = ['code' => 0];
  331. break;
  332. case 'clear':
  333. if($this->_oDb->deleteTimer(['content_id' => $iContentId, 'profile_id' => $iProfileId]) !== false)
  334. $aResult = ['code' => 0];
  335. break;
  336. }
  337. return $aResult;
  338. }
  339. public function serviceManageTools($sType = 'common')
  340. {
  341. $CNF = &$this->_oConfig->CNF;
  342. $mixedResults = '';
  343. if(!($mixedResults = parent::serviceManageTools($sType)))
  344. return $mixedResults;
  345. return array_merge(parent::serviceManageTools($sType), [
  346. 'menu' => BxDolMenu::getObjectInstance($CNF['OBJECT_MENU_MANAGE_TOOLS_SUBMENU'])
  347. ]);
  348. }
  349. public function serviceGetBlockMenuBrowse()
  350. {
  351. return $this->_oTemplate->getBlockMenuBrowse();
  352. }
  353. public function serviceGetBlockMenuContext($iProfileId)
  354. {
  355. $CNF = &$this->_oConfig->CNF;
  356. if(!$this->isAllowView($iProfileId))
  357. return '';
  358. $oMenu = BxDolMenu::getObjectInstance($CNF['OBJECT_MENU_SUBMENU_VIEW_CONTEXT']);
  359. if(!$oMenu)
  360. return '';
  361. $oMenu->addMarkers(['profile_id' => $iProfileId]);
  362. return $oMenu->getCode();
  363. }
  364. public function serviceGetBlockPreValues($iContextPid = 0)
  365. {
  366. $CNF = &$this->_oConfig->CNF;
  367. if(!$this->isAllowManageByContext($iContextPid))
  368. return $this->_bIsApi ? [] : '';
  369. $bContextPid = !empty($iContextPid);
  370. $sGrid = $CNF['OBJECT_GRID_PRE_VALUES'];
  371. $oGrid = BxDolGrid::getObjectInstance($sGrid);
  372. if(!$oGrid)
  373. return $this->_bIsApi ? [] : '';
  374. if($bContextPid)
  375. $oGrid->setContextPid($iContextPid);
  376. if($this->_bIsApi)
  377. return [
  378. bx_api_get_block('grid', $oGrid->getCodeAPI())
  379. ];
  380. $this->_oTemplate->addCss(['manage_tools.css']);
  381. $this->_oTemplate->addJs(['modules/base/text/js/|manage_tools.js', 'pre_values.js']);
  382. $this->_oTemplate->addJsTranslation(['_sys_grid_search']);
  383. $aResult = [
  384. 'content' => $this->_oTemplate->getJsCode('pre_values', [
  385. 'sObjNameGrid' => $sGrid
  386. ]) . $oGrid->getCode()
  387. ];
  388. return $aResult;
  389. }
  390. public function serviceGetBlockManageTime($sType = 'common', $iContextPid = 0)
  391. {
  392. $CNF = &$this->_oConfig->CNF;
  393. $bContextPid = !empty($iContextPid);
  394. $sGrid = $CNF['OBJECT_GRID_TIME_' . ($bContextPid ? 'CONTEXT_' : '') . strtoupper($sType)];
  395. $oGrid = BxDolGrid::getObjectInstance($sGrid);
  396. if(!$oGrid)
  397. return $this->_bIsApi ? [] : '';
  398. if($bContextPid)
  399. $oGrid->setContextPid($iContextPid);
  400. if($this->_bIsApi)
  401. return [
  402. bx_api_get_block('grid', $oGrid->getCodeAPI())
  403. ];
  404. list($aCssCalendar, $aJsCalendar) = BxBaseFormView::getCssJsCalendar();
  405. $this->_oTemplate->addCss(array_merge($aCssCalendar, ['manage_tools.css', 'time.css']));
  406. $this->_oTemplate->addJs(array_merge($aJsCalendar, ['modules/base/text/js/|manage_tools.js', 'time.js']));
  407. $this->_oTemplate->addJsTranslation(['_sys_grid_search']);
  408. $aResult = [
  409. 'content' => $this->_oTemplate->getJsCode('time', [
  410. 'sObjNameGrid' => $sGrid
  411. ]) . $oGrid->getCode()
  412. ];
  413. if(!$bContextPid)
  414. $aResult['menu'] = BxDolMenu::getObjectInstance($CNF['OBJECT_MENU_MANAGE_TOOLS_SUBMENU']);
  415. return $aResult;
  416. }
  417. public function serviceGetBlockTimers()
  418. {
  419. $CNF = &$this->_oConfig->CNF;
  420. return [
  421. 'content' => $this->_oTemplate->getBlockTimers($this->_iProfileId),
  422. 'menu' => BxDolMenu::getObjectInstance($CNF['OBJECT_MENU_MANAGE_TOOLS_SUBMENU'])
  423. ];
  424. }
  425. /**
  426. * Data for Timeline module
  427. */
  428. public function serviceGetTimelineData()
  429. {
  430. $sModule = $this->_aModule['name'];
  431. return array(
  432. 'handlers' => array(
  433. 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' => ''),
  434. 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' => ''),
  435. 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' => ''),
  436. array('group' => $sModule . '_object', 'type' => 'update', 'alert_unit' => $sModule, 'alert_action' => 'edited'),
  437. array('group' => $sModule . '_object', 'type' => 'delete', 'alert_unit' => $sModule, 'alert_action' => 'deleted'),
  438. ),
  439. 'alerts' => array(
  440. array('unit' => $sModule, 'action' => 'added'),
  441. array('unit' => $sModule, 'action' => 'completed'),
  442. array('unit' => $sModule, 'action' => 'reopened'),
  443. array('unit' => $sModule, 'action' => 'edited'),
  444. array('unit' => $sModule, 'action' => 'deleted'),
  445. )
  446. );
  447. }
  448. /**
  449. * Entry task for Timeline module
  450. */
  451. public function serviceGetTimelinePost($aEvent, $aBrowseParams = array())
  452. {
  453. $CNF = &$this->_oConfig->CNF;
  454. $aResult = parent::serviceGetTimelinePost($aEvent, $aBrowseParams);
  455. if(empty($aResult) || !is_array($aResult) || empty($aResult['date']))
  456. return $aResult;
  457. $aContentInfo = $this->_oDb->getContentInfoById($aEvent['object_id']);
  458. if($aContentInfo[$CNF['FIELD_PUBLISHED']] > $aResult['date'])
  459. $aResult['date'] = $aContentInfo[$CNF['FIELD_PUBLISHED']];
  460. return $aResult;
  461. }
  462. public function serviceGetTimelineCompleted($aEvent, $aBrowseParams = array())
  463. {
  464. $CNF = &$this->_oConfig->CNF;
  465. $aResult = parent::serviceGetTimelinePost($aEvent, $aBrowseParams);
  466. if(empty($aResult) || !is_array($aResult) || empty($aResult['date']))
  467. return $aResult;
  468. $aContentInfo = $this->_oDb->getContentInfoById($aEvent['object_id']);
  469. if($aContentInfo[$CNF['FIELD_PUBLISHED']] > $aResult['date'])
  470. $aResult['date'] = $aContentInfo[$CNF['FIELD_PUBLISHED']];
  471. $aResult['sample_action'] = $aResult['content']['sample_action'] = _t('_bx_tasks_txt_action_completed');
  472. return $aResult;
  473. }
  474. public function serviceGetTimelineReopened($aEvent, $aBrowseParams = array())
  475. {
  476. $CNF = &$this->_oConfig->CNF;
  477. $aResult = parent::serviceGetTimelinePost($aEvent, $aBrowseParams);
  478. if(empty($aResult) || !is_array($aResult) || empty($aResult['date']))
  479. return $aResult;
  480. $aContentInfo = $this->_oDb->getContentInfoById($aEvent['object_id']);
  481. if($aContentInfo[$CNF['FIELD_PUBLISHED']] > $aResult['date'])
  482. $aResult['date'] = $aContentInfo[$CNF['FIELD_PUBLISHED']];
  483. $aResult['sample_action'] = $aResult['content']['sample_action'] = _t('_bx_tasks_txt_action_reopened');
  484. return $aResult;
  485. }
  486. public function serviceGetNotificationsData()
  487. {
  488. $sModule = $this->_aModule['name'];
  489. $sEventPrivacy = $sModule . '_allow_view_event_to';
  490. if(BxDolPrivacy::getObjectInstance($sEventPrivacy) === false)
  491. $sEventPrivacy = '';
  492. $aResult = parent::serviceGetNotificationsData();
  493. $aResult['handlers'] = array_merge($aResult['handlers'], array(
  494. 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),
  495. 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),
  496. 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),
  497. 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),
  498. array('group' => $sModule . '_assign', 'type' => 'delete', 'alert_unit' => $sModule, 'alert_action' => 'unassigned'),
  499. ));
  500. $aResult['settings'] = array_merge($aResult['settings'], array(
  501. array('group' => 'content', 'unit' => $sModule, 'action' => 'completed', 'types' => array('personal', 'follow_member', 'follow_context')),
  502. array('group' => 'content', 'unit' => $sModule, 'action' => 'reopened', 'types' => array('personal', 'follow_member', 'follow_context')),
  503. array('group' => 'content', 'unit' => $sModule, 'action' => 'expired', 'types' => array('personal')),
  504. array('group' => 'content', 'unit' => $sModule, 'action' => 'assigned', 'types' => array('personal')),
  505. ));
  506. $aResult['alerts'] = array_merge($aResult['alerts'], array(
  507. array('unit' => $sModule, 'action' => 'completed'),
  508. array('unit' => $sModule, 'action' => 'reopened'),
  509. array('unit' => $sModule, 'action' => 'expired'),
  510. array('unit' => $sModule, 'action' => 'assigned'),
  511. array('unit' => $sModule, 'action' => 'unassigned'),
  512. ));
  513. return $aResult;
  514. }
  515. public function serviceGetNotificationsCompleted($aEvent)
  516. {
  517. return $this->_serviceGetNotificationsByAction($aEvent, 'completed');
  518. }
  519. public function serviceGetNotificationsReopened($aEvent)
  520. {
  521. return $this->_serviceGetNotificationsByAction($aEvent, 'reopened');
  522. }
  523. public function serviceGetNotificationsExpired($aEvent)
  524. {
  525. return $this->_serviceGetNotificationsByAction($aEvent, 'expired');
  526. }
  527. public function serviceGetNotificationsAssigned($aEvent)
  528. {
  529. return $this->_serviceGetNotificationsByAction($aEvent, 'assigned');
  530. }
  531. protected function _serviceGetNotificationsByAction($aEvent, $sAction)
  532. {
  533. $CNF = &$this->_oConfig->CNF;
  534. $aResult = parent::serviceGetNotificationsPost($aEvent);
  535. if(empty($aResult) || !is_array($aResult))
  536. return $aResult;
  537. $aResult['entry_author'] = $aEvent['object_owner_id'];
  538. $aResult['entry_author_name'] = '';
  539. if(($oAuthor = BxDolProfile::getInstance($aResult['entry_author'])) !== false)
  540. $aResult['entry_author_name'] = $oAuthor->getDisplayName();
  541. $sLangKey = '_bx_tasks_txt_notification_' . $sAction;
  542. if((int)$aEvent['object_privacy_view'] < 0)
  543. $sLangKey .= '_in_context';
  544. $aResult['lang_key'] = _t($sLangKey);
  545. return $aResult;
  546. }
  547. public function serviceCheckAllowedManageInContext($iContextPid)
  548. {
  549. if(!$this->isAllowManageByContext($iContextPid))
  550. return false;
  551. return true;
  552. }
  553. public function serviceCheckAllowedManage($iContentId)
  554. {
  555. if(!$this->isAllowManage($iContentId))
  556. return false;
  557. return true;
  558. }
  559. public function serviceCheckAllowedComplete($iContentId)
  560. {
  561. if(!$this->serviceCheckAllowedManage($iContentId))
  562. return false;
  563. return !$this->isCompleted($iContentId);
  564. }
  565. public function serviceCheckAllowedUncomplete($iContentId)
  566. {
  567. if(!$this->serviceCheckAllowedManage($iContentId))
  568. return false;
  569. return $this->isCompleted($iContentId);
  570. }
  571. public function serviceIsCompleted($iContentId)
  572. {
  573. return $this->isCompleted($iContentId);
  574. }
  575. public function serviceIsUncompleted($iContentId)
  576. {
  577. return !$this->isCompleted($iContentId);
  578. }
  579. public function serviceIsAllowBadges($iContentId)
  580. {
  581. if (!$this->isAllowManage($iContentId))
  582. return false;
  583. if (!$this->serviceIsBadgesAvaliable())
  584. return false;
  585. return true;
  586. }
  587. public function serviceEntityAssignments($iContentId = 0, $bAsArray = false)
  588. {
  589. if(!$iContentId)
  590. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  591. if(!$iContentId)
  592. return false;
  593. $CNF = &$this->_oConfig->CNF;
  594. $aProfiles = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTION'])->getConnectedInitiators($iContentId);
  595. if($bAsArray)
  596. return $aProfiles;
  597. return $this->_oTemplate->entryAssignments($aProfiles);
  598. }
  599. public function serviceEntityTimer($iContentId = 0)
  600. {
  601. if(!$iContentId)
  602. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  603. if(!$iContentId)
  604. return false;
  605. return $this->_oTemplate->entryTimer($iContentId, $this->_iProfileId);
  606. }
  607. public function serviceCheckAllowedCommentsTask($iContentId, $sObjectComments)
  608. {
  609. $CNF = &$this->_oConfig->CNF;
  610. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  611. if($aContentInfo[$CNF['FIELD_ALLOW_COMMENTS']] == 0)
  612. return false;
  613. return parent::serviceCheckAllowedCommentsTask($iContentId, $sObjectComments);
  614. }
  615. public function serviceCheckAllowedCommentsView($iContentId, $sObjectComments)
  616. {
  617. $CNF = &$this->_oConfig->CNF;
  618. //negative id used in comments for reports
  619. if($iContentId < 0)
  620. return CHECK_ACTION_RESULT_ALLOWED;
  621. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  622. if($aContentInfo[$CNF['FIELD_ALLOW_COMMENTS']] == 0)
  623. return false;
  624. return parent::serviceCheckAllowedCommentsView($iContentId, $sObjectComments);
  625. }
  626. /**
  627. * @page service Service Calls
  628. * @section bx_tasks Tasks
  629. * @subsection bx_tasks-page_blocks Page Blocks
  630. * @subsubsection bx_tasks-calendar calendar
  631. *
  632. * @code bx_srv('bx_tasks', 'calendar', [...]); @endcode
  633. *
  634. * Shows tasks calendar baced on die date
  635. *
  636. * @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.
  637. * @param $sTemplate template to use to show calendar, or leave empty for default template, possible options: calendar.html, calendar_compact.html
  638. * @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.
  639. *
  640. * @see BxTasksModule::serviceCalendar
  641. */
  642. /**
  643. * @ref bx_tasks-calendar "calendar"
  644. */
  645. public function serviceCalendar($aData = array(), $sTemplate = 'calendar.html')
  646. {
  647. if (!$this->isAllowView($aData['context_id']))
  648. return;
  649. $o = new BxTemplCalendar(array(
  650. 'eventSources' => array (
  651. bx_append_url_params(BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'calendar_data', $aData),
  652. ),
  653. ), $this->_oTemplate);
  654. return $o->display($sTemplate);
  655. }
  656. public function serviceGetCalendarEntries($iProfileId)
  657. {
  658. $CNF = &$this->_oConfig->CNF;
  659. $oConn = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTION']);
  660. $aData = $oConn->getConnectedInitiators($iProfileId);
  661. $aData2 = array(0);
  662. foreach($aData as $iProfileId2) {
  663. $oProfile = BxDolProfile::getInstance($iProfileId2);
  664. array_push($aData2, $oProfile->getContentId());
  665. }
  666. $aSQLPart['where'] = " AND " . $CNF['TABLE_ENTRIES'] . ".`" . $CNF['FIELD_ID'] . "` IN(" . implode(',', $aData2) . ")";
  667. return $this->_oDb->getEntriesByDate(bx_get('start'), bx_get('end'), null, $aSQLPart);
  668. }
  669. public function serviceBrowseHome($aParams = [])
  670. {
  671. $iContextPid = bx_process_input(bx_get('context_pid'), BX_DATA_INT);
  672. if(!$iContextPid)
  673. return MsgBox(_t('_Empty'));
  674. $sContent = $this->serviceBrowseTasks($iContextPid);
  675. if($sContent && ($oProfile = BxDolProfile::getInstance($iContextPid)) !== false)
  676. return [
  677. 'title' => bx_replace_markers(_t('_bx_tasks_page_block_title_entries_in_context'), [
  678. 'display_name' => $oProfile->getDisplayName(),
  679. 'profile_link' => $oProfile->getUrl()
  680. ]),
  681. 'content' => $sContent
  682. ];
  683. return $sContent;
  684. }
  685. public function serviceBrowseContext($iProfileId = 0, $aParams = [])
  686. {
  687. if(!$iProfileId)
  688. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  689. if(!$iProfileId)
  690. return '';
  691. return $this->serviceBrowseTasks ($iProfileId, $aParams);
  692. }
  693. public function serviceBrowseTasks($iContextId = 0, $aParams = [])
  694. {
  695. if(!$this->isAllowView($iContextId))
  696. return '';
  697. return $this->_oTemplate->getEntriesList($iContextId, $aParams);
  698. }
  699. /**
  700. * Common methods
  701. */
  702. public function getModuleTitle($sName)
  703. {
  704. $sResult = $sName;
  705. if(($_sTitle = '_' . $sName) && ($sTitle = _t($_sTitle)) && strcmp($_sTitle, $sTitle) != 0)
  706. $sResult = $sTitle;
  707. else if(($aModule = $this->_oModule->_oDb->getModuleByName($sName)) && is_array($aModule))
  708. $sResult = $aModule['title'] ?? _t('_undefined');
  709. return $sResult;
  710. }
  711. public function getAssignees()
  712. {
  713. $aResults = [];
  714. if(($oProfile = BxDolProfile::getInstance()) !== false && ($aIds = $this->_oDb->getTimeTracks(['sample' => 'assignees'])))
  715. foreach($aIds as $iId)
  716. $aResults[] = ['key' => $iId, 'value' => $oProfile->getDisplayName($iId)];
  717. return $aResults;
  718. }
  719. public function getPreLists()
  720. {
  721. $CNF = &$this->_oConfig->CNF;
  722. $aResults = [];
  723. if(($aPreLists = $this->_oDb->getPreLists(['sample' => 'all'])) && is_array($aPreLists))
  724. foreach($aPreLists as $aPreList)
  725. $aResults[$aPreList['name']] = _t($aPreList['title']);
  726. return $aResults;
  727. }
  728. public function getContexts()
  729. {
  730. $aContexts = [];
  731. $aModules = bx_srv('system', 'get_modules_by_type', ['context', ['name_as_key' => true]]);
  732. foreach($aModules as $sModule => $aModule)
  733. $aContexts[$sModule] = ($_sTitle = '_' . $sModule) && ($sTitle = _t($_sTitle)) && strcmp($_sTitle, $sTitle) != 0 ? $sTitle : $aModule['title'];
  734. return $aContexts;
  735. }
  736. public function getContextMembers($iContextPid)
  737. {
  738. $aMembers = [];
  739. if(($oContext = BxDolProfile::getInstance($iContextPid)) !== false) {
  740. $aPids = bx_srv($oContext->getModule(), 'fans', [$oContext->getContentId(), true]);
  741. foreach($aPids as $iPid)
  742. $aMembers[$iPid] = $oContext->getDisplayName($iPid);
  743. }
  744. return $aMembers;
  745. }
  746. public function getContextEntries($iContextPid)
  747. {
  748. $CNF = &$this->_oConfig->CNF;
  749. $aEntries = [];
  750. if(($aTasks = $this->_oDb->getTasks($iContextPid)) && is_array($aTasks))
  751. foreach($aTasks as $aTask)
  752. $aEntries[$aTask[$CNF['FIELD_ID']]] = $aTask[$CNF['FIELD_TITLE']];
  753. return $aEntries;
  754. }
  755. public function getTimer($iContentId, $iProfileId)
  756. {
  757. return $this->_oDb->getTimers(['sample' => 'content_profile_ids', 'content_id' => $iContentId, 'profile_id' => $iProfileId]);
  758. }
  759. public function getTimersByAuthor($iProfileId, $bActive = false)
  760. {
  761. return $this->_oDb->getTimers(['sample' => 'profile_id', 'profile_id' => $iProfileId, 'active' => $bActive]);
  762. }
  763. public function stopTimerByAuthor($iProfileId)
  764. {
  765. $aTimer = $this->getTimersByAuthor($iProfileId, true);
  766. if(!$aTimer || !is_array($aTimer))
  767. return false;
  768. return $this->updateTimerById($aTimer['id'], [
  769. 'started' => 0,
  770. 'duration' => $aTimer['duration'] + (time() - $aTimer['started']),
  771. ]);
  772. }
  773. public function updateTimerById($iId, $aSet)
  774. {
  775. return $this->_oDb->updateTimer($aSet, ['id' => (int)$iId]) !== false;
  776. }
  777. public function onPublished($iContentId)
  778. {
  779. parent::onPublished($iContentId);
  780. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  781. if(!$aContentInfo)
  782. return;
  783. $CNF = &$this->_oConfig->CNF;
  784. if(($iContextId = (int)$aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]) < 0) {
  785. $iContextId = abs($iContextId);
  786. $mixedRepo = $this->_oDb->getContextRepository($iContextId);
  787. if($mixedRepo !== false && ($sMl = 'bx_github') && ($sMd = 'create_issue') && bx_is_srv($sMl, $sMd)) {
  788. list($sGhUsername, $sGhRepository) = $mixedRepo;
  789. $aFldLoc2Repo = [
  790. $CNF['FIELD_STICKERS'] => 'labels',
  791. $CNF['FIELD_TYPE'] => 'type',
  792. ];
  793. $aGhFields = [];
  794. foreach($aFldLoc2Repo as $sLocField => $sRepoField)
  795. if(($sLocValue = $aContentInfo[$sLocField] ?? false))
  796. switch($sLocField) {
  797. case $CNF['FIELD_STICKERS']:
  798. $aStickers = $this->getStickers($sLocValue, $iContextId);
  799. if($aStickers && is_array($aStickers)) {
  800. $aGhFields[$sRepoField] = [];
  801. foreach($aStickers as $aSticker)
  802. $aGhFields[$sRepoField][] = $aSticker['title'];
  803. }
  804. break;
  805. case $CNF['FIELD_TYPE']:
  806. $aGhFields[$sRepoField] = $this->getType($sLocValue, $iContextId);
  807. break;
  808. default:
  809. $aGhFields[$sRepoField] = $sLocValue;
  810. }
  811. $aIssue = bx_srv($sMl, $sMd, [$sGhUsername, $sGhRepository, $aContentInfo[$CNF['FIELD_TITLE']], $aContentInfo[$CNF['FIELD_TEXT']], $aGhFields]);
  812. if($aIssue && is_array($aIssue) && ($iNumber = (int)($aIssue['number'] ?? 0)) != 0 && ($sUrl = $aIssue['html_url'] ?? '') != '') {
  813. $this->_oDb->updateEntriesBy([
  814. $CNF['FIELD_GH_ISSUE'] => $iNumber,
  815. $CNF['FIELD_GH_ISSUE_URL'] => $sUrl,
  816. ], [$CNF['FIELD_ID'] => $iContentId]);
  817. $this->logActivity($iContentId, ['key' => '_bx_tasks_txt_msg_synced', 'markers' => [
  818. 'ghi_link' => $sUrl,
  819. 'ghi_number' => $iNumber
  820. ]]);
  821. }
  822. }
  823. }
  824. }
  825. public function onExpired($iContentId)
  826. {
  827. $CNF = &$this->_oConfig->CNF;
  828. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  829. if(!$aContentInfo)
  830. return;
  831. $oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTION']);
  832. if($oConnection) {
  833. $aProfileIds = $oConnection->getConnectedContent($iContentId);
  834. if(!empty($aProfileIds) && is_array($aProfileIds))
  835. foreach($aProfileIds as $iProfileId){
  836. /**
  837. * @hooks
  838. * @hookdef hook-bx_tasks-expired 'bx_tasks', 'expired' - hook on task unassigned to profile
  839. * - $unit_name - equals `bx_tasks`
  840. * - $action - equals `expired`
  841. * - $object_id - task id
  842. * - $sender_id - not used
  843. * - $extra_params - array of additional params with the following array keys:
  844. * - `object_author_id` - [int] profile_id for task's author
  845. * - `privacy_view` - [string] privacy view value
  846. * @hook @ref hook-bx_tasks-expired
  847. */
  848. bx_alert($this->getName(), 'expired', $iContentId, false, array(
  849. 'object_author_id' => $iProfileId,
  850. 'privacy_view' => $aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]
  851. ));
  852. }
  853. }
  854. }
  855. public function isAllowView($iContextId)
  856. {
  857. if(!($oContext = BxDolProfile::getInstance($iContextId)) || $oContext->checkAllowedProfileView($iContextId) !== CHECK_ACTION_RESULT_ALLOWED)
  858. return false;
  859. return true;
  860. }
  861. public function isAllowAdd($iContextId)
  862. {
  863. if(!($oContext = BxDolProfile::getInstance($iContextId)) || $oContext->checkAllowedPostInProfile($iContextId) !== CHECK_ACTION_RESULT_ALLOWED)
  864. return false;
  865. return true;
  866. }
  867. public function isAllowManageByContext($iContextId)
  868. {
  869. if(isAdmin())
  870. return true;
  871. $oProfileContext = BxDolProfile::getInstance($iContextId);
  872. if(BxDolService::call($oProfileContext->getModule(), 'is_admin', array($iContextId)))
  873. return true;
  874. return false;
  875. }
  876. public function isAllowManage($mixedContent)
  877. {
  878. $CNF = &$this->_oConfig->CNF;
  879. $aContentInfo = !is_array($mixedContent) ? $this->_oDb->getContentInfoById((int)$mixedContent) : $mixedContent;
  880. if($this->checkAllowedEdit($aContentInfo) === CHECK_ACTION_RESULT_ALLOWED)
  881. return true;
  882. if($this->isAllowManageByContext(abs($aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']])))
  883. return true;
  884. if(($oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTION'])) !== false) {
  885. $iLoggedId = bx_get_logged_profile_id();
  886. $aProfileIds = $oConnection->getConnectedInitiators($aContentInfo[$CNF['FIELD_ID']]);
  887. if(!empty($aProfileIds) && is_array($aProfileIds) && in_array($iLoggedId, $aProfileIds))
  888. return true;
  889. }
  890. return false;
  891. }
  892. /**
  893. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden. So make sure to make strict(===) checking.
  894. */
  895. public function checkAllowedManage ($aDataEntry, $isPerformAction = false)
  896. {
  897. return $this->isAllowManage($aDataEntry) ? CHECK_ACTION_RESULT_ALLOWED : _t('_sys_txt_access_denied');
  898. }
  899. public function isCompleted($iContentId)
  900. {
  901. $CNF = &$this->_oConfig->CNF;
  902. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  903. return (bool)$aContentInfo[$CNF['FIELD_COMPLETED']];
  904. }
  905. public function complete($iContentId, $iCompleted)
  906. {
  907. $CNF = &$this->_oConfig->CNF;
  908. $iCompleted = (int)$iCompleted;
  909. $this->_oDb->updateEntriesBy([$CNF['FIELD_COMPLETED'] => $iCompleted], [$CNF['FIELD_ID'] => (int)$iContentId]);
  910. $sModule = $this->getName();
  911. $sAction = !$iCompleted ? 'reopened' : 'completed';
  912. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  913. $iContentAuthor = (int)$aContentInfo[$CNF['FIELD_AUTHOR']];
  914. /**
  915. * @hooks
  916. * @hookdef hook-bx_tasks-completed 'bx_tasks', 'completed' - hook on task unassigned to profile
  917. * - $unit_name - equals `bx_tasks`
  918. * - $action - can be `completed` or `reopened`
  919. * - $object_id - task id
  920. * - $sender_id - not used
  921. * - $extra_params - array of additional params with the following array keys:
  922. * - `object_author_id` - [int] profile_id for task's author
  923. * - `privacy_view` - [string] privacy view value
  924. * @hook @ref hook-bx_tasks-completed
  925. */
  926. bx_alert($sModule, $sAction, $iContentId, false, array(
  927. 'object_author_id' => $iContentAuthor,
  928. 'privacy_view' => $aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]
  929. ));
  930. if(($oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTION'])) !== false) {
  931. $aProfileIds = $oConnection->getConnectedContent($iContentId);
  932. if(!empty($aProfileIds) && is_array($aProfileIds))
  933. foreach($aProfileIds as $iProfileId) {
  934. if($iProfileId == $iContentAuthor)
  935. continue;
  936. bx_alert($sModule, $sAction, $iContentId, false, array(
  937. 'object_author_id' => $iProfileId,
  938. 'privacy_view' => $aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]
  939. ));
  940. }
  941. }
  942. $this->logActivity($iContentId, ['key' => '_bx_tasks_txt_msg_' . ($iCompleted ? '' : 'un') . 'completed']);
  943. if(($iContextId = (int)$aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]) < 0 && ($mixedRepository = $this->_oDb->getContextRepository(abs($iContextId))) !== false) {
  944. $sModule = 'bx_github';
  945. $sMethod = ($iCompleted ? 'close' : 'reopen') . '_issue';
  946. if(bx_is_srv($sModule, $sMethod) && ($iGhIssue = (int)$aContentInfo[$CNF['FIELD_GH_ISSUE']]) != 0) {
  947. list($sGhUsername, $sGhRepository) = $mixedRepository;
  948. bx_srv($sModule, $sMethod, [$sGhUsername, $sGhRepository, $iGhIssue]);
  949. }
  950. }
  951. return true;
  952. }
  953. public function logActivity($iContentId, $aMessage)
  954. {
  955. $CNF = &$this->_oConfig->CNF;
  956. $oCmts = BxDolCmts::getObjectInstance($CNF['OBJECT_COMMENTS'], $iContentId);
  957. if(!$oCmts || !$oCmts->isEnabled())
  958. return false;
  959. if(($oAuthor = BxDolProfile::getInstance()) !== false)
  960. $aMessage['markers'] = array_merge($aMessage['markers'] ?? [], [
  961. 'author_link' => $oAuthor->getUrl(),
  962. 'author_name' => $oAuthor->getDisplayName(),
  963. ]);
  964. return $oCmts->addAuto($aMessage);
  965. }
  966. public function ghUpdateIssue($mixedContent, $aGhFields = [])
  967. {
  968. $aContentInfo = is_array($mixedContent) ? $mixedContent : $this->_oDb->getContentInfoById((int)$mixedContent);
  969. if(!$aContentInfo || !is_array($aContentInfo))
  970. return false;
  971. $CNF = &$this->_oConfig->CNF;
  972. $iContentId = (int)$aContentInfo[$CNF['FIELD_ID']];
  973. $iContextId = (int)$aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']];
  974. if($iContextId >= 0)
  975. return false;
  976. $mixedRepo = $this->_oDb->getContextRepository(abs($iContextId));
  977. if($mixedRepo === false)
  978. return false;
  979. list($sGhUsername, $sGhRepository) = $mixedRepo;
  980. $iGhIssue = (int)$aContentInfo[$CNF['FIELD_GH_ISSUE']];
  981. $sMl = 'bx_github';
  982. if($aGhFields && ($sMd = 'update_issue') && bx_is_srv($sMl, $sMd))
  983. if(bx_srv($sMl, $sMd, [$sGhUsername, $sGhRepository, $iGhIssue, $aGhFields]) !== false)
  984. $this->logActivity($iContentId, ['key' => '_bx_tasks_txt_msg_synced', 'markers' => [
  985. 'ghi_link' => $aContentInfo[$CNF['FIELD_GH_ISSUE_URL']],
  986. 'ghi_number' => $iGhIssue
  987. ]]);
  988. return true;
  989. }
  990. public function getStickers($mixedValues, $iContextId = 0)
  991. {
  992. $CNF = &$this->_oConfig->CNF;
  993. $aValues = is_array($mixedValues) ? $mixedValues : BxDolFormCheckerHelper::displaySet($mixedValues);
  994. $aResults = [];
  995. if($iContextId) {
  996. $aStickers = $this->_oDb->getPreValues([
  997. 'sample' => 'context_list',
  998. 'context_id' => $iContextId,
  999. 'list' => 'sticker'
  1000. ]);
  1001. foreach($aValues as $sValue)
  1002. if(($aSticker = $aStickers[$sValue] ?? false))
  1003. $aResults[] = [
  1004. 'title' => $aSticker['title'],
  1005. 'color' => $aSticker['color']
  1006. ];
  1007. }
  1008. if(!$aResults) {
  1009. $aStickers = BxDolForm::getDataItems($CNF['OBJECT_PRE_LIST_STICKERS']);
  1010. foreach($aValues as $sValue)
  1011. $aResults[] = [
  1012. 'title' => $aStickers[$sValue],
  1013. 'color' => ''
  1014. ];
  1015. }
  1016. return $aResults;
  1017. }
  1018. public function getType($mixedValue, $iContextId = 0)
  1019. {
  1020. $sResult = '';
  1021. if($iContextId) {
  1022. $aType = $this->_oDb->getPreValues([
  1023. 'sample' => 'context_list_value',
  1024. 'context_id' => $iContextId,
  1025. 'list' => 'type',
  1026. 'value' => $mixedValue
  1027. ]);
  1028. if($aType && is_array($aType))
  1029. $sResult = $aType['title'];
  1030. }
  1031. if(!$sResult && ($sTypeTitle = $this->_oConfig->getTypeTitle($mixedValue)) != '')
  1032. $sResult = _t($sTypeTitle);
  1033. return $sResult;
  1034. }
  1035. /**
  1036. * Internal methods
  1037. */
  1038. protected function _onEditProperty($aContentInfo, $sProperty, &$oForm)
  1039. {
  1040. $CNF = &$this->_oConfig->CNF;
  1041. $aPropLoc2Repo = [
  1042. $CNF['FIELD_TYPE'] => 'type',
  1043. ];
  1044. $iContentId = (int)$aContentInfo[$CNF['FIELD_ID']];
  1045. $mixedValue = $oForm->getCleanValue($CNF['FIELD_' . strtoupper($sProperty)]);
  1046. if(($sMethod = 'get' . bx_gen_method_name($sProperty) . 'Title') && method_exists($this->_oConfig, $sMethod))
  1047. $mixedValue = $this->_oConfig->$sMethod($mixedValue);
  1048. $this->logActivity($iContentId, ['key' => '_bx_tasks_txt_msg_edit_' . $sProperty, 'markers' => ['value' => $mixedValue]]);
  1049. if(($sPropRepo = $aPropLoc2Repo[$sProperty] ?? false))
  1050. $this->ghUpdateIssue($aContentInfo, [
  1051. $sPropRepo => _t($mixedValue)
  1052. ]);
  1053. }
  1054. protected function _onEditState($aContentInfo, &$oForm)
  1055. {
  1056. $CNF = &$this->_oConfig->CNF;
  1057. $iContentId = (int)$aContentInfo[$CNF['FIELD_ID']];
  1058. $iState = $oForm->getCleanValue($CNF['FIELD_STATE']);
  1059. $this->logActivity($iContentId, ['key' => '_bx_tasks_txt_msg_edit_state', 'markers' => ['value' => $this->_oConfig->getStateTitle($iState)]]);
  1060. $iCompleted = (int)$this->_oConfig->isCompleted($iState);
  1061. if($iCompleted != (int)$this->_oConfig->isCompleted($aContentInfo[$CNF['FIELD_STATE']]))
  1062. $this->complete($iContentId, $iCompleted);
  1063. }
  1064. }
  1065. /** @} */