9bb6c6d44067ea95fd0266eaf517d423a0b407ae
[fa-stable.git] / admin / tags.php
1 <?php
2 /**********************************************************************
3     Copyright (C) FrontAccounting, LLC.
4         Released under the terms of the GNU General Public License, GPL, 
5         as published by the Free Software Foundation, either version 3 
6         of the License, or (at your option) any later version.
7     This program is distributed in the hope that it will be useful,
8     but WITHOUT ANY WARRANTY; without even the implied warranty of
9     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
10     See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
12 $path_to_root = "..";
13 include_once($path_to_root . "/includes/types.inc"); // For tag constants
14
15 // Set up page security based on what type of tags we're working with
16 if ($_GET['type'] == "account" || $_POST['type'] == TAG_ACCOUNT) {
17         $page_security = 'SA_GLACCOUNTTAGS';
18 } else if($_GET['type'] == "dimension" || $_POST['type'] == TAG_DIMENSION) {
19         $page_security = 'SA_DIMTAGS';
20 }
21
22 include($path_to_root . "/includes/session.inc");  // Define session before using $_SESSION
23
24 if (!isset($page_security)) {
25   // If GET & POST don't have the info, try getting it from the session.
26   $page_security = $_SESSION['tags_page_security'];
27 }
28 $_SESSION['tags_page_security'] = $page_security;
29
30 // We use $_POST['type'] throughout this script, so convert $_GET vars
31 // if $_POST['type'] is not set.
32 if (!isset($_POST['type'])) {
33         if ($_GET['type'] == "account")
34                 $_POST['type'] = TAG_ACCOUNT;
35         elseif ($_GET['type'] == "dimension")
36                 $_POST['type'] = TAG_DIMENSION;
37         else
38                 die(_("Unspecified tag type"));
39 }
40
41 // Set up page based on what type of tags we're working with
42 // TODO: Set $_SESSION['sel_app'] to be relevant app (see /includes/page/header.inc::page_header()
43 switch ($_POST['type']) {
44         case TAG_ACCOUNT:
45                 // Account tags
46                 $_SESSION['page_title'] = _("Account Tags");
47                 break;
48         case TAG_DIMENSION:
49                 // Dimension tags
50                 $_SESSION['page_title'] = _("Dimension Tags");
51 }
52
53 page($_SESSION['page_title']);
54 include_once($path_to_root . "/admin/db/tags_db.inc");
55
56 include($path_to_root . "/includes/ui.inc");
57
58 simple_page_mode(true);
59
60 //-----------------------------------------------------------------------------------
61
62 function can_process() 
63 {
64         if (strlen($_POST['name']) == 0) 
65         {
66                 display_error( _("The tag name cannot be empty."));
67                 set_focus('name');
68                 return false;
69         }
70         return true;
71 }
72
73 //-----------------------------------------------------------------------------------
74
75 if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM') 
76 {
77         if (can_process()) 
78         {
79         if ($selected_id != -1) 
80         {
81                 if( $ret = update_tag($selected_id, $_POST['name'], $_POST['description']))
82                                 display_notification(_('Selected tag settings have been updated'));
83         } 
84         else 
85         {
86                 if( $ret = add_tag($_POST['type'], $_POST['name'], $_POST['description']))
87                                 display_notification(_('New tag has been added'));
88         }
89                 if ($ret) $Mode = 'RESET';
90         }
91 }
92
93 //-----------------------------------------------------------------------------------
94
95 function can_delete($selected_id)
96 {
97         if ($selected_id == -1)
98                 return false;
99         $result = get_records_associated_with_tag($selected_id);
100         
101         if (db_num_rows($result) > 0)   
102         {
103                 display_error(_("Cannot delete this tag because records have been created referring to it."));
104                 return false;
105         }
106
107         return true;
108 }
109
110
111 //-----------------------------------------------------------------------------------
112
113 if ($Mode == 'Delete')
114 {
115         if (can_delete($selected_id))
116         {
117                 delete_tag($selected_id);
118                 display_notification(_('Selected tag has been deleted'));
119         }
120         $Mode = 'RESET';
121 }
122
123 //-----------------------------------------------------------------------------------
124
125 if ($Mode == 'RESET')
126 {
127         $selected_id = -1;
128         $_POST['name'] = $_POST['description'] = '';
129 }
130
131 //-----------------------------------------------------------------------------------
132
133 $result = get_tags($_POST['type'], check_value('show_inactive'));
134
135 start_form();
136 start_table($table_style);
137 $th = array(_("Tag Name"), _("Tag Description"), "", "");
138 inactive_control_column($th);
139 table_header($th);
140
141 $k = 0;
142 while ($myrow = db_fetch($result)) 
143 {
144         alt_table_row_color($k);
145
146         label_cell($myrow['name']);
147         label_cell($myrow['description']);
148         inactive_control_cell($myrow["id"], $myrow["inactive"], 'tags', 'id');
149         edit_button_cell("Edit".$myrow["id"], _("Edit"));
150         delete_button_cell("Delete".$myrow["id"], _("Delete"));
151         end_row();
152 }
153
154 inactive_control_row($th);
155 end_table(1);
156
157 //-----------------------------------------------------------------------------------
158
159 start_table($table_style2);
160
161 if ($selected_id != -1) // We've selected a tag 
162 {
163         if ($Mode == 'Edit') {
164                 // Editing an existing tag
165                 $myrow = get_tag($selected_id);
166         
167                 $_POST['name'] = $myrow["name"];
168                 $_POST['description'] = $myrow["description"];
169         }
170         // Note the selected tag
171         hidden('selected_id', $selected_id);
172 }
173         
174 text_row_ex(_("Tag Name:"), 'name', 15, 30);
175 text_row_ex(_("Tag Description:"), 'description', 40, 60);
176 hidden('type');
177
178 end_table(1);
179
180 submit_add_or_update_center($selected_id == -1, '', 'both');
181
182 end_form();
183
184 //------------------------------------------------------------------------------------
185
186 end_page();
187
188 ?>