DashboardWidget.class.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. /**
  3. * Define a Bloc de tableau de bord
  4. * @author Jean CARRUESCO
  5. * @category Plugin
  6. * @license copyright
  7. */
  8. class DashboardWidget extends Entity{
  9. public $id;
  10. public $model; //Modèle (Texte)
  11. public $type; //Type (Liste classique)
  12. public $meta = array(); //Meta (Texte Long)
  13. public $column = 0 ; //Colonne (Nombre Entier)
  14. public $row = 0 ; //Ligne (Nombre Entier)
  15. public $width = 1; //Largeur (Nombre Entier)
  16. public $height = 1; //Hauteur (Nombre Entier)
  17. public $dashboard; //Tableau de bord (Nombre Entier)
  18. public $refresh;
  19. public $live = false;
  20. public $icon;
  21. public $iconColor;
  22. public $titleColor;
  23. public $headerBackground;
  24. public $bodyBackground;
  25. public $bodyColor;
  26. public $description;
  27. public $label;
  28. public $content;
  29. public $uid;
  30. public const MODEL_NEW = 'new';
  31. protected $TABLE_NAME = 'dashboard_widget';
  32. public $entityLabel = 'Bloc de tableau de bord';
  33. public $fields = array(
  34. 'id' => array('type'=>'key', 'label' => 'Identifiant'),
  35. 'model' => array('type'=>'text','label' => 'Modèle'),
  36. 'type' => array('type'=>'list','label' => 'Type'),
  37. 'meta' => array('type'=>'textarea','label' => 'Meta'),
  38. 'column' => array('type'=>'integer','label' => 'Colonne'),
  39. 'row' => array('type'=>'integer','label' => 'Ligne'),
  40. 'width' => array('type'=>'integer','label' => 'Largeur'),
  41. 'height' => array('type'=>'integer','label' => 'Hauteur'),
  42. 'icon' => array('type'=>'icon','label' => 'Icône'),
  43. 'iconColor' => array('type'=>'color','label' => 'Couleur icône'),
  44. 'titleColor' => array('type'=>'color','label' => 'Couleur titre'),
  45. 'headerBackground' => array('type'=>'color','label' => 'Couleur de fond en-têtes'),
  46. 'bodyBackground' => array('type'=>'color','label' => 'Couleur de fond du corps'),
  47. 'bodyColor' => array('type'=>'color','label' => 'Couleur texte du corps'),
  48. 'dashboard' => array('type'=>'integer','label' => 'Tableau de bord','link'=>'plugin/dashboard/Dashboard.class.php')
  49. );
  50. //Colonnes indexées
  51. public $indexes = array();
  52. public function __construct(){
  53. $this->model = self::MODEL_NEW;
  54. parent::__construct();
  55. }
  56. public function save(){
  57. if(is_array($this->meta)) $this->meta = json_encode($this->meta);
  58. parent::save();
  59. }
  60. public function toArray($decoded=false){
  61. return array_merge(array(
  62. 'icon' => $this->icon,
  63. 'headerBackground' => $this->headerBackground,
  64. 'description' => $this->description,
  65. 'label' => $this->label,
  66. 'content' => $this->content
  67. ),parent::toArray($decoded));
  68. }
  69. function toData(){
  70. $row = $this->toArray();
  71. $model = DashboardWidget::model($this->model);
  72. $finalRow = $model->toArray();
  73. if(isset($model->css)){
  74. if(!isset($finalRow['cssUrl'])) $finalRow['cssUrl'] = array();
  75. foreach($model->css as $css){
  76. $finalRow['cssUrl'][] = ROOT_URL.'/'.str_replace(array('\\','/'),SLASH,str_replace(__ROOT__,'',$css));
  77. }
  78. }
  79. if(isset($model->js)){
  80. if(!isset($finalRow['jsUrl'])) $finalRow['jsUrl'] = array();
  81. foreach($model->js as $js){
  82. $finalRow['jsUrl'][] = ROOT_URL.'/'.str_replace(array('\\','/'),SLASH,str_replace(__ROOT__,'',$js));
  83. }
  84. }
  85. foreach($row as $attribute=>$value){
  86. if(isset($value)) $finalRow[$attribute] = $value;
  87. }
  88. return $finalRow;
  89. }
  90. //liste des Type possibles
  91. public static function types($key=null){
  92. $items = array(
  93. 'widget' => array('label'=>'Widget'),
  94. 'model' => array('label'=>'Modèle de widget'),
  95. );
  96. if(!isset($key)) return $items;
  97. return isset($items[$key]) ? $items[$key] : array('label'=>'Non définis');
  98. }
  99. public function refresh(&$widget){
  100. if(!isset($this->content) || is_string($this->content) ) return $this->content;
  101. $method = $this->content;
  102. $widget->meta = json_decode($widget->meta,true);
  103. if(!is_array($widget->meta)) $widget->meta = array();
  104. $method($widget);
  105. }
  106. public static function model($slug = null){
  107. $model = array();
  108. Plugin::callHook('widget',array(&$models));
  109. $newModel = new DashboardWidget();
  110. $newModel->icon = 'far fa-user';
  111. $newModel->headerBackground = 'rgb(0, 123, 255)';
  112. $newModel->description = 'Type de widget non défini';
  113. $newModel->label = 'Nouveau widget';
  114. $newModel->content = '<small class="text-center m-auto text-muted">Veuillez configurer votre widget</small>';
  115. $newModel->width = 3;
  116. $newModel->configure = function($widget){
  117. echo 'Veuillez sélectionner un type de widget avant de le configurer';
  118. return;
  119. };
  120. $newModel->height = 2;
  121. $newModel->model = self::MODEL_NEW;
  122. $models[self::MODEL_NEW] = $newModel;
  123. foreach($models as $model){
  124. if(!isset($model->model)) continue;
  125. if(empty($model->label)) $model->label = 'Sans titre';
  126. $models[$model->model] = $model;
  127. }
  128. if(!isset($slug)) return $models;
  129. return isset($models[$slug]) ? $models[$slug] : $model;
  130. }
  131. }