BxTasksModule.php 66 KB

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