Completed
Push — master ( 8eaf12...df89b6 )
by Adam
02:13
created

Slug::enableOneTimeUpdate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 2
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * Slug.php
4
 *
5
 * @copyright      More in license.md
6
 * @license        http://www.ipublikuj.eu
7
 * @author         Adam Kadlec http://www.ipublikuj.eu
8
 * @package        iPublikuj:FormSlug!
9
 * @subpackage     Controls
10
 * @since          1.0.0
11
 *
12
 * @date           08.01.15
13
 */
14
15
declare(strict_types=1);
16
17
namespace IPub\FormSlug\Controls;
18
19
use Nette;
20
use Nette\Application\UI;
21
use Nette\Bridges;
22
use Nette\Forms;
23
use Nette\Utils;
24
25
use IPub;
26
use IPub\FormSlug;
27
use IPub\FormSlug\Exceptions;
28
29
/**
30
 * Form slug control element
31
 *
32
 * @package        iPublikuj:FormSlug!
33
 * @subpackage     Controls
34
 *
35
 * @author         Adam Kadlec <[email protected]>
36
 */
37 1
class Slug extends Forms\Controls\TextInput
38
{
39
	/**
40
	 * @var string|NULL
41
	 */
42
	private $templateFile;
43
44
	/**
45
	 * @var UI\ITemplate
46
	 */
47
	private $template;
48
49
	/**
50
	 * @var UI\ITemplateFactory
51
	 */
52
	private $templateFactory;
53
54
	/**
55
	 * @var Forms\Controls\BaseControl[]
56
	 */
57
	private $fields = [];
58
59
	/**
60
	 * Toggle box selector
61
	 *
62
	 * @var string
63
	 */
64
	private $toggleBox = '.ipub-slug-box';
65
66
	/**
67
	 * @var bool
68
	 */
69
	private static $registered = FALSE;
70
71
	/**
72
	 * Enable or disable one time auto updating slug field from watched fields
73
	 *
74
	 * @var bool
75
	 */
76
	private $onetimeAutoUpdate = TRUE;
77
78
	/**
79
	 * Enable or disable updating field when editing watched field
80
	 *
81
	 * @var bool
82
	 */
83
	private $forceEditUpdate = FALSE;
84
85
	/**
86
	 * @param UI\ITemplateFactory $templateFactory
87
	 * @param string|NULL $label
88
	 * @param int|NULL $maxLength
89
	 */
90
	public function __construct(UI\ITemplateFactory $templateFactory, string $label = NULL, int $maxLength = NULL)
91
	{
92 1
		parent::__construct($label, $maxLength);
93
94 1
		$this->templateFactory = $templateFactory;
95 1
	}
96
97
	/**
98
	 * Add filed from which slug will be created
99
	 *
100
	 * @param Forms\Controls\BaseControl $field
101
	 *
102
	 * @return self
103
	 */
104
	public function addField(Forms\Controls\BaseControl $field)
105
	{
106
		// Assign filed to collection
107
		$this->fields[$field->getHtmlId()] = $field;
108
109
		return $this;
110
	}
111
112
	/**
113
	 * @return self
114
	 */
115
	public function disableOneTimeUpdate()
116
	{
117
		$this->onetimeAutoUpdate = FALSE;
118
119
		return $this;
120
	}
121
122
	/**
123
	 * @return self
124
	 */
125
	public function enableOneTimeUpdate()
126
	{
127
		$this->onetimeAutoUpdate = TRUE;
128
129
		return $this;
130
	}
131
132
	/**
133
	 * @return self
134
	 */
135
	public function disableForceEditUpdate()
136
	{
137
		$this->forceEditUpdate = FALSE;
138
139
		return $this;
140
	}
141
142
	/**
143
	 * @return self
144
	 */
145
	public function enableForceEditUpdate()
146
	{
147
		$this->forceEditUpdate = TRUE;
148
149
		return $this;
150
	}
151
152
	/**
153
	 * Generates control's HTML element
154
	 */
155
	public function getControl() : Utils\Html
156
	{
157
		// Create form input
158 1
		$input = parent::getControl();
159
160 1
		$template = $this->getTemplate();
161
162
		// If template file was not defined before...
163 1
		if ($template->getFile() === NULL) {
164
			// ...try to get base control template file
165
			$templateFile = $this->getTemplateFile();
166
167
			// ...& set it to template engine
168
			$template->setFile($templateFile);
169
		}
170
171
		// Assign vars to template
172 1
		$template->input = $input;
173 1
		$template->value = $this->getValue();
174 1
		$template->caption = $this->caption;
175 1
		$template->_form = $this->getForm();
176
177
		// Component js settings
178 1
		$template->settings = [
179 1
			'toggle'    => $this->toggleBox,
180 1
			'onetime'   => $this->onetimeAutoUpdate,
181 1
			'forceEdit' => $this->forceEditUpdate,
182 1
			'fields'    => (array_reduce($this->fields, function (array $result, Forms\Controls\BaseControl $row) {
183
				$result[] = '#' . $row->getHtmlId();
184
185
				return $result;
186 1
			}, [])),
187
		];
188
189 1
		return Utils\Html::el()
190 1
			->addHtml((string) $template);
191
	}
192
193
	/**
194
	 * Change default control template path
195
	 *
196
	 * @param string $templateFile
197
	 *
198
	 * @return void
199
	 *
200
	 * @throws Exceptions\FileNotFoundException
201
	 */
202
	public function setTemplateFile(string $templateFile)
203
	{
204
		// Check if template file exists...
205
		if (!is_file($templateFile)) {
206
			// ...check if extension template is used
207
			if (is_file(__DIR__ . DIRECTORY_SEPARATOR . 'template' . DIRECTORY_SEPARATOR . $templateFile)) {
208
				$templateFile = __DIR__ . DIRECTORY_SEPARATOR . 'template' . DIRECTORY_SEPARATOR . $templateFile;
209
210
			} else {
211
				// ...if not throw exception
212
				throw new Exceptions\FileNotFoundException(sprintf('Template file "%s" was not found.', $templateFile));
213
			}
214
		}
215
216
		$this->templateFile = $templateFile;
217
	}
218
219
	/**
220
	 * @return string
221
	 */
222
	private function getTemplateFile() : string
223
	{
224 1
		return $this->templateFile !== NULL ? $this->templateFile : __DIR__ . DIRECTORY_SEPARATOR . 'template' . DIRECTORY_SEPARATOR . 'default.latte';
225
	}
226
227
	/**
228
	 * @return UI\ITemplate|Bridges\ApplicationLatte\Template
229
	 */
230
	private function getTemplate() : UI\ITemplate
231
	{
232 1
		if ($this->template === NULL) {
233 1
			$this->template = $this->templateFactory->createTemplate();
234 1
			$this->template->setFile($this->getTemplateFile());
235
		}
236
237 1
		return $this->template;
238
	}
239
240
	/**
241
	 * @param UI\ITemplateFactory $templateFactory
242
	 * @param string $method
243
	 *
244
	 * @return void
245
	 */
246
	public static function register(UI\ITemplateFactory $templateFactory, $method = 'addSlug')
247
	{
248
		// Check for multiple registration
249 1
		if (static::$registered) {
0 ignored issues
show
Bug introduced by
Since $registered is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $registered to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
250 1
			throw new Exceptions\InvalidStateException('Slug control already registered.');
251
		}
252
253 1
		static::$registered = TRUE;
0 ignored issues
show
Bug introduced by
Since $registered is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $registered to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
254
255 1
		$class = function_exists('get_called_class') ? get_called_class() : __CLASS__;
256 1
		Forms\Container::extensionMethod(
257 1
			$method, function (Forms\Container $form, $name, $label = NULL, $maxLength = NULL) use ($class, $templateFactory) {
258 1
			$component = new $class($templateFactory, $label, $maxLength);
259 1
			$form->addComponent($component, $name);
260
261 1
			return $component;
262 1
		}
263
		);
264 1
	}
265
}
266