action.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. $model = DashboardWidget::model($widget->model);
  17. $model->refresh($widget);
  18. $response['widgets'][] = $widget->toData();
  19. }
  20. });
  21. Action::register('dashboard_widget_by_id',function(&$response){
  22. global $_;
  23. User::check_access('dashboard','read');
  24. require_once(__DIR__.SLASH.'DashboardWidget.class.php');
  25. require_once(__DIR__.SLASH.'Dashboard.class.php');
  26. if(empty($_['id'])) throw new Exception('Id non spécifiée');
  27. $response['item'] = DashboardWidget::getById($_['id'])->toArray();
  28. });
  29. Action::register('dashboard_widget_add',function(&$response){
  30. global $_,$myUser;
  31. User::check_access('dashboard','edit');
  32. require_once(__DIR__.SLASH.'DashboardWidget.class.php');
  33. require_once(__DIR__.SLASH.'Dashboard.class.php');
  34. $filters = array();
  35. if(empty($_['scope'])) throw new Exception('Portée non sépcifiée');
  36. $filters = array('scope'=>$_['scope']);
  37. if(!empty($_['uid'])) $filters['uid'] = $_['uid'];
  38. $dashboard = Dashboard::load($filters);
  39. if(!$dashboard) throw new Exception("Tableau de bord introuvable");
  40. $widget = new DashboardWidget();
  41. $widget->dashboard = $dashboard->id;
  42. if(isset($_['row'])) $widget->row = $_['row'];
  43. if(isset($_['column'])) $widget->column = $_['column'];
  44. $widget->save();
  45. $response = $widget->toData();
  46. });
  47. Action::register('dashboard_widget_move',function(&$response){
  48. global $_;
  49. User::check_access('dashboard','edit');
  50. require_once(__DIR__.SLASH.'DashboardWidget.class.php');
  51. require_once(__DIR__.SLASH.'Dashboard.class.php');
  52. if(empty($_['widget']['id'])) throw new Exception('Aucun identifiant renseigné');
  53. if(!isset($_['widget']['column'])) throw new Exception('Colonne non spécifiée');
  54. if(!isset($_['widget']['row'])) throw new Exception('Ligne non spécifiée');
  55. $widget = DashboardWidget::getById($_['widget']['id'],1);
  56. if(!$widget) throw new Exception('Widget introuvable');
  57. $widget->column = $_['widget']['column'];
  58. $widget->row = $_['widget']['row'];
  59. $widget->save();
  60. });
  61. Action::register('dashboard_widget_resize',function(&$response){
  62. global $_;
  63. User::check_access('dashboard','edit');
  64. require_once(__DIR__.SLASH.'DashboardWidget.class.php');
  65. require_once(__DIR__.SLASH.'Dashboard.class.php');
  66. if(empty($_['widget']['id'])) throw new Exception('Aucun identifiant renseigné');
  67. if(!isset($_['widget']['width'])) throw new Exception('Largeur non spécifiée');
  68. if(!isset($_['widget']['height'])) throw new Exception('Hauteur non spécifiée');
  69. $widget = DashboardWidget::getById($_['widget']['id'],1);
  70. if(!$widget) throw new Exception('Widget introuvable');
  71. $widget->width = $_['widget']['width'];
  72. $widget->height = $_['widget']['height'];
  73. $widget->save();
  74. });
  75. Action::register('dashboard_widget_delete',function(&$response){
  76. global $_;
  77. User::check_access('dashboard','delete');
  78. require_once(__DIR__.SLASH.'DashboardWidget.class.php');
  79. require_once(__DIR__.SLASH.'Dashboard.class.php');
  80. if(empty($_['widget']['id'])) throw new Exception('Aucun identifiant renseigné');
  81. $widget = DashboardWidget::getById($_['widget']['id'],1);
  82. if(!$widget) throw new Exception('Widget introuvable');
  83. DashboardWidget::deleteById($widget->id);
  84. });
  85. Action::register('dashboard_model_search',function(&$response){
  86. global $_;
  87. User::check_access('dashboard','read');
  88. require_once(__DIR__.SLASH.'DashboardWidget.class.php');
  89. require_once(__DIR__.SLASH.'Dashboard.class.php');
  90. $response['rows'] = array();
  91. foreach(DashboardWidget::model() as $model){
  92. $row = $model->toArray();
  93. $row["excerpt"] = truncate($row["description"], $length = 100, array('html'=>true)) ;
  94. if($row["description"] == $row["excerpt"]) $row["description"] = '';
  95. $row['created'] = date('d-m-Y',$row['created']);
  96. $response['rows'][] = $row;
  97. }
  98. unset($response['rows'][DashboardWidget::MODEL_NEW]);
  99. });
  100. Action::register('dashboard_widget_refresh',function(&$response){
  101. global $_;
  102. User::check_access('dashboard','read');
  103. require_once(__DIR__.SLASH.'Dashboard.class.php');
  104. require_once(__DIR__.SLASH.'DashboardWidget.class.php');
  105. $response['rows'] = array();
  106. $widgets = array();
  107. $filters = array();
  108. if(empty($_['scope'])) throw new Exception('Portée non spécifiée');
  109. $filters['scope'] = $_['scope'];
  110. if(empty($_['uid'])) $filters['uid'] = $_['uid'];
  111. $dashboard = Dashboard::load($filters);
  112. foreach(DashboardWidget::loadAll(array('dashboard'=>$dashboard->id)) as $widget){
  113. if(!isset($widgets[$widget->model])) $widgets[$widget->model] = array();
  114. $widgets[$widget->model][] = $widget;
  115. }
  116. foreach(DashboardWidget::model() as $model){
  117. if(!isset($model->content) || !$model->live || is_string($model->content) || !isset($widgets[$model->model]) ) continue;
  118. foreach($widgets[$model->model] as $currentWidget){
  119. $oldWidget = clone $currentWidget;
  120. $model->refresh($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. if(is_string($widget->meta)) $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. if(!is_array($widget->meta)){
  165. $widget->meta = json_decode($widget->meta,true);
  166. if(!is_array($widget->meta)) $widget->meta = array();
  167. }
  168. if(!isset($model->configure)) $model->configure = function($widget){
  169. echo 'Aucune configuration disponible';
  170. return;
  171. };
  172. $configure = $model->configure;
  173. $configure($widget);
  174. exit();
  175. });