1
0

BxTasksModule.php 83 KB

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