BxTasksModule.php 78 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101
  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. $this->_oConfig->init($this->_oDb);
  26. $CNF = &$this->_oConfig->CNF;
  27. $this->_aSearchableNamesExcept = array_merge($this->_aSearchableNamesExcept, array(
  28. $CNF['FIELD_PUBLISHED'],
  29. $CNF['FIELD_ALLOW_COMMENTS']
  30. ));
  31. }
  32. /**
  33. * Action methods
  34. */
  35. /**
  36. * Get possible recipients for start conversation form
  37. */
  38. public function actionAjaxGetInitialMembers($iContextPid)
  39. {
  40. $sTerm = bx_get('term');
  41. header('Content-Type:text/javascript; charset=utf-8');
  42. echo json_encode($this->serviceGetInitialMembers($iContextPid, $sTerm));
  43. }
  44. public function actionSetCompleted($iContentId, $iCompleted)
  45. {
  46. return echoJson($this->serviceSetCompleted($iContentId, $iCompleted));
  47. }
  48. public function actionApplyFilter($iContextId, $iFilterId)
  49. {
  50. return echoJson([
  51. 'content' => $this->serviceApplyFilter($iContextId, $iFilterId),
  52. 'eval' => $this->_oConfig->getJsObject('tasks') . '.onApplyFilter(oData)'
  53. ]);
  54. }
  55. public function actionCreateFilter($iContextId)
  56. {
  57. $mixedResult = $this->serviceCreateFilter($iContextId);
  58. if(is_array($mixedResult))
  59. return echoJson($mixedResult);
  60. $sContent = BxTemplFunctions::getInstance()->popupBox($this->_oConfig->getHtmlIds('filter_popup'), _t('_bx_tasks_popup_f_title_add'), $this->_oTemplate->parseHtmlByName('popup_form.html', [
  61. 'form_id' => $mixedResult->getId(),
  62. 'form' => $mixedResult->getCode(true)
  63. ]));
  64. return echoJson(['popup' => ['html' => $sContent, 'options' => ['closeOnOuterClick' => false]]]);
  65. }
  66. public function actionProcessTaskListForm($iContextId, $iId)
  67. {
  68. $mixedResult = $this->serviceProcessTaskListForm($iContextId, $iId);
  69. if(($bReload = is_bool($mixedResult) || is_numeric($mixedResult)) || is_array($mixedResult)) {
  70. if($bReload)
  71. $mixedResult = [
  72. 'eval' => $this->_oConfig->getJsObject('tasks') . '.reloadData(oData, ' . $iContextId . ')',
  73. ];
  74. return echoJson($mixedResult);
  75. }
  76. echo $mixedResult;
  77. }
  78. public function actionDeleteTaskList($iContextId, $iId)
  79. {
  80. return echoJson($this->serviceDeleteTaskList($iContextId, $iId) ? [
  81. 'context_id' => $iContextId,
  82. ] : []);
  83. }
  84. public function actionProcessTaskForm($iContextId, $iListId)
  85. {
  86. $mixedResult = $this->serviceProcessTaskForm($iContextId, $iListId);
  87. if(($bReload = is_bool($mixedResult) || is_numeric($mixedResult)) || is_array($mixedResult)) {
  88. if($bReload)
  89. $mixedResult = [
  90. 'eval' => $this->_oConfig->getJsObject('tasks') . '.reloadData(oData, ' . $iContextId . ')',
  91. ];
  92. return echoJson($mixedResult);
  93. }
  94. echo $mixedResult;
  95. }
  96. public function actionProcessTaskFormEditProperty($iContentId, $sProperty)
  97. {
  98. $mixedResult = $this->serviceProcessTaskFormEditProperty($iContentId, $sProperty);
  99. if(($bReload = is_bool($mixedResult)) || is_array($mixedResult)) {
  100. if($bReload)
  101. $mixedResult = [
  102. 'eval' => $this->_oConfig->getJsObject('tasks') . '.reload(oData)',
  103. ];
  104. return echoJson($mixedResult);
  105. }
  106. echo $mixedResult;
  107. }
  108. public function actionCalendarData()
  109. {
  110. // check permissions
  111. $aSQLPart = array();
  112. $iContextId = (int)bx_get('context_id');
  113. if(!$this->isAllowView($iContextId))
  114. return;
  115. $oPrivacy = BxDolPrivacy::getObjectInstance($this->_oConfig->CNF['OBJECT_PRIVACY_VIEW']);
  116. if($iContextId) {
  117. $aSQLPart = $oPrivacy ? $oPrivacy->getContentByGroupAsSQLPart(- $iContextId) : array();
  118. }
  119. // get entries
  120. $aEntries = $this->_oDb->getEntriesByDate(bx_get('start'), bx_get('end'), bx_get('event'), $aSQLPart);
  121. header('Content-Type: application/json; charset=utf-8');
  122. echo json_encode($aEntries);
  123. }
  124. public function actionProcessTimer($sAction, $iContentId, $iProfileId)
  125. {
  126. if(!$this->_iProfileId || $this->_iProfileId != $iProfileId)
  127. return echoJson(['msg' => _t('_sys_txt_access_denied')]);
  128. $sName = '';
  129. if(($sName = bx_get('name')) !== false)
  130. $sName = bx_process_input($sName);
  131. $aResult = $this->serviceProcessTimer($sAction, $iContentId, $iProfileId);
  132. if($aResult['code'] == 0)
  133. $aResult = array_merge($aResult, [
  134. 'content_id' => $iContentId,
  135. 'profile_id' => $iProfileId,
  136. 'content' => $this->_oTemplate->getTimer($iContentId, $iProfileId, $sName),
  137. 'eval' => $this->_oConfig->getJsObject('timer') . '.onPerformAction' . bx_gen_method_name($sAction) . '(oData)'
  138. ]);
  139. return echoJson($aResult);
  140. }
  141. public function serviceGetSafeServices()
  142. {
  143. return array_merge(parent::serviceGetSafeServices(), [
  144. 'GetInitialMembers' => '',
  145. 'BrowseContext' => '',
  146. 'BrowseTasksByProfile' => '',
  147. 'ProcessTaskListForm' => '',
  148. 'DeleteTaskList' => '',
  149. 'ProcessTaskForm' => '',
  150. 'ProcessTaskFormEditProperty' => '',
  151. 'SetProperty' => '',
  152. 'SetCompleted' => '',
  153. 'CreateFilter' => '',
  154. 'SaveFilter' => '',
  155. 'ApplyFilter' => '',
  156. 'ProcessTimer' => '',
  157. ]);
  158. }
  159. public function serviceGetInitialMembers($iContextPid, $sTerm)
  160. {
  161. if($this->_bIsApi) {
  162. if(!$sTerm)
  163. return [];
  164. $aParams = json_decode($sTerm, true);
  165. if(!isset($aParams['term']))
  166. return [];
  167. $sTerm = $aParams['term'];
  168. }
  169. $sModule = $this->_oConfig->getName();
  170. return bx_srv('system', 'profiles_search', [$sTerm, [
  171. 'module' => $sModule,
  172. 'search_params' => ['name' => $sModule . '_initial_members', 'context_pid' => $iContextPid]
  173. ]], 'TemplServiceProfiles');
  174. }
  175. public function serviceProcessTaskListForm($iContextId, $iId)
  176. {
  177. if(!$this->isAllowAdd(abs($iContextId)))
  178. return [];
  179. $CNF = &$this->_oConfig->CNF;
  180. $bAdd = $iId == 0;
  181. $aContentInfo = [];
  182. $sFormDisplay = $sPopupTitle = '';
  183. if($bAdd) {
  184. $sFormDisplay = $CNF['OBJECT_FORM_LIST_ENTRY_DISPLAY_ADD'];
  185. $sPopupTitle = _t('_bx_tasks_form_list_entry_display_add');
  186. }
  187. else {
  188. $aContentInfo = $this->_oDb->getList($iId);
  189. $sFormDisplay = $CNF['OBJECT_FORM_LIST_ENTRY_DISPLAY_EDIT'];
  190. $sPopupTitle = _t('_bx_tasks_form_list_entry_display_edit');
  191. }
  192. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_LIST_ENTRY'], $sFormDisplay);
  193. if(!$oForm)
  194. return [];
  195. $oForm->setAction(BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'process_task_list_form/' . $iContextId . '/' . $iId . '/');
  196. $oForm->initChecker($aContentInfo, []);
  197. if($oForm->isSubmittedAndValid()) {
  198. $mixedResult = $bAdd ? $oForm->insert(['context_id' => $iContextId]) : (bool)$oForm->update($iId);
  199. return $this->_bIsApi ? [] : $mixedResult;
  200. }
  201. else {
  202. if($this->_bIsApi)
  203. return [bx_api_get_block('form', $oForm->getCodeAPI(), [
  204. 'ext' => [
  205. 'name' => $this->_aModule['name'],
  206. 'request' => ['url' => '/api.php?r=' . $this->_aModule['name'] . '/process_task_list_form/&params[]=' . $iContextId . '&params[]=' . $iId, 'immutable' => true]
  207. ]
  208. ])];
  209. $sContent = $this->_oTemplate->parseHtmlByName('popup_form.html', [
  210. 'form_id' => $oForm->getId(),
  211. 'form' => $oForm->getCode(true)
  212. ]);
  213. if($oForm->isSubmitted())
  214. return [
  215. 'form' => $sContent,
  216. 'form_id' => $oForm->getId()
  217. ];
  218. return $sContent;
  219. }
  220. }
  221. public function serviceDeleteTaskList($iContextId, $iId)
  222. {
  223. if(!$this->isAllowManageByContext($iContextId))
  224. return false;
  225. $CNF = &$this->_oConfig->CNF;
  226. $aTasks = $this->_oDb->getTasks($iContextId, $iId);
  227. if(!empty($aTasks) && ($oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTION'])) !== false)
  228. foreach($aTasks as &$aTask)
  229. $oConnection->onDeleteContent($aTask[$CNF['FIELD_ID']]);
  230. return $this->_oDb->deleteList($iId);
  231. }
  232. public function serviceProcessTaskForm($iContextId, $iListId = 0)
  233. {
  234. if(!$this->isAllowAdd($iContextId))
  235. return [];
  236. $CNF = &$this->_oConfig->CNF;
  237. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_ENTRY'], $CNF['OBJECT_FORM_ENTRY_DISPLAY_ADD']);
  238. if(!$oForm)
  239. return [];
  240. $oForm->setAction(BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'process_task_form/' . $iContextId . '/' . $iListId . '/');
  241. $oForm->setContextId($iContextId);
  242. $oForm->initChecker();
  243. if($oForm->isSubmittedAndValid()) {
  244. $iContentId = $oForm->insert([$CNF['FIELD_TASKLIST'] => $iListId]);
  245. if($iContentId)
  246. $this->onPublished($iContentId);
  247. return $this->_bIsApi ? [] : $iContentId;
  248. }
  249. else {
  250. if($this->_bIsApi)
  251. return [bx_api_get_block('form', $oForm->getCodeAPI(), [
  252. 'ext' => [
  253. 'name' => $this->_aModule['name'],
  254. 'request' => ['url' => '/api.php?r=' . $this->_aModule['name'] . '/process_task_form/&params[]=' . $iContextId . '&params[]=' . $iListId, 'immutable' => true]
  255. ]
  256. ])];
  257. $sContent = $this->_oTemplate->parseHtmlByName('popup_form.html', [
  258. 'form_id' => $oForm->getId(),
  259. 'form' => $oForm->getCode(true)
  260. ]);
  261. if($oForm->isSubmitted())
  262. return [
  263. 'form' => $sContent,
  264. 'form_id' => $oForm->getId()
  265. ];
  266. return $sContent;
  267. }
  268. }
  269. public function serviceProcessTaskFormEditProperty($iContentId, $sProperty)
  270. {
  271. $CNF = &$this->_oConfig->CNF;
  272. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  273. if(!$this->isAllowAdd(abs($aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']])))
  274. return [];
  275. $sForm = $CNF['OBJECT_FORM_ENTRY_DISPLAY_EDIT_' . strtoupper($sProperty)] ?? false;
  276. if(!$sForm)
  277. return [];
  278. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_ENTRY'], $sForm);
  279. $oForm->setId($sForm);
  280. $oForm->setName($sForm);
  281. $oForm->setAction(BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'process_task_form_edit_property/' . $iContentId . '/' . $sProperty . '/');
  282. if(!$oForm)
  283. return [];
  284. $oForm->initChecker($aContentInfo);
  285. if($oForm->isSubmittedAndValid()) {
  286. if(!$oForm->update($iContentId))
  287. return ['msg' => _t('_bx_tasks_txt_err_cannot_perform_action')];
  288. $mixedPropertyValue = $oForm->getCleanValue($CNF['FIELD_' . strtoupper($sProperty)]);
  289. if(($sMethod = '_onEdit' . bx_gen_method_name($sProperty)) && method_exists($this, $sMethod))
  290. $this->$sMethod($aContentInfo, $mixedPropertyValue);
  291. else
  292. $this->_onEditProperty($aContentInfo, $sProperty, $mixedPropertyValue);
  293. return $this->_bIsApi ? [] : true;
  294. }
  295. else {
  296. if($this->_bIsApi)
  297. return [bx_api_get_block('form', $oForm->getCodeAPI(), [
  298. 'ext' => [
  299. 'name' => $this->_aModule['name'],
  300. 'request' => ['url' => '/api.php?r=' . $this->_aModule['name'] . '/process_task_form/&params[]=' . $iContextId . '&params[]=' . $sProperty, 'immutable' => true]
  301. ]
  302. ])];
  303. $sContent = $this->_oTemplate->parseHtmlByName('popup_form.html', [
  304. 'form_id' => $oForm->getId(),
  305. 'form' => $oForm->getCode(true)
  306. ]);
  307. if($oForm->isSubmitted())
  308. return [
  309. 'form' => $sContent,
  310. 'form_id' => $oForm->getId()
  311. ];
  312. return $sContent;
  313. }
  314. }
  315. public function serviceSetProperty($iContentId, $sProperty, $mixedValue)
  316. {
  317. $CNF = &$this->_oConfig->CNF;
  318. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  319. if(!$this->isAllowAdd(abs($aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']])))
  320. return [];
  321. $aProperties = $this->_oConfig->getProperties();
  322. if(in_array($sProperty, $aProperties) && !$this->_oDb->updateEntriesBy([$sProperty => $mixedValue], [$CNF['FIELD_ID'] => $iContentId]))
  323. return ['msg' => _t('_bx_tasks_txt_err_cannot_perform_action')];
  324. if(($sMethod = '_onEdit' . bx_gen_method_name($sProperty)) && method_exists($this, $sMethod))
  325. $this->$sMethod($aContentInfo, $mixedValue);
  326. else
  327. $this->_onEditProperty($aContentInfo, $sProperty, $mixedValue);
  328. return $this->_bIsApi ? [] : true;
  329. }
  330. public function serviceSetCompleted($iContentId, $iCompleted)
  331. {
  332. if(!$this->isAllowManage($iContentId) || !$this->complete($iContentId, $iCompleted))
  333. return ['code' => 1];
  334. return $this->_bIsApi ? [
  335. 'title' => _t('_bx_tasks_menu_item_title_system_set_' . ($iCompleted ? 'un' : '') . 'completed'),
  336. 'request_url' => $this->_aModule['name'] . '/set_completed/&params[]=' . $iContentId . '&params[]=' . (1 - $iCompleted),
  337. ] : [
  338. 'code' => 0,
  339. 'reload' => 1
  340. ];
  341. }
  342. public function serviceCreateFilter($iContextId)
  343. {
  344. $iAuthorId = bx_get_logged_profile_id();
  345. $iFilterId = $this->_oDb->getTemporaryFilterId($iContextId, $iAuthorId);
  346. $oForm = $this->_getFilterForm($iContextId);
  347. if(!$oForm)
  348. return [];
  349. $oForm->initChecker();
  350. if($oForm->isSubmittedAndValid()) {
  351. $aValsToAdd = [
  352. 'context_id' => $iContextId,
  353. 'author' => $iAuthorId,
  354. 'added' => time()
  355. ];
  356. $sTitle = '';
  357. $aValues = [];
  358. foreach($oForm->aInputs as $sName => $aInput) {
  359. if(in_array($sName, ['save_me', 'save_all', 'title', 'controls']))
  360. continue;
  361. if(($mixedValue = $oForm->getCleanValue($sName))) {
  362. $sFltTitle = '';
  363. if(($aFltValues = $aInput['values'] ?? false) && is_array($aFltValues))
  364. $sFltTitle = ' (' . (is_array($mixedValue) ? implode(', ', array_intersect_key($aFltValues, array_flip($mixedValue))) : $aFltValues[$mixedValue]) . ')';
  365. $sTitle .= $aInput['caption'] . $sFltTitle . ' + ';
  366. $aValues[] = [
  367. 'f' => $sName,
  368. 'v' => $mixedValue,
  369. 'r' => ($mixedR = $oForm->getCleanValue($sName . '_reverse')) !== false ? (int)$mixedR : 0
  370. ];
  371. }
  372. }
  373. if($aValues && is_array($aValues) && ($aCnds = $this->_getFilterConditions($iContextId, $aValues)))
  374. $aValsToAdd['conditions'] = json_encode($aCnds);
  375. if(($bForAll = $oForm->getCleanValue('save_all') == 'on') || ($oForm->getCleanValue('save_me') == 'on')) {
  376. $aValsToAdd = array_merge($aValsToAdd, [
  377. 'author' => $bForAll ? 0 : $iAuthorId,
  378. 'title' => $oForm->getCleanValue('title') ?: $sFltTitle,
  379. 'permanent' => 1
  380. ]);
  381. }
  382. else
  383. $aValsToAdd['title'] = trim($sTitle, " +");
  384. $aRes = [];
  385. if(($iFilterId && $oForm->update($iFilterId, $aValsToAdd)) || (!$iFilterId && ($iFilterId = (int)$oForm->insert($aValsToAdd)) != 0)) {
  386. $this->applyFilter($iContextId, $iFilterId);
  387. $aRes = $this->_bIsApi ? [] : ['reload' => 1];
  388. }
  389. else
  390. $aRes = ($sMsg = _t('_bx_tasks_txt_err_cannot_perform_action')) && $this->_bIsApi ? [bx_api_get_msg($sMsg)] : ['msg' => $sMsg];
  391. return $aRes;
  392. }
  393. return $this->_bIsApi ? [bx_api_get_block('form', $oForm->getCodeAPI(), [
  394. 'ext' => [
  395. 'name' => $this->_aModule['name'],
  396. 'request' => ['url' => '/api.php?r=' . $this->_aModule['name'] . '/create_filter/&params[]=' . $iContextId, 'immutable' => true]
  397. ]
  398. ])] : $oForm;
  399. }
  400. public function serviceSaveFilter($iContextId, $iFilterId = 0)
  401. {
  402. $iAuthorId = bx_get_logged_profile_id();
  403. $iFilterId = $iFilterId ?: $this->_oDb->getTemporaryFilterId($iContextId, $iAuthorId);
  404. $oForm = $this->_getSaveFilterForm($iContextId);
  405. if(!$oForm)
  406. return [];
  407. $oForm->initChecker();
  408. if($oForm->isSubmittedAndValid()) {
  409. $aValsToAdd = [
  410. 'author' => $oForm->getCleanValue('save_all') == ($this->_bIsApi ? 1 : 'on') ? 0 : $iAuthorId,
  411. 'title' => $oForm->getCleanValue('title'),
  412. 'permanent' => 1
  413. ];
  414. $aRes = [];
  415. if($oForm->update($iFilterId, $aValsToAdd)) {
  416. $this->applyFilter($iContextId, $iFilterId);
  417. $aRes = $this->_bIsApi ? [] : ['reload' => 1];
  418. }
  419. else
  420. $aRes = ($sMsg = _t('_bx_tasks_txt_err_cannot_perform_action')) && $this->_bIsApi ? [bx_api_get_msg($sMsg)] : ['msg' => $sMsg];
  421. return $aRes;
  422. }
  423. return $this->_bIsApi ? [bx_api_get_block('form', $oForm->getCodeAPI(), [
  424. 'ext' => [
  425. 'name' => $this->_aModule['name'],
  426. 'request' => ['url' => '/api.php?r=' . $this->_aModule['name'] . '/save_filter/&params[]=' . $iContextId, 'immutable' => true]
  427. ]
  428. ])] : $oForm;
  429. }
  430. public function serviceApplyFilter($iContextId, $iFilterId)
  431. {
  432. $this->applyFilter($iContextId, $iFilterId);
  433. return $this->_oTemplate->getEntries($iContextId, ['filter' => $iFilterId]);
  434. }
  435. public function serviceProcessTimer($sAction, $iContentId, $iProfileId)
  436. {
  437. $CNF = &$this->_oConfig->CNF;
  438. $aResult = ['code' => 1, 'msg' => _t('_sys_txt_error_occured')];
  439. $iNow = time();
  440. switch($sAction) {
  441. case 'start':
  442. $this->pauseTimerByAuthor($iProfileId);
  443. $iTimer = false;
  444. $aTimer = $this->getTimer($iContentId, $iProfileId);
  445. if($aTimer && is_array($aTimer)) {
  446. if($this->updateTimerById($aTimer['id'], ['started' => $iNow]))
  447. $iTimer = $aTimer['id'];
  448. }
  449. else
  450. $iTimer = $this->_oDb->insertTimer(['content_id' => $iContentId, 'profile_id' => $iProfileId, 'started' => $iNow]);
  451. if($iTimer !== false)
  452. $aResult = $this->_bIsApi ? $this->_oTemplate->getTimer($iContentId, $iProfileId) : ['code' => 0, 'id' => $iTimer];
  453. break;
  454. case 'pause':
  455. if($this->pauseTimerByAuthor($iProfileId))
  456. $aResult = $this->_bIsApi ? $this->_oTemplate->getTimer($iContentId, $iProfileId) : ['code' => 0];
  457. break;
  458. case 'resume':
  459. $this->pauseTimerByAuthor($iProfileId);
  460. $aTimer = $this->getTimer($iContentId, $iProfileId);
  461. if($aTimer && is_array($aTimer) && $this->updateTimerById($aTimer['id'], ['started' => $iNow]))
  462. $aResult = $this->_bIsApi ? $this->_oTemplate->getTimer($iContentId, $iProfileId) : ['code' => 0];
  463. break;
  464. case 'reload':
  465. $aTimer = $this->getTimer($iContentId, $iProfileId);
  466. $aResult = $this->_bIsApi ? $this->_oTemplate->getTimer($iContentId, $iProfileId) : [
  467. 'code' => 0,
  468. 'started' => $aTimer && ($aTimer['started'] ?? 0) != 0
  469. ];
  470. break;
  471. case 'log':
  472. $this->pauseTimerByAuthor($iProfileId);
  473. $aTimer = $this->getTimer($iContentId, $iProfileId);
  474. if(!$aTimer || !is_array($aTimer) || !$aTimer['duration'])
  475. break;
  476. list($iH, $iM) = $this->_oConfig->timeI2A($aTimer['duration'], true);
  477. $iNow = time();
  478. $iTrackId = $this->_oDb->insertTimeTrack([
  479. 'object_id' => $iContentId,
  480. 'author_id' => $iProfileId,
  481. 'author_nip' => $iProfileId == $this->_iProfileId ? bx_get_ip_hash(getVisitorIP()) : 0,
  482. 'value' => $this->_oConfig->timeA2I([(int)$iH, (int)$iM + 1]),
  483. 'value_date' => $iNow,
  484. 'date' => $iNow
  485. ]);
  486. if(!$iTrackId)
  487. break;
  488. $oTime = BxDolReport::getObjectInstance($CNF['OBJECT_REPORTS_TIME'], $iContentId);
  489. if(!$oTime || !$oTime->isEnabled())
  490. break;
  491. if(!$oTime->putReport($iContentId, $iProfileId, $iTrackId))
  492. break;
  493. $this->_oDb->deleteTimer([
  494. 'content_id' => $iContentId,
  495. 'profile_id' => $iProfileId
  496. ]);
  497. $aResult = $this->_bIsApi ? $this->_oTemplate->getTimer($iContentId, $iProfileId) : ['code' => 0];
  498. break;
  499. case 'log_all':
  500. $aTimers = $this->getTimersByAuthor($iProfileId);
  501. $iAffected = 0;
  502. foreach($aTimers as $aTimer) {
  503. $aSubResult = $this->serviceProcessTimer('log', $aTimer['content_id'], $iProfileId);
  504. if(($aSubResult['code'] ?? false) === 0 || ($this->_bIsApi && ($aSubResult['id'] ?? false)))
  505. $iAffected++;
  506. }
  507. if(count($aTimers) == $iAffected)
  508. $aResult = ['code' => 0, 'reload' => 1];
  509. break;
  510. case 'clear':
  511. if($this->_oDb->deleteTimer(['content_id' => $iContentId, 'profile_id' => $iProfileId]) !== false)
  512. $aResult = $this->_bIsApi ? $this->_oTemplate->getTimer($iContentId, $iProfileId) : ['code' => 0];
  513. break;
  514. case 'clear_all':
  515. $aTimers = $this->getTimersByAuthor($iProfileId);
  516. $iAffected = 0;
  517. foreach($aTimers as $aTimer) {
  518. $aSubResult = $this->serviceProcessTimer('clear', $aTimer['content_id'], $iProfileId);
  519. if(($aSubResult['code'] ?? false) === 0)
  520. $iAffected++;
  521. }
  522. if(count($aTimers) == $iAffected)
  523. $aResult = ['code' => 0, 'reload' => 1];
  524. break;
  525. }
  526. return $aResult;
  527. }
  528. public function serviceManageTools($sType = 'common')
  529. {
  530. $mixedResults = parent::serviceManageTools($sType);
  531. if(!$mixedResults)
  532. return $mixedResults;
  533. if($this->_bIsApi)
  534. return $mixedResults;
  535. if(isset($mixedResults['menu']))
  536. unset($mixedResults['menu']);
  537. return $mixedResults;
  538. }
  539. public function serviceGetBlockMenuBrowse()
  540. {
  541. $mixedResult = $this->_oTemplate->getBlockMenuBrowse();
  542. return $this->_bIsApi ? [
  543. bx_api_get_block('tasks_menu', $mixedResult)
  544. ] : $mixedResult;
  545. }
  546. public function serviceGetBlockMenuContext($iProfileId)
  547. {
  548. $CNF = &$this->_oConfig->CNF;
  549. if(!$this->isAllowView($iProfileId))
  550. return $this->_bIsApi ? [] : '';
  551. $oMenu = BxDolMenu::getObjectInstance($CNF['OBJECT_MENU_SUBMENU_VIEW_CONTEXT']);
  552. if(!$oMenu)
  553. return $this->_bIsApi ? [] : '';
  554. $oMenu->setContextId($iProfileId);
  555. return $this->_bIsApi ? [bx_api_get_block('menu', [
  556. 'title' => '',
  557. 'content' => $oMenu->getCodeAPI()
  558. ])] : $oMenu->getCode();
  559. }
  560. public function serviceGetBlockContextPreValues($iContextPid = 0)
  561. {
  562. $CNF = &$this->_oConfig->CNF;
  563. if(!$this->isAllowManageByContext($iContextPid))
  564. return $this->_bIsApi ? [] : '';
  565. $bContextPid = !empty($iContextPid);
  566. $sGrid = $CNF['OBJECT_GRID_PRE_VALUES'];
  567. $oGrid = BxDolGrid::getObjectInstance($sGrid);
  568. if(!$oGrid)
  569. return $this->_bIsApi ? [] : '';
  570. if($bContextPid)
  571. $oGrid->setContextPid($iContextPid);
  572. if($this->_bIsApi)
  573. return [
  574. bx_api_get_block('grid', $oGrid->getCodeAPI())
  575. ];
  576. $this->_oTemplate->addCss(['manage_tools.css']);
  577. $this->_oTemplate->addJs(['modules/base/text/js/|manage_tools.js', 'pre_values.js']);
  578. $this->_oTemplate->addJsTranslation(['_sys_grid_search']);
  579. $aResult = [
  580. 'content' => $this->_oTemplate->getJsCode('pre_values', [
  581. 'sObjNameGrid' => $sGrid,
  582. 'sPageUrl' => BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink($CNF['URL_CONTEXT_VALUES'], ['profile_id' => $iContextPid])
  583. ]) . $oGrid->getCode()
  584. ];
  585. return $aResult;
  586. }
  587. public function serviceGetBlockContextAuthorize($iContextPid = 0)
  588. {
  589. $sModule = 'bx_github';
  590. $sMethod = 'get_block_authorize';
  591. if(!bx_is_srv($sModule, $sMethod))
  592. return '';
  593. $aContext = $this->_oDb->getContexts(['sample' => 'id', 'id' => $iContextPid]);
  594. if(empty($aContext) || !is_array($aContext) || empty($aContext['gh_app_id']) || empty($aContext['gh_username']) || empty($aContext['gh_repository']))
  595. return '';
  596. return bx_srv($sModule, $sMethod, [$aContext['gh_app_id']]);
  597. }
  598. public function serviceGetBlockContextSettings($iContextPid = 0)
  599. {
  600. $CNF = &$this->_oConfig->CNF;
  601. $iContextAuthorPid = 0;
  602. if(!$this->isAllowManageByContext($iContextPid) || !($iContextAuthorPid = $this->getContextAuthorId($iContextPid)))
  603. return $this->_bIsApi ? [] : '';
  604. $aContext = $this->_oDb->getContexts(['sample' => 'id', 'id' => $iContextPid]);
  605. $bContext = !empty($aContext) && is_array($aContext);
  606. $sMessageText = '';
  607. $iMessageTimer = 0;
  608. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_CONTEXT'], $CNF['OBJECT_FORM_CONTEXT_DISPLAY_EDIT']);
  609. $oForm->setProfileId($iContextAuthorPid);
  610. $oForm->initChecker($aContext);
  611. if($oForm->isSubmittedAndValid()) {
  612. if(!$bContext)
  613. $bResult = $oForm->insert(['id' => $iContextPid]);
  614. else
  615. $bResult = $oForm->update($iContextPid) !== false;
  616. $sMessageText = '_bx_tasks_txt' . (!$bResult ? '_err_cannot' : '_msg') . '_perform_action';
  617. $iMessageTimer = $bResult ? 3 : 0;
  618. }
  619. return $this->_bIsApi ? [bx_api_get_block('form', $oForm->getCodeAPI(), [
  620. 'ext' => [
  621. 'name' => $oForm->getName(),
  622. 'request' => ['url' => '/api.php?r=' . $this->_aModule['name'] . '/get_block_context_settings&profile_id=' . $iContextPid, 'immutable' => true]
  623. ]
  624. ])] : (!empty($sMessageText) ? MsgBox(_t($sMessageText), $iMessageTimer) : '') . $oForm->getCode();
  625. }
  626. public function serviceGetBlockManageTime($sType = 'common', $iContextPid = 0)
  627. {
  628. $CNF = &$this->_oConfig->CNF;
  629. $bContextPid = !empty($iContextPid);
  630. $sGrid = $CNF['OBJECT_GRID_TIME_' . ($bContextPid ? 'CONTEXT_' : '') . strtoupper($sType)];
  631. $oGrid = BxDolGrid::getObjectInstance($sGrid);
  632. if(!$oGrid)
  633. return $this->_bIsApi ? [] : '';
  634. if($bContextPid)
  635. $oGrid->setContextPid($iContextPid);
  636. if($this->_bIsApi)
  637. return [
  638. bx_api_get_block('grid', $oGrid->getCodeAPI())
  639. ];
  640. list($aCssCalendar, $aJsCalendar) = BxBaseFormView::getCssJsCalendar();
  641. $this->_oTemplate->addCss(array_merge($aCssCalendar, ['manage_tools.css', 'time.css']));
  642. $this->_oTemplate->addJs(array_merge($aJsCalendar, ['modules/base/text/js/|manage_tools.js', 'time.js']));
  643. $this->_oTemplate->addJsTranslation(['_sys_grid_search']);
  644. $aResult = [
  645. 'content' => $this->_oTemplate->getJsCode('time', [
  646. 'sObjNameGrid' => $sGrid
  647. ]) . $oGrid->getCode()
  648. ];
  649. if(!$bContextPid)
  650. $aResult['menu'] = BxDolMenu::getObjectInstance($CNF['OBJECT_MENU_MANAGE_TOOLS_SUBMENU']);
  651. return $aResult;
  652. }
  653. public function serviceGetBlockTimers()
  654. {
  655. $mixedResult = $this->_oTemplate->getBlockTimers($this->_iProfileId);
  656. return $this->_bIsApi ? [
  657. bx_api_get_block('tasks_timers', $mixedResult)
  658. ] : $mixedResult;
  659. }
  660. /**
  661. * Data for Timeline module
  662. */
  663. public function serviceGetTimelineData()
  664. {
  665. $sModule = $this->_aModule['name'];
  666. return array(
  667. 'handlers' => array(
  668. 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' => ''),
  669. 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' => ''),
  670. 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' => ''),
  671. array('group' => $sModule . '_object', 'type' => 'update', 'alert_unit' => $sModule, 'alert_action' => 'edited'),
  672. array('group' => $sModule . '_object', 'type' => 'delete', 'alert_unit' => $sModule, 'alert_action' => 'deleted'),
  673. ),
  674. 'alerts' => array(
  675. array('unit' => $sModule, 'action' => 'added'),
  676. array('unit' => $sModule, 'action' => 'completed'),
  677. array('unit' => $sModule, 'action' => 'reopened'),
  678. array('unit' => $sModule, 'action' => 'edited'),
  679. array('unit' => $sModule, 'action' => 'deleted'),
  680. )
  681. );
  682. }
  683. /**
  684. * Entry task for Timeline module
  685. */
  686. public function serviceGetTimelinePost($aEvent, $aBrowseParams = array())
  687. {
  688. $CNF = &$this->_oConfig->CNF;
  689. $aResult = parent::serviceGetTimelinePost($aEvent, $aBrowseParams);
  690. if(empty($aResult) || !is_array($aResult) || empty($aResult['date']))
  691. return $aResult;
  692. $aContentInfo = $this->_oDb->getContentInfoById($aEvent['object_id']);
  693. if($aContentInfo[$CNF['FIELD_PUBLISHED']] > $aResult['date'])
  694. $aResult['date'] = $aContentInfo[$CNF['FIELD_PUBLISHED']];
  695. return $aResult;
  696. }
  697. public function serviceGetTimelineCompleted($aEvent, $aBrowseParams = array())
  698. {
  699. $CNF = &$this->_oConfig->CNF;
  700. $aResult = parent::serviceGetTimelinePost($aEvent, $aBrowseParams);
  701. if(empty($aResult) || !is_array($aResult) || empty($aResult['date']))
  702. return $aResult;
  703. $aContentInfo = $this->_oDb->getContentInfoById($aEvent['object_id']);
  704. if($aContentInfo[$CNF['FIELD_PUBLISHED']] > $aResult['date'])
  705. $aResult['date'] = $aContentInfo[$CNF['FIELD_PUBLISHED']];
  706. $aResult['sample_action'] = $aResult['content']['sample_action'] = _t('_bx_tasks_txt_action_completed');
  707. return $aResult;
  708. }
  709. public function serviceGetTimelineReopened($aEvent, $aBrowseParams = array())
  710. {
  711. $CNF = &$this->_oConfig->CNF;
  712. $aResult = parent::serviceGetTimelinePost($aEvent, $aBrowseParams);
  713. if(empty($aResult) || !is_array($aResult) || empty($aResult['date']))
  714. return $aResult;
  715. $aContentInfo = $this->_oDb->getContentInfoById($aEvent['object_id']);
  716. if($aContentInfo[$CNF['FIELD_PUBLISHED']] > $aResult['date'])
  717. $aResult['date'] = $aContentInfo[$CNF['FIELD_PUBLISHED']];
  718. $aResult['sample_action'] = $aResult['content']['sample_action'] = _t('_bx_tasks_txt_action_reopened');
  719. return $aResult;
  720. }
  721. public function serviceGetNotificationsData()
  722. {
  723. $sModule = $this->_aModule['name'];
  724. $sEventPrivacy = $sModule . '_allow_view_event_to';
  725. if(BxDolPrivacy::getObjectInstance($sEventPrivacy) === false)
  726. $sEventPrivacy = '';
  727. $aResult = parent::serviceGetNotificationsData();
  728. $aResult['handlers'] = array_merge($aResult['handlers'], array(
  729. 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),
  730. 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),
  731. 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),
  732. 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),
  733. array('group' => $sModule . '_assign', 'type' => 'delete', 'alert_unit' => $sModule, 'alert_action' => 'unassigned'),
  734. ));
  735. $aResult['settings'] = array_merge($aResult['settings'], array(
  736. array('group' => 'content', 'unit' => $sModule, 'action' => 'completed', 'types' => array('personal', 'follow_member', 'follow_context')),
  737. array('group' => 'content', 'unit' => $sModule, 'action' => 'reopened', 'types' => array('personal', 'follow_member', 'follow_context')),
  738. array('group' => 'content', 'unit' => $sModule, 'action' => 'expired', 'types' => array('personal')),
  739. array('group' => 'content', 'unit' => $sModule, 'action' => 'assigned', 'types' => array('personal')),
  740. ));
  741. $aResult['alerts'] = array_merge($aResult['alerts'], array(
  742. array('unit' => $sModule, 'action' => 'completed'),
  743. array('unit' => $sModule, 'action' => 'reopened'),
  744. array('unit' => $sModule, 'action' => 'expired'),
  745. array('unit' => $sModule, 'action' => 'assigned'),
  746. array('unit' => $sModule, 'action' => 'unassigned'),
  747. ));
  748. return $aResult;
  749. }
  750. public function serviceGetNotificationsCompleted($aEvent)
  751. {
  752. return $this->_serviceGetNotificationsByAction($aEvent, 'completed');
  753. }
  754. public function serviceGetNotificationsReopened($aEvent)
  755. {
  756. return $this->_serviceGetNotificationsByAction($aEvent, 'reopened');
  757. }
  758. public function serviceGetNotificationsExpired($aEvent)
  759. {
  760. return $this->_serviceGetNotificationsByAction($aEvent, 'expired');
  761. }
  762. public function serviceGetNotificationsAssigned($aEvent)
  763. {
  764. return $this->_serviceGetNotificationsByAction($aEvent, 'assigned');
  765. }
  766. protected function _serviceGetNotificationsByAction($aEvent, $sAction)
  767. {
  768. $CNF = &$this->_oConfig->CNF;
  769. $aResult = parent::serviceGetNotificationsPost($aEvent);
  770. if(empty($aResult) || !is_array($aResult))
  771. return $aResult;
  772. $aResult['entry_author'] = $aEvent['object_owner_id'];
  773. $aResult['entry_author_name'] = '';
  774. if(($oAuthor = BxDolProfile::getInstance($aResult['entry_author'])) !== false)
  775. $aResult['entry_author_name'] = $oAuthor->getDisplayName();
  776. $sLangKey = '_bx_tasks_txt_notification_' . $sAction;
  777. if((int)$aEvent['object_privacy_view'] < 0)
  778. $sLangKey .= '_in_context';
  779. $aResult['lang_key'] = _t($sLangKey);
  780. return $aResult;
  781. }
  782. public function serviceCheckAllowedManageInContext($iContextPid)
  783. {
  784. if(!$this->isAllowManageByContext($iContextPid))
  785. return false;
  786. return true;
  787. }
  788. public function serviceCheckAllowedManage($iContentId)
  789. {
  790. if(!$this->isAllowManage($iContentId))
  791. return false;
  792. return true;
  793. }
  794. public function serviceCheckAllowedComplete($iContentId)
  795. {
  796. if(!$this->serviceCheckAllowedManage($iContentId))
  797. return false;
  798. return !$this->isCompleted($iContentId);
  799. }
  800. public function serviceCheckAllowedUncomplete($iContentId)
  801. {
  802. if(!$this->serviceCheckAllowedManage($iContentId))
  803. return false;
  804. return $this->isCompleted($iContentId);
  805. }
  806. public function serviceIsCompleted($iContentId)
  807. {
  808. return $this->isCompleted($iContentId);
  809. }
  810. public function serviceIsUncompleted($iContentId)
  811. {
  812. return !$this->isCompleted($iContentId);
  813. }
  814. public function serviceIsAllowBadges($iContentId)
  815. {
  816. if (!$this->isAllowManage($iContentId))
  817. return false;
  818. if (!$this->serviceIsBadgesAvaliable())
  819. return false;
  820. return true;
  821. }
  822. public function serviceEntityTextBlock ($iContentId = 0)
  823. {
  824. if(!$iContentId)
  825. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  826. if(!$iContentId)
  827. return false;
  828. $CNF = &$this->_oConfig->CNF;
  829. $bAllowEdit = $this->isAllowEdit($iContentId);
  830. if($this->_bIsApi) {
  831. $aResult = parent::serviceEntityTextBlock($iContentId);
  832. if($bAllowEdit) {
  833. $aResult['0']['data']['editable'] = true;
  834. $aResult['0']['data']['params']['request_url'] = $this->_aModule['name'] . '/set_property/&params[]=' . $iContentId . '&params[]=';
  835. }
  836. return $aResult;
  837. }
  838. $sResult = '';
  839. if(!$bAllowEdit)
  840. $sResult = parent::serviceEntityTextBlock($iContentId);
  841. else
  842. $sResult = $this->serviceEntityEdit($iContentId, $CNF['OBJECT_FORM_ENTRY_DISPLAY_EDIT_BODY']);
  843. return $sResult;
  844. }
  845. public function serviceEntityAssignments($iContentId = 0, $bAsArray = false)
  846. {
  847. if(!$iContentId)
  848. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  849. if(!$iContentId)
  850. return false;
  851. $CNF = &$this->_oConfig->CNF;
  852. $aProfiles = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTION'])->getConnectedInitiators($iContentId);
  853. if($bAsArray)
  854. return $aProfiles;
  855. $mixedResult = $this->_oTemplate->entryAssignments($aProfiles);
  856. return $this->_bIsApi ? [
  857. bx_api_get_block('profiles_list', ['data' => $mixedResult])
  858. ] : $mixedResult;
  859. }
  860. public function serviceEntityTimer($iContentId = 0)
  861. {
  862. if(!$iContentId)
  863. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  864. if(!$iContentId)
  865. return false;
  866. $mixedResult = $this->_oTemplate->entryTimer($iContentId, $this->_iProfileId);
  867. return $this->_bIsApi ? [
  868. bx_api_get_block('task_timer', $mixedResult)
  869. ] : $mixedResult;
  870. }
  871. public function serviceCheckAllowedCommentsTask($iContentId, $sObjectComments)
  872. {
  873. $CNF = &$this->_oConfig->CNF;
  874. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  875. if($aContentInfo[$CNF['FIELD_ALLOW_COMMENTS']] == 0)
  876. return false;
  877. return parent::serviceCheckAllowedCommentsTask($iContentId, $sObjectComments);
  878. }
  879. public function serviceCheckAllowedCommentsView($iContentId, $sObjectComments)
  880. {
  881. $CNF = &$this->_oConfig->CNF;
  882. //negative id used in comments for reports
  883. if($iContentId < 0)
  884. return CHECK_ACTION_RESULT_ALLOWED;
  885. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  886. if($aContentInfo[$CNF['FIELD_ALLOW_COMMENTS']] == 0)
  887. return false;
  888. return parent::serviceCheckAllowedCommentsView($iContentId, $sObjectComments);
  889. }
  890. /**
  891. * @page service Service Calls
  892. * @section bx_tasks Tasks
  893. * @subsection bx_tasks-page_blocks Page Blocks
  894. * @subsubsection bx_tasks-calendar calendar
  895. *
  896. * @code bx_srv('bx_tasks', 'calendar', [...]); @endcode
  897. *
  898. * Shows tasks calendar baced on die date
  899. *
  900. * @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.
  901. * @param $sTemplate template to use to show calendar, or leave empty for default template, possible options: calendar.html, calendar_compact.html
  902. * @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.
  903. *
  904. * @see BxTasksModule::serviceCalendar
  905. */
  906. /**
  907. * @ref bx_tasks-calendar "calendar"
  908. */
  909. public function serviceCalendar($aData = array(), $sTemplate = 'calendar.html')
  910. {
  911. if (!$this->isAllowView($aData['context_id']))
  912. return;
  913. $o = new BxTemplCalendar(array(
  914. 'eventSources' => array (
  915. bx_append_url_params(BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'calendar_data', $aData),
  916. ),
  917. ), $this->_oTemplate);
  918. return $o->display($sTemplate);
  919. }
  920. public function serviceGetCalendarEntries($iProfileId)
  921. {
  922. $CNF = &$this->_oConfig->CNF;
  923. $oConn = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTION']);
  924. $aData = $oConn->getConnectedInitiators($iProfileId);
  925. $aData2 = array(0);
  926. foreach($aData as $iProfileId2) {
  927. $oProfile = BxDolProfile::getInstance($iProfileId2);
  928. array_push($aData2, $oProfile->getContentId());
  929. }
  930. $aSQLPart['where'] = " AND " . $CNF['TABLE_ENTRIES'] . ".`" . $CNF['FIELD_ID'] . "` IN(" . implode(',', $aData2) . ")";
  931. return $this->_oDb->getEntriesByDate(bx_get('start'), bx_get('end'), null, $aSQLPart);
  932. }
  933. public function serviceBrowseHome($aParams = [])
  934. {
  935. $CNF = &$this->_oConfig->CNF;
  936. $sContent = '';
  937. if(($iContextPid = bx_process_input(bx_get('context_pid'), BX_DATA_INT))) {
  938. $sContent = $this->serviceBrowseTasks($iContextPid);
  939. if($sContent && ($oProfile = BxDolProfile::getInstance($iContextPid)) !== false)
  940. return [
  941. 'title' => bx_replace_markers(_t('_bx_tasks_page_block_title_entries_in_context'), [
  942. 'display_name' => $oProfile->getDisplayName(),
  943. 'profile_link' => bx_absolute_url(BxDolPermalinks::getInstance()->permalink($CNF['URL_CONTEXT_ENTRIES'], ['profile_id' => $iContextPid]))
  944. ]),
  945. 'content' => $sContent
  946. ];
  947. }
  948. else
  949. $sContent = $this->serviceBrowseTasksByProfile();
  950. return $sContent ?: MsgBox(_t('_Empty'));
  951. }
  952. public function serviceBrowseContext($iProfileId = 0, $aParams = [])
  953. {
  954. if(!$iProfileId)
  955. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  956. if(!$iProfileId)
  957. return '';
  958. if($this->_bIsApi && is_string($aParams))
  959. $aParams = bx_api_get_browse_params($aParams);
  960. return $this->serviceBrowseTasks ($iProfileId, $aParams);
  961. }
  962. public function serviceBrowseTasks($iContextId = 0, $aParams = [])
  963. {
  964. if(!$this->isAllowView($iContextId))
  965. return $this->_bIsApi ? [] : '';
  966. $mixedResult = $this->_oTemplate->getEntriesList($iContextId, $aParams);
  967. return $this->_bIsApi ? [
  968. bx_api_get_block('tasks_list', $mixedResult)
  969. ] : $mixedResult;
  970. }
  971. public function serviceBrowseTasksByProfile($iProfileId = 0, $aParams = [])
  972. {
  973. if(!$iProfileId && $this->_iProfileId)
  974. $iProfileId = $this->_iProfileId;
  975. $mixedResult = $this->_oTemplate->getEntriesList(0, array_merge($aParams, [
  976. 'for_profile' => $iProfileId
  977. ]));
  978. return $this->_bIsApi ? [
  979. bx_api_get_block('tasks_list', $mixedResult)
  980. ] : $mixedResult;
  981. }
  982. /**
  983. * Common methods
  984. */
  985. public function getModuleTitle($sName)
  986. {
  987. $sResult = $sName;
  988. if(($_sTitle = '_' . $sName) && ($sTitle = _t($_sTitle)) && strcmp($_sTitle, $sTitle) != 0)
  989. $sResult = $sTitle;
  990. else if(($aModule = $this->_oDb->getModuleByName($sName)) && is_array($aModule))
  991. $sResult = $aModule['title'] ?? _t('_undefined');
  992. return $sResult;
  993. }
  994. public function getAssignees()
  995. {
  996. $aResults = [];
  997. if(($oProfile = BxDolProfile::getInstance()) !== false && ($aIds = $this->_oDb->getTimeTracks(['sample' => 'assignees'])))
  998. foreach($aIds as $iId)
  999. $aResults[] = ['key' => $iId, 'value' => $oProfile->getDisplayName($iId)];
  1000. return $aResults;
  1001. }
  1002. public function getPreLists()
  1003. {
  1004. $CNF = &$this->_oConfig->CNF;
  1005. $aResults = [];
  1006. if(($aPreLists = $this->_oDb->getPreLists(['sample' => 'all'])) && is_array($aPreLists))
  1007. foreach($aPreLists as $aPreList)
  1008. $aResults[$aPreList['name']] = _t($aPreList['title']);
  1009. return $aResults;
  1010. }
  1011. public function getContexts()
  1012. {
  1013. $aContexts = [];
  1014. $aModules = bx_srv('system', 'get_modules_by_type', ['context', ['name_as_key' => true]]);
  1015. foreach($aModules as $sModule => $aModule)
  1016. $aContexts[$sModule] = ($_sTitle = '_' . $sModule) && ($sTitle = _t($_sTitle)) && strcmp($_sTitle, $sTitle) != 0 ? $sTitle : $aModule['title'];
  1017. return $aContexts;
  1018. }
  1019. public function getContextAuthorId($iContextPid)
  1020. {
  1021. $iContextAuthorPid = 0;
  1022. if(($oContext = BxDolProfile::getInstance($iContextPid)) !== false)
  1023. $iContextAuthorPid = bx_srv($oContext->getModule(), 'get_author', [$oContext->getContentId()]);
  1024. return $iContextAuthorPid;
  1025. }
  1026. public function getContextMembers($iContextPid)
  1027. {
  1028. $aMembers = [];
  1029. if(($oContext = BxDolProfile::getInstance($iContextPid)) !== false) {
  1030. $aPids = bx_srv($oContext->getModule(), 'fans', [$oContext->getContentId(), true]);
  1031. foreach($aPids as $iPid)
  1032. $aMembers[$iPid] = $oContext->getDisplayName($iPid);
  1033. }
  1034. return $aMembers;
  1035. }
  1036. public function getContextEntries($iContextPid)
  1037. {
  1038. $CNF = &$this->_oConfig->CNF;
  1039. $aEntries = [];
  1040. if(($aTasks = $this->_oDb->getTasks($iContextPid)) && is_array($aTasks))
  1041. foreach($aTasks as $aTask)
  1042. $aEntries[$aTask[$CNF['FIELD_ID']]] = $aTask[$CNF['FIELD_TITLE']];
  1043. return $aEntries;
  1044. }
  1045. public function getTimer($iContentId, $iProfileId)
  1046. {
  1047. return $this->_oDb->getTimers(['sample' => 'content_profile_ids', 'content_id' => $iContentId, 'profile_id' => $iProfileId]);
  1048. }
  1049. public function getTimersByAuthor($iProfileId, $bActive = false)
  1050. {
  1051. return $this->_oDb->getTimers(['sample' => 'profile_id', 'profile_id' => $iProfileId, 'active' => $bActive]);
  1052. }
  1053. public function pauseTimerByAuthor($iProfileId)
  1054. {
  1055. $aTimer = $this->getTimersByAuthor($iProfileId, true);
  1056. if(!$aTimer || !is_array($aTimer) || empty($aTimer['started']))
  1057. return false;
  1058. return $this->updateTimerById($aTimer['id'], [
  1059. 'started' => 0,
  1060. 'duration' => $aTimer['duration'] + (time() - $aTimer['started']),
  1061. ]);
  1062. }
  1063. public function updateTimerById($iId, $aSet)
  1064. {
  1065. return $this->_oDb->updateTimer($aSet, ['id' => (int)$iId]) !== false;
  1066. }
  1067. public function onPublished($iContentId)
  1068. {
  1069. parent::onPublished($iContentId);
  1070. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  1071. if(!$aContentInfo)
  1072. return;
  1073. $CNF = &$this->_oConfig->CNF;
  1074. if(($iContextId = (int)$aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]) < 0) {
  1075. $iContextId = abs($iContextId);
  1076. $mixedRepo = $this->ghGetRepository($iContextId);
  1077. if($mixedRepo !== false && ($sMl = 'bx_github') && ($sMd = 'create_issue') && bx_is_srv($sMl, $sMd)) {
  1078. list($sGhUsername, $sGhRepository, $mixedAuth) = $mixedRepo;
  1079. $aFldLoc2Repo = [
  1080. $CNF['FIELD_STICKERS'] => 'labels',
  1081. $CNF['FIELD_TYPE'] => 'type',
  1082. ];
  1083. $aGhFields = [];
  1084. foreach($aFldLoc2Repo as $sLocField => $sRepoField)
  1085. if(($sLocValue = $aContentInfo[$sLocField] ?? false))
  1086. switch($sLocField) {
  1087. case $CNF['FIELD_STICKERS']:
  1088. $aStickers = $this->getStickers($sLocValue, $iContextId);
  1089. if($aStickers && is_array($aStickers)) {
  1090. $aGhFields[$sRepoField] = [];
  1091. foreach($aStickers as $aSticker)
  1092. $aGhFields[$sRepoField][] = $aSticker['title'];
  1093. }
  1094. break;
  1095. case $CNF['FIELD_TYPE']:
  1096. $aGhFields[$sRepoField] = $this->getType($sLocValue, $iContextId);
  1097. break;
  1098. default:
  1099. $aGhFields[$sRepoField] = $sLocValue;
  1100. }
  1101. $aIssue = bx_srv($sMl, $sMd, [$sGhUsername, $sGhRepository, $mixedAuth, $aContentInfo[$CNF['FIELD_TITLE']], $aContentInfo[$CNF['FIELD_TEXT']], $aGhFields]);
  1102. if($aIssue && is_array($aIssue) && ($iNumber = (int)($aIssue['number'] ?? 0)) != 0 && ($sUrl = $aIssue['html_url'] ?? '') != '') {
  1103. $this->_oDb->updateEntriesBy([
  1104. $CNF['FIELD_GH_ISSUE'] => $iNumber,
  1105. $CNF['FIELD_GH_ISSUE_URL'] => $sUrl,
  1106. ], [$CNF['FIELD_ID'] => $iContentId]);
  1107. $this->logActivity($iContentId, ['key' => '_bx_tasks_txt_msg_synced', 'markers' => [
  1108. 'ghi_link' => $sUrl,
  1109. 'ghi_number' => $iNumber
  1110. ]]);
  1111. }
  1112. }
  1113. }
  1114. }
  1115. public function onExpired($iContentId)
  1116. {
  1117. $CNF = &$this->_oConfig->CNF;
  1118. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  1119. if(!$aContentInfo)
  1120. return;
  1121. $oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTION']);
  1122. if($oConnection) {
  1123. $aProfileIds = $oConnection->getConnectedContent($iContentId);
  1124. if(!empty($aProfileIds) && is_array($aProfileIds))
  1125. foreach($aProfileIds as $iProfileId) {
  1126. /**
  1127. * @hooks
  1128. * @hookdef hook-bx_tasks-expired 'bx_tasks', 'expired' - hook on task unassigned to profile
  1129. * - $unit_name - equals `bx_tasks`
  1130. * - $action - equals `expired`
  1131. * - $object_id - task id
  1132. * - $sender_id - not used
  1133. * - $extra_params - array of additional params with the following array keys:
  1134. * - `object_author_id` - [int] profile_id for task's author
  1135. * - `privacy_view` - [string] privacy view value
  1136. * @hook @ref hook-bx_tasks-expired
  1137. */
  1138. bx_alert($this->getName(), 'expired', $iContentId, getParam('sys_profile_bot'), array(
  1139. 'object_author_id' => $iProfileId,
  1140. 'privacy_view' => $aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]
  1141. ));
  1142. }
  1143. }
  1144. }
  1145. public function isAllowView($iContextId)
  1146. {
  1147. if(!($oContext = BxDolProfile::getInstance($iContextId)) || $oContext->checkAllowedProfileView($iContextId) !== CHECK_ACTION_RESULT_ALLOWED)
  1148. return false;
  1149. return true;
  1150. }
  1151. public function isAllowAdd($iContextId)
  1152. {
  1153. if(!($oContext = BxDolProfile::getInstance($iContextId)) || $oContext->checkAllowedPostInProfile($iContextId) !== CHECK_ACTION_RESULT_ALLOWED)
  1154. return false;
  1155. return true;
  1156. }
  1157. public function isAllowManageByContext($iContextId)
  1158. {
  1159. if(isAdmin())
  1160. return true;
  1161. $oProfileContext = BxDolProfile::getInstance($iContextId);
  1162. if(BxDolService::call($oProfileContext->getModule(), 'is_admin', array($iContextId)))
  1163. return true;
  1164. return false;
  1165. }
  1166. public function isAllowEdit($mixedContent)
  1167. {
  1168. $aContentInfo = !is_array($mixedContent) ? $this->_oDb->getContentInfoById((int)$mixedContent) : $mixedContent;
  1169. if($this->checkAllowedEdit($aContentInfo) === CHECK_ACTION_RESULT_ALLOWED)
  1170. return true;
  1171. return false;
  1172. }
  1173. public function isAllowManage($mixedContent)
  1174. {
  1175. $CNF = &$this->_oConfig->CNF;
  1176. $aContentInfo = !is_array($mixedContent) ? $this->_oDb->getContentInfoById((int)$mixedContent) : $mixedContent;
  1177. if($this->checkAllowedEdit($aContentInfo) === CHECK_ACTION_RESULT_ALLOWED)
  1178. return true;
  1179. if($this->isAllowManageByContext(abs($aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']])))
  1180. return true;
  1181. if(($oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTION'])) !== false) {
  1182. $iLoggedId = bx_get_logged_profile_id();
  1183. $aProfileIds = $oConnection->getConnectedInitiators($aContentInfo[$CNF['FIELD_ID']]);
  1184. if(!empty($aProfileIds) && is_array($aProfileIds) && in_array($iLoggedId, $aProfileIds))
  1185. return true;
  1186. }
  1187. return false;
  1188. }
  1189. /**
  1190. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden. So make sure to make strict(===) checking.
  1191. */
  1192. public function checkAllowedManage ($aDataEntry, $isPerformAction = false)
  1193. {
  1194. return $this->isAllowManage($aDataEntry) ? CHECK_ACTION_RESULT_ALLOWED : _t('_sys_txt_access_denied');
  1195. }
  1196. public function isCompleted($iContentId)
  1197. {
  1198. $CNF = &$this->_oConfig->CNF;
  1199. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  1200. return (bool)$aContentInfo[$CNF['FIELD_COMPLETED']];
  1201. }
  1202. public function complete($iContentId, $iCompleted)
  1203. {
  1204. $CNF = &$this->_oConfig->CNF;
  1205. $iCompleted = (int)$iCompleted;
  1206. $this->_oDb->updateEntriesBy([$CNF['FIELD_COMPLETED'] => $iCompleted], [$CNF['FIELD_ID'] => (int)$iContentId]);
  1207. $sModule = $this->getName();
  1208. $sAction = !$iCompleted ? 'reopened' : 'completed';
  1209. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  1210. $iContentAuthor = (int)$aContentInfo[$CNF['FIELD_AUTHOR']];
  1211. /**
  1212. * @hooks
  1213. * @hookdef hook-bx_tasks-completed 'bx_tasks', 'completed' - hook on task unassigned to profile
  1214. * - $unit_name - equals `bx_tasks`
  1215. * - $action - can be `completed` or `reopened`
  1216. * - $object_id - task id
  1217. * - $sender_id - not used
  1218. * - $extra_params - array of additional params with the following array keys:
  1219. * - `object_author_id` - [int] profile_id for task's author
  1220. * - `privacy_view` - [string] privacy view value
  1221. * @hook @ref hook-bx_tasks-completed
  1222. */
  1223. bx_alert($sModule, $sAction, $iContentId, false, array(
  1224. 'object_author_id' => $iContentAuthor,
  1225. 'privacy_view' => $aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]
  1226. ));
  1227. if(($oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTION'])) !== false) {
  1228. $aProfileIds = $oConnection->getConnectedContent($iContentId);
  1229. if(!empty($aProfileIds) && is_array($aProfileIds))
  1230. foreach($aProfileIds as $iProfileId) {
  1231. if($iProfileId == $iContentAuthor)
  1232. continue;
  1233. bx_alert($sModule, $sAction, $iContentId, false, array(
  1234. 'object_author_id' => $iProfileId,
  1235. 'privacy_view' => $aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]
  1236. ));
  1237. }
  1238. }
  1239. $this->logActivity($iContentId, ['key' => '_bx_tasks_txt_msg_' . ($iCompleted ? '' : 'un') . 'completed']);
  1240. if(($iContextId = (int)$aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]) < 0 && ($mixedRepo = $this->ghGetRepository(abs($iContextId))) !== false) {
  1241. $sModule = 'bx_github';
  1242. $sMethod = ($iCompleted ? 'close' : 'reopen') . '_issue';
  1243. if(bx_is_srv($sModule, $sMethod) && ($iGhIssue = (int)$aContentInfo[$CNF['FIELD_GH_ISSUE']]) != 0) {
  1244. list($sGhUsername, $sGhRepository, $mixedAuth) = $mixedRepo;
  1245. bx_srv($sModule, $sMethod, [$sGhUsername, $sGhRepository, $mixedAuth, $iGhIssue]);
  1246. }
  1247. }
  1248. return true;
  1249. }
  1250. public function logActivity($iContentId, $aMessage)
  1251. {
  1252. $CNF = &$this->_oConfig->CNF;
  1253. $oCmts = BxDolCmts::getObjectInstance($CNF['OBJECT_COMMENTS'], $iContentId);
  1254. if(!$oCmts || !$oCmts->isEnabled())
  1255. return false;
  1256. if(($oAuthor = BxDolProfile::getInstance()) !== false)
  1257. $aMessage['markers'] = array_merge($aMessage['markers'] ?? [], [
  1258. 'author_link' => $oAuthor->getUrl(),
  1259. 'author_name' => $oAuthor->getDisplayName(),
  1260. ]);
  1261. return $oCmts->addAuto($aMessage);
  1262. }
  1263. public function ghGetRepository($iContextId)
  1264. {
  1265. $mixedRepo = $this->_oDb->getContextRepository($iContextId);
  1266. if($mixedRepo === false)
  1267. return false;
  1268. return [
  1269. $mixedRepo['gh_username'],
  1270. $mixedRepo['gh_repository'],
  1271. $mixedRepo['gh_app_id'] ? [$this->_iProfileId, (int)$mixedRepo['gh_app_id']] : $this->_iProfileId
  1272. ];
  1273. }
  1274. public function ghUpdateIssue($mixedContent, $aGhFields = [])
  1275. {
  1276. $aContentInfo = is_array($mixedContent) ? $mixedContent : $this->_oDb->getContentInfoById((int)$mixedContent);
  1277. if(!$aContentInfo || !is_array($aContentInfo))
  1278. return false;
  1279. $CNF = &$this->_oConfig->CNF;
  1280. $iContentId = (int)$aContentInfo[$CNF['FIELD_ID']];
  1281. $iContextId = (int)$aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']];
  1282. if($iContextId >= 0)
  1283. return false;
  1284. $mixedRepo = $this->ghGetRepository(abs($iContextId));
  1285. if($mixedRepo === false)
  1286. return false;
  1287. list($sGhUsername, $sGhRepository, $mixedAuth) = $mixedRepo;
  1288. $iGhIssue = (int)$aContentInfo[$CNF['FIELD_GH_ISSUE']];
  1289. $sMl = 'bx_github';
  1290. if($aGhFields && ($sMd = 'update_issue') && bx_is_srv($sMl, $sMd))
  1291. if(bx_srv($sMl, $sMd, [$sGhUsername, $sGhRepository, $mixedAuth, $iGhIssue, $aGhFields]) !== false)
  1292. $this->logActivity($iContentId, ['key' => '_bx_tasks_txt_msg_synced', 'markers' => [
  1293. 'ghi_link' => $aContentInfo[$CNF['FIELD_GH_ISSUE_URL']],
  1294. 'ghi_number' => $iGhIssue
  1295. ]]);
  1296. return true;
  1297. }
  1298. public function getStickers($mixedValues, $iContextId = 0)
  1299. {
  1300. $CNF = &$this->_oConfig->CNF;
  1301. $aValues = is_array($mixedValues) ? $mixedValues : BxDolFormCheckerHelper::displaySet($mixedValues);
  1302. $aResults = [];
  1303. if($iContextId) {
  1304. $aStickers = $this->_oDb->getPreValues([
  1305. 'sample' => 'context_list',
  1306. 'context_id' => $iContextId,
  1307. 'list' => 'sticker'
  1308. ]);
  1309. foreach($aValues as $sValue)
  1310. if(($aSticker = $aStickers[$sValue] ?? false))
  1311. $aResults[] = [
  1312. 'title' => $aSticker['title'],
  1313. 'color' => $aSticker['color']
  1314. ];
  1315. }
  1316. if(!$aResults) {
  1317. $aStickers = BxDolForm::getDataItems($CNF['OBJECT_PRE_LIST_STICKERS']);
  1318. foreach($aValues as $sValue)
  1319. $aResults[] = [
  1320. 'title' => $aStickers[$sValue],
  1321. 'color' => ''
  1322. ];
  1323. }
  1324. return $aResults;
  1325. }
  1326. public function getType($mixedValue, $iContextId = 0)
  1327. {
  1328. $sResult = '';
  1329. if($iContextId) {
  1330. $aType = $this->_oDb->getPreValues([
  1331. 'sample' => 'context_list_value',
  1332. 'context_id' => $iContextId,
  1333. 'list' => 'type',
  1334. 'value' => $mixedValue
  1335. ]);
  1336. if($aType && is_array($aType))
  1337. $sResult = $aType['title'];
  1338. }
  1339. if(!$sResult && ($sTypeTitle = $this->_oConfig->getTypeTitle($mixedValue)) != '')
  1340. $sResult = _t($sTypeTitle);
  1341. return $sResult;
  1342. }
  1343. public function applyFilter($iContextId, $iFilterId)
  1344. {
  1345. $sCookieKey = $this->_oConfig->CNF['COOKIE_SETTING_KEY'];
  1346. $aFilters = [];
  1347. if(isset($_COOKIE[$sCookieKey]))
  1348. $aFilters = json_decode($_COOKIE[$sCookieKey], true);
  1349. if(($iFilterId = (int)$iFilterId) != 0)
  1350. $aFilters[$iContextId] = $iFilterId;
  1351. else
  1352. unset($aFilters[$iContextId]);
  1353. bx_setcookie($sCookieKey, json_encode($aFilters), time() + 60 * 60 * 24 * 365);
  1354. }
  1355. public function setAssignments($iContentId, $aMembers)
  1356. {
  1357. $CNF = &$this->_oConfig->CNF;
  1358. $oConn = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTION']);
  1359. if($this->_bIsApi && is_string($aMembers))
  1360. $aMembers = explode(',', $aMembers);
  1361. $aMembersCurrent = $oConn->getConnectedInitiators($iContentId);
  1362. $aMembersToAdd = [];
  1363. $aMembersToRemove = $aMembersCurrent;
  1364. if (is_array($aMembers)){
  1365. $aMembersToAdd = array_diff($aMembers, $aMembersCurrent);
  1366. $aMembersToRemove = array_diff($aMembersCurrent, $aMembers);
  1367. }
  1368. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  1369. foreach($aMembersToAdd as $iProfileId){
  1370. $oConn->addConnection($iProfileId, $iContentId);
  1371. /**
  1372. * @hooks
  1373. * @hookdef hook-bx_tasks-assigned 'bx_tasks', 'assigned' - hook on task assigned to profile
  1374. * - $unit_name - equals `bx_tasks`
  1375. * - $action - equals `assigned`
  1376. * - $object_id - task id
  1377. * - $sender_id - not used
  1378. * - $extra_params - array of additional params with the following array keys:
  1379. * - `object_author_id` - [int] id for assigned profile
  1380. * - `privacy_view` - [string] privacy view value
  1381. * @hook @ref hook-bx_tasks-assigned
  1382. */
  1383. bx_alert($this->_aModule['name'], 'assigned', $iContentId, false, array(
  1384. 'object_author_id' => $iProfileId,
  1385. 'privacy_view' => $aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]
  1386. ));
  1387. }
  1388. foreach($aMembersToRemove as $iProfileId){
  1389. $oConn->removeConnection($iProfileId, $iContentId);
  1390. /**
  1391. * @hooks
  1392. * @hookdef hook-bx_tasks-unassigned 'bx_tasks', 'unassigned' - hook on task unassigned to profile
  1393. * - $unit_name - equals `bx_tasks`
  1394. * - $action - equals `unassigned`
  1395. * - $object_id - task id
  1396. * - $sender_id - not used
  1397. * - $extra_params - array of additional params with the following array keys:
  1398. * - `object_author_id` - [int] id for unassigned profile
  1399. * - `privacy_view` - [string] privacy view value
  1400. * @hook @ref hook-bx_tasks-unassigned
  1401. */
  1402. bx_alert($this->_aModule['name'], 'unassigned', $iContentId, false, array(
  1403. 'object_author_id' => $iProfileId,
  1404. 'privacy_view' => $aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]
  1405. ));
  1406. }
  1407. }
  1408. /**
  1409. * Internal methods
  1410. */
  1411. protected function _onEditProperty($aContentInfo, $sProperty, $mixedValue)
  1412. {
  1413. $CNF = &$this->_oConfig->CNF;
  1414. $aPropLoc2Repo = [
  1415. $CNF['FIELD_TYPE'] => 'type',
  1416. ];
  1417. $iContentId = (int)$aContentInfo[$CNF['FIELD_ID']];
  1418. if(($sMethod = 'get' . bx_gen_method_name($sProperty) . 'Title') && method_exists($this->_oConfig, $sMethod))
  1419. $mixedValue = $this->_oConfig->$sMethod($mixedValue);
  1420. $this->logActivity($iContentId, ['key' => '_bx_tasks_txt_msg_edit_' . $sProperty, 'markers' => ['value' => $mixedValue]]);
  1421. if(($sPropRepo = $aPropLoc2Repo[$sProperty] ?? false))
  1422. $this->ghUpdateIssue($aContentInfo, [
  1423. $sPropRepo => _t($mixedValue)
  1424. ]);
  1425. }
  1426. protected function _onEditState($aContentInfo, $iState)
  1427. {
  1428. $CNF = &$this->_oConfig->CNF;
  1429. $iContentId = (int)$aContentInfo[$CNF['FIELD_ID']];
  1430. $this->logActivity($iContentId, ['key' => '_bx_tasks_txt_msg_edit_state', 'markers' => ['value' => $this->_oConfig->getStateTitle($iState)]]);
  1431. $iCompleted = (int)$this->_oConfig->isCompleted($iState);
  1432. if($iCompleted != (int)$this->_oConfig->isCompleted($aContentInfo[$CNF['FIELD_STATE']]) || $iCompleted != (int)$aContentInfo[$CNF['FIELD_COMPLETED']])
  1433. $this->complete($iContentId, $iCompleted);
  1434. }
  1435. protected function _onEditInitialMembers($aContentInfo, $mixedIds)
  1436. {
  1437. $CNF = &$this->_oConfig->CNF;
  1438. if(is_string($mixedIds))
  1439. $mixedIds = explode(',', $mixedIds);
  1440. $iContentId = (int)$aContentInfo[$CNF['FIELD_ID']];
  1441. $aValue = [];
  1442. if(($oProfile = BxDolProfile::getInstance()) !== false)
  1443. foreach($mixedIds as $iId)
  1444. $aValue[] = $oProfile->getDisplayName($iId);
  1445. $this->logActivity($iContentId, ['key' => '_bx_tasks_txt_msg_edit_initial_members', 'markers' => ['value' => implode(', ', $aValue)]]);
  1446. $this->setAssignments($iContentId, $mixedIds);
  1447. }
  1448. protected function _getFilterConditions($iContextId = 0, $aItems = [])
  1449. {
  1450. $iJoins = 0;
  1451. $aResult = [
  1452. 'join' => [],
  1453. 'where' => []
  1454. ];
  1455. if(count($aItems) > 1) {
  1456. $aResult['where'] = ['grp' => true, 'o' => 'AND', 'cnds' => []];
  1457. foreach($aItems as $aItem)
  1458. if(($mixedCnd = $this->_getFilterCondition($iContextId, $aItem)) !== false) {
  1459. if(isset($mixedCnd['join'])) {
  1460. $iJoins++;
  1461. $aResult['join'][] = $mixedCnd['join'];
  1462. }
  1463. if(isset($mixedCnd['where']))
  1464. $aResult['where']['cnds'][] = $mixedCnd['where'];
  1465. }
  1466. }
  1467. else {
  1468. $mixedCnd = $this->_getFilterCondition($iContextId, reset($aItems));
  1469. if(isset($mixedCnd['join'])) {
  1470. $iJoins++;
  1471. $aResult['join'][] = $mixedCnd['join'];
  1472. }
  1473. if(isset($mixedCnd['where']))
  1474. $aResult['where'] = $mixedCnd['where'];
  1475. }
  1476. if($iJoins == 1)
  1477. $aResult['join'] = reset($aResult['join']);
  1478. else if($iJoins > 1)
  1479. $aResult['join'] = ['grp' => true, 'cnds' => $aResult['join']];
  1480. return $aResult;
  1481. }
  1482. protected function _getFilterCondition($iContextId = 0, $aItem = [])
  1483. {
  1484. $sMethod = '_getFilterCondition' . bx_gen_method_name($aItem['f']);
  1485. if(method_exists($this, $sMethod))
  1486. return $this->$sMethod($iContextId, $aItem);
  1487. $mixedV = $mixedO = false;
  1488. if(!($bArray = is_array($aItem['v'])) || count($aItem['v']) == 1) {
  1489. $mixedV = $bArray ? reset($aItem['v']) : $aItem['v'];
  1490. $mixedO = '=';
  1491. }
  1492. else {
  1493. $mixedV = $aItem['v'];
  1494. $mixedO = !($aItem['r'] ?? 0) ? 'IN' : 'NOT IN';
  1495. }
  1496. if(!$mixedV || !$mixedO)
  1497. return false;
  1498. return [
  1499. 'where' => ['cnd' => true, 't' => 'te', 'f' => $aItem['f'], 'v' => $mixedV, 'o' => $mixedO]
  1500. ];
  1501. }
  1502. protected function _getFilterConditionInitialMembers ($iContextId = 0, $aItem = [])
  1503. {
  1504. $CNF = &$this->_oConfig->CNF;
  1505. $mixedV = $mixedO = false;
  1506. if(!($bArray = is_array($aItem['v'])) || count($aItem['v']) == 1) {
  1507. $mixedV = $bArray ? reset($aItem['v']) : $aItem['v'];
  1508. $mixedO = '=';
  1509. }
  1510. else {
  1511. $mixedV = $aItem['v'];
  1512. $mixedO = !($aItem['r'] ?? 0) ? 'IN' : 'NOT IN';
  1513. }
  1514. return [
  1515. 'join' => ['cnd' => true, 'j' => 'LEFT', 'tj' => $CNF['TABLE_ASSIGNMENTS'], 'taj' => 'ta', 'fj' => 'content', 'tam' => 'te', 'fm' => 'id'],
  1516. 'where' => ['cnd' => true, 't' => 'ta', 'f' => 'initiator', 'v' => $mixedV, 'o' => $mixedO]
  1517. ];
  1518. }
  1519. protected function _getFilterForm($iContextId = 0)
  1520. {
  1521. $CNF = &$this->_oConfig->CNF;
  1522. $sJsObject = $this->_oConfig->getJsObject('tasks');
  1523. $aFieldToList = [
  1524. $CNF['FIELD_TYPE'] => $CNF['OBJECT_PRE_LIST_TYPES'],
  1525. $CNF['FIELD_PRIORITY'] => $CNF['OBJECT_PRE_LIST_PRIORITIES'],
  1526. $CNF['FIELD_ESTIMATE'] => $CNF['OBJECT_PRE_LIST_ESTIMATES'],
  1527. $CNF['FIELD_STATE'] => $CNF['OBJECT_PRE_LIST_STATES']
  1528. ];
  1529. $aForm = [
  1530. 'form_attrs' => [
  1531. 'id' => 'bx-tasks-filter-add',
  1532. 'action' => BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'create_filter/' . $iContextId,
  1533. 'method' => BX_DOL_FORM_METHOD_DEFAULT
  1534. ],
  1535. 'params' => [
  1536. 'db' => [
  1537. 'table' => $CNF['TABLE_FILTERS'],
  1538. 'key' => 'id',
  1539. 'uri' => '',
  1540. 'uri_title' => '',
  1541. 'submit_name' => 'do_apply'
  1542. ],
  1543. ],
  1544. 'inputs' => [
  1545. $CNF['FIELD_AUTHOR'] => [
  1546. 'type' => 'checkbox_set',
  1547. ],
  1548. $CNF['FIELD_INITIAL_MEMBERS'] => [
  1549. 'type' => 'checkbox_set',
  1550. ],
  1551. $CNF['FIELD_TYPE'] => [
  1552. 'type' => 'checkbox_set',
  1553. ],
  1554. $CNF['FIELD_PRIORITY'] => [
  1555. 'type' => 'checkbox_set',
  1556. ],
  1557. $CNF['FIELD_ESTIMATE'] => [
  1558. 'type' => 'checkbox_set',
  1559. ],
  1560. $CNF['FIELD_STATE'] => [
  1561. 'type' => 'checkbox_set',
  1562. ],
  1563. 'save_me' => [
  1564. 'type' => 'switcher',
  1565. 'name' => 'save_me',
  1566. 'caption' => _t('_bx_tasks_form_f_input_save_me'),
  1567. 'value' => 'on',
  1568. 'attrs' => [
  1569. 'onchange' => $sJsObject . '.onChangeSave(this)'
  1570. ]
  1571. ],
  1572. 'save_all' => [
  1573. 'type' => 'switcher',
  1574. 'name' => 'save_all',
  1575. 'caption' => _t('_bx_tasks_form_f_input_save_all'),
  1576. 'value' => 'on',
  1577. 'attrs' => [
  1578. 'onchange' => $sJsObject . '.onChangeSave(this)'
  1579. ]
  1580. ],
  1581. 'title' => [
  1582. 'type' => 'text',
  1583. 'name' => 'title',
  1584. 'caption' => _t('_bx_tasks_form_f_input_title'),
  1585. 'value' => '',
  1586. 'required' => '0',
  1587. 'tr_attrs' => [
  1588. 'class' => 'bx-tasks-ffi-hidden'
  1589. ]
  1590. ],
  1591. 'controls' => [
  1592. 'name' => 'controls',
  1593. 'type' => 'input_set',
  1594. [
  1595. 'type' => 'submit',
  1596. 'name' => 'do_apply',
  1597. 'value' => _t('_bx_tasks_form_f_input_apply'),
  1598. ], [
  1599. 'type' => 'reset',
  1600. 'name' => 'do_cancel',
  1601. 'value' => _t('_bx_tasks_form_f_input_cancel'),
  1602. 'attrs' => [
  1603. 'onclick' => "$('.bx-popup-applied:visible').dolPopupHide()",
  1604. 'class' => 'bx-def-margin-sec-left',
  1605. ],
  1606. ]
  1607. ]
  1608. ]
  1609. ];
  1610. foreach($aForm['inputs'] as $sName => $aInput) {
  1611. if(isset($aInput['name']))
  1612. continue;
  1613. $aForm['inputs'][$sName] = array_merge($aForm['inputs'][$sName], [
  1614. 'name' => $sName,
  1615. 'caption' => _t('_bx_tasks_form_f_input_' . $sName),
  1616. 'value' => '',
  1617. 'values' => []
  1618. ]);
  1619. if(($sList = $aFieldToList[$sName] ?? false) && ($aList = BxDolForm::getDataItems($sList))) {
  1620. if(isset($aList['']))
  1621. unset($aList['']);
  1622. $aForm['inputs'][$sName]['values'] = $aList;
  1623. }
  1624. }
  1625. $oContext = false;
  1626. $sContextModule = '';
  1627. if($iContextId && ($oContext = BxDolProfile::getInstance($iContextId)) !== false)
  1628. $sContextModule = $oContext->getModule();
  1629. foreach([$CNF['FIELD_AUTHOR'], $CNF['FIELD_INITIAL_MEMBERS']] as $sK){
  1630. if(!isset($aForm['inputs'][$sK]))
  1631. continue;
  1632. if($oContext !== false) {
  1633. $aForm['inputs'][$sK]['values']['{logged_pid}'] = _t('_bx_tasks_txt_flt_user_current');
  1634. $aMembers = bx_srv($sContextModule, 'fans', [$oContext->getContentId(), true]);
  1635. foreach($aMembers as $iMember)
  1636. if(($iMember != $this->_iProfileId) && ($oMember = BxDolProfile::getInstance($iMember)) !== false && !($oMember instanceof BxDolProfileUndefined))
  1637. $aForm['inputs'][$sK]['values'][$iMember] = $oMember->getDisplayName();
  1638. }
  1639. else
  1640. unset($aForm['inputs'][$sK]);
  1641. }
  1642. if(!$oContext || !bx_srv($sContextModule, 'is_admin', [$iContextId, $this->_iProfileId]))
  1643. unset($aForm['inputs']['save_all']);
  1644. return new BxTemplFormView($aForm);
  1645. }
  1646. protected function _getSaveFilterForm($iContextId = 0)
  1647. {
  1648. $CNF = &$this->_oConfig->CNF;
  1649. $aForm = [
  1650. 'form_attrs' => [
  1651. 'id' => 'bx-tasks-filter-save',
  1652. 'action' => BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'save_filter/' . $iContextId,
  1653. 'method' => BX_DOL_FORM_METHOD_DEFAULT
  1654. ],
  1655. 'params' => [
  1656. 'db' => [
  1657. 'table' => $CNF['TABLE_FILTERS'],
  1658. 'key' => 'id',
  1659. 'uri' => '',
  1660. 'uri_title' => '',
  1661. 'submit_name' => 'do_save'
  1662. ],
  1663. ],
  1664. 'inputs' => [
  1665. 'save_all' => [
  1666. 'type' => 'switcher',
  1667. 'name' => 'save_all',
  1668. 'caption' => _t('_bx_tasks_form_f_input_save_all'),
  1669. 'value' => 'on',
  1670. ],
  1671. 'title' => [
  1672. 'type' => 'text',
  1673. 'name' => 'title',
  1674. 'caption' => _t('_bx_tasks_form_f_input_title'),
  1675. 'value' => '',
  1676. 'required' => '0',
  1677. ],
  1678. 'controls' => [
  1679. 'name' => 'controls',
  1680. 'type' => 'input_set',
  1681. [
  1682. 'type' => 'submit',
  1683. 'name' => 'do_save',
  1684. 'value' => _t('_bx_tasks_form_f_input_save'),
  1685. ], [
  1686. 'type' => 'reset',
  1687. 'name' => 'do_cancel',
  1688. 'value' => _t('_bx_tasks_form_f_input_cancel'),
  1689. 'attrs' => [
  1690. 'onclick' => "$('.bx-popup-applied:visible').dolPopupHide()",
  1691. 'class' => 'bx-def-margin-sec-left',
  1692. ],
  1693. ]
  1694. ]
  1695. ]
  1696. ];
  1697. $oContext = false;
  1698. $sContextModule = '';
  1699. if($iContextId && ($oContext = BxDolProfile::getInstance($iContextId)) !== false)
  1700. $sContextModule = $oContext->getModule();
  1701. if(!$oContext || !bx_srv($sContextModule, 'is_admin', [$iContextId, $this->_iProfileId]))
  1702. unset($aForm['inputs']['save_all']);
  1703. return new BxTemplFormView($aForm);
  1704. }
  1705. }
  1706. /** @} */