1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\Admin; |
4
|
|
|
|
5
|
|
|
use App\Http\Controllers\BasePageController; |
6
|
|
|
use App\Models\Category; |
7
|
|
|
use Blacklight\Regexes; |
8
|
|
|
use Illuminate\Http\Request; |
9
|
|
|
|
10
|
|
|
class AdminCategoryRegexesController extends BasePageController |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @throws \Exception |
14
|
|
|
*/ |
15
|
|
|
public function index(Request $request): void |
16
|
|
|
{ |
17
|
|
|
$this->setAdminPrefs(); |
18
|
|
|
$regexes = new Regexes(['Settings' => null, 'Table_Name' => 'category_regexes']); |
19
|
|
|
|
20
|
|
|
$meta_title = $title = 'Category Regex List'; |
21
|
|
|
|
22
|
|
|
$group = $request->has('group') && ! empty($request->input('group')) ? $request->input('group') : ''; |
23
|
|
|
$regex = $regexes->getRegex($group); |
24
|
|
|
|
25
|
|
|
$this->smarty->assign( |
26
|
|
|
[ |
27
|
|
|
'group' => $group, |
28
|
|
|
'regex' => $regex, |
29
|
|
|
] |
30
|
|
|
); |
31
|
|
|
|
32
|
|
|
$content = $this->smarty->fetch('category_regexes-list.tpl'); |
33
|
|
|
|
34
|
|
|
$this->smarty->assign(compact('title', 'meta_title', 'content')); |
35
|
|
|
|
36
|
|
|
$this->adminrender(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return \Illuminate\Http\RedirectResponse|void |
41
|
|
|
* |
42
|
|
|
* @throws \Exception |
43
|
|
|
*/ |
44
|
|
|
public function edit(Request $request) |
45
|
|
|
{ |
46
|
|
|
$this->setAdminPrefs(); |
47
|
|
|
$regexes = new Regexes(['Settings' => null, 'Table_Name' => 'category_regexes']); |
48
|
|
|
|
49
|
|
|
// Set the current action. |
50
|
|
|
$action = $request->input('action') ?? 'view'; |
51
|
|
|
|
52
|
|
|
$regex = [ |
53
|
|
|
'id' => '', |
54
|
|
|
'group_regex' => '', |
55
|
|
|
'regex' => '', |
56
|
|
|
'description' => '', |
57
|
|
|
'ordinal' => '', |
58
|
|
|
'categories_id' => '', |
59
|
|
|
'status' => 1, ]; |
60
|
|
|
|
61
|
|
|
$this->smarty->assign('regex', $regex); |
62
|
|
|
|
63
|
|
|
switch ($action) { |
64
|
|
|
case 'submit': |
65
|
|
|
if (empty($request->input('group_regex'))) { |
66
|
|
|
$this->smarty->assign('error', 'Group regex must not be empty!'); |
67
|
|
|
break; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
if (empty($request->input('regex'))) { |
71
|
|
|
$this->smarty->assign('error', 'Regex cannot be empty'); |
72
|
|
|
break; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
if (! is_numeric($request->input('ordinal')) || $request->input('ordinal') < 0) { |
76
|
|
|
$this->smarty->assign('error', 'Ordinal must be a number, 0 or higher.'); |
77
|
|
|
break; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if (empty($request->input('id'))) { |
81
|
|
|
$regexes->addRegex($request->all()); |
82
|
|
|
} else { |
83
|
|
|
$regexes->updateRegex($request->all()); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return redirect()->to('admin/category_regexes-list'); |
87
|
|
|
break; |
|
|
|
|
88
|
|
|
|
89
|
|
|
case 'view': |
90
|
|
|
default: |
91
|
|
|
if ($request->has('id')) { |
92
|
|
|
$meta_title = $title = 'Category Regex Edit'; |
93
|
|
|
$id = $request->input('id'); |
94
|
|
|
$regex = $regexes->getRegexByID($id); |
95
|
|
|
} else { |
96
|
|
|
$meta_title = $title = 'Category Regex Add'; |
97
|
|
|
} |
98
|
|
|
$this->smarty->assign('regex', $regex); |
99
|
|
|
break; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$this->smarty->assign('status_ids', [Category::STATUS_ACTIVE, Category::STATUS_INACTIVE]); |
103
|
|
|
$this->smarty->assign('status_names', ['Yes', 'No']); |
104
|
|
|
|
105
|
|
|
$categories_db = Category::query() |
106
|
|
|
->select(['c.id', 'c.title', 'cp.title as parent_title']) |
107
|
|
|
->from('categories as c') |
|
|
|
|
108
|
|
|
->join('root_categories as cp', 'c.root_categories_id', '=', 'cp.id') |
109
|
|
|
->whereNotNull('c.root_categories_id') |
110
|
|
|
->orderBy('c.id') |
|
|
|
|
111
|
|
|
->get(); |
112
|
|
|
// Build arrays for Smarty html_options helper. Previously only last row was kept. |
113
|
|
|
$category_ids = []; |
114
|
|
|
$category_names = []; |
115
|
|
|
foreach ($categories_db as $category_db) { |
116
|
|
|
$category_ids[] = $category_db->id; |
117
|
|
|
$category_names[] = $category_db->parent_title.' '.$category_db->title.': '.$category_db->id; |
118
|
|
|
} |
119
|
|
|
$this->smarty->assign('category_names', $category_names); |
120
|
|
|
$this->smarty->assign('category_ids', $category_ids); |
121
|
|
|
|
122
|
|
|
$content = $this->smarty->fetch('category_regexes-edit.tpl'); |
123
|
|
|
$this->smarty->assign(compact('title', 'meta_title', 'content')); |
124
|
|
|
$this->adminrender(); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
The
break
statement is not necessary if it is preceded for example by areturn
statement:If you would like to keep this construct to be consistent with other
case
statements, you can safely mark this issue as a false-positive.