noalyss Version-10
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 //!< $var_name string name of the variable in javascript
34 protected $var_name;
35
36
37
38 public function __construct(protected int $user_widget_id=0,protected string $widget_code="",protected $db=null)
39 {
40 if ($db == null) {
41 $this->db=\Dossier::connect();
42 }
43 $this->var_name='';
44 }
45
46 /**
47 * @return mixed
48 */
49 public function get_var_name()
50 {
51 return $this->var_name;
52 }
53
54 /**
55 * @param mixed $var_name
56 */
57 public function set_var_name($var_name):Widget
58 {
59 $this->var_name = $var_name;
60 return $this;
61 }
62
63 public function get_user_widget_id(): int
64 {
65 return $this->user_widget_id;
66 }
67
68 public function set_user_widget_id(int $user_widget_id): Widget
69 {
70 $this->user_widget_id = $user_widget_id;
71 return $this;
72 }
73
74 public function get_widget_code(): string
75 {
76 return $this->widget_code;
77 }
78
79 public function set_widget_code(string $widget_code): Widget
80 {
81 $this->widget_code = $widget_code;
82 return $this;
83 }
84
85 /**
86 * @brief display the content for the current connected user of the widget with the parameter
87 * @return mixed
88 */
89 abstract function display();
90
91 /**
92 * @brief display a description of the widget and allow to save it for the current user, call input_param function
93 * of the widget if it exists
94 * @return mixed
95 */
96 function input($flnumber=true)
97 {
98 static $nb=0;
99 $nb++;
100 //read description from database
101 $row=$this->db->get_row("
102 select
103 wd_code
104 ,wd_name
105 ,wd_parameter,
106 wd_description
107 from
108 widget_dashboard
109 where
110 wd_code=$1",
111 [$this->widget_code]);
112 $strNumber="";
113 if ( $flnumber) $strNumber="[ $nb ]";
114 echo "<li id=\"elt_{$this->user_widget_id}\"> $strNumber <span class='widget-name'>{$row['wd_name']}</span>{$row['wd_description']}";
115
116 if ( $this->user_widget_id > 0) {
117 if ( $row['wd_parameter'] == 1) {
118 $this->display_parameter();
119 }
120 echo '<span style="float:right;color:red">'.\Icon_Action::trash(uniqid(),sprintf("widget.delete('%s')",$this->user_widget_id));
121 }
122 else {
123 if ( $row['wd_parameter'] == 1) {
124 $this->input_parameter();
125 }
126 echo '<span style="float:right;">'.\Icon_Action::icon_add(uniqid(),sprintf("widget.add('%s')",$this->widget_code));
127 }
128
129 echo '</span>';
130 echo '</li>';
131 }
132
133 /**
134 * @brief returns an array of widget for the connected user, ordered
135 * @return array [ uw_id,dashboard_widget_id,wd_code,wd_description
136 */
137 static function get_enabled_widget():array
138 {
139 global $g_user,$cn;
140 return $cn->get_array("
141 select
142 uw.uw_id,
143 uw.dashboard_widget_id ,
144 wd.wd_code,
145 wd.wd_description
146 from user_widget uw
147join widget_dashboard wd on (uw.dashboard_widget_id=wd.wd_id)
148where use_login=$1 order by uw.uw_order
149",[$g_user->login]);
150
151 }
152
153 /**
154 * @brief Build a widget thank the user_widget_id (SQL :PK : USER_WIDGET.UW_ID) and $widget_code
155 * @param $user_widget_id integer (SQL :PK : USER_WIDGET.UW_ID)
156 * @param $widget_code string (SQL WIDGET_DASHBOARD.WD_CODE)
157 * @return Widget
158 */
159 static function build_user_widget($user_widget_id,$widget_code):?Widget
160 {
161 // load the class if file (code/code.php) exists.
162 if (file_exists(NOALYSS_INCLUDE."/widget/$widget_code/$widget_code.php")) {
163 require_once NOALYSS_INCLUDE."/widget/$widget_code/$widget_code.php";
164 $class=sprintf("\\Noalyss\\Widget\\%s",$widget_code);
165 $obj= new $class;
166 $obj->set_widget_code($widget_code);
167 $obj->set_user_widget_id($user_widget_id);
168 return $obj;
169 }
170
171 // return the object
172 return null;
173 }
174
175 /**
176 * @brief output the DIV HTML with class and id for the widget
177 * @return void
178 */
179 function open_div() {
180 printf( '<div id="%s" class="box widget-box">',$this->get_div_domid());
181 }
182
183 /**
184 * @brief compute the DIV ID
185 * @return string
186 */
187 function get_div_domid() :string {
188 return sprintf( "%s_%s",$this->widget_code,$this->user_widget_id);
189 }
190 function close_div() {
191 echo '</div>';
192 }
193
194 /**
195 * @brief display a box and fills it with the content of an ajax calls , the ajax calls Widget::display
196 * @param Widget $widget
197 * @return void
198 */
199 static function ajax_display(Widget $widget ){
200 $box= sprintf( '%s_%s',$widget->get_widget_code(),$widget->get_user_widget_id());
201 $widget->open_div();
202 echo h2(_("Un instant, on charge :-)"),' class="title" ');
203
204
205 print '<div style="display:flex;justify-content: center">';
206 print '<div style="margin-top: 50px;margin-left: 20px;">';
207 print '<div class="loading_msg"></div>';
208 print '<div class="loading_msg"></div>';
209 print '<div class="loading_msg"></div>';
210 print '<div class="loading_msg"></div>';
211 print '<div class="loading_msg"></div>';
212 print '</div>';
213 print '</div>';
214
215 $widget->close_div();
216
217 $dossier_id=\Dossier::id();
218 $widgetjs=uniqid('widget');
219 echo <<<EOF
220<script>
221var {$widgetjs}= new Widget('{$dossier_id}')
222{$widgetjs}.display('{$box}',{$widget->get_user_widget_id()},'{$widget->get_widget_code()}','{$widgetjs}')
223</script>
224
225
226EOF;
227
228
229
230 }
231
232 /**
233 * @brier display activated widgets
234 * @return void
235 */
236 static function display_available()
237 {
238
240 echo '<ul class="list-unstyled" id="contain_widget">';
241 foreach ($aWidget as $item) {
242 $widget=Widget::build_user_widget($item['uw_id'],$item['wd_code']);
243 $widget->input();
244 }
245 echo '</ul>';
246 echo \HtmlInput::hidden("order_widget_hidden", "");
247 create_script("widget.create_sortable()");
248 }
249
250 /**
251 * @brief save widget order from an array
252 * @param $array array of USER_WIDGET.UW_ID
253 * @return void
254 * @exception DatabaseCore fails , cannot update
255 */
256 static function save($array) {
257 global $cn,$g_user;
258 if (empty($array)) {
259 $cn->exec_sql("delete from user_widget where use_login = $1",[$g_user->getLogin()]);
260 return;
261 }
262 try {
263 $cn->start();
264 $order=10;
265 $cn->exec_sql("create temporary table tmp_widget(user_widget_id integer,tw_order integer )");
266 foreach ($array as $item) {
267 $cn->exec_sql('insert into tmp_widget(user_widget_id,tw_order ) values ($1,$2)',
268 [$item,$order]);
269 $order+=20;
270 }
271 $cn->exec_sql("delete from user_widget where use_login = $1 and uw_id not in (select user_widget_id from tmp_widget)",
272 array($g_user->getLogin()));
273
274 $cn->exec_sql("update user_widget set uw_order =tw_order from tmp_widget where user_widget_id=uw_id");
275
276 $cn->commit();
277
278 } catch (\Exception $e) {
279 throw ($e);
280 }
281
282
283 }
284
285 /**
286 * @brief show all the widget that can be added
287 * @return void
288 */
289 public static function select_available()
290 {
291 global $cn;
293 $aWidget=$cn->get_array("select wd_code,wd_name, wd_description,wd_parameter from widget_dashboard order by wd_name");
294 echo '<ul id="widget_add" class="list-unstyled">';
295 foreach ($aWidget as $item) {
296 $widget=Widget::build_user_widget(-1,$item['wd_code']);
297 $widget?->input(false);
298
299 }
300 echo '</ul>';
301 }
302
303 /**
304 * @brief open a form with the DOMID "widget_code"_param, it appears once only for each widget in the dialog box
305 * for adding widget to the dashboard
306 * @param $html_input string HTML string with all the HTML INPUT that will be enclosed by the FORM
307 * @return void
308 */
309 function make_form($html_input)
310 {
311 printf ('<form id="%s_param" style="display:inline">',$this->widget_code);
312 echo $html_input;
313 printf ('</form>');
314 }
315
316 /**
317 * @brief MUST BE overrided if the widget needs extra parameters, create a FORM to add extra-parameter
318 * @return void
319 * @throws \Exception
320 */
321 function input_parameter ()
322 {
323 throw new \Exception(__FUNCTION__." not implemented");
324 }
325
326 /**
327 * @brief MUST BE overrided if the widget needs extra parameters, display the content of extra-parameter
328 * @param $user_widget_id
329 * @return void
330 * @throws \Exception
331 */
332
333 function display_parameter() {
334 throw new \Exception(__FUNCTION__." not implemented");
335 }
336
337 /**
338 * @brief scan folder to find install.php file , include them if the code is not in DB
339 * @return void
340 */
341 static function scanfolder()
342 {
343 global $cn;
344 $handle=opendir(NOALYSS_INCLUDE."/widget");
345 while (($dir = readdir($handle)) != false ) {
346 $directory=NOALYSS_INCLUDE."/widget".DIRECTORY_SEPARATOR.$dir;
347 if (is_dir($directory) && $dir != "." && $dir != "..") {
348 // code exists in DB ?
349 $cnt=$cn->get_value("select count(*) from widget_dashboard where wd_code = $1",[$dir]);
350 if ( $cnt > 0) {
351 continue;
352 } else {
353 // include the install.php file if any and install the widget
354 if (file_exists($directory . DIRECTORY_SEPARATOR . "install.php")){
355 include $directory . DIRECTORY_SEPARATOR . "install.php";
356 }
357 }
358 }
359 }
360
361 }
362
363 /**
364 * @brief get the parameter of the widget and returns an array
365 * @return array key=>value
366 */
367 function get_parameter()
368 {
369 $param = $this->db->get_value("select uw_parameter from user_widget where uw_id=$1",[$this->user_widget_id]);
370 if (empty ($param)) return [];
371 parse_str($param,$aParam);
372 return $aParam;
373 }
374
375 /**
376 * @brief compute the button ZOOM to put in the title
377 * @return \html
378 */
379 function button_zoom() {
380 $bt = \Icon_Action::zoom(uniqid(), sprintf("widget.toggle_full_size('%s')",$this->get_div_domid()));
381 return $bt;
382 }
383 /**
384 * @brief display the title and the icon for zooming + refresh if possible
385 * @param $title string title of the widget
386 */
387 function title( $title) {
388 // var $refresh string javascript to refresh the widget
389 $refresh='';
390 if ( $this->get_var_name() !="") {
392 $this->get_user_widget_id(),
393 $this->get_var_name());
394
395 }
396
397 $r='<div class="bxbutton">';
399 $r.='<span id="span_'.uniqid().'" style="float:right;margin-right:5px">'.$this->button_zoom()."</span>";
400 $r.='</div>';
401 $r.=sprintf('<h2 class="title">%s</h2>',$title);
402 echo $r;
403 }
404 /**
405 * @brief build refresh javascript
406 * @param $widget_id int id from DB
407 * @param $widget_code string code of the widget
408 * @param $var_name string name of the js variable
409 */
410 static function build_refresh_js($widget_code,$widget_id,$var_name)
411 {
412 // var $box string DOM ID of the DIV containing the widget
413 $box= sprintf( '%s_%s',$widget_code,$widget_id);
414 $refresh=sprintf("%s.display('%s','%s','%s','%s')",
415 $var_name,
416 $box,
417 $widget_id,
418 $widget_code,
420 );
421 return $refresh;
422 }
423}
h2($p_string, $p_class="", $raw="")
Definition ac_common.php:68
global $g_user
if no group available , then stop
for($i=0; $i< $nb_vatex_code; $i++)($i % 2==0) ? " odd " $cnt
$dossier_id
catch(Exception $e) $obj
catch(Exception $exc) if(! $g_user->can_write_action($ag_id)) $r
_("actif, passif,charge,...")
$class
Display the Plugin and for each profile were it is installed or not.
static refresh($p_id, $javascript)
static zoom($p_div, $p_javascript)
Display a icon for zooming.
Main class for widget.
Definition widget.php:32
set_var_name($var_name)
Definition widget.php:57
get_parameter()
get the parameter of the widget and returns an array
Definition widget.php:367
set_widget_code(string $widget_code)
Definition widget.php:79
open_div()
output the DIV HTML with class and id for the widget
Definition widget.php:179
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:199
input_parameter()
MUST BE overrided if the widget needs extra parameters, create a FORM to add extra-parameter.
Definition widget.php:321
__construct(protected int $user_widget_id=0, protected string $widget_code="", protected $db=null)
Definition widget.php:38
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:159
title( $title)
display the title and the icon for zooming + refresh if possible
Definition widget.php:387
$var_name
< $var_name string name of the variable in javascript
Definition widget.php:34
static build_refresh_js($widget_code, $widget_id, $var_name)
build refresh javascript
Definition widget.php:410
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:309
static save($array)
save widget order from an array
Definition widget.php:256
get_div_domid()
compute the DIV ID
Definition widget.php:187
static display_available()
@brier display activated widgets
Definition widget.php:236
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:96
button_zoom()
compute the button ZOOM to put in the title
Definition widget.php:379
static scanfolder()
scan folder to find install.php file , include them if the code is not in DB
Definition widget.php:341
static select_available()
show all the widget that can be added
Definition widget.php:289
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:333
static get_enabled_widget()
returns an array of widget for the connected user, ordered
Definition widget.php:137
set_user_widget_id(int $user_widget_id)
Definition widget.php:68
$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.
$directory
Definition test.php:75