dolibarr  9.0.0
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 <http://www.gnu.org/licenses/>.
19  * or see http://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=0;
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 
118  function __construct($db,$param='')
119  {
120  $this->db=$db;
121  }
122 
128  function error()
129  {
130  return $this->error;
131  }
132 
133 
141  function fetch($rowid)
142  {
143  global $conf;
144 
145  // Recupere liste des boites d'un user si ce dernier a sa propre liste
146  $sql = "SELECT b.rowid, b.box_id, b.position, b.box_order, b.fk_user";
147  $sql.= " FROM ".MAIN_DB_PREFIX."boxes as b";
148  $sql.= " WHERE b.entity = ".$conf->entity;
149  $sql.= " AND b.rowid = ".$rowid;
150  dol_syslog(get_class($this)."::fetch rowid=".$rowid);
151 
152  $resql = $this->db->query($sql);
153  if ($resql)
154  {
155  $obj = $this->db->fetch_object($resql);
156  if ($obj)
157  {
158  $this->rowid=$obj->rowid;
159  $this->box_id=$obj->box_id;
160  $this->position=$obj->position;
161  $this->box_order=$obj->box_order;
162  $this->fk_user=$obj->fk_user;
163  return 1;
164  }
165  else
166  {
167  return -1;
168  }
169  }
170  else
171  {
172  return -1;
173  }
174  }
175 
176 
185  function outputBox($head = null, $contents = null)
186  {
187  global $langs, $user, $conf;
188 
189  // Trick to get result into a var from a function that makes print instead of return
190  // TODO Replace ob_start with param nooutput=1 into showBox
191  ob_start();
192  $result = $this->showBox($head, $contents);
193  $output = ob_get_contents();
194  ob_end_clean();
195 
196  return $output;
197  }
198 
207  function showBox($head = null, $contents = null, $nooutput=0)
208  {
209  global $langs, $user, $conf;
210 
211  if (! empty($this->hidden)) return '\n<!-- Box ".get_class($this)." hidden -->\n'; // Nothing done if hidden (for example when user has no permission)
212 
213  require_once DOL_DOCUMENT_ROOT .'/core/lib/files.lib.php';
214 
215  $MAXLENGTHBOX=60; // Mettre 0 pour pas de limite
216 
217  $cachetime = 900; // 900 : 15mn
218  $cachedir = DOL_DATA_ROOT.'/boxes/temp';
219  $fileid = get_class($this).'id-'.$this->box_id.'-e'.$conf->entity.'-u'.$user->id.'-s'.$user->societe_id.'.cache';
220  $filename = '/box-'.$fileid;
221  $refresh = dol_cache_refresh($cachedir, $filename, $cachetime);
222  $out = '';
223 
224  if ($refresh) {
225  dol_syslog(get_class($this).'::showBox');
226 
227  // Define nbcol and nblines of the box to show
228  $nbcol=0;
229  if (isset($contents[0])) $nbcol=count($contents[0]);
230  $nblines=count($contents);
231 
232  $out.= "\n<!-- Box ".get_class($this)." start -->\n";
233 
234  $out.= '<div class="box boxdraggable" id="boxto_'.$this->box_id.'">'."\n";
235 
236  if (! empty($head['text']) || ! empty($head['sublink']) || ! empty($head['subpicto']) || $nblines)
237  {
238  $out.= '<table summary="boxtable'.$this->box_id.'" width="100%" class="noborder boxtable">'."\n";
239  }
240 
241  // Show box title
242  if (! empty($head['text']) || ! empty($head['sublink']) || ! empty($head['subpicto']))
243  {
244  $out.= '<tr class="liste_titre box_titre">';
245  $out.= '<td';
246  if ($nbcol > 0) { $out.= ' colspan="'.$nbcol.'"'; }
247  $out.= '>';
248  if ($conf->use_javascript_ajax)
249  {
250  $out.= '<table summary="" class="nobordernopadding" width="100%"><tr><td class="tdoverflowmax150 maxwidth150onsmartphone">';
251  }
252  if (! empty($head['text']))
253  {
254  $s=dol_trunc($head['text'],isset($head['limit'])?$head['limit']:$MAXLENGTHBOX);
255  $out.= $s;
256  }
257  $out.= '</td>';
258 
259  if (! empty($conf->use_javascript_ajax))
260  {
261  $sublink='';
262  if (! empty($head['sublink'])) $sublink.= '<a href="'.$head['sublink'].'"'.(empty($head['target'])?'':' target="'.$head['target'].'"').'>';
263  if (! empty($head['subpicto'])) $sublink.= img_picto($head['subtext'], $head['subpicto'], 'class="opacitymedium '.(empty($head['subclass'])?'':$head['subclass']).'" id="idsubimg'.$this->boxcode.'"');
264  if (! empty($head['sublink'])) $sublink.= '</a>';
265 
266  $out.= '<td class="nocellnopadd boxclose right nowraponall">';
267  $out.=$sublink;
268  // 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
269  $out.= img_picto($langs->trans("MoveBox",$this->box_id),'grip_title','class="opacitymedium boxhandle hideonsmartphone cursormove"');
270  $out.= img_picto($langs->trans("CloseBox",$this->box_id),'close_title','class="opacitymedium boxclose cursorpointer" rel="x:y" id="imgclose'.$this->box_id.'"');
271  $label=$head['text'];
272  //if (! empty($head['graph'])) $label.=' ('.$langs->trans("Graph").')';
273  if (! empty($head['graph'])) $label.=' <span class="opacitymedium fa fa-bar-chart"></span>';
274  $out.= '<input type="hidden" id="boxlabelentry'.$this->box_id.'" value="'.dol_escape_htmltag($label).'">';
275  $out.= '</td></tr></table>';
276  }
277 
278  $out.= "</tr>\n";
279  }
280 
281  // Show box lines
282  if ($nblines)
283  {
284  // Loop on each record
285  for ($i=0, $n=$nblines; $i < $n; $i++)
286  {
287  if (isset($contents[$i]))
288  {
289  // TR
290  if (isset($contents[$i][0]['tr'])) $out.= '<tr '.$contents[$i][0]['tr'].'>';
291  else $out.= '<tr class="oddeven">';
292 
293  // Loop on each TD
294  $nbcolthisline=count($contents[$i]);
295  for ($j=0; $j < $nbcolthisline; $j++) {
296  // Define tdparam
297  $tdparam='';
298  if (isset($contents[$i][$j]['td'])) $tdparam.=' '.$contents[$i][$j]['td'];
299 
300  $text=isset($contents[$i][$j]['text'])?$contents[$i][$j]['text']:'';
301  $textwithnotags=preg_replace('/<([^>]+)>/i','',$text);
302  $text2=isset($contents[$i][$j]['text2'])?$contents[$i][$j]['text2']:'';
303  $text2withnotags=preg_replace('/<([^>]+)>/i','',$text2);
304  $textnoformat=isset($contents[$i][$j]['textnoformat'])?$contents[$i][$j]['textnoformat']:'';
305  //$out.= "xxx $textwithnotags y";
306  if (empty($contents[$i][$j]['tooltip'])) $contents[$i][$j]['tooltip']="";
307  $tooltip=isset($contents[$i][$j]['tooltip'])?$contents[$i][$j]['tooltip']:'';
308 
309  $out.= '<td'.$tdparam.'>'."\n";
310 
311  // Url
312  if (! empty($contents[$i][$j]['url']) && empty($contents[$i][$j]['logo']))
313  {
314  $out.= '<a href="'.$contents[$i][$j]['url'].'"';
315  if (!empty($tooltip)) {
316  $out .= ' title="'.dol_escape_htmltag($langs->trans("Show").' '.$tooltip, 1).'" class="classfortooltip"';
317  }
318  //$out.= ' alt="'.$textwithnotags.'"'; // Pas de alt sur un "<a href>"
319  $out.= isset($contents[$i][$j]['target'])?' target="'.$contents[$i][$j]['target'].'"':'';
320  $out.= '>';
321  }
322 
323  // Logo
324  if (! empty($contents[$i][$j]['logo']))
325  {
326  $logo=preg_replace("/^object_/i","",$contents[$i][$j]['logo']);
327  $out.= '<a href="'.$contents[$i][$j]['url'].'">';
328  $out.= img_object($langs->trans("Show").' '.$tooltip, $logo, 'class="classfortooltip"');
329  }
330 
331  $maxlength=$MAXLENGTHBOX;
332  if (! empty($contents[$i][$j]['maxlength'])) $maxlength=$contents[$i][$j]['maxlength'];
333 
334  if ($maxlength) $textwithnotags=dol_trunc($textwithnotags,$maxlength);
335  if (preg_match('/^<img/i',$text) || preg_match('/^<div/i',$text) || ! empty($contents[$i][$j]['asis'])) $out.= $text; // show text with no html cleaning
336  else $out.= $textwithnotags; // show text with html cleaning
337 
338  // End Url
339  if (! empty($contents[$i][$j]['url'])) $out.= '</a>';
340 
341  if (preg_match('/^<img/i',$text2) || preg_match('/^<div/i',$text2) || ! empty($contents[$i][$j]['asis2'])) $out.= $text2; // show text with no html cleaning
342  else $out.= $text2withnotags; // show text with html cleaning
343 
344  if (! empty($textnoformat)) $out.= "\n".$textnoformat."\n";
345 
346  $out.= "</td>\n";
347  }
348 
349  $out.= "</tr>\n";
350  }
351  }
352  }
353 
354  if (! empty($head['text']) || ! empty($head['sublink']) || ! empty($head['subpicto']) || $nblines)
355  {
356  $out.= "</table>\n";
357  }
358 
359  // If invisible box with no contents
360  if (empty($head['text']) && empty($head['sublink']) && empty($head['subpicto']) && ! $nblines) $out.= "<br>\n";
361 
362  $out.= "</div>\n";
363 
364  $out.= "<!-- Box ".get_class($this)." end -->\n\n";
365  if (! empty($conf->global->MAIN_ACTIVATE_FILECACHE)) {
366  dol_filecache($cachedir, $filename, $out);
367  }
368  } else {
369  dol_syslog(get_class($this).'::showBoxCached');
370  $out = "<!-- Box ".get_class($this)." from cache -->";
371  $out.= dol_readcachefile($cachedir, $filename);
372  }
373 
374  if ($nooutput) return $out;
375  else print $out;
376 
377  return '';
378  }
379 
380 
388  static function getWidgetsList($forcedirwidget=null)
389  {
390  global $conf, $langs, $db;
391 
392  $files = array();
393  $fullpath = array();
394  $relpath = array();
395  $iscoreorexternal = array();
396  $modules = array();
397  $orders = array();
398  $i = 0;
399 
400  $dirwidget=array_merge(array('/core/boxes/'));
401  if (is_array($forcedirwidget))
402  {
403  $dirwidget=$forcedirwidget;
404  }
405 
406  foreach($dirwidget as $reldir)
407  {
408  $dir=dol_buildpath($reldir,0);
409  $newdir=dol_osencode($dir);
410 
411  // Check if directory exists (we do not use dol_is_dir to avoid loading files.lib.php at each call)
412  if (! is_dir($newdir)) continue;
413 
414  $handle=opendir($newdir);
415  if (is_resource($handle))
416  {
417  while (($file = readdir($handle))!==false)
418  {
419  if (is_readable($newdir.'/'.$file) && preg_match('/^(.+)\.php/',$file,$reg))
420  {
421  if (preg_match('/\.back$/',$file)) continue;
422 
423  $part1=$reg[1];
424 
425  $modName = ucfirst($reg[1]);
426  //print "file=$file"; print "modName=$modName"; exit;
427  if (in_array($modName,$modules))
428  {
429  $langs->load("errors");
430  print '<div class="error">'.$langs->trans("Error").' : '.$langs->trans("ErrorDuplicateWidget",$modName,"").'</div>';
431  }
432  else
433  {
434  try {
435  include_once $newdir.'/'.$file;
436  }
437  catch(Exception $e)
438  {
439  print $e->getMessage();
440  }
441  }
442 
443  $files[$i] = $file;
444  $fullpath[$i] = $dir.'/'.$file;
445  $relpath[$i] = preg_replace('/^\//','',$reldir).'/'.$file;
446  $iscoreorexternal[$i] = ($reldir == '/core/boxes/'?'internal':'external');
447  $modules[$i] = $modName;
448  $orders[$i] = $part1; // Set sort criteria value
449 
450  $i++;
451  }
452  }
453  closedir($handle);
454  }
455  }
456 
457  asort($orders);
458 
459  $widget = array();
460  $j = 0;
461 
462  // Loop on each widget
463  foreach ($orders as $key => $value)
464  {
465  $modName = $modules[$key];
466  if (empty($modName)) continue;
467 
468  if (! class_exists($modName))
469  {
470  print 'Error: A widget file was found but its class "'.$modName.'" was not found.'."<br>\n";
471  continue;
472  }
473 
474  $objMod = new $modName($db);
475  if (is_object($objMod))
476  {
477  // Define disabledbyname and disabledbymodule
478  $disabledbyname=0;
479  $module='';
480 
481  // Check if widget file is disabled by name
482  if (preg_match('/NORUN$/i',$files[$key])) $disabledbyname=1;
483 
484  // We set info of modules
485  $widget[$j]['picto'] = $objMod->picto?img_object('',$objMod->picto):img_object('','generic');
486  $widget[$j]['file'] = $files[$key];
487  $widget[$j]['fullpath'] = $fullpath[$key];
488  $widget[$j]['relpath'] = $relpath[$key];
489  $widget[$j]['iscoreorexternal'] = $iscoreorexternal[$key];
490  //$widget[$j]['version'] = $objMod->getVersion();
491  $widget[$j]['status'] = img_picto($langs->trans("Active"),'tick');
492  if ($disabledbyname > 0 || $disabledbymodule > 1) $widget[$j]['status'] = '';
493 
494  $text ='<b>'.$langs->trans("Description").':</b><br>';
495  $text.=$objMod->boxlabel.'<br>';
496  $text.='<br><b>'.$langs->trans("Status").':</b><br>';
497  if ($disabledbymodule == 2) $text.=$langs->trans("HooksDisabledAsModuleDisabled",$module).'<br>';
498 
499  $widget[$j]['info'] = $text;
500  }
501  $j++;
502  }
503  return $widget;
504  }
505 }
506 
507 
dol_osencode($str)
Return a string encoded into OS filesystem encoding.
dol_cache_refresh($directory, $filename, $cachetime)
Test if Refresh needed.
Definition: files.lib.php:2888
print
Draft customers invoices.
Definition: index.php:91
dol_trunc($string, $size=40, $trunc='right', $stringencoding='UTF-8', $nodot=0, $display=0)
Truncate a string to a particular length adding &#39;...&#39; if string larger than length.
if(! empty($conf->facture->enabled) && $user->rights->facture->lire) if(! empty($conf->fournisseur->enabled) && $user->rights->fournisseur->facture->lire) if(! empty($conf->don->enabled) && $user->rights->societe->lire) if(! empty($conf->tax->enabled) && $user->rights->tax->charges->lire) if(! empty($conf->facture->enabled) &&! empty($conf->commande->enabled) && $user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(! empty($conf->facture->enabled) && $user->rights->facture->lire) if(! empty($conf->fournisseur->enabled) && $user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1053
fetch($rowid)
Load a box line from its rowid.
static getWidgetsList($forcedirwidget=null)
Return list of widget.
__construct($db, $param='')
Constructor.
Class ModeleBoxes.
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0)
Returns text escaped for inclusion in HTML alt or title tags, or into values of HTML input fields...
outputBox($head=null, $contents=null)
Standard method to get content of a box.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
error()
Return last error message.
dol_readcachefile($directory, $filename)
Read object from cachefile.
Definition: files.lib.php:2903
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...
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='')
Show picto whatever it&#39;s its name (generic function)
dol_filecache($directory, $filename, $object)
Store object in file.
Definition: files.lib.php:2872
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)