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