action.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. global $_,$conf;
  3. switch($_['action']){
  4. /** DASHBOARD **/
  5. //Récuperation d'une liste de dashboard
  6. case 'dashboard_dashboard_search':
  7. Action::write(function(&$response){
  8. global $myUser,$_;
  9. if(!$myUser->can('dashboard','read')) throw new Exception("Permissions insuffisantes",403);
  10. require_once(__DIR__.SLASH.'Dashboard.class.php');
  11. foreach(Dashboard::populate() as $dashboard){
  12. $response['rows'][] = $dashboard;
  13. }
  14. });
  15. break;
  16. //Ajout ou modification d'élément dashboard
  17. case 'dashboard_dashboard_save':
  18. Action::write(function(&$response){
  19. global $myUser,$_;
  20. if(!$myUser->can('dashboard','edit')) throw new Exception("Permissions insuffisantes",403);
  21. require_once(__DIR__.SLASH.'Dashboard.class.php');
  22. $item = Dashboard::provide();
  23. $item->user = $myUser->login;
  24. $item->label = $_['label'];
  25. $item->icon = $_['icon'];
  26. $item->default = $_['default'];
  27. $item->save();
  28. });
  29. break;
  30. //Récuperation ou edition d'élément dashboard
  31. case 'dashboard_dashboard_edit':
  32. Action::write(function(&$response){
  33. global $myUser,$_;
  34. if(!$myUser->can('dashboard','edit')) throw new Exception("Permissions insuffisantes",403);
  35. require_once(__DIR__.SLASH.'Dashboard.class.php');
  36. $response = Dashboard::getById($_['id']);
  37. });
  38. break;
  39. //Suppression d'élement dashboard
  40. case 'dashboard_dashboard_delete':
  41. Action::write(function(&$response){
  42. global $myUser,$_;
  43. if(!$myUser->can('dashboard','delete')) throw new Exception("Permissions insuffisantes",403);
  44. require_once(__DIR__.SLASH.'Dashboard.class.php');
  45. Dashboard::deleteById($_['id']);
  46. });
  47. break;
  48. //Sauvegarde des configurations de dashboard
  49. case 'dashboard_setting_save':
  50. Action::write(function(&$response){
  51. global $myUser,$_,$conf;
  52. if(!$myUser->can('dashboard','configure')) throw new Exception("Permissions insuffisantes",403);
  53. foreach(Configuration::setting('dashboard') as $key=>$value){
  54. if(!is_array($value)) continue;
  55. $allowed[] = $key;
  56. }
  57. foreach ($_['fields'] as $key => $value) {
  58. if(in_array($key, $allowed))
  59. $conf->put($key,$value);
  60. }
  61. });
  62. break;
  63. /** DASHBOARDWIDGET **/
  64. //Récuperation d'une liste de dashboardwidget
  65. case 'dashboard_dashboardwidget_search':
  66. Action::write(function(&$response){
  67. global $myUser,$_;
  68. if(!$myUser->can('dashboard','read')) throw new Exception("Permissions insuffisantes",403);
  69. require_once(__DIR__.SLASH.'DashboardWidget.class.php');
  70. $models = array();
  71. Plugin::callHook('widget',array(&$models));
  72. foreach($models as $model):
  73. $models[$model->model] = $model;
  74. endforeach;
  75. $widgets = DashboardWidget::loadAll(array('dashboard'=>$_['dashboard']),array('position'));
  76. foreach($widgets as $widget):
  77. if(!isset($models[$widget->model])) continue;
  78. $model = clone $models[$widget->model];
  79. $model->id = $widget->id;
  80. $model->position = $widget->position;
  81. $model->minified = $widget->minified;
  82. $model->dashboard = $widget->dashboard;
  83. $response['rows'][] = $model;
  84. endforeach;
  85. });
  86. break;
  87. //Ajout ou modification d'élément dashboardwidget
  88. case 'dashboard_dashboardwidget_save':
  89. Action::write(function(&$response){
  90. global $myUser,$_;
  91. if(!$myUser->can('dashboard','edit')) throw new Exception("Permissions insuffisantes",403);
  92. require_once(__DIR__.SLASH.'DashboardWidget.class.php');
  93. $item = DashboardWidget::provide();
  94. $item->model = $_['model'];
  95. $item->data = $_['data'];
  96. $item->position = $_['position'];
  97. $item->minified = $_['minified'];
  98. $item->dashboard = $_['dashboard'];
  99. $item->save();
  100. });
  101. break;
  102. //Récuperation ou edition d'élément dashboardwidget
  103. case 'dashboard_dashboardwidget_edit':
  104. Action::write(function(&$response){
  105. global $myUser,$_;
  106. if(!$myUser->can('dashboard','edit')) throw new Exception("Permissions insuffisantes",403);
  107. require_once(__DIR__.SLASH.'DashboardWidget.class.php');
  108. $response = DashboardWidget::getById($_['id']);
  109. });
  110. break;
  111. //Suppression d'élement dashboardwidget
  112. case 'dashboard_dashboardwidget_delete':
  113. Action::write(function(&$response){
  114. global $myUser,$_;
  115. if(!$myUser->can('dashboard','delete')) throw new Exception("Permissions insuffisantes",403);
  116. require_once(__DIR__.SLASH.'DashboardWidget.class.php');
  117. require_once(__DIR__.SLASH.'Dashboard.class.php');
  118. if(!$myUser->can('widget','delete')) throw new Exception("Permissions insuffisantes",403);
  119. $widget = DashboardWidget::getById($_['widget']);
  120. $dashboard = Dashboard::getById($_['dashboard']);
  121. if($widget->dashboard!=$dashboard->id || $dashboard->user!=$myUser->login)
  122. throw new Exception("Vous ne pouvez supprimer que vos propres widgets");
  123. $widget->deleteById($widget->id);
  124. $response['message'] = 'Widget supprimé';
  125. });
  126. break;
  127. case 'dashboard_dashboardwidget_refresh':
  128. Action::write(function(&$response){
  129. global $myUser,$_;
  130. if(!$myUser->can('dashboard','read')) throw new Exception("Permissions insuffisantes",403);
  131. $widgets = array();
  132. Plugin::callHook('widget_refresh',array(&$widgets));
  133. $response['rows'] = $widgets;
  134. });
  135. break;
  136. //Sauvegarde des configurations de dashboard
  137. case 'dashboard_setting_save':
  138. Action::write(function(&$response){
  139. global $myUser,$_,$conf;
  140. if(!$myUser->can('dashboard','configure')) throw new Exception("Permissions insuffisantes",403);
  141. foreach(Configuration::setting('dashboard') as $key=>$value){
  142. if(!is_array($value)) continue;
  143. $allowed[] = $key;
  144. }
  145. foreach ($_['fields'] as $key => $value) {
  146. if(in_array($key, $allowed))
  147. $conf->put($key,$value);
  148. }
  149. });
  150. break;
  151. case 'dashboard_dashboardwidget_add':
  152. Action::write(function(&$response){
  153. global $myUser,$_;
  154. if(!$myUser->can('dashboard','edit')) throw new Exception("Permissions insuffisantes",403);
  155. require_once(__DIR__.SLASH.'DashboardWidget.class.php');
  156. $widget = new DashboardWidget();
  157. $widget->model = $_['widget'];
  158. $widget->position = 666;
  159. $widget->minified = false;
  160. $widget->dashboard = $_['dashboard'];
  161. $widget->save();
  162. $response['message'] = 'Widget ajouté';
  163. });
  164. break;
  165. case 'dashboard_dashboardwidget_save_position':
  166. Action::write(function(&$response){
  167. global $myUser,$_;
  168. if(!$myUser->can('dashboard','update')) throw new Exception("Permissions insuffisantes",403);
  169. require_once(__DIR__.SLASH.'Dashboard.class.php');
  170. require_once(__DIR__.SLASH.'DashboardWidget.class.php');
  171. $dashboard = Dashboard::getById($_['dashboard']);
  172. if( $dashboard->user!=$myUser->login)
  173. throw new Exception("Vous ne pouvez modifier que vos propres widgets");
  174. $dashboard_widgets = DashboardWidget::loadAll( array('dashboard' => $dashboard->id ) );
  175. foreach($_['positions'] as $move){
  176. foreach($dashboard_widgets as $dashboard_widget){
  177. if($dashboard_widget->id!=$move['id']) continue;
  178. $dashboard_widget->position = $move['position'];
  179. $dashboard_widget->save();
  180. }
  181. }
  182. });
  183. break;
  184. case 'dashboard_widget_clock_load':
  185. require_once('DashboardWidget.class.php');
  186. $widget = DashboardWidget::current();
  187. $widget->title = 'Horloge';
  188. $widget->content = '<div class="clockContainer">
  189. <div class="clock" id="clock"></div>
  190. </div>';
  191. echo json_encode($widget);
  192. break;
  193. case 'dashboard_widget_log_load':
  194. global $myUser;
  195. require_once('DashboardWidget.class.php');
  196. if(!$myUser->can('log','read')) throw new Exception("Permissions insuffisantes",403);
  197. $widget = DashboardWidget::current();
  198. $logs = Log::loadAll(array(),array('created DESC'),array(30));
  199. $widget->title = '30 derniers logs';
  200. $widget->content = '<table class="table table-stripped table-hover">';
  201. $widget->content .= '<tr><th style="width:90px">Date</th><th>Libellé</th><th>Utilisateur</th></tr>';
  202. foreach($logs as $log){
  203. $widget->content .= '<tr><td><i class="fa fa-calendar-o"></i> '.date('d-m-y',$log->created).'<i class="fa fa-clock-o"></i> '.date('H:i:s',$log->created).'</td><td>'.$log->label().'</td><td><i class="fa fa-user"></i> '.$log->creator.'</td></tr>';
  204. }
  205. $widget->content .= '</ul>';
  206. echo json_encode($widget);
  207. break;
  208. case 'dashboard_widget_profile_load':
  209. global $myUser;
  210. require_once('DashboardWidget.class.php');
  211. $widget = DashboardWidget::current();
  212. $widget->title = 'Profile';
  213. $widget->content = '<div class="profileContainer">
  214. <div class="profileHeader"></div>
  215. <a href="account.php" class="profileImage"><img class="avatar-mini" src="'.$myUser->getAvatar().'"></a>
  216. <h3>'.$myUser->fullname().'</h3>
  217. <small>'.$myUser->function.'</small>
  218. <a href="account.php" class="btn btn-dark block ml-2 mr-2" ><i class="far fa-meh-blank"></i> Editer mon profil</a>
  219. </div>';
  220. echo json_encode($widget);
  221. break;
  222. }
  223. ?>