infobox.class.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. <?php
  2. /* Copyright (C) 2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2012 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@inodbox.com>
  5. * Copyright (C) 2019 Nicolas ZABOURI <info@inovea-conseil.com>
  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. */
  20. /**
  21. * \file htdocs/core/class/infobox.class.php
  22. * \brief File of class to manage widget boxes
  23. */
  24. /**
  25. * Class to manage boxes on pages. This is an utility class (all is static)
  26. */
  27. class InfoBox
  28. {
  29. /**
  30. * Name of positions (See below)
  31. *
  32. * @return string[] Array with list of zones
  33. */
  34. public static function getListOfPagesForBoxes()
  35. {
  36. global $conf;
  37. if (empty($conf->global->MAIN_FEATURES_LEVEL) || $conf->global->MAIN_FEATURES_LEVEL < 2) {
  38. return array(
  39. 0 => 'Home',
  40. 1 => 'UsersHome',
  41. 2 => 'MembersHome',
  42. 3 => 'ThirdpartiesHome',
  43. 11 => 'TicketsHome',
  44. 27 => 'AccountancyHome'
  45. );
  46. } else {
  47. return array(
  48. 0 => 'Home',
  49. 1 => 'UsersHome',
  50. 2 => 'MembersHome',
  51. 3 => 'ThirdpartiesHome',
  52. 4 => 'productindex',
  53. 5 => 'productindex',
  54. 6 => 'mrpindex',
  55. 7 => 'commercialindex',
  56. 8 => 'projectsindex',
  57. 9 => 'invoiceindex',
  58. 10 => 'hrmindex',
  59. 11 => 'TicketsHome',
  60. 12 => 'stockindex',
  61. 13 => 'sendingindex',
  62. 14 => 'receptionindex',
  63. 15 => 'activityindex',
  64. 16 => 'proposalindex',
  65. 17 => 'ordersindex',
  66. 18 => 'orderssuppliersindex',
  67. 19 => 'contractindex',
  68. 20 => 'interventionindex',
  69. 21 => 'suppliersproposalsindex',
  70. 22 => 'donationindex',
  71. 23 => 'specialexpensesindex',
  72. 24 => 'expensereportindex',
  73. 25 => 'mailingindex',
  74. 26 => 'opensurveyindex',
  75. 27 => 'AccountancyHome'
  76. );
  77. }
  78. }
  79. /**
  80. * Return array of boxes qualified for area and user
  81. *
  82. * @param DoliDB $db Database handler
  83. * @param string $mode 'available' or 'activated'
  84. * @param int $zone Name or area (-1 for all, 0 for Homepage, 1 for Accountancy, 2 for xxx, ...)
  85. * @param User|null $user Object user to filter
  86. * @param array $excludelist Array of box id (box.box_id = boxes_def.rowid) to exclude
  87. * @param int $includehidden Include also hidden boxes
  88. * @return array Array of boxes
  89. */
  90. public static function listBoxes($db, $mode, $zone, $user = null, $excludelist = array(), $includehidden = 1)
  91. {
  92. global $conf;
  93. $boxes = array();
  94. if ($mode == 'activated') { // activated
  95. $sql = "SELECT b.rowid, b.position, b.box_order, b.fk_user,";
  96. $sql .= " d.rowid as box_id, d.file, d.note, d.tms";
  97. $sql .= " FROM ".MAIN_DB_PREFIX."boxes as b, ".MAIN_DB_PREFIX."boxes_def as d";
  98. $sql .= " WHERE b.box_id = d.rowid";
  99. $sql .= " AND b.entity IN (0,".$conf->entity.")";
  100. if ($zone >= 0) {
  101. $sql .= " AND b.position = ".((int) $zone);
  102. }
  103. if (is_object($user)) {
  104. $sql .= " AND b.fk_user IN (0,".$user->id.")";
  105. } else {
  106. $sql .= " AND b.fk_user = 0";
  107. }
  108. $sql .= " ORDER BY b.box_order";
  109. } else { // available
  110. $sql = "SELECT d.rowid as box_id, d.file, d.note, d.tms";
  111. $sql .= " FROM ".MAIN_DB_PREFIX."boxes_def as d";
  112. $sql .= " WHERE d.entity IN (0, ".$conf->entity.")";
  113. }
  114. dol_syslog(get_class()."::listBoxes get default box list for mode=".$mode." userid=".(is_object($user) ? $user->id : '')."", LOG_DEBUG);
  115. $resql = $db->query($sql);
  116. if ($resql) {
  117. $num = $db->num_rows($resql);
  118. $j = 0;
  119. while ($j < $num) {
  120. $obj = $db->fetch_object($resql);
  121. if (!in_array($obj->box_id, $excludelist)) {
  122. $regs = array();
  123. if (preg_match('/^([^@]+)@([^@]+)$/i', $obj->file, $regs)) {
  124. $boxname = preg_replace('/\.php$/i', '', $regs[1]);
  125. $module = $regs[2];
  126. $relsourcefile = "/".$module."/core/boxes/".$boxname.".php";
  127. } else {
  128. $boxname = preg_replace('/\.php$/i', '', $obj->file);
  129. $relsourcefile = "/core/boxes/".$boxname.".php";
  130. }
  131. //print $obj->box_id.'-'.$boxname.'-'.$relsourcefile.'<br>';
  132. // TODO PERF Do not make "dol_include_once" here, nor "new" later. This means, we must store a 'depends' field to store modules list, then
  133. // the "enabled" condition for modules forbidden for external users and the depends condition can be done.
  134. // Goal is to avoid making a "new" done for each boxes returned by select.
  135. dol_include_once($relsourcefile);
  136. if (class_exists($boxname)) {
  137. $box = new $boxname($db, $obj->note); // Constructor may set properties like box->enabled. obj->note is note into box def, not user params.
  138. //$box=new stdClass();
  139. // box properties
  140. $box->rowid = (empty($obj->rowid) ? '' : $obj->rowid);
  141. $box->id = (empty($obj->box_id) ? '' : $obj->box_id);
  142. $box->position = ((isset($obj->position) && $obj->position == '') ? '' : (isset($obj->position) ? $obj->position : '')); // '0' must stay '0'
  143. $box->box_order = (empty($obj->box_order) ? '' : $obj->box_order);
  144. $box->fk_user = (empty($obj->fk_user) ? 0 : $obj->fk_user);
  145. $box->sourcefile = $relsourcefile;
  146. $box->class = $boxname;
  147. if ($mode == 'activated' && !is_object($user)) { // List of activated box was not yet personalized into database
  148. if (is_numeric($box->box_order)) {
  149. if ($box->box_order % 2 == 1) {
  150. $box->box_order = 'A'.$box->box_order;
  151. } elseif ($box->box_order % 2 == 0) {
  152. $box->box_order = 'B'.$box->box_order;
  153. }
  154. }
  155. }
  156. // box_def properties
  157. $box->box_id = (empty($obj->box_id) ? '' : $obj->box_id);
  158. $box->note = (empty($obj->note) ? '' : $obj->note);
  159. // Filter on box->enabled (used for example by box_comptes)
  160. // Filter also on box->depends. Example: array("product|service") or array("contrat", "service")
  161. $enabled = $box->enabled;
  162. if (isset($box->depends) && count($box->depends) > 0) {
  163. foreach ($box->depends as $moduleelem) {
  164. $arrayelem = explode('|', $moduleelem);
  165. $tmpenabled = 0; // $tmpenabled is used for the '|' test (OR)
  166. foreach ($arrayelem as $module) {
  167. $tmpmodule = preg_replace('/@[^@]+/', '', $module);
  168. if (!empty($conf->$tmpmodule->enabled)) {
  169. $tmpenabled = 1;
  170. }
  171. //print $boxname.'-'.$module.'-module enabled='.(empty($conf->$tmpmodule->enabled)?0:1).'<br>';
  172. }
  173. if (empty($tmpenabled)) { // We found at least one module required that is disabled
  174. $enabled = 0;
  175. break;
  176. }
  177. }
  178. }
  179. //print '=>'.$boxname.'-enabled='.$enabled.'<br>';
  180. //print 'xx module='.$module.' enabled='.$enabled;
  181. if ($enabled && ($includehidden || empty($box->hidden))) {
  182. $boxes[] = $box;
  183. } else {
  184. unset($box);
  185. }
  186. } else {
  187. dol_syslog("Failed to load box '".$boxname."' into file '".$relsourcefile."'", LOG_WARNING);
  188. }
  189. }
  190. $j++;
  191. }
  192. } else {
  193. dol_syslog($db->lasterror(), LOG_ERR);
  194. return array('error'=>$db->lasterror());
  195. }
  196. return $boxes;
  197. }
  198. /**
  199. * Save order of boxes for area and user
  200. *
  201. * @param DoliDB $db Database handler
  202. * @param int $zone Name of area (0 for Homepage, ...)
  203. * @param string $boxorder List of boxes with correct order 'A:123,456,...-B:789,321...'
  204. * @param int $userid Id of user
  205. * @return int <0 if KO, 0=Nothing done, > 0 if OK
  206. */
  207. public static function saveboxorder($db, $zone, $boxorder, $userid = 0)
  208. {
  209. global $conf;
  210. $error = 0;
  211. require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
  212. dol_syslog(get_class()."::saveboxorder zone=".$zone." userid=".$userid);
  213. if (!$userid || $userid == 0) {
  214. return 0;
  215. }
  216. $user = new User($db);
  217. $user->id = $userid;
  218. $db->begin();
  219. // Save parameters to say user has a dedicated setup
  220. $tab = array();
  221. $confuserzone = 'MAIN_BOXES_'.$zone;
  222. $tab[$confuserzone] = 1;
  223. if (dol_set_user_param($db, $conf, $user, $tab) < 0) {
  224. $error = $db->lasterror();
  225. $db->rollback();
  226. return -3;
  227. }
  228. // Delete all lines
  229. $sql = "DELETE FROM ".MAIN_DB_PREFIX."boxes";
  230. $sql .= " WHERE entity = ".$conf->entity;
  231. $sql .= " AND fk_user = ".((int) $userid);
  232. $sql .= " AND position = ".((int) $zone);
  233. dol_syslog(get_class()."::saveboxorder", LOG_DEBUG);
  234. $result = $db->query($sql);
  235. if ($result) {
  236. $colonnes = explode('-', $boxorder);
  237. foreach ($colonnes as $collist) {
  238. $part = explode(':', $collist);
  239. $colonne = $part[0];
  240. $list = $part[1];
  241. dol_syslog(get_class()."::saveboxorder column=".$colonne.' list='.$list);
  242. $i = 0;
  243. $listarray = explode(',', $list);
  244. foreach ($listarray as $id) {
  245. if (is_numeric($id)) {
  246. //dol_syslog("aaaaa".count($listarray));
  247. $i++;
  248. $ii = sprintf('%02d', $i);
  249. $sql = "INSERT INTO ".MAIN_DB_PREFIX."boxes";
  250. $sql .= "(box_id, position, box_order, fk_user, entity)";
  251. $sql .= " values (";
  252. $sql .= " ".((int) $id).",";
  253. $sql .= " ".((int) $zone).",";
  254. $sql .= " '".$db->escape($colonne.$ii)."',";
  255. $sql .= " ".((int) $userid).",";
  256. $sql .= " ".((int) $conf->entity);
  257. $sql .= ")";
  258. $result = $db->query($sql);
  259. if ($result < 0) {
  260. $error++;
  261. break;
  262. }
  263. }
  264. }
  265. }
  266. } else {
  267. $error++;
  268. }
  269. if ($error) {
  270. $db->rollback();
  271. return -2;
  272. } else {
  273. $db->commit();
  274. return 1;
  275. }
  276. }
  277. }