action.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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->getContent($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) || is_string($model->content) || !isset($widgets[$model->model]) ) continue;
  118. foreach($widgets[$model->model] as $currentWidget){
  119. $model->getContent($currentWidget);
  120. $response['rows'][] = $currentWidget;
  121. }
  122. }
  123. });
  124. Action::register('dashboard_widget_configure',function(&$response){
  125. global $_;
  126. User::check_access('dashboard','edit');
  127. require_once(__DIR__.SLASH.'DashboardWidget.class.php');
  128. require_once(__DIR__.SLASH.'Dashboard.class.php');
  129. if(empty($_['id'])) throw new Exception('Aucun identifiant renseigné');
  130. $widget = DashboardWidget::getById($_['id'],1);
  131. $widget->meta = json_decode($widget->meta,true);
  132. if(!is_array($widget->meta)) $widget->meta = array();
  133. if(!$widget) throw new Exception('Widget introuvable');
  134. switch($_['type']){
  135. case 'style':
  136. $widget->fromArray($_);
  137. break;
  138. case 'model':
  139. $widget->fromArray($_);
  140. break;
  141. case 'properties':
  142. $model = DashboardWidget::model($widget->model);
  143. if(isset($model->save)){
  144. $save = $model->save;
  145. if(!is_array($widget->meta)) $widget->meta = array();
  146. $save($widget,$_['meta']);
  147. }else{
  148. $widget->meta = $_['meta'];
  149. }
  150. break;
  151. }
  152. $widget->save();
  153. });
  154. Action::register('dashboard_widget_configure_form',function(&$response){
  155. global $_;
  156. User::check_access('dashboard','edit');
  157. require_once(__DIR__.SLASH.'DashboardWidget.class.php');
  158. require_once(__DIR__.SLASH.'Dashboard.class.php');
  159. if(empty($_['id'])) throw new Exception('Aucun identifiant renseigné');
  160. $widget = DashboardWidget::getById($_['id'],1);
  161. if(!$widget) throw new Exception('Widget introuvable');
  162. $model = DashboardWidget::model($widget->model);
  163. $widget->meta = json_decode($widget->meta,true);
  164. if(!is_array($widget->meta)) $widget->meta = array();
  165. $configure = $model->configure;
  166. $configure($widget);
  167. exit();
  168. });