Implemented an Items tab for attachments and modified the existing setup attachments.
[fa-stable.git] / admin / attachments.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 $page_security = 'SA_ATTACHDOCUMENT';
14
15 include_once($path_to_root . "/includes/db_pager.inc");
16 include_once($path_to_root . "/includes/session.inc");
17
18 include_once($path_to_root . "/includes/date_functions.inc");
19 include_once($path_to_root . "/includes/ui.inc");
20 include_once($path_to_root . "/includes/data_checks.inc");
21 include_once($path_to_root . "/admin/db/attachments_db.inc");
22 include_once($path_to_root . "/admin/db/transactions_db.inc");
23 include_once($path_to_root . "/inventory/includes/db/items_db.inc");
24
25 if (isset($_GET['vw']))
26         $view_id = $_GET['vw'];
27 else
28         $view_id = find_submit('view');
29 if ($view_id != -1)
30 {
31         $row = get_attachment($view_id);
32         if ($row['filename'] != "")
33         {
34                 if(in_ajax()) {
35                         $Ajax->popup($_SERVER['PHP_SELF'].'?vw='.$view_id);
36                 } else {
37                         $type = ($row['filetype']) ? $row['filetype'] : 'application/octet-stream';     
38                 header("Content-type: ".$type);
39                 header('Content-Length: '.$row['filesize']);
40                         header("Content-Disposition: inline");
41                 echo file_get_contents(company_path(). "/attachments/".$row['unique_name']);
42                 exit();
43                 }
44         }       
45 }
46 if (isset($_GET['dl']))
47         $download_id = $_GET['dl'];
48 else
49         $download_id = find_submit('download');
50
51 if ($download_id != -1)
52 {
53         $row = get_attachment($download_id);
54         if ($row['filename'] != "")
55         {
56                 if(in_ajax()) {
57                         $Ajax->redirect($_SERVER['PHP_SELF'].'?dl='.$download_id);
58                 } else {
59                         $type = ($row['filetype']) ? $row['filetype'] : 'application/octet-stream';     
60                 header("Content-type: ".$type);
61                 header('Content-Length: '.$row['filesize']);
62                 header('Content-Disposition: attachment; filename="'.$row['filename'].'"');
63                 echo file_get_contents(company_path()."/attachments/".$row['unique_name']);
64                 exit();
65                 }
66         }       
67 }
68
69 $js = "";
70 if ($SysPrefs->use_popup_windows)
71         $js .= get_js_open_window(800, 500);
72 page(_($help_context = "Attach Documents"), false, false, "", $js);
73
74 simple_page_mode(true);
75 //----------------------------------------------------------------------------------------
76 if (isset($_GET['filterType'])) // catch up external links
77         $_POST['filterType'] = $_GET['filterType'];
78 if (isset($_GET['trans_no']))
79         $_POST['trans_no'] = $_GET['trans_no'];
80
81 if ($Mode == 'ADD_ITEM' || $Mode == 'UPDATE_ITEM')
82 {
83         
84         $filename = basename($_FILES['filename']['name']);
85         if ($_POST['filterType'] == ST_ITEM && $Mode == 'ADD_ITEM')
86                 $_POST['trans_no'] = get_item_code_id($_POST['trans_no']);
87         if (!transaction_exists($_POST['filterType'], $_POST['trans_no']))
88                 display_error(_("Selected transaction does not exists."));
89         elseif ($Mode == 'ADD_ITEM' && !in_array(strtoupper(substr($filename, strlen($filename) - 3)), array('JPG','PNG','GIF', 'PDF', 'DOC', 'ODT')))
90         {
91                 display_error(_('Only graphics,pdf,doc and odt files are supported.'));
92         } elseif ($Mode == 'ADD_ITEM' && !isset($_FILES['filename']))
93                 display_error(_("Select attachment file."));
94         elseif ($Mode == 'ADD_ITEM' && ($_FILES['filename']['error'] > 0)) {
95         if ($_FILES['filename']['error'] == UPLOAD_ERR_INI_SIZE) 
96                         display_error(_("The file size is over the maximum allowed."));
97         else
98                         display_error(_("Select attachment file."));
99         } elseif ( strlen($filename) > 60) {
100                 display_error(_("File name exceeds maximum of 60 chars. Please change filename and try again."));
101         } else {
102                 //$content = base64_encode(file_get_contents($_FILES['filename']['tmp_name']));
103                 $tmpname = $_FILES['filename']['tmp_name'];
104
105                 $dir =  company_path()."/attachments";
106                 if (!file_exists($dir))
107                 {
108                         mkdir ($dir,0777);
109                         $index_file = "<?php\nheader(\"Location: ../index.php\");\n";
110                         $fp = fopen($dir."/index.php", "w");
111                         fwrite($fp, $index_file);
112                         fclose($fp);
113                 }
114
115                 $filesize = $_FILES['filename']['size'];
116                 $filetype = $_FILES['filename']['type'];
117
118                 // file name compatible with POSIX
119                 // protect against directory traversal
120                 if ($Mode == 'UPDATE_ITEM')
121                 {
122                     $row = get_attachment($selected_id);
123                     if ($row['filename'] == "")
124                         exit();
125                         $unique_name = $row['unique_name'];
126                         if ($filename && file_exists($dir."/".$unique_name))
127                                 unlink($dir."/".$unique_name);
128                 }
129                 else
130                         $unique_name = random_id();
131
132                 //save the file
133                 move_uploaded_file($tmpname, $dir."/".$unique_name);
134
135                 if ($Mode == 'ADD_ITEM')
136                 {
137                         add_attachment($_POST['filterType'], $_POST['trans_no'], $_POST['description'],
138                                 $filename, $unique_name, $filesize, $filetype);
139                         display_notification(_("Attachment has been inserted.")); 
140                 }
141                 else
142                 {
143                         update_attachment($selected_id, $_POST['filterType'], $_POST['trans_no'], $_POST['description'],
144                                 $filename, $unique_name, $filesize, $filetype); 
145                         display_notification(_("Attachment has been updated.")); 
146                 }
147                 reset_form();
148         }
149         refresh_pager('trans_tbl');
150         $Ajax->activate('_page_body');
151 }
152
153 if ($Mode == 'Delete')
154 {
155         $row = get_attachment($selected_id);
156         $dir =  company_path()."/attachments";
157         if (file_exists($dir."/".$row['unique_name']))
158                 unlink($dir."/".$row['unique_name']);
159         delete_attachment($selected_id);        
160         display_notification(_("Attachment has been deleted.")); 
161         reset_form();
162 }
163
164 if ($Mode == 'RESET')
165         reset_form();
166
167 function reset_form()
168 {
169         global $selected_id;
170         unset($_POST['trans_no']);
171         unset($_POST['description']);
172         $selected_id = -1;
173 }
174
175 function viewing_controls()
176 {
177         global $selected_id;
178         
179     start_table(TABLESTYLE_NOBORDER);
180
181         start_row();
182         systypes_list_cells(_("Type:"), 'filterType', null, true);
183         if (list_updated('filterType'))
184                 reset_form();
185
186         if(get_post('filterType') == ST_CUSTOMER ){
187                 customer_list_cells(_("Select a customer: "), 'trans_no', null, false, true, true);
188         } elseif(get_post('filterType') == ST_SUPPLIER){
189                 supplier_list_cells(_("Select a supplier: "), 'trans_no', null,  false, true,true);
190         } elseif(get_post('filterType') == ST_ITEM){
191                 stock_items_list_cells(_("Select an Item: "), 'trans_no', null,  false, true,true);
192         }
193
194         end_row();
195     end_table(1);
196
197 }
198
199 function trans_view($trans)
200 {
201         if ($type==ST_SUPPLIER || $type==ST_CUSTOMER || $type==ST_ITEM)
202                 return $trans['id'];
203         return get_trans_view_str($trans["type_no"], $trans["trans_no"]);
204 }
205
206 function edit_link($row)
207 {
208         return button('Edit'.$row["id"], _("Edit"), _("Edit"), ICON_EDIT);
209 }
210
211 function view_link($row)
212 {
213         return button('view'.$row["id"], _("View"), _("View"), ICON_VIEW);
214 }
215
216 function download_link($row)
217 {
218         return button('download'.$row["id"], _("Download"), _("Download"), ICON_DOWN);
219 }
220
221 function delete_link($row)
222 {
223         return button('Delete'.$row["id"], _("Delete"), _("Delete"), ICON_DELETE);
224 }
225
226 function display_rows($type, $trans_no)
227 {
228         $sql = get_sql_for_attached_documents($type, $type==ST_SUPPLIER || $type==ST_CUSTOMER ? $trans_no : 
229                 ($type==ST_ITEM ? get_item_code_id($trans_no) : 0));
230         $cols = array(
231             _("#") => array('fun'=>'trans_view', 'ord'=>''), 
232                 _("Doc Title") => array('name'=>'description'),
233             _("Filename") => array('name'=>'filename'),
234             _("Size") => array('name'=>'filesize'),
235             _("Filetype") => array('name'=>'filetype'),
236             _("Doc Date") => array('name'=>'tran_date', 'type'=>'date', 'ord'=>''),
237                 array('insert'=>true, 'fun'=>'edit_link'),
238                 array('insert'=>true, 'fun'=>'view_link'),
239                 array('insert'=>true, 'fun'=>'download_link'),
240                 array('insert'=>true, 'fun'=>'delete_link')
241             );  
242
243         $table =& new_db_pager('trans_tbl', $sql, $cols);
244
245         $table->width = "60%";
246
247         display_db_pager($table);
248 }
249
250 //----------------------------------------------------------------------------------------
251 if (list_updated('filterType') || list_updated('trans_no'))
252         $Ajax->activate('_page_body');
253
254 start_form(true);
255
256 viewing_controls();
257
258 $type = get_post('filterType');
259
260 display_rows($type, get_post('trans_no'));
261
262 br(2);
263
264 start_table(TABLESTYLE2);
265
266 if ($selected_id != -1)
267 {
268         if ($Mode == 'Edit')
269         {
270                 $row = get_attachment($selected_id);
271                 $_POST['trans_no']  = $row["trans_no"];
272                 $_POST['description']  = $row["description"];
273                 hidden('trans_no', $row['trans_no']);
274                 hidden('unique_name', $row['unique_name']);
275                 if ($type != ST_SUPPLIER && $type != ST_CUSTOMER && $type != ST_ITEM)
276                         label_row(_("Transaction #"), $row['trans_no']);
277         }       
278         hidden('selected_id', $selected_id);
279 }
280 else {
281         if ($type != ST_SUPPLIER && $type != ST_CUSTOMER && $type != ST_ITEM)
282                 text_row_ex(_("Transaction #").':', 'trans_no', 10);
283 }
284 text_row_ex(_("Doc Title").':', 'description', 40);
285 file_row(_("Attached File") . ":", 'filename', 'filename');
286
287 end_table(1);
288
289 submit_add_or_update_center($selected_id == -1, '', 'process');
290
291 end_form();
292
293 end_page();
294