forked from NextDom/nextdom-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataStoreAjax.php
124 lines (117 loc) · 4.01 KB
/
DataStoreAjax.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
/* This file is part of Jeedom.
*
* Jeedom is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Jeedom is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Jeedom. If not, see <http://www.gnu.org/licenses/>.
*/
namespace NextDom\Ajax;
use NextDom\Enums\AjaxParams;
use NextDom\Enums\Common;
use NextDom\Enums\NextDomObj;
use NextDom\Enums\UserRight;
use NextDom\Exceptions\CoreException;
use NextDom\Helpers\Utils;
use NextDom\Managers\DataStoreManager;
use NextDom\Model\Entity\Cmd;
use NextDom\Model\Entity\DataStore;
use NextDom\Model\Entity\EqLogic;
use NextDom\Model\Entity\InteractDef;
use NextDom\Model\Entity\Scenario;
/**
* Class DataStoreAjax
* @package NextDom\Ajax
*/
class DataStoreAjax extends BaseAjax
{
protected $NEEDED_RIGHTS = UserRight::USER;
protected $MUST_BE_CONNECTED = true;
protected $CHECK_AJAX_TOKEN = true;
/**
* Remove variable from the dataStore
* @throws CoreException
*/
public function remove()
{
$dataStoreId = Utils::initInt('id');
$dataStore = DataStoreManager::byId($dataStoreId);
if (!is_object($dataStore)) {
throw new CoreException(__('Dépôt de données inconnu. Vérifiez l\'ID : ') . $dataStoreId);
}
$dataStore->remove();
$this->ajax->success();
}
/**
* Save variable in the dataStore
*
* @throws CoreException
* @throws \ReflectionException
*/
public function save()
{
$dataStoreId = Utils::initInt('id', -1);
if ($dataStoreId <= 0) {
$dataStore = new DataStore();
$dataStore->setKey(Utils::init('key'));
$dataStore->setLink_id(Utils::init('link_id'));
$dataStore->setType(Utils::init('type'));
} else {
$dataStore = DataStoreManager::byId($dataStoreId);
}
if (!is_object($dataStore)) {
throw new CoreException(__('Dépôt de données inconnu. Vérifiez l\'ID : ') . $dataStoreId);
}
$dataStore->setValue(Utils::init('value'));
$dataStore->save();
$this->ajax->success();
}
/**
* Get all variables from the dataStore
* @throws \ReflectionException
*/
public function all()
{
$dataStores = DataStoreManager::byTypeLinkId(Utils::init('type'));
$result = [];
if (Utils::init(AjaxParams::USED_BY) == 1) {
$linkedObjectTypes = [
NextDomObj::SCENARIO => [],
NextDomObj::EQLOGIC => [],
NextDomObj::CMD => [],
NextDomObj::INTERACT_DEF => [],
];
/**
* Loop on all variables
*/
foreach ($dataStores as $datastore) {
$dataStoreInformations = Utils::o2a($datastore);
$dataStoreInformations[Common::USED_BY] = $linkedObjectTypes;
$usedBy = $datastore->getUsedBy();
/**
* Loop on all linked objects to the variable
*/
foreach (array_keys($linkedObjectTypes) as $objectType) {
/**
* @var Scenario|EqLogic|Cmd|InteractDef $objectToConvert
*/
foreach ($usedBy[$objectType] as $objectToConvert) {
$dataStoreInformations[Common::USED_BY][$objectType][] = $objectToConvert->getHumanName();
}
}
$result[] = $dataStoreInformations;
}
} else {
$result = Utils::o2a($dataStores);
}
$this->ajax->success($result);
}
}