action.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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. User::check_access('dashboard','read');
  10. require_once(__DIR__.SLASH.'Dashboard.class.php');
  11. $filters = array();
  12. if(!$myUser->can('dashboard','configure')) $filters['creator'] = $myUser->login;
  13. foreach(Dashboard::loadAll($filters,array('label','creator')) as $dashboard){
  14. $userName = User::byLogin($dashboard->user)->fullName();
  15. $dashboard->user = !empty($userName) ? $userName : $dashboard->user;
  16. $dashboard->mandatory = $dashboard->mandatory == 1;
  17. $dashboard->default = $dashboard->default == 1;
  18. $response['rows'][] = $dashboard;
  19. }
  20. });
  21. break;
  22. //Ajout ou modification d'élément dashboard
  23. case 'dashboard_dashboard_save':
  24. Action::write(function(&$response){
  25. global $myUser,$_;
  26. User::check_access('dashboard','edit');
  27. if($_['mandatory']==1 && !$myUser->can('dashboard','configure')) throw new Exception("Vous n'avez pas les droits pour rendre ce dashboard obigatoire",403);
  28. require_once(__DIR__.SLASH.'Dashboard.class.php');
  29. $item = Dashboard::provide();
  30. if(!isset($_['user']) || empty($_['user'])) $_['user'] = $myUser->login;
  31. if($myUser->login!=$item->creator && !$myUser->can('dashboard','configure') && $item->id!=0) throw new Exception("Vous n'avez pas les droits pour éditer le dashboard d'un autre utilisateur",403);
  32. if($myUser->login!=$_['user'] && !$myUser->can('dashboard','configure')) throw new Exception("Vous n'avez pas les droits pour créer un dashboard à un autre utilisateur");
  33. $item->user = $_['user'];
  34. $item->label = $_['label'];
  35. $item->icon = $_['icon'];
  36. $item->default = $_['default'];
  37. if($item->default) Dashboard::change(array('default'=>0), array('user'=>$item->user));
  38. $item->mandatory = $_['mandatory'];
  39. if($item->mandatory) Dashboard::change(array('mandatory'=>0));
  40. $item->save();
  41. });
  42. break;
  43. //Récuperation ou edition d'élément dashboard
  44. case 'dashboard_dashboard_edit':
  45. Action::write(function(&$response){
  46. global $myUser,$_;
  47. User::check_access('dashboard','edit');
  48. require_once(__DIR__.SLASH.'Dashboard.class.php');
  49. $response = Dashboard::getById($_['id']);
  50. });
  51. break;
  52. //Suppression d'élement dashboard
  53. case 'dashboard_dashboard_delete':
  54. Action::write(function(&$response){
  55. global $myUser,$_;
  56. User::check_access('dashboard','delete');
  57. require_once(__DIR__.SLASH.'Dashboard.class.php');
  58. $item = Dashboard::provide();
  59. if($myUser->login!=$item->creator && !$myUser->can('dashboard','configure')) throw new Exception("Vous n'avez pas les droits pour supprimer le dashboard d'un autre utilisateur",403);
  60. Dashboard::deleteById($_['id']);
  61. });
  62. break;
  63. //Sauvegarde des configurations de dashboard
  64. case 'dashboard_setting_save':
  65. Action::write(function(&$response){
  66. global $myUser,$_,$conf;
  67. User::check_access('dashboard','configure');
  68. foreach(Configuration::setting('dashboard') as $key=>$value){
  69. if(!is_array($value)) continue;
  70. $allowed[] = $key;
  71. }
  72. foreach ($_['fields'] as $key => $value)
  73. if(in_array($key, $allowed)) $conf->put($key,$value);
  74. });
  75. break;
  76. /** DASHBOARDWIDGET **/
  77. //Récuperation d'une liste de dashboardwidget
  78. case 'dashboard_dashboardwidget_search':
  79. Action::write(function(&$response){
  80. global $myUser,$myFirm,$_;
  81. User::check_access('dashboard','read');
  82. require_once(__DIR__.SLASH.'DashboardWidget.class.php');
  83. require_once(__DIR__.SLASH.'DashboardWidgetShare.class.php');
  84. $models = DashboardWidget::models();
  85. $ranksId = $myUser->getRanksId($myFirm->id);
  86. $widgetsQry = "SELECT {{table}}.*,ds.mandatory,ds.sort as sort
  87. FROM {{table}}
  88. LEFT JOIN ".DashboardWidgetShare::tableName()." ds ON ds.widget={{table}}.id
  89. WHERE dashboard=?
  90. OR {{table}}.id IN (
  91. SELECT widget FROM ".DashboardWidgetShare::tableName()." WHERE (entity=? AND uid=?) OR (entity=? and uid IN (".str_repeat('?,', count($ranksId) - 1) . '?'.")) OR (entity IS NULL)
  92. )
  93. ORDER BY sort, position DESC";
  94. $widgets = DashboardWidget::staticQuery($widgetsQry,array_merge(array($_['dashboard'],'user',$myUser->login,'rank'),$ranksId),true);
  95. foreach($widgets as $widget){
  96. if(!isset($models[$widget->model])) continue;
  97. $model = clone $models[$widget->model];
  98. $row = $model->toArray();
  99. $row['id'] = $widget->id;
  100. $row['width'] = !empty($widget->width) && $widget->width>0 ? $widget->width : $model->defaultWidth;
  101. $row['position'] = $widget->position;
  102. $row['minified'] = $widget->minified;
  103. $row['dashboard'] = $widget->dashboard;
  104. if(!empty($widget->foreign('mandatory'))) $row['mandatory'] = $widget->foreign('mandatory');
  105. if(!empty($widget->foreign('sort'))) $row['position'] = $widget->foreign('sort');
  106. $response['rows'][] = $row;
  107. }
  108. if(isset($response['rows'])){
  109. usort($response['rows'],function($a,$b){
  110. return $a['position'] - $b['position'];
  111. });
  112. }
  113. });
  114. break;
  115. //Ajout ou modification d'élément dashboardwidget
  116. case 'dashboard_dashboardwidget_save':
  117. Action::write(function(&$response){
  118. global $myUser,$_;
  119. User::check_access('dashboard','edit');
  120. require_once(__DIR__.SLASH.'DashboardWidget.class.php');
  121. $item = DashboardWidget::provide();
  122. $item->model = $_['model'];
  123. $item->data = $_['data'];
  124. $item->position = $_['position'];
  125. $item->minified = $_['minified'];
  126. $item->dashboard = $_['dashboard'];
  127. $item->save();
  128. });
  129. break;
  130. //Récuperation ou edition d'élément dashboardwidget
  131. case 'dashboard_dashboardwidget_edit':
  132. Action::write(function(&$response){
  133. global $myUser,$_;
  134. User::check_access('dashboard','edit');
  135. require_once(__DIR__.SLASH.'DashboardWidget.class.php');
  136. $response = DashboardWidget::getById($_['id']);
  137. });
  138. break;
  139. //Suppression d'élement dashboardwidget
  140. case 'dashboard_dashboardwidget_delete':
  141. Action::write(function(&$response){
  142. global $myUser,$_;
  143. User::check_access('dashboard','delete');
  144. require_once(__DIR__.SLASH.'Dashboard.class.php');
  145. require_once(__DIR__.SLASH.'DashboardWidget.class.php');
  146. $widget = DashboardWidget::getById($_['widget']);
  147. if(!$widget) return;
  148. $dashboard = Dashboard::getById($_['dashboard']);
  149. if($widget->dashboard!=$dashboard->id || $dashboard->user!=$myUser->login)
  150. throw new Exception("Vous ne pouvez supprimer que vos propres widgets");
  151. $widget->deleteById($widget->id);
  152. $response['message'] = 'Widget supprimé';
  153. });
  154. break;
  155. //Resize largeur d'élement dashboardwidget
  156. case 'dashboard_dashboardwidget_resize':
  157. Action::write(function(&$response){
  158. global $myUser,$_;
  159. require_once(__DIR__.SLASH.'Dashboard.class.php');
  160. require_once(__DIR__.SLASH.'DashboardWidget.class.php');
  161. User::check_access('dashboard','edit');
  162. $widget = DashboardWidget::getById($_['widget']);
  163. $dashboard = Dashboard::getById($widget->dashboard);
  164. if($widget->dashboard!=$dashboard->id || $dashboard->user!=$myUser->login)
  165. throw new Exception("Vous ne pouvez redimenssioner que vos propres widgets");
  166. $widget->width = $_['width'];
  167. $widget->save();
  168. });
  169. break;
  170. case 'dashboard_dashboardwidget_refresh':
  171. Action::write(function(&$response){
  172. global $myUser,$_;
  173. User::check_access('dashboard','read');
  174. $widgets = array();
  175. Plugin::callHook('widget_refresh',array(&$widgets));
  176. $response['rows'] = $widgets;
  177. });
  178. break;
  179. case 'dashboard_dashboardwidget_add':
  180. Action::write(function(&$response){
  181. global $myUser,$_;
  182. User::check_access('dashboard','edit');
  183. require_once(__DIR__.SLASH.'DashboardWidget.class.php');
  184. $widget = new DashboardWidget();
  185. $widget->model = $_['widget'];
  186. $widget->position = 666;
  187. $widget->minified = false;
  188. $widget->width = $widget->width> 0 ? $widget->width: $widget->defaultWidth ;
  189. $widget->dashboard = $_['dashboard'];
  190. $widget->save();
  191. $response['message'] = 'Widget ajouté';
  192. });
  193. break;
  194. case 'dashboard_dashboardwidget_save_position':
  195. Action::write(function(&$response){
  196. global $myUser,$_;
  197. User::check_access('dashboard','edit');
  198. require_once(__DIR__.SLASH.'Dashboard.class.php');
  199. require_once(__DIR__.SLASH.'DashboardWidget.class.php');
  200. $dashboard = Dashboard::getById($_['dashboard']);
  201. if($dashboard->user!=$myUser->login) throw new Exception("Vous ne pouvez modifier que vos propres widgets");
  202. $dashboard_widgets = DashboardWidget::loadAll( array('dashboard' => $dashboard->id ) );
  203. foreach($_['positions'] as $move){
  204. foreach($dashboard_widgets as $dashboard_widget){
  205. if($dashboard_widget->id!=$move['id']) continue;
  206. $dashboard_widget->position = $move['position'];
  207. $dashboard_widget->save();
  208. }
  209. }
  210. });
  211. break;
  212. /* CLOCK */
  213. case 'dashboard_widget_clock_load':
  214. global $myUser;
  215. User::check_access('dashboard','read');
  216. require_once(__DIR__.SLASH.'DashboardWidget.class.php');
  217. $widget = DashboardWidget::current();
  218. ob_start();
  219. require_once(__DIR__.SLASH.'widget.clock.php');
  220. $widget->content = ob_get_clean();
  221. echo json_encode($widget);
  222. break;
  223. /* LOGS */
  224. case 'dashboard_widget_log_load':
  225. global $myUser;
  226. require_once('DashboardWidget.class.php');
  227. User::check_access('log','read');
  228. $widget = DashboardWidget::current();
  229. $widget->title = '30 derniers logs';
  230. ob_start();
  231. require_once(__DIR__.SLASH.'widget.logs.php');
  232. $widget->content = ob_get_clean();
  233. echo json_encode($widget);
  234. break;
  235. /* PROFILE */
  236. case 'dashboard_widget_profile_load':
  237. global $myUser;
  238. require_once(__DIR__.SLASH.'DashboardWidget.class.php');
  239. User::check_access('dashboard','read');
  240. $widget = DashboardWidget::current();
  241. if(!empty($widget->data('background-color'))) $widget->background = $widget->data('background-color');
  242. ob_start();
  243. require_once(__DIR__.SLASH.'widget.profile.php');
  244. $widget->content = ob_get_clean();
  245. echo json_encode($widget);
  246. break;
  247. case 'dashboard_widget_profile_configure':
  248. global $myUser;
  249. require_once(__DIR__.SLASH.'DashboardWidget.class.php');
  250. User::check_access('dashboard','read');
  251. $widget = DashboardWidget::current();
  252. ob_start();
  253. require_once(__DIR__.SLASH.'widget.profile.configure.php');
  254. $content = ob_get_clean();
  255. echo $content ;
  256. break;
  257. case 'dashboard_widget_profile_configure_save':
  258. Action::write(function(&$response){
  259. global $myUser,$_;
  260. User::check_access('dashboard','read');
  261. require_once(__DIR__.SLASH.'DashboardWidget.class.php');
  262. $widget = DashboardWidget::getById($_['id']);
  263. $widget->data('background-color',$_['widget-profile-background-color']);
  264. $widget->save();
  265. });
  266. break;
  267. /* HTML */
  268. case 'dashboard_widget_html_load':
  269. global $myUser;
  270. require_once(__DIR__.SLASH.'DashboardWidget.class.php');
  271. User::check_access('dashboard','read');
  272. $widget = DashboardWidget::current();
  273. $widget->title = $widget->data('title') != "" ? $widget->data('title') : 'Bloc HTML';
  274. if($widget->data('color') != "") $widget->background = $widget->data('color');
  275. ob_start();
  276. require_once(__DIR__.SLASH.'widget.html.php');
  277. $widget->content = ob_get_clean();
  278. echo json_encode($widget);
  279. break;
  280. case 'dashboard_widget_html_configure':
  281. global $myUser;
  282. User::check_access('dashboard','read');
  283. require_once(__DIR__.SLASH.'DashboardWidget.class.php');
  284. $widget = DashboardWidget::current();
  285. ob_start();
  286. require_once(__DIR__.SLASH.'widget.html.configure.php');
  287. $content = ob_get_clean();
  288. echo $content ;
  289. break;
  290. case 'dashboard_widget_html_configure_save':
  291. Action::write(function(&$response){
  292. global $myUser,$_;
  293. User::check_access('dashboard','read');
  294. require_once(__DIR__.SLASH.'DashboardWidget.class.php');
  295. $widget = DashboardWidget::getById($_['id']);
  296. $widget->data('html',html_entity_decode($_['widget-html-content']));
  297. $widget->data('title',$_['widget-html-title']);
  298. $widget->data('color',$_['widget-html-color']);
  299. $widget->save();
  300. });
  301. break;
  302. /* DASHBOARD SHARE */
  303. /** DASHBOARDWIDGETSHARE **/
  304. //Récuperation d'une liste de dashboardwidgetshare
  305. case 'dashboard_widget_share_search':
  306. Action::write(function(&$response){
  307. global $myUser,$_;
  308. User::check_access('dashboard','read');
  309. require_once(__DIR__.SLASH.'DashboardWidgetShare.class.php');
  310. require_once(__DIR__.SLASH.'DashboardWidget.class.php');
  311. $dashboardwidgetshares = DashboardWidgetShare::loadAll(array(), null, null,array('*'), 1);
  312. foreach($dashboardwidgetshares as $dashboardwidgetshare){
  313. $row = $dashboardwidgetshare->toArray();
  314. $row['widget'] = $dashboardwidgetshare->join('widget')->toArray();
  315. $row['for'] = 'Tout le monde';
  316. if($row['entity'] == 'rank' ) $row['for'] = 'Rang: '. Rank::getById($row['uid'] )->label;
  317. if($row['entity'] == 'user' ) $row['for'] ='Utilisateur: '. User::byLogin($row['uid'] )->fullName();
  318. $response['rows'][] = $row;
  319. }
  320. });
  321. break;
  322. //Ajout ou modification d'élément dashboardwidgetshare
  323. case 'dashboard_widget_share_save':
  324. Action::write(function(&$response){
  325. global $myUser,$_;
  326. User::check_access('dashboard','edit');
  327. require_once(__DIR__.SLASH.'DashboardWidgetShare.class.php');
  328. //DashboardWidgetShare::create();
  329. $item = DashboardWidgetShare::provide();
  330. if(!isset( $_['widget']) || !is_numeric($_['widget'])) throw new Exception("Widget non spécifié ou invalide");
  331. $item->widget = $_['widget'];
  332. $item->mandatory = 1;//$_['mandatory'];
  333. if(isset($_['entity'])){
  334. $item->entity = $_['entity'];
  335. $item->uid = $_['uid'];
  336. }
  337. $item->sort = !isset($_['sort']) ? 0 : $_['sort'];
  338. $item->save();
  339. });
  340. break;
  341. //Récuperation ou edition d'élément dashboardwidgetshare
  342. case 'dashboard_widget_share_edit':
  343. Action::write(function(&$response){
  344. global $myUser,$_;
  345. User::check_access('dashboard','edit');
  346. require_once(__DIR__.SLASH.'DashboardWidgetShare.class.php');
  347. require_once(__DIR__.SLASH.'DashboardWidget.class.php');
  348. $response = DashboardWidgetShare::getById($_['id'],1)->toArray();
  349. });
  350. break;
  351. //Suppression d'élement dashboardwidgetshare
  352. case 'dashboard_widget_share_delete':
  353. Action::write(function(&$response){
  354. global $myUser,$_;
  355. User::check_access('dashboard','delete');
  356. require_once(__DIR__.SLASH.'DashboardWidgetShare.class.php');
  357. DashboardWidgetShare::deleteById($_['id']);
  358. });
  359. break;
  360. }
  361. ?>