1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Controllers\Ajax; |
4
|
|
|
|
5
|
|
|
use App\Models\TagModel; |
6
|
|
|
use Core\AjaxController; |
7
|
|
|
use Core\JsonException; |
8
|
|
|
|
9
|
|
|
class Tag extends AjaxController |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Update the tag via ajax |
15
|
|
|
* @throws \Core\JsonException |
16
|
|
|
*/ |
17
|
|
|
public function update() |
18
|
|
|
{ |
19
|
|
|
//security checks |
20
|
|
|
$this->onlyAdmin(); |
21
|
|
|
if (!$this->request->isPost()) { |
22
|
|
|
throw new JsonException('Call is not post'); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
//prepating our return results |
26
|
|
|
$result = array(); |
27
|
|
|
$tagUpdateJson = ($this->request->getData('tag-update')); |
28
|
|
|
$tagUpdate = json_decode($tagUpdateJson); |
29
|
|
|
|
30
|
|
|
//Converting our array of objects to simple array |
31
|
|
|
$send = array(); |
32
|
|
|
foreach ($tagUpdate as $item) { |
33
|
|
|
$send[$item->name] = $item->value; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
$tagModel = new TagModel($this->container); |
37
|
|
|
$result['success'] = $tagModel->update($send["idtags"], $send["tag_name"]); |
38
|
|
|
echo json_encode($result); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Delete a tag via Ajax |
43
|
|
|
* @throws \Core\JsonException |
44
|
|
|
*/ |
45
|
|
|
public function delete() |
46
|
|
|
{ |
47
|
|
|
//security checks |
48
|
|
|
$this->onlyAdmin(); |
49
|
|
|
if (!$this->request->isPost()) { |
50
|
|
|
throw new JsonException('Call is not post'); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
//prepating our return results |
54
|
|
|
$result = array(); |
55
|
|
|
$DeleteJson = ($this->request->getData('tag-delete')); |
56
|
|
|
$Delete = json_decode($DeleteJson); |
57
|
|
|
|
58
|
|
|
//Converting our array of objects to simple array |
59
|
|
|
$send = array(); |
60
|
|
|
foreach ($Delete as $item) { |
61
|
|
|
$send[$item->name] = $item->value; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$tagModel = new TagModel($this->container); |
65
|
|
|
$result['success'] = $tagModel->delete($send["idtags"]); |
66
|
|
|
echo json_encode($result); |
67
|
|
|
} |
68
|
|
|
} |