dolibarr  13.0.2
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 
118  public function __construct($db, $param = '')
119  {
120  $this->db = $db;
121  }
122 
128  public function error()
129  {
130  return $this->error;
131  }
132 
133 
141  public 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 as id, 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 = ".((int) $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->id = $obj->id;
159  $this->rowid = $obj->id; // For backward compatibility
160  $this->box_id = $obj->box_id;
161  $this->position = $obj->position;
162  $this->box_order = $obj->box_order;
163  $this->fk_user = $obj->fk_user;
164  return 1;
165  } else {
166  return -1;
167  }
168  } else {
169  return -1;
170  }
171  }
172 
181  public function showBox($head = null, $contents = null, $nooutput = 0)
182  {
183  global $langs, $user, $conf;
184 
185  if (!empty($this->hidden)) return '\n<!-- Box ".get_class($this)." hidden -->\n'; // Nothing done if hidden (for example when user has no permission)
186 
187  require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
188 
189  $MAXLENGTHBOX = 60; // Mettre 0 pour pas de limite
190 
191  $cachetime = 900; // 900 : 15mn
192  $cachedir = DOL_DATA_ROOT.'/boxes/temp';
193  $fileid = get_class($this).'id-'.$this->box_id.'-e'.$conf->entity.'-u'.$user->id.'-s'.$user->socid.'.cache';
194  $filename = '/box-'.$fileid;
195  $refresh = dol_cache_refresh($cachedir, $filename, $cachetime);
196  $out = '';
197 
198  if ($refresh) {
199  dol_syslog(get_class($this).'::showBox');
200 
201  // Define nbcol and nblines of the box to show
202  $nbcol = 0;
203  if (isset($contents[0])) $nbcol = count($contents[0]);
204  $nblines = count($contents);
205 
206  $out .= "\n<!-- Box ".get_class($this)." start -->\n";
207 
208  $out .= '<div class="box boxdraggable" id="boxto_'.$this->box_id.'">'."\n";
209 
210  if (!empty($head['text']) || !empty($head['sublink']) || !empty($head['subpicto']) || $nblines)
211  {
212  $out .= '<table summary="boxtable'.$this->box_id.'" width="100%" class="noborder boxtable">'."\n";
213  }
214 
215  // Show box title
216  if (!empty($head['text']) || !empty($head['sublink']) || !empty($head['subpicto']))
217  {
218  $out .= '<tr class="liste_titre box_titre">';
219  $out .= '<td';
220  if ($nbcol > 0) { $out .= ' colspan="'.$nbcol.'"'; }
221  $out .= '>';
222  if (!empty($conf->use_javascript_ajax))
223  {
224  //$out.= '<table summary="" class="nobordernopadding" width="100%"><tr><td class="tdoverflowmax150 maxwidth150onsmartphone">';
225  $out .= '<div class="tdoverflowmax250 maxwidth150onsmartphone float">';
226  }
227  if (!empty($head['text']))
228  {
229  $s = dol_trunc($head['text'], isset($head['limit']) ? $head['limit'] : $MAXLENGTHBOX);
230  $out .= $s;
231  }
232  if (!empty($conf->use_javascript_ajax))
233  {
234  $out .= '</div>';
235  }
236  //$out.= '</td>';
237 
238  if (!empty($conf->use_javascript_ajax))
239  {
240  $sublink = '';
241  if (!empty($head['sublink'])) $sublink .= '<a href="'.$head['sublink'].'"'.(empty($head['target']) ? '' : ' target="'.$head['target'].'"').'>';
242  if (!empty($head['subpicto'])) $sublink .= img_picto($head['subtext'], $head['subpicto'], 'class="opacitymedium marginleftonly '.(empty($head['subclass']) ? '' : $head['subclass']).'" id="idsubimg'.$this->boxcode.'"');
243  if (!empty($head['sublink'])) $sublink .= '</a>';
244 
245  //$out.= '<td class="nocellnopadd boxclose right nowraponall">';
246  $out .= '<div class="nocellnopadd boxclose floatright nowraponall">';
247  $out .= $sublink;
248  // 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
249  $out .= img_picto($langs->trans("MoveBox", $this->box_id), 'grip_title', 'class="opacitymedium boxhandle hideonsmartphone cursormove marginleftonly"');
250  $out .= img_picto($langs->trans("CloseBox", $this->box_id), 'close_title', 'class="opacitymedium boxclose cursorpointer marginleftonly" rel="x:y" id="imgclose'.$this->box_id.'"');
251  $label = $head['text'];
252  //if (! empty($head['graph'])) $label.=' ('.$langs->trans("Graph").')';
253  if (!empty($head['graph'])) $label .= ' <span class="opacitymedium fa fa-bar-chart"></span>';
254  $out .= '<input type="hidden" id="boxlabelentry'.$this->box_id.'" value="'.dol_escape_htmltag($label).'">';
255  //$out.= '</td></tr></table>';
256  $out .= '</div>';
257  }
258 
259  $out .= "</td>";
260  $out .= "</tr>\n";
261  }
262 
263  // Show box lines
264  if ($nblines)
265  {
266  // Loop on each record
267  for ($i = 0, $n = $nblines; $i < $n; $i++)
268  {
269  if (isset($contents[$i]))
270  {
271  // TR
272  if (isset($contents[$i][0]['tr'])) $out .= '<tr '.$contents[$i][0]['tr'].'>';
273  else $out .= '<tr class="oddeven">';
274 
275  // Loop on each TD
276  $nbcolthisline = count($contents[$i]);
277  for ($j = 0; $j < $nbcolthisline; $j++) {
278  // Define tdparam
279  $tdparam = '';
280  if (!empty($contents[$i][$j]['td'])) $tdparam .= ' '.$contents[$i][$j]['td'];
281 
282  $text = isset($contents[$i][$j]['text']) ? $contents[$i][$j]['text'] : '';
283  $textwithnotags = preg_replace('/<([^>]+)>/i', '', $text);
284  $text2 = isset($contents[$i][$j]['text2']) ? $contents[$i][$j]['text2'] : '';
285  $text2withnotags = preg_replace('/<([^>]+)>/i', '', $text2);
286 
287  $textnoformat = isset($contents[$i][$j]['textnoformat']) ? $contents[$i][$j]['textnoformat'] : '';
288  //$out.= "xxx $textwithnotags y";
289  if (empty($contents[$i][$j]['tooltip'])) $contents[$i][$j]['tooltip'] = "";
290  $tooltip = isset($contents[$i][$j]['tooltip']) ? $contents[$i][$j]['tooltip'] : '';
291 
292  $out .= '<td'.$tdparam.'>'."\n";
293 
294  // Url
295  if (!empty($contents[$i][$j]['url']) && empty($contents[$i][$j]['logo']))
296  {
297  $out .= '<a href="'.$contents[$i][$j]['url'].'"';
298  if (!empty($tooltip)) {
299  $out .= ' title="'.dol_escape_htmltag($langs->trans("Show").' '.$tooltip, 1).'" class="classfortooltip"';
300  }
301  //$out.= ' alt="'.$textwithnotags.'"'; // Pas de alt sur un "<a href>"
302  $out .= isset($contents[$i][$j]['target']) ? ' target="'.$contents[$i][$j]['target'].'"' : '';
303  $out .= '>';
304  }
305 
306  // Logo
307  if (!empty($contents[$i][$j]['logo']))
308  {
309  $logo = preg_replace("/^object_/i", "", $contents[$i][$j]['logo']);
310  $out .= '<a href="'.$contents[$i][$j]['url'].'">';
311  $out .= img_object($langs->trans("Show").' '.$tooltip, $logo, 'class="classfortooltip"');
312  }
313 
314  $maxlength = $MAXLENGTHBOX;
315  if (!empty($contents[$i][$j]['maxlength'])) $maxlength = $contents[$i][$j]['maxlength'];
316 
317  if ($maxlength) $textwithnotags = dol_trunc($textwithnotags, $maxlength);
318  if (preg_match('/^<(img|div|span)/i', $text) || !empty($contents[$i][$j]['asis'])) $out .= $text; // show text with no html cleaning
319  else $out .= $textwithnotags; // show text with html cleaning
320 
321  // End Url
322  if (!empty($contents[$i][$j]['url'])) $out .= '</a>';
323 
324  if (preg_match('/^<(img|div|span)/i', $text2) || !empty($contents[$i][$j]['asis2'])) $out .= $text2; // show text with no html cleaning
325  else $out .= $text2withnotags; // show text with html cleaning
326 
327  if (!empty($textnoformat)) $out .= "\n".$textnoformat."\n";
328 
329  $out .= "</td>\n";
330  }
331 
332  $out .= "</tr>\n";
333  }
334  }
335  }
336 
337  if (!empty($head['text']) || !empty($head['sublink']) || !empty($head['subpicto']) || $nblines)
338  {
339  $out .= "</table>\n";
340  }
341 
342  // If invisible box with no contents
343  if (empty($head['text']) && empty($head['sublink']) && empty($head['subpicto']) && !$nblines) $out .= "<br>\n";
344 
345  $out .= "</div>\n";
346 
347  $out .= "<!-- Box ".get_class($this)." end -->\n\n";
348  if (!empty($conf->global->MAIN_ACTIVATE_FILECACHE)) {
349  dol_filecache($cachedir, $filename, $out);
350  }
351  } else {
352  dol_syslog(get_class($this).'::showBoxCached');
353  $out = "<!-- Box ".get_class($this)." from cache -->";
354  $out .= dol_readcachefile($cachedir, $filename);
355  }
356 
357  if ($nooutput) return $out;
358  else print $out;
359 
360  return '';
361  }
362 
363 
371  public static function getWidgetsList($forcedirwidget = null)
372  {
373  global $conf, $langs, $db;
374 
375  $files = array();
376  $fullpath = array();
377  $relpath = array();
378  $iscoreorexternal = array();
379  $modules = array();
380  $orders = array();
381  $i = 0;
382 
383  //$dirwidget=array_merge(array('/core/boxes/'), $conf->modules_parts['widgets']);
384  $dirwidget = array('/core/boxes/'); // $conf->modules_parts['widgets'] is not required
385  if (is_array($forcedirwidget))
386  {
387  $dirwidget = $forcedirwidget;
388  }
389 
390  foreach ($dirwidget as $reldir)
391  {
392  $dir = dol_buildpath($reldir, 0);
393  $newdir = dol_osencode($dir);
394 
395  // Check if directory exists (we do not use dol_is_dir to avoid loading files.lib.php at each call)
396  if (!is_dir($newdir)) continue;
397 
398  $handle = opendir($newdir);
399  if (is_resource($handle))
400  {
401  while (($file = readdir($handle)) !== false)
402  {
403  $reg = array();
404  if (is_readable($newdir.'/'.$file) && preg_match('/^(.+)\.php/', $file, $reg))
405  {
406  if (preg_match('/\.back$/', $file)) continue;
407 
408  $part1 = $reg[1];
409 
410  $modName = ucfirst($reg[1]);
411  //print "file=$file"; print "modName=$modName"; exit;
412  if (in_array($modName, $modules))
413  {
414  $langs->load("errors");
415  print '<div class="error">'.$langs->trans("Error").' : '.$langs->trans("ErrorDuplicateWidget", $modName, "").'</div>';
416  } else {
417  try {
418  include_once $newdir.'/'.$file;
419  } catch (Exception $e)
420  {
421  print $e->getMessage();
422  }
423  }
424 
425  $files[$i] = $file;
426  $fullpath[$i] = $dir.'/'.$file;
427  $relpath[$i] = preg_replace('/^\//', '', $reldir).'/'.$file;
428  $iscoreorexternal[$i] = ($reldir == '/core/boxes/' ? 'internal' : 'external');
429  $modules[$i] = $modName;
430  $orders[$i] = $part1; // Set sort criteria value
431 
432  $i++;
433  }
434  }
435  closedir($handle);
436  }
437  }
438 
439  asort($orders);
440 
441  $widget = array();
442  $j = 0;
443 
444  // Loop on each widget
445  foreach ($orders as $key => $value)
446  {
447  $modName = $modules[$key];
448  if (empty($modName)) continue;
449 
450  if (!class_exists($modName))
451  {
452  print 'Error: A widget file was found but its class "'.$modName.'" was not found.'."<br>\n";
453  continue;
454  }
455 
456  $objMod = new $modName($db);
457  if (is_object($objMod))
458  {
459  // Define disabledbyname and disabledbymodule
460  $disabledbyname = 0;
461  $disabledbymodule = 0; // TODO Set to 2 if module is not enabled
462  $module = '';
463 
464  // Check if widget file is disabled by name
465  if (preg_match('/NORUN$/i', $files[$key])) $disabledbyname = 1;
466 
467  // We set info of modules
468  $widget[$j]['picto'] = $objMod->picto ?img_object('', $objMod->picto) : img_object('', 'generic');
469  $widget[$j]['file'] = $files[$key];
470  $widget[$j]['fullpath'] = $fullpath[$key];
471  $widget[$j]['relpath'] = $relpath[$key];
472  $widget[$j]['iscoreorexternal'] = $iscoreorexternal[$key];
473  //$widget[$j]['version'] = $objMod->getVersion();
474  $widget[$j]['status'] = img_picto($langs->trans("Active"), 'tick');
475  if ($disabledbyname > 0 || $disabledbymodule > 1) $widget[$j]['status'] = '';
476 
477  $text = '<b>'.$langs->trans("Description").':</b><br>';
478  $text .= $objMod->boxlabel.'<br>';
479  $text .= '<br><b>'.$langs->trans("Status").':</b><br>';
480  if ($disabledbymodule == 2) $text .= $langs->trans("WidgetDisabledAsModuleDisabled", $module).'<br>';
481 
482  $widget[$j]['info'] = $text;
483  }
484  $j++;
485  }
486  return $widget;
487  }
488 }
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:2943
</td >< tdcolspan="3">< spanclass="opacitymedium"></span ></td ></tr >< trclass="liste_total"> CREANCES DETTES< tdcolspan="3"class="right"></td >< tdcolspan="3"class="right"></td ></tr > CREANCES DETTES RECETTES DEPENSES trips CREANCES DETTES Y m expensereport p date_valid Y m expensereport pe datep $db idate($date_start)."' AND $column < p rowid
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.
$conf db
API class for accounts.
Definition: inc.php:54
img_picto($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt= '', $morecss= '', $marginleftonlyshort=2)
Show picto whatever it&#39;s its name (generic function)
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
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:89
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->don->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)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1232
dol_readcachefile($directory, $filename)
Read object from cachefile.
Definition: files.lib.php:2958
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.
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:2927
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $keepmoretags= '', $escapeonlyhtmltags=0)
Returns text escaped for inclusion in HTML alt or title tags, or into values of HTML input fields...