action.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. $method($currentWidget);
  119. $response['rows'][] = $currentWidget;
  120. }
  121. });
  122. Action::register('dashboard_widget_configure',function(&$response){
  123. global $_;
  124. User::check_access('dashboard','edit');
  125. require_once(__DIR__.SLASH.'DashboardWidget.class.php');
  126. require_once(__DIR__.SLASH.'Dashboard.class.php');
  127. if(empty($_['id'])) throw new Exception('Aucun identifiant renseigné');
  128. $widget = DashboardWidget::getById($_['id'],1);
  129. if(!$widget) throw new Exception('Widget introuvable');
  130. $widget->fromArray($_);
  131. $widget->save();
  132. });