action.php 14 KB

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