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$page_security = 'SA_PRINTERS';
13$path_to_root="..";
14include($path_to_root . "/includes/session.inc");
15
16page(_($help_context = "Printer Locations"));
17
18include($path_to_root . "/admin/db/printers_db.inc");
19include($path_to_root . "/includes/ui.inc");
20
21simple_page_mode(true);
22//-------------------------------------------------------------------------------------------
23if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM')
24{
25
26	$error = 0;
27
28	if (empty($_POST['name']))
29	{
30		$error = 1;
31		display_error( _("Printer name cannot be empty."));
32		set_focus('name');
33	}
34	elseif (empty($_POST['host']))
35	{
36		display_notification_centered( _("You have selected printing to server at user IP."));
37	}
38	elseif (!check_num('tout', 0, 60))
39	{
40		$error = 1;
41		display_error( _("Timeout cannot be less than zero nor longer than 60 (sec)."));
42		set_focus('tout');
43	}
44
45	if ($error != 1)
46	{
47		write_printer_def($selected_id, get_post('name'), get_post('descr'),
48			get_post('queue'), get_post('host'), input_num('port',0),
49			input_num('tout',0));
50
51		display_notification_centered($selected_id==-1?
52			_('New printer definition has been created')
53			:_('Selected printer definition has been updated'));
54 		$Mode = 'RESET';
55	}
56}
57
58if ($Mode == 'Delete')
59{
60	// PREVENT DELETES IF DEPENDENT RECORDS IN print_profiles
61
62	if (key_in_foreign_table($selected_id, 'print_profiles', 'printer'))
63	{
64		display_error(_("Cannot delete this printer definition, because print profile have been created using it."));
65	}
66	else
67	{
68		delete_printer($selected_id);
69		display_notification(_('Selected printer definition has been deleted'));
70	}
71	$Mode = 'RESET';
72}
73
74if ($Mode == 'RESET')
75{
76	$selected_id = -1;
77	unset($_POST);
78}
79//-------------------------------------------------------------------------------------------------
80
81$result = get_all_printers();
82start_form();
83start_table(TABLESTYLE);
84$th = array(_("Name"), _("Description"), _("Host"), _("Printer Queue"),'','');
85table_header($th);
86
87$k = 0; //row colour counter
88while ($myrow = db_fetch($result))
89{
90	alt_table_row_color($k);
91
92    label_cell($myrow['name']);
93    label_cell($myrow['description']);
94    label_cell($myrow['host']);
95    label_cell($myrow['queue']);
96 	edit_button_cell("Edit".$myrow['id'], _("Edit"));
97 	delete_button_cell("Delete".$myrow['id'], _("Delete"));
98    end_row();
99
100
101} //END WHILE LIST LOOP
102
103end_table();
104end_form();
105echo '<br>';
106
107//-------------------------------------------------------------------------------------------------
108
109start_form();
110
111start_table(TABLESTYLE2);
112
113if ($selected_id != -1)
114{
115	if ($Mode == 'Edit') {
116		$myrow = get_printer($selected_id);
117		$_POST['name'] = $myrow['name'];
118		$_POST['descr'] = $myrow['description'];
119		$_POST['queue'] = $myrow['queue'];
120		$_POST['tout'] = $myrow['timeout'];
121		$_POST['host'] = $myrow['host'];
122		$_POST['port'] = $myrow['port'];
123	}
124	hidden('selected_id', $selected_id);
125} else {
126	if(!isset($_POST['host']))
127		$_POST['host'] = 'localhost';
128	if(!isset($_POST['port']))
129		$_POST['port'] = '515';
130}
131
132text_row(_("Printer Name").':', 'name', null, 20, 20);
133text_row(_("Printer Description").':', 'descr', null, 40, 60);
134text_row(_("Host name or IP").':', 'host', null, 30, 40);
135text_row(_("Port").':', 'port', null, 5, 5);
136text_row(_("Printer Queue").':', 'queue', null, 20, 20);
137text_row(_("Timeout").':', 'tout', null, 5, 5);
138
139end_table(1);
140
141submit_add_or_update_center($selected_id == -1, '', 'both');
142
143end_form();
144
145end_page();
146
147?>
148