forked from timschofield/webERP
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBrands.php
More file actions
349 lines (288 loc) · 12.7 KB
/
Brands.php
File metadata and controls
349 lines (288 loc) · 12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
<?php
require(__DIR__ . '/includes/session.php');
$Title = __('Brands Maintenance');
$ViewTopic = 'Inventory';
$BookMark = '';
include(__DIR__ . '/includes/header.php');
include(__DIR__ . '/includes/ImageFunctions.php');
if (isset($_GET['SelectedBrand'])){
$SelectedBrand = $_GET['SelectedBrand'];
} elseif (isset($_POST['SelectedBrand'])){
$SelectedBrand = $_POST['SelectedBrand'];
}
$SupportedImgExt = array('png','jpg','jpeg');
if (isset($_POST['submit'])) {
//initialise no input errors assumed initially before we test
$InputError = 0;
/* actions to take once the user has clicked the submit button
ie the page has called itself with some user input */
if (isset($SelectedBrand) AND $InputError !=1) {
if (isset($_FILES['BrandPicture']) AND $_FILES['BrandPicture']['name'] !='') {
$Result = $_FILES['BrandPicture']['error'];
$UploadTheFile = 'Yes'; //Assume all is well to start off with
$ImgExt = pathinfo($_FILES['BrandPicture']['name'], PATHINFO_EXTENSION);
$FileName = $_SESSION['part_pics_dir'] . '/BRAND-' . $SelectedBrand . '.' . $ImgExt;
//But check for the worst
if (!in_array ($ImgExt, $SupportedImgExt)) {
prnMsg(__('Only ' . implode(", ", $SupportedImgExt) . ' files are supported - a file extension of ' . implode(", ", $SupportedImgExt) . ' is expected'),'warn');
$UploadTheFile ='No';
} elseif ( $_FILES['BrandPicture']['size'] > ($_SESSION['MaxImageSize']*1024)) { //File Size Check
prnMsg(__('The file size is over the maximum allowed. The maximum size allowed in KB is') . ' ' . $_SESSION['MaxImageSize'],'warn');
$UploadTheFile ='No';
} elseif ( $_FILES['BrandPicture']['type'] == 'text/plain' ) { //File Type Check
prnMsg( __('Only graphics files can be uploaded'),'warn');
$UploadTheFile ='No';
}
foreach ($SupportedImgExt as $Ext) {
$File = $_SESSION['part_pics_dir'] . '/BRAND-' . $SelectedBrand . '.' . $Ext;
if (file_exists ($File) ) {
$Result = unlink($File);
if (!$Result){
prnMsg(__('The existing image could not be removed'),'error');
$UploadTheFile ='No';
}
}
}
if ($UploadTheFile=='Yes'){
$Result = move_uploaded_file($_FILES['BrandPicture']['tmp_name'], $FileName);
$Message = ($Result)?__('File url') . '<a href="' . $FileName .'">' . $FileName . '</a>' : __('Something is wrong with uploading a file');
$_POST['BrandsImage'] = 'BRAND-' . $SelectedBrand;
} else {
$_POST['BrandsImage'] = '';
}
}
if ( isset($_POST['BrandsImage'])){
foreach ($SupportedImgExt as $Ext) {
$File = $_SESSION['part_pics_dir'] . '/BRAND-' . $SelectedBrand . '.' . $Ext;
if (file_exists ($File) ) {
$_POST['BrandsImage'] = 'BRAND-' . $SelectedBrand;
break;
} else {
$_POST['BrandsImage'] = '';
}
}
}
if (isset($_POST['ClearImage']) ) {
foreach ($SupportedImgExt as $Ext) {
$File = $_SESSION['part_pics_dir'] . '/BRAND-' . $SelectedBrand . '.' . $Ext;
if (file_exists ($File) ) {
@unlink($File);
$_POST['BrandsImage'] = '';
if (is_file($File)) {
prnMsg(__('You do not have access to delete this item image file.'),'error');
}
}
}
}
$SQL = "UPDATE brands SET brands_name='" . $_POST['BrandsName'] . "',
brands_url='" . $_POST['BrandsURL'] . "'";
if (isset($_POST['BrandsImage'])){
$SQL .= ", brands_image='" . $_POST['BrandsImage'] . "'";
}
$SQL .= " WHERE brands_id = '" . $SelectedBrand . "'";
$ErrMsg = __('An error occurred updating the') . ' ' . $SelectedBrand . ' ' . __('brand record because');
$Result = DB_query($SQL, $ErrMsg);
prnMsg( __('The brand record has been updated'),'success');
unset($_POST['BrandsName']);
unset($_POST['BrandsURL']);
unset($_POST['BrandsImage']);
unset($SelectedBrand);
} elseif ($InputError !=1) {
/*SelectedBrand is null cos no item selected on first time round so must be adding a record must be submitting new entries in the new Location form */
$SQL = "INSERT INTO brands (brands_name,
brands_url)
VALUES ('" . $_POST['BrandsName'] . "',
'" . $_POST['BrandsURL'] . "')";
$ErrMsg = __('An error occurred inserting the new brand record because');
$Result = DB_query($SQL, $ErrMsg);
$LastInsertId = DB_Last_Insert_ID('brands', 'brands_id');
if (isset($_FILES['BrandPicture']) AND $_FILES['BrandPicture']['name'] !='') {
$Result = $_FILES['BrandPicture']['error'];
$UploadTheFile = 'Yes'; //Assume all is well to start off with
$ImgExt = pathinfo($_FILES['BrandPicture']['name'], PATHINFO_EXTENSION);
$FileName = $_SESSION['part_pics_dir'] . '/BRAND-' . $LastInsertId . '.' . $ImgExt;
//But check for the worst
if (!in_array ($ImgExt, $SupportedImgExt)) {
prnMsg(__('Only ' . implode(", ", $SupportedImgExt) . ' files are supported - a file extension of ' . implode(", ", $SupportedImgExt) . ' is expected'),'warn');
$UploadTheFile ='No';
} elseif ( $_FILES['BrandPicture']['size'] > ($_SESSION['MaxImageSize']*1024)) { //File Size Check
prnMsg(__('The file size is over the maximum allowed. The maximum size allowed in KB is') . ' ' . $_SESSION['MaxImageSize'],'warn');
$UploadTheFile ='No';
} elseif ( $_FILES['BrandPicture']['type'] == 'text/plain' ) { //File Type Check
prnMsg( __('Only graphics files can be uploaded'),'warn');
$UploadTheFile ='No';
}
foreach ($SupportedImgExt as $Ext) {
$File = $_SESSION['part_pics_dir'] . '/BRAND-' . $LastInsertId . '.' . $Ext;
if (file_exists ($File) ) {
$Result = unlink($File);
if (!$Result){
prnMsg(__('The existing image could not be removed'),'error');
$UploadTheFile ='No';
}
}
}
if ($UploadTheFile=='Yes'){
$Result = move_uploaded_file($_FILES['BrandPicture']['tmp_name'], $FileName);
$Message = ($Result)?__('File url') . '<a href="' . $FileName .'">' . $FileName . '</a>' : __('Something is wrong with uploading a file');
DB_query("UPDATE brands
SET brands_image='" . 'BRAND-' . $LastInsertId . "'
WHERE brands_id = '" . $LastInsertId . "'
");
}
}
prnMsg( __('The new brand record has been added'),'success');
unset($_POST['BrandsName']);
unset($_POST['BrandsURL']);
unset($_POST['BrandsImage']);
unset($SelectedBrand);
}
} elseif (isset($_GET['delete'])) {
//the link to delete a selected record was clicked instead of the submit button
$CancelDelete = false;
// PREVENT DELETES IF DEPENDENT RECORDS
$SQL= "SELECT COUNT(*) FROM salescatprod WHERE brands_id='". $SelectedBrand . "'";
$Result = DB_query($SQL);
$MyRow = DB_fetch_row($Result);
if ($MyRow[0]>0) {
$CancelDelete = true;
prnMsg( __('Cannot delete this brand because products have been defined as from this brand'),'warn');
echo __('There are') . ' ' . $MyRow[0] . ' ' . __('items with this brand code');
}
if (!$CancelDelete) {
$Result = DB_query("DELETE FROM brands WHERE brands_id='" . $SelectedBrand . "'");
foreach ($SupportedImgExt as $Ext) {
$File = $_SESSION['part_pics_dir'] . '/BRAND-' . $SelectedBrand . '.' . $Ext;
if (file_exists ($File) ) {
@unlink($File);
}
}
prnMsg( __('Brand') . ' ' . $SelectedBrand . ' ' . __('has been deleted') . '!', 'success');
unset ($SelectedBrand);
} //end if Delete Brand
unset($SelectedBrand);
unset($_GET['delete']);
}
if (!isset($SelectedBrand)) {
/* It could still be the second time the page has been run and a record has been selected for modification - SelectedBrand will exist because it was sent with the new call. If its the first time the page has been displayed with no parameters
then none of the above are true and the list of Brands will be displayed with
links to delete or edit each. These will call the same page again and allow update/input
or deletion of the records*/
$SQL = "SELECT brands_id,
brands_name,
brands_url,
brands_image
FROM brands";
$Result = DB_query($SQL);
if (DB_num_rows($Result)==0){
prnMsg(__('There are no brands to display'),'error');
}
echo '<p class="page_title_text"><img src="'.$RootPath.'/css/'.$Theme.'/images/supplier.png" Title="' .
__('Brands') . '" alt="" />' . ' ' . $Title . '</p>';
echo '<table class="selection">';
echo '<tr>
<th>' . __('Brand Code') . '</th>
<th>' . __('Brand Name') . '</th>
<th>' . __('Brand URL') . '</th>
<th>' . __('Brands Image') . '</th>
<th colspan="2"></th>
</tr>';
while ($MyRow = DB_fetch_array($Result)) {
$Glob = (glob($_SESSION['part_pics_dir'] . '/BRAND-' . $MyRow['brands_id'] . '.{' . implode(",", $SupportedImgExt) . '}', GLOB_BRACE));
$ImageFile = reset($Glob);
$BrandImgLink = GetImageLink($ImageFile, '/BRAND-' . $MyRow['brands_id'], 120, 120, "", "");
echo '<tr class="striped_row">
<td>', $MyRow['brands_id'], '</td>
<td>', $MyRow['brands_name'], '</td>
<td><a target="_blank" href="', $MyRow['brands_url'], '">', $MyRow['brands_url'], '</a></td>
<td>', $BrandImgLink, '</td>
<td><a href="', htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'UTF-8') . '?SelectedBrand=', $MyRow['brands_id'], '&edit=1">' . __('Edit') . '</a></td>
<td><a href="', htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'UTF-8') . '?SelectedBrand=', $MyRow['brands_id'], '&delete=1" onclick="return confirm(\'' . __('Are you sure you wish to delete this brand?') . '\');">' . __('Delete') . '</a></td>
</tr>';
}
//END WHILE LIST LOOP
echo '</table>';
}
//end of ifs and buts!
if (isset($SelectedBrand)) {
echo '<a href="' . htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'UTF-8') . '">' . __('Review BrandRecords') . '</a>';
}
if (!isset($_GET['delete'])) {
echo '<form enctype="multipart/form-data" method="post" action="' . htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'UTF-8') . '">';
echo '<input type="hidden" name="FormID" value="' . $_SESSION['FormID'] . '" />';
if (isset($SelectedBrand)) {
//editing an existing Brand
echo '<p class="page_title_text"><img src="'.$RootPath.'/css/'.$Theme.'/images/supplier.png" Title="' .
__('Brand') . '" alt="" />' . ' ' . $Title . '</p>';
$SQL = "SELECT brands_id,
brands_name,
brands_url,
brands_image
FROM brands
WHERE brands_id='" . $SelectedBrand . "'";
$Result = DB_query($SQL);
$MyRow = DB_fetch_array($Result);
$_POST['BrandsName'] = $MyRow['brands_name'];
$_POST['BrandsURL'] = $MyRow['brands_url'];
$_POST['BrandsImage'] = $MyRow['brands_image'];
echo '<input type="hidden" name="SelectedBrand" value="' . $SelectedBrand . '" />';
echo '<fieldset>';
echo '<legend>' . __('Update Brand Details') . '</legend>';
} else { //end of if $SelectedBrand only do the else when a new record is being entered
echo '<fieldset>
<legend>' . __('New Brand Details') . '</legend>';
}
if (!isset($_POST['BrandsName'])) {
$_POST['BrandsName'] = '';
}
if (!isset($_POST['BrandsURL'])) {
$_POST['BrandsURL'] = ' ';
}
if (!isset($_POST['BrandsImage'])) {
$_POST['BrandsImage'] = '';
}
echo '<field>
<label for="BrandsName">' . __('Brand Name') . ':' . '</label>
<input type="text" required="required" autofocus="autofocus" name="BrandsName" value="'. $_POST['BrandsName'] . '" size="32" maxlength="32" />
</field>
<field>
<label for="BrandsURL">' . __('Brand URL') . ':' . '</label>
<input type="text" name="BrandsURL" value="' . $_POST['BrandsURL'] . '" size="50" maxlength="50" />
</field>
<field>
<label for="BrandPicture">' . __('Brand Image File (' . implode(", ", $SupportedImgExt) . ')') . ':</label>
<input type="file" id="BrandPicture" name="BrandPicture" />';
if (isset ($_GET['edit']) ) {
echo '<field>
<label for="ClearImage">'.__('Clear Image').'</label>
<input type="checkbox" name="ClearImage" id="ClearImage" value="1">
</field>';
}
echo '</field>';
if (isset($SelectedBrand)){
$Glob = (glob($_SESSION['part_pics_dir'] . '/BRAND-' . $SelectedBrand . '.{' . implode(",", $SupportedImgExt) . '}', GLOB_BRACE));
$ImageFile = reset($Glob);
if (extension_loaded('gd') && function_exists('gd_info') && file_exists($ImageFile)) {
$BrandImgLink = '<img src="GetStockImage.php?automake=1&textcolor=FFFFFF&bgcolor=CCCCCC'.
'&StockID='.urlencode('/BRAND-' . $SelectedBrand).
'&text='.
'&width=100'.
'&height=100'.
'" alt="" />';
} else {
if ( isset($SelectedBrand) AND !empty($SelectedBrand) AND file_exists($ImageFile) ) {
$BrandImgLink = '<img src="' . $ImageFile . '" height="100" width="100" />';
} else {
$BrandImgLink = __('No Image');
}
}
$BrandImgLink = GetImageLink($ImageFile, '/BRAND-' . $SelectedBrand, 100, 100, "", "");
echo '<field><td colspan="2">' . $BrandImgLink . '</td></field>';
}
echo '</fieldset>
<div class="centre">
<input type="submit" name="submit" value="' . __('Enter Information') . '" />
</div>
</form>';
} //end if record deleted no point displaying form to add record
include(__DIR__ . '/includes/footer.php');