1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Controllers\Ajax; |
4
|
|
|
|
5
|
|
|
use App\Models\CategoryModel; |
6
|
|
|
use Core\AjaxController; |
7
|
|
|
use Core\JsonException; |
8
|
|
|
|
9
|
|
|
class Category extends AjaxController |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Create a new category via Ajax |
14
|
|
|
* @throws \Core\JsonException |
15
|
|
|
*/ |
16
|
|
|
public function new() |
17
|
|
|
{ |
18
|
|
|
//security checks |
19
|
|
|
$this->onlyAdmin(); |
20
|
|
|
if (!$this->container->getRequest()->isPost()) { |
21
|
|
|
throw new JsonException('Call is not post'); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
//prepating our return results |
25
|
|
|
$result = array(); |
26
|
|
|
$categoryUpdateJson = ($this->request->getData('category-new')); |
27
|
|
|
$categoryUpdate = json_decode($categoryUpdateJson); |
28
|
|
|
|
29
|
|
|
//Converting our array of objects to simple array |
30
|
|
|
$send = array(); |
31
|
|
|
foreach ($categoryUpdate as $item) { |
32
|
|
|
$send[$item->name] = $item->value; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
$categoryModel = new CategoryModel($this->container); |
36
|
|
|
$result['success'] = $categoryModel->new($send["category_name"], $send["categories_slug"]); |
37
|
|
|
echo json_encode($result); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Update the category via ajax |
42
|
|
|
* @throws \Core\JsonException |
43
|
|
|
*/ |
44
|
|
|
public function update() |
45
|
|
|
{ |
46
|
|
|
//security checks |
47
|
|
|
$this->onlyAdmin(); |
48
|
|
|
if (!$this->container->getRequest()->isPost()) { |
49
|
|
|
throw new JsonException('Call is not post'); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
//prepating our return results |
53
|
|
|
$result = array(); |
54
|
|
|
$categoryUpdateJson = ($this->request->getData('category-update')); |
55
|
|
|
$categoryUpdate = json_decode($categoryUpdateJson); |
56
|
|
|
|
57
|
|
|
//Converting our array of objects to simple array |
58
|
|
|
$send = array(); |
59
|
|
|
foreach ($categoryUpdate as $item) { |
60
|
|
|
$send[$item->name] = $item->value; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$categoryModel = new CategoryModel($this->container); |
64
|
|
|
$result['success'] = $categoryModel->update($send["idcategories"], $send["category_name"], |
65
|
|
|
$send["categories_slug"]); |
66
|
|
|
echo json_encode($result); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Delete a category via Ajax |
71
|
|
|
* @throws \Core\JsonException |
72
|
|
|
*/ |
73
|
|
|
public function delete() |
74
|
|
|
{ |
75
|
|
|
//security checks |
76
|
|
|
$this->onlyAdmin(); |
77
|
|
|
if (!$this->container->getRequest()->isPost()) { |
78
|
|
|
throw new JsonException('Call is not post'); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
//prepating our return results |
82
|
|
|
$result = array(); |
83
|
|
|
$categoryDeleteJson = ($this->request->getData('category-delete')); |
84
|
|
|
$categoryDelete = json_decode($categoryDeleteJson); |
85
|
|
|
|
86
|
|
|
//Converting our array of objects to simple array |
87
|
|
|
$send = array(); |
88
|
|
|
foreach ($categoryDelete as $item) { |
89
|
|
|
$send[$item->name] = $item->value; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$categoryModel = new CategoryModel($this->container); |
93
|
|
|
$result['success'] = $categoryModel->delete($send["idcategories"]); |
94
|
|
|
echo json_encode($result); |
95
|
|
|
} |
96
|
|
|
} |