16a328be0c1140dfc28ea0fb81faf0a183bc3e03
[mailer.git] / inc / fix_menu.php
1 <?php
2 /************************************************************************
3  * Mailer v0.2.1-FINAL                                Start: 10/22/2009 *
4  * ===================                          Last change: 10/22/2009 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : fix_menu.php                                     *
8  * -------------------------------------------------------------------- *
9  * Short description : Fixes dublicate entries in filters table         *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Repariert doppelte Eintraege in filters-Tabelle  *
12  * -------------------------------------------------------------------- *
13  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
14  * Copyright (c) 2009 - 2016 by Mailer Developer Team                   *
15  * For more information visit: http://mxchange.org                      *
16  *                                                                      *
17  * This program is free software; you can redistribute it and/or modify *
18  * it under the terms of the GNU General Public License as published by *
19  * the Free Software Foundation; either version 2 of the License, or    *
20  * (at your option) any later version.                                  *
21  *                                                                      *
22  * This program is distributed in the hope that it will be useful,      *
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
25  * GNU General Public License for more details.                         *
26  *                                                                      *
27  * You should have received a copy of the GNU General Public License    *
28  * along with this program; if not, write to the Free Software          *
29  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
30  * MA  02110-1301  USA                                                  *
31  ************************************************************************/
32
33 // Some security stuff...
34 if (!defined('__SECURITY')) {
35         die();
36 } // END - if
37
38 // Get all menus
39 foreach (array('guest','member','admin') as $menu) {
40         // Query it
41         $result = sqlQueryEscaped("SELECT `action`, `what` FROM `{?_MYSQL_PREFIX?}_%s_menu` ORDER BY `action` ASC,`what` ASC",
42                 array($menu), __FILE__, __LINE__);
43
44         // Init array
45         $menus = array();
46
47         // Load all entries
48         while ($entry = sqlFetchArray($result)) {
49                 // Is the menu entry set?
50                 if (!isset($menus['menu'][$entry['action']][$entry['what']])) {
51                         // Not found
52                         $menus['menu'][$entry['action']][$entry['what']] = 1;
53                 } else {
54                         // Double entry, so get count
55                         if (is_null($entry['what'])) {
56                                 // Main menu
57                                 $entries = countSumTotalData($entry['action'], $menu . '_menu', 'action', 'action', TRUE, ' AND `what` IS NULL');
58                         } else {
59                                 // Sub menu
60                                 $entries = countSumTotalData($entry['action'], $menu . '_menu', 'action', 'action', TRUE, sprintf(" AND `what`='%s'", $entry['what']));
61                         }
62                         //* DEBUG: */ debugOutput('menu='.$menu.',action='.$entry['action'].',what='.$entry['what'].',entries='.$entries);
63
64                         // Only remove if we have at least 2 entries from same menu
65                         if ($entries > 1) {
66                                 // Remove all except one
67                                 if (is_null($entry['what'])) {
68                                         // Main menu is double
69                                         sqlQueryEscaped("DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_%s_menu` WHERE `action`='%s' AND `what` IS NULL LIMIT %s",
70                                                 array(
71                                                         $menu,
72                                                         $entry['action'],
73                                                         ($entries - 1)
74                                                 ), __FILE__, __LINE__);
75                                 } else {
76                                         // Sub menu is double
77                                         sqlQueryEscaped("DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_%s_menu` WHERE `action`='%s' AND `what`='%s' LIMIT %s",
78                                                 array(
79                                                         $menu,
80                                                         $entry['action'],
81                                                         $entry['what'],
82                                                         ($entries - 1)
83                                                 ), __FILE__, __LINE__);
84                                 }
85                         } // END - if
86                 }
87         } // END - while
88
89         // Free result
90         sqlFreeResult($result);
91
92         // Init sqls
93         initSqls();
94
95         // Now insert our unqiue key for action-what combination
96         addSql(sprintf('ALTER TABLE `{?_MYSQL_PREFIX?}_%s_menu` DROP INDEX `action_what`', $menu));
97         addSql(sprintf('ALTER TABLE `{?_MYSQL_PREFIX?}_%s_menu` ADD UNIQUE INDEX `action_what` (`action`, `what`)', $menu));
98
99         // Now insert our unqiue key for what
100         addSql(sprintf('ALTER TABLE `{?_MYSQL_PREFIX?}_%s_menu` DROP INDEX `what`', $menu));
101         addSql(sprintf('ALTER TABLE `{?_MYSQL_PREFIX?}_%s_menu` ADD UNIQUE INDEX `what` (`what`)', $menu));
102
103         // And run all
104         runFilterChain('run_sqls');
105 } // END - foreach
106
107 // [EOF]
108 ?>