noalyss Version-9
NOALYSS : serveur de comptabilité et ERP (2002)
Loading...
Searching...
No Matches
widget.php
Go to the documentation of this file.
1<?php
2/*
3 * This file is part of NOALYSS.
4 *
5 * NOALYSS is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * NOALYSS is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with NOALYSS; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18*/
19// Copyright Author Dany De Bontridder danydb@aevalys.eu 10/08/24
20/*!
21 * \file
22 * \brief Main class for widget
23 */
24
25namespace Noalyss\Widget;
26
27/*!
28 *\class
29 * \brief Main class for widget
30 */
31abstract class Widget
32{
33
34 public function __construct(protected int $user_widget_id=0,protected string $widget_code="",protected $db=null)
35 {
36 if ($db == null) {
37 $this->db=\Dossier::connect();
38 }
39 }
40
41 public function get_user_widget_id(): int
42 {
43 return $this->user_widget_id;
44 }
45
46 public function set_user_widget_id(int $user_widget_id): Widget
47 {
48 $this->user_widget_id = $user_widget_id;
49 return $this;
50 }
51
52 public function get_widget_code(): string
53 {
54 return $this->widget_code;
55 }
56
57 public function set_widget_code(string $widget_code): Widget
58 {
59 $this->widget_code = $widget_code;
60 return $this;
61 }
62
63 /**
64 * @brief display the content for the current connected user of the widget with the parameter
65 * @return mixed
66 */
67 abstract function display();
68
69 /**
70 * @brief display a description of the widget and allow to save it for the current user, call input_param function
71 * of the widget if it exists
72 * @return mixed
73 */
74 function input($flnumber=true)
75 {
76 static $nb=0;
77 $nb++;
78 //read description from database
79 $row=$this->db->get_row("
80 select
81 wd_code
82 ,wd_name
83 ,wd_parameter,
84 wd_description
85 from
86 widget_dashboard
87 where
88 wd_code=$1",
89 [$this->widget_code]);
90 $strNumber="";
91 if ( $flnumber) $strNumber="[ $nb ]";
92 echo "<li id=\"elt_{$this->user_widget_id}\"> $strNumber <span class='widget-name'>{$row['wd_name']}</span>{$row['wd_description']}";
93
94 if ( $this->user_widget_id > 0) {
95 if ( $row['wd_parameter'] == 1) {
96 $this->display_parameter();
97 }
98 echo '<span style="float:right;color:red">'.\Icon_Action::trash(uniqid(),sprintf("widget.delete('%s')",$this->user_widget_id));
99 }
100 else {
101 if ( $row['wd_parameter'] == 1) {
102 $this->input_parameter();
103 }
104 echo '<span style="float:right;">'.\Icon_Action::icon_add(uniqid(),sprintf("widget.add('%s')",$this->widget_code));
105 }
106
107 echo '</span>';
108 echo '</li>';
109 }
110
111 /**
112 * @brief returns an array of widget for the connected user, ordered
113 * @return array [ uw_id,dashboard_widget_id,wd_code,wd_description
114 */
115 static function get_enabled_widget():array
116 {
117 global $g_user,$cn;
118 return $cn->get_array("
119 select
120 uw.uw_id,
121 uw.dashboard_widget_id ,
122 wd.wd_code,
123 wd.wd_description
124 from user_widget uw
125join widget_dashboard wd on (uw.dashboard_widget_id=wd.wd_id)
126where use_login=$1 order by uw.uw_order
127",[$g_user->login]);
128
129 }
130
131 /**
132 * @brief Build a widget thank the user_widget_id (SQL :PK : USER_WIDGET.UW_ID) and $widget_code
133 * @param $user_widget_id integer (SQL :PK : USER_WIDGET.UW_ID)
134 * @param $widget_code string (SQL WIDGET_DASHBOARD.WD_CODE)
135 * @return Widget
136 */
137 static function build_user_widget($user_widget_id,$widget_code):?Widget
138 {
139 // load the class if file (code/code.php) exists.
140 if (file_exists(NOALYSS_INCLUDE."/widget/$widget_code/$widget_code.php")) {
141 require_once NOALYSS_INCLUDE."/widget/$widget_code/$widget_code.php";
142 $class=sprintf("\\Noalyss\\Widget\\%s",$widget_code);
143 $obj= new $class;
144 $obj->set_widget_code($widget_code);
145 $obj->set_user_widget_id($user_widget_id);
146 return $obj;
147 }
148
149 // return the object
150 return null;
151 }
152
153 /**
154 * @brief output the DIV HTML with class and id for the widget
155 * @return void
156 */
157 function open_div() {
158 printf( '<div id="%s" class="box widget-box">',$this->get_div_domid());
159 }
160
161 /**
162 * @brief compute the DIV ID
163 * @return string
164 */
165 function get_div_domid() :string {
166 return sprintf( "%s_%s",$this->widget_code,$this->user_widget_id);
167 }
168 function close_div() {
169 echo '</div>';
170 }
171
172 /**
173 * @brief display a box and fills it with the content of an ajax calls , the ajax calls Widget::display
174 * @param Widget $widget
175 * @return void
176 */
177 static function ajax_display(Widget $widget ){
178 $box= sprintf( '%s_%s',$widget->get_widget_code(),$widget->get_user_widget_id());
179 $widget->open_div();
180 echo h2(_("Un instant, on charge :-)"),' class="title" ');
181
182
183 print '<div style="display:flex;justify-content: center">';
184 print '<div style="margin-top: 50px;margin-left: 20px;">';
185 print '<div class="loading_msg"></div>';
186 print '<div class="loading_msg"></div>';
187 print '<div class="loading_msg"></div>';
188 print '<div class="loading_msg"></div>';
189 print '<div class="loading_msg"></div>';
190 print '</div>';
191 print '</div>';
192
193 $widget->close_div();
194
195 $dossier_id=\Dossier::id();
196 $widgetjs=uniqid('widget');
197 echo <<<EOF
198<script>
199var {$widgetjs}= new Widget('{$dossier_id}')
200{$widgetjs}.display('{$box}',{$widget->get_user_widget_id()},'{$widget->get_widget_code()}')
201</script>
202
203
204EOF;
205
206
207
208 }
209
210 /**
211 * @brier display activated widgets
212 * @return void
213 */
214 static function display_available()
215 {
216
218 echo '<ul class="list-unstyled" id="contain_widget">';
219 foreach ($aWidget as $item) {
220 $widget=Widget::build_user_widget($item['uw_id'],$item['wd_code']);
221 $widget->input();
222 }
223 echo '</ul>';
224 echo \HtmlInput::hidden("order_widget_hidden", "");
225 create_script("widget.create_sortable()");
226 }
227
228 /**
229 * @brief save widget order from an array
230 * @param $array array of USER_WIDGET.UW_ID
231 * @return void
232 * @exception DatabaseCore fails , cannot update
233 */
234 static function save($array) {
235 global $cn,$g_user;
236 if (empty($array)) {
237 $cn->exec_sql("delete from user_widget where use_login = $1",[$g_user->getLogin()]);
238 return;
239 }
240 try {
241 $cn->start();
242 $order=10;
243 $cn->exec_sql("create temporary table tmp_widget(user_widget_id integer,tw_order integer )");
244 foreach ($array as $item) {
245 $cn->exec_sql('insert into tmp_widget(user_widget_id,tw_order ) values ($1,$2)',
246 [$item,$order]);
247 $order+=20;
248 }
249 $cn->exec_sql("delete from user_widget where use_login = $1 and uw_id not in (select user_widget_id from tmp_widget)",
250 array($g_user->getLogin()));
251
252 $cn->exec_sql("update user_widget set uw_order =tw_order from tmp_widget where user_widget_id=uw_id");
253
254 $cn->commit();
255
256 } catch (\Exception $e) {
257 throw ($e);
258 }
259
260
261 }
262
263 /**
264 * @brief show all the widget that can be added
265 * @return void
266 */
267 public static function select_available()
268 {
269 global $cn;
271 $aWidget=$cn->get_array("select wd_code,wd_name, wd_description,wd_parameter from widget_dashboard order by wd_name");
272 echo '<ul id="widget_add" class="list-unstyled">';
273 foreach ($aWidget as $item) {
274 $widget=Widget::build_user_widget(-1,$item['wd_code']);
275 $widget?->input(false);
276
277 }
278 echo '</ul>';
279 }
280
281 /**
282 * @brief open a form with the DOMID "widget_code"_param, it appears once only for each widget in the dialog box
283 * for adding widget to the dashboard
284 * @param $html_input string HTML string with all the HTML INPUT that will be enclosed by the FORM
285 * @return void
286 */
287 function make_form($html_input)
288 {
289 printf ('<form id="%s_param" style="display:inline">',$this->widget_code);
290 echo $html_input;
291 printf ('</form>');
292 }
293
294 /**
295 * @brief MUST BE overrided if the widget needs extra parameters, create a FORM to add extra-parameter
296 * @return void
297 * @throws \Exception
298 */
299 function input_parameter ()
300 {
301 throw new \Exception(__FUNCTION__." not implemented");
302 }
303
304 /**
305 * @brief MUST BE overrided if the widget needs extra parameters, display the content of extra-parameter
306 * @param $user_widget_id
307 * @return void
308 * @throws \Exception
309 */
310
311 function display_parameter() {
312 throw new \Exception(__FUNCTION__." not implemented");
313 }
314
315 /**
316 * @brief scan folder to find install.php file , include them if the code is not in DB
317 * @return void
318 */
319 static function scanfolder()
320 {
321 global $cn;
322 $handle=opendir(NOALYSS_INCLUDE."/widget");
323 while (($dir = readdir($handle)) != false ) {
324 $directory=NOALYSS_INCLUDE."/widget".DIRECTORY_SEPARATOR.$dir;
325 if (is_dir($directory) && $dir != "." && $dir != "..") {
326 // code exists in DB ?
327 $cnt=$cn->get_value("select count(*) from widget_dashboard where wd_code = $1",[$dir]);
328 if ( $cnt > 0) {
329 continue;
330 } else {
331 // include the install.php file if any and install the widget
332 if (file_exists($directory . DIRECTORY_SEPARATOR . "install.php")){
333 include $directory . DIRECTORY_SEPARATOR . "install.php";
334 }
335 }
336 }
337 }
338
339 }
340
341 /**
342 * @brief get the parameter of the widget and returns an array
343 * @return array key=>value
344 */
345 function get_parameter()
346 {
347 $param = $this->db->get_value("select uw_parameter from user_widget where uw_id=$1",[$this->user_widget_id]);
348 if (empty ($param)) return [];
349 parse_str($param,$aParam);
350 return $aParam;
351 }
352
353 /**
354 * @brief compute the button ZOOM to put in the title
355 * @return \html
356 */
357 function button_zoom() {
358 $bt = \Icon_Action::zoom(uniqid(), sprintf("widget.toggle_full_size('%s')",$this->get_div_domid()));
359 return $bt;
360 }
361 /**
362 * @brief display the title and the icon for zooming
363 * @param $title string title of the widget
364 */
365 function title( $title) {
366
367 $r='<div class="bxbutton">';
368 $r.='<span id="span_'.uniqid().'" style="float:right;margin-right:5px">'.$this->button_zoom()."</span>";
369 $r.='</div>';
370 $r.=sprintf('<h2 class="title">%s</h2>',$title);
371 echo $r;
372 }
373}
h2($p_string, $p_class="", $raw="")
Definition ac_common.php:68
global $g_user
if no group available , then stop
$dossier_id
catch(Exception $exc) if(! $g_user->can_write_action($ag_id)) $r
_("actif, passif,charge,...")
static zoom($p_div, $p_javascript)
Display a icon for zooming.
Main class for widget.
Definition widget.php:32
get_parameter()
get the parameter of the widget and returns an array
Definition widget.php:345
set_widget_code(string $widget_code)
Definition widget.php:57
open_div()
output the DIV HTML with class and id for the widget
Definition widget.php:157
static ajax_display(Widget $widget)
display a box and fills it with the content of an ajax calls , the ajax calls Widget::display
Definition widget.php:177
input_parameter()
MUST BE overrided if the widget needs extra parameters, create a FORM to add extra-parameter.
Definition widget.php:299
__construct(protected int $user_widget_id=0, protected string $widget_code="", protected $db=null)
Definition widget.php:34
static build_user_widget($user_widget_id, $widget_code)
Build a widget thank the user_widget_id (SQL :PK : USER_WIDGET.UW_ID) and $widget_code.
Definition widget.php:137
title( $title)
display the title and the icon for zooming
Definition widget.php:365
make_form($html_input)
open a form with the DOMID "widget_code"_param, it appears once only for each widget in the dialog bo...
Definition widget.php:287
static save($array)
save widget order from an array
Definition widget.php:234
get_div_domid()
compute the DIV ID
Definition widget.php:165
static display_available()
@brier display activated widgets
Definition widget.php:214
input($flnumber=true)
display a description of the widget and allow to save it for the current user, call input_param funct...
Definition widget.php:74
button_zoom()
compute the button ZOOM to put in the title
Definition widget.php:357
static scanfolder()
scan folder to find install.php file , include them if the code is not in DB
Definition widget.php:319
static select_available()
show all the widget that can be added
Definition widget.php:267
display()
display the content for the current connected user of the widget with the parameter
display_parameter()
MUST BE overrided if the widget needs extra parameters, display the content of extra-parameter.
Definition widget.php:311
static get_enabled_widget()
returns an array of widget for the connected user, ordered
Definition widget.php:115
set_user_widget_id(int $user_widget_id)
Definition widget.php:46
$aWidget
Definition dashboard.php:28
$SecUser db
create_script($p_string)
create the HTML for adding the script tags around of the script
print
Type of printing.