dashboard.plugin.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. /*
  3. @name Dashboard
  4. @author Valentin CARRUESCO <idleman@idleman.fr>
  5. @link http://blog.idleman.fr
  6. @licence CC by nc sa
  7. @version 1.0.0
  8. @description Active la dashboard dynamique utilisable par d'autres plugins
  9. @type plugin
  10. */
  11. function dashboard_plugin_actions(){
  12. global $myUser,$_,$conf;
  13. switch($_['action']){
  14. case 'GET_WIDGETS':
  15. header('Content-type: application/json');
  16. require_once(dirname(__FILE__).'/Dashboard.class.php');
  17. require_once(dirname(__FILE__).'/Widget.class.php');
  18. $dashManager = new Dashboard();
  19. $dashManager->change(array('default'=>'0'),array('user'=>$myUser->getId()));
  20. $dashManager->change(array('default'=>'1'),array('id'=>$_['dashboard'],'user'=>$myUser->getId()));
  21. $widgetManager = new Widget();
  22. $model = array();
  23. Plugin::callHook("widgets",array(&$model));
  24. $widgets = $widgetManager->loadAll(array('dashboard'=>$_['dashboard']),'cell');
  25. $data = array();
  26. foreach($widgets as $widget){
  27. if(!is_active_model($widget->model,$model)) continue;
  28. $data[] = array('data'=>$widget->data,
  29. 'column'=>$widget->column,
  30. 'id'=>$widget->id,
  31. 'cell'=>$widget->cell,
  32. 'minified'=>$widget->minified,
  33. 'model'=>$widget->model
  34. );
  35. }
  36. echo json_encode(array('model'=>$model,'data'=>$data));
  37. break;
  38. case 'ADD_WIDGET':
  39. header('Content-type: application/json');
  40. require_once(dirname(__FILE__).'/Widget.class.php');
  41. $response = array();
  42. $widget = new Widget();
  43. $widget->data = json_encode($_POST['data']);
  44. $widget->column = $_['column'];
  45. $widget->cell = $_['cell'];
  46. $widget->model = $_['model'];
  47. $widget->dashboard = $_['view'];
  48. $widget->save();
  49. $response['id'] = $widget->id;
  50. echo json_encode($response);
  51. break;
  52. case 'MINIMIZE_WIDGET':
  53. header('Content-type: application/json');
  54. require_once(dirname(__FILE__).'/Widget.class.php');
  55. $response = array();
  56. $widgetManager = new Widget();
  57. $widgetManager = $widgetManager->getById($_['id']);
  58. $widgetManager->minified = 1;
  59. $widgetManager->save();
  60. echo json_encode($response);
  61. break;
  62. case 'MAXIMIZE_WIDGET':
  63. header('Content-type: application/json');
  64. require_once(dirname(__FILE__).'/Widget.class.php');
  65. $response = array();
  66. $widgetManager = new Widget();
  67. $widgetManager = $widgetManager->getById($_['id']);
  68. $widgetManager->minified = 0;
  69. $widgetManager->save();
  70. echo json_encode($response);
  71. break;
  72. case 'MOVE_WIDGET':
  73. header('Content-type: application/json');
  74. require_once(dirname(__FILE__).'/Widget.class.php');
  75. $response = array();
  76. $widgetManager = new Widget();
  77. foreach($_['sort']['cells'] as $id=>$sort){
  78. if(empty($sort)) continue;
  79. $widgetManager->change(array('cell'=>$sort['cell'],'column'=>$sort['column']),array('id'=>$id));
  80. }
  81. echo json_encode($response);
  82. break;
  83. case 'DELETE_WIDGET':
  84. header('Content-type: application/json');
  85. require_once(dirname(__FILE__).'/Widget.class.php');
  86. $response = array();
  87. $widgetManager = new Widget();
  88. $widgetManager->delete(array('id'=>$_['id']));
  89. echo json_encode($response);
  90. break;
  91. case 'DASH_ADD_VIEW':
  92. global $_,$myUser;
  93. require_once(dirname(__FILE__).'/Dashboard.class.php');
  94. $entity = new Dashboard();
  95. $entity->user = $myUser->getId();
  96. $entity->label = $_['viewName'];
  97. $entity->default = 0;
  98. $entity->save();
  99. header('location: setting.php?section=preference&block=dashboard');
  100. break;
  101. case 'DASH_DELETE_VIEW':
  102. global $_,$myUser;
  103. require_once(dirname(__FILE__).'/Dashboard.class.php');
  104. $entity = new Dashboard();
  105. $entity->delete(array('id'=>$_['id']));
  106. header('location: setting.php?section=preference&block=dashboard');
  107. break;
  108. }
  109. }
  110. function dashboard_plugin_home(){
  111. global $_,$myUser;
  112. if(!isset($_['module'])){
  113. require_once(dirname(__FILE__).'/Dashboard.class.php');
  114. require_once(dirname(__FILE__).'/Widget.class.php');
  115. $dashManager = new Dashboard();
  116. $dashes = $dashManager->loadAll(array('user'=>$myUser->getId()),'label');
  117. if(count($dashes) == 0){
  118. Dashboard::initForUser($myUser->getId());
  119. $dashes = $dashManager->loadAll(array('user'=>$myUser->getId()),'label');
  120. }
  121. echo '<div style="margin:0;text-align:center;"><select id="dashboard_switch" onchange="plugin_dashboard_load_view($(this).val());"><option value="">-</option>';
  122. foreach($dashes as $dash){
  123. echo '<option '.($dash->default=='1'?'selected="selected"':'').' value="'.$dash->id.'">'.$dash->label.'</option>';
  124. }
  125. echo '</select></div>';
  126. echo '<div id="dashboard"></div>';
  127. }
  128. }
  129. function dashboard_plugin_preference_menu(){
  130. global $_;
  131. echo '<li '.(@$_['block']=='dashboard'?'class="active"':'').'><a href="setting.php?section=preference&block=dashboard"><i class="fa fa-angle-right"></i> Dashboard</a></li>';
  132. }
  133. function dashboard_plugin_preference_page(){
  134. global $myUser,$_,$conf;
  135. if((isset($_['section']) && $_['section']=='preference' && @$_['block']=='dashboard' ) ){
  136. if($myUser!=false){
  137. require_once(dirname(__FILE__).'/Dashboard.class.php');
  138. $dashManager = new Dashboard();
  139. $dashes = $dashManager->loadAll(array('user'=>$myUser->getId()));
  140. ?>
  141. <div class="span9 userBloc">
  142. <legend>Dashboard disponibles</legend>
  143. <form style="margin:0;" action="action.php?action=DASH_ADD_VIEW" method="POST">
  144. <div class="input-append">
  145. <input type="text" name="viewName" placeholder="Salon,cuisine...">
  146. <button type="submit" class="btn">Ajouter la vue</button>
  147. </div>
  148. </form>
  149. <table class="table table-striped table-bordered">
  150. <tr>
  151. <th>Nom</th>
  152. <th>Options</th>
  153. </tr>
  154. <?php foreach($dashes as $dash){ ?>
  155. <tr class="command" data-id="<?php echo $dash->id; ?>">
  156. <td><?php echo $dash->label; ?></td>
  157. <td><a class="btn" href="action.php?action=DASH_DELETE_VIEW&id=<?php echo $dash->id; ?>"><i class="fa fa-times"></i></a></td>
  158. </tr>
  159. <?php } ?>
  160. <tr>
  161. <td colspan="3"><div class="btn" onclick="plugin_vocalinfo_save();">Enregistrer</div></td>
  162. </tr>
  163. </table>
  164. </div>
  165. <?php }else{ ?>
  166. <div id="main" class="wrapper clearfix">
  167. <article>
  168. <h3>Vous devez être connecté</h3>
  169. </article>
  170. </div>
  171. <?php
  172. }
  173. }
  174. }
  175. function is_active_model($model,$models){
  176. foreach($models as $m){
  177. if($m['uid'] == $model) return true;
  178. }
  179. return false;
  180. }
  181. Plugin::addJs('/js/jquery.dashboard.js',true);
  182. Plugin::addJs('/js/main.js',true);
  183. Plugin::addCss('/css/jquery.dashboard.css',true);
  184. Plugin::addHook("home", "dashboard_plugin_home");
  185. Plugin::addHook("action_post_case", "dashboard_plugin_actions");
  186. Plugin::addHook("preference_menu", "dashboard_plugin_preference_menu");
  187. Plugin::addHook("preference_content", "dashboard_plugin_preference_page");
  188. ?>