dolibarr  16.0.5
modules_boxes.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2004-2013 Laurent Destailleur <eldy@users.sourceforge.net>
3  * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@inodbox.com>
4  * Copyright (C) 2014 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
5  * Copyright (C) 2015 Frederic France <frederic.france@free.fr>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <https://www.gnu.org/licenses/>.
19  * or see https://www.gnu.org/
20  */
21 
34 class ModeleBoxes // Can't be abtract as it is instantiated to build "empty" boxes
35 {
39  public $db;
40 
44  public $error = '';
45 
49  public $max = 5;
50 
54  public $enabled = 1;
55 
59  public $hidden = false;
60 
64  public $rowid;
65 
70  public $id;
71 
75  public $position;
76 
80  public $box_order;
81 
85  public $fk_user;
86 
90  public $sourcefile;
91 
95  public $class;
96 
100  public $box_id;
101 
105  public $boxcode;
106 
110  public $note;
111 
115  public $widgettype = '';
116 
117 
124  public function __construct($db, $param = '')
125  {
126  $this->db = $db;
127  }
128 
134  public function error()
135  {
136  return $this->error;
137  }
138 
139 
147  public function fetch($rowid)
148  {
149  global $conf;
150 
151  // Recupere liste des boites d'un user si ce dernier a sa propre liste
152  $sql = "SELECT b.rowid as id, b.box_id, b.position, b.box_order, b.fk_user";
153  $sql .= " FROM ".MAIN_DB_PREFIX."boxes as b";
154  $sql .= " WHERE b.entity = ".$conf->entity;
155  $sql .= " AND b.rowid = ".((int) $rowid);
156  dol_syslog(get_class($this)."::fetch rowid=".$rowid);
157 
158  $resql = $this->db->query($sql);
159  if ($resql) {
160  $obj = $this->db->fetch_object($resql);
161  if ($obj) {
162  $this->id = $obj->id;
163  $this->rowid = $obj->id; // For backward compatibility
164  $this->box_id = $obj->box_id;
165  $this->position = $obj->position;
166  $this->box_order = $obj->box_order;
167  $this->fk_user = $obj->fk_user;
168  return 1;
169  } else {
170  return -1;
171  }
172  } else {
173  return -1;
174  }
175  }
176 
185  public function showBox($head = null, $contents = null, $nooutput = 0)
186  {
187  global $langs, $user, $conf;
188 
189  if (!empty($this->hidden)) {
190  return '\n<!-- Box ".get_class($this)." hidden -->\n'; // Nothing done if hidden (for example when user has no permission)
191  }
192 
193  require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
194 
195  $MAXLENGTHBOX = 60; // Mettre 0 pour pas de limite
196 
197  $cachetime = 900; // 900 : 15mn
198  $cachedir = DOL_DATA_ROOT.'/boxes/temp';
199  $fileid = get_class($this).'id-'.$this->box_id.'-e'.$conf->entity.'-u'.$user->id.'-s'.$user->socid.'.cache';
200  $filename = '/box-'.$fileid;
201  $refresh = dol_cache_refresh($cachedir, $filename, $cachetime);
202  $out = '';
203 
204  if ($refresh) {
205  dol_syslog(get_class($this).'::showBox');
206 
207  // Define nbcol and nblines of the box to show
208  $nbcol = 0;
209  if (isset($contents[0])) {
210  $nbcol = count($contents[0]);
211  }
212  $nblines = count($contents);
213 
214  $out .= "\n<!-- Box ".get_class($this)." start -->\n";
215 
216  $out .= '<div class="box boxdraggable" id="boxto_'.$this->box_id.'">'."\n";
217 
218  if (!empty($head['text']) || !empty($head['sublink']) || !empty($head['subpicto']) || $nblines) {
219  $out .= '<table summary="boxtable'.$this->box_id.'" width="100%" class="noborder boxtable">'."\n";
220  }
221 
222  // Show box title
223  if (!empty($head['text']) || !empty($head['sublink']) || !empty($head['subpicto'])) {
224  $out .= '<tr class="liste_titre box_titre">';
225  $out .= '<th';
226  if ($nbcol > 0) {
227  $out .= ' colspan="'.$nbcol.'"';
228  }
229  $out .= '>';
230  if (!empty($conf->use_javascript_ajax)) {
231  //$out.= '<table summary="" class="nobordernopadding" width="100%"><tr><td class="tdoverflowmax150 maxwidth150onsmartphone">';
232  $out .= '<div class="tdoverflowmax400 maxwidth250onsmartphone float">';
233  }
234  if (!empty($head['text'])) {
235  $s = dol_trunc($head['text'], isset($head['limit']) ? $head['limit'] : $MAXLENGTHBOX);
236  $out .= $s;
237  }
238  if (!empty($conf->use_javascript_ajax)) {
239  $out .= '</div>';
240  }
241  //$out.= '</td>';
242 
243  if (!empty($conf->use_javascript_ajax)) {
244  $sublink = '';
245  if (!empty($head['sublink'])) {
246  $sublink .= '<a href="'.$head['sublink'].'"'.(empty($head['target']) ? '' : ' target="'.$head['target'].'"').'>';
247  }
248  if (!empty($head['subpicto'])) {
249  $sublink .= img_picto($head['subtext'], $head['subpicto'], 'class="opacitymedium marginleftonly '.(empty($head['subclass']) ? '' : $head['subclass']).'" id="idsubimg'.$this->boxcode.'"');
250  }
251  if (!empty($head['sublink'])) {
252  $sublink .= '</a>';
253  }
254 
255  //$out.= '<td class="nocellnopadd boxclose right nowraponall">';
256  $out .= '<div class="nocellnopadd boxclose floatright nowraponall">';
257  $out .= $sublink;
258  // The image must have the class 'boxhandle' beause it's value used in DOM draggable objects to define the area used to catch the full object
259  $out .= img_picto($langs->trans("MoveBox", $this->box_id), 'grip_title', 'class="opacitymedium boxhandle hideonsmartphone cursormove marginleftonly"');
260  $out .= img_picto($langs->trans("CloseBox", $this->box_id), 'close_title', 'class="opacitymedium boxclose cursorpointer marginleftonly" rel="x:y" id="imgclose'.$this->box_id.'"');
261  $label = $head['text'];
262  //if (! empty($head['graph'])) $label.=' ('.$langs->trans("Graph").')';
263  if (!empty($head['graph'])) {
264  $label .= ' <span class="opacitymedium fa fa-bar-chart"></span>';
265  }
266  $out .= '<input type="hidden" id="boxlabelentry'.$this->box_id.'" value="'.dol_escape_htmltag($label).'">';
267  //$out.= '</td></tr></table>';
268  $out .= '</div>';
269  }
270 
271  $out .= "</th>";
272  $out .= "</tr>\n";
273  }
274 
275  // Show box lines
276  if ($nblines) {
277  // Loop on each record
278  for ($i = 0, $n = $nblines; $i < $n; $i++) {
279  if (isset($contents[$i])) {
280  // TR
281  if (isset($contents[$i][0]['tr'])) {
282  $out .= '<tr '.$contents[$i][0]['tr'].'>';
283  } else {
284  $out .= '<tr class="oddeven">';
285  }
286 
287  // Loop on each TD
288  $nbcolthisline = count($contents[$i]);
289  for ($j = 0; $j < $nbcolthisline; $j++) {
290  // Define tdparam
291  $tdparam = '';
292  if (!empty($contents[$i][$j]['td'])) {
293  $tdparam .= ' '.$contents[$i][$j]['td'];
294  }
295 
296  $text = isset($contents[$i][$j]['text']) ? $contents[$i][$j]['text'] : '';
297  $textwithnotags = preg_replace('/<([^>]+)>/i', '', $text);
298  $text2 = isset($contents[$i][$j]['text2']) ? $contents[$i][$j]['text2'] : '';
299  $text2withnotags = preg_replace('/<([^>]+)>/i', '', $text2);
300 
301  $textnoformat = isset($contents[$i][$j]['textnoformat']) ? $contents[$i][$j]['textnoformat'] : '';
302  //$out.= "xxx $textwithnotags y";
303  if (empty($contents[$i][$j]['tooltip'])) {
304  $contents[$i][$j]['tooltip'] = "";
305  }
306  $tooltip = isset($contents[$i][$j]['tooltip']) ? $contents[$i][$j]['tooltip'] : '';
307 
308  $out .= '<td'.$tdparam.'>'."\n";
309 
310  // Url
311  if (!empty($contents[$i][$j]['url']) && empty($contents[$i][$j]['logo'])) {
312  $out .= '<a href="'.$contents[$i][$j]['url'].'"';
313  if (!empty($tooltip)) {
314  $out .= ' title="'.dol_escape_htmltag($langs->trans("Show").' '.$tooltip, 1).'" class="classfortooltip"';
315  }
316  //$out.= ' alt="'.$textwithnotags.'"'; // Pas de alt sur un "<a href>"
317  $out .= isset($contents[$i][$j]['target']) ? ' target="'.$contents[$i][$j]['target'].'"' : '';
318  $out .= '>';
319  }
320 
321  // Logo
322  if (!empty($contents[$i][$j]['logo'])) {
323  $logo = preg_replace("/^object_/i", "", $contents[$i][$j]['logo']);
324  $out .= '<a href="'.$contents[$i][$j]['url'].'">';
325  $out .= img_object($langs->trans("Show").' '.$tooltip, $logo, 'class="classfortooltip"');
326  }
327 
328  $maxlength = $MAXLENGTHBOX;
329  if (!empty($contents[$i][$j]['maxlength'])) {
330  $maxlength = $contents[$i][$j]['maxlength'];
331  }
332 
333  if ($maxlength) {
334  $textwithnotags = dol_trunc($textwithnotags, $maxlength);
335  }
336  if (preg_match('/^<(img|div|span)/i', $text) || !empty($contents[$i][$j]['asis'])) {
337  $out .= $text; // show text with no html cleaning
338  } else {
339  $out .= $textwithnotags; // show text with html cleaning
340  }
341 
342  // End Url
343  if (!empty($contents[$i][$j]['url'])) {
344  $out .= '</a>';
345  }
346 
347  if (preg_match('/^<(img|div|span)/i', $text2) || !empty($contents[$i][$j]['asis2'])) {
348  $out .= $text2; // show text with no html cleaning
349  } else {
350  $out .= $text2withnotags; // show text with html cleaning
351  }
352 
353  if (!empty($textnoformat)) {
354  $out .= "\n".$textnoformat."\n";
355  }
356 
357  $out .= "</td>\n";
358  }
359 
360  $out .= "</tr>\n";
361  }
362  }
363  }
364 
365  if (!empty($head['text']) || !empty($head['sublink']) || !empty($head['subpicto']) || $nblines) {
366  $out .= "</table>\n";
367  }
368 
369  // If invisible box with no contents
370  if (empty($head['text']) && empty($head['sublink']) && empty($head['subpicto']) && !$nblines) {
371  $out .= "<br>\n";
372  }
373 
374  $out .= "</div>\n";
375 
376  $out .= "<!-- Box ".get_class($this)." end -->\n\n";
377  if (!empty($conf->global->MAIN_ACTIVATE_FILECACHE)) {
378  dol_filecache($cachedir, $filename, $out);
379  }
380  } else {
381  dol_syslog(get_class($this).'::showBoxCached');
382  $out = "<!-- Box ".get_class($this)." from cache -->";
383  $out .= dol_readcachefile($cachedir, $filename);
384  }
385 
386  if ($nooutput) {
387  return $out;
388  } else {
389  print $out;
390  }
391 
392  return '';
393  }
394 
395 
403  public static function getWidgetsList($forcedirwidget = null)
404  {
405  global $conf, $langs, $db;
406 
407  $files = array();
408  $fullpath = array();
409  $relpath = array();
410  $iscoreorexternal = array();
411  $modules = array();
412  $orders = array();
413  $i = 0;
414 
415  //$dirwidget=array_merge(array('/core/boxes/'), $conf->modules_parts['widgets']);
416  $dirwidget = array('/core/boxes/'); // $conf->modules_parts['widgets'] is not required
417  if (is_array($forcedirwidget)) {
418  $dirwidget = $forcedirwidget;
419  }
420 
421  foreach ($dirwidget as $reldir) {
422  $dir = dol_buildpath($reldir, 0);
423  $newdir = dol_osencode($dir);
424 
425  // Check if directory exists (we do not use dol_is_dir to avoid loading files.lib.php at each call)
426  if (!is_dir($newdir)) {
427  continue;
428  }
429 
430  $handle = opendir($newdir);
431  if (is_resource($handle)) {
432  while (($file = readdir($handle)) !== false) {
433  $reg = array();
434  if (is_readable($newdir.'/'.$file) && preg_match('/^(.+)\.php/', $file, $reg)) {
435  if (preg_match('/\.back$/', $file)) {
436  continue;
437  }
438 
439  $part1 = $reg[1];
440 
441  $modName = ucfirst($reg[1]);
442  //print "file=$file"; print "modName=$modName"; exit;
443  if (in_array($modName, $modules)) {
444  $langs->load("errors");
445  print '<div class="error">'.$langs->trans("Error").' : '.$langs->trans("ErrorDuplicateWidget", $modName, "").'</div>';
446  } else {
447  try {
448  include_once $newdir.'/'.$file;
449  } catch (Exception $e) {
450  print $e->getMessage();
451  }
452  }
453 
454  $files[$i] = $file;
455  $fullpath[$i] = $dir.'/'.$file;
456  $relpath[$i] = preg_replace('/^\//', '', $reldir).'/'.$file;
457  $iscoreorexternal[$i] = ($reldir == '/core/boxes/' ? 'internal' : 'external');
458  $modules[$i] = $modName;
459  $orders[$i] = $part1; // Set sort criteria value
460 
461  $i++;
462  }
463  }
464  closedir($handle);
465  }
466  }
467 
468  asort($orders);
469 
470  $widget = array();
471  $j = 0;
472 
473  // Loop on each widget
474  foreach ($orders as $key => $value) {
475  $modName = $modules[$key];
476  if (empty($modName)) {
477  continue;
478  }
479 
480  if (!class_exists($modName)) {
481  print 'Error: A widget file was found but its class "'.$modName.'" was not found.'."<br>\n";
482  continue;
483  }
484 
485  $objMod = new $modName($db);
486  if (is_object($objMod)) {
487  // Define disabledbyname and disabledbymodule
488  $disabledbyname = 0;
489  $disabledbymodule = 0; // TODO Set to 2 if module is not enabled
490  $module = '';
491 
492  // Check if widget file is disabled by name
493  if (preg_match('/NORUN$/i', $files[$key])) {
494  $disabledbyname = 1;
495  }
496 
497  // We set info of modules
498  $widget[$j]['picto'] = $objMod->picto ? img_object('', $objMod->picto) : img_object('', 'generic');
499  $widget[$j]['file'] = $files[$key];
500  $widget[$j]['fullpath'] = $fullpath[$key];
501  $widget[$j]['relpath'] = $relpath[$key];
502  $widget[$j]['iscoreorexternal'] = $iscoreorexternal[$key];
503  $widget[$j]['version'] = empty($objMod->version) ? '' : $objMod->version;
504  $widget[$j]['status'] = img_picto($langs->trans("Active"), 'tick');
505  if ($disabledbyname > 0 || $disabledbymodule > 1) {
506  $widget[$j]['status'] = '';
507  }
508 
509  $text = '<b>'.$langs->trans("Description").':</b><br>';
510  $text .= $objMod->boxlabel.'<br>';
511  $text .= '<br><b>'.$langs->trans("Status").':</b><br>';
512  if ($disabledbymodule == 2) {
513  $text .= $langs->trans("WidgetDisabledAsModuleDisabled", $module).'<br>';
514  }
515 
516  $widget[$j]['info'] = $text;
517  }
518  $j++;
519  }
520  return $widget;
521  }
522 }
db
$conf db
API class for accounts.
Definition: inc.php:41
dol_escape_htmltag
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0)
Returns text escaped for inclusion in HTML alt or title tags, or into values of HTML input fields.
Definition: functions.lib.php:1468
dol_trunc
dol_trunc($string, $size=40, $trunc='right', $stringencoding='UTF-8', $nodot=0, $display=0)
Truncate a string to a particular length adding '…' if string larger than length.
Definition: functions.lib.php:3805
dol_osencode
dol_osencode($str)
Return a string encoded into OS filesystem encoding.
Definition: functions.lib.php:8498
ModeleBoxes\error
error()
Return last error message.
Definition: modules_boxes.php:134
ModeleBoxes\getWidgetsList
static getWidgetsList($forcedirwidget=null)
Return list of widget.
Definition: modules_boxes.php:403
dol_buildpath
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
Definition: functions.lib.php:1062
rowid
print *****$script_file(".$version.") pid c cd cd cd description as p label as s rowid
Definition: email_expire_services_to_representatives.php:79
dol_readcachefile
dol_readcachefile($directory, $filename)
Read object from cachefile.
Definition: files.lib.php:3182
img_picto
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
Definition: functions.lib.php:3880
Exception
dol_filecache
dol_filecache($directory, $filename, $object)
Store object in file.
Definition: files.lib.php:3149
ModeleBoxes\showBox
showBox($head=null, $contents=null, $nooutput=0)
Standard method to show a box (usage by boxes not mandatory, a box can still use its own showBox func...
Definition: modules_boxes.php:185
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1603
ModeleBoxes\fetch
fetch($rowid)
Load a box line from its rowid.
Definition: modules_boxes.php:147
dol_cache_refresh
dol_cache_refresh($directory, $filename, $cachetime)
Test if Refresh needed.
Definition: files.lib.php:3167
ModeleBoxes\__construct
__construct($db, $param='')
Constructor.
Definition: modules_boxes.php:124
img_object
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
Definition: functions.lib.php:4211
$resql
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->rights->fournisseur->facture->lire)||(isModEnabled('supplier_invoice') && $user->rights->supplier_invoice->lire)) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:742
ModeleBoxes
Class ModeleBoxes.
Definition: modules_boxes.php:34