modules_boxes.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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. /**
  22. * \file htdocs/core/boxes/modules_boxes.php
  23. * \ingroup facture
  24. * \brief Fichier contenant la classe mere des boites
  25. */
  26. /**
  27. * Class ModeleBoxes
  28. *
  29. * Boxes parent class
  30. */
  31. class ModeleBoxes // Can't be abtract as it is instantiated to build "empty" boxes
  32. {
  33. /**
  34. * @var DoliDB Database handler
  35. */
  36. public $db;
  37. /**
  38. * @var string Error message
  39. */
  40. public $error = '';
  41. /**
  42. * @var int Maximum lines
  43. */
  44. public $max = 5;
  45. /**
  46. * @var int Condition to have widget enabled
  47. */
  48. public $enabled = 1;
  49. /**
  50. * @var boolean Condition to have widget visible (in most cases, permissions)
  51. */
  52. public $hidden = false;
  53. /**
  54. * @var int Box definition database ID
  55. */
  56. public $rowid;
  57. /**
  58. * @var int ID
  59. * @deprecated Same as box_id?
  60. */
  61. public $id;
  62. /**
  63. * @var int Position?
  64. */
  65. public $position;
  66. /**
  67. * @var string Display order
  68. */
  69. public $box_order;
  70. /**
  71. * @var int User ID
  72. */
  73. public $fk_user;
  74. /**
  75. * @var string Source file
  76. */
  77. public $sourcefile;
  78. /**
  79. * @var string Class name
  80. */
  81. public $class;
  82. /**
  83. * @var string ID
  84. */
  85. public $box_id;
  86. /**
  87. * @var string Alphanumeric ID
  88. */
  89. public $boxcode;
  90. /**
  91. * @var string Note
  92. */
  93. public $note;
  94. /**
  95. * @var string Widget type ('graph' means the widget is a graph widget)
  96. */
  97. public $widgettype = '';
  98. /**
  99. * Constructor
  100. *
  101. * @param DoliDB $db Database handler
  102. * @param string $param More parameters
  103. */
  104. public function __construct($db, $param = '')
  105. {
  106. $this->db = $db;
  107. }
  108. /**
  109. * Return last error message
  110. *
  111. * @return string Error message
  112. */
  113. public function error()
  114. {
  115. return $this->error;
  116. }
  117. /**
  118. * Load a box line from its rowid
  119. *
  120. * @param int $rowid Row id to load
  121. *
  122. * @return int <0 if KO, >0 if OK
  123. */
  124. public function fetch($rowid)
  125. {
  126. global $conf;
  127. // Recupere liste des boites d'un user si ce dernier a sa propre liste
  128. $sql = "SELECT b.rowid as id, b.box_id, b.position, b.box_order, b.fk_user";
  129. $sql .= " FROM ".MAIN_DB_PREFIX."boxes as b";
  130. $sql .= " WHERE b.entity = ".$conf->entity;
  131. $sql .= " AND b.rowid = ".((int) $rowid);
  132. dol_syslog(get_class($this)."::fetch rowid=".$rowid);
  133. $resql = $this->db->query($sql);
  134. if ($resql) {
  135. $obj = $this->db->fetch_object($resql);
  136. if ($obj) {
  137. $this->id = $obj->id;
  138. $this->rowid = $obj->id; // For backward compatibility
  139. $this->box_id = $obj->box_id;
  140. $this->position = $obj->position;
  141. $this->box_order = $obj->box_order;
  142. $this->fk_user = $obj->fk_user;
  143. return 1;
  144. } else {
  145. return -1;
  146. }
  147. } else {
  148. return -1;
  149. }
  150. }
  151. /**
  152. * Standard method to show a box (usage by boxes not mandatory, a box can still use its own showBox function)
  153. *
  154. * @param array $head Array with properties of box title
  155. * @param array $contents Array with properties of box lines
  156. * @param int $nooutput No print, only return string
  157. * @return string
  158. */
  159. public function showBox($head = null, $contents = null, $nooutput = 0)
  160. {
  161. global $langs, $user, $conf;
  162. if (!empty($this->hidden)) {
  163. return '\n<!-- Box ".get_class($this)." hidden -->\n'; // Nothing done if hidden (for example when user has no permission)
  164. }
  165. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  166. $MAXLENGTHBOX = 60; // Mettre 0 pour pas de limite
  167. $cachetime = 900; // 900 : 15mn
  168. $cachedir = DOL_DATA_ROOT.'/boxes/temp';
  169. $fileid = get_class($this).'id-'.$this->box_id.'-e'.$conf->entity.'-u'.$user->id.'-s'.$user->socid.'.cache';
  170. $filename = '/box-'.$fileid;
  171. $refresh = dol_cache_refresh($cachedir, $filename, $cachetime);
  172. $out = '';
  173. if ($refresh) {
  174. dol_syslog(get_class($this).'::showBox');
  175. // Define nbcol and nblines of the box to show
  176. $nbcol = 0;
  177. if (isset($contents[0])) {
  178. $nbcol = count($contents[0]);
  179. }
  180. $nblines = count($contents);
  181. $out .= "\n<!-- Box ".get_class($this)." start -->\n";
  182. $out .= '<div class="box boxdraggable" id="boxto_'.$this->box_id.'">'."\n";
  183. if (!empty($head['text']) || !empty($head['sublink']) || !empty($head['subpicto']) || $nblines) {
  184. $out .= '<table summary="boxtable'.$this->box_id.'" width="100%" class="noborder boxtable">'."\n";
  185. }
  186. // Show box title
  187. if (!empty($head['text']) || !empty($head['sublink']) || !empty($head['subpicto'])) {
  188. $out .= '<tr class="liste_titre box_titre">';
  189. $out .= '<th';
  190. if ($nbcol > 0) {
  191. $out .= ' colspan="'.$nbcol.'"';
  192. }
  193. $out .= '>';
  194. if (!empty($conf->use_javascript_ajax)) {
  195. //$out.= '<table summary="" class="nobordernopadding" width="100%"><tr><td class="tdoverflowmax150 maxwidth150onsmartphone">';
  196. $out .= '<div class="tdoverflowmax400 maxwidth250onsmartphone float">';
  197. }
  198. if (!empty($head['text'])) {
  199. $s = dol_trunc($head['text'], isset($head['limit']) ? $head['limit'] : $MAXLENGTHBOX);
  200. $out .= $s;
  201. }
  202. if (!empty($conf->use_javascript_ajax)) {
  203. $out .= '</div>';
  204. }
  205. //$out.= '</td>';
  206. if (!empty($conf->use_javascript_ajax)) {
  207. $sublink = '';
  208. if (!empty($head['sublink'])) {
  209. $sublink .= '<a href="'.$head['sublink'].'"'.(empty($head['target']) ? '' : ' target="'.$head['target'].'"').'>';
  210. }
  211. if (!empty($head['subpicto'])) {
  212. $sublink .= img_picto($head['subtext'], $head['subpicto'], 'class="opacitymedium marginleftonly '.(empty($head['subclass']) ? '' : $head['subclass']).'" id="idsubimg'.$this->boxcode.'"');
  213. }
  214. if (!empty($head['sublink'])) {
  215. $sublink .= '</a>';
  216. }
  217. //$out.= '<td class="nocellnopadd boxclose right nowraponall">';
  218. $out .= '<div class="nocellnopadd boxclose floatright nowraponall">';
  219. $out .= $sublink;
  220. // 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
  221. $out .= img_picto($langs->trans("MoveBox", $this->box_id), 'grip_title', 'class="opacitymedium boxhandle hideonsmartphone cursormove marginleftonly"');
  222. $out .= img_picto($langs->trans("CloseBox", $this->box_id), 'close_title', 'class="opacitymedium boxclose cursorpointer marginleftonly" rel="x:y" id="imgclose'.$this->box_id.'"');
  223. $label = $head['text'];
  224. //if (! empty($head['graph'])) $label.=' ('.$langs->trans("Graph").')';
  225. if (!empty($head['graph'])) {
  226. $label .= ' <span class="opacitymedium fa fa-bar-chart"></span>';
  227. }
  228. $out .= '<input type="hidden" id="boxlabelentry'.$this->box_id.'" value="'.dol_escape_htmltag($label).'">';
  229. //$out.= '</td></tr></table>';
  230. $out .= '</div>';
  231. }
  232. $out .= "</th>";
  233. $out .= "</tr>\n";
  234. }
  235. // Show box lines
  236. if ($nblines) {
  237. // Loop on each record
  238. for ($i = 0, $n = $nblines; $i < $n; $i++) {
  239. if (isset($contents[$i])) {
  240. // TR
  241. if (isset($contents[$i][0]['tr'])) {
  242. $out .= '<tr '.$contents[$i][0]['tr'].'>';
  243. } else {
  244. $out .= '<tr class="oddeven">';
  245. }
  246. // Loop on each TD
  247. $nbcolthisline = count($contents[$i]);
  248. for ($j = 0; $j < $nbcolthisline; $j++) {
  249. // Define tdparam
  250. $tdparam = '';
  251. if (!empty($contents[$i][$j]['td'])) {
  252. $tdparam .= ' '.$contents[$i][$j]['td'];
  253. }
  254. $text = isset($contents[$i][$j]['text']) ? $contents[$i][$j]['text'] : '';
  255. $textwithnotags = preg_replace('/<([^>]+)>/i', '', $text);
  256. $text2 = isset($contents[$i][$j]['text2']) ? $contents[$i][$j]['text2'] : '';
  257. $text2withnotags = preg_replace('/<([^>]+)>/i', '', $text2);
  258. $textnoformat = isset($contents[$i][$j]['textnoformat']) ? $contents[$i][$j]['textnoformat'] : '';
  259. //$out.= "xxx $textwithnotags y";
  260. if (empty($contents[$i][$j]['tooltip'])) {
  261. $contents[$i][$j]['tooltip'] = "";
  262. }
  263. $tooltip = isset($contents[$i][$j]['tooltip']) ? $contents[$i][$j]['tooltip'] : '';
  264. $out .= '<td'.$tdparam.'>'."\n";
  265. // Url
  266. if (!empty($contents[$i][$j]['url']) && empty($contents[$i][$j]['logo'])) {
  267. $out .= '<a href="'.$contents[$i][$j]['url'].'"';
  268. if (!empty($tooltip)) {
  269. $out .= ' title="'.dol_escape_htmltag($langs->trans("Show").' '.$tooltip, 1).'" class="classfortooltip"';
  270. }
  271. //$out.= ' alt="'.$textwithnotags.'"'; // Pas de alt sur un "<a href>"
  272. $out .= isset($contents[$i][$j]['target']) ? ' target="'.$contents[$i][$j]['target'].'"' : '';
  273. $out .= '>';
  274. }
  275. // Logo
  276. if (!empty($contents[$i][$j]['logo'])) {
  277. $logo = preg_replace("/^object_/i", "", $contents[$i][$j]['logo']);
  278. $out .= '<a href="'.$contents[$i][$j]['url'].'">';
  279. $out .= img_object($langs->trans("Show").' '.$tooltip, $logo, 'class="classfortooltip"');
  280. }
  281. $maxlength = $MAXLENGTHBOX;
  282. if (!empty($contents[$i][$j]['maxlength'])) {
  283. $maxlength = $contents[$i][$j]['maxlength'];
  284. }
  285. if ($maxlength) {
  286. $textwithnotags = dol_trunc($textwithnotags, $maxlength);
  287. }
  288. if (preg_match('/^<(img|div|span)/i', $text) || !empty($contents[$i][$j]['asis'])) {
  289. $out .= $text; // show text with no html cleaning
  290. } else {
  291. $out .= $textwithnotags; // show text with html cleaning
  292. }
  293. // End Url
  294. if (!empty($contents[$i][$j]['url'])) {
  295. $out .= '</a>';
  296. }
  297. if (preg_match('/^<(img|div|span)/i', $text2) || !empty($contents[$i][$j]['asis2'])) {
  298. $out .= $text2; // show text with no html cleaning
  299. } else {
  300. $out .= $text2withnotags; // show text with html cleaning
  301. }
  302. if (!empty($textnoformat)) {
  303. $out .= "\n".$textnoformat."\n";
  304. }
  305. $out .= "</td>\n";
  306. }
  307. $out .= "</tr>\n";
  308. }
  309. }
  310. }
  311. if (!empty($head['text']) || !empty($head['sublink']) || !empty($head['subpicto']) || $nblines) {
  312. $out .= "</table>\n";
  313. }
  314. // If invisible box with no contents
  315. if (empty($head['text']) && empty($head['sublink']) && empty($head['subpicto']) && !$nblines) {
  316. $out .= "<br>\n";
  317. }
  318. $out .= "</div>\n";
  319. $out .= "<!-- Box ".get_class($this)." end -->\n\n";
  320. if (!empty($conf->global->MAIN_ACTIVATE_FILECACHE)) {
  321. dol_filecache($cachedir, $filename, $out);
  322. }
  323. } else {
  324. dol_syslog(get_class($this).'::showBoxCached');
  325. $out = "<!-- Box ".get_class($this)." from cache -->";
  326. $out .= dol_readcachefile($cachedir, $filename);
  327. }
  328. if ($nooutput) {
  329. return $out;
  330. } else {
  331. print $out;
  332. }
  333. return '';
  334. }
  335. /**
  336. * Return list of widget. Function used by admin page htdoc/admin/widget.
  337. * List is sorted by widget filename so by priority to run.
  338. *
  339. * @param array $forcedirwidget null=All default directories. This parameter is used by modulebuilder module only.
  340. * @return array Array list of widget
  341. */
  342. public static function getWidgetsList($forcedirwidget = null)
  343. {
  344. global $conf, $langs, $db;
  345. $files = array();
  346. $fullpath = array();
  347. $relpath = array();
  348. $iscoreorexternal = array();
  349. $modules = array();
  350. $orders = array();
  351. $i = 0;
  352. //$dirwidget=array_merge(array('/core/boxes/'), $conf->modules_parts['widgets']);
  353. $dirwidget = array('/core/boxes/'); // $conf->modules_parts['widgets'] is not required
  354. if (is_array($forcedirwidget)) {
  355. $dirwidget = $forcedirwidget;
  356. }
  357. foreach ($dirwidget as $reldir) {
  358. $dir = dol_buildpath($reldir, 0);
  359. $newdir = dol_osencode($dir);
  360. // Check if directory exists (we do not use dol_is_dir to avoid loading files.lib.php at each call)
  361. if (!is_dir($newdir)) {
  362. continue;
  363. }
  364. $handle = opendir($newdir);
  365. if (is_resource($handle)) {
  366. while (($file = readdir($handle)) !== false) {
  367. $reg = array();
  368. if (is_readable($newdir.'/'.$file) && preg_match('/^(.+)\.php/', $file, $reg)) {
  369. if (preg_match('/\.back$/', $file)) {
  370. continue;
  371. }
  372. $part1 = $reg[1];
  373. $modName = ucfirst($reg[1]);
  374. //print "file=$file"; print "modName=$modName"; exit;
  375. if (in_array($modName, $modules)) {
  376. $langs->load("errors");
  377. print '<div class="error">'.$langs->trans("Error").' : '.$langs->trans("ErrorDuplicateWidget", $modName, "").'</div>';
  378. } else {
  379. try {
  380. include_once $newdir.'/'.$file;
  381. } catch (Exception $e) {
  382. print $e->getMessage();
  383. }
  384. }
  385. $files[$i] = $file;
  386. $fullpath[$i] = $dir.'/'.$file;
  387. $relpath[$i] = preg_replace('/^\//', '', $reldir).'/'.$file;
  388. $iscoreorexternal[$i] = ($reldir == '/core/boxes/' ? 'internal' : 'external');
  389. $modules[$i] = $modName;
  390. $orders[$i] = $part1; // Set sort criteria value
  391. $i++;
  392. }
  393. }
  394. closedir($handle);
  395. }
  396. }
  397. asort($orders);
  398. $widget = array();
  399. $j = 0;
  400. // Loop on each widget
  401. foreach ($orders as $key => $value) {
  402. $modName = $modules[$key];
  403. if (empty($modName)) {
  404. continue;
  405. }
  406. if (!class_exists($modName)) {
  407. print 'Error: A widget file was found but its class "'.$modName.'" was not found.'."<br>\n";
  408. continue;
  409. }
  410. $objMod = new $modName($db);
  411. if (is_object($objMod)) {
  412. // Define disabledbyname and disabledbymodule
  413. $disabledbyname = 0;
  414. $disabledbymodule = 0; // TODO Set to 2 if module is not enabled
  415. $module = '';
  416. // Check if widget file is disabled by name
  417. if (preg_match('/NORUN$/i', $files[$key])) {
  418. $disabledbyname = 1;
  419. }
  420. // We set info of modules
  421. $widget[$j]['picto'] = $objMod->picto ? img_object('', $objMod->picto) : img_object('', 'generic');
  422. $widget[$j]['file'] = $files[$key];
  423. $widget[$j]['fullpath'] = $fullpath[$key];
  424. $widget[$j]['relpath'] = $relpath[$key];
  425. $widget[$j]['iscoreorexternal'] = $iscoreorexternal[$key];
  426. $widget[$j]['version'] = empty($objMod->version) ? '' : $objMod->version;
  427. $widget[$j]['status'] = img_picto($langs->trans("Active"), 'tick');
  428. if ($disabledbyname > 0 || $disabledbymodule > 1) {
  429. $widget[$j]['status'] = '';
  430. }
  431. $text = '<b>'.$langs->trans("Description").':</b><br>';
  432. $text .= $objMod->boxlabel.'<br>';
  433. $text .= '<br><b>'.$langs->trans("Status").':</b><br>';
  434. if ($disabledbymodule == 2) {
  435. $text .= $langs->trans("WidgetDisabledAsModuleDisabled", $module).'<br>';
  436. }
  437. $widget[$j]['info'] = $text;
  438. }
  439. $j++;
  440. }
  441. return $widget;
  442. }
  443. }