1<?php
2
3// Pandora FMS - http://pandorafms.com
4// ==================================================
5// Copyright (c) 2005-2009 Artica Soluciones Tecnologicas
6// Please see http://pandorafms.org for full contribution list
7
8// This program is free software; you can redistribute it and/or
9// modify it under the terms of the GNU General Public License
10// as published by the Free Software Foundation for version 2.
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14// GNU General Public License for more details.
15
16check_login ();
17
18enterprise_hook('open_meta_frame');
19
20//Include functions code
21require_once ($config['homedir'].'/include/functions_categories.php');
22
23if (! check_acl ($config['id_user'], 0, "PM") && ! is_user_admin ($config['id_user'])) {
24	db_pandora_audit("ACL Violation", "Trying to access Edit Category");
25	require ("general/noaccess.php");
26
27	return;
28}
29
30// Get parameters
31$action = (string) get_parameter ("action", "");
32$id_category = (int) get_parameter ("id_category", 0);
33$update_category = (int) get_parameter ("update_category", 0);
34$create_category = (int) get_parameter ("create_category", 0);
35$name_category = (string) get_parameter ("name_category", "");
36$tab = (string) get_parameter ("tab", "list");
37
38if(defined('METACONSOLE')) {
39	$buttons = array(
40		'list' => array(
41			'active' => false,
42			'text' => '<a href="index.php?sec=advanced&sec2=godmode/category/category&tab=list&pure='.(int)$config['pure'].'">' .
43				html_print_image ("images/list.png", true, array ("title" => __('List categories'))) .'</a>'));
44}
45else {
46	$buttons = array(
47		'list' => array(
48			'active' => false,
49			'text' => '<a href="index.php?sec=gmodules&sec2=godmode/category/category&tab=list&pure='.(int)$config['pure'].'">' .
50				html_print_image ("images/list.png", true, array ("title" => __('List categories'))) .'</a>'));
51}
52
53$buttons[$tab]['active'] = false;
54
55// Header
56if (defined('METACONSOLE')) {
57	ui_meta_print_header(__('Categories configuration'), __('Editor'), $buttons);
58}
59else {
60	ui_print_page_header (__('Categories configuration'), "images/gm_modules.png", false, "", true, $buttons);
61}
62
63
64// Two actions can performed in this page: update and create categories
65// Update category: update an existing category
66if ($update_category && $id_category != 0) {
67	$values = array();
68	$values['name'] = $name_category;
69
70	$result = false;
71	if ($values['name'] != '')
72		$result = db_process_sql_update('tcategory', $values, array('id' => $id_category));
73
74	if ($result === false) {
75		db_pandora_audit("Category management", "Fail try to update category #$id_category");
76		ui_print_error_message(__('Error updating category'));
77	}
78	else {
79		db_pandora_audit("Category management", "Update category #$id_category");
80		ui_print_success_message(__('Successfully updated category'));
81	}
82}
83
84// Create category: creates a new category
85if ($create_category) {
86
87	$return_create = true;
88
89	$values = array();
90	$values['name'] = $name_category;
91
92	// DB insert
93	$return_create = false;
94	if ($values['name'] != '')
95		$return_create = db_process_sql_insert('tcategory', $values);
96
97	if ($return_create === false) {
98		db_pandora_audit("Category management", "Fail try to create category");
99		ui_print_error_message(__('Error creating category'));
100		$action = "new";
101	// If create action ends successfully then current action is update
102	}
103	else {
104		db_pandora_audit("Category management", "Create category #$return_create");
105		ui_print_success_message(__('Successfully created category'));
106		$id_category = $return_create;
107		$action = "update";
108	}
109}
110
111// Form fields are filled here
112// Get results when update action is performed
113if ($action == "update" && $id_category != 0) {
114	$result_category = db_get_row_filter('tcategory', array('id' => $id_category));
115	$name_category = $result_category["name"];
116} // If current action is create (new) or somethig goes wrong fields are filled with void value
117else {
118	$name_category = "";
119}
120
121
122// Create/Update category form
123echo '<form method="post" action="index.php?sec=gmodules&sec2=godmode/category/edit_category&action=' . $action . '&id_category=' . $id_category . '&pure='.(int)$config['pure'].'" enctype="multipart/form-data">';
124
125if(!defined('METACONSOLE'))
126	echo '<div align=left style="width: 100%" class="pandora_form">';
127else
128	echo '<div align=left style="width: 100%" class="pandora_form">';
129
130echo "<table border=0 cellpadding=4 cellspacing=4 class='databox filters' width=100%>";
131
132	if (defined("METACONSOLE")) {
133		if ($action == "update") {
134			echo "<thead>
135					<tr>
136						<th align=center colspan=5>" .
137						 __('Update category') .
138						 "</th>
139					</tr>
140				</thead>";
141		}
142		if ($action == "new") {
143			echo "<thead>
144					<tr>
145						<th align=center colspan=5>" .
146						 __('Create category') .
147						 "</th>
148					</tr>
149				</thead>";
150		}
151	}
152	echo "<tr>";
153		echo "<td style='font-weight: bold'>";
154
155		html_print_label (__("Name"),'name');
156		echo "</td>";
157		echo "<td>";
158		html_print_input_text ('name_category', $name_category);
159		echo "</td>";
160	echo "</tr>";
161
162echo "</table>";
163
164echo "<table border=0 cellpadding=0 cellspacing=0 class='' width=100%>";
165echo "<tr>";
166echo "<td align=right>";
167if ($action == "update") {
168	html_print_input_hidden ('update_category', 1);
169	html_print_submit_button (__('Update'), 'update_button', false,
170		'class="sub next"');
171}
172if ($action == "new") {
173	html_print_input_hidden ('create_category', 1);
174	html_print_submit_button (__('Create'), 'create_button', false,
175		'class="sub next"');
176}
177echo "</td>";
178echo "</tr>";
179echo "</table>";
180
181echo '</div>';
182echo '</form>';
183
184enterprise_hook('close_meta_frame');
185
186?>
187