action.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. /** DASHBOARDWIDGET / BLOC DE TABLEAU DE BORD **/
  3. Action::register('dashboard_widget_search',function(&$response){
  4. global $_;
  5. User::check_access('dashboard','read');
  6. require_once(__DIR__.SLASH.'DashboardWidget.class.php');
  7. require_once(__DIR__.SLASH.'Dashboard.class.php');
  8. $filters = array();
  9. if(empty($_['scope'])) throw new Exception('Portée non spécifiée');
  10. $filters['scope'] = $_['scope'];
  11. if(empty($_['uid'])) $filters['uid'] = $_['uid'];
  12. $dashboard = Dashboard::load($filters);
  13. $widgets = DashboardWidget::loadAll(array('dashboard'=>$dashboard->id));
  14. $response['widgets'] = array();
  15. foreach($widgets as $widget){
  16. $response['widgets'][] = $widget->toData();
  17. }
  18. });
  19. Action::register('dashboard_widget_by_id',function(&$response){
  20. global $_;
  21. User::check_access('dashboard','read');
  22. require_once(__DIR__.SLASH.'DashboardWidget.class.php');
  23. require_once(__DIR__.SLASH.'Dashboard.class.php');
  24. if(empty($_['id'])) throw new Exception('Id non spécifiée');
  25. $response['item'] = DashboardWidget::getById($_['id'])->toArray();
  26. });
  27. Action::register('dashboard_widget_add',function(&$response){
  28. global $_,$myUser;
  29. User::check_access('dashboard','edit');
  30. require_once(__DIR__.SLASH.'DashboardWidget.class.php');
  31. require_once(__DIR__.SLASH.'Dashboard.class.php');
  32. $filters = array();
  33. if(empty($_['scope'])) throw new Exception('Portée non sépcifiée');
  34. $filters = array('scope'=>$_['scope']);
  35. if(!empty($_['uid'])) $filters['uid'] = $_['uid'];
  36. $dashboard = Dashboard::load($filters);
  37. if(!$dashboard) throw new Exception("Tableau de bord introuvable");
  38. $widget = new DashboardWidget();
  39. $widget->dashboard = $dashboard->id;
  40. if(isset($_['row'])) $widget->row = $_['row'];
  41. if(isset($_['column'])) $widget->column = $_['column'];
  42. $widget->save();
  43. $response = $widget->toData();
  44. });
  45. Action::register('dashboard_widget_move',function(&$response){
  46. global $_;
  47. User::check_access('dashboard','edit');
  48. require_once(__DIR__.SLASH.'DashboardWidget.class.php');
  49. require_once(__DIR__.SLASH.'Dashboard.class.php');
  50. if(empty($_['widget']['id'])) throw new Exception('Aucun identifiant renseigné');
  51. if(!isset($_['widget']['column'])) throw new Exception('Colonne non spécifiée');
  52. if(!isset($_['widget']['row'])) throw new Exception('Ligne non spécifiée');
  53. $widget = DashboardWidget::getById($_['widget']['id'],1);
  54. if(!$widget) throw new Exception('Widget introuvable');
  55. $widget->column = $_['widget']['column'];
  56. $widget->row = $_['widget']['row'];
  57. $widget->save();
  58. });
  59. Action::register('dashboard_widget_resize',function(&$response){
  60. global $_;
  61. User::check_access('dashboard','edit');
  62. require_once(__DIR__.SLASH.'DashboardWidget.class.php');
  63. require_once(__DIR__.SLASH.'Dashboard.class.php');
  64. if(empty($_['widget']['id'])) throw new Exception('Aucun identifiant renseigné');
  65. if(!isset($_['widget']['width'])) throw new Exception('Largeur non spécifiée');
  66. if(!isset($_['widget']['height'])) throw new Exception('Hauteur non spécifiée');
  67. $widget = DashboardWidget::getById($_['widget']['id'],1);
  68. if(!$widget) throw new Exception('Widget introuvable');
  69. $widget->width = $_['widget']['width'];
  70. $widget->height = $_['widget']['height'];
  71. $widget->save();
  72. });
  73. Action::register('dashboard_widget_delete',function(&$response){
  74. global $_;
  75. User::check_access('dashboard','delete');
  76. require_once(__DIR__.SLASH.'DashboardWidget.class.php');
  77. require_once(__DIR__.SLASH.'Dashboard.class.php');
  78. if(empty($_['widget']['id'])) throw new Exception('Aucun identifiant renseigné');
  79. $widget = DashboardWidget::getById($_['widget']['id'],1);
  80. if(!$widget) throw new Exception('Widget introuvable');
  81. DashboardWidget::deleteById($widget->id);
  82. });
  83. Action::register('dashboard_model_search',function(&$response){
  84. global $_;
  85. User::check_access('dashboard','read');
  86. require_once(__DIR__.SLASH.'DashboardWidget.class.php');
  87. require_once(__DIR__.SLASH.'Dashboard.class.php');
  88. $response['rows'] = array();
  89. foreach(DashboardWidget::model() as $model){
  90. $row = $model->toArray();
  91. $row["excerpt"] = truncate($row["description"], $length = 100, array('html'=>true)) ;
  92. if($row["description"] == $row["excerpt"]) $row["description"] = '';
  93. $row['created'] = date('d-m-Y',$row['created']);
  94. $response['rows'][] = $row;
  95. }
  96. unset($response['rows'][DashboardWidget::MODEL_NEW]);
  97. });
  98. Action::register('dashboard_widget_refresh',function(&$response){
  99. global $_;
  100. User::check_access('dashboard','read');
  101. require_once(__DIR__.SLASH.'Dashboard.class.php');
  102. require_once(__DIR__.SLASH.'DashboardWidget.class.php');
  103. $response['rows'] = array();
  104. $widgets = array();
  105. $filters = array();
  106. if(empty($_['scope'])) throw new Exception('Portée non spécifiée');
  107. $filters['scope'] = $_['scope'];
  108. if(empty($_['uid'])) $filters['uid'] = $_['uid'];
  109. $dashboard = Dashboard::load($filters);
  110. foreach(DashboardWidget::loadAll(array('dashboard'=>$dashboard->id)) as $widget){
  111. if(!isset($widgets[$widget->model])) $widgets[$widget->model] = array();
  112. $widgets[$widget->model][] = $widget;
  113. }
  114. foreach(DashboardWidget::model() as $model){
  115. if(!isset($model->content) || is_string($model->content) || !isset($widgets[$model->model]) ) continue;
  116. $method = $model->content;
  117. foreach($widgets[$model->model] as $currentWidget){
  118. $currentWidget->meta = json_decode($currentWidget->meta,true);
  119. if(!is_array($currentWidget->meta)) $currentWidget->meta = array();
  120. $method($currentWidget);
  121. $response['rows'][] = $currentWidget;
  122. }
  123. }
  124. });
  125. Action::register('dashboard_widget_configure',function(&$response){
  126. global $_;
  127. User::check_access('dashboard','edit');
  128. require_once(__DIR__.SLASH.'DashboardWidget.class.php');
  129. require_once(__DIR__.SLASH.'Dashboard.class.php');
  130. if(empty($_['id'])) throw new Exception('Aucun identifiant renseigné');
  131. $widget = DashboardWidget::getById($_['id'],1);
  132. $widget->meta = json_decode($widget->meta,true);
  133. if(!is_array($widget->meta)) $widget->meta = array();
  134. if(!$widget) throw new Exception('Widget introuvable');
  135. switch($_['type']){
  136. case 'style':
  137. $widget->fromArray($_);
  138. break;
  139. case 'model':
  140. $widget->fromArray($_);
  141. break;
  142. case 'properties':
  143. $model = DashboardWidget::model($widget->model);
  144. if(isset($model->save)){
  145. $save = $model->save;
  146. if(!is_array($widget->meta)) $widget->meta = array();
  147. $save($widget,$_['meta']);
  148. }else{
  149. $widget->meta = $_['meta'];
  150. }
  151. break;
  152. }
  153. $widget->save();
  154. });
  155. Action::register('dashboard_widget_configure_form',function(&$response){
  156. global $_;
  157. User::check_access('dashboard','edit');
  158. require_once(__DIR__.SLASH.'DashboardWidget.class.php');
  159. require_once(__DIR__.SLASH.'Dashboard.class.php');
  160. if(empty($_['id'])) throw new Exception('Aucun identifiant renseigné');
  161. $widget = DashboardWidget::getById($_['id'],1);
  162. if(!$widget) throw new Exception('Widget introuvable');
  163. $model = DashboardWidget::model($widget->model);
  164. $widget->meta = json_decode($widget->meta,true);
  165. if(!is_array($widget->meta)) $widget->meta = array();
  166. $configure = $model->configure;
  167. $configure($widget);
  168. exit();
  169. });