Added cancel edition hotkey
[fa-stable.git] / admin / attachments.php
1 <?php
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 ***********************************************************************/
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 file_get_contents($comp_path."/".user_company(). "/attachments/".$row['unique_name']);
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 file_get_contents($comp_path."/".user_company(). "/attachments/".$row['unique_name']);
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
73                 $dir =  $comp_path."/".user_company(). "/attachments";
74                 if (!file_exists($dir))
75                 {
76                         mkdir ($dir,0777);
77                         $index_file = "<?php\nheader(\"Location: ../index.php\");\n?>";
78                         $fp = fopen($dir."/index.php", "w");
79                         fwrite($fp, $index_file);
80                         fclose($fp);
81                 }
82                 if ($Mode == 'UPDATE_ITEM' && file_exists($dir."/".$_POST['unique_name']))
83                         unlink($dir."/".$_POST['unique_name']);
84
85                 $unique_name = uniqid('');
86                 move_uploaded_file($tmpname, $dir."/".$unique_name);
87                 //save the file
88                 $filename = $_FILES['filename']['name'];
89                 $filesize = $_FILES['filename']['size'];
90                 $filetype = $_FILES['filename']['type'];
91         }
92         else
93         {
94                 $unique_name = $filename = $filetype = "";
95                 $filesize = 0;
96         }
97         $date = date2sql(Today());
98         if ($Mode == 'ADD_ITEM')
99         {
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.")); 
105         }
106         else
107         {
108                 $sql = "UPDATE ".TB_PREF."attachments SET
109                         type_no=".$_POST['filterType'].",
110                         trans_no=".$_POST['trans_no'].",
111                         description=".db_escape($_POST['description']).", ";
112                 if ($filename != "")
113                 {
114                         $sql .= "filename='$filename',
115                         unique_name='$unique_name',
116                         filesize='$filesize',
117                         filetype='$filetype', ";
118                 }       
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.")); 
122         }
123         $Mode = 'RESET';
124 }               
125
126 if ($Mode == 'Delete')
127 {
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.")); 
135         $Mode = 'RESET';
136 }
137
138 if ($Mode == 'RESET')
139 {
140         unset($_POST['trans_no']);
141         unset($_POST['description']);
142         $selected_id = -1;
143 }
144
145 function viewing_controls()
146 {
147     start_form();
148
149     start_table("class='tablestyle_noborder'");
150
151         systypes_list_row(_("Type:"), 'filterType', null, true);
152
153     end_table(1);
154
155         end_form();
156 }
157
158 //----------------------------------------------------------------------------------------
159
160 function get_attached_documents($type)
161 {
162         $sql = "SELECT * FROM ".TB_PREF."attachments WHERE type_no=$type ORDER BY trans_no";
163         return db_query($sql, "Could not retrieve attachments");
164 }
165
166 function get_attachment($id)
167 {
168         $sql = "SELECT * FROM ".TB_PREF."attachments WHERE id=$id";
169         $result = db_query($sql, "Could not retrieve attachments");
170         return db_fetch($result);
171 }
172
173 function display_rows($type)
174 {
175         global $table_style;
176
177         $rows = get_attached_documents($type);
178         $th = array(_("#"), _("Description"), _("Filename"), _("Size"), _("Filetype"), _("Date Uploaded"), "", "", "", "");
179         
180         div_start('transactions');
181         start_form();
182         start_table($table_style);
183         table_header($th);
184         $k = 0;
185         while ($row = db_fetch($rows))
186         {
187                 alt_table_row_color($k);
188                 
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"));
199         end_row();
200         }       
201         end_table(1);
202         hidden('filterType', $type);
203         end_form();
204         div_end();
205 }
206
207 //----------------------------------------------------------------------------------------
208
209 viewing_controls();
210
211 if (isset($_POST['filterType']))
212         display_rows($_POST['filterType']);
213
214 start_form(true);
215
216 start_table($table_style2);
217
218 if ($selected_id != -1)
219 {
220         if ($Mode == 'Edit')
221         {
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']);
228         }       
229         hidden('selected_id', $selected_id);
230 }
231 else
232         text_row_ex(_("Transaction #").':', 'trans_no', 10);
233 text_row_ex(_("Description").':', 'description', 40);
234 start_row();
235 label_cells(_("Attached File") . ":", "<input type='file' id='filename' name='filename'>");
236 end_row();
237
238 end_table(1);
239 if (isset($_POST['filterType']))
240         hidden('filterType', $_POST['filterType']);
241
242 submit_add_or_update_center($selected_id == -1, '', 'both');
243
244 end_form();
245
246 end_page();
247
248 ?>