BxTasksModule.php 65 KB

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