Category::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
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
    private $categoryModel;
17
18
    public function __construct(Container $container)
19
    {
20
        $this->loadModules[] = 'Slug';
21
        parent::__construct($container);
22
23
        $this->categoryModel = new CategoryModel($this->container);
24
    }
25
26
    /**
27
     * Create a new category via Ajax
28
     * @throws \Core\JsonException
29
     */
30
    public function new()
31
    {
32
        //security checks
33
        $this->onlyAdmin();
34
        $this->onlyPost();
35
36
        //preparing our return results
37
        $result = array();
38
        $categoryUpdateJson = ($this->request->getData('category-new'));
39
        $categoryUpdate = json_decode($categoryUpdateJson);
40
41
        //Converting our array of objects to simple array
42
        $send = array();
43
        foreach ($categoryUpdate as $item) {
44
            $send[$item->name] = $item->value;
45
        }
46
47
        if (!$this->slug->isSlugValid($send["categories_slug"])) {
48
            $result["success"] = false;
49
            $result["errorMessage"] = "Invalid Slug";
50
            echo json_encode($result);
51
            die();
52
        }
53
54
        if(!$this->categoryModel->isCategorySlugUnique($send["categories_slug"]))
55
        {
56
            $result["success"] = false;
57
            $result["errorMessage"] = "Slug is not unique";
58
            echo json_encode($result);
59
            die();
60
        }
61
62
63
        $result["success"] = $this->categoryModel->new($send["category_name"], $send["categories_slug"]);
64
        echo json_encode($result);
65
    }
66
67
    /**
68
     * Update the category via ajax
69
     * @throws \Core\JsonException
70
     */
71
    public function update()
72
    {
73
        //security checks
74
        $this->onlyAdmin();
75
        $this->onlyPost();
76
77
        //preparing our return results
78
        $result = array();
79
        $categoryUpdateJson = ($this->request->getData('category-update'));
80
        $categoryUpdate = json_decode($categoryUpdateJson);
81
82
        //Converting our array of objects to simple array
83
        $send = array();
84
        foreach ($categoryUpdate as $item) {
85
            $send[$item->name] = $item->value;
86
        }
87
        if (!$this->slug->isSlugValid($send["categories_slug"])) {
88
            $result["success"] = false;
89
            $result["errorMessage"] = "Invalid Slug";
90
            echo json_encode($result);
91
            die();
92
        }
93
94
        if (!$this->isInt($send["idcategories"])) {
95
            $result["success"] = false;
96
            $result["errorMessage"] = "Invalid ID";
97
            echo json_encode($result);
98
            die();
99
        }
100
101
        $result['success'] = $this->categoryModel->update($send["idcategories"], $send["category_name"],
102
            $send["categories_slug"]);
103
        echo json_encode($result);
104
    }
105
106
    /**
107
     * Delete a category via Ajax
108
     * @throws \Core\JsonException
109
     */
110
    public function delete()
111
    {
112
        //security checks
113
        $this->onlyAdmin();
114
        $this->onlyPost();
115
116
        //preparing our return results
117
        $result = array();
118
        $categoryDeleteJson = ($this->request->getData('category-delete'));
119
        $categoryDelete = json_decode($categoryDeleteJson);
120
121
        //Converting our array of objects to simple array
122
        $send = array();
123
        foreach ($categoryDelete as $item) {
124
            $send[$item->name] = $item->value;
125
        }
126
127
        if (!$this->isInt($send["idcategories"])) {
128
            $result["success"] = false;
129
            $result["errorMessage"] = "Invalid ID";
130
            echo json_encode($result);
131
            die();
132
        }
133
134
        $result['success'] = $this->categoryModel->delete($send["idcategories"]);
135
        echo json_encode($result);
136
    }
137
}