BxTasksModule.php 79 KB

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