index.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. <?php
  2. /* Copyright (C) 2007 Patrick Raguin <patrick.raguin@gmail.com>
  3. * Copyright (C) 2007-2011 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2009-2011 Regis Houssin <regis@dolibarr.fr>
  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 2 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/admin/menus/index.php
  21. * \ingroup core
  22. * \brief Index page for menu editor
  23. */
  24. require("../../main.inc.php");
  25. require_once(DOL_DOCUMENT_ROOT."/core/class/html.formadmin.class.php");
  26. require_once(DOL_DOCUMENT_ROOT."/lib/treeview.lib.php");
  27. $langs->load("other");
  28. $langs->load("admin");
  29. if (! $user->admin)
  30. accessforbidden();
  31. $dirtop = "/includes/menus/standard";
  32. $dirleft = "/includes/menus/standard";
  33. $dirsmartphone = "/includes/menus/smartphone";
  34. $dirmenu = array($dirleft,$dirsmartphone);
  35. $mesg=$_GET["mesg"];
  36. $menu_handler_top=$conf->global->MAIN_MENU_STANDARD;
  37. $menu_handler_smartphone=$conf->global->MAIN_MENU_SMARTPHONE;
  38. $menu_handler_top=preg_replace('/_backoffice.php/i','',$menu_handler_top);
  39. $menu_handler_top=preg_replace('/_frontoffice.php/i','',$menu_handler_top);
  40. $menu_handler_smartphone=preg_replace('/_backoffice.php/i','',$menu_handler_smartphone);
  41. $menu_handler_smartphone=preg_replace('/_frontoffice.php/i','',$menu_handler_smartphone);
  42. $menu_handler=$menu_handler_top;
  43. if ($_REQUEST["handler_origine"]) $menu_handler=$_REQUEST["handler_origine"];
  44. if ($_REQUEST["menu_handler"]) $menu_handler=$_REQUEST["menu_handler"];
  45. /*
  46. * Actions
  47. */
  48. if (isset($_GET["action"]) && ($_GET["action"] == 'up'))
  49. {
  50. $current=array();
  51. $previous=array();
  52. // Get current position
  53. $sql = "SELECT m.rowid, m.position, m.type, m.fk_menu";
  54. $sql.= " FROM ".MAIN_DB_PREFIX."menu as m";
  55. $sql.= " WHERE m.rowid = ".$_GET["menuId"];
  56. dol_syslog("admin/menus/index.php ".$sql);
  57. $result = $db->query($sql);
  58. $num = $db->num_rows($result);
  59. $i = 0;
  60. while($i < $num)
  61. {
  62. $obj = $db->fetch_object($result);
  63. $current['rowid'] = $obj->rowid;
  64. $current['order'] = $obj->position;
  65. $current['type'] = $obj->type;
  66. $current['fk_menu'] = $obj->fk_menu;
  67. $i++;
  68. }
  69. // Menu before
  70. $sql = "SELECT m.rowid, m.position";
  71. $sql.= " FROM ".MAIN_DB_PREFIX."menu as m";
  72. $sql.= " WHERE (m.position < ".($current['order'])." OR (m.position = ".($current['order'])." AND rowid < ".$_GET["menuId"]."))";
  73. $sql.= " AND m.menu_handler='".$menu_handler."'";
  74. $sql.= " AND m.entity = ".$conf->entity;
  75. $sql.= " AND m.type = '".$current['type']."'";
  76. $sql.= " AND m.fk_menu = '".$current['fk_menu']."'";
  77. $sql.= " ORDER BY m.position, m.rowid";
  78. dol_syslog("admin/menus/index.php ".$sql);
  79. $result = $db->query($sql);
  80. $num = $db->num_rows($result);
  81. $i = 0;
  82. while($i < $num)
  83. {
  84. $obj = $db->fetch_object($result);
  85. $previous['rowid'] = $obj->rowid;
  86. $previous['order'] = $obj->position;
  87. $i++;
  88. }
  89. $sql = "UPDATE ".MAIN_DB_PREFIX."menu as m";
  90. $sql.= " SET m.position = ".$previous['order'];
  91. $sql.= " WHERE m.rowid = ".$current['rowid']; // Up the selected entry
  92. dol_syslog("admin/menus/index.php ".$sql);
  93. $db->query($sql);
  94. $sql = "UPDATE ".MAIN_DB_PREFIX."menu as m";
  95. $sql.= " SET m.position = ".($current['order']!=$previous['order']?$current['order']:$current['order']+1);
  96. $sql.= " WHERE m.rowid = ".$previous['rowid']; // Descend celui du dessus
  97. dol_syslog("admin/menus/index.php ".$sql);
  98. $db->query($sql);
  99. }
  100. if (isset($_GET["action"]) && $_GET["action"] == 'down')
  101. {
  102. $current=array();
  103. $next=array();
  104. // Get current position
  105. $sql = "SELECT m.rowid, m.position, m.type, m.fk_menu";
  106. $sql.= " FROM ".MAIN_DB_PREFIX."menu as m";
  107. $sql.= " WHERE m.rowid = ".$_GET["menuId"];
  108. dol_syslog("admin/menus/index.php ".$sql);
  109. $result = $db->query($sql);
  110. $num = $db->num_rows($result);
  111. $i = 0;
  112. while($i < $num)
  113. {
  114. $obj = $db->fetch_object($result);
  115. $current['rowid'] = $obj->rowid;
  116. $current['order'] = $obj->position;
  117. $current['type'] = $obj->type;
  118. $current['fk_menu'] = $obj->fk_menu;
  119. $i++;
  120. }
  121. // Menu after
  122. $sql = "SELECT m.rowid, m.position";
  123. $sql.= " FROM ".MAIN_DB_PREFIX."menu as m";
  124. $sql.= " WHERE (m.position > ".($current['order'])." OR (m.position = ".($current['order'])." AND rowid > ".$_GET["menuId"]."))";
  125. $sql.= " AND m.menu_handler='".$menu_handler."'";
  126. $sql.= " AND m.entity = ".$conf->entity;
  127. $sql.= " AND m.type = '".$current['type']."'";
  128. $sql.= " AND m.fk_menu = '".$current['fk_menu']."'";
  129. $sql.= " ORDER BY m.position, m.rowid";
  130. dol_syslog("admin/menus/index.php ".$sql);
  131. $result = $db->query($sql);
  132. $num = $db->num_rows($result);
  133. $i = 0;
  134. while($i < $num)
  135. {
  136. $obj = $db->fetch_object($result);
  137. $next['rowid'] = $obj->rowid;
  138. $next['order'] = $obj->position;
  139. $i++;
  140. }
  141. $sql = "UPDATE ".MAIN_DB_PREFIX."menu as m";
  142. $sql.= " SET m.position = ".($current['order']!=$next['order']?$next['order']:$current['order']+1); // Down the selected entry
  143. $sql.= " WHERE m.rowid = ".$current['rowid'];
  144. dol_syslog("admin/menus/index.php ".$sql);
  145. $db->query($sql);
  146. $sql = "UPDATE ".MAIN_DB_PREFIX."menu as m"; // Up the next entry
  147. $sql.= " SET m.position = ".$current['order'];
  148. $sql.= " WHERE m.rowid = ".$next['rowid'];
  149. dol_syslog("admin/menus/index.php ".$sql);
  150. $db->query($sql);
  151. }
  152. if ($_POST["action"] == 'confirm_delete' && $_POST["confirm"] == 'yes')
  153. {
  154. $db->begin();
  155. $sql = "DELETE FROM ".MAIN_DB_PREFIX."menu";
  156. $sql.= " WHERE rowid = ".$_GET['menuId'];
  157. $resql=$db->query($sql);
  158. if ($resql)
  159. {
  160. $db->commit();
  161. Header("Location: ".DOL_URL_ROOT.'/admin/menus/index.php?menu_handler='.$menu_handler.'&mesg='.urlencode($langs->trans("MenuDeleted")));
  162. exit ;
  163. }
  164. else
  165. {
  166. $db->rollback();
  167. $reload = 0;
  168. $_GET["action"]='';
  169. }
  170. }
  171. /*
  172. * View
  173. */
  174. $html=new Form($db);
  175. $htmladmin=new FormAdmin($db);
  176. $arrayofjs=array('/admin/menus/menu.js.php?lang='.$langs->defaultlang);
  177. llxHeader('',$langs->trans("Menus"),'','',0,0,$arrayofjs);
  178. print_fiche_titre($langs->trans("Menus"),'','setup');
  179. print $langs->trans("MenusEditorDesc")."<br>\n";
  180. print "<br>\n";
  181. if ($mesg) print '<div class="ok">'.$mesg.'.</div><br>';
  182. $h = 0;
  183. $head[$h][0] = DOL_URL_ROOT."/admin/menus.php";
  184. $head[$h][1] = $langs->trans("MenuHandlers");
  185. $head[$h][2] = 'handler';
  186. $h++;
  187. $head[$h][0] = DOL_URL_ROOT."/admin/menus/index.php";
  188. $head[$h][1] = $langs->trans("MenuAdmin");
  189. $head[$h][2] = 'editor';
  190. $h++;
  191. $head[$h][0] = DOL_URL_ROOT."/admin/menus/other.php";
  192. $head[$h][1] = $langs->trans("Miscellanous");
  193. $head[$h][2] = 'misc';
  194. $h++;
  195. dol_fiche_head($head, 'editor', $langs->trans("Menus"));
  196. // Confirmation for remove menu entry
  197. if ($_GET["action"] == 'delete')
  198. {
  199. $sql = "SELECT m.titre";
  200. $sql.= " FROM ".MAIN_DB_PREFIX."menu as m";
  201. $sql.= " WHERE m.rowid = ".$_GET['menuId'];
  202. $result = $db->query($sql);
  203. $obj = $db->fetch_object($result);
  204. $ret=$html->form_confirm("index.php?menu_handler=".$menu_handler."&menuId=".$_GET['menuId'],$langs->trans("DeleteMenu"),$langs->trans("ConfirmDeleteMenu",$obj->titre),"confirm_delete");
  205. if ($ret == 'html') print '<br>';
  206. }
  207. print '<form name="newmenu" class="nocellnopadd" action="'.$_SERVER["PHP_SELF"].'">';
  208. print '<input type="hidden" action="change_menu_handler">';
  209. print $langs->trans("MenuHandler").': ';
  210. print $htmladmin->select_menu_families($menu_handler,'menu_handler',$dirmenu);
  211. print ' &nbsp; <input type="submit" class="button" value="'.$langs->trans("Refresh").'">';
  212. print '</form>';
  213. print '<br>';
  214. print '<table class="border" width="100%">';
  215. print '<tr class="liste_titre">';
  216. print '<td>'.$langs->trans("TreeMenuPersonalized").'</td>';
  217. print '</tr>';
  218. print '<tr>';
  219. print '<td>';
  220. // ARBORESCENCE
  221. $rangLast = 0;
  222. $idLast = -1;
  223. if ($conf->use_javascript_ajax)
  224. {
  225. /*-------------------- MAIN -----------------------
  226. tableau des elements de l'arbre:
  227. c'est un tableau a 2 dimensions.
  228. Une ligne represente un element : data[$x]
  229. chaque ligne est decomposee en 3 donnees:
  230. - l'index de l'élément
  231. - l'index de l'élément parent
  232. - la chaine a afficher
  233. ie: data[]= array (index, index parent, chaine )
  234. */
  235. //il faut d'abord declarer un element racine de l'arbre
  236. $data[] = array(0,-1,"racine");
  237. //puis tous les elements enfants
  238. $sql = "SELECT m.rowid, m.fk_menu, m.titre, m.langs";
  239. $sql.= " FROM ".MAIN_DB_PREFIX."menu as m";
  240. $sql.= " WHERE menu_handler = '".$menu_handler."'";
  241. $sql.= " AND entity = ".$conf->entity;
  242. $sql.= " AND fk_menu >= 0";
  243. $sql.= " ORDER BY m.position, m.rowid"; // Order is position then rowid (because we need a sort criteria when position is same)
  244. $res = $db->query($sql);
  245. if ($res)
  246. {
  247. $num = $db->num_rows($res);
  248. $i = 1;
  249. while ($menu = $db->fetch_array ($res))
  250. {
  251. if (! empty($menu['langs'])) $langs->load($menu['langs']);
  252. $titre = $langs->trans($menu['titre']);
  253. $data[] = array($menu['rowid'],$menu['fk_menu'],$titre);
  254. $i++;
  255. }
  256. }
  257. // Appelle de la fonction recursive (ammorce)
  258. // avec recherche depuis la racine.
  259. // array($menu['rowid'],$menu['fk_menu'],$titre);
  260. tree_recur($data,0,0);
  261. print '</td>';
  262. print '</tr>';
  263. print '</table>';
  264. print '</div>';
  265. /*
  266. * Boutons actions
  267. */
  268. print '<div class="tabsAction">';
  269. print '<a class="butAction" href="'.DOL_URL_ROOT.'/admin/menus/edit.php?menuId=0&amp;action=create&amp;menu_handler='.urlencode($menu_handler).'">'.$langs->trans("NewMenu").'</a>';
  270. print '</div>';
  271. }
  272. else
  273. {
  274. $langs->load("errors");
  275. print '<div class="error">'.$langs->trans("ErrorFeatureNeedJavascript").'</div>';
  276. }
  277. $db->close();
  278. print '<br>';
  279. llxFooter();