Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 45
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
tao_actions_form_EditClassLabel
0.00% covered (danger)
0.00%
0 / 45
0.00% covered (danger)
0.00%
0 / 6
132
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 getClassInstance
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 initForm
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
 initElements
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 1
20
 addSignature
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
2
 getNamespaceHelper
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
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) 2018 (original work) Open Assessment Technologies SA;
19 *
20 */
21
22use oat\generis\model\OntologyRdfs;
23use oat\tao\helpers\form\validators\ResourceSignatureValidator;
24use oat\tao\model\security\SignatureValidator;
25use oat\tao\helpers\NamespaceHelper;
26use oat\oatbox\service\ServiceManager;
27
28/**
29 *
30 * This form let's you edit the label of a class, only.
31 *
32 * @author Bertrand Chevrier, <bertrand@taotesting.com>
33 */
34class tao_actions_form_EditClassLabel extends \tao_helpers_form_FormContainer
35{
36    /**
37     * @var core_kernel_classes_Class
38     */
39    protected $clazz;
40    /**
41     * @var string
42     */
43    private $signature;
44
45
46    /**
47     * @param core_kernel_classes_Class $clazz
48     * @param array $classData
49     * @param string $signature
50     * @param array $options
51     */
52    public function __construct(\core_kernel_classes_Class $clazz, $classData, $signature, $options = [])
53    {
54        $this->clazz = $clazz;
55        $this->signature = $signature;
56
57        parent::__construct($classData, $options);
58    }
59
60    /**
61     * Class instance being authored
62     *
63     * @return core_kernel_classes_Class
64     */
65    protected function getClassInstance()
66    {
67        return $this->clazz;
68    }
69
70    /**
71     * Initialize the form
72     *
73     * @access protected
74     * @author Bertrand Chevrier, <bertrand.chevrier@tudor.lu>
75     * @return mixed
76     */
77    protected function initForm()
78    {
79        (isset($this->options['name'])) ? $name = $this->options['name'] : $name = '';
80        if (empty($name)) {
81            $name = 'form_' . (count(self::$forms) + 1);
82        }
83        unset($this->options['name']);
84
85        $this->form = tao_helpers_form_FormFactory::getForm($name, $this->options);
86
87
88        $this->form->setActions(tao_helpers_form_FormFactory::getCommonActions(), 'bottom');
89    }
90
91    /**
92     * Initialize the form elements
93     *
94     * @access protected
95     * @author Bertrand Chevrier, <bertrand.chevrier@tudor.lu>
96     * @return mixed
97     */
98    protected function initElements()
99    {
100        $clazz = $this->getClassInstance();
101
102        $labelProp = new \core_kernel_classes_Property(OntologyRdfs::RDFS_LABEL);
103        //map properties widgets to form elements
104        $element = \tao_helpers_form_GenerisFormFactory::elementMap($labelProp);
105        if (!is_null($element)) {
106            $value = $clazz->getLabel();
107            if (!is_null($value)) {
108                $element->setValue($value);
109            }
110            //set label validator
111            $element->addValidators([
112                tao_helpers_form_FormFactory::getValidator('NotEmpty'),
113            ]);
114            $namespace = substr($clazz->getUri(), 0, strpos($clazz->getUri(), '#'));
115            if (!$this->getNamespaceHelper()->isNamespaceSupported($namespace)) {
116                $readonly = tao_helpers_form_FormFactory::getElement('', 'Readonly');
117                $readonly->disable();
118                $readonly->setDescription($element->getDescription());
119                $readonly->setValue($element->getRawValue());
120                $element = $readonly;
121            }
122            $element->addClass('global');
123            $this->form->addElement($element);
124        }
125
126        //add an hidden elt for the class uri
127        $classUriElt = tao_helpers_form_FormFactory::getElement('classUri', 'Hidden');
128        $classUriElt->setValue(\tao_helpers_Uri::encode($clazz->getUri()));
129        $classUriElt->addClass('global');
130        $this->form->addElement($classUriElt);
131
132        $this->addSignature();
133    }
134
135    /**
136     * @throws \common_Exception
137     */
138    protected function addSignature()
139    {
140        $signature = tao_helpers_form_FormFactory::getElement('signature', 'Hidden');
141
142        $signature->setValue($this->signature);
143        $signature->addValidator(
144            new ResourceSignatureValidator(
145                new SignatureValidator(),
146                tao_helpers_Uri::encode($this->clazz->getUri())
147            )
148        );
149
150        $this->form->addElement($signature, true);
151    }
152
153    private function getNamespaceHelper(): NamespaceHelper
154    {
155        return ServiceManager::getServiceManager()->get(NamespaceHelper::SERVICE_ID);
156    }
157}