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 ***********************************************************************/
15 include_once($path_to_root . "/includes/session.inc");
17 include_once($path_to_root . "/includes/date_functions.inc");
18 include_once($path_to_root . "/includes/ui.inc");
19 include_once($path_to_root . "/includes/data_checks.inc");
21 $view_id = find_submit('view');
24 $row = get_attachment($view_id);
25 if ($row['filename'] != "")
27 $type = ($row['filetype']) ? $row['filetype'] : 'application/octet-stream';
28 header("Content-type: ".$type);
29 header('Content-Length: '.$row['filesize']);
30 if ($type == 'application/octet-stream')
31 header('Content-Disposition: attachment; filename='.$row['filename']);
33 header("Content-Disposition: inline");
34 echo file_get_contents($comp_path."/".user_company(). "/attachments/".$row['unique_name']);
39 $download_id = find_submit('download');
40 if ($download_id != -1)
42 $row = get_attachment($download_id);
43 if ($row['filename'] != "")
45 $type = ($row['filetype']) ? $row['filetype'] : 'application/octet-stream';
46 header("Content-type: ".$type);
47 header('Content-Length: '.$row['filesize']);
48 header('Content-Disposition: attachment; filename='.$row['filename']);
49 echo file_get_contents($comp_path."/".user_company(). "/attachments/".$row['unique_name']);
55 if ($use_popup_windows)
56 $js .= get_js_open_window(800, 500);
57 page(_("Attach Documents"), false, false, "", $js);
59 simple_page_mode(true);
60 //----------------------------------------------------------------------------------------
61 if (isset($_GET['filterType'])) // catch up external links
62 $_POST['filterType'] = $_GET['filterType'];
63 if (isset($_GET['trans_no']))
64 $_POST['trans_no'] = $_GET['trans_no'];
66 if ($Mode == 'ADD_ITEM' || $Mode == 'UPDATE_ITEM')
68 if (isset($_FILES['filename']) && $_FILES['filename']['size'] > 0)
70 //$content = base64_encode(file_get_contents($_FILES['filename']['tmp_name']));
71 $tmpname = $_FILES['filename']['tmp_name'];
73 $dir = $comp_path."/".user_company(). "/attachments";
74 if (!file_exists($dir))
77 $index_file = "<?php\nheader(\"Location: ../index.php\");\n?>";
78 $fp = fopen($dir."/index.php", "w");
79 fwrite($fp, $index_file);
82 if ($Mode == 'UPDATE_ITEM' && file_exists($dir."/".$_POST['unique_name']))
83 unlink($dir."/".$_POST['unique_name']);
85 $unique_name = uniqid('');
86 move_uploaded_file($tmpname, $dir."/".$unique_name);
88 $filename = $_FILES['filename']['name'];
89 $filesize = $_FILES['filename']['size'];
90 $filetype = $_FILES['filename']['type'];
94 $unique_name = $filename = $filetype = "";
97 $date = date2sql(Today());
98 if ($Mode == 'ADD_ITEM')
100 $sql = "INSERT INTO ".TB_PREF."attachments (type_no, trans_no, description, filename, unique_name,
101 filesize, filetype, tran_date) VALUES (".$_POST['filterType'].",".$_POST['trans_no'].",".
102 db_escape($_POST['description']).", '$filename', '$unique_name', '$filesize', '$filetype', '$date')";
103 db_query($sql, "Attachment could not be inserted");
104 display_notification(_("Attachment has been inserted."));
108 $sql = "UPDATE ".TB_PREF."attachments SET
109 type_no=".$_POST['filterType'].",
110 trans_no=".$_POST['trans_no'].",
111 description=".db_escape($_POST['description']).", ";
114 $sql .= "filename='$filename',
115 unique_name='$unique_name',
116 filesize='$filesize',
117 filetype='$filetype', ";
119 $sql .= "tran_date='$date' WHERE id=$selected_id";
120 db_query($sql, "Attachment could not be updated");
121 display_notification(_("Attachment has been updated."));
126 if ($Mode == 'Delete')
128 $row = get_attachment($selected_id);
129 $dir = $comp_path."/".user_company(). "/attachments";
130 if (file_exists($dir."/".$row['unique_name']))
131 unlink($dir."/".$row['unique_name']);
132 $sql = "DELETE FROM ".TB_PREF."attachments WHERE id = $selected_id";
133 db_query($sql, "Could not delete attachment");
134 display_notification(_("Attachment has been deleted."));
138 if ($Mode == 'RESET')
140 unset($_POST['trans_no']);
141 unset($_POST['description']);
145 function viewing_controls()
147 start_form(false, true);
149 start_table("class='tablestyle_noborder'");
151 systypes_list_row(_("Type:"), 'filterType', null, true);
158 //----------------------------------------------------------------------------------------
160 function get_attached_documents($type)
162 $sql = "SELECT * FROM ".TB_PREF."attachments WHERE type_no=$type ORDER BY trans_no";
163 return db_query($sql, "Could not retrieve attachments");
166 function get_attachment($id)
168 $sql = "SELECT * FROM ".TB_PREF."attachments WHERE id=$id";
169 $result = db_query($sql, "Could not retrieve attachments");
170 return db_fetch($result);
173 function display_rows($type)
177 $rows = get_attached_documents($type);
178 $th = array(_("#"), _("Description"), _("Filename"), _("Size"), _("Filetype"), _("Date Uploaded"), "", "", "", "");
180 div_start('transactions');
182 start_table($table_style);
185 while ($row = db_fetch($rows))
187 alt_table_row_color($k);
189 label_cell(get_trans_view_str($type, $row['trans_no']));
190 label_cell($row['description']);
191 label_cell($row['filename']);
192 label_cell($row['filesize']);
193 label_cell($row['filetype']);
194 label_cell(sql2date($row['tran_date']));
195 edit_button_cell("Edit".$row['id'], _("Edit"));
196 button_cell("view".$row['id'], _("View"), false, ICON_VIEW);
197 button_cell("download".$row['id'], _("Download"), false, ICON_DOWN);
198 delete_button_cell("Delete".$row['id'], _("Delete"));
202 hidden('filterType', $type);
207 //----------------------------------------------------------------------------------------
211 if (isset($_POST['filterType']))
212 display_rows($_POST['filterType']);
216 start_table($table_style2);
218 if ($selected_id != -1)
222 $row = get_attachment($selected_id);
223 $_POST['trans_no'] = $row["trans_no"];
224 $_POST['description'] = $row["description"];
225 hidden('trans_no', $row['trans_no']);
226 hidden('unique_name', $row['unique_name']);
227 label_row(_("Transaction #"), $row['trans_no']);
229 hidden('selected_id', $selected_id);
232 text_row_ex(_("Transaction #").':', 'trans_no', 10);
233 text_row_ex(_("Description").':', 'description', 40);
235 label_cells(_("Attached File") . ":", "<input type='file' id='filename' name='filename'>");
239 if (isset($_POST['filterType']))
240 hidden('filterType', $_POST['filterType']);
242 submit_add_or_update_center($selected_id == -1, '', true);