BxTasksModule.php 70 KB

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