DashboardWidget.class.php 4.6 KB

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