PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/core/class/infobox.class.php

https://bitbucket.org/speedealing/speedealing
PHP | 254 lines | 175 code | 29 blank | 50 comment | 34 complexity | 7c13e77f1482216ba32bda79e1487651 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1, GPL-3.0, MIT
  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@capnetworks.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * \file htdocs/core/class/infobox.class.php
  21. * \brief File of class to manage widget boxes
  22. */
  23. /**
  24. * Class to manage boxes on pages
  25. */
  26. class InfoBox
  27. {
  28. /**
  29. * Return array of boxes qualified for area and user
  30. *
  31. * @param DoliDB $db Database handler
  32. * @param string $mode 'available' or 'activated'
  33. * @param string $zone Name or area (-1 for all, 0 for Homepage, 1 for xxx, ...)
  34. * @param User $user Objet user to filter (used only if $zone >= 0)
  35. * @param array $excludelist Array of box id (box.box_id = boxes_def.rowid) to exclude
  36. * @return array Array of boxes
  37. */
  38. static function listBoxes($db, $mode, $zone, $user, $excludelist=array())
  39. {
  40. global $conf;
  41. $boxes=array();
  42. $confuserzone='MAIN_BOXES_'.$zone;
  43. if ($mode == 'activated')
  44. {
  45. $sql = "SELECT b.rowid, b.position, b.box_order, b.fk_user,";
  46. $sql.= " d.rowid as box_id, d.file, d.note, d.tms";
  47. $sql.= " FROM ".MAIN_DB_PREFIX."boxes as b, ".MAIN_DB_PREFIX."boxes_def as d";
  48. $sql.= " WHERE b.box_id = d.rowid";
  49. $sql.= " AND b.entity = ".$conf->entity;
  50. if ($zone >= 0) $sql.= " AND b.position = ".$zone;
  51. if ($user->id && ! empty($user->conf->$confuserzone)) $sql.= " AND b.fk_user = ".$user->id;
  52. else $sql.= " AND b.fk_user = 0";
  53. $sql.= " ORDER BY b.box_order";
  54. }
  55. else
  56. {
  57. $sql = "SELECT d.rowid as box_id, d.file, d.note, d.tms";
  58. $sql.= " FROM ".MAIN_DB_PREFIX."boxes_def as d";
  59. if (! empty($conf->multicompany->enabled) && ! empty($conf->multicompany->transverse_mode)) {
  60. $sql.= " WHERE entity IN (1,".$conf->entity.")"; // TODO add method for define another master entity
  61. } else {
  62. $sql.= " WHERE entity = ".$conf->entity;
  63. }
  64. }
  65. $resql = $db->query($sql);
  66. if ($resql)
  67. {
  68. $num = $db->num_rows($resql);
  69. $j = 0;
  70. while ($j < $num)
  71. {
  72. $obj = $db->fetch_object($resql);
  73. if (! in_array($obj->box_id, $excludelist))
  74. {
  75. if (preg_match('/^([^@]+)@([^@]+)$/i',$obj->file,$regs))
  76. {
  77. $boxname = $regs[1];
  78. $module = $regs[2];
  79. $relsourcefile = "/".$module."/core/boxes/".$boxname.".php";
  80. }
  81. else
  82. {
  83. $boxname=preg_replace('/.php$/i','',$obj->file);
  84. $relsourcefile = "/core/boxes/".$boxname.".php";
  85. }
  86. dol_include_once($relsourcefile);
  87. if (class_exists($boxname))
  88. {
  89. $box=new $boxname($db,$obj->note);
  90. // box properties
  91. $box->rowid = (! empty($obj->rowid) ? $obj->rowid : '');
  92. $box->id = (! empty($obj->box_id) ? $obj->box_id : '');
  93. $box->position = (! empty($obj->position) ? $obj->position : '');
  94. $box->box_order = (! empty($obj->box_order) ? $obj->box_order : '');
  95. $box->fk_user = (! empty($obj->fk_user) ? $obj->fk_user : '');
  96. $box->sourcefile=$relsourcefile;
  97. if ($mode == 'activated' && (! $user->id || empty($user->conf->$confuserzone))) // List of activated box was not yet personalized into database
  98. {
  99. if (is_numeric($box->box_order))
  100. {
  101. if ($box->box_order % 2 == 1) $box->box_order='A'.$box->box_order;
  102. elseif ($box->box_order % 2 == 0) $box->box_order='B'.$box->box_order;
  103. }
  104. }
  105. // box_def properties
  106. $box->box_id = (! empty($obj->box_id) ? $obj->box_id : '');
  107. $box->note = (! empty($obj->note) ? $obj->note : '');
  108. $enabled=true;
  109. if (isset($box->depends) && count($box->depends) > 0)
  110. {
  111. foreach($box->depends as $module)
  112. {
  113. //print $boxname.'-'.$module.'<br>';
  114. if (empty($conf->$module->enabled)) $enabled=false;
  115. }
  116. }
  117. if ($enabled) $boxes[]=$box;
  118. }
  119. }
  120. $j++;
  121. }
  122. }
  123. else
  124. {
  125. //dol_print_error($db);
  126. $error=$db->error();
  127. dol_syslog(get_class()."::listBoxes Error ".$error, LOG_ERR);
  128. return array();
  129. }
  130. return $boxes;
  131. }
  132. /**
  133. * Save order of boxes for area and user
  134. *
  135. * @param DoliDB $db Database handler
  136. * @param string $zone Name of area (0 for Homepage, ...)
  137. * @param string $boxorder List of boxes with correct order 'A:123,456,...-B:789,321...'
  138. * @param int $userid Id of user
  139. * @return int <0 if KO, >= 0 if OK
  140. */
  141. static function saveboxorder($db, $zone,$boxorder,$userid=0)
  142. {
  143. global $conf;
  144. $error=0;
  145. require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
  146. dol_syslog(get_class()."::saveboxorder zone=".$zone." userid=".$userid);
  147. if (! $userid || $userid == 0) return 0;
  148. $user = new User($db);
  149. $user->id=$userid;
  150. $db->begin();
  151. // Sauve parametre indiquant que le user a une config dediee
  152. $confuserzone='MAIN_BOXES_'.$zone;
  153. $tab[$confuserzone]=1;
  154. if (dol_set_user_param($db, $conf, $user, $tab) < 0)
  155. {
  156. $this->error=$db->lasterror();
  157. $db->rollback();
  158. return -3;
  159. }
  160. // Delete all lines
  161. $sql = "DELETE FROM ".MAIN_DB_PREFIX."boxes";
  162. $sql.= " WHERE entity = ".$conf->entity;
  163. $sql.= " AND fk_user = ".$userid;
  164. $sql.= " AND position = ".$zone;
  165. dol_syslog(get_class()."::saveboxorder sql=".$sql);
  166. $result = $db->query($sql);
  167. if ($result)
  168. {
  169. $colonnes=explode('-',$boxorder);
  170. foreach ($colonnes as $collist)
  171. {
  172. $part=explode(':',$collist);
  173. $colonne=$part[0];
  174. $list=$part[1];
  175. dol_syslog(get_class()."::saveboxorder column=".$colonne.' list='.$list);
  176. $i=0;
  177. $listarray=explode(',',$list);
  178. foreach ($listarray as $id)
  179. {
  180. if (is_numeric($id))
  181. {
  182. //dol_syslog("aaaaa".count($listarray));
  183. $i++;
  184. $ii=sprintf('%02d',$i);
  185. $sql = "INSERT INTO ".MAIN_DB_PREFIX."boxes";
  186. $sql.= "(box_id, position, box_order, fk_user, entity)";
  187. $sql.= " values (";
  188. $sql.= " ".$id.",";
  189. $sql.= " ".$zone.",";
  190. $sql.= " '".$colonne.$ii."',";
  191. $sql.= " ".$userid.",";
  192. $sql.= " ".$conf->entity;
  193. $sql.= ")";
  194. dol_syslog(get_class()."::saveboxorder sql=".$sql);
  195. $result = $db->query($sql);
  196. if ($result < 0)
  197. {
  198. $error++;
  199. break;
  200. }
  201. }
  202. }
  203. }
  204. if ($error)
  205. {
  206. $error=$db->error();
  207. $db->rollback();
  208. return -2;
  209. }
  210. else
  211. {
  212. $db->commit();
  213. return 1;
  214. }
  215. }
  216. else
  217. {
  218. $error=$db->lasterror();
  219. $db->rollback();
  220. dol_syslog(get_class()."::saveboxorder ".$error);
  221. return -1;
  222. }
  223. }
  224. }
  225. ?>