index.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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', 'aZ09');
  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. } elseif ($action == 'down')
  106. {
  107. $current = array();
  108. $next = array();
  109. // Get current position
  110. $sql = "SELECT m.rowid, m.position, m.type, m.fk_menu";
  111. $sql .= " FROM ".MAIN_DB_PREFIX."menu as m";
  112. $sql .= " WHERE m.rowid = ".GETPOST("menuId", "int");
  113. dol_syslog("admin/menus/index.php ".$sql);
  114. $result = $db->query($sql);
  115. $num = $db->num_rows($result);
  116. $i = 0;
  117. while ($i < $num)
  118. {
  119. $obj = $db->fetch_object($result);
  120. $current['rowid'] = $obj->rowid;
  121. $current['order'] = $obj->position;
  122. $current['type'] = $obj->type;
  123. $current['fk_menu'] = $obj->fk_menu;
  124. $i++;
  125. }
  126. // Menu after
  127. $sql = "SELECT m.rowid, m.position";
  128. $sql .= " FROM ".MAIN_DB_PREFIX."menu as m";
  129. $sql .= " WHERE (m.position > ".($current['order'])." OR (m.position = ".($current['order'])." AND rowid > ".GETPOST("menuId", "int")."))";
  130. $sql .= " AND m.menu_handler='".$db->escape($menu_handler_to_search)."'";
  131. $sql .= " AND m.entity = ".$conf->entity;
  132. $sql .= " AND m.type = '".$db->escape($current['type'])."'";
  133. $sql .= " AND m.fk_menu = '".$db->escape($current['fk_menu'])."'";
  134. $sql .= " ORDER BY m.position, m.rowid";
  135. dol_syslog("admin/menus/index.php ".$sql);
  136. $result = $db->query($sql);
  137. $num = $db->num_rows($result);
  138. $i = 0;
  139. while ($i < $num)
  140. {
  141. $obj = $db->fetch_object($result);
  142. $next['rowid'] = $obj->rowid;
  143. $next['order'] = $obj->position;
  144. $i++;
  145. }
  146. $sql = "UPDATE ".MAIN_DB_PREFIX."menu as m";
  147. $sql .= " SET m.position = ".($current['order'] != $next['order'] ? $next['order'] : $current['order'] + 1); // Down the selected entry
  148. $sql .= " WHERE m.rowid = ".$current['rowid'];
  149. dol_syslog("admin/menus/index.php ".$sql);
  150. $db->query($sql);
  151. $sql = "UPDATE ".MAIN_DB_PREFIX."menu as m"; // Up the next entry
  152. $sql .= " SET m.position = ".$current['order'];
  153. $sql .= " WHERE m.rowid = ".$next['rowid'];
  154. dol_syslog("admin/menus/index.php ".$sql);
  155. $db->query($sql);
  156. } elseif ($action == 'confirm_delete' && $confirm == 'yes')
  157. {
  158. $db->begin();
  159. $sql = "DELETE FROM ".MAIN_DB_PREFIX."menu";
  160. $sql .= " WHERE rowid = ".GETPOST('menuId', 'int');
  161. $resql = $db->query($sql);
  162. if ($resql)
  163. {
  164. $db->commit();
  165. setEventMessages($langs->trans("MenuDeleted"), null, 'mesgs');
  166. header("Location: ".DOL_URL_ROOT.'/admin/menus/index.php?menu_handler='.$menu_handler);
  167. exit;
  168. } else {
  169. $db->rollback();
  170. $reload = 0;
  171. $action = '';
  172. }
  173. }
  174. /*
  175. * View
  176. */
  177. $form = new Form($db);
  178. $formadmin = new FormAdmin($db);
  179. $arrayofjs = array('/includes/jquery/plugins/jquerytreeview/jquery.treeview.js', '/includes/jquery/plugins/jquerytreeview/lib/jquery.cookie.js');
  180. $arrayofcss = array('/includes/jquery/plugins/jquerytreeview/jquery.treeview.css');
  181. llxHeader('', $langs->trans("Menus"), '', '', 0, 0, $arrayofjs, $arrayofcss);
  182. print load_fiche_titre($langs->trans("Menus"), '', 'title_setup');
  183. $h = 0;
  184. $head[$h][0] = DOL_URL_ROOT."/admin/menus.php";
  185. $head[$h][1] = $langs->trans("MenuHandlers");
  186. $head[$h][2] = 'handler';
  187. $h++;
  188. $head[$h][0] = DOL_URL_ROOT."/admin/menus/index.php";
  189. $head[$h][1] = $langs->trans("MenuAdmin");
  190. $head[$h][2] = 'editor';
  191. $h++;
  192. $head[$h][0] = DOL_URL_ROOT."/admin/menus/other.php";
  193. $head[$h][1] = $langs->trans("Miscellaneous");
  194. $head[$h][2] = 'misc';
  195. $h++;
  196. dol_fiche_head($head, 'editor', '', -1);
  197. print '<span class="opacitymedium">'.$langs->trans("MenusEditorDesc")."</span><br>\n";
  198. print "<br>\n";
  199. // Confirmation for remove menu entry
  200. if ($action == 'delete')
  201. {
  202. $sql = "SELECT m.titre as title";
  203. $sql .= " FROM ".MAIN_DB_PREFIX."menu as m";
  204. $sql .= " WHERE m.rowid = ".GETPOST('menuId', 'int');
  205. $result = $db->query($sql);
  206. $obj = $db->fetch_object($result);
  207. print $form->formconfirm("index.php?menu_handler=".$menu_handler."&menuId=".GETPOST('menuId', 'int'), $langs->trans("DeleteMenu"), $langs->trans("ConfirmDeleteMenu", $obj->title), "confirm_delete");
  208. }
  209. $newcardbutton = '';
  210. if ($user->admin)
  211. {
  212. $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']));
  213. }
  214. print '<form name="newmenu" class="nocellnopadd" action="'.$_SERVER["PHP_SELF"].'">';
  215. print '<input type="hidden" action="change_menu_handler">';
  216. print $langs->trans("MenuHandler").': ';
  217. $formadmin->select_menu_families($menu_handler.(preg_match('/_menu/', $menu_handler) ? '' : '_menu'), 'menu_handler', array_merge($dirstandard, $dirsmartphone));
  218. print ' &nbsp; <input type="submit" class="button" value="'.$langs->trans("Refresh").'">';
  219. print '<div class="floatright">';
  220. print $newcardbutton;
  221. print '</div>';
  222. print '</form>';
  223. print '<br>';
  224. print '<table class="noborder centpercent">';
  225. print '<tr class="liste_titre">';
  226. print '<td>'.$langs->trans("TreeMenuPersonalized").'</td>';
  227. print '<td class="right"><div id="iddivjstreecontrol"><a href="#">'.img_picto('', 'folder', 'class="paddingright"').$langs->trans("UndoExpandAll").'</a>';
  228. print ' | <a href="#">'.img_picto('', 'folder-open', 'class="paddingright"').$langs->trans("ExpandAll").'</a></div></td>';
  229. print '</tr>';
  230. print '<tr>';
  231. print '<td colspan="2">';
  232. // ARBORESCENCE
  233. $rangLast = 0;
  234. $idLast = -1;
  235. if ($conf->use_javascript_ajax)
  236. {
  237. /*-------------------- MAIN -----------------------
  238. tableau des elements de l'arbre:
  239. c'est un tableau a 2 dimensions.
  240. Une ligne represente un element : data[$x]
  241. chaque ligne est decomposee en 3 donnees:
  242. - l'index de l'élément
  243. - l'index de l'élément parent
  244. - la chaine a afficher
  245. ie: data[]= array (index, index parent, chaine )
  246. */
  247. //il faut d'abord declarer un element racine de l'arbre
  248. $data[] = array('rowid'=>0, 'fk_menu'=>-1, 'title'=>"racine", 'mainmenu'=>'', 'leftmenu'=>'', 'fk_mainmenu'=>'', 'fk_leftmenu'=>'');
  249. //puis tous les elements enfants
  250. $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";
  251. $sql .= " FROM ".MAIN_DB_PREFIX."menu as m";
  252. $sql .= " WHERE menu_handler = '".$db->escape($menu_handler_to_search)."'";
  253. $sql .= " AND entity = ".$conf->entity;
  254. //$sql.= " AND fk_menu >= 0";
  255. $sql .= " ORDER BY m.position, m.rowid"; // Order is position then rowid (because we need a sort criteria when position is same)
  256. $res = $db->query($sql);
  257. if ($res)
  258. {
  259. $num = $db->num_rows($res);
  260. $i = 1;
  261. while ($menu = $db->fetch_array($res))
  262. {
  263. if (!empty($menu['langs'])) $langs->load($menu['langs']);
  264. $titre = $langs->trans($menu['titre']);
  265. $entry = '<table class="nobordernopadding centpercent"><tr><td>';
  266. $entry .= '<strong> &nbsp; <a href="edit.php?menu_handler='.$menu_handler_to_search.'&action=edit&token='.newToken().'&menuId='.$menu['rowid'].'">'.$titre.'</a></strong>';
  267. $entry .= '</td><td class="right">';
  268. $entry .= '<a class="editfielda marginleftonly marginrightonly" href="edit.php?menu_handler='.$menu_handler_to_search.'&action=edit&token='.newToken().'&menuId='.$menu['rowid'].'">'.img_edit('default', 0, 'class="menuEdit" id="edit'.$menu['rowid'].'"').'</a> ';
  269. $entry .= '<a class="marginleftonly marginrightonly" href="edit.php?menu_handler='.$menu_handler_to_search.'&action=create&token='.newToken().'&menuId='.$menu['rowid'].'">'.img_edit_add('default').'</a> ';
  270. $entry .= '<a class="marginleftonly marginrightonly" href="index.php?menu_handler='.$menu_handler_to_search.'&action=delete&token='.newToken().'&menuId='.$menu['rowid'].'">'.img_delete('default').'</a> ';
  271. $entry .= '&nbsp; &nbsp; &nbsp;';
  272. $entry .= '<a class="marginleftonly marginrightonly" href="index.php?menu_handler='.$menu_handler_to_search.'&action=up&token='.newToken().'&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>';
  273. $entry .= '</td></tr></table>';
  274. $buttons = '<a class="editfielda marginleftonly marginrightonly" href="edit.php?menu_handler='.$menu_handler_to_search.'&action=edit&token='.newToken().'&menuId='.$menu['rowid'].'">'.img_edit('default', 0, 'class="menuEdit" id="edit'.$menu['rowid'].'"').'</a> ';
  275. $buttons .= '<a class="marginleftonly marginrightonly" href="edit.php?menu_handler='.$menu_handler_to_search.'&action=create&token='.newToken().'&menuId='.$menu['rowid'].'">'.img_edit_add('default').'</a> ';
  276. $buttons .= '<a class="marginleftonly marginrightonly" href="index.php?menu_handler='.$menu_handler_to_search.'&action=delete&token='.newToken().'&menuId='.$menu['rowid'].'">'.img_delete('default').'</a> ';
  277. $buttons .= '&nbsp; &nbsp; &nbsp;';
  278. $buttons .= '<a class="marginleftonly marginrightonly" href="index.php?menu_handler='.$menu_handler_to_search.'&action=up&token='.newToken().'&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>';
  279. $data[] = array(
  280. 'rowid'=>$menu['rowid'],
  281. 'module'=>$menu['module'],
  282. 'fk_menu'=>$menu['fk_menu'],
  283. 'title'=>$titre,
  284. 'mainmenu'=>$menu['mainmenu'],
  285. 'leftmenu'=>$menu['leftmenu'],
  286. 'fk_mainmenu'=>$menu['fk_mainmenu'],
  287. 'fk_leftmenu'=>$menu['fk_leftmenu'],
  288. 'position'=>$menu['position'],
  289. 'entry'=>$entry,
  290. 'buttons'=>$buttons
  291. );
  292. $i++;
  293. }
  294. }
  295. global $tree_recur_alreadyadded; // This var was def into tree_recur
  296. //var_dump($data);
  297. // Appelle de la fonction recursive (ammorce) avec recherche depuis la racine.
  298. //tree_recur($data, $data[0], 0, 'iddivjstree', 0, 1); // use this to get info on name and foreign keys of menu entry
  299. tree_recur($data, $data[0], 0, 'iddivjstree', 0, 0); // $data[0] is virtual record 'racine'
  300. print '</td>';
  301. print '</tr>';
  302. print '</table>';
  303. // Process remaining records (records that are not linked to root by any path)
  304. $remainingdata = array();
  305. foreach ($data as $datar)
  306. {
  307. if (empty($datar['rowid']) || $tree_recur_alreadyadded[$datar['rowid']]) continue;
  308. $remainingdata[] = $datar;
  309. }
  310. if (count($remainingdata))
  311. {
  312. print '<table class="noborder centpercent">';
  313. print '<tr class="liste_titre">';
  314. print '<td>'.$langs->trans("NotTopTreeMenuPersonalized").'</td>';
  315. print '<td class="right"></td>';
  316. print '</tr>';
  317. print '<tr>';
  318. print '<td colspan="2">';
  319. foreach ($remainingdata as $datar)
  320. {
  321. $father = array('rowid'=>$datar['rowid'], 'title'=>"???", 'mainmenu'=>$datar['fk_mainmenu'], 'leftmenu'=>$datar['fk_leftmenu'], 'fk_mainmenu'=>'', 'fk_leftmenu'=>'');
  322. //print 'Start with rowid='.$datar['rowid'].' mainmenu='.$father ['mainmenu'].' leftmenu='.$father ['leftmenu'].'<br>'."\n";
  323. tree_recur($data, $father, 0, 'iddivjstree'.$datar['rowid'], 1, 1);
  324. }
  325. print '</td>';
  326. print '</tr>';
  327. print '</table>';
  328. }
  329. print '</div>';
  330. } else {
  331. $langs->load("errors");
  332. setEventMessages($langs->trans("ErrorFeatureNeedJavascript"), null, 'errors');
  333. }
  334. print '<br>';
  335. // End of page
  336. llxFooter();
  337. $db->close();