BxTasksModule.php 74 KB

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