*** empty log message ***
[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 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         $sql = "DELETE FROM ".TB_PREF."attachments WHERE id = $selected_id";
129         db_query($sql, "Could not delete attachment");
130         display_notification(_("Attachment has been deleted.")); 
131         $Mode = 'RESET';
132 }
133
134 if ($Mode == 'RESET')
135 {
136         unset($_POST['trans_no']);
137         unset($_POST['description']);
138         $selected_id = -1;
139 }
140
141 function viewing_controls()
142 {
143     start_form(false, true);
144
145     start_table("class='tablestyle_noborder'");
146
147         systypes_list_row(_("Type:"), 'filterType', null, true);
148
149     end_table(1);
150
151         end_form();
152 }
153
154 //----------------------------------------------------------------------------------------
155
156 function get_attached_documents($type)
157 {
158         $sql = "SELECT * FROM ".TB_PREF."attachments WHERE type_no=$type ORDER BY trans_no";
159         return db_query($sql, "Could not retrieve attachments");
160 }
161
162 function get_attachment($id)
163 {
164         $sql = "SELECT * FROM ".TB_PREF."attachments WHERE id=$id";
165         $result = db_query($sql, "Could not retrieve attachments");
166         return db_fetch($result);
167 }
168
169 function display_rows($type)
170 {
171         global $table_style;
172
173         $rows = get_attached_documents($type);
174         $th = array(_("#"), _("Description"), _("Filename"), _("Size"), _("Filetype"), _("Date Uploaded"), "", "", "", "");
175         
176         div_start('transactions');
177         start_form();
178         start_table($table_style);
179         table_header($th);
180         $k = 0;
181         while ($row = db_fetch($rows))
182         {
183                 alt_table_row_color($k);
184                 
185                 label_cell(get_trans_view_str($type, $row['trans_no']));
186                 label_cell($row['description']);
187                 label_cell($row['filename']);
188                 label_cell($row['filesize']);
189                 label_cell($row['filetype']);
190                 label_cell(sql2date($row['tran_date']));
191                 edit_button_cell("Edit".$row['id'], _("Edit"));
192                 button_cell("view".$row['id'], _("View"), false, ICON_VIEW);
193                 button_cell("download".$row['id'], _("Download"), false, ICON_DOWN);
194                 delete_button_cell("Delete".$row['id'], _("Delete"));
195         end_row();
196         }       
197         end_table(1);
198         hidden('filterType', $type);
199         end_form();
200         div_end();
201 }
202
203 //----------------------------------------------------------------------------------------
204
205 viewing_controls();
206
207 if (isset($_POST['filterType']))
208         display_rows($_POST['filterType']);
209
210 start_form(true);
211
212 start_table("$table_style2 width=30%");
213
214 if ($selected_id != -1)
215 {
216         if ($Mode == 'Edit')
217         {
218                 $row = get_attachment($selected_id);
219                 $_POST['trans_no']  = $row["trans_no"];
220                 $_POST['description']  = $row["description"];
221                 hidden('trans_no', $row['trans_no']);
222                 hidden('unique_name', $row['unique_name']);
223                 label_row(_("Transaction #"), $row['trans_no']);
224         }       
225         hidden('selected_id', $selected_id);
226 }
227 else
228         text_row_ex(_("Transaction #").':', 'trans_no', 10);
229 text_row_ex(_("Description").':', 'description', 40);
230 start_row();
231 label_cells(_("Attached File") . ":", "<input type='file' id='filename' name='filename'>");
232 end_row();
233
234 end_table(1);
235 if (isset($_POST['filterType']))
236         hidden('filterType', $_POST['filterType']);
237
238 submit_add_or_update_center($selected_id == -1, '', true);
239
240 end_form();
241
242 end_page();
243
244 ?>