index.php 15 KB

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