index.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. <?php
  2. /* Copyright (C) 2007 Patrick Raguin <patrick.raguin@gmail.com>
  3. * Copyright (C) 2007-2012 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2009-2012 Regis Houssin <regis.houssin@inodbox.com>
  5. * Copyright (C) 2019 Frédéric France <frederic.france@netlogic.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. */
  20. /**
  21. * \file htdocs/admin/menus/index.php
  22. * \ingroup core
  23. * \brief Index page for menu editor
  24. */
  25. require '../../main.inc.php';
  26. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formadmin.class.php';
  27. require_once DOL_DOCUMENT_ROOT.'/core/lib/treeview.lib.php';
  28. // Load translation files required by the page
  29. $langs->loadLangs(array("other", "admin"));
  30. if (!$user->admin) accessforbidden();
  31. $dirstandard = array();
  32. $dirsmartphone = array();
  33. $dirmenus = array_merge(array("/core/menus/"), (array) $conf->modules_parts['menus']);
  34. foreach ($dirmenus as $dirmenu)
  35. {
  36. $dirstandard[] = $dirmenu.'standard';
  37. $dirsmartphone[] = $dirmenu.'smartphone';
  38. }
  39. $action = GETPOST('action', 'alpha');
  40. $confirm = GETPOST('confirm', 'alpha');
  41. $menu_handler_top = $conf->global->MAIN_MENU_STANDARD;
  42. $menu_handler_smartphone = $conf->global->MAIN_MENU_SMARTPHONE;
  43. $menu_handler_top = preg_replace('/(_backoffice\.php|_menu\.php)/i', '', $menu_handler_top);
  44. $menu_handler_top = preg_replace('/(_frontoffice\.php|_menu\.php)/i', '', $menu_handler_top);
  45. $menu_handler_smartphone = preg_replace('/(_backoffice\.php|_menu\.php)/i', '', $menu_handler_smartphone);
  46. $menu_handler_smartphone = preg_replace('/(_frontoffice\.php|_menu\.php)/i', '', $menu_handler_smartphone);
  47. $menu_handler = $menu_handler_top;
  48. if (GETPOST("handler_origine")) $menu_handler = GETPOST("handler_origine");
  49. if (GETPOST("menu_handler")) $menu_handler = GETPOST("menu_handler");
  50. $menu_handler_to_search = preg_replace('/(_backoffice|_frontoffice|_menu)?(\.php)?/i', '', $menu_handler);
  51. /*
  52. * Actions
  53. */
  54. if ($action == 'up')
  55. {
  56. $current = array();
  57. $previous = array();
  58. // Get current position
  59. $sql = "SELECT m.rowid, m.position, m.type, m.fk_menu";
  60. $sql .= " FROM ".MAIN_DB_PREFIX."menu as m";
  61. $sql .= " WHERE m.rowid = ".GETPOST("menuId", "int");
  62. dol_syslog("admin/menus/index.php ".$sql);
  63. $result = $db->query($sql);
  64. $num = $db->num_rows($result);
  65. $i = 0;
  66. while ($i < $num)
  67. {
  68. $obj = $db->fetch_object($result);
  69. $current['rowid'] = $obj->rowid;
  70. $current['order'] = $obj->position;
  71. $current['type'] = $obj->type;
  72. $current['fk_menu'] = $obj->fk_menu;
  73. $i++;
  74. }
  75. // Menu before
  76. $sql = "SELECT m.rowid, m.position";
  77. $sql .= " FROM ".MAIN_DB_PREFIX."menu as m";
  78. $sql .= " WHERE (m.position < ".($current['order'])." OR (m.position = ".($current['order'])." AND rowid < ".GETPOST("menuId", "int")."))";
  79. $sql .= " AND m.menu_handler='".$db->escape($menu_handler_to_search)."'";
  80. $sql .= " AND m.entity = ".$conf->entity;
  81. $sql .= " AND m.type = '".$db->escape($current['type'])."'";
  82. $sql .= " AND m.fk_menu = '".$db->escape($current['fk_menu'])."'";
  83. $sql .= " ORDER BY m.position, m.rowid";
  84. dol_syslog("admin/menus/index.php ".$sql);
  85. $result = $db->query($sql);
  86. $num = $db->num_rows($result);
  87. $i = 0;
  88. while ($i < $num)
  89. {
  90. $obj = $db->fetch_object($result);
  91. $previous['rowid'] = $obj->rowid;
  92. $previous['order'] = $obj->position;
  93. $i++;
  94. }
  95. $sql = "UPDATE ".MAIN_DB_PREFIX."menu as m";
  96. $sql .= " SET m.position = ".$previous['order'];
  97. $sql .= " WHERE m.rowid = ".$current['rowid']; // Up the selected entry
  98. dol_syslog("admin/menus/index.php ".$sql);
  99. $db->query($sql);
  100. $sql = "UPDATE ".MAIN_DB_PREFIX."menu as m";
  101. $sql .= " SET m.position = ".($current['order'] != $previous['order'] ? $current['order'] : $current['order'] + 1);
  102. $sql .= " WHERE m.rowid = ".$previous['rowid']; // Descend celui du dessus
  103. dol_syslog("admin/menus/index.php ".$sql);
  104. $db->query($sql);
  105. }
  106. elseif ($action == 'down')
  107. {
  108. $current = array();
  109. $next = array();
  110. // Get current position
  111. $sql = "SELECT m.rowid, m.position, m.type, m.fk_menu";
  112. $sql .= " FROM ".MAIN_DB_PREFIX."menu as m";
  113. $sql .= " WHERE m.rowid = ".GETPOST("menuId", "int");
  114. dol_syslog("admin/menus/index.php ".$sql);
  115. $result = $db->query($sql);
  116. $num = $db->num_rows($result);
  117. $i = 0;
  118. while ($i < $num)
  119. {
  120. $obj = $db->fetch_object($result);
  121. $current['rowid'] = $obj->rowid;
  122. $current['order'] = $obj->position;
  123. $current['type'] = $obj->type;
  124. $current['fk_menu'] = $obj->fk_menu;
  125. $i++;
  126. }
  127. // Menu after
  128. $sql = "SELECT m.rowid, m.position";
  129. $sql .= " FROM ".MAIN_DB_PREFIX."menu as m";
  130. $sql .= " WHERE (m.position > ".($current['order'])." OR (m.position = ".($current['order'])." AND rowid > ".GETPOST("menuId", "int")."))";
  131. $sql .= " AND m.menu_handler='".$db->escape($menu_handler_to_search)."'";
  132. $sql .= " AND m.entity = ".$conf->entity;
  133. $sql .= " AND m.type = '".$db->escape($current['type'])."'";
  134. $sql .= " AND m.fk_menu = '".$db->escape($current['fk_menu'])."'";
  135. $sql .= " ORDER BY m.position, m.rowid";
  136. dol_syslog("admin/menus/index.php ".$sql);
  137. $result = $db->query($sql);
  138. $num = $db->num_rows($result);
  139. $i = 0;
  140. while ($i < $num)
  141. {
  142. $obj = $db->fetch_object($result);
  143. $next['rowid'] = $obj->rowid;
  144. $next['order'] = $obj->position;
  145. $i++;
  146. }
  147. $sql = "UPDATE ".MAIN_DB_PREFIX."menu as m";
  148. $sql .= " SET m.position = ".($current['order'] != $next['order'] ? $next['order'] : $current['order'] + 1); // Down the selected entry
  149. $sql .= " WHERE m.rowid = ".$current['rowid'];
  150. dol_syslog("admin/menus/index.php ".$sql);
  151. $db->query($sql);
  152. $sql = "UPDATE ".MAIN_DB_PREFIX."menu as m"; // Up the next entry
  153. $sql .= " SET m.position = ".$current['order'];
  154. $sql .= " WHERE m.rowid = ".$next['rowid'];
  155. dol_syslog("admin/menus/index.php ".$sql);
  156. $db->query($sql);
  157. }
  158. elseif ($action == 'confirm_delete' && $confirm == 'yes')
  159. {
  160. $db->begin();
  161. $sql = "DELETE FROM ".MAIN_DB_PREFIX."menu";
  162. $sql .= " WHERE rowid = ".GETPOST('menuId', 'int');
  163. $resql = $db->query($sql);
  164. if ($resql)
  165. {
  166. $db->commit();
  167. setEventMessages($langs->trans("MenuDeleted"), null, 'mesgs');
  168. header("Location: ".DOL_URL_ROOT.'/admin/menus/index.php?menu_handler='.$menu_handler);
  169. exit;
  170. }
  171. else
  172. {
  173. $db->rollback();
  174. $reload = 0;
  175. $action = '';
  176. }
  177. }
  178. /*
  179. * View
  180. */
  181. $form = new Form($db);
  182. $formadmin = new FormAdmin($db);
  183. $arrayofjs = array('/includes/jquery/plugins/jquerytreeview/jquery.treeview.js', '/includes/jquery/plugins/jquerytreeview/lib/jquery.cookie.js');
  184. $arrayofcss = array('/includes/jquery/plugins/jquerytreeview/jquery.treeview.css');
  185. llxHeader('', $langs->trans("Menus"), '', '', 0, 0, $arrayofjs, $arrayofcss);
  186. print load_fiche_titre($langs->trans("Menus"), '', 'title_setup');
  187. $h = 0;
  188. $head[$h][0] = DOL_URL_ROOT."/admin/menus.php";
  189. $head[$h][1] = $langs->trans("MenuHandlers");
  190. $head[$h][2] = 'handler';
  191. $h++;
  192. $head[$h][0] = DOL_URL_ROOT."/admin/menus/index.php";
  193. $head[$h][1] = $langs->trans("MenuAdmin");
  194. $head[$h][2] = 'editor';
  195. $h++;
  196. $head[$h][0] = DOL_URL_ROOT."/admin/menus/other.php";
  197. $head[$h][1] = $langs->trans("Miscellaneous");
  198. $head[$h][2] = 'misc';
  199. $h++;
  200. dol_fiche_head($head, 'editor', $langs->trans("Menus"), -1);
  201. print '<span class="opacitymedium">'.$langs->trans("MenusEditorDesc")."</span><br>\n";
  202. print "<br>\n";
  203. // Confirmation for remove menu entry
  204. if ($action == 'delete')
  205. {
  206. $sql = "SELECT m.titre as title";
  207. $sql .= " FROM ".MAIN_DB_PREFIX."menu as m";
  208. $sql .= " WHERE m.rowid = ".GETPOST('menuId', 'int');
  209. $result = $db->query($sql);
  210. $obj = $db->fetch_object($result);
  211. print $form->formconfirm("index.php?menu_handler=".$menu_handler."&menuId=".GETPOST('menuId', 'int'), $langs->trans("DeleteMenu"), $langs->trans("ConfirmDeleteMenu", $obj->title), "confirm_delete");
  212. }
  213. $newcardbutton = '';
  214. if ($user->admin)
  215. {
  216. $newcardbutton .= dolGetButtonTitle($langs->trans('New'), '', 'fa fa-plus-circle', DOL_URL_ROOT.'/admin/menus/edit.php?menuId=0&action=create&menu_handler='.urlencode($menu_handler).'&backtopage='.urlencode($_SERVER['PHP_SELF']));
  217. }
  218. print '<form name="newmenu" class="nocellnopadd" action="'.$_SERVER["PHP_SELF"].'">';
  219. print '<input type="hidden" action="change_menu_handler">';
  220. print $langs->trans("MenuHandler").': ';
  221. $formadmin->select_menu_families($menu_handler.(preg_match('/_menu/', $menu_handler) ? '' : '_menu'), 'menu_handler', array_merge($dirstandard, $dirsmartphone));
  222. print ' &nbsp; <input type="submit" class="button" value="'.$langs->trans("Refresh").'">';
  223. print '<div class="floatright">';
  224. print $newcardbutton;
  225. print '</div>';
  226. print '</form>';
  227. print '<br>';
  228. print '<table class="noborder centpercent">';
  229. print '<tr class="liste_titre">';
  230. print '<td>'.$langs->trans("TreeMenuPersonalized").'</td>';
  231. print '<td class="right"><div id="iddivjstreecontrol"><a href="#">'.img_picto('', 'object_category').' '.$langs->trans("UndoExpandAll").'</a>';
  232. print ' | <a href="#">'.img_picto('', 'object_category-expanded').' '.$langs->trans("ExpandAll").'</a></div></td>';
  233. print '</tr>';
  234. print '<tr>';
  235. print '<td colspan="2">';
  236. // ARBORESCENCE
  237. $rangLast = 0;
  238. $idLast = -1;
  239. if ($conf->use_javascript_ajax)
  240. {
  241. /*-------------------- MAIN -----------------------
  242. tableau des elements de l'arbre:
  243. c'est un tableau a 2 dimensions.
  244. Une ligne represente un element : data[$x]
  245. chaque ligne est decomposee en 3 donnees:
  246. - l'index de l'élément
  247. - l'index de l'élément parent
  248. - la chaine a afficher
  249. ie: data[]= array (index, index parent, chaine )
  250. */
  251. //il faut d'abord declarer un element racine de l'arbre
  252. $data[] = array('rowid'=>0, 'fk_menu'=>-1, 'title'=>"racine", 'mainmenu'=>'', 'leftmenu'=>'', 'fk_mainmenu'=>'', 'fk_leftmenu'=>'');
  253. //puis tous les elements enfants
  254. $sql = "SELECT m.rowid, m.titre, m.langs, m.mainmenu, m.leftmenu, m.fk_menu, m.fk_mainmenu, m.fk_leftmenu, m.position, m.module";
  255. $sql .= " FROM ".MAIN_DB_PREFIX."menu as m";
  256. $sql .= " WHERE menu_handler = '".$db->escape($menu_handler_to_search)."'";
  257. $sql .= " AND entity = ".$conf->entity;
  258. //$sql.= " AND fk_menu >= 0";
  259. $sql .= " ORDER BY m.position, m.rowid"; // Order is position then rowid (because we need a sort criteria when position is same)
  260. $res = $db->query($sql);
  261. if ($res)
  262. {
  263. $num = $db->num_rows($res);
  264. $i = 1;
  265. while ($menu = $db->fetch_array($res))
  266. {
  267. if (!empty($menu['langs'])) $langs->load($menu['langs']);
  268. $titre = $langs->trans($menu['titre']);
  269. $data[] = array(
  270. 'rowid'=>$menu['rowid'],
  271. 'module'=>$menu['module'],
  272. 'fk_menu'=>$menu['fk_menu'],
  273. 'title'=>$titre,
  274. 'mainmenu'=>$menu['mainmenu'],
  275. 'leftmenu'=>$menu['leftmenu'],
  276. 'fk_mainmenu'=>$menu['fk_mainmenu'],
  277. 'fk_leftmenu'=>$menu['fk_leftmenu'],
  278. 'position'=>$menu['position'],
  279. 'entry'=>'<table class="nobordernopadding centpercent"><tr><td>'.
  280. '<strong> &nbsp; <a href="edit.php?menu_handler='.$menu_handler_to_search.'&action=edit&menuId='.$menu['rowid'].'">'.$titre.'</a></strong>'.
  281. '</td><td class="right">'.
  282. '<a href="edit.php?menu_handler='.$menu_handler_to_search.'&action=edit&menuId='.$menu['rowid'].'">'.img_edit('default', 0, 'class="menuEdit" id="edit'.$menu['rowid'].'"').'</a> '.
  283. '<a href="edit.php?menu_handler='.$menu_handler_to_search.'&action=create&menuId='.$menu['rowid'].'">'.img_edit_add('default').'</a> '.
  284. '<a href="index.php?menu_handler='.$menu_handler_to_search.'&action=delete&menuId='.$menu['rowid'].'">'.img_delete('default').'</a> '.
  285. '&nbsp; &nbsp; &nbsp;'.
  286. '<a href="index.php?menu_handler='.$menu_handler_to_search.'&action=up&menuId='.$menu['rowid'].'">'.img_picto("Up", "1uparrow").'</a><a href="index.php?menu_handler='.$menu_handler_to_search.'&action=down&menuId='.$menu['rowid'].'">'.img_picto("Down", "1downarrow").'</a>'.
  287. '</td></tr></table>',
  288. 'buttons'=>'<a href="edit.php?menu_handler='.$menu_handler_to_search.'&action=edit&menuId='.$menu['rowid'].'">'.img_edit('default', 0, 'class="menuEdit" id="edit'.$menu['rowid'].'"').'</a> '.
  289. '<a href="edit.php?menu_handler='.$menu_handler_to_search.'&action=create&menuId='.$menu['rowid'].'">'.img_edit_add('default').'</a> '.
  290. '<a href="index.php?menu_handler='.$menu_handler_to_search.'&action=delete&menuId='.$menu['rowid'].'">'.img_delete('default').'</a> '.
  291. '&nbsp; &nbsp; &nbsp;'.
  292. '<a href="index.php?menu_handler='.$menu_handler_to_search.'&action=up&menuId='.$menu['rowid'].'">'.img_picto("Up", "1uparrow").'</a><a href="index.php?menu_handler='.$menu_handler_to_search.'&action=down&menuId='.$menu['rowid'].'">'.img_picto("Down", "1downarrow").'</a>'
  293. );
  294. $i++;
  295. }
  296. }
  297. global $tree_recur_alreadyadded; // This var was def into tree_recur
  298. //var_dump($data);
  299. // Appelle de la fonction recursive (ammorce) avec recherche depuis la racine.
  300. //tree_recur($data, $data[0], 0, 'iddivjstree', 0, 1); // use this to get info on name and foreign keys of menu entry
  301. tree_recur($data, $data[0], 0, 'iddivjstree', 0, 0); // $data[0] is virtual record 'racine'
  302. print '</td>';
  303. print '</tr>';
  304. print '</table>';
  305. // Process remaining records (records that are not linked to root by any path)
  306. $remainingdata = array();
  307. foreach ($data as $datar)
  308. {
  309. if (empty($datar['rowid']) || $tree_recur_alreadyadded[$datar['rowid']]) continue;
  310. $remainingdata[] = $datar;
  311. }
  312. if (count($remainingdata))
  313. {
  314. print '<table class="noborder centpercent">';
  315. print '<tr class="liste_titre">';
  316. print '<td>'.$langs->trans("NotTopTreeMenuPersonalized").'</td>';
  317. print '<td class="right"></td>';
  318. print '</tr>';
  319. print '<tr>';
  320. print '<td colspan="2">';
  321. foreach ($remainingdata as $datar)
  322. {
  323. $father = array('rowid'=>$datar['rowid'], 'title'=>"???", 'mainmenu'=>$datar['fk_mainmenu'], 'leftmenu'=>$datar['fk_leftmenu'], 'fk_mainmenu'=>'', 'fk_leftmenu'=>'');
  324. //print 'Start with rowid='.$datar['rowid'].' mainmenu='.$father ['mainmenu'].' leftmenu='.$father ['leftmenu'].'<br>'."\n";
  325. tree_recur($data, $father, 0, 'iddivjstree'.$datar['rowid'], 1, 1);
  326. }
  327. print '</td>';
  328. print '</tr>';
  329. print '</table>';
  330. }
  331. print '</div>';
  332. }
  333. else
  334. {
  335. $langs->load("errors");
  336. setEventMessages($langs->trans("ErrorFeatureNeedJavascript"), null, 'errors');
  337. }
  338. print '<br>';
  339. // End of page
  340. llxFooter();
  341. $db->close();