Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 103 |
|
0.00% |
0 / 12 |
CRAP | |
0.00% |
0 / 1 |
tao_actions_SaSModule | |
0.00% |
0 / 103 |
|
0.00% |
0 / 12 |
1640 | |
0.00% |
0 / 1 |
setView | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
getRootClass | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getDataKind | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
sasSelect | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
6 | |||
sasSet | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
30 | |||
sasAddInstance | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
20 | |||
sasEditInstance | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
12 | |||
sasDeleteInstance | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
getCurrentClass | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
20 | |||
sasGetOntologyData | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
72 | |||
setVariables | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
20 | |||
isStandalone | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
20 |
1 | <?php |
2 | |
3 | /* |
4 | * This program is free software; you can redistribute it and/or |
5 | * modify it under the terms of the GNU General Public License |
6 | * as published by the Free Software Foundation; under version 2 |
7 | * of the License (non-upgradable). |
8 | * |
9 | * This program is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | * GNU General Public License for more details. |
13 | * |
14 | * You should have received a copy of the GNU General Public License |
15 | * along with this program; if not, write to the Free Software |
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
17 | * |
18 | * Copyright (c) 2002-2008 (original work) Public Research Centre Henri Tudor & University of Luxembourg |
19 | * (under the project TAO & TAO2); |
20 | * 2008-2010 (update and modification) Deutsche Institut für Internationale Pädagogische Forschung |
21 | * (under the project TAO-TRANSFER); |
22 | * 2009-2012 (update and modification) Public Research Centre Henri Tudor |
23 | * (under the project TAO-SUSTAIN & TAO-DEV); |
24 | * |
25 | */ |
26 | use oat\tao\model\GenerisTreeFactory; |
27 | use tao_helpers_form_FormContainer as FormContainer; |
28 | |
29 | /** |
30 | * This controller provides backward compatibility for legacy |
31 | * Stand-alone Services of the workflow engine |
32 | * |
33 | * @author CRP Henri Tudor - TAO Team - {@link http://www.tao.lu} |
34 | * @license GPLv2 http://www.opensource.org/licenses/gpl-2.0.php |
35 | * @package tao |
36 | |
37 | * |
38 | */ |
39 | abstract class tao_actions_SaSModule extends tao_actions_RdfController |
40 | { |
41 | /** |
42 | * Whenever or not the call was made in standAlone mode |
43 | * |
44 | * @var boolean |
45 | */ |
46 | private $isStandAlone; |
47 | |
48 | public function setView($path, $extensionID = null) |
49 | { |
50 | // override non AJAX calls for SAS |
51 | if (!$this->isStandAlone() || $this->isXmlHttpRequest()) { |
52 | parent::setView($path, $extensionID); |
53 | } else { |
54 | $this->setData('client_config_url', $this->getClientConfigUrl()); |
55 | $this->setData('includeTemplate', $path); |
56 | $this->setData('includeExtension', $extensionID); |
57 | parent::setView('sas.tpl', 'tao'); |
58 | } |
59 | } |
60 | |
61 | /** |
62 | * Returns the root class of the module |
63 | * @return core_kernel_classes_Class |
64 | */ |
65 | protected function getRootClass() |
66 | { |
67 | return $this->getClassService()->getRootClass(); |
68 | } |
69 | |
70 | protected function getDataKind() |
71 | { |
72 | return Camelizer::camelize(explode(' ', strtolower(trim($this->getRootClass()->getLabel()))), false); |
73 | } |
74 | |
75 | /** |
76 | * Service of class or instance selection with a tree. |
77 | * @return void |
78 | */ |
79 | public function sasSelect() |
80 | { |
81 | $context = Context::getInstance(); |
82 | $module = $context->getModuleName(); |
83 | |
84 | $this->setData('treeName', __('Select')); |
85 | $this->setData('dataUrl', _url('sasGetOntologyData')); |
86 | $this->setData('editClassUrl', tao_helpers_Uri::url('sasSet', $module)); |
87 | |
88 | if ($this->getRequestParameter('selectInstance') == 'true') { |
89 | $this->setData('editInstanceUrl', tao_helpers_Uri::url('sasSet', $module)); |
90 | $this->setData('editClassUrl', false); |
91 | } else { |
92 | $this->setData('editInstanceUrl', false); |
93 | $this->setData('editClassUrl', tao_helpers_Uri::url('sasSet', $module)); |
94 | } |
95 | |
96 | $this->setData('classLabel', $this->getRootClass()->getLabel()); |
97 | |
98 | $this->setView("sas/select.tpl", 'tao'); |
99 | } |
100 | |
101 | /** |
102 | * Save the uri or the classUri in parameter into the workflow engine by using the dedicated seervice |
103 | * @return void |
104 | */ |
105 | public function sasSet() |
106 | { |
107 | $message = __('Error'); |
108 | |
109 | //set the class uri |
110 | if ($this->hasRequestParameter('classUri')) { |
111 | $clazz = $this->getCurrentClass(); |
112 | if (!is_null($clazz)) { |
113 | $this->setVariables([$this->getDataKind() . 'ClassUri' => $clazz->getUri()]); |
114 | $message = $clazz->getLabel() . ' ' . __('class selected'); |
115 | } |
116 | } |
117 | |
118 | //set the instance uri |
119 | if ($this->hasRequestParameter('uri')) { |
120 | $instance = $this->getCurrentInstance(); |
121 | if (!is_null($instance)) { |
122 | $this->setVariables([$this->getDataKind() . 'Uri' => $instance->getUri()]); |
123 | $message = __('%s %s selected', $instance->getLabel(), $this->getDataKind()); |
124 | } |
125 | } |
126 | $this->setData('message', $message); |
127 | |
128 | //only for the notification |
129 | $this->setView('messages.tpl', 'tao'); |
130 | } |
131 | |
132 | /** |
133 | * Add a new instance |
134 | * @return void |
135 | */ |
136 | public function sasAddInstance() |
137 | { |
138 | try { |
139 | $clazz = $this->getCurrentClass(); |
140 | } catch (common_Exception $e) { |
141 | $clazz = $this->getRootClass(); |
142 | } |
143 | // @todo call the correct service |
144 | $instance = $this->getClassService()->createInstance($clazz); |
145 | if (!is_null($instance) && $instance instanceof core_kernel_classes_Resource) { |
146 | //init variable service: |
147 | $this->setVariables([$this->getDataKind() . 'Uri' => $instance->getUri()]); |
148 | |
149 | $params = [ |
150 | 'uri' => tao_helpers_Uri::encode($instance->getUri()), |
151 | 'classUri' => tao_helpers_Uri::encode($clazz->getUri()), |
152 | 'standalone' => $this->isStandAlone() |
153 | ]; |
154 | $this->redirect(_url('sasEditInstance', null, null, $params)); |
155 | } |
156 | } |
157 | |
158 | |
159 | /** |
160 | * Edit an instances |
161 | * @return void |
162 | */ |
163 | public function sasEditInstance() |
164 | { |
165 | $clazz = $this->getCurrentClass(); |
166 | $instance = $this->getCurrentInstance(); |
167 | |
168 | $formContainer = new tao_actions_form_Instance( |
169 | $clazz, |
170 | $instance, |
171 | [FormContainer::CSRF_PROTECTION_OPTION => true] |
172 | ); |
173 | $myForm = $formContainer->getForm(); |
174 | |
175 | if ($myForm->isSubmited() && $myForm->isValid()) { |
176 | $binder = new tao_models_classes_dataBinding_GenerisFormDataBinder($instance); |
177 | $instance = $binder->bind($myForm->getValues()); |
178 | $this->setData('message', __('Resource saved')); |
179 | } |
180 | |
181 | $this->setData('uri', tao_helpers_Uri::encode($instance->getUri())); |
182 | $this->setData('classUri', tao_helpers_Uri::encode($clazz->getUri())); |
183 | $this->setData('formTitle', __('Edit')); |
184 | $this->setData('myForm', $myForm->render()); |
185 | $this->setView('form.tpl', 'tao'); |
186 | } |
187 | |
188 | /** |
189 | * Delete an instance |
190 | * @return void |
191 | */ |
192 | public function sasDeleteInstance() |
193 | { |
194 | $clazz = $this->getCurrentClass(); |
195 | $instance = $this->getCurrentInstance(); |
196 | |
197 | $this->setData('label', $instance->getLabel()); |
198 | |
199 | $this->setData('uri', tao_helpers_Uri::encode($instance->getUri())); |
200 | $this->setData('classUri', tao_helpers_Uri::encode($clazz->getUri())); |
201 | $this->setView('sas/delete.tpl', 'tao'); |
202 | } |
203 | |
204 | // Below this line, basic functionalities copied from TaoModule |
205 | |
206 | /** |
207 | * get the current item class regarding the classUri' request parameter |
208 | * prevent exception by returning the root class if no class is selected |
209 | * |
210 | * @return core_kernel_classes_Class the item class |
211 | */ |
212 | protected function getCurrentClass() |
213 | { |
214 | $classUri = tao_helpers_Uri::decode($this->getRequestParameter('classUri')); |
215 | if ($this->isStandAlone() && (is_null($classUri) || empty($classUri))) { |
216 | return $this->getRootClass(); |
217 | } else { |
218 | return parent::getCurrentClass(); |
219 | } |
220 | } |
221 | |
222 | /** |
223 | * simplified Version of TaoModule function |
224 | * |
225 | * @return void |
226 | */ |
227 | public function sasGetOntologyData() |
228 | { |
229 | if (!$this->isXmlHttpRequest()) { |
230 | throw new common_exception_IsAjaxAction(__FUNCTION__); |
231 | } |
232 | |
233 | $showInstances = $this->hasRequestParameter('hideInstances') |
234 | ? !(bool)$this->getRequestParameter('hideInstances') |
235 | : true; |
236 | |
237 | $hideNode = $this->hasRequestParameter('classUri'); |
238 | $clazz = $this->hasRequestParameter('classUri') ? $this->getCurrentClass() : $this->getRootClass(); |
239 | |
240 | if ($this->hasRequestParameter('offset')) { |
241 | $options['offset'] = $this->getRequestParameter('offset'); |
242 | } |
243 | $limit = $this->hasRequestParameter('limit') ? $this->getRequestParameter('limit') : 0; |
244 | $offset = $this->hasRequestParameter('offset') ? $this->getRequestParameter('offset') : 0; |
245 | |
246 | $factory = new GenerisTreeFactory($showInstances, [$clazz->getUri()], $limit, $offset); |
247 | $tree = $factory->buildTree($clazz); |
248 | |
249 | $returnValue = $hideNode ? ($tree['children']) : $tree; |
250 | $this->returnJson($returnValue); |
251 | } |
252 | |
253 | protected function setVariables($variables) |
254 | { |
255 | $this->getServiceLocator()->get(common_ext_ExtensionsManager::SERVICE_ID)->getExtensionById('wfEngine')->load(); |
256 | |
257 | $variableService = wfEngine_models_classes_VariableService::singleton(); |
258 | |
259 | $cleaned = []; |
260 | foreach ($variables as $key => $value) { |
261 | $cleaned[$key] = (is_object($value) && $value instanceof core_kernel_classes_Resource) |
262 | ? $value->getUri() |
263 | : $value; |
264 | } |
265 | return $variableService->save($cleaned); |
266 | } |
267 | |
268 | /** |
269 | * Get the standAlone state |
270 | * |
271 | * @return bool |
272 | */ |
273 | protected function isStandalone() |
274 | { |
275 | if (is_null($this->isStandAlone)) { |
276 | if ($this->hasRequestParameter('standalone') && $this->getRequestParameter('standalone')) { |
277 | tao_helpers_Context::load('STANDALONE_MODE'); |
278 | $this->isStandAlone = true; |
279 | $this->setData('client_config_url', $this->getClientConfigUrl()); |
280 | $this->logDebug('Standalone mode set'); |
281 | } else { |
282 | $this->isStandAlone = false; |
283 | } |
284 | } |
285 | |
286 | return $this->isStandAlone; |
287 | } |
288 | } |