zentao.bot.php 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015
  1. <?php
  2. class zentaoBot extends xuanBot
  3. {
  4. /**
  5. * Bot name.
  6. *
  7. * @var string
  8. * @access public
  9. */
  10. public $name = 'ZenTao';
  11. /**
  12. * Bot codename.
  13. *
  14. * @var string
  15. * @access public
  16. */
  17. public $code = 'zentao';
  18. /**
  19. * Command list, inits in init() since all commands are internal commands.
  20. *
  21. * @var array
  22. * @access public
  23. */
  24. public $commands = array();
  25. /**
  26. * Construct function, load lang of zentao and setup commands.
  27. *
  28. * @access public
  29. * @return void
  30. */
  31. public function __construct()
  32. {
  33. if(isset($_SESSION['clientLang']))
  34. {
  35. if($_SESSION['clientLang'] == 'zh-cn') $this->name = '禅道';
  36. if($_SESSION['clientLang'] == 'zh-tw') $this->name = '禪道';
  37. }
  38. }
  39. /**
  40. * Init bot, load model.
  41. *
  42. * @access public
  43. * @return void
  44. */
  45. public function init()
  46. {
  47. $this->lang = $this->im->lang->im->bot->zentaoBot;
  48. foreach(array('view', 'start', 'close', 'finish') as $command)
  49. {
  50. $this->commands[] = array('command' => $command, 'alias' => $this->lang->commands->$command->alias, 'description' => $this->lang->commands->$command->description, 'internal' => true);
  51. }
  52. /* Backup user model of im module, load user module and restore. Use `$this->userModel` as user module from now on. */
  53. $imUser = $this->im->user;
  54. $this->userModel = $this->im->loadModel('user');
  55. $this->im->user = $imUser;
  56. $this->im->loadModel('task');
  57. $this->im->app->loadClass('pager', $static = true);
  58. $this->users = array_filter($this->userModel->getPairs('noclosed|noletter'));
  59. $this->taskStatusList = array_filter($this->im->lang->task->statusList);
  60. $this->help = $this->lang->help;
  61. $this->inited = true;
  62. }
  63. /**
  64. * Close command.
  65. *
  66. * @param array $args bot command arguments
  67. * @access public
  68. * @return object|string
  69. */
  70. public function close($args = array())
  71. {
  72. $args = $this->removeEqualSign($args);
  73. foreach($args as $key => $value)
  74. {
  75. if(in_array(strtolower($value), $this->lang->condKeywords['task'])) unset($args[$key]);
  76. }
  77. $task = $this->closeTask($args);
  78. return $task;
  79. }
  80. /**
  81. * Finish command.
  82. *
  83. * @param array $args bot command arguments
  84. * @access public
  85. * @return object|string
  86. */
  87. public function finish($args = array())
  88. {
  89. $originArgs = $args;
  90. $args = array_filter(explode('=', implode('=', $args)));
  91. foreach($args as $key => $value)
  92. {
  93. if(in_array(strtolower($value), $this->lang->condKeywords['task'])) unset($args[$key]);
  94. }
  95. return $this->finishTask($args, $originArgs);
  96. }
  97. /**
  98. * View command.
  99. *
  100. * @param array $args bot command arguments
  101. * @param int $userID
  102. * @param object $user
  103. * @access public
  104. * @return object|string
  105. */
  106. public function view($args = array(), $userID = 0, $user = null)
  107. {
  108. $pager = pager::init(0, 10, 1);
  109. $pageSearchRegex = $this->lang->pageSearchRegex;
  110. $pageIDKeywords = $this->lang->condKeywords['pageID'];
  111. $args = array_filter($args, function($arg) use ($pager, $pageSearchRegex, $pageIDKeywords)
  112. {
  113. $matches = array();
  114. preg_match($pageSearchRegex, $arg, $matches);
  115. if(count($matches) > 2)
  116. {
  117. if(in_array(strtolower($matches[1]), $pageIDKeywords))
  118. {
  119. $pager->pageID = $matches[2];
  120. }
  121. else
  122. {
  123. $pager->recPerPage = $matches[2];
  124. }
  125. return false;
  126. }
  127. return true;
  128. });
  129. $originArgs = $args;
  130. $args = $this->removeEqualSign($args);
  131. foreach($args as $key => $value)
  132. {
  133. if(in_array(strtolower($value), $this->lang->condKeywords['task']))
  134. {
  135. unset($args[$key]);
  136. $tasks = $this->viewTask($args, $user, $pager);
  137. return $this->renderTask($tasks, $originArgs, $pager);
  138. }
  139. }
  140. }
  141. /**
  142. * Parse bot command arguments.
  143. *
  144. * @param array $args bot command arguments
  145. * @param array $keys
  146. * @access public
  147. * @return object
  148. */
  149. public function parseArguments($args = array(), $keys = array())
  150. {
  151. $assignedToList = '';
  152. $priList = '';
  153. $statusList = '';
  154. $idList = '';
  155. $taskName = '';
  156. $comment = '';
  157. while(!empty($args))
  158. {
  159. $arg = array_shift($args);
  160. if(isset($keys['pri']))
  161. {
  162. if(in_array(strtolower($arg), $this->lang->condKeywords['pri']))
  163. {
  164. $pris = array_shift($args);
  165. $priList = $this->buildCondParamByModifier($pris, $priList, 'p');
  166. continue;
  167. }
  168. if(preg_match('/p[1-4]/i', $arg) == 1)
  169. {
  170. $priList = $this->buildCondParamByModifier($arg, $priList, 'p');
  171. continue;
  172. }
  173. }
  174. if(isset($keys['id']))
  175. {
  176. if(in_array(strtolower($arg), $this->lang->condKeywords['id']))
  177. {
  178. $ids = array_shift($args);
  179. $idList .= $this->buildCondParamByModifier($ids, $idList, '#');
  180. continue;
  181. }
  182. if(preg_match('/#\d+/', $arg) == 1)
  183. {
  184. $idList .= $this->buildCondParamByModifier($arg, $idList, '#');
  185. continue;
  186. }
  187. }
  188. if(isset($keys['status']))
  189. {
  190. if(in_array(strtolower($arg), $this->lang->condKeywords['status']))
  191. {
  192. $statuses = array_shift($args);
  193. $statusList = $this->buildCondParam($statuses, $statusList, $this->taskStatusList);
  194. continue;
  195. }
  196. if($this->checkCondType($arg, $this->taskStatusList))
  197. {
  198. $statusList = $this->buildCondParam($arg, $statusList, $this->taskStatusList);
  199. continue;
  200. }
  201. }
  202. if(isset($keys['assignTo']))
  203. {
  204. if(in_array(strtolower($arg), $this->lang->condKeywords['assignTo']))
  205. {
  206. $usernames = array_shift($args);
  207. $assignedToList = $this->buildCondParam($usernames, $assignedToList, $this->users);
  208. continue;
  209. }
  210. if($this->checkCondType($arg, $this->users))
  211. {
  212. $assignedToList = $this->buildCondParam($arg, $assignedToList, $this->users);
  213. continue;
  214. }
  215. }
  216. if(isset($keys['comment']))
  217. {
  218. if(in_array(strtolower($arg), $this->lang->condKeywords['comment']))
  219. {
  220. $comment = array_shift($args);
  221. }
  222. else {
  223. $comment = $arg;
  224. }
  225. continue;
  226. }
  227. if(isset($keys['taskName']))
  228. {
  229. if(in_array(strtolower($arg), $this->lang->condKeywords['taskName']))
  230. {
  231. $taskName = array_shift($args);
  232. }
  233. else
  234. {
  235. $taskName = $arg;
  236. }
  237. }
  238. }
  239. $conds = new stdClass();
  240. if(isset($keys['pri'])) $conds->priList = trim($priList, ',');
  241. if(isset($keys['assignTo'])) $conds->assignedToList = trim($assignedToList, ',');
  242. if(isset($keys['status'])) $conds->statusList = trim($statusList, ',');
  243. if(isset($keys['id'])) $conds->idList = trim($idList, ',');
  244. if(isset($keys['taskName'])) $conds->taskName = $taskName;
  245. if(isset($keys['comment'])) $conds->comment = $comment;
  246. return $conds;
  247. }
  248. /**
  249. * Check condition type.
  250. *
  251. * @param string $arg
  252. * @param array $list
  253. * @access public
  254. * @return bool
  255. */
  256. public function checkCondType($arg, $list)
  257. {
  258. $args = explode(',', $arg);
  259. $array_inter = array_intersect($args, $list);
  260. if(!empty($array_inter)) return true;
  261. $list = array_flip($list);
  262. $array_inter = array_intersect($args, $list);
  263. if(!empty($array_inter)) return true;
  264. return false;
  265. }
  266. /**
  267. * Build condition param.
  268. *
  269. * @param string $arg bot command arguments
  270. * @param string $condParam
  271. * @param array $condArr
  272. * @access public
  273. * @return string
  274. */
  275. public function buildCondParam($arg, $condParam, $condArr)
  276. {
  277. $arr = explode(',', $arg);
  278. foreach($arr as $cond)
  279. {
  280. if(array_key_exists($cond, $condArr))
  281. {
  282. $condParam .= ',' . $cond;
  283. }
  284. else
  285. {
  286. $key = array_search($cond, $condArr);
  287. if($key !== false) $condParam .= ',' . $key;
  288. }
  289. }
  290. return $condParam;
  291. }
  292. /**
  293. * Remove equal sign from bot command arguments.
  294. *
  295. * @param array $args bot command arguments
  296. * @access public
  297. * @return array
  298. */
  299. public function removeEqualSign($args)
  300. {
  301. return array_filter(explode('=', implode('=', $args)));
  302. }
  303. /**
  304. * Build condition param by modifier.
  305. *
  306. * @param array $args bot command arguments
  307. * @param string $condParam
  308. * @param string $modifier
  309. * @access public
  310. * @return string
  311. */
  312. public function buildCondParamByModifier($arg, $condParam, $modifier)
  313. {
  314. $arg = strtolower($arg);
  315. $arg = str_replace($modifier, '', $arg);
  316. $condParam .= ',' . $arg;
  317. return $condParam;
  318. }
  319. /**
  320. * View task command.
  321. *
  322. * @param array $args bot command arguments
  323. * @param object $user
  324. * @param object $pager
  325. * @access public
  326. * @return array
  327. */
  328. public function viewTask($args = array(), $user = null, $pager = null)
  329. {
  330. $keys = array();
  331. foreach(array('pri', 'id', 'status', 'assignTo', 'taskName') as $key) $keys[$key] = true;
  332. if(empty($args))
  333. {
  334. $args['assignTo'] = is_object($user) ? $user->account : false;
  335. $args['status'] = 'wait,doing,done,pause,cancel';
  336. }
  337. $conds = $this->parseArguments($args, $keys);
  338. $conds->search = 1;
  339. $conds->order = 'status_asc';
  340. $conds->limit = 10;
  341. if(is_object($pager))
  342. {
  343. $conds->page = $pager->pageID;
  344. $conds->limit = $pager->recPerPage;
  345. }
  346. $result = $this->loadEntry('tasks', 'get', array(), (array)$conds);
  347. if(isset($result->tasks))
  348. {
  349. $pager->setRecTotal($result->total);
  350. $pager->setRecPerPage($result->limit);
  351. $pager->setPageID($result->page);
  352. $pager->setPageTotal();
  353. return $result->tasks;
  354. }
  355. return array();
  356. }
  357. /**
  358. * Close task command.
  359. *
  360. * @param array $args bot command arguments
  361. * @access public
  362. * @return object|string
  363. */
  364. public function closeTask($args)
  365. {
  366. $keys = array();
  367. $keys['id'] = true;
  368. $keys['comment'] = true;
  369. $conds = $this->parseArguments($args, $keys);
  370. $tasks = array_filter(explode(',', $conds->idList));
  371. $taskID = array_pop($tasks);
  372. $comment = $conds->comment;
  373. if(!is_numeric($taskID)) return $this->lang->errors->invalidCommand;
  374. $task = $this->loadEntry('task', 'get', array('taskID'=> $taskID));
  375. if(empty($task)) return $this->lang->errors->emptyResult;
  376. if($task->status != 'done' && $task->status != 'cancel') return sprintf($this->lang->errors->invalidStatus, $this->taskStatusList[$task->status]);
  377. $task = $this->loadEntry('taskclose', 'post', array('taskID' => $taskID, 'comment' => $comment));
  378. if(isset($task->result) && $task->result == 'fail') return $task->message;
  379. $link = str_replace('x.php', 'index.php', helper::createLink('task', 'view', "taskID=$taskID", 'html'));
  380. $messages = new stdClass();
  381. $messages->type = 'url';
  382. $messages->url = common::getSysURL() . $link;
  383. return array($this->lang->success, $messages);
  384. }
  385. /**
  386. * Finish task.
  387. *
  388. * @param array $args
  389. * @param array $originArgs
  390. * @access public
  391. * @return object|string
  392. */
  393. private function finishTask($args, $originArgs)
  394. {
  395. $taskID = '';
  396. $params = array();
  397. while(!empty($args))
  398. {
  399. $arg = array_shift($args);
  400. if(in_array(strtolower($arg), $this->lang->condKeywords['id']))
  401. {
  402. $taskID = array_shift($args);
  403. $taskID = str_replace('#', '', $taskID);
  404. }
  405. elseif(preg_match('/#\d+/', $arg) == 1)
  406. {
  407. $taskID = str_replace('#', '', $arg);
  408. }
  409. else
  410. {
  411. parse_str(str_replace('amp;', '', end($originArgs)), $params);
  412. }
  413. }
  414. if(!is_numeric($taskID)) return $this->lang->errors->invalidCommand;
  415. $task = $this->loadEntry('task', 'get', array('taskID'=> $taskID));
  416. if(!common::hasPriv('task', 'finish')) return $this->lang->errors->unauthorized;
  417. if(empty($task) || isset($task->error)) return $this->lang->errors->emptyResult;
  418. if($task->status != 'wait' && $task->status != 'doing') return sprintf($this->lang->errors->invalidStatus, $this->taskStatusList[$task->status]);
  419. $reply = new stdClass();
  420. $reply->messages = array();
  421. $reply->responses = array();
  422. $realStarted = $this->formatDate($task, 'realStarted');
  423. $finishedDate = $this->formatDate($task, 'finishedDate');
  424. if(empty($params))
  425. {
  426. $originCommand = rawurlencode($this->lang->finishCommand . ' ' . implode(' ', $originArgs));
  427. $json = (object)array
  428. (
  429. 'title' => "#{$taskID} {$task->name}",
  430. 'submitLabel' => $this->im->lang->task->finish,
  431. 'inputs' =>
  432. array(
  433. (object)array
  434. (
  435. 'name' => $this->im->lang->task->hasConsumed,
  436. 'type' => 'input',
  437. 'label' => $this->im->lang->task->hasConsumed,
  438. 'value' => $task->consumed,
  439. 'disabled' => true,
  440. 'addon' => $this->im->lang->task->hour,
  441. ),
  442. (object)array
  443. (
  444. 'name' => $this->im->lang->task->currentConsumed,
  445. 'type' => 'input',
  446. 'label' => $this->im->lang->task->currentConsumed,
  447. 'value' => 0,
  448. 'addon' => $this->im->lang->task->hour,
  449. ),
  450. (object)array
  451. (
  452. 'name' => $this->im->lang->task->realStarted,
  453. 'type' => 'datetime-local',
  454. 'label' => $this->im->lang->task->realStarted,
  455. 'value' => $realStarted,
  456. ),
  457. (object)array
  458. (
  459. 'name' => $this->im->lang->task->finishedDate,
  460. 'type' => 'datetime-local',
  461. 'label' => $this->im->lang->task->finishedDate,
  462. 'value' => $finishedDate,
  463. ),
  464. ),
  465. );
  466. $json = rawurlencode(json_encode($json));
  467. $reply->messages[] = $this->lang->finish->tip;
  468. $reply->messages[] = "[{$this->lang->finish->tipLinkTitle}](xxc://openFormAndSendToServerBySendbox/$json/$originCommand)";
  469. }
  470. else
  471. {
  472. $messageID = isset($params['messageId']) ? $params['messageId'] : '';
  473. unset($params['messageId']);
  474. $currentConsumed = 0;
  475. foreach($params as $key => $value)
  476. {
  477. if(in_array(strtolower($key), $this->lang->condKeywords['realStarted'])) $realStarted = $value;
  478. if(in_array(strtolower($key), $this->lang->condKeywords['finishedDate'])) $finishedDate = $value;
  479. if(in_array(strtolower($key), $this->lang->condKeywords['currentConsumed'])) $currentConsumed = $value;
  480. };
  481. if(empty($realStarted) || empty($finishedDate)) return '';
  482. $params = array
  483. (
  484. 'currentConsumed' => $currentConsumed,
  485. 'realStarted' => $realStarted,
  486. 'finishedDate' => $finishedDate,
  487. );
  488. $task = $this->loadEntry('taskfinish', 'post', array_merge(array('taskID' => $taskID, 'assignedTo' => '', 'comment' => ''), $params));
  489. if(isset($task->error))
  490. {
  491. foreach($task->error as $error) $reply->messages[] = is_array($error) ? implode(',', $error): $error;
  492. return $reply;
  493. }
  494. $finishedDate = $this->formatDate($task, 'finishedDate', '', 'Y-m-d H:i:s');
  495. $reply->messages[] = sprintf($this->lang->finish->done, $taskID, $finishedDate, $task->consumed);
  496. $reply->messages[] = $this->renderTaskTable(array($task));
  497. $reply->responses[] = $this->disableMarkdownLinkInMessage($messageID);
  498. if(!empty($task->fromBug))
  499. {
  500. $reply->messages[] = sprintf($this->lang->finish->bugTip, $taskID);
  501. $link = commonModel::getSysURL() . str_replace('x.php', 'index.php', helper::createLink('bug', 'view', "bugID={$task->fromBug}", 'html'));
  502. $href = urlencode($link);
  503. $reply->messages[] = "[{$this->lang->finish->bugTipLinkTitle}](xxc:openInApp/zentao-integrated/{$href})";
  504. }
  505. }
  506. return $reply;
  507. }
  508. /**
  509. * Format date.
  510. *
  511. * @param object $task
  512. * @param string $field
  513. * @param string $default
  514. * @param string $format
  515. * @return string
  516. */
  517. private function formatDate($task, $field, $default = '', $format = 'Y-m-d\TH:i')
  518. {
  519. try
  520. {
  521. $datetime = new DateTime(empty($task->$field) ? 'now' : $task->$field, new DateTimeZone('UTC'));
  522. $datetime->setTimezone(new DateTimeZone(date_default_timezone_get()));
  523. return $datetime->format($format);
  524. }
  525. catch(Exception $e)
  526. {
  527. return $default;
  528. }
  529. }
  530. /**
  531. * Render tasks as table html.
  532. *
  533. * @param array $tasks
  534. * @param array $originArgs
  535. * @param pager $pager
  536. * @return string|array
  537. */
  538. public function renderTask($tasks, $originArgs, $pager)
  539. {
  540. $lang = $this->im->lang;
  541. $taskCount = $pager->recTotal ? $pager->recTotal : count($tasks);
  542. if($taskCount === 0) return $lang->task->noTask;
  543. if($taskCount == 1)
  544. {
  545. $task = current($tasks);
  546. $link = str_replace('x.php', 'index.php', helper::createLink('task', 'view', "taskID=$task->id", 'html'));
  547. $messages = new stdclass();
  548. $messages->type = 'url';
  549. $messages->url = common::getSysURL() . $link;
  550. return array(sprintf($this->lang->tasksFound, $taskCount), $messages);
  551. }
  552. $messages = array();
  553. if($pager->pageID == 1) $messages[] = sprintf($this->lang->tasksFound, $taskCount);
  554. $taskTable = $this->renderTaskTable($tasks);
  555. $originCommand = $this->lang->viewCommand . ' ' . implode(' ', $originArgs);
  556. $paging = "$pager->pageID / $pager->pageTotal";
  557. if($pager->pageID != 1)
  558. {
  559. $previousPageCommand = $originCommand . " {$this->lang->condKeywords['pageID'][1]}=" . ($pager->pageID - 1) . " {$this->lang->condKeywords['recPerPage'][1]}=" . $pager->recPerPage;
  560. $previousPageCommand = rawurlencode($previousPageCommand);
  561. $paging .= " [{$this->lang->prevPage}](xxc://sendContentToServerBySendbox/{$previousPageCommand})";
  562. }
  563. if($pager->pageID != $pager->pageTotal)
  564. {
  565. $nextPageCommand = $originCommand . " {$this->lang->condKeywords['pageID'][1]}=" . ($pager->pageID + 1) . " {$this->lang->condKeywords['recPerPage'][1]}=" . $pager->recPerPage;
  566. $nextPageCommand = rawurlencode($nextPageCommand);
  567. $paging .= " [{$this->lang->nextPage}](xxc://sendContentToServerBySendbox/{$nextPageCommand})";
  568. }
  569. $paging = "<div class=\"text-right\">$paging</div>";
  570. $messages[] = $taskTable . $paging;
  571. return $messages;
  572. }
  573. /**
  574. * Render tasks as markdown table.
  575. *
  576. * @param array $tasks
  577. * @access public
  578. * @return void
  579. */
  580. public function renderTaskTable($tasks)
  581. {
  582. $lang = $this->im->lang;
  583. $headMap = array('id'=>'ID', 'pri' => $lang->task->pri, 'name' => $lang->task->name, 'assignedTo' => $lang->task->assignedTo, 'status' => $lang->task->status, 'estimate' => $lang->task->estimateAB, 'consumed' => $lang->task->consumedAB, 'left' => $lang->task->leftAB, 'actions' => $lang->actions);
  584. $thead = '';
  585. foreach($headMap as $value) $thead .= "<th>{$value}</th>";
  586. $thead = "<thead class='text-nowrap'><tr>{$thead}</tr></thead>";
  587. $tbody = '';
  588. foreach($tasks as $task)
  589. {
  590. $tr = '';
  591. $link = str_replace('x.php', 'index.php', helper::createLink('task', 'view', "taskID=$task->id", 'html'));
  592. $href = urlencode(common::getSysURL() . $link);
  593. $isParent = $task->parent == -1;
  594. $isChild = $task->parent > 0;
  595. $isMulti = !empty($task->mode);
  596. foreach($headMap as $key => $value)
  597. {
  598. switch($key)
  599. {
  600. case 'name':
  601. $tr .= "<td>";
  602. if($isParent) $tr .= " <span class='label label-sm circle'>{$lang->task->parentAB}</span>&nbsp;";
  603. if($isChild) $tr .= " <span class='label label-sm circle'>{$lang->task->childrenAB}</span>&nbsp;";
  604. if($isMulti) $tr .= " <span class='label label-sm circle'>" . zget($lang->task->modeList, $task->mode) . "</span>&nbsp;";
  605. $tr .= "<a href='xxc:openInApp/zentao-integrated/{$href}'>{$task->$key}</a>";
  606. $tr .= "</td>";
  607. break;
  608. case 'status':
  609. $tr .= "<td class='text-nowrap'>{$this->taskStatusList[$task->$key]}</td>";
  610. break;
  611. case 'assignedTo':
  612. if($task->mode == 'multi')
  613. {
  614. $tr .= "<td class='text-nowrap'>{$lang->task->team}</td>";
  615. }
  616. else if($task->$key == 'closed')
  617. {
  618. $tr .= "<td class='text-nowrap'>Closed</td>";
  619. }
  620. else
  621. {
  622. $tr .= "<td class='text-nowrap'>" . (empty($task->assignedTo) ? $lang->task->noAssigned : (is_object($task->assignedTo) ? $task->assignedTo->realname : $task->assignedTo)) . "</td>";
  623. }
  624. break;
  625. case 'actions':
  626. $startUrl = 'xxc://sendContentToServerBySendbox/' . "{$this->lang->startCommand} #$task->id";
  627. $finishUrl = 'xxc://sendContentToServerBySendbox/' . "{$this->lang->finishCommand} #$task->id";
  628. $closeUrl = 'xxc://sendContentToServerBySendbox/' . "{$this->lang->closeCommand} #$task->id";
  629. $canStart = in_array($task->status, array('wait', 'pause'));
  630. $canFinish = in_array($task->status, array('wait', 'pause', 'doing'));
  631. $canClose = in_array($task->status, array('done', 'cancel'));
  632. /* Disable all if task has children. */
  633. if($isParent)
  634. {
  635. $canStart = false;
  636. $canFinish = false;
  637. $canClose = false;
  638. }
  639. $tr .= '<td><div class="flex gap-xs">';
  640. $tr .= $canStart ? "<a href='$startUrl'><i class='icon-zt-play'></i></a>" : "<div><i class='icon-zt-play disabled'></i></div>";
  641. $tr .= $canFinish ? "<a href='$finishUrl'><i class='icon-zt-checked'></i></a>" : "<div><i class='icon-zt-checked disabled'></i></div>";
  642. $tr .= $canClose ? "<a href='$closeUrl'><i class='icon-zt-off'></i></a>" : "<div><i class='icon-zt-off disabled'></i></div>";
  643. $tr .= '</div></td>';
  644. break;
  645. case 'estimate':
  646. case 'consumed':
  647. case 'left':
  648. $tr .= "<td class='text-nowrap'>{$task->$key}h</td>";
  649. break;
  650. default:
  651. $tr .= "<td class='text-nowrap'>{$task->$key}</td>";
  652. }
  653. }
  654. $tbody .= "<tr>{$tr}</tr>";
  655. }
  656. return "<table>{$thead}{$tbody}</table>";
  657. }
  658. /**
  659. * Start task command.
  660. *
  661. * @param array $args bot command arguments
  662. * @param int $userID
  663. * @access public
  664. * @return object|string
  665. */
  666. public function start($args = array(), $userID = 0)
  667. {
  668. $originArgs = $args;
  669. foreach($args as $key => $value)
  670. {
  671. if(in_array(strtolower($value), $this->lang->condKeywords['task']))
  672. {
  673. unset($args[$key]);
  674. }
  675. }
  676. if(count($args) == 0) return $this->lang->errors->taskIDRequired;
  677. $taskID = 0;
  678. foreach($args as $key => $value)
  679. {
  680. if(strpos($value, '#') !== false)
  681. {
  682. $id = str_replace('#', '', $value);
  683. if($id && is_numeric($id))
  684. {
  685. $taskID = $id;
  686. unset($args[$key]);
  687. }
  688. else
  689. {
  690. if($args[$key + 1] && is_numeric($args[$key + 1]))
  691. {
  692. $taskID = $args[$key + 1];
  693. unset($args[$key]);
  694. unset($args[$key + 1]);
  695. }
  696. }
  697. }
  698. }
  699. if(!$taskID) return $this->lang->errors->taskIDRequired;
  700. $task = $this->loadEntry('task', 'get', array('taskID' => $taskID));
  701. if(!$task) return $this->lang->errors->taskNotFound;
  702. if($task->status != 'wait') return sprintf($this->lang->errors->invalidStatus, $this->taskStatusList[$task->status]);
  703. $reply = new stdClass();
  704. $reply->messages = array();
  705. $reply->responses = array();
  706. if(count($args) == 0)
  707. {
  708. $originCommand = rawurlencode($this->lang->startCommand . ' ' . implode(' ', $originArgs));
  709. $realStarted = $this->formatDate($task, 'realStarted');
  710. $json = (object)array
  711. (
  712. 'title' => "#{$taskID} {$task->name}",
  713. 'submitLabel' => $this->im->lang->task->start,
  714. 'inputs' => array
  715. (
  716. (object)array
  717. (
  718. 'name' => $this->im->lang->task->realStarted,
  719. 'type' => 'datetime-local',
  720. 'label' => $this->im->lang->task->realStarted,
  721. 'value' => $realStarted,
  722. ),
  723. (object)array
  724. (
  725. 'name' => $this->im->lang->task->consumed,
  726. 'type' => 'number',
  727. 'label' => $this->im->lang->task->consumed,
  728. 'value' => $task->consumed,
  729. 'addon' => $this->im->lang->task->hour,
  730. ),
  731. (object)array
  732. (
  733. 'name' => $this->im->lang->task->left,
  734. 'type' => 'number',
  735. 'label' => $this->im->lang->task->left,
  736. 'value' => $task->left,
  737. 'addon' => $this->im->lang->task->hour,
  738. 'helpText' => $this->lang->start->finishWithZeroLeft,
  739. )
  740. ),
  741. );
  742. $json = rawurlencode(json_encode($json));
  743. $reply->messages[] = sprintf($this->lang->start->tip, $taskID);
  744. $reply->messages[] = "[{$this->lang->start->tipLinkTitle}](xxc://openFormAndSendToServerBySendbox/$json/$originCommand)";
  745. }
  746. else
  747. {
  748. $hasFormArgs = false;
  749. $left = $task->left;
  750. $consumed = $task->consumed;
  751. $realStarted = $task->realStarted;
  752. foreach($args as $arg)
  753. {
  754. if(strpos($arg, 'messageId'))
  755. {
  756. $hasFormArgs = true;
  757. $formArgs = explode('&', $arg);
  758. $replyArgs = array();
  759. foreach($formArgs as $formArg)
  760. {
  761. $item = explode('=', $formArg);
  762. if(count($item) == 2)
  763. {
  764. $replyArgs[$item[0]] = $item[1];
  765. }
  766. }
  767. foreach($replyArgs as $key => $value)
  768. {
  769. if(in_array(strtolower($key), $this->lang->condKeywords['consumed'])) $consumed = $value;
  770. if(in_array(strtolower($key), $this->lang->condKeywords['realStarted'])) $realStarted = $value;
  771. if(in_array(strtolower($key), $this->lang->condKeywords['left'])) $left = $value;
  772. }
  773. if($consumed == 0 && $left == 0) return $this->im->lang->task->noticeTaskStart;
  774. $task = $this->loadEntry('taskStart', 'post', array
  775. (
  776. 'taskID' => $taskID,
  777. 'left' => $left,
  778. 'realStarted' => $realStarted,
  779. 'consumed' => $consumed,
  780. ));
  781. if(isset($task->result) and $task->result == 'fail')
  782. {
  783. $reply->messages[] = $task->message;
  784. }
  785. elseif(isset($task->error))
  786. {
  787. foreach($task->error as $error) $reply->messages[] = is_array($error) ? implode(',', $error): $error;
  788. }
  789. else
  790. {
  791. $reply->messages[] = sprintf($this->lang->effortRecorded, $taskID);
  792. $reply->messages[] = $this->renderTaskTable(array($task));
  793. $reply->responses[] = $this->disableMarkdownLinkInMessage($replyArgs['messageId']);
  794. }
  795. }
  796. }
  797. if(!$hasFormArgs)
  798. {
  799. while(!empty($args))
  800. {
  801. $arg = array_shift($args);
  802. if(in_array(strtolower($arg), $this->lang->condKeywords['left']))
  803. {
  804. $left = array_shift($args);
  805. }
  806. elseif(in_array(strtolower($arg), $this->lang->condKeywords['consumed']))
  807. {
  808. $consumed = array_shift($args);
  809. }
  810. elseif(in_array(strtolower($arg), $this->lang->condKeywords['realStarted']))
  811. {
  812. $realStarted = array_shift($args);
  813. }
  814. }
  815. $task = $this->loadEntry('taskStart', 'post', array
  816. (
  817. 'taskID' => $taskID,
  818. 'left' => $left,
  819. 'realStarted' => $realStarted,
  820. 'consumed' => $consumed,
  821. ));
  822. if($task->result and $task->result == 'fail')
  823. {
  824. $reply->messages[] = $task->message;
  825. }
  826. elseif(isset($task->error))
  827. {
  828. foreach($task->error as $error) $reply->messages[] = is_array($error) ? implode(',', $error): $error;
  829. }
  830. else
  831. {
  832. $reply->messages[] = sprintf($this->lang->effortRecorded, $taskID);
  833. $reply->messages[] = $this->renderTaskTable(array($task));
  834. }
  835. }
  836. }
  837. return $reply;
  838. }
  839. /**
  840. * Remove a Markdown link in the message and generate a response.
  841. *
  842. * @param $messageID
  843. * @access public
  844. * @return object
  845. */
  846. private function disableMarkdownLinkInMessage($messageID)
  847. {
  848. if(!$messageID) return null;
  849. $message = $this->im->message->getList('', array($messageID));
  850. $message = current($message);
  851. if($message->user != 'xuanbot') return null;
  852. $message->content = preg_replace('/\[(.*?)\]\(.*?\)/', '$1', $message->content);
  853. $this->im->message->setMessage($message);
  854. $userID = array_filter(explode('&', $message->cgid), function($item)
  855. {
  856. return is_numeric($item);
  857. });
  858. $response = new stdclass();
  859. $response->result = 'success';
  860. $response->method = 'messageUpdate';
  861. $response->users = $userID;
  862. $response->data = array($message);
  863. return $response;
  864. }
  865. /**
  866. * Used to call zentao's API.
  867. *
  868. * @param string $entry
  869. * @param string $action
  870. * @param array $params
  871. * @param array $query
  872. * @param string $version
  873. * @access public
  874. * @return object
  875. */
  876. public function loadEntry($entry, $action, $params = array(), $query = array(), $version = 'v1')
  877. {
  878. try
  879. {
  880. $imUser = $this->im->user;
  881. $this->userModel = $this->im->loadModel('user');
  882. $this->im->user = $imUser;
  883. $user = $this->userModel->getByID($this->im->app->input['userID'], 'id');
  884. /* Authorize him and save to session. */
  885. $user->rights = $this->userModel->authorize($user->account);
  886. $user->groups = $this->userModel->getGroups($user->account);
  887. $user->view = $this->userModel->grantUserView($user->account, $user->rights['acls'], $user->rights['projects']);
  888. $user->admin = strpos($this->im->app->company->admins, ",{$user->account},") !== false;
  889. $_SESSION['user'] = $user;
  890. global $app;
  891. $app->action = $action;
  892. $app->user = $user;
  893. include_once($this->im->app->appRoot . "framework/api/entry.class.php");
  894. include_once($this->im->app->appRoot . "api/{$version}/entries/" . strtolower($entry) . ".php");
  895. $entryName = $entry . 'Entry';
  896. $entry = new $entryName();
  897. if($action == 'post' || $action == 'put')
  898. {
  899. $entry->requestBody = (object)$params;
  900. }
  901. elseif($action == 'get' && !empty($query))
  902. {
  903. $entry->setParam($query);
  904. }
  905. $content = call_user_func_array(array($entry, $action), $params);
  906. return json_decode($content);
  907. }
  908. catch(EndResponseException $endResponseException)
  909. {
  910. return $endResponseException->getContent();
  911. }
  912. }
  913. }