All *_list() functions now does not display html directly - added echo when needed.
[fa-stable.git] / admin / print_profiles.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 $page_security = 'SA_PRINTPROFILE';
13 $path_to_root = "..";
14 include($path_to_root . "/includes/session.inc");
15 include($path_to_root . "/admin/db/printers_db.inc");
16 include($path_to_root . "/includes/ui.inc");
17
18 page(_($help_context = "Printing Profiles"));
19
20 $selected_id = get_post('profile_id','');
21
22 //-------------------------------------------------------------------------------------------------
23 // Returns array of defined reports
24 //
25 function get_reports() {
26         global $path_to_root, $comp_path, $go_debug;
27
28 if ($go_debug || !isset($_SESSION['reports'])) {        
29         // to save time, store in session.
30                 $paths = array (
31                         $path_to_root.'/reporting/',
32                         $comp_path .'/'. user_company() . '/reporting/');
33                 $reports = array( '' => _('Default printing destination'));
34
35         foreach($paths as $dirno => $path) {
36                 $repdir = opendir($path);
37                 while(false !== ($fname = readdir($repdir)))
38                 {
39                 // reports have filenames in form rep(repid).php 
40                 // where repid must contain at least one digit (reports_main.php is not ;)
41                         if (is_file($path.$fname) 
42 //                              && preg_match('/.*[^0-9]([0-9]+)[.]php/', $fname, $match))
43                                 && preg_match('/rep(.*[0-9]+.*)[.]php/', $fname, $match))
44                         {
45                                 $repno = $match[1];
46                                 $title = '';
47
48                                 $line = file_get_contents($path.$fname);
49                                 if (preg_match('/.*(FrontReport\()\s*_\([\'"]([^\'"]*)/', $line, $match)) {
50                                         $title = trim($match[2]);
51                                 }
52                                 else // for any 3rd party printouts without FrontReport() class use
53                                         if (preg_match('/.*(\$Title).*[\'"](.*)[\'"].+/', $line, $match)) {
54                                                 $title = trim($match[2]);
55                                         }
56                                 $reports[$repno] = $title;
57                         }
58                 }
59         closedir();
60         }
61                 ksort($reports);
62                 $_SESSION['reports'] = $reports;
63         }
64         return $_SESSION['reports'];
65 }
66
67 function clear_form() 
68 {
69         global $selected_id, $Ajax;
70
71         $selected_id = '';
72         $_POST['name'] = '';
73         $Ajax->activate('_page_body');
74 }
75
76 function check_delete($name)
77 {
78 // check if selected profile is used by any user
79         if ($name=='') return 0; // cannot delete system default profile
80         $sql = "SELECT * FROM ".TB_PREF."users WHERE print_profile=".db_escape($name);
81         $res = db_query($sql,'cannot check printing profile usage');
82         return db_num_rows($res);
83 }
84 //-------------------------------------------------------------------------------------------
85 if ( get_post('submit'))
86 {
87
88         $error = 0;
89
90         if ($_POST['profile_id'] == '' && empty($_POST['name']))
91         {
92                 $error = 1;
93                 display_error( _("Printing profile name cannot be empty."));
94                 set_focus('name');
95         } 
96
97         if (!$error)
98         {
99                 $prof = array('' => get_post('Prn')); // store default value/profile name
100                 foreach (get_reports() as $rep => $descr) {
101                         $val = get_post('Prn'.$rep);
102                         $prof[$rep] = $val;
103                 }
104                 if ($_POST['profile_id']=='')
105                 $_POST['profile_id'] = get_post('name');
106                 
107                 update_printer_profile($_POST['profile_id'], $prof);
108                 if ($selected_id == '') {
109                         display_notification_centered(_('New printing profile has been created')); 
110                         clear_form();
111                 } else {
112                         display_notification_centered(_('Printing profile has been updated'));
113                 }
114         }
115 }
116
117 if(get_post('delete'))
118 {
119  if (!check_delete(get_post('name'))) {
120         delete_printer_profile($selected_id);
121         display_notification(_('Selected printing profile has been deleted'));
122         clear_form();
123  }
124 }
125
126 if(get_post('_profile_id_update')) {
127         $Ajax->activate('_page_body');
128 }
129
130 start_form();
131 start_table();
132 print_profiles_list_row(_('Select printing profile'). ':', 'profile_id', null,
133         _('New printing profile'), true);
134 end_table();
135 echo '<hr>';
136 start_table();
137 if (get_post('profile_id') == '')
138         text_row(_("Printing Profile Name").':', 'name', null, 30, 30);
139 else
140         label_cells(_("Printing Profile Name").':', get_post('profile_id'));
141 end_table(1);
142
143 $result = get_print_profile(get_post('profile_id'));
144 $prints = array();
145 while ($myrow = db_fetch($result)) {
146         $prints[$myrow['report']] = $myrow['printer'];
147 }
148
149 start_table($table_style);
150 $th = array(_("Report Id"), _("Description"), _("Printer"));
151 table_header($th);
152
153 $k = 0;
154 $unkn = 0;
155 foreach(get_reports() as $rep => $descr)
156 {
157         alt_table_row_color($k);
158
159     label_cell($rep=='' ? '-' : $rep, 'align=center');
160     label_cell($descr == '' ? '???<sup>1)</sup>' : _($descr));
161         $_POST['Prn'.$rep] = isset($prints[$rep]) ? $prints[$rep] : '';
162     echo '<td>';
163         echo printers_list('Prn'.$rep, null, 
164                 $rep == '' ? _('Browser support') : _('Default'));
165         echo '</td>';
166         if ($descr == '') $unkn = 1;
167     end_row();
168 }
169 end_table();
170 if ($unkn)
171         display_note('<sup>1)</sup>&nbsp;-&nbsp;'._("no title was found in this report definition file."), 0, 1, '');
172 else
173         echo '<br>';
174
175 div_start('controls');
176 if (get_post('profile_id') == '') {
177         submit_center('submit', _("Add New Profile"), true, '', 'default');
178 } else {
179         submit_center_first('submit', _("Update Profile"), 
180           _('Update printer profile'), 'default');
181         submit_center_last('delete', _("Delete Profile"), 
182           _('Delete printer profile (only if not used by any user)'), true);
183 }
184 div_end();
185
186 end_form();
187 end_page();
188
189 ?>