2 /**********************************************************************
3 Copyright (C) FrontAccounting, LLC.
4 Released under the terms of the GNU Affero General Public License,
5 AGPL, as published by the Free Software Foundation, either version
6 3 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/agpl-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 $row["bin_data"];
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 $row["bin_data"];
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'];
72 $fp = fopen($tmpname, 'r');
73 $content = fread($fp, filesize($tmpname));
74 $content = addslashes($content);
77 //$content = addslashes(file_get_contents($_FILES['filename']['tmp_name']));
78 $filename = $_FILES['filename']['name'];
79 $filesize = $_FILES['filename']['size'];
80 $filetype = $_FILES['filename']['type'];
84 $content = $filename = $filetype = "";
87 $date = date2sql(Today());
88 if ($Mode == 'ADD_ITEM')
90 $sql = "INSERT INTO ".TB_PREF."attachments (type_no, trans_no, description, bin_data, filename,
91 filesize, filetype, tran_date) VALUES (".$_POST['filterType'].",".$_POST['trans_no'].",".
92 db_escape($_POST['description']).",'$content', '$filename', '$filesize', '$filetype', '$date')";
93 db_query($sql, "Attachment could not be inserted");
94 display_notification(_("Attachment has been inserted."));
98 $sql = "UPDATE ".TB_PREF."attachments SET
99 type_no=".$_POST['filterType'].",
100 trans_no=".$_POST['trans_no'].",
101 description=".db_escape($_POST['description']).", ";
104 $sql .= "bin_data='$content',
105 filename='$filename',
106 filesize='$filesize',
107 filetype='$filetype', ";
109 $sql .= "tran_date='$date' WHERE id=$selected_id";
110 db_query($sql, "Attachment could not be updated");
111 display_notification(_("Attachment has been updated."));
116 if ($Mode == 'Delete')
118 $sql = "DELETE FROM ".TB_PREF."attachments WHERE id = $selected_id";
119 db_query($sql, "Could not delete attachment");
120 display_notification(_("Attachment has been deleted."));
124 if ($Mode == 'RESET')
126 unset($_POST['trans_no']);
127 unset($_POST['description']);
131 function viewing_controls()
133 start_form(false, true);
135 start_table("class='tablestyle_noborder'");
137 systypes_list_row(_("Type:"), 'filterType', null, true);
144 //----------------------------------------------------------------------------------------
146 function get_attached_documents($type)
148 $sql = "SELECT * FROM ".TB_PREF."attachments WHERE type_no=$type ORDER BY trans_no";
149 return db_query($sql, "Could not retrieve attachments");
152 function get_attachment($id)
154 $sql = "SELECT * FROM ".TB_PREF."attachments WHERE id=$id";
155 $result = db_query($sql, "Could not retrieve attachments");
156 return db_fetch($result);
159 function display_rows($type)
163 $rows = get_attached_documents($type);
164 $th = array(_("#"), _("Description"), _("Filename"), _("Size"), _("Filetype"), _("Date Uploaded"), "", "", "", "");
166 div_start('transactions');
168 start_table($table_style);
171 while ($row = db_fetch($rows))
173 alt_table_row_color($k);
175 label_cell(get_trans_view_str($type, $row['trans_no']));
176 label_cell($row['description']);
177 label_cell($row['filename']);
178 label_cell($row['filesize']);
179 label_cell($row['filetype']);
180 label_cell(sql2date($row['tran_date']));
181 edit_button_cell("Edit".$row['id'], _("Edit"));
182 button_cell("view".$row['id'], _("View"), false, ICON_VIEW);
183 button_cell("download".$row['id'], _("Download"), false, ICON_DOWN);
184 delete_button_cell("Delete".$row['id'], _("Delete"));
188 hidden('filterType', $type);
193 //----------------------------------------------------------------------------------------
197 if (isset($_POST['filterType']))
198 display_rows($_POST['filterType']);
202 start_table("$table_style2 width=30%");
204 if ($selected_id != -1)
208 $row = get_attachment($selected_id);
209 $_POST['trans_no'] = $row["trans_no"];
210 $_POST['description'] = $row["description"];
211 hidden('trans_no', $row['trans_no']);
212 label_row(_("Transaction #"), $row['trans_no']);
214 hidden('selected_id', $selected_id);
217 text_row_ex(_("Transaction #").':', 'trans_no', 10);
218 text_row_ex(_("Description").':', 'description', 40);
220 label_cells(_("Attached File") . ":", "<input type='file' id='filename' name='filename'>");
224 if (isset($_POST['filterType']))
225 hidden('filterType', $_POST['filterType']);
227 submit_add_or_update_center($selected_id == -1, '', true);