6 include_once($path_to_root . "/includes/session.inc");
8 include_once($path_to_root . "/includes/date_functions.inc");
9 include_once($path_to_root . "/includes/ui.inc");
10 include_once($path_to_root . "/includes/data_checks.inc");
12 $view_id = find_submit('view');
15 $row = get_attachment($view_id);
16 if ($row['filename'] != "")
18 $type = ($row['filetype']) ? $row['filetype'] : 'application/octet-stream';
19 header("Content-type: ".$type);
20 header('Content-Length: '.$row['filesize']);
21 if ($type == 'application/octet-stream')
22 header('Content-Disposition: attachment; filename='.$row['filename']);
24 header("Content-Disposition: inline");
25 echo $row["bin_data"];
30 $download_id = find_submit('download');
31 if ($download_id != -1)
33 $row = get_attachment($download_id);
34 if ($row['filename'] != "")
36 $type = ($row['filetype']) ? $row['filetype'] : 'application/octet-stream';
37 header("Content-type: ".$type);
38 header('Content-Length: '.$row['filesize']);
39 header('Content-Disposition: attachment; filename='.$row['filename']);
40 echo $row["bin_data"];
46 if ($use_popup_windows)
47 $js .= get_js_open_window(800, 500);
48 page(_("Attach Documents"), false, false, "", $js);
50 simple_page_mode(true);
51 //----------------------------------------------------------------------------------------
52 if (isset($_GET['filterType'])) // catch up external links
53 $_POST['filterType'] = $_GET['filterType'];
54 if (isset($_GET['trans_no']))
55 $_POST['trans_no'] = $_GET['trans_no'];
57 if ($Mode == 'ADD_ITEM' || $Mode == 'UPDATE_ITEM')
59 if (isset($_FILES['filename']) && $_FILES['filename']['size'] > 0)
61 //$content = base64_encode(file_get_contents($_FILES['filename']['tmp_name']));
62 $tmpname = $_FILES['filename']['tmp_name'];
63 $fp = fopen($tmpname, 'r');
64 $content = fread($fp, filesize($tmpname));
65 $content = addslashes($content);
68 //$content = addslashes(file_get_contents($_FILES['filename']['tmp_name']));
69 $filename = $_FILES['filename']['name'];
70 $filesize = $_FILES['filename']['size'];
71 $filetype = $_FILES['filename']['type'];
75 $content = $filename = $filetype = "";
78 $date = date2sql(Today());
79 if ($Mode == 'ADD_ITEM')
81 $sql = "INSERT INTO ".TB_PREF."attachments (type_no, trans_no, description, bin_data, filename,
82 filesize, filetype, tran_date) VALUES (".$_POST['filterType'].",".$_POST['trans_no'].",".
83 db_escape($_POST['description']).",'$content', '$filename', '$filesize', '$filetype', '$date')";
84 db_query($sql, "Attachment could not be inserted");
85 display_notification(_("Attachment has been inserted."));
89 $sql = "UPDATE ".TB_PREF."attachments SET
90 type_no=".$_POST['filterType'].",
91 trans_no=".$_POST['trans_no'].",
92 description=".db_escape($_POST['description']).", ";
95 $sql .= "bin_data='$content',
98 filetype='$filetype', ";
100 $sql .= "tran_date='$date' WHERE id=$selected_id";
101 db_query($sql, "Attachment could not be updated");
102 display_notification(_("Attachment has been updated."));
107 if ($Mode == 'Delete')
109 $sql = "DELETE FROM ".TB_PREF."attachments WHERE id = $selected_id";
110 db_query($sql, "Could not delete attachment");
111 display_notification(_("Attachment has been deleted."));
115 if ($Mode == 'RESET')
117 unset($_POST['trans_no']);
118 unset($_POST['description']);
122 function viewing_controls()
124 start_form(false, true);
126 start_table("class='tablestyle_noborder'");
128 systypes_list_row(_("Type:"), 'filterType', null, true);
135 //----------------------------------------------------------------------------------------
137 function get_attached_documents($type)
139 $sql = "SELECT * FROM ".TB_PREF."attachments WHERE type_no=$type ORDER BY trans_no";
140 return db_query($sql, "Could not retrieve attachments");
143 function get_attachment($id)
145 $sql = "SELECT * FROM ".TB_PREF."attachments WHERE id=$id";
146 $result = db_query($sql, "Could not retrieve attachments");
147 return db_fetch($result);
150 function display_rows($type)
154 $rows = get_attached_documents($type);
155 $th = array(_("#"), _("Description"), _("Filename"), _("Size"), _("Filetype"), _("Date Uploaded"), "", "", "", "");
157 div_start('transactions');
159 start_table($table_style);
162 while ($row = db_fetch($rows))
164 alt_table_row_color($k);
166 label_cell(get_trans_view_str($type, $row['trans_no']));
167 label_cell($row['description']);
168 label_cell($row['filename']);
169 label_cell($row['filesize']);
170 label_cell($row['filetype']);
171 label_cell(sql2date($row['tran_date']));
172 edit_button_cell("Edit".$row['id'], _("Edit"));
173 edit_button_cell("view".$row['id'], _("View"));
174 edit_button_cell("download".$row['id'], _("Download"));
175 edit_button_cell("Delete".$row['id'], _("Delete"));
179 hidden('filterType', $type);
184 //----------------------------------------------------------------------------------------
188 if (isset($_POST['filterType']))
189 display_rows($_POST['filterType']);
193 start_table("$table_style2 width=30%");
195 if ($selected_id != -1)
199 $row = get_attachment($selected_id);
200 $_POST['trans_no'] = $row["trans_no"];
201 $_POST['description'] = $row["description"];
202 hidden('trans_no', $row['trans_no']);
203 label_row(_("Transaction #"), $row['trans_no']);
205 hidden('selected_id', $selected_id);
208 text_row_ex(_("Transaction #").':', 'trans_no', 10);
209 text_row_ex(_("Description").':', 'description', 40);
211 label_cells(_("Attached File") . ":", "<input type='file' id='filename' name='filename'>");
215 if (isset($_POST['filterType']))
216 hidden('filterType', $_POST['filterType']);
218 submit_add_or_update_center($selected_id == -1, '', true);