Completed
Push — master ( 7659ff...13b4bf )
by Khoa
10:41
created

bbook_words::set_bbcode_options()   B

Complexity

Conditions 7
Paths 1

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 6
Code Lines 4

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 8.2222
cc 7
eloc 4
nc 1
nop 2
1
<?php
2
/**
3
* This file is part of the VinaBB.vn package.
4
*
5
* @copyright (c) VinaBB <vinabb.vn>
6
* @license GNU General Public License, version 2 (GPL-2.0)
7
*/
8
9
namespace vinabb\web\controllers\acp;
10
11
use Symfony\Component\DependencyInjection\ContainerInterface;
12
13
/**
14
* Controller for the bbook_words_module
15
*/
16
class bbook_words implements bbook_words_interface
17
{
18
	/** @var \vinabb\web\controllers\cache\service_interface $cache */
19
	protected $cache;
20
21
	/** @var ContainerInterface $container */
22
	protected $container;
23
24
	/** @var \phpbb\language\language $language */
25
	protected $language;
26
27
	/** @var \phpbb\log\log $log */
28
	protected $log;
29
30
	/** @var \vinabb\web\operators\bbook_word_interface $operator */
31
	protected $operator;
32
33
	/** @var \phpbb\request\request $request */
34
	protected $request;
35
36
	/** @var \phpbb\template\template $template */
37
	protected $template;
38
39
	/** @var \phpbb\user $user */
40
	protected $user;
41
42
	/** @var string $root_path */
43
	protected $root_path;
44
45
	/** @var string $php_ext */
46
	protected $php_ext;
47
48
	/** @var string $u_action */
49
	protected $u_action;
50
51
	/** @var string $book_lang */
52
	protected $book_lang;
53
54
	/** @var array $data */
55
	protected $data;
56
57
	/** @var array $errors */
58
	protected $errors;
59
60
	/** @var string $ext_root_path */
61
	protected $ext_root_path;
62
63
	/** @var array $lang_data */
64
	protected $lang_data;
65
66
	/**
67
	* Constructor
68
	*
69
	* @param \vinabb\web\controllers\cache\service_interface	$cache			Cache service
70
	* @param ContainerInterface									$container		Container object
71
	* @param \phpbb\language\language							$language		Language object
72
	* @param \phpbb\log\log										$log			Log object
73
	* @param \vinabb\web\operators\bbook_word_interface			$operator		Word operators
74
	* @param \phpbb\request\request								$request		Request object
75
	* @param \phpbb\template\template							$template		Template object
76
	* @param \phpbb\user										$user			User object
77
	* @param string												$root_path		phpBB root path
78
	* @param string												$php_ext		PHP file extension
79
	*/
80
	public function __construct(
81
		\vinabb\web\controllers\cache\service_interface $cache,
82
		ContainerInterface $container,
83
		\phpbb\language\language $language,
84
		\phpbb\log\log $log,
85
		\vinabb\web\operators\bbook_word_interface $operator,
86
		\phpbb\request\request $request,
87
		\phpbb\template\template $template,
88
		\phpbb\user $user,
89
		$root_path,
90
		$php_ext
91
	)
92
	{
93
		$this->cache = $cache;
94
		$this->container = $container;
95
		$this->language = $language;
96
		$this->log = $log;
97
		$this->operator = $operator;
98
		$this->request = $request;
99
		$this->template = $template;
100
		$this->user = $user;
101
		$this->root_path = $root_path;
102
		$this->php_ext = $php_ext;
103
104
		$this->lang_data = $this->cache->get_lang_data();
105
	}
106
107
	/**
108
	* Set form data
109
	*
110
	* @param array $data Form data
111
	*/
112
	public function set_form_data($data)
0 ignored issues
show
Coding Style introduced by Khoa Nguyen
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
113
	{
114
		$this->word_lang = $data['word_lang'];
0 ignored issues
show
Bug introduced by Khoa Nguyen
The property word_lang does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
115
		$this->u_action = $data['u_action'] . "&lang={$this->word_lang}";
0 ignored issues
show
Coding Style Best Practice introduced by Khoa Nguyen
As per coding-style, please use concatenation or sprintf for the variable $this instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
116
	}
117
118
	/**
119
	* Language selection
120
	*/
121
	public function select_lang()
0 ignored issues
show
Coding Style introduced by Khoa Nguyen
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
122
	{
123
		if (count($this->lang_data) > 1)
124
		{
125
			foreach ($this->lang_data as $lang_iso => $lang_data)
126
			{
127
				$this->template->assign_block_vars('lang_options', [
128
					'VALUE'	=> $lang_iso,
129
					'NAME'	=> ($lang_data['english_name'] == $lang_data['local_name']) ? $lang_data['english_name'] : $lang_data['english_name'] . ' (' . $lang_data['local_name'] . ')'
130
				]);
131
			}
132
133
			$this->template->assign_var('U_ACTION', $this->u_action);
134
		}
135
		// If there is only one available language, we do not need the selection list
136
		else
137
		{
138
			$this->display_words();
139
		}
140
	}
141
142
	/**
143
	* Display words
144
	*/
145
	public function display_words()
0 ignored issues
show
Coding Style introduced by Khoa Nguyen
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
146
	{
147
		// Grab all from database
148
		$entities = $this->operator->get_words($this->word_lang);
149
150
		/** @var \vinabb\web\entities\bbook_word_interface $entity */
151
		foreach ($entities as $entity)
152
		{
153
			$this->template->assign_block_vars('words', [
154
				'WORD'	=> $entity->get_word(),
155
				'DESC'	=> $entity->get_desc(),
156
				'URL'	=> $entity->get_url(),
157
158
				'U_EDIT'	=> "{$this->u_action}&action=edit&id={$entity->get_id()}",
0 ignored issues
show
Coding Style Best Practice introduced by Khoa Nguyen
As per coding-style, please use concatenation or sprintf for the variable $this instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
Coding Style Best Practice introduced by Khoa Nguyen
As per coding-style, please use concatenation or sprintf for the variable $entity instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
159
				'U_DELETE'	=> "{$this->u_action}&action=delete&id={$entity->get_id()}"
0 ignored issues
show
Coding Style Best Practice introduced by Khoa Nguyen
As per coding-style, please use concatenation or sprintf for the variable $this instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
Coding Style Best Practice introduced by Khoa Nguyen
As per coding-style, please use concatenation or sprintf for the variable $entity instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
160
			]);
161
		}
162
163
		$this->template->assign_vars([
164
			'LANG_NAME'	=> $this->lang_data[$this->word_lang]['local_name'],
165
166
			'U_ACTION'	=> "{$this->u_action}&action=add"
0 ignored issues
show
Coding Style Best Practice introduced by Khoa Nguyen
As per coding-style, please use concatenation or sprintf for the variable $this instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
167
		]);
168
	}
169
170
	/**
171
	* Add a word
172
	*/
173
	public function add_word()
0 ignored issues
show
Coding Style introduced by Khoa Nguyen
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
174
	{
175
		// Initiate an entity
176
		/** @var \vinabb\web\entities\bbook_word_interface $entity */
177
		$entity = $this->container->get('vinabb.web.entities.bbook_words');
178
179
		// Process the new entity
180
		$this->add_edit_data($entity);
181
182
		$this->template->assign_vars([
183
			'S_ADD'		=> true,
184
185
			'U_ACTION'	=> "{$this->u_action}&action=add",
0 ignored issues
show
Coding Style Best Practice introduced by Khoa Nguyen
As per coding-style, please use concatenation or sprintf for the variable $this instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
186
			'U_BACK'	=> $this->u_action
187
		]);
188
	}
189
190
	/**
191
	* Edit a word
192
	*
193
	* @param int $word_id Word ID
194
	*/
195
	public function edit_word($word_id)
0 ignored issues
show
Coding Style introduced by Khoa Nguyen
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
196
	{
197
		// Initiate and load the entity
198
		/** @var \vinabb\web\entities\bbook_word_interface $entity */
199
		$entity = $this->container->get('vinabb.web.entities.bbook_word')->load($word_id);
200
201
		// Process the edited entity
202
		$this->add_edit_data($entity);
203
204
		$this->template->assign_vars([
205
			'S_EDIT'	=> true,
206
207
			'U_ACTION'	=> "{$this->u_action}&action=edit&id={$word_id}",
0 ignored issues
show
Coding Style Best Practice introduced by Khoa Nguyen
As per coding-style, please use concatenation or sprintf for the variable $this instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
Coding Style Best Practice introduced by Khoa Nguyen
As per coding-style, please use concatenation or sprintf for the variable $word_id instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
208
			'U_BACK'	=> $this->u_action
209
		]);
210
	}
211
212
	/**
213
	* Process data to be added or edited
214
	*
215
	* @param \vinabb\web\entities\bbook_word_interface $entity Word entity
216
	*/
217
	public function add_edit_data(\vinabb\web\entities\bbook_word_interface $entity)
0 ignored issues
show
Coding Style introduced by Khoa Nguyen
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
218
	{
219
		$submit = $this->request->is_set_post('submit');
220
221
		// Create a form key for preventing CSRF attacks
222
		add_form_key('acp_bbook_words');
223
224
		// Get form data
225
		$this->request_data();
226
227
		// Set the parse options to the entity
228
		$this->set_bbcode_options($entity, $submit);
229
230
		if ($submit)
231
		{
232
			// Test if the submitted form is valid
233
			if (!check_form_key('acp_bbook_words'))
234
			{
235
				$this->errors[] = $this->language->lang('FORM_INVALID');
236
			}
237
238
			// Map and set data to the entity
239
			$this->map_set_data($entity);
240
241
			// Insert or update
242
			if (empty($this->errors))
243
			{
244
				$this->save_data($entity);
245
			}
246
		}
247
248
		// Output
249
		$this->data_to_tpl($entity);
250
251
		$this->template->assign_vars([
252
			'LANG_NAME'	=> $this->lang_data[$this->word_lang]['local_name'],
253
			'ERRORS'	=> !empty($this->errors) ? implode('<br>', $this->errors) : ''
254
		]);
255
256
		// Custom BBCode
257
		include_once "{$this->root_path}includes/functions_display.{$this->php_ext}";
0 ignored issues
show
Coding Style Best Practice introduced by Khoa Nguyen
As per coding-style, please use concatenation or sprintf for the variable $this instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
258
		display_custom_bbcodes();
259
	}
260
261
	/**
262
	* Request data from the form
263
	*/
264
	protected function request_data()
0 ignored issues
show
Coding Style introduced by Khoa Nguyen
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
265
	{
266
		$this->data = [
267
			'word'			=> $this->request->variable('word', '', true),
268
			'word_desc'		=> $this->request->variable('word_desc', '', true),
269
			'desc_bbcode'	=> $this->request->variable('desc_bbcode', true),
270
			'desc_urls'		=> $this->request->variable('desc_urls', true),
271
			'desc_smilies'	=> $this->request->variable('desc_smilies', true),
272
			'word_url'		=> $this->request->variable('word_url', '')
273
		];
274
	}
275
276
	/**
277
	* Grab the form data's parsing options and set them to the entity
278
	*
279
	* If submit, use data from the form
280
	* In edit mode, use data stored in the entity
281
	* In add mode, use default values
282
	*
283
	* @param \vinabb\web\entities\bbook_word_interface $entity Word entity
284
	*/
285
	protected function set_bbcode_options(\vinabb\web\entities\bbook_word_interface $entity, $submit)
0 ignored issues
show
Coding Style introduced by Khoa Nguyen
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
286
	{
287
		$entity->desc_enable_bbcode($submit ? $this->request->is_set_post('desc_bbcode') : ($entity->get_id() ? $entity->desc_bbcode_enabled() : true));
288
		$entity->desc_enable_urls($submit ? $this->request->is_set_post('desc_urls') : ($entity->get_id() ? $entity->desc_urls_enabled() : true));
289
		$entity->desc_enable_smilies($submit ? $this->request->is_set_post('desc_smilies') : ($entity->get_id() ? $entity->desc_smilies_enabled() : true));
290
	}
291
292
	/**
293
	* Map the form data fields to setters and set them to the entity
294
	*
295
	* @param \vinabb\web\entities\bbook_word_interface $entity Word entity
296
	*/
297
	protected function map_set_data(\vinabb\web\entities\bbook_word_interface $entity)
0 ignored issues
show
Coding Style introduced by Khoa Nguyen
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
298
	{
299
		$map_fields = [
300
			'set_word'		=> $this->data['word'],
301
			'set_lang'		=> $this->word_lang,
302
			'set_desc'		=> $this->data['word_desc'],
303
			'set_url'		=> $this->data['word_url']
304
		];
305
306
		// Set the mapped data in the entity
307
		foreach ($map_fields as $entity_function => $word_data)
308
		{
309
			try
310
			{
311
				// Calling the $entity_function on the entity and passing it $word_data
312
				$entity->$entity_function($word_data);
313
			}
314
			catch (\vinabb\web\exceptions\base $e)
315
			{
316
				$this->errors[] = $e->get_friendly_message($this->language);
317
			}
318
		}
319
320
		unset($map_fields);
321
	}
322
323
	/**
324
	* Insert or update data, then log actions and clear cache if needed
325
	*
326
	* @param \vinabb\web\entities\bbook_word_interface $entity Word entity
327
	*/
328
	protected function save_data(\vinabb\web\entities\bbook_word_interface $entity)
0 ignored issues
show
Coding Style introduced by Khoa Nguyen
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
329
	{
330
		if ($entity->get_id())
331
		{
332
			// Save the edited entity to the database
333
			$entity->save();
334
335
			$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_BBOOK_WORD_EDIT', time(), [$entity->get_word()]);
336
337
			$message = 'MESSAGE_WORD_EDIT';
338
		}
339
		else
340
		{
341
			// Add the new entity to the database
342
			$entity = $this->operator->add_word($entity);
343
344
			$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_BBOOK_WORD_ADD', time(), [$entity->get_word()]);
345
346
			$message = 'MESSAGE_WORD_ADD';
347
		}
348
349
		trigger_error($this->language->lang($message) . adm_back_link($this->u_action));
350
	}
351
352
	/**
353
	* Output entity data to template variables
354
	*
355
	* @param \vinabb\web\entities\bbook_word_interface $entity Word entity
356
	*/
357
	protected function data_to_tpl(\vinabb\web\entities\bbook_word_interface $entity)
0 ignored issues
show
Coding Style introduced by Khoa Nguyen
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
358
	{
359
		$this->template->assign_vars([
360
			'WORD'				=> $entity->get_word(),
361
			'WORD_DESC'			=> $entity->get_desc_for_edit(),
362
			'WORD_DESC_BBCODE'	=> $entity->desc_bbcode_enabled(),
363
			'WORD_DESC_URLS'	=> $entity->desc_urls_enabled(),
364
			'WORD_DESC_SMILIES'	=> $entity->desc_smilies_enabled(),
365
			'URL'				=> $entity->get_url()
366
		]);
367
	}
368
369
	/**
370
	* Delete a word
371
	*
372
	* @param int $word_id Word ID
373
	*/
374
	public function delete_word($word_id)
0 ignored issues
show
Coding Style introduced by Khoa Nguyen
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
375
	{
376
		/** @var \vinabb\web\entities\bbook_word_interface $entity */
377
		$entity = $this->container->get('vinabb.web.entities.bbook_word')->load($word_id);
378
379
		try
380
		{
381
			$this->operator->delete_word($word_id);
382
		}
383
		catch (\vinabb\web\exceptions\base $e)
384
		{
385
			trigger_error($this->language->lang('ERROR_WORD_DELETE', $e->get_message($this->language)) . adm_back_link($this->u_action), E_USER_WARNING);
386
		}
387
388
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_BBOOK_WORD_DELETE', time(), [$entity->get_word()]);
389
390
		// If AJAX was used, show user a result message
391
		if ($this->request->is_ajax())
392
		{
393
			$json_response = new \phpbb\json_response;
394
			$json_response->send([
395
				'MESSAGE_TITLE'	=> $this->language->lang('INFORMATION'),
396
				'MESSAGE_TEXT'	=> $this->language->lang('MESSAGE_WORD_DELETE'),
397
				'REFRESH_DATA'	=> ['time'	=> 3]
398
			]);
399
		}
400
	}
401
}
402