Added Attachment of Documents and a couple of links from supplier invoice/crfedit...
[fa-stable.git] / admin / attachments.php
1 <?php
2
3 $path_to_root="..";
4 $page_security = 8;
5
6 include_once($path_to_root . "/includes/session.inc");
7
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");
11
12 $view_id = find_submit('view');
13 if ($view_id != -1)
14 {
15         $row = get_attachment($view_id);
16         if ($row['filename'] != "")
17         {
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']);
23         else
24                         header("Content-Disposition: inline");
25         echo $row["bin_data"];
26         exit();
27         }       
28 }
29
30 $download_id = find_submit('download');
31 if ($download_id != -1)
32 {
33         $row = get_attachment($download_id);
34         if ($row['filename'] != "")
35         {
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"];
41         exit();
42         }       
43 }
44
45 $js = "";
46 if ($use_popup_windows)
47         $js .= get_js_open_window(800, 500);
48 page(_("Attach Documents"), false, false, "", $js);
49
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'];
56         
57 if ($Mode == 'ADD_ITEM' || $Mode == 'UPDATE_ITEM')
58 {
59         if (isset($_FILES['filename']) && $_FILES['filename']['size'] > 0)
60         {
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);
66                 fclose($fp);
67
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'];
72         }
73         else
74         {
75                 $content = $filename = $filetype = "";
76                 $filesize = 0;
77         }
78         $date = date2sql(Today());
79         if ($Mode == 'ADD_ITEM')
80         {
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.")); 
86         }
87         else
88         {
89                 $sql = "UPDATE ".TB_PREF."attachments SET
90                         type_no=".$_POST['filterType'].",
91                         trans_no=".$_POST['trans_no'].",
92                         description=".db_escape($_POST['description']).", ";
93                 if ($filename != "")
94                 {
95                         $sql .= "bin_data='$content',
96                         filename='$filename',
97                         filesize='$filesize',
98                         filetype='$filetype', ";
99                 }       
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.")); 
103         }
104         $Mode = 'RESET';
105 }               
106
107 if ($Mode == 'Delete')
108 {
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.")); 
112         $Mode = 'RESET';
113 }
114
115 if ($Mode == 'RESET')
116 {
117         unset($_POST['trans_no']);
118         unset($_POST['description']);
119         $selected_id = -1;
120 }
121
122 function viewing_controls()
123 {
124     start_form(false, true);
125
126     start_table("class='tablestyle_noborder'");
127
128         systypes_list_row(_("Type:"), 'filterType', null, true);
129
130     end_table(1);
131
132         end_form();
133 }
134
135 //----------------------------------------------------------------------------------------
136
137 function get_attached_documents($type)
138 {
139         $sql = "SELECT * FROM ".TB_PREF."attachments WHERE type_no=$type ORDER BY trans_no";
140         return db_query($sql, "Could not retrieve attachments");
141 }
142
143 function get_attachment($id)
144 {
145         $sql = "SELECT * FROM ".TB_PREF."attachments WHERE id=$id";
146         $result = db_query($sql, "Could not retrieve attachments");
147         return db_fetch($result);
148 }
149
150 function display_rows($type)
151 {
152         global $table_style;
153
154         $rows = get_attached_documents($type);
155         $th = array(_("#"), _("Description"), _("Filename"), _("Size"), _("Filetype"), _("Date Uploaded"), "", "", "", "");
156         
157         div_start('transactions');
158         start_form();
159         start_table($table_style);
160         table_header($th);
161         $k = 0;
162         while ($row = db_fetch($rows))
163         {
164                 alt_table_row_color($k);
165                 
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"));
176         end_row();
177         }       
178         end_table(1);
179         hidden('filterType', $type);
180         end_form();
181         div_end();
182 }
183
184 //----------------------------------------------------------------------------------------
185
186 viewing_controls();
187
188 if (isset($_POST['filterType']))
189         display_rows($_POST['filterType']);
190
191 start_form(true);
192
193 start_table("$table_style2 width=30%");
194
195 if ($selected_id != -1)
196 {
197         if ($Mode == 'Edit')
198         {
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']);
204         }       
205         hidden('selected_id', $selected_id);
206 }
207 else
208         text_row_ex(_("Transaction #").':', 'trans_no', 10);
209 text_row_ex(_("Description").':', 'description', 40);
210 start_row();
211 label_cells(_("Attached File") . ":", "<input type='file' id='filename' name='filename'>");
212 end_row();
213
214 end_table(1);
215 if (isset($_POST['filterType']))
216         hidden('filterType', $_POST['filterType']);
217
218 submit_add_or_update_center($selected_id == -1, '', true);
219
220 end_form();
221
222 end_page();
223
224 ?>