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 ***********************************************************************/
13 $page_security = 'SA_ATTACHDOCUMENT';
15 include_once($path_to_root . "/includes/db_pager.inc");
16 include_once($path_to_root . "/includes/session.inc");
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");
25 if (isset($_GET['vw']))
26 $view_id = $_GET['vw'];
28 $view_id = find_submit('view');
31 $row = get_attachment($view_id);
32 if ($row['filename'] != "")
35 $Ajax->popup($_SERVER['PHP_SELF'].'?vw='.$view_id);
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']);
46 if (isset($_GET['dl']))
47 $download_id = $_GET['dl'];
49 $download_id = find_submit('download');
51 if ($download_id != -1)
53 $row = get_attachment($download_id);
54 if ($row['filename'] != "")
57 $Ajax->redirect($_SERVER['PHP_SELF'].'?dl='.$download_id);
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']);
70 if ($SysPrefs->use_popup_windows)
71 $js .= get_js_open_window(800, 500);
72 page(_($help_context = "Attach Documents"), false, false, "", $js);
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'];
81 if ($Mode == 'ADD_ITEM' || $Mode == 'UPDATE_ITEM')
84 $filename = basename($_FILES['filename']['name']);
85 if (($_POST['filterType'] == ST_ITEM || $_POST['filterType'] == ST_FIXEDASSET) && $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')))
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."));
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."));
102 //$content = base64_encode(file_get_contents($_FILES['filename']['tmp_name']));
103 $tmpname = $_FILES['filename']['tmp_name'];
105 $dir = company_path()."/attachments";
106 if (!file_exists($dir))
109 $index_file = "<?php\nheader(\"Location: ../index.php\");\n";
110 $fp = fopen($dir."/index.php", "w");
111 fwrite($fp, $index_file);
115 $filesize = $_FILES['filename']['size'];
116 $filetype = $_FILES['filename']['type'];
118 // file name compatible with POSIX
119 // protect against directory traversal
120 if ($Mode == 'UPDATE_ITEM')
122 $row = get_attachment($selected_id);
123 if ($row['filename'] == "")
125 $unique_name = $row['unique_name'];
126 if ($filename && file_exists($dir."/".$unique_name))
127 unlink($dir."/".$unique_name);
130 $unique_name = random_id();
133 move_uploaded_file($tmpname, $dir."/".$unique_name);
135 if ($Mode == 'ADD_ITEM')
137 add_attachment($_POST['filterType'], $_POST['trans_no'], $_POST['description'],
138 $filename, $unique_name, $filesize, $filetype);
139 display_notification(_("Attachment has been inserted."));
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."));
149 refresh_pager('trans_tbl');
150 $Ajax->activate('_page_body');
153 if ($Mode == 'Delete')
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."));
164 if ($Mode == 'RESET')
167 function reset_form()
170 unset($_POST['trans_no']);
171 unset($_POST['description']);
175 function viewing_controls()
179 start_table(TABLESTYLE_NOBORDER);
182 systypes_list_cells(_("Type:"), 'filterType', null, true);
183 if (list_updated('filterType'))
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 } elseif(get_post('filterType') == ST_FIXEDASSET){
193 stock_items_list_cells(_("Select an Item: "), 'trans_no', null, false, true,false, false,
194 array('fixed_asset' => 1));
202 function trans_view($trans)
204 if ($trans['type_no']==ST_SUPPLIER || $trans['type_no']==ST_CUSTOMER || $trans['type_no']==ST_ITEM ||
205 $trans['type_no']==ST_FIXEDASSET)
207 return get_trans_view_str($trans["type_no"], $trans["trans_no"]);
210 function edit_link($row)
212 return button('Edit'.$row["id"], _("Edit"), _("Edit"), ICON_EDIT);
215 function view_link($row)
217 return button('view'.$row["id"], _("View"), _("View"), ICON_VIEW);
220 function download_link($row)
222 return button('download'.$row["id"], _("Download"), _("Download"), ICON_DOWN);
225 function delete_link($row)
227 return button('Delete'.$row["id"], _("Delete"), _("Delete"), ICON_DELETE);
230 function display_rows($type, $trans_no)
232 $sql = get_sql_for_attached_documents($type, $type==ST_SUPPLIER || $type==ST_CUSTOMER ? $trans_no :
233 ($type==ST_ITEM || $type==ST_FIXEDASSET ? get_item_code_id($trans_no) : 0));
235 _("#") => array('fun'=>'trans_view', 'ord'=>''),
236 _("Doc Title") => array('name'=>'description'),
237 _("Filename") => array('name'=>'filename'),
238 _("Size") => array('name'=>'filesize'),
239 _("Filetype") => array('name'=>'filetype'),
240 _("Doc Date") => array('name'=>'tran_date', 'type'=>'date', 'ord'=>''),
241 array('insert'=>true, 'fun'=>'edit_link'),
242 array('insert'=>true, 'fun'=>'view_link'),
243 array('insert'=>true, 'fun'=>'download_link'),
244 array('insert'=>true, 'fun'=>'delete_link')
247 $table =& new_db_pager('trans_tbl', $sql, $cols);
249 $table->width = "60%";
251 display_db_pager($table);
254 //----------------------------------------------------------------------------------------
255 if (list_updated('filterType') || list_updated('trans_no'))
256 $Ajax->activate('_page_body');
262 $type = get_post('filterType');
264 display_rows($type, get_post('trans_no'));
268 start_table(TABLESTYLE2);
270 if ($selected_id != -1)
274 $row = get_attachment($selected_id);
275 $_POST['trans_no'] = $row["trans_no"];
276 $_POST['description'] = $row["description"];
277 hidden('trans_no', $row['trans_no']);
278 hidden('unique_name', $row['unique_name']);
279 if ($type != ST_SUPPLIER && $type != ST_CUSTOMER && $type != ST_ITEM)
280 label_row(_("Transaction #"), $row['trans_no']);
282 hidden('selected_id', $selected_id);
285 if ($type != ST_SUPPLIER && $type != ST_CUSTOMER && $type != ST_ITEM && $type != ST_FIXEDASSET)
286 text_row_ex(_("Transaction #").':', 'trans_no', 10);
288 text_row_ex(_("Doc Title").':', 'description', 40);
289 file_row(_("Attached File") . ":", 'filename', 'filename');
293 submit_add_or_update_center($selected_id == -1, '', 'process');