dolibarr  7.0.0-beta
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@capnetworks.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  $var = false;
217 
218  $cachetime = 900; // 900 : 15mn
219  $cachedir = DOL_DATA_ROOT.'/boxes/temp';
220  $fileid = get_class($this).'id-'.$this->box_id.'-e'.$conf->entity.'-u'.$user->id.'-s'.$user->societe_id.'.cache';
221  $filename = '/box-'.$fileid;
222  $refresh = dol_cache_refresh($cachedir, $filename, $cachetime);
223  $out = '';
224 
225  if ($refresh) {
226  dol_syslog(get_class($this).'::showBox');
227 
228  // Define nbcol and nblines of the box to show
229  $nbcol=0;
230  if (isset($contents[0])) $nbcol=count($contents[0]);
231  $nblines=count($contents);
232 
233  $out.= "\n<!-- Box ".get_class($this)." start -->\n";
234 
235  $out.= '<div class="box boxdraggable" id="boxto_'.$this->box_id.'">'."\n";
236 
237  if (! empty($head['text']) || ! empty($head['sublink']) || ! empty($head['subpicto']) || $nblines)
238  {
239  $out.= '<table summary="boxtable'.$this->box_id.'" width="100%" class="noborder boxtable">'."\n";
240  }
241 
242  // Show box title
243  if (! empty($head['text']) || ! empty($head['sublink']) || ! empty($head['subpicto']))
244  {
245  $out.= '<tr class="liste_titre box_titre">';
246  $out.= '<td';
247  if ($nbcol > 0) { $out.= ' colspan="'.$nbcol.'"'; }
248  $out.= '>';
249  if ($conf->use_javascript_ajax)
250  {
251  $out.= '<table summary="" class="nobordernopadding" width="100%"><tr><td class="tdoverflowmax100 maxwidth100onsmartphone">';
252  }
253  if (! empty($head['text']))
254  {
255  $s=dol_trunc($head['text'],isset($head['limit'])?$head['limit']:$MAXLENGTHBOX);
256  $out.= $s;
257  }
258  $out.= '</td>';
259 
260  if (! empty($conf->use_javascript_ajax))
261  {
262  $sublink='';
263  if (! empty($head['sublink'])) $sublink.= '<a href="'.$head['sublink'].'"'.(empty($head['target'])?'':' target="'.$head['target'].'"').'>';
264  if (! empty($head['subpicto'])) $sublink.= img_picto($head['subtext'], $head['subpicto'], 'class="'.(empty($head['subclass'])?'':$head['subclass']).'" id="idsubimg'.$this->boxcode.'"');
265  if (! empty($head['sublink'])) $sublink.= '</a>';
266 
267  $out.= '<td class="nocellnopadd boxclose right nowraponall">';
268  $out.=$sublink;
269  // 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
270  $out.= img_picto($langs->trans("MoveBox",$this->box_id),'grip_title','class="boxhandle hideonsmartphone cursormove"');
271  $out.= img_picto($langs->trans("CloseBox",$this->box_id),'close_title','class="boxclose cursorpointer" rel="x:y" id="imgclose'.$this->box_id.'"');
272  $label=$head['text'];
273  //if (! empty($head['graph'])) $label.=' ('.$langs->trans("Graph").')';
274  if (! empty($head['graph'])) $label.=' <span class="fa fa-bar-chart"></span>';
275  $out.= '<input type="hidden" id="boxlabelentry'.$this->box_id.'" value="'.dol_escape_htmltag($label).'">';
276  $out.= '</td></tr></table>';
277  }
278 
279  $out.= "</tr>\n";
280  }
281 
282  // Show box lines
283  if ($nblines)
284  {
285  // Loop on each record
286  for ($i=0, $n=$nblines; $i < $n; $i++)
287  {
288  if (isset($contents[$i]))
289  {
290  // TR
291  if (isset($contents[$i][0]['tr'])) $out.= '<tr '.$contents[$i][0]['tr'].'>';
292  else $out.= '<tr class="oddeven">';
293 
294  // Loop on each TD
295  $nbcolthisline=count($contents[$i]);
296  for ($j=0; $j < $nbcolthisline; $j++) {
297  // Define tdparam
298  $tdparam='';
299  if (isset($contents[$i][$j]['td'])) $tdparam.=' '.$contents[$i][$j]['td'];
300 
301  $text=isset($contents[$i][$j]['text'])?$contents[$i][$j]['text']:'';
302  $textwithnotags=preg_replace('/<([^>]+)>/i','',$text);
303  $text2=isset($contents[$i][$j]['text2'])?$contents[$i][$j]['text2']:'';
304  $text2withnotags=preg_replace('/<([^>]+)>/i','',$text2);
305  $textnoformat=isset($contents[$i][$j]['textnoformat'])?$contents[$i][$j]['textnoformat']:'';
306  //$out.= "xxx $textwithnotags y";
307  if (empty($contents[$i][$j]['tooltip'])) $contents[$i][$j]['tooltip']="";
308  $tooltip=isset($contents[$i][$j]['tooltip'])?$contents[$i][$j]['tooltip']:'';
309 
310  $out.= '<td'.$tdparam.'>'."\n";
311 
312  // Url
313  if (! empty($contents[$i][$j]['url']) && empty($contents[$i][$j]['logo']))
314  {
315  $out.= '<a href="'.$contents[$i][$j]['url'].'"';
316  if (!empty($tooltip)) {
317  $out .= ' title="'.dol_escape_htmltag($langs->trans("Show").' '.$tooltip, 1).'" class="classfortooltip"';
318  }
319  //$out.= ' alt="'.$textwithnotags.'"'; // Pas de alt sur un "<a href>"
320  $out.= isset($contents[$i][$j]['target'])?' target="'.$contents[$i][$j]['target'].'"':'';
321  $out.= '>';
322  }
323 
324  // Logo
325  if (! empty($contents[$i][$j]['logo']))
326  {
327  $logo=preg_replace("/^object_/i","",$contents[$i][$j]['logo']);
328  $out.= '<a href="'.$contents[$i][$j]['url'].'">';
329  $out.= img_object($langs->trans("Show").' '.$tooltip, $logo, 'class="classfortooltip"');
330  }
331 
332  $maxlength=$MAXLENGTHBOX;
333  if (! empty($contents[$i][$j]['maxlength'])) $maxlength=$contents[$i][$j]['maxlength'];
334 
335  if ($maxlength) $textwithnotags=dol_trunc($textwithnotags,$maxlength);
336  if (preg_match('/^<img/i',$text) || preg_match('/^<div/i',$text) || ! empty($contents[$i][$j]['asis'])) $out.= $text; // show text with no html cleaning
337  else $out.= $textwithnotags; // show text with html cleaning
338 
339  // End Url
340  if (! empty($contents[$i][$j]['url'])) $out.= '</a>';
341 
342  if (preg_match('/^<img/i',$text2) || preg_match('/^<div/i',$text2) || ! empty($contents[$i][$j]['asis2'])) $out.= $text2; // show text with no html cleaning
343  else $out.= $text2withnotags; // show text with html cleaning
344 
345  if (! empty($textnoformat)) $out.= "\n".$textnoformat."\n";
346 
347  $out.= "</td>\n";
348  }
349 
350  $out.= "</tr>\n";
351  }
352  }
353  }
354 
355  if (! empty($head['text']) || ! empty($head['sublink']) || ! empty($head['subpicto']) || $nblines)
356  {
357  $out.= "</table>\n";
358  }
359 
360  // If invisible box with no contents
361  if (empty($head['text']) && empty($head['sublink']) && empty($head['subpicto']) && ! $nblines) $out.= "<br>\n";
362 
363  $out.= "</div>\n";
364 
365  $out.= "<!-- Box ".get_class($this)." end -->\n\n";
366  if (! empty($conf->global->MAIN_ACTIVATE_FILECACHE)) {
367  dol_filecache($cachedir, $filename, $out);
368  }
369  } else {
370  dol_syslog(get_class($this).'::showBoxCached');
371  $out = "<!-- Box ".get_class($this)." from cache -->";
372  $out.= dol_readcachefile($cachedir, $filename);
373  }
374 
375  if ($nooutput) return $out;
376  else print $out;
377 
378  return '';
379  }
380 
381 
389  static function getWidgetsList($forcedirwidget=null)
390  {
391  global $conf, $langs, $db;
392 
393  $files = array();
394  $fullpath = array();
395  $relpath = array();
396  $iscoreorexternal = array();
397  $modules = array();
398  $orders = array();
399  $i = 0;
400 
401  $dirwidget=array_merge(array('/core/boxes/'));
402  if (is_array($forcedirwidget))
403  {
404  $dirwidget=$forcedirwidget;
405  }
406 
407  foreach($dirwidget as $reldir)
408  {
409  $dir=dol_buildpath($reldir,0);
410  $newdir=dol_osencode($dir);
411 
412  // Check if directory exists (we do not use dol_is_dir to avoid loading files.lib.php at each call)
413  if (! is_dir($newdir)) continue;
414 
415  $handle=opendir($newdir);
416  if (is_resource($handle))
417  {
418  while (($file = readdir($handle))!==false)
419  {
420  if (is_readable($newdir.'/'.$file) && preg_match('/^(.+)\.php/',$file,$reg))
421  {
422  if (preg_match('/\.back$/',$file)) continue;
423 
424  $part1=$reg[1];
425 
426  $modName = ucfirst($reg[1]);
427  //print "file=$file"; print "modName=$modName"; exit;
428  if (in_array($modName,$modules))
429  {
430  $langs->load("errors");
431  print '<div class="error">'.$langs->trans("Error").' : '.$langs->trans("ErrorDuplicateWidget",$modName,"").'</div>';
432  }
433  else
434  {
435  try {
436  include_once $newdir.'/'.$file;
437  }
438  catch(Exception $e)
439  {
440  print $e->getMessage();
441  }
442  }
443 
444  $files[$i] = $file;
445  $fullpath[$i] = $dir.'/'.$file;
446  $relpath[$i] = preg_replace('/^\//','',$reldir).'/'.$file;
447  $iscoreorexternal[$i] = ($reldir == '/core/boxes/'?'internal':'external');
448  $modules[$i] = $modName;
449  $orders[$i] = $part1; // Set sort criteria value
450 
451  $i++;
452  }
453  }
454  closedir($handle);
455  }
456  }
457 
458  asort($orders);
459 
460  $widget = array();
461  $j = 0;
462 
463  // Loop on each widget
464  foreach ($orders as $key => $value)
465  {
466  $modName = $modules[$key];
467  if (empty($modName)) continue;
468 
469  if (! class_exists($modName))
470  {
471  print 'Error: A widget file was found but its class "'.$modName.'" was not found.'."<br>\n";
472  continue;
473  }
474 
475  $objMod = new $modName($db);
476  if (is_object($objMod))
477  {
478  // Define disabledbyname and disabledbymodule
479  $disabledbyname=0;
480  $module='';
481 
482  // Check if widget file is disabled by name
483  if (preg_match('/NORUN$/i',$files[$key])) $disabledbyname=1;
484 
485  // We set info of modules
486  $widget[$j]['picto'] = $objMod->picto?img_object('',$objMod->picto):img_object('','generic');
487  $widget[$j]['file'] = $files[$key];
488  $widget[$j]['fullpath'] = $fullpath[$key];
489  $widget[$j]['relpath'] = $relpath[$key];
490  $widget[$j]['iscoreorexternal'] = $iscoreorexternal[$key];
491  //$widget[$j]['version'] = $objMod->getVersion();
492  $widget[$j]['status'] = img_picto($langs->trans("Active"),'tick');
493  if ($disabledbyname > 0 || $disabledbymodule > 1) $widget[$j]['status'] = '';
494 
495  $text ='<b>'.$langs->trans("Description").':</b><br>';
496  $text.=$objMod->boxlabel.'<br>';
497  $text.='<br><b>'.$langs->trans("Status").':</b><br>';
498  if ($disabledbymodule == 2) $text.=$langs->trans("HooksDisabledAsModuleDisabled",$module).'<br>';
499 
500  $widget[$j]['info'] = $text;
501  }
502  $j++;
503  }
504  return $widget;
505  }
506 
507 
508 }
509 
510 
dol_osencode($str)
Return a string encoded into OS filesystem encoding.
img_picto($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='')
Show picto whatever it's its name (generic function)
dol_cache_refresh($directory, $filename, $cachetime)
Test if Refresh needed.
Definition: files.lib.php:2736
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.
fetch($rowid)
Load a box line from its rowid.
static getWidgetsList($forcedirwidget=null)
Return list of widget.
background position
Definition: style.css.php:368
__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.
img_object($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
error()
Return last error message.
print
Draft customers invoices.
Definition: index.php:91
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:1013
dol_readcachefile($directory, $filename)
Read object from cachefile.
Definition: files.lib.php:2751
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...
dol_filecache($directory, $filename, $object)
Store object in file.
Definition: files.lib.php:2720