action.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  1. <?php
  2. Action::register('stats_save_report',function(&$response){
  3. global $myUser,$_;
  4. User::check_access('statistic','edit');
  5. require_once(__DIR__.SLASH.'StatisticReport.class.php');
  6. $report = isset($_['report']) && !empty($_['report']) ? StatisticReport::getByid($_['report']) : new StatisticReport();
  7. $report->fromArray($_);
  8. $report->save();
  9. $response['id'] = $report->id;
  10. });
  11. Action::register('stats_update_widget',function(&$response){
  12. global $myUser,$_;
  13. User::check_access('statistic','edit');
  14. require_once(__DIR__.SLASH.'Widget.class.php');
  15. if(!isset($_['id'])) throw new Exception('ID widget non spécifié');
  16. $widget = Widget::getById($_['id']);
  17. $widget->fromArray($_);
  18. $widget->save();
  19. $response['label'] = $widget->label;
  20. });
  21. Action::register('stats_properties_load',function(&$response){
  22. global $myUser,$_;
  23. User::check_access('statistic','read');
  24. require_once(__DIR__.SLASH.'Widget.class.php');
  25. $widget = Widget::getById($_['id']);
  26. ob_start();
  27. require_once(__DIR__.SLASH.'page.propertie.php');
  28. $response['html'] = ob_get_clean();
  29. });
  30. Action::register('stats_properties_save',function(&$response){
  31. global $myUser,$_;
  32. User::check_access('statistic','edit');
  33. require_once(__DIR__.SLASH.'Widget.class.php');
  34. $widget = Widget::getById($_['id']);
  35. require_once(__DIR__.SLASH.'View.class.php');
  36. View::require_all();
  37. $widget->meta = json_decode($widget->meta,true);
  38. if(!is_array($widget->meta)) $widget->meta = array();
  39. if(!isset($widget->meta['properties'])) $widget->meta['properties'] = array();
  40. if(!is_null($widget->view)){
  41. foreach ($widget->view::option() as $key => $value) {
  42. if(isset($_[$key])) $widget->meta['properties'][$key]= $_[$key];
  43. }
  44. }
  45. $widget->meta = json_encode($widget->meta);
  46. $widget->save();
  47. });
  48. Action::register('stats_save_widget',function(&$response){
  49. global $myUser,$_;
  50. User::check_access('statistic','edit');
  51. require_once(__DIR__.SLASH.'Widget.class.php');
  52. if(!isset($_['widgets'])) return;
  53. $widgets = array();
  54. foreach(Widget::loadAll(array('report'=>$_['report'])) as $item)
  55. $widgets[$item->id] = $item;
  56. $lastId = false;
  57. foreach ($_['widgets'] as $widgetInfos) {
  58. $widget = (isset($widgetInfos['id']) && isset($widgets[$widgetInfos['id']])) ? $widgets[$widgetInfos['id']] : new Widget();
  59. $widget->fromArray($widgetInfos);
  60. $widget->report = $_['report'];
  61. if(isset($widgetInfos['id']) && !$myUser->can('statistic','edit',$widgetInfos['id'])) continue;
  62. $widget->save();
  63. if(!isset($widgetInfos['id'])){
  64. $response['lastId'] = $widget->id;
  65. $class = WidgetElement::getType('query');
  66. $element = new $class();
  67. $element->fromArray(array(
  68. 'widget' => $widget->id,
  69. 'label' => 'Requete 1',
  70. 'connection' => 1,
  71. 'sql' => 'SELECT 1 legende1,2 legende2,3 legende3',
  72. 'sort' => 0
  73. ));
  74. $element->slug='requete-1';
  75. $element->save();
  76. $class = WidgetElement::getType('treatment');
  77. $element = new $class();
  78. $element->fromArray(array(
  79. 'widget' => $widget->id,
  80. 'label' => 'Traitement 1',
  81. 'source' => '$output = array();
  82. foreach($data[\'requete-1\'] as $index=>$element){
  83. foreach($element as $key=>$row){
  84. if(is_int($key)) continue;
  85. $outputRow[$key] = $row;
  86. }
  87. $output[] = $outputRow;
  88. }
  89. return $output[0];',
  90. 'sort' => 1
  91. ));
  92. $element->slug='traitement-1';
  93. $element->save();
  94. // Si création de widget, on créée les droits d'accès pour l'auteur
  95. $form = array();
  96. $form['uid'] = $widget->id;
  97. $form['read'] = $form['edit'] = $form['delete'] = $form['configure'] = 1;
  98. $form['recursive'] = 0;
  99. $form['targetScope'] = 'user';
  100. $form['targetUid'] = $myUser->login;
  101. $form['scope'] = 'statistic';
  102. $myUser->loadRights();
  103. $_SESSION['currentUser'] = serialize($myUser);
  104. }
  105. }
  106. });
  107. Action::register('stats_delete_widget',function(&$response){
  108. global $myUser,$_;
  109. User::check_access('statistic','delete');
  110. require_once(__DIR__.SLASH.'Widget.class.php');
  111. $widget = Widget::getById($_['id']);
  112. if(!$myUser->can('statistic','delete',$widget->id) ) throw new Exception("Permission refusée");
  113. $widget->remove();
  114. });
  115. Action::register('stats_export_view_table',function(&$response){
  116. global $myUser,$_;
  117. User::check_access('statistic','read');
  118. require_once(__DIR__.SLASH.'Widget.class.php');
  119. require_once(__DIR__.SLASH.'WidgetElement.class.php');
  120. require_once(__DIR__.SLASH.'View.class.php');
  121. View::require_all();
  122. $widget = Widget::provide();
  123. if(!$widget) throw new Exception("Widget non spécifié ou inexistant en base");
  124. $filters = isset($_['filters']) ? $_['filters'] : array();
  125. $item = array();
  126. try {
  127. $item = $widget->toArray();
  128. $view = $widget->view;
  129. if($view==null) return;
  130. $results = $widget->cascadePreview($filters);
  131. $results = $view::toArray($results);
  132. $output = array();
  133. foreach($results as $result){
  134. if(!is_array($result)) continue;
  135. $output[] = array_map('strip_tags', $result);
  136. }
  137. $xlsx = Excel::exportArray($output, null , 'Statistiques');
  138. File::downloadStream($xlsx,'export-tableau-'.date('d-m-Y-H').'.xlsx','application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  139. exit();
  140. } catch(Exception $e) {
  141. $item['html'] = '<div class="alert alert-danger"> <strong>WIDGET EN ERREUR</strong><p>Erreur de paramétrage du widget</p> <br>'.$e->getMessage().'</div>';
  142. }
  143. });
  144. Action::register('stats_search_widget',function(&$response){
  145. global $myUser,$_;
  146. User::check_access('statistic','read');
  147. require_once(__DIR__.SLASH.'Widget.class.php');
  148. require_once(__DIR__.SLASH.'StatisticReport.class.php');
  149. $widgets = array();
  150. foreach(Widget::loadAll(array('report'=>$_['report'])) as $widget){
  151. if(!$myUser->can('statistic_report','read',$widget->report) && !$myUser->can('statistic_widget','read',$widget->id)) continue;
  152. $row = $widget->toArray();
  153. $row['edit'] = $myUser->can('statistic','edit',$widget->id);
  154. $row['delete'] = $myUser->can('statistic','delete',$widget->id);
  155. $widgets[] = $row;
  156. }
  157. $response['widgets'] = $widgets;
  158. });
  159. Action::register('stats_search_query_tables',function(&$response){
  160. global $myUser,$_;
  161. User::check_access('statistic','edit');
  162. require_once(__DIR__.SLASH.'Connection.class.php');
  163. $reponse['rows'] = array();
  164. $connection = Connection::getById($_['connection']);
  165. foreach($connection->tables() as $table)
  166. $response['rows'][] = array('table'=>$table);
  167. });
  168. Action::register('stats_search_query_columns',function(&$response){
  169. global $myUser,$_;
  170. User::check_access('statistic','edit');
  171. require_once(__DIR__.SLASH.'Connection.class.php');
  172. $reponse['rows'] = array();
  173. $connection = Connection::getById($_['connection']);
  174. foreach($connection->columns($_['table']) as $column)
  175. $response['rows'][] = $column;
  176. });
  177. Action::register('stats_refresh_widget_content',function(&$response){
  178. global $myUser,$_;
  179. User::check_access('statistic','read');
  180. require_once(__DIR__.SLASH.'Widget.class.php');
  181. require_once(__DIR__.SLASH.'WidgetElement.class.php');
  182. require_once(__DIR__.SLASH.'View.class.php');
  183. View::require_all();
  184. $widgets = Widget::loadAll(array('report'=>$_['report']));
  185. $filters = isset($_['filters']) ? $_['filters'] : array();
  186. $logs = array();
  187. foreach($widgets as $widget){
  188. $options = array();
  189. $meta = json_decode($widget->meta,true);
  190. if(is_array($meta) && isset($meta['properties'])) $options['properties'] = $meta['properties'];
  191. if($myUser->login != $widget->creator && !$myUser->can('statistic_report','read',$widget->report) && !$myUser->can('statistic_widget','read',$widget->id)) continue;
  192. $item = array();
  193. try {
  194. $item = $widget->toArray();
  195. $view = $widget->view;
  196. $logs[] = $item['label'];
  197. if($view==null) continue;
  198. $results = $widget->cascadePreview($filters);
  199. $item['html'] = $view::toHtml($widget->label,$results,$options);
  200. } catch(Exception $e) {
  201. $item['html'] = '<div class="alert alert-danger"> <strong>WIDGET EN ERREUR</strong><p>Erreur de paramétrage du widget</p> <br>'.$e->getMessage().'</div>';
  202. }
  203. $response['widgets'][] = $item;
  204. }
  205. Log::put('Consultation du rapport '.$_['report'].(isset($_['filters']) ? '. Filtres : '.json_encode($_['filters']) : '').'. Blocs consultés :'.json_encode($logs), 'Statistiques');
  206. });
  207. Action::register('stats_edit_widget_content',function(&$response){
  208. User::check_access('statistic','edit');
  209. ob_start();
  210. require_once(__DIR__.SLASH.'edit_widget.php');
  211. $response['content'] = ob_get_clean();
  212. });
  213. ////////////////////
  214. //ELEMENTS WIDGET //
  215. ////////////////////
  216. Action::register('stats_search_widget_element',function(&$response){
  217. global $myUser,$_;
  218. require_once(__DIR__.SLASH.'WidgetElement.class.php');
  219. User::check_access('statistic','read');
  220. $elements = WidgetElement::getElementsByWidget($_['id']);
  221. foreach ($elements as $element) {
  222. $row = $element->toArray();
  223. $row['type'] = get_class($element);
  224. $row['typeLabel'] = $element->typeLabel;
  225. $row['icon'] = $element->icon;
  226. $row['label'] = html_entity_decode($row['label']);
  227. $response['rows'][] = $row;
  228. }
  229. });
  230. Action::register('stats_element_preview',function(&$response){
  231. global $myUser,$_;
  232. require_once(__DIR__.SLASH.'Widget.class.php');
  233. require_once(__DIR__.SLASH.'WidgetElement.class.php');
  234. User::check_access('statistic','edit');
  235. $filters = isset($_['filters']) ? $_['filters'] : array();
  236. $widget = Widget::getById($_['id']);
  237. $data = WidgetElement::cascadePreview($_['type'],$_['id'],$filters,$widget);
  238. //$data = end($data);
  239. $response['data'] = json_encode($data['data'] ,JSON_PRETTY_PRINT);
  240. if(isset($data['meta'])) $response['meta'] = $data['meta'] ;
  241. if($response['data']==false)
  242. $response['data'] = json_encode(array_map_recursive('utf8_encode', $data) ,JSON_PRETTY_PRINT);
  243. if($response['data']==false)
  244. $response['data'] = json_encode(array_map_recursive('utf8_decode', $data) ,JSON_PRETTY_PRINT);
  245. });
  246. Action::register('stats_delete_widget_element',function(&$response){
  247. require_once(__DIR__.SLASH.'WidgetElement.class.php');
  248. require_once(__DIR__.SLASH.'Widget.class.php');
  249. global $myUser,$_;
  250. User::check_access('statistic','delete');
  251. if(!isset($_['type'])) return;
  252. $class = WidgetElement::getType($_['type']);
  253. $element = isset($_['id']) ? $class::getById($_['id']) : new $class();
  254. $widget = Widget::getById($element->widget);
  255. if(!$widget->check_access('delete')) throw new Exception('permission denied');
  256. $class::delete(array('id'=>$_['id']));
  257. });
  258. Action::register('stats_widget_element_sort',function(&$response){
  259. require_once(__DIR__.SLASH.'Widget.class.php');
  260. require_once(__DIR__.SLASH.'WidgetElement.class.php');
  261. global $myUser,$_;
  262. User::check_access('statistic','edit');
  263. foreach ($_['sort'] as $i=>$element) {
  264. $class = $element['type'];
  265. require_once(__DIR__.SLASH.'element'.SLASH.$class.'.class.php');
  266. $element = $class::getById($element['id']);
  267. $widget = Widget::getById($element->widget);
  268. if(!$widget->check_access('edit')) continue;
  269. $element->sort=$i;
  270. $element->save();
  271. }
  272. });
  273. Action::register('stats_edit_widget_element',function(&$response){
  274. require_once(__DIR__.SLASH.'WidgetElement.class.php');
  275. global $myUser,$_;
  276. User::check_access('statistic','edit');
  277. if(!isset($_['type'])) return;
  278. $class = WidgetElement::getType($_['type']);
  279. $element = isset($_['id']) ? $class::getById($_['id']) : new $class();
  280. $response['script'] = $element->javascript;
  281. $response['edit'] = '<div id="editor">'.$element->editor().'</div>';
  282. $filters = isset($_['filters']) ? $_['filters'] : array();
  283. $response['data'] = '{}';
  284. });
  285. Action::register('stats_rename_widget_element',function(&$response){
  286. require_once(__DIR__.SLASH.'WidgetElement.class.php');
  287. require_once(__DIR__.SLASH.'Widget.class.php');
  288. global $myUser,$_;
  289. User::check_access('statistic','edit');
  290. if(!isset($_['type'])) return;
  291. $class = WidgetElement::getType($_['type']);
  292. $element = isset($_['id']) ? $class::getById($_['id']) : new $class();
  293. $widget = Widget::getById($element->widget);
  294. if(!$widget->check_access('edit')) return;
  295. $response = $element->toArray();
  296. $response['type'] = $_['type'];
  297. });
  298. Action::register('stats_save_widget_element',function(&$response){
  299. global $myUser,$_;
  300. require_once(__DIR__.SLASH.'Widget.class.php');
  301. require_once(__DIR__.SLASH.'WidgetElement.class.php');
  302. User::check_access('statistic','edit');
  303. $class = WidgetElement::getType($_['type']);
  304. $element = isset($_['id']) ? $class::getById($_['id']) : new $class();
  305. if(!isset($element->sort) || $element->sort == '') $element->sort = 1000;
  306. $element->fromArray($_);
  307. $widget = Widget::getById($element->widget);
  308. if(!$widget->check_access('edit')) return;
  309. if($class == 'Treatment'){
  310. $output = array();
  311. $tmpPath = File::temp().uniqid('check_syntax_error_', true).'.tmp';
  312. file_put_contents($tmpPath, "<?php ".html_entity_decode($element->source));
  313. exec('php -l '.$tmpPath.' 2<&1', $output, $return);
  314. if(preg_match('/n\'est pas reconnu/is', $output[0])) throw new Exception('Merci d\'installer PHP-cli pour utiliser ce module');
  315. $noSyntaxError = false;
  316. foreach ($output as $line) {
  317. if(preg_match('/No syntax errors detected/is', $line)) $noSyntaxError = true;
  318. }
  319. if(!$noSyntaxError && isset($output[1])) throw new Exception('Erreur de parsing du code php de traitement : '. preg_replace("/\sin\s.*(\son.*)/i", "$1", $output[1]));
  320. unlink($tmpPath);
  321. }
  322. if(empty($element->slug)) $element->slug = slugify($element->label);
  323. $element->save();
  324. });
  325. Action::register('stats_preview_view',function(&$response){
  326. global $myUser,$_;
  327. User::check_access('statistic','edit');
  328. require_once(__DIR__.SLASH.'Widget.class.php');
  329. require_once(__DIR__.SLASH.'View.class.php');
  330. View::require_all();
  331. $filter = isset($_['filter'])? $_['filter'] : array();
  332. $filters = isset($_['filters']) ? $_['filters'] : array();
  333. $widget = Widget::getById($_['widget']);
  334. $results = $widget->cascadePreview($filters);
  335. $options = array();
  336. $meta = json_decode($widget->meta,true);
  337. if(is_array($meta) && isset($meta['properties'])) $options['properties'] = $meta['properties'];
  338. $html = '<div><label><i class="fas fa-chart-pie"></i> Rendu</label>';
  339. $html .= isset($_['view']) ? $_['view']::toHtml('Prévisualisation',$results,$options) : '<br/>Aucune vue sélectionnée, choisissez une vue sur le panneau de gauche';
  340. $html .= '</div>';
  341. $response['html'] = $html;
  342. });
  343. Action::register('stats_report_import',function(&$response){
  344. global $myUser,$_;
  345. User::check_access('statistic','configure');
  346. require_once(__DIR__.SLASH.'StatisticReport.class.php');
  347. $maxSize = 200000000 ;
  348. foreach ($_FILES['file']['tmp_name'] as $i=>$temp) {
  349. if($_FILES['file']['type'][$i] != 'application/x-zip-compressed') throw new Exception("Type de fichier non accpté");
  350. if($_FILES['file']['size'][$i] > $maxSize) throw new Exception("Taille de l'import trop importante, max :".$maxSize);
  351. StatisticReport::import($_FILES['file']['tmp_name'][$i]);
  352. };
  353. });
  354. Action::register('stats_export_report',function(&$response){
  355. global $myUser,$_;
  356. User::check_access('statistic','configure');
  357. require_once(__DIR__.SLASH.'StatisticReport.class.php');
  358. require_once(__DIR__.SLASH.'Widget.class.php');
  359. $report = StatisticReport::getById($_['id']);
  360. File::downloadStream($report->export(), $name= slugify($report->label).'-'.date('d-m-Y h.i').'.zip', $mime='application/zip');
  361. exit();
  362. });
  363. Action::register('stats_delete_report',function(&$response){
  364. global $myUser,$_;
  365. User::check_access('statistic','delete');
  366. require_once(__DIR__.SLASH.'StatisticReport.class.php');
  367. $report = StatisticReport::provide();
  368. $report->remove();
  369. header('location: index.php?module=statistic');
  370. exit();
  371. });
  372. Action::register('stats_save_widget_connection',function(&$response){
  373. global $myUser,$_;
  374. User::check_access('statistic','configure');
  375. Plugin::need('statistic/WidgetElement');
  376. require_once(__DIR__.SLASH.'element'.SLASH.'Query.class.php');
  377. $query = Query::provide();
  378. if(!$query) throw new Exception("Requête non identifiée");
  379. $query->connection = $_['connection'];
  380. $query->save();
  381. });
  382. //FILTERS
  383. Action::register('stats_save_filter',function(&$response){
  384. global $myUser,$_;
  385. require_once(__DIR__.SLASH.'Filter.class.php');
  386. User::check_access('statistic','edit');
  387. $filter = isset($_['id']) ? Filter::getById($_['id']) : new Filter();
  388. $filter->fromArray($_);
  389. $filter->default = str_replace(' ','',$filter->default);
  390. $filter->save();
  391. });
  392. Action::register('stats_report_move_filter',function(&$response){
  393. global $myUser,$_;
  394. User::check_access('statistic','edit');
  395. require_once(__DIR__.SLASH.'Filter.class.php');
  396. if(isset($_['sort']) && !empty($_['sort'])){
  397. foreach ($_['sort'] as $sort => $id) {
  398. $item = Filter::getById($id);
  399. $item->sort = $sort;
  400. $item->save();
  401. }
  402. }
  403. });
  404. Action::register('stats_report_search_filter',function(&$response){
  405. global $myUser,$_;
  406. require_once(__DIR__.SLASH.'Filter.class.php');
  407. User::check_access('statistic','read');
  408. foreach(Filter::loadAll(array('report'=>$_['report']),array('sort ASC')) as $filter){
  409. $filter->slug = slugify($filter->label);
  410. $row = $filter->toArray();
  411. $row['label'] = html_entity_decode($row['label']);
  412. $response['rows'][] = $row;
  413. }
  414. });
  415. Action::register('stats_edit_filter',function(&$response){
  416. global $myUser,$_;
  417. require_once(__DIR__.SLASH.'Filter.class.php');
  418. User::check_access('statistic','edit');
  419. $filter = Filter::getById($_['id']);
  420. $response = $filter;
  421. });
  422. Action::register('stats_delete_filter',function(&$response){
  423. global $myUser,$_;
  424. require_once(__DIR__.SLASH.'Filter.class.php');
  425. User::check_access('statistic','delete');
  426. Filter::deleteById($_['id']);
  427. });
  428. //CONNECTION
  429. Action::register('stats_search_connection',function(&$response){
  430. global $myUser,$_;
  431. require_once(__DIR__.SLASH.'Connection.class.php');
  432. User::check_access('statistic','edit');
  433. foreach(Connection::loadAll() as $connection)
  434. $response['rows'][] = $connection;
  435. });
  436. Action::register('stats_save_connection',function(&$response){
  437. global $myUser,$_;
  438. require_once(__DIR__.SLASH.'Connection.class.php');
  439. User::check_access('statistic','configure');
  440. if(empty($_['label'])) throw new Exception("Libellé obligatoire");
  441. if(empty($_['handler'])) throw new Exception("Type de base obligatoire");
  442. $connection = isset($_['id']) && !empty($_['id']) ? Connection::getById($_['id']) : new Connection();
  443. $connection->label = $_['label'];
  444. $connection->handler = $_['handler'];
  445. $connection->meta = json_encode($_['meta']);
  446. //test readonly
  447. /*try{
  448. if(!$connection->readOnly()) throw new Exception("Permissions trops larges sur la base, merci de specifier un compte en lecture seule");
  449. } catch(PDOException $ex) {
  450. if($ex->getCode() == 1045) throw new Exception('Identifiant ou mot de passe incorrect');
  451. throw $ex;
  452. }*/
  453. $connection->save();
  454. });
  455. Action::register('stats_connection_handler_search',function(&$response){
  456. global $myUser,$_;
  457. require_once(__DIR__.SLASH.'Connection.class.php');
  458. User::check_access('statistic','configure');
  459. $response['html'] = '';
  460. if(empty($_['handler'])) return;
  461. $handler= str_replace('.', '', $_['handler']);
  462. require_once(__ROOT__.SLASH.'connector'.SLASH.$handler.'.class.php');
  463. foreach($handler::fields() as $field){
  464. $response['html'] .= '<div class="input-group mb-3"><div class="input-group-prepend">
  465. <label class="input-group-text" for="'.$field['id'].'">'.$field['label'].'</label></div>';
  466. if(!isset($field['comment']))
  467. $response['html'] .= '<small>'.$field['comment'].'</small><br/>';
  468. $response['html'] .= '<input type="text" class="form-control" value="'.$field['default'].'" name="'.$field['id'].'" id="'.$field['id'].'"/></div>';
  469. }
  470. });
  471. Action::register('stats_connection_test',function(&$response){
  472. global $myUser,$_;
  473. require_once(__DIR__.SLASH.'Connection.class.php');
  474. User::check_access('statistic','configure');
  475. $connection = Connection::getById($_['id']);
  476. $tableNumber = $connection->test();
  477. $response['message'] = $tableNumber.' tables trouvées';
  478. });
  479. Action::register('stats_edit_connection',function(&$response){
  480. global $myUser,$_;
  481. require_once(__DIR__.SLASH.'Connection.class.php');
  482. User::check_access('statistic','configure');
  483. $connection = Connection::getById($_['id']);
  484. $connection->meta = json_decode($connection->meta,true);
  485. $response = $connection;
  486. });
  487. Action::register('stats_delete_connection',function(&$response){
  488. global $myUser,$_;
  489. require_once(__DIR__.SLASH.'Connection.class.php');
  490. User::check_access('statistic','configure');
  491. Connection::deleteById($_['id']);
  492. });
  493. /* WIDGET */
  494. Action::register('stats_widget_load',function(&$response){
  495. global $myUser;
  496. User::check_access('read','statistic');
  497. require_once(PLUGIN_PATH.'dashboard'.SLASH.'DashboardWidget.class.php');
  498. $widget = DashboardWidget::current();
  499. if( $widget->data('title') == ""){
  500. $widget->title = 'Bloc statistiques';
  501. }else{
  502. $widget->title = '';
  503. $widget->icon = '';
  504. }
  505. if($widget->data('color') != "") $widget->background = $widget->data('color');
  506. ob_start();
  507. require_once(__DIR__.SLASH.'widget.php');
  508. $widget->content = ob_get_clean();
  509. echo json_encode($widget);
  510. exit();
  511. });
  512. Action::register('stats_widget_configure_save',function(&$response){
  513. global $myUser,$_;
  514. User::check_access('statistic','read');
  515. require_once(PLUGIN_PATH.'dashboard'.SLASH.'DashboardWidget.class.php');
  516. $widget = DashboardWidget::getById($_['id']);
  517. $widget->data('stats',$_['stats']);
  518. $widget->save();
  519. });
  520. Action::register('stats_widget_configure',function(&$response){
  521. global $myUser;
  522. User::check_access('statistic','read');
  523. require_once(PLUGIN_PATH.'dashboard'.SLASH.'DashboardWidget.class.php');
  524. $widget = DashboardWidget::current();
  525. ob_start();
  526. require_once(__DIR__.SLASH.'widget.configure.php');
  527. $content = ob_get_clean();
  528. echo $content ;
  529. });
  530. Action::register('stats_widget_configure_autocomplete',function(&$response){
  531. global $myUser,$_;
  532. User::check_access('statistic','read');
  533. require_once(__DIR__.SLASH.'Widget.class.php');
  534. require_once(__DIR__.SLASH.'StatisticReport.class.php');
  535. $response['rows'] = array();
  536. if($_['keyword'] == '') return;
  537. foreach(Widget::staticQuery('SELECT w.*,r.label as report FROM {{table}} w LEFT JOIN '.StatisticReport::tableName().' r ON r.id=w.report WHERE w.label LIKE ?',array('%'.$_['keyword'].'%')) as $row){
  538. if($row['creator'] != $myUser->login && !$myUser->can('statistic','read',$row['id'])) continue;
  539. $row['name'] = $row['label'];
  540. $response['rows'][] = $row;
  541. }
  542. });
  543. ?>