1<?php
2/*
3	FusionPBX
4	Version: MPL 1.1
5
6	The contents of this file are subject to the Mozilla Public License Version
7	1.1 (the "License"); you may not use this file except in compliance with
8	the License. You may obtain a copy of the License at
9	http://www.mozilla.org/MPL/
10
11	Software distributed under the License is distributed on an "AS IS" basis,
12	WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13	for the specific language governing rights and limitations under the
14	License.
15
16	The Original Code is FusionPBX
17
18	The Initial Developer of the Original Code is
19	Mark J Crane <markjcrane@fusionpbx.com>
20	Portions created by the Initial Developer are Copyright (C) 2008-2015
21	the Initial Developer. All Rights Reserved.
22
23	Contributor(s):
24	Mark J Crane <markjcrane@fusionpbx.com>
25*/
26require_once "root.php";
27require_once "resources/require.php";
28require_once "resources/check_auth.php";
29if (permission_exists('menu_add') || permission_exists('menu_edit')) {
30	//access granted
31}
32else {
33	echo "access denied";
34	exit;
35}
36
37//add multi-lingual support
38	$language = new text;
39	$text = $language->get();
40
41//action add or update
42	if (isset($_REQUEST["id"])) {
43		$action = "update";
44		$menu_uuid = check_str($_REQUEST["id"]);
45	}
46	else {
47		$action = "add";
48	}
49
50//get http post variables and set them to php variables
51	if (count($_POST)>0) {
52		$menu_uuid = check_str($_POST["menu_uuid"]);
53		$menu_name = check_str($_POST["menu_name"]);
54		$menu_language = check_str($_POST["menu_language"]);
55		$menu_description = check_str($_POST["menu_description"]);
56	}
57
58if (count($_POST)>0 && strlen($_POST["persistformvar"]) == 0) {
59
60	$msg = '';
61	if ($action == "update") {
62		$menu_uuid = check_str($_POST["menu_uuid"]);
63	}
64
65	//check for all required data
66		//if (strlen($menu_name) == 0) { $msg .= $text['message-required'].$text['label-name']."<br>\n"; }
67		//if (strlen($menu_language) == 0) { $msg .= $text['message-required'].$text['label-language']."<br>\n"; }
68		//if (strlen($menu_description) == 0) { $msg .= $text['message-required'].$text['label-description']."<br>\n"; }
69		if (strlen($msg) > 0 && strlen($_POST["persistformvar"]) == 0) {
70			require_once "resources/header.php";
71			require_once "resources/persist_form_var.php";
72			echo "<div align='center'>\n";
73			echo "<table><tr><td>\n";
74			echo $msg."<br />";
75			echo "</td></tr></table>\n";
76			persistformvar($_POST);
77			echo "</div>\n";
78			require_once "resources/footer.php";
79			return;
80		}
81
82	//add or update the database
83	if ($_POST["persistformvar"] != "true") {
84		if ($action == "add") {
85			//create a new unique id
86				$menu_uuid = uuid();
87
88			//start a new menu
89				$sql = "insert into v_menus ";
90				$sql .= "(";
91				$sql .= "menu_uuid, ";
92				$sql .= "menu_name, ";
93				$sql .= "menu_language, ";
94				$sql .= "menu_description ";
95				$sql .= ")";
96				$sql .= "values ";
97				$sql .= "(";
98				$sql .= "'".$menu_uuid."', ";
99				$sql .= "'".$menu_name."', ";
100				$sql .= "'".$menu_language."', ";
101				$sql .= "'".$menu_description."' ";
102				$sql .= ")";
103				$db->exec(check_sql($sql));
104				unset($sql);
105
106			//add the default items in the menu
107				require_once "resources/classes/menu.php";
108				$menu = new menu;
109				$menu->db = $db;
110				$menu->menu_uuid = $menu_uuid;
111				$menu->menu_language = $menu_language;
112				$menu->restore();
113
114			//redirect the user back to the main menu
115				messages::add($text['message-add']);
116				header("Location: menu.php");
117				return;
118		} //if ($action == "add")
119
120		if ($action == "update") {
121			//update the menu
122				$sql = "update v_menus set ";
123				$sql .= "menu_name = '".$menu_name."', ";
124				$sql .= "menu_language = '".$menu_language."', ";
125				$sql .= "menu_description = '".$menu_description."' ";
126				$sql .= "where menu_uuid = '".$menu_uuid."'";
127				$db->exec(check_sql($sql));
128				unset($sql);
129
130			//redirect the user back to the main menu
131				messages::add($text['message-update']);
132				header("Location: menu.php");
133				return;
134		} //if ($action == "update")
135	} //if ($_POST["persistformvar"] != "true")
136} //(count($_POST)>0 && strlen($_POST["persistformvar"]) == 0)
137
138//pre-populate the form
139	if (count($_GET)>0 && $_POST["persistformvar"] != "true") {
140		$menu_uuid = $_GET["id"];
141		$sql = "select * from v_menus ";
142		$sql .= "where menu_uuid = '$menu_uuid' ";
143		$prep_statement = $db->prepare(check_sql($sql));
144		$prep_statement->execute();
145		$result = $prep_statement->fetchAll(PDO::FETCH_NAMED);
146		foreach ($result as &$row) {
147			$menu_uuid = $row["menu_uuid"];
148			$menu_name = $row["menu_name"];
149			$menu_language = $row["menu_language"];
150			$menu_description = $row["menu_description"];
151			break; //limit to 1 row
152		}
153		unset ($prep_statement);
154	}
155
156//show the header
157	require_once "resources/header.php";
158	if ($action == "update") {
159		$document['title'] = $text['title-menu-edit'];
160	}
161	if ($action == "add") {
162		$document['title'] = $text['title-menu-add'];
163	}
164
165//show the content
166	echo "<form method='post' name='frm' action=''>\n";
167	echo "<table width='100%' border='0' cellpadding='0' cellspacing='0'>\n";
168	echo "<tr>\n";
169	if ($action == "add") {
170		echo "<td align='left' width='30%' nowrap='nowrap'><b>".$text['header-menu-add']."</b></td>\n";
171	}
172	if ($action == "update") {
173		echo "<td align='left' width='30%' nowrap='nowrap'><b>".$text['header-menu-edit']."</b></td>\n";
174	}
175	echo "<td width='70%' align='right'>\n";
176	echo "	<input type='button' class='btn' name='' alt='".$text['button-back']."' onclick=\"window.location='menu.php'\" value='".$text['button-back']."'>\n";
177	if (permission_exists('menu_restore') && $action == "update") {
178		echo "	<input type='button' class='btn' value='".$text['button-restore_default']."' onclick=\"document.location.href='menu_restore_default.php?menu_uuid=$menu_uuid&menu_language=$menu_language';\" />";
179	}
180	echo "	<input type='submit' name='submit' class='btn' value='".$text['button-save']."'>\n";
181	echo "</td>";
182	echo "</tr>\n";
183	echo "<tr>\n";
184	echo "<td align='left' colspan='2'>\n";
185	if ($action == "update") {
186		echo $text['description-menu-edit'];
187	}
188	if ($action == "add") {
189		echo $text['description-menu-add'];
190	}
191	echo "<br /><br />\n";
192	echo "</td>\n";
193	echo "</tr>\n";
194
195	echo "<tr>\n";
196	echo "<td class='vncellreq' valign='top' align='left' nowrap='nowrap'>\n";
197	echo "	".$text['label-name']."\n";
198	echo "</td>\n";
199	echo "<td class='vtable' align='left'>\n";
200	echo "	<input class='formfld' type='text' name='menu_name' maxlength='255' value=\"$menu_name\">\n";
201	echo "<br />\n";
202	echo "\n";
203	echo $text['description-name']."</td>\n";
204	echo "</tr>\n";
205
206	echo "<tr>\n";
207	echo "<td class='vncellreq' valign='top' align='left' nowrap='nowrap'>\n";
208	echo "	".$text['label-language']."\n";
209	echo "</td>\n";
210	echo "<td class='vtable' align='left'>\n";
211	echo "	<input class='formfld' type='text' name='menu_language' maxlength='255' value=\"$menu_language\">\n";
212	echo "<br />\n";
213	echo $text['description-language']."\n";
214	echo "</td>\n";
215	echo "</tr>\n";
216
217	echo "<tr>\n";
218	echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
219	echo "	".$text['label-description']."\n";
220	echo "</td>\n";
221	echo "<td class='vtable' align='left'>\n";
222	echo "	<input class='formfld' type='text' name='menu_description' maxlength='255' value=\"$menu_description\">\n";
223	echo "<br />\n";
224	echo $text['description-description']."\n";
225	echo "</td>\n";
226	echo "</tr>\n";
227	echo "	<tr>\n";
228	echo "		<td colspan='2' align='right'>\n";
229	if ($action == "update") {
230		echo "		<input type='hidden' name='menu_uuid' value='$menu_uuid'>\n";
231	}
232	echo "			<br>";
233	echo "			<input type='submit' name='submit' class='btn' value='".$text['button-save']."'>\n";
234	echo "		</td>\n";
235	echo "	</tr>";
236	echo "</table>";
237	echo "<br><br>";
238	echo "</form>";
239
240//show the menu items
241	require_once "core/menu/menu_item_list.php";
242
243//include the footer
244	require_once "resources/footer.php";
245?>
246