action.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. <?php
  2. global $_,$conf;
  3. switch($_['action']){
  4. /** ISSUEREPORT **/
  5. //Récuperation d'une liste de issuereport
  6. case 'issue_issuereport_search':
  7. Action::write(function(&$response){
  8. global $myUser,$_;
  9. User::check_access('issue','read');
  10. require_once(__DIR__.SLASH.'IssueReport.class.php');
  11. require_once(__DIR__.SLASH.'IssueEvent.class.php');
  12. require_once(__DIR__.SLASH.'IssueReportTag.class.php');
  13. $allTags = IssueReportTag::tags();
  14. $query = 'SELECT DISTINCT {{table}}.id, {{table}}.*,(SELECT ie.content FROM '.IssueEvent::tableName().' ie WHERE ie.type=? AND ie.issue={{table}}.id ORDER BY id ASC LIMIT 1) as comment,(SELECT COUNT(ie2.id) FROM '.IssueEvent::tableName().' ie2 WHERE ie2.type=? AND ie2.issue={{table}}.id) as comments FROM {{table}} LEFT JOIN '.IssueReportTag::tableName().' t ON t.report={{table}}.id WHERE 1';
  15. $data = array(IssueEvent::TYPE_COMMENT,IssueEvent::TYPE_COMMENT);
  16. //Recherche simple
  17. if(!empty($_['filters']['keyword'])){
  18. $query .= ' AND (SELECT ie.content FROM '.IssueEvent::tableName().' ie WHERE ie.type=? AND ie.issue={{table}}.id ORDER BY id ASC LIMIT 1) LIKE ?';
  19. $data[] = IssueEvent::TYPE_COMMENT;
  20. $data[] = '%'.$_['filters']['keyword'].'%';
  21. }
  22. $tags = array_filter(explode(',',$_['tags']));
  23. if(count($tags)!=0){
  24. $query .= ' AND t.tag IN ('.str_repeat('?,',count($tags)-1).'?)';
  25. $data = array_merge($data, $tags);
  26. }
  27. //Recherche avancée
  28. if(isset($_['filters']['advanced'])){
  29. $_['filters']['advanced'] = filter_alteration($_['filters']['advanced'],'comment',function($filter) use (&$query,&$data){
  30. $query .= ' AND (SELECT ie.content FROM '.IssueEvent::tableName().' ie WHERE ie.type=? AND ie.issue={{table}}.id ORDER BY id ASC LIMIT 1) ';
  31. switch($filter['operator']){
  32. case 'like':
  33. $value = '%'.$filter['value'][0].'%';
  34. $query .= ' LIKE ? ';
  35. break;
  36. case 'not like':
  37. $value = '%'.$filter['value'][0].'%';
  38. $query .= ' NOT LIKE ? ';
  39. break;
  40. case 'null':
  41. $value = null;
  42. $query .= ' IS NULL ';
  43. break;
  44. case 'not null':
  45. $value = null;
  46. $query .= ' IS NOT NULL ';
  47. break;
  48. case '=':
  49. case '!=':
  50. $value = $filter['value'][0];
  51. $query .= ' '.$filter['operator'].' ? ';
  52. break;
  53. }
  54. $data[] = IssueEvent::TYPE_COMMENT;
  55. if($value!= null) $data[] = $value;
  56. return null;
  57. });
  58. filter_secure_query($_['filters']['advanced'],array('{{table}}.id','from','module','{{table}}.creator','{{table}}.created','assign','browser','ip','state'),$query,$data);
  59. }
  60. $query .= ' ORDER BY {{table}}.id desc ';
  61. $dataPaginate = $data;
  62. array_splice($dataPaginate, 0, 2);
  63. //Pagination
  64. $response['pagination'] = IssueReport::paginate(20,(!empty($_['page'])?$_['page']:0),$query,$dataPaginate);
  65. //Récupération des Users
  66. $users = array();
  67. foreach(User::getAll(array('right'=>false)) as $user)
  68. $users[$user->login] = $user;
  69. $response['rows'] = array();
  70. foreach(IssueReport::staticQuery($query,$data) as $row){
  71. //$row = $issueReport->toArray(true);
  72. $row['state'] = IssueReport::states($row['state']);
  73. $row['relativefrom'] = str_replace(ROOT_URL,'',$row['from']);
  74. $row['date'] = date('d-m-Y',$row['created']);
  75. $row['hour'] = date('H:i',$row['created']);
  76. $row['osIcon'] = 'fas fa-question-circle';
  77. $row['browserIcon'] = 'fas fa-question-circle';
  78. $row['comments'] = $row['comments']==1 ? false : $row['comments']-1;
  79. $row['excerpt'] = isset($row['comment']) ? truncate(strip_tags($row['comment']),150) : 'Aucun commentaire';
  80. $row['excerpt'] = preg_replace('/https?:\/\/[^\s]*\?module=issue&page=sheet\.report&id=([0-9]*)/is','<span class="text-muted" title="$0">#$1</span>',$row['excerpt']);
  81. $row['belongUser'] = $myUser->login == $row['creator'] || $myUser->can('issue','configure');
  82. $row['fullName'] = isset($users[$row['creator']]) ? $users[$row['creator']]->fullName() : $row['creator'];
  83. if(strlen($row['os'])>=3 && substr(strtolower($row['os']),0,3) == 'win') $row['osIcon'] = 'fab fa-windows text-primary';
  84. if(strlen($row['os'])>=5 && substr(strtolower($row['os']),0,5) == 'linux') $row['osIcon'] = 'fab fa-linux text-danger';
  85. if(strlen($row['os'])>=3 && substr(strtolower($row['os']),0,3) == 'mac') $row['osIcon'] = 'fab fa-apple text-secondary';
  86. switch($row['browser']){
  87. case 'firefox': $row['browserIcon'] = 'fab fa-firefox text-warning'; break;
  88. case 'ie': $row['browserIcon'] = 'fab fa-internet-explorer text-danger'; break;
  89. case 'edge': $row['browserIcon'] = 'fab fa-edge text-primary'; break;
  90. case 'chrome': $row['browserIcon'] = 'fab fa-chrome text-success'; break;
  91. }
  92. $row['tags'] = array();
  93. foreach(IssueReportTag::loadAll(array('report'=>$row['id'])) as $tag){
  94. if(!isset($allTags[$tag->tag])) continue;
  95. $row['tags'][] = $allTags[$tag->tag];
  96. }
  97. $response['rows'][] = $row;
  98. }
  99. });
  100. break;
  101. case 'issue_autocomplete':
  102. Action::write(function(&$response){
  103. global $myUser,$_;
  104. if(!$myUser->connected()) throw new Exception("Vous devez être connecté",401);
  105. require_once(__DIR__.SLASH.'IssueReport.class.php');
  106. require_once(__DIR__.SLASH.'IssueEvent.class.php');
  107. $query = IssueReport::staticQuery('SELECT ir.id as id,ie.content as content FROM {{table}} ir LEFT JOIN '.IssueEvent::tableName().' ie ON ie.id=( SELECT id FROM '.IssueEvent::tableName().' ie2 WHERE ir.id=ie2.issue AND ie2.type=? ORDER BY id LIMIT 1) WHERE ie.content LIKE ?',array("comment",'%'.$_['keyword'].'%'));
  108. $response['rows'] = array();
  109. foreach ($query->fetchAll() as $row) {
  110. $response['rows'][] = array(
  111. 'id' => $row['id'],
  112. 'name' => $row['id'].' - '.strip_tags($row['content']),
  113. 'slug' => $row['id']
  114. );
  115. }
  116. });
  117. break;
  118. //Gestion avatar drag & drop
  119. case 'issue_attachments':
  120. Action::write(function(&$response){
  121. File::handle_component(array(
  122. 'namespace' => 'issue', //stockés dans file/summary/*.*
  123. 'access' => 'issue', // crud sur summary,
  124. 'size' => '1000000000', // taille max
  125. 'storage' => 'issue/attachments/{{data.id}}/*' //chemin complet vers le fichier stocké
  126. ),$response);
  127. });
  128. break;
  129. case 'issue_add_document':
  130. Action::write(function(&$response){
  131. global $myUser,$_;
  132. if(!$myUser->connected()) throw new Exception("Vous devez être connecté",401);
  133. require_once(__DIR__.SLASH.'IssueReport.class.php');
  134. require_once(__DIR__.SLASH.'IssueEvent.class.php');
  135. $report = IssueReport::provide();
  136. $report->save();
  137. foreach ($_['files'] as $file) {
  138. $name = (get_OS() === 'WIN') ? utf8_decode($file['name']) : $file['name'];
  139. $row = File::move(File::temp().$file['path'],'issue'.SLASH.'screens'.SLASH.$report->id.SLASH.$name);
  140. $row['url'] = 'action.php?action=issue_download_document&event='.$event->id.'&path='.base64_encode($file['name']);
  141. $row['oldPath'] = $file['path'];
  142. $response['files'][] = $row;
  143. }
  144. $response['id'] = $report->id;
  145. });
  146. break;
  147. //Téléchargement des documents
  148. case 'issue_download_document':
  149. global $myUser,$_;
  150. if(!$myUser->connected()) throw new Exception("Vous devez être connecté",401);
  151. require_once(__DIR__.SLASH.'IssueEvent.class.php');
  152. $event = IssueEvent::getById($_['event']);
  153. $path = str_replace(array('..','/','\\'),'',base64_decode($_['path']));
  154. $path = $event->dir().SLASH.$path;
  155. File::downloadFile($path);
  156. break;
  157. case 'issue_issuereport_meta_save':
  158. Action::write(function(&$response){
  159. global $myUser,$_,$conf;
  160. User::check_access('issue','edit');
  161. require_once(__DIR__.SLASH.'IssueReport.class.php');
  162. require_once(__DIR__.SLASH.'IssueEvent.class.php');
  163. require_once(__DIR__.SLASH.'IssueReportTag.class.php');
  164. if(!array_key_exists($_['state'], IssueReport::states())) throw new Exception("L'état du ticket est invalide");
  165. $item = IssueReport::provide();
  166. $oldItem = clone $item;
  167. if($item->creator != $myUser->login && !$myUser->can('issue','configure')) throw new Exception("Permissions insuffisantes", 403);
  168. //Maj des tags
  169. if(empty($_['tags'])) throw new Exception("Le ticket doit avoir au moins un tag");
  170. $existingTags = IssueReportTag::loadAll(array('report'=>$item->id));
  171. $tags = explode(',',$_['tags']);
  172. $tagsAction = false;
  173. $similarTags = array();
  174. foreach ($existingTags as $existingTag) {
  175. if(in_array($existingTag->tag, $tags)){
  176. $similarTags[] = $existingTag->tag;
  177. continue;
  178. }
  179. IssueReportTag::deleteById($existingTag->id);
  180. $tagsAction = array('action'=>'deleted','tag'=>IssueReportTag::tags($existingTag->tag));
  181. }
  182. foreach ($tags as $tag) {
  183. if(in_array($tag, $similarTags)) continue;
  184. $newTag = new IssueReportTag();
  185. $newTag->tag = $tag;
  186. $newTag->report = $item->id;
  187. $newTag->save();
  188. $tagsAction = array('action'=>'added','tag'=>IssueReportTag::tags($tag));
  189. }
  190. if($tagsAction != false){
  191. $event = new IssueEvent();
  192. $event->type = IssueEvent::TYPE_TAG;
  193. $event->issue = $item->id;
  194. $event->content = json_encode($tagsAction);
  195. $event->save();
  196. }
  197. if(!$myUser->can('issue','configure')) throw new Exception("Permissions insuffisantes", 403);
  198. $item->state = $_['state'];
  199. if(!empty($_['assign'])){
  200. $item->assign = stripslashes($_['assign']);
  201. $item->addFollower($item->assign);
  202. }
  203. $item->addFollower($myUser->login);
  204. $item->save();
  205. //Si l'assignation a changée on envoi une notification
  206. if($oldItem->assign != $item->assign){
  207. $event = new IssueEvent();
  208. $event->type = IssueEvent::TYPE_ASSIGNATION;
  209. $event->issue = $item->id;
  210. $event->content = json_encode(array(
  211. 'assigned' => $item->assign
  212. ));
  213. $event->save();
  214. if($item->assign != $myUser->login){
  215. Plugin::callHook("emit_notification", array(array(
  216. 'label' => '['.PROGRAM_NAME.' ] Le Ticket #'.$item->id.' vous a été assigné',
  217. 'html' => "Le ticket <a href='".ROOT_URL."/index.php?module=issue&page=sheet.report&id=".$item->id."'>#".$item->id."</a> vous a été assigné par ".(!empty($myUser->fullName())?$myUser->fullName():$myUser->login)." le ".date('d-m-Y à H:i').".
  218. <br>Bonne chance :).",
  219. 'type' => "issue",
  220. 'meta' => array('link' => ROOT_URL.'/index.php?module=issue&page=sheet.report&id='.$item->id),
  221. 'recipients' => array($item->assign)
  222. )));
  223. Plugin::callHook("emit_notification", array(array(
  224. 'label' => '['.PROGRAM_NAME.'] Le Ticket #'.$item->id.' a été assigné à '.User::byLogin($item->assign)->fullName(),
  225. 'html' => "Le ticket <a href='".ROOT_URL."/index.php?module=issue&page=sheet.report&id=".$item->id."'>#".$item->id."</a> a été assigné à ".User::byLogin($item->assign)->fullName()." par ".(!empty($myUser->fullName())?$myUser->fullName():$myUser->login)." le ".date('d-m-Y à H:i').".",
  226. 'type' => "issue",
  227. 'meta' => array('link' => ROOT_URL.'/index.php?module=issue&page=sheet.report&id='.$item->id),
  228. 'recipients' => array_diff( $item->followers(), array($myUser->login), array($item->assign) )
  229. )));
  230. }
  231. }
  232. //Si l'etat a changé on envois une notification
  233. if($oldItem->state != $item->state){
  234. $infos = array(
  235. 'meta' => array('link' => ROOT_URL.'/index.php?module=issue&page=sheet.report&id='.$item->id),
  236. 'recipients' => array_diff( $item->followers(), array($myUser->login) ) ,
  237. 'type' => "notice"
  238. );
  239. switch ($item->state) {
  240. case 'closed':
  241. $infos['label'] = "Le Ticket #".$item->id." est résolu";
  242. $infos['html'] = "Le ticket d'erreur <a href='".ROOT_URL."/index.php?module=issue&page=sheet.report&id=".$item->id."'>#".$item->id."</a> a été marqué comme résolu par ".(!empty($myUser->fullName())?$myUser->fullName():$myUser->login).' le '.date('d-m-Y à H:i').'.
  243. <br>Il est possible que la résolution de cette erreur ne soit mise en production que dans quelques jours.';
  244. break;
  245. case 'open':
  246. $infos['label'] = "Le Ticket #".$item->id." a été ré-ouvert";
  247. $infos['html'] = "Le ticket d'erreur <a href='".ROOT_URL."/index.php?module=issue&page=sheet.report&id=".$item->id."'>#".$item->id."</a> a été ré-ouvert par ".(!empty($myUser->fullName())?$myUser->fullName():$myUser->login).' le '.date('d-m-Y à H:i').'.
  248. <br>Il est possible que la résolution de cette erreur ne soit mise en production que dans quelques jours.';
  249. break;
  250. case 'canceled':
  251. $infos['label'] = "Le Ticket #".$item->id." a été annulé";
  252. $infos['html'] = "Le ticket d'erreur <a href='".ROOT_URL."/index.php?module=issue&page=sheet.report&id=".$item->id."'>#".$item->id."</a> a été annulé ".(!empty($myUser->fullName())?$myUser->fullName():$myUser->login).' le '.date('d-m-Y à H:i').'.
  253. <br>Il est possible que ce ticket soit un doublon ou que son contenu soit innaproprié.';
  254. break;
  255. default:
  256. # code...
  257. break;
  258. }
  259. $event = new IssueEvent();
  260. $event->type = IssueEvent::TYPE_STATE;
  261. $event->issue = $item->id;
  262. $event->content = json_encode(array(
  263. 'old' => $oldItem->state,
  264. 'new' => $item->state
  265. ));
  266. $event->save();
  267. Plugin::callHook("emit_notification", array($infos));
  268. }
  269. });
  270. break;
  271. //Ajout ou modification d'élément issuereport
  272. case 'issue_issuereport_save':
  273. Action::write(function(&$response){
  274. global $myUser,$_,$conf;
  275. require_once(__DIR__.SLASH.'IssueReport.class.php');
  276. require_once(__DIR__.SLASH.'IssueEvent.class.php');
  277. require_once(__DIR__.SLASH.'IssueReportTag.class.php');
  278. if(!$myUser->connected()) throw new Exception("Vous devez être connecté pour laisser un rapport d'erreur",401);
  279. $item = IssueReport::provide();
  280. if(!$myUser->can('issue','edit') && is_numeric($item->id) && $item->id!=0) throw new Exception("Permissions insuffisantes",403);
  281. $tags = array_filter(explode(',',$_['tags']));
  282. if(count($tags)==0) throw new Exception("Merci de sélectionner au moins une catégorie");
  283. if(!isset($_['issue-comment']) || empty($_['issue-comment'])) throw new Exception("Merci de commenter votre ticket");
  284. $item->browser = $_['browser'];
  285. $item->browserVersion = $_['browserVersion'];
  286. $item->online = $_['online'];
  287. $item->os = $_['os'];
  288. $item->state = 'open';
  289. $item->from = $_['from'];
  290. $item->width = $_['width'];
  291. $item->height = $_['height'];
  292. $item->history = $_['history'];
  293. $item->ip = ip();
  294. $item->addFollower($myUser->login);
  295. $item->save();
  296. $firstEvent = new IssueEvent();
  297. $firstEvent->created = $item->created;
  298. $firstEvent->updated = $item->updated;
  299. $firstEvent->creator = $item->creator;
  300. $firstEvent->updater = $item->updater;
  301. $firstEvent->type = IssueEvent::TYPE_COMMENT;
  302. $firstEvent->content = preg_replace('/<img.*?(src="data).*?>/', '', wysiwyg_filter(html_entity_decode($_['issue-comment'])));
  303. $firstEvent->issue = $item->id;
  304. $firstEvent->save();
  305. if (preg_match_all('/<img.*?>/', wysiwyg_filter(html_entity_decode($_['issue-comment'])), $matches)) {
  306. foreach ($matches[0] as $index => $imgTag) {
  307. if(preg_match('/src="data:image.*base64,.*"/', $imgTag, $imgSrc)) {
  308. list($type, $data) = explode(';', $imgSrc[0]);
  309. $ext = explode('/', $type)[1];
  310. $dataEncode = explode(',', $data);
  311. $stream = base64_decode($dataEncode[1]);
  312. $attachmentFolder = $firstEvent->dir();
  313. if(!file_exists($attachmentFolder)) mkdir($attachmentFolder,0755,true);
  314. $screenPath = $firstEvent->dir().SLASH.'image'.$index.'.'.$ext;
  315. file_put_contents($screenPath, $stream);
  316. }
  317. }
  318. }
  319. $response['item'] = $item->toArray();
  320. IssueReportTag::delete(array('report'=>$item->id));
  321. foreach ($tags as $tag) {
  322. $reportTag = new IssueReportTag();
  323. $reportTag->tag = $tag;
  324. $reportTag->report = $item->id;
  325. $reportTag->save();
  326. }
  327. if(!empty($_['screenshot'])){
  328. $screenshot = str_replace('data:image/png;base64,', '', $_['screenshot']);
  329. $screenshot = str_replace(' ', '+', $screenshot);
  330. $stream = base64_decode($screenshot);
  331. $attachmentFolder = $firstEvent->dir();
  332. if(!file_exists($attachmentFolder)) mkdir($attachmentFolder,0755,true);
  333. $screenPath = $attachmentFolder.SLASH.'screenshot.jpg';
  334. file_put_contents($screenPath, $stream);
  335. }
  336. //Ajout fichiers joints
  337. if(!empty($_['document']))
  338. File::save_component('document', 'issue/attachments/'.$firstEvent->id.'/{{label}}');
  339. $data = $item->toArray();
  340. $data['comment'] = '<strong>'.$item->creator.': </strong>'.$firstEvent->content;
  341. $data['url'] = ROOT_URL.'/index.php?module=issue&page=sheet.report&id='.$data['id'];
  342. $data['title'] = 'Le Ticket <a href="'.$data['url'].'">#'.$item->id.'</a> a été ouvert par '.$item->creator;
  343. if(!empty($conf->get('issue_report_mails'))){
  344. $path = __DIR__.SLASH.'mail.template.php';
  345. if(!file_exists($path)) return;
  346. $stream = file_get_contents($path);
  347. $recipients = array();
  348. foreach (explode(',', $conf->get('issue_report_mails')) as $recipient) {
  349. if(is_numeric($recipient)){
  350. foreach(UserFirmRank::loadAll(array('rank'=>$recipient)) as $ufr)
  351. $recipients[] = $ufr->user;
  352. continue;
  353. }
  354. $recipients[] = $recipient;
  355. }
  356. // Émission d'une notification pour les devs
  357. Plugin::callHook("emit_notification", array(array(
  358. 'label' => 'Ticket #'.$item->id.' ouvert',
  359. 'html' => template($stream,$data),
  360. 'type' => "issue",
  361. 'meta' => array('link' => $data['url']),
  362. 'recipients' => $recipients
  363. )));
  364. }
  365. // Émission d'une notification pour l'auteur du ticket
  366. Plugin::callHook("emit_notification", array(array(
  367. 'label' => "Votre Ticket #".$item->id." a été envoyé",
  368. 'html' => "Le ticket <a href='".ROOT_URL."/index.php?module=issue&page=sheet.report&id=".$item->id."'>#".$item->id."</a> a été créé et sera pris en compte par nos techniciens, vous pouvez consulter
  369. son avancement en <a href='".ROOT_URL."/index.php?module=issue&page=sheet.report&id=".$item->id."'>cliquant ici</a>",
  370. 'type' => "notice",
  371. 'meta' => array('link' => $data['url']),
  372. 'recipients' => array($item->creator)
  373. )));
  374. Log::put('Déclaration d\'un rapport d\'erreur #'.$item->toText(), 'Issue');
  375. });
  376. break;
  377. case 'issue_screenshot_download':
  378. global $myUser,$_;
  379. if(!$myUser->connected()) throw new Exception("Connexion requise",401);
  380. try {
  381. require_once(__DIR__.SLASH.'IssueEvent.class.php');
  382. $event = IssueEvent::getById($_['id']);
  383. File::downloadFile($event->dir().SLASH.'screenshot.jpg');
  384. } catch(Exception $e) {
  385. File::downloadFile(__ROOT__.'img/default-image.png');
  386. }
  387. break;
  388. //Suppression d'élement issuereport
  389. case 'issue_issuereport_delete':
  390. Action::write(function(&$response){
  391. global $myUser,$_;
  392. require_once(__DIR__.SLASH.'IssueReport.class.php');
  393. require_once(__DIR__.SLASH.'IssueEvent.class.php');
  394. $issue = IssueReport::provide();
  395. if($issue->creator != $myUser->login && !$myUser->can('issue','configure')) throw new Exception("Permissions insuffisantes", 403);
  396. $issue->remove();
  397. });
  398. break;
  399. //Etre notifié du sujet
  400. case 'issue_follow_toggle':
  401. Action::write(function(&$response){
  402. global $myUser,$_;
  403. require_once(__DIR__.SLASH.'IssueReport.class.php');
  404. require_once(__DIR__.SLASH.'IssueEvent.class.php');
  405. User::check_access('issue','read');
  406. $issue = IssueReport::provide();
  407. $followers = $issue->followers();
  408. $key = array_search($myUser->login, $followers);
  409. if($_['state']){
  410. if($key===false) $followers[] = $myUser->login;
  411. }else{
  412. if($issue->creator == $myUser->login) throw new Exception("Vous ne pouvez pas arrêter de suivre un ticket que vous avez créé.");
  413. if($key!==false) unset($followers[$key]);
  414. }
  415. $issue->followers($followers);
  416. $issue->save();
  417. });
  418. break;
  419. //Sauvegarde des configurations de issue
  420. case 'issue_setting_save':
  421. Action::write(function(&$response){
  422. global $myUser,$_,$conf;
  423. User::check_access('issue','configure');
  424. foreach(Configuration::setting('issue') as $key=>$value){
  425. if(!is_array($value)) continue;
  426. $allowed[] = $key;
  427. }
  428. foreach ($_['fields'] as $key => $value)
  429. if(in_array($key, $allowed)) $conf->put($key,$value);
  430. });
  431. break;
  432. /** EVENT **/
  433. //Récuperation d'une liste de issueevent
  434. case 'issue_issue_event_search':
  435. Action::write(function(&$response){
  436. global $myUser,$_;
  437. User::check_access('issue','read');
  438. require_once(__DIR__.SLASH.'IssueEvent.class.php');
  439. require_once(__DIR__.SLASH.'IssueReport.class.php');
  440. $events = IssueEvent::loadAll(array('issue'=>$_['issue']),array('id ASC'));
  441. foreach($events as $i=>$event){
  442. $row = $event->toArray();
  443. $creator = User::byLogin($event->creator);
  444. $row['edit_rights'] = ($myUser->login == $event->creator || $myUser->superadmin);
  445. $row['creator'] = $creator->toArray();
  446. $row['avatar'] = $creator->getAvatar();
  447. $row['fullName'] = $creator->fullName();
  448. $row['created'] = date('d/m/Y H:i', $event->created);
  449. $row['createdRelative'] = relative_time($event->created, null, 6, true);
  450. switch($event->type){
  451. case 'comment':
  452. $row['classes'] = $i == 0 ? 'issue-first-comment' : '';
  453. $row['files'] = array();
  454. foreach (glob($event->dir().SLASH.'*') as $key => $value){
  455. $file = array();
  456. $file['extension'] = getExt($value);
  457. $file['label'] = mt_basename($value);
  458. $file['labelExcerpt'] = truncate($file['label'],35);
  459. $file['type'] = in_array($file['extension'], array('jpg','png','jpeg','bmp','gif','svg')) ? 'image' : 'file';
  460. $file['url'] = 'action.php?action=issue_download_document&event='.$event->id.'&path='.base64_encode(basename($value));
  461. $file['icon'] = getExtIcon($file['extension']);
  462. $row['files'][] = $file;
  463. }
  464. if($creator->login == $myUser->login || $myUser->can('issue','configure')) $row['classes'] .=' editable';
  465. $row['comment'] = empty($event->content) ? 'Pas de commentaire': $event->content;
  466. $row['comment'] = preg_replace('/https?:\/\/[^\s]*\?module=issue&page=sheet\.report&id=([0-9]*)/is','<a href="$0">#$1</a>',$row['comment']);
  467. $row['hasfiles'] = count($row['files']) > 0 ? true : false;
  468. break;
  469. case IssueEvent::TYPE_STATE:
  470. $infos = json_decode($row['content'],true);
  471. $row['oldstate'] = IssueReport::states($infos['old']);
  472. $row['state'] = IssueReport::states($infos['new']);
  473. break;
  474. case IssueEvent::TYPE_TAG:
  475. $infos = json_decode($row['content'],true);
  476. $row['action'] = $infos['action'] == 'added' ? 'ajouté' :'supprimé';
  477. $row['tag'] = $infos['tag'];
  478. break;
  479. case IssueEvent::TYPE_ASSIGNATION:
  480. $infos = json_decode($row['content'],true);
  481. $assigned = User::byLogin($infos['assigned']);
  482. $row['assigned'] = array(
  483. 'fullName' => $assigned->fullName(),
  484. 'avatar' => $assigned->getAvatar()
  485. );
  486. break;
  487. }
  488. $response['rows'][] = $row;
  489. }
  490. });
  491. break;
  492. //Ajout ou modification d'élément issueevent
  493. case 'issue_issue_event_save':
  494. Action::write(function(&$response){
  495. global $myUser,$_;
  496. require_once(__DIR__.SLASH.'IssueReport.class.php');
  497. require_once(__DIR__.SLASH.'IssueEvent.class.php');
  498. $item = IssueEvent::provide('id',1);
  499. $isNew = empty($item->id);
  500. if($myUser->login != $item->creator && !$myUser->can('issue','edit')) throw new Exception("Permissions insuffisantes", 403);
  501. if(!isset($_['content']) || empty($_['content'])) throw new Exception("Vous devez spécifier un commentaire");
  502. $item->type = IssueEvent::TYPE_COMMENT;
  503. $item->content = wysiwyg_filter(html_entity_decode($_['content']));
  504. $item->issue = $_['issue'];
  505. $mentionned = array();
  506. if($item->id == 0){
  507. preg_match_all('|data-mention-value="([^"]*)"|isU', $item->content, $matches,PREG_SET_ORDER);
  508. foreach ($matches as $match) {
  509. $mentionned[] = $match[1];
  510. }
  511. }
  512. $item->save();
  513. if(!empty($_['event-files']))
  514. File::save_component('event-files', 'issue/attachments/'.$item->id.'/{{label}}');
  515. $issue = IssueReport::getById($item->issue);
  516. $issue->addFollower($myUser->login);
  517. $issue->save();
  518. $path = __DIR__.SLASH.'mail.template.php';
  519. if(!file_exists($path)) return;
  520. $stream = file_get_contents($path);
  521. $data = $item->toArray();
  522. $data['comment'] = '<strong>'.$item->creator.': </strong>'.$item->content;
  523. $data['url'] = ROOT_URL.'/index.php?module=issue&page=sheet.report&id='.$data['issue'];
  524. $data['title'] = 'Le Ticket <a href="'.$data['url'].'">#'.$item->issue.'</a> a été commenté par '.$item->creator;
  525. $recipients = array_diff($issue->followers(), array($myUser->login));
  526. // Émission d'une notification pour les acteurs du rapport
  527. Plugin::callHook("emit_notification", array(array(
  528. 'label' => 'Ticket #'.$item->issue.': '.($isNew?'Nouveau commentaire':'Édition d\'un commentaire') ,
  529. 'html' => template($stream,$data),
  530. 'type' => "issue",
  531. 'meta' => array('link' => $data['url']),
  532. 'recipients' => $recipients
  533. )));
  534. // Émission d'une notification pour les acteurs spécifiquement mentionnés
  535. if(count($mentionned)){
  536. $data['comment'] = 'Vous avez été mentionné dans le commentaire suivant : <br>'.$data['comment'];
  537. $data['url'] = ROOT_URL.'/index.php?module=issue&page=sheet.report&id='.$data['issue'].'#issue-comment-'.$item->id;
  538. $data['title'] = 'Vous avez été mentionné par '.$item->creator.' dans le ticket <a href="'.$data['url'].'">#'.$item->issue.'</a>';
  539. Plugin::callHook("emit_notification", array(array(
  540. 'label' => 'Vous avez été mentionné par '.$item->creator.' dans le ticket #'.$item->issue.':' ,
  541. 'html' => template($stream,$data),
  542. 'type' => "issue",
  543. 'meta' => array('link' => $data['url']),
  544. 'recipients' => $mentionned
  545. )));
  546. }
  547. });
  548. break;
  549. //Récuperation ou edition d'élément issueevent
  550. case 'issue_issue_event_edit':
  551. Action::write(function(&$response){
  552. global $myUser,$_;
  553. User::check_access('issue','edit');
  554. require_once(__DIR__.SLASH.'IssueEvent.class.php');
  555. require_once(__DIR__.SLASH.'IssueReport.class.php');
  556. $event = IssueEvent::getById($_['id'],1);
  557. if($event->type != 'comment') throw new Exception("Vous ne pouvez pas éditer cet événement");
  558. if($myUser->login != $event->creator && !$myUser->superadmin) throw new Exception("Permissions insuffisantes", 403);
  559. $response = $event->toArray(true);
  560. });
  561. break;
  562. //Suppression d'élement issueevent
  563. case 'issue_issue_event_delete':
  564. Action::write(function(&$response){
  565. global $myUser,$_;
  566. require_once(__DIR__.SLASH.'IssueReport.class.php');
  567. require_once(__DIR__.SLASH.'IssueEvent.class.php');
  568. $event = IssueEvent::getById($_['id'],1);
  569. if($event->type != 'comment') throw new Exception("Vous ne pouvez pas supprimer cet événement");
  570. $issue = $event->join('issue');
  571. if($event->creator != $myUser->login && !$myUser->can('issue','configure')) throw new Exception("Permissions insuffisantes", 403);
  572. $event->remove();
  573. if(IssueEvent::rowCount(array('issue'=>$issue->id)) == 0){
  574. if($issue->creator != $myUser->login && !$myUser->can('issue','configure')) throw new Exception("Permissions insuffisantes", 403);
  575. $issue->remove(false);
  576. $response['redirect'] = 'index.php?module=issue&page=list.issue';
  577. }
  578. });
  579. break;
  580. }
  581. ?>