1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Controllers\Ajax; |
4
|
|
|
|
5
|
|
|
use App\Models\CategoryModel; |
6
|
|
|
use Core\AjaxController; |
7
|
|
|
use Core\Container; |
8
|
|
|
use Core\Traits\StringFunctions; |
9
|
|
|
|
10
|
|
|
class Category extends AjaxController |
11
|
|
|
{ |
12
|
|
|
use StringFunctions; |
|
|
|
|
13
|
|
|
|
14
|
|
|
protected $slug; |
15
|
|
|
|
16
|
|
|
public function __construct(Container $container) |
17
|
|
|
{ |
18
|
|
|
$this->loadModules[] = 'Slug'; |
19
|
|
|
parent::__construct($container); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Create a new category via Ajax |
24
|
|
|
* @throws \Core\JsonException |
25
|
|
|
*/ |
26
|
|
|
public function new() |
27
|
|
|
{ |
28
|
|
|
//security checks |
29
|
|
|
$this->onlyAdmin(); |
30
|
|
|
$this->onlyPost(); |
31
|
|
|
|
32
|
|
|
//preparing our return results |
33
|
|
|
$result = array(); |
34
|
|
|
$categoryUpdateJson = ($this->request->getData('category-new')); |
35
|
|
|
$categoryUpdate = json_decode($categoryUpdateJson); |
36
|
|
|
|
37
|
|
|
//Converting our array of objects to simple array |
38
|
|
|
$send = array(); |
39
|
|
|
foreach ($categoryUpdate as $item) { |
40
|
|
|
$send[$item->name] = $item->value; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
if(!$this->slug->isSlugValid($send["categories_slug"])) |
44
|
|
|
{ |
45
|
|
|
$result["success"] = false; |
46
|
|
|
$result["errorMessage"] = "Invalid Slug"; |
47
|
|
|
echo json_encode($result); |
48
|
|
|
die(); |
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$categoryModel = new CategoryModel($this->container); |
52
|
|
|
$result["success"] = $categoryModel->new($send["category_name"], $send["categories_slug"]); |
53
|
|
|
echo json_encode($result); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Update the category via ajax |
58
|
|
|
* @throws \Core\JsonException |
59
|
|
|
*/ |
60
|
|
|
public function update() |
61
|
|
|
{ |
62
|
|
|
//security checks |
63
|
|
|
$this->onlyAdmin(); |
64
|
|
|
$this->onlyPost(); |
65
|
|
|
|
66
|
|
|
//preparing our return results |
67
|
|
|
$result = array(); |
68
|
|
|
$categoryUpdateJson = ($this->request->getData('category-update')); |
69
|
|
|
$categoryUpdate = json_decode($categoryUpdateJson); |
70
|
|
|
|
71
|
|
|
//Converting our array of objects to simple array |
72
|
|
|
$send = array(); |
73
|
|
|
foreach ($categoryUpdate as $item) { |
74
|
|
|
$send[$item->name] = $item->value; |
75
|
|
|
} |
76
|
|
|
if(!$this->slug->isSlugValid($send["categories_slug"])) |
77
|
|
|
{ |
78
|
|
|
$result["success"] = false; |
79
|
|
|
$result["errorMessage"] = "Invalid Slug"; |
80
|
|
|
echo json_encode($result); |
81
|
|
|
die(); |
|
|
|
|
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
if(!$this->isInt($send["idcategories"])){ |
85
|
|
|
$result["success"] = false; |
86
|
|
|
$result["errorMessage"] = "Invalid ID"; |
87
|
|
|
echo json_encode($result); |
88
|
|
|
die(); |
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
|
92
|
|
|
$categoryModel = new CategoryModel($this->container); |
93
|
|
|
|
94
|
|
|
$result['success'] = $categoryModel->update($send["idcategories"], $send["category_name"], |
95
|
|
|
$send["categories_slug"]); |
96
|
|
|
echo json_encode($result); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Delete a category via Ajax |
101
|
|
|
* @throws \Core\JsonException |
102
|
|
|
*/ |
103
|
|
|
public function delete() |
104
|
|
|
{ |
105
|
|
|
//security checks |
106
|
|
|
$this->onlyAdmin(); |
107
|
|
|
$this->onlyPost(); |
108
|
|
|
|
109
|
|
|
//preparing our return results |
110
|
|
|
$result = array(); |
111
|
|
|
$categoryDeleteJson = ($this->request->getData('category-delete')); |
112
|
|
|
$categoryDelete = json_decode($categoryDeleteJson); |
113
|
|
|
|
114
|
|
|
//Converting our array of objects to simple array |
115
|
|
|
$send = array(); |
116
|
|
|
foreach ($categoryDelete as $item) { |
117
|
|
|
$send[$item->name] = $item->value; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
if(!$this->isInt($send["idcategories"])){ |
121
|
|
|
$result["success"] = false; |
122
|
|
|
$result["errorMessage"] = "Invalid ID"; |
123
|
|
|
echo json_encode($result); |
124
|
|
|
die(); |
|
|
|
|
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
$categoryModel = new CategoryModel($this->container); |
128
|
|
|
$result['success'] = $categoryModel->delete($send["idcategories"]); |
129
|
|
|
echo json_encode($result); |
130
|
|
|
} |
131
|
|
|
} |