Feature 5388: Print Invoices (documents) list gets too long. Fixed by default 180...
[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, $SysPrefs;
27
28         if ($SysPrefs->go_debug || !isset($_SESSION['reports'])) {      
29         // to save time, store in session.
30                 $paths = array (
31                         $path_to_root.'/reporting/',
32                         company_path(). '/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('/rep(.*[0-9]+.*)[.]php/', $fname, $match))
43                                 {
44                                         $repno = $match[1];
45                                         $title = '';
46
47                                         $line = file_get_contents($path.$fname);
48                                         if (preg_match('/.*(FrontReport\()\s*_\([\'"]([^\'"]*)/', $line, $match)) {
49                                                 $title = trim($match[2]);
50                                         }
51                                         else // for any 3rd party printouts without FrontReport() class use
52                                         if (preg_match('/.*(\$Title).*[\'"](.*)[\'"].+/', $line, $match)) {
53                                                 $title = trim($match[2]);
54                                         }
55                                         $reports[$repno] = $title;
56                                 }
57                         }
58                         closedir();
59                 }
60                 ksort($reports);
61                 $_SESSION['reports'] = $reports;
62         }
63         return $_SESSION['reports'];
64 }
65
66 function clear_form() 
67 {
68         global $selected_id, $Ajax;
69
70         $selected_id = '';
71         $_POST['name'] = '';
72         $Ajax->activate('_page_body');
73 }
74
75 function check_delete($name)
76 {
77         // check if selected profile is used by any user
78         if ($name=='') return 0; // cannot delete system default profile
79         return key_in_foreign_table($name, 'users', 'print_profile');
80 }
81 //-------------------------------------------------------------------------------------------
82 if ( get_post('submit'))
83 {
84
85         $error = 0;
86
87         if ($_POST['profile_id'] == '' && empty($_POST['name']))
88         {
89                 $error = 1;
90                 display_error( _("Printing profile name cannot be empty."));
91                 set_focus('name');
92         } 
93
94         if (!$error)
95         {
96                 $prof = array('' => get_post('Prn')); // store default value/profile name
97                 foreach (get_reports() as $rep => $descr) {
98                         $val = get_post('Prn'.$rep);
99                         $prof[$rep] = $val;
100                 }
101                 if ($_POST['profile_id']=='')
102                         $_POST['profile_id'] = get_post('name');
103                 
104                 update_printer_profile($_POST['profile_id'], $prof);
105                 if ($selected_id == '') {
106                         display_notification_centered(_('New printing profile has been created')); 
107                         clear_form($selected_id);
108                 } else {
109                         display_notification_centered(_('Printing profile has been updated'));
110                 }
111         }
112 }
113
114 if(get_post('delete'))
115 {
116         if (!check_delete(get_post('name'))) {
117                 delete_printer_profile($selected_id);
118                 display_notification(_('Selected printing profile has been deleted'));
119                 clear_form();
120         }
121 }
122
123 if(get_post('_profile_id_update')) {
124         $Ajax->activate('_page_body');
125 }
126
127 start_form();
128 start_table();
129 print_profiles_list_row(_('Select printing profile'). ':', 'profile_id', null,
130         _('New printing profile'), true);
131 end_table();
132 echo '<hr>';
133 start_table();
134 if (get_post('profile_id') == '')
135         text_row(_("Printing Profile Name").':', 'name', null, 30, 30);
136 else
137         label_cells(_("Printing Profile Name").':', get_post('profile_id'));
138 end_table(1);
139
140 $result = get_print_profile(get_post('profile_id'));
141 $prints = array();
142 while ($myrow = db_fetch($result)) {
143         $prints[$myrow['report']] = $myrow['printer'];
144 }
145
146 start_table(TABLESTYLE);
147 $th = array(_("Report Id"), _("Description"), _("Printer"));
148 table_header($th);
149
150 $k = 0;
151 $unkn = 0;
152 foreach(get_reports() as $rep => $descr)
153 {
154         alt_table_row_color($k);
155
156     label_cell($rep=='' ? '-' : $rep, 'align=center');
157     label_cell($descr == '' ? '???<sup>1)</sup>' : _($descr));
158         $_POST['Prn'.$rep] = isset($prints[$rep]) ? $prints[$rep] : '';
159     echo '<td>';
160         echo printers_list('Prn'.$rep, null, 
161                 $rep == '' ? _('Browser support') : _('Default'));
162         echo '</td>';
163         if ($descr == '') $unkn = 1;
164     end_row();
165 }
166 end_table();
167 if ($unkn)
168         display_note('<sup>1)</sup>&nbsp;-&nbsp;'._("no title was found in this report definition file."), 0, 1, '');
169 else
170         echo '<br>';
171
172 div_start('controls');
173 if (get_post('profile_id') == '') {
174         submit_center('submit', _("Add New Profile"), true, '', 'default');
175 } else {
176         submit_center_first('submit', _("Update Profile"), 
177           _('Update printer profile'), 'default');
178         submit_center_last('delete', _("Delete Profile"), 
179           _('Delete printer profile (only if not used by any user)'), true);
180 }
181 div_end();
182
183 end_form();
184 end_page();
185