ソースを参照

Dashboard : widgets common refresh

idleman 1 年間 前
コミット
da908095f1

+ 1 - 1
class/Configuration.class.php

@@ -7,7 +7,7 @@
  * @license MIT
  */
 class Configuration extends Entity {
-    public $id,$key,$value;
+    public $id,$key,$value,$scope,$uid;
     public $confTab;
     public $entityLabel = 'Configuration';
     protected $fields = array(

+ 4 - 3
plugin/dashboard/DashboardWidget.class.php

@@ -17,6 +17,9 @@ class DashboardWidget extends Entity{
 	public $height = 1; //Hauteur (Nombre Entier)
 	public $dashboard; //Tableau de bord (Nombre Entier)
 
+
+	public $refresh;
+
 	public $icon;
 	public $iconColor;
 	public $titleColor;
@@ -92,14 +95,12 @@ class DashboardWidget extends Entity{
 		return isset($items[$key]) ? $items[$key] : array('label'=>'Non définis');
 	}
 
+	
 
 	public static function model($slug = null){
 		$model = array();
 		Plugin::callHook('widget',array(&$models));
 
-	
-
-
 		$newModel = new DashboardWidget();
 		$newModel->icon = 'far fa-user';
 		$newModel->headerBackground = 'rgb(0, 123, 255)';

+ 37 - 0
plugin/dashboard/action.php

@@ -23,6 +23,9 @@ Action::register('dashboard_widget_search',function(&$response){
 	}
 });
 
+
+
+
 Action::register('dashboard_widget_by_id',function(&$response){
 	global $_;
 	User::check_access('dashboard','read');
@@ -125,6 +128,38 @@ Action::register('dashboard_model_search',function(&$response){
 });
 
 
+Action::register('dashboard_widget_refresh',function(&$response){
+	global $_;
+	User::check_access('dashboard','read');
+	require_once(__DIR__.SLASH.'Dashboard.class.php');
+	require_once(__DIR__.SLASH.'DashboardWidget.class.php');
+
+	$response['rows'] = array();
+	$widgets = array();
+
+	$filters = array();
+	if(empty($_['scope']))  throw new Exception('Portée non spécifiée');
+	$filters['scope'] = $_['scope'];
+	if(empty($_['uid'])) $filters['uid'] = $_['uid'];
+
+	$dashboard = Dashboard::load($filters);
+
+
+	foreach(DashboardWidget::loadAll(array('dashboard'=>$dashboard->id)) as $widget){
+		if(!isset($widgets[$widget->model])) $widgets[$widget->model] = array();
+		$widgets[$widget->model][] = $widget;
+	}
+
+	foreach(DashboardWidget::model() as $model){
+		if(!isset($model->refresh) || !isset($widgets[$model->model]) ) continue;
+		
+		$method = $model->refresh;
+		foreach($widgets[$model->model] as $currentWidget)
+			$method($currentWidget);
+			$response['rows'][] = $currentWidget;
+	}
+});
+
 Action::register('dashboard_widget_configure',function(&$response){
 	global $_;
 	User::check_access('dashboard','edit');
@@ -141,3 +176,5 @@ Action::register('dashboard_widget_configure',function(&$response){
 });
 
 
+
+

+ 19 - 12
plugin/dashboard/dashboard.plugin.php

@@ -200,19 +200,26 @@ function dashboard_application_bottom(){
 }
 
 Plugin::addHook('widget',function(&$models){
-		$clock = new DashboardWidget();
-		$clock->icon = 'far fa-user';
-		$clock->headerBackground = 'rgb(0, 123, 255)';
-		$clock->description = 'Une Horloge toute couillone';
-		$clock->label = 'Horloge';
-		$clock->content = '<div class="text-center m-auto">13:37</div>';
-		$clock->width = 3;
-		$clock->height = 3;
-		$clock->model = 'clock';
-		$clock->refresh = function($widget){
-			$widget->content = '<div class="text-center m-auto">13:38</div>';
+		$model = new DashboardWidget();
+		$model->icon = 'far fa-user';
+		$model->headerBackground = 'rgb(0, 123, 255)';
+		$model->description = 'Une Horloge toute couillone';
+		$model->label = 'Horloge';
+		$model->content = '<div class="text-center m-auto">13:37</div>';
+		$model->width = 3;
+		$model->height = 3;
+		$model->model = 'clock';
+		$model->refresh = function(&$widget){
+			$widget->content = '<div class="text-center m-auto">'.date('H:i:s').'</div>';
+			$widget->label = 'Horloge '.date('d-m-Y H:i:s').'';
 		};
-		$models['clock'] = $clock;
+		$model->configure = function($widget,$form){
+			//conf form @todo
+		};
+		$model->save = function($widget){
+			//save conf action @todo
+		};
+		$models[$model->model] = $model;
 });
 
 

+ 19 - 0
plugin/dashboard/js/component.js

@@ -6,6 +6,7 @@ function init_components_dashboard(input){
 		onRemove: 'dashboard_widget_delete',
 		onResize: 'dashboard_widget_resize',
 		onConfigure: 'dashboard_widget_configure',
+		onRefresh: 'dashboard_widget_refresh',
 		onAdd: 'dashboard_widget_add'
 	},{
 		column: input.attr('data-column'),
@@ -35,6 +36,24 @@ function init_components_dashboard(input){
 		});
 	}
 
+	if(data.onRefresh){
+		setInterval(function(){
+			$.action({
+				action : data.onRefresh,
+				scope: data.scope,
+				uid: data.uid
+			}, function(response){
+				for(var k in response.rows){
+					
+					var widget = $.extend(component.widgets[response.rows[k].id],response.rows[k]);
+					component.widgets[widget.id] = widget;
+			 		component.renderContent(widget.id);
+				}
+			});
+
+		},1000);
+	}
+
 	/*component.addWidgets([{
 		width : 1,
 		height: 1,