Inserted Copyright Notice and fixed graphic items
[fa-stable.git] / admin / attachments.php
1 <?php
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 ***********************************************************************/
12 $path_to_root="..";
13 $page_security = 8;
14
15 include_once($path_to_root . "/includes/session.inc");
16
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");
20
21 $view_id = find_submit('view');
22 if ($view_id != -1)
23 {
24         $row = get_attachment($view_id);
25         if ($row['filename'] != "")
26         {
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']);
32         else
33                         header("Content-Disposition: inline");
34         echo $row["bin_data"];
35         exit();
36         }       
37 }
38
39 $download_id = find_submit('download');
40 if ($download_id != -1)
41 {
42         $row = get_attachment($download_id);
43         if ($row['filename'] != "")
44         {
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"];
50         exit();
51         }       
52 }
53
54 $js = "";
55 if ($use_popup_windows)
56         $js .= get_js_open_window(800, 500);
57 page(_("Attach Documents"), false, false, "", $js);
58
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'];
65         
66 if ($Mode == 'ADD_ITEM' || $Mode == 'UPDATE_ITEM')
67 {
68         if (isset($_FILES['filename']) && $_FILES['filename']['size'] > 0)
69         {
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);
75                 fclose($fp);
76
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'];
81         }
82         else
83         {
84                 $content = $filename = $filetype = "";
85                 $filesize = 0;
86         }
87         $date = date2sql(Today());
88         if ($Mode == 'ADD_ITEM')
89         {
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.")); 
95         }
96         else
97         {
98                 $sql = "UPDATE ".TB_PREF."attachments SET
99                         type_no=".$_POST['filterType'].",
100                         trans_no=".$_POST['trans_no'].",
101                         description=".db_escape($_POST['description']).", ";
102                 if ($filename != "")
103                 {
104                         $sql .= "bin_data='$content',
105                         filename='$filename',
106                         filesize='$filesize',
107                         filetype='$filetype', ";
108                 }       
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.")); 
112         }
113         $Mode = 'RESET';
114 }               
115
116 if ($Mode == 'Delete')
117 {
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.")); 
121         $Mode = 'RESET';
122 }
123
124 if ($Mode == 'RESET')
125 {
126         unset($_POST['trans_no']);
127         unset($_POST['description']);
128         $selected_id = -1;
129 }
130
131 function viewing_controls()
132 {
133     start_form(false, true);
134
135     start_table("class='tablestyle_noborder'");
136
137         systypes_list_row(_("Type:"), 'filterType', null, true);
138
139     end_table(1);
140
141         end_form();
142 }
143
144 //----------------------------------------------------------------------------------------
145
146 function get_attached_documents($type)
147 {
148         $sql = "SELECT * FROM ".TB_PREF."attachments WHERE type_no=$type ORDER BY trans_no";
149         return db_query($sql, "Could not retrieve attachments");
150 }
151
152 function get_attachment($id)
153 {
154         $sql = "SELECT * FROM ".TB_PREF."attachments WHERE id=$id";
155         $result = db_query($sql, "Could not retrieve attachments");
156         return db_fetch($result);
157 }
158
159 function display_rows($type)
160 {
161         global $table_style;
162
163         $rows = get_attached_documents($type);
164         $th = array(_("#"), _("Description"), _("Filename"), _("Size"), _("Filetype"), _("Date Uploaded"), "", "", "", "");
165         
166         div_start('transactions');
167         start_form();
168         start_table($table_style);
169         table_header($th);
170         $k = 0;
171         while ($row = db_fetch($rows))
172         {
173                 alt_table_row_color($k);
174                 
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"));
185         end_row();
186         }       
187         end_table(1);
188         hidden('filterType', $type);
189         end_form();
190         div_end();
191 }
192
193 //----------------------------------------------------------------------------------------
194
195 viewing_controls();
196
197 if (isset($_POST['filterType']))
198         display_rows($_POST['filterType']);
199
200 start_form(true);
201
202 start_table("$table_style2 width=30%");
203
204 if ($selected_id != -1)
205 {
206         if ($Mode == 'Edit')
207         {
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']);
213         }       
214         hidden('selected_id', $selected_id);
215 }
216 else
217         text_row_ex(_("Transaction #").':', 'trans_no', 10);
218 text_row_ex(_("Description").':', 'description', 40);
219 start_row();
220 label_cells(_("Attached File") . ":", "<input type='file' id='filename' name='filename'>");
221 end_row();
222
223 end_table(1);
224 if (isset($_POST['filterType']))
225         hidden('filterType', $_POST['filterType']);
226
227 submit_add_or_update_center($selected_id == -1, '', true);
228
229 end_form();
230
231 end_page();
232
233 ?>