save   A
last analyzed

Complexity

Total Complexity 28

Size/Duplication

Total Lines 386
Duplicated Lines 0 %

Test Coverage

Coverage 95.88%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 152
c 2
b 0
f 0
dl 0
loc 386
ccs 163
cts 170
cp 0.9588
rs 10
wmc 28

14 Methods

Rating   Name   Duplication   Size   Complexity  
A get_field_props() 0 13 1
A ensure_content_has_fields() 0 5 2
A get_field_types() 0 9 2
A __construct() 0 17 1
A copy_forum_permissions() 0 9 3
A handle_content_type() 0 23 2
A ensure_content_name_is_unique() 0 5 3
A create_content_forum() 0 12 1
A show_results() 0 26 4
A get_unsaved_entity() 0 28 1
A handle_langname_change() 0 7 2
A get_existing_field_ids() 0 14 2
A handle_content_fields() 0 27 3
A execute() 0 32 1
1
<?php
2
/**
3
 *
4
 * @package sitemaker
5
 * @copyright (c) 2016 Daniel A. (blitze)
6
 * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
7
 *
8
 */
9
10
namespace blitze\content\services\actions\type;
11
12
use blitze\content\services\actions\action_utils;
13
use blitze\content\services\actions\action_interface;
14
15
class save extends action_utils implements action_interface
16
{
17
	/** @var \phpbb\auth\auth */
18
	protected $auth;
19
20
	/** @var \phpbb\cache\driver\driver_interface */
21
	protected $cache;
22
23
	/** @var \phpbb\config\config */
24
	protected $config;
25
26
	/** @var \phpbb\db\driver\driver_interface */
27
	protected $db;
28
29
	/** @var \phpbb\event\dispatcher_interface */
30
	protected $phpbb_dispatcher;
31
32
	/** @var \phpbb\language\language */
33
	protected $language;
34
35
	/** @var \phpbb\log\log_interface */
36
	protected $logger;
37
38
	/** @var \phpbb\request\request_interface */
39
	protected $request;
40
41
	/** @var \phpbb\user */
42
	protected $user;
43
44
	/** @var \blitze\content\services\types */
45
	protected $content_types;
46
47
	/** @var \blitze\sitemaker\services\forum\manager */
0 ignored issues
show
Bug introduced by
The type blitze\sitemaker\services\forum\manager was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
48
	protected $forum_manager;
49
50
	/** @var \blitze\content\model\mapper_factory */
51
	protected $mapper_factory;
52
53
	/** @var string */
54
	protected $phpbb_admin_path;
55
56
	/** @var string */
57
	protected $php_ext;
58
59
	/** @var bool */
60
	protected $auto_refresh;
61
62
	/**
63
	 * Constructor
64
	 *
65
	 * @param \phpbb\auth\auth							$auth					Auth object
66
	 * @param \phpbb\cache\driver\driver_interface		$cache					Cache object
67
	 * @param \phpbb\config\config						$config					Config object
68
	 * @param \phpbb\db\driver\driver_interface			$db						Database object
69
	 * @param \phpbb\event\dispatcher_interface			$phpbb_dispatcher		Event dispatcher object
70
	 * @param \phpbb\language\language					$language				Language Object
71
	 * @param \phpbb\log\log_interface					$logger					phpBB logger
72
	 * @param \phpbb\request\request_interface			$request				Template object
73
	 * @param \phpbb\user								$user					User object
74
	 * @param \blitze\content\services\types			$content_types			Content types object
75
	 * @param \blitze\sitemaker\services\forum\manager	$forum_manager			Forum manager object
76
	 * @param \blitze\content\model\mapper_factory		$mapper_factory			Mapper factory object
77
	 * @param string									$phpbb_admin_path       Relative admin root path
78
	 * @param string									$php_ext				php file extension
79
	 * @param boolean									$auto_refresh			Used for testing
80
	*/
81 5
	public function __construct(\phpbb\auth\auth $auth, \phpbb\cache\driver\driver_interface $cache, \phpbb\config\config $config, \phpbb\db\driver\driver_interface $db, \phpbb\event\dispatcher_interface $phpbb_dispatcher, \phpbb\language\language $language, \phpbb\log\log_interface $logger, \phpbb\request\request_interface $request, \phpbb\user $user, \blitze\content\services\types $content_types, \blitze\sitemaker\services\forum\manager $forum_manager, \blitze\content\model\mapper_factory $mapper_factory, $phpbb_admin_path, $php_ext, $auto_refresh = true)
82
	{
83 5
		$this->auth = $auth;
84 5
		$this->cache = $cache;
85 5
		$this->config = $config;
86 5
		$this->db = $db;
87 5
		$this->phpbb_dispatcher = $phpbb_dispatcher;
88 5
		$this->language = $language;
89 5
		$this->logger = $logger;
90 5
		$this->request = $request;
91 5
		$this->user = $user;
92 5
		$this->content_types = $content_types;
93 5
		$this->forum_manager = $forum_manager;
94 5
		$this->mapper_factory = $mapper_factory;
95 5
		$this->phpbb_admin_path = $phpbb_admin_path;
96 5
		$this->php_ext = $php_ext;
97 5
		$this->auto_refresh = $auto_refresh;
98 5
	}
99
100
	/**
101
	 * @inheritdoc
102
	 */
103 5
	public function execute($u_action, $type = '')
104
	{
105 5
		$fields_data = $this->request->variable('field_data', array('' => array('' => '')), true);
106
107 5
		$types_mapper = $this->mapper_factory->create('types');
108 5
		$unsaved_entity = $this->get_unsaved_entity($types_mapper);
109
110 5
		$this->ensure_content_name_is_unique($unsaved_entity->get_content_name(), $type);
111 4
		$this->ensure_content_has_fields($fields_data);
112 2
		$this->db->sql_transaction('begin');
113
114 2
		$field_types = $this->get_field_types($fields_data);
115 2
		$old_langname = $this->handle_content_type($type, $unsaved_entity);
116
117
		/** @var \blitze\content\model\entity\type $entity */
118 2
		$entity = $types_mapper->save($unsaved_entity);
119
120
		/**
121
		 * Event to modify submitted field data before they are saved
122
		 *
123
		 * @event blitze.content.acp_save_fields_before
124
		 * @var	array									field_types			Array mapping field types to field names of form array([field_type] => array([field_name1], [field_name2]))
125
		 * @var	array									field_data			Array containing field data of form array([field_name] => array([field_type] => 'foo', ...))
126
		 * @var	\blitze\content\model\entity\type		entity				Content type entity
127
		 */
128 2
		$vars = array('field_types', 'field_data', 'entity');
129 2
		extract($this->phpbb_dispatcher->trigger_event('blitze.content.acp_save_fields_before', compact($vars)));
130
131 2
		$this->handle_content_fields($entity->get_content_id(), $fields_data);
132 2
		$this->db->sql_transaction('commit');
133 2
		$this->cache->destroy('_content_types');
134 2
		$this->show_results($entity, $u_action, $type, $old_langname);
135
	}
136
137
	/**
138
	 * @param string $type
139
	 * @param \blitze\content\model\entity\type $unsaved_entity
140
	 * @return string
141
	 */
142 2
	protected function handle_content_type($type, \blitze\content\model\entity\type &$unsaved_entity)
143
	{
144 2
		$existing_langname = '';
145 2
		$forum_perm_from = $this->request->variable('copy_forum_perm', 0);
146
147
		if ($type)
148 2
		{
149 1
			$entity = $this->content_types->get_type($type);
150 1
			$forum_id = $entity->get_forum_id();
151 1
			$existing_langname = $entity->get_content_langname();
152
153 1
			$unsaved_entity->set_forum_id($forum_id);
154 1
			$unsaved_entity->set_content_id($entity->get_content_id());
155 1
			$this->handle_langname_change($forum_id, $entity->get_content_langname(), $unsaved_entity->get_content_langname());
156 1
			$this->copy_forum_permissions($forum_id, $forum_perm_from);
157 1
		}
158
		else
159
		{
160 1
			$forum_id = $this->create_content_forum($unsaved_entity->get_content_langname(), $forum_perm_from);
161 1
			$unsaved_entity->set_forum_id($forum_id);
162
		}
163
164 2
		return $existing_langname;
165
	}
166
167
	/**
168
	 * @param \blitze\content\model\mapper\types $mapper
169
	 * @return \blitze\content\model\entity\type
170
	 */
171 5
	protected function get_unsaved_entity(\blitze\content\model\mapper\types $mapper)
172
	{
173 5
		$content_desc = $this->request->variable('content_desc', '', true);
174 5
		$content_view = $this->request->variable('content_view', '');
175 5
		$comments = $this->request->variable('comments', '');
176
		$view_settings = $this->request->variable(array('view_settings', $content_view), array('' => ''));
177 5
		$comments_settings = $this->request->variable(array('comments_settings', $comments), array('' => ''));
178 5
179 5
		$entity = $mapper->create_entity(array(
180 5
			'content_name'			=> $this->request->variable('content_name', ''),
181 5
			'content_langname'		=> $this->request->variable('content_langname', '', true),
182 5
			'content_enabled'		=> $this->request->variable('content_enabled', true),
183 5
			'content_view'			=> $content_view,
184 5
			'content_view_settings'	=> $view_settings,
185 5
			'comments'				=> $comments,
186 5
			'comments_settings'		=> $comments_settings,
187 5
			'req_approval'			=> $this->request->variable('req_approval', 1),
188 5
			'allow_views'			=> $this->request->variable('allow_views', 0),
189 5
			'show_pagination'		=> $this->request->variable('show_pagination', 0),
190 5
			'index_show_desc'		=> $this->request->variable('index_show_desc', 0),
191 5
			'items_per_page'		=> $this->request->variable('items_per_page', 1),
192 5
			'summary_tpl'			=> $this->request->variable('summary_tpl', '', true),
193 5
			'detail_tpl'			=> $this->request->variable('detail_tpl', '', true),
194
			'topic_blocks'			=> $this->request->variable('topic_blocks', ''),
195 5
			'last_modified'			=> time(),
196
		));
197
198
		return $entity->set_content_desc($content_desc, 'storage');
199
	}
200
201
	/**
202
	 * @param int $forum_id
203 1
	 * @param int $forum_perm_from
204
	 * @return void
205 1
	 */
206 1
	protected function copy_forum_permissions($forum_id, $forum_perm_from)
207
	{
208
		if ($forum_perm_from && $forum_perm_from != $forum_id)
209
		{
210
			copy_forum_permissions($forum_perm_from, array($forum_id), false, false);
211
			phpbb_cache_moderators($this->db, $this->cache, $this->auth);
212
213 1
			$this->auth->acl_clear_prefetch();
214
			$this->cache->destroy('sql', FORUMS_TABLE);
215
		}
216
	}
217
218
	/**
219
	 * @param \blitze\content\model\entity\type $entity
220
	 * @param string $u_action
221
	 * @param string $type
222 2
	 * @param string $old_langname
223
	 * @return void
224 2
	 */
225 2
	protected function show_results(\blitze\content\model\entity\type $entity, $u_action, $type, $old_langname)
226 1
	{
227 1
		if (!$type)
228 1
		{
229 1
			$u_set_permission = append_sid("{$this->phpbb_admin_path}index.$this->php_ext", 'i=permissions&mode=setting_forum_local&forum_id[]=' . $entity->get_forum_id(), true);
230
			$lang_key = 'CONTENT_TYPE_CREATED';
231
			$message = $this->language->lang($lang_key, '<a href="' . $u_set_permission . '">', '</a>');
232 1
		}
233 1
		else
234 1
		{
235
			$this->meta_refresh(3, $u_action);
236
			$lang_key = 'CONTENT_TYPE_UPDATED';
237 2
			$message = $this->language->lang($lang_key);
238 2
		}
239 2
240 1
		$additional_data = array();
241 1
		if ($type && $entity->get_content_name() !== $type)
242 1
		{
243
			$lang_key = 'CONTENT_TYPE_RENAMED';
244 2
			$additional_data[] = $old_langname;
245 2
		}
246
247 2
		$additional_data[] = $entity->get_content_langname();
248
		$this->logger->add('admin', $this->user->data['user_id'], $this->user->ip, 'ACP_LOG_' . $lang_key, time(), $additional_data);
249
250
		$this->trigger_error($message, $u_action);
251
	}
252
253
	/**
254
	 * @param string $test_name
255
	 * @param string $content_type
256 5
	 * @return void
257
	 * @throws \blitze\sitemaker\exception\invalid_argument
258 5
	 */
259 5
	protected function ensure_content_name_is_unique($test_name, $content_type)
260 1
	{
261
		if ($test_name !== $content_type && $this->content_types->exists($test_name))
262 4
		{
263
			throw new \blitze\sitemaker\exception\invalid_argument(array($test_name, 'CONTENT_NAME_EXISTS'));
0 ignored issues
show
Bug introduced by
The type blitze\sitemaker\exception\invalid_argument was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
264
		}
265
	}
266
267
	/**
268
	 * @param array $fields_data
269 4
	 * @return void
270
	 * @throws \blitze\sitemaker\exception\invalid_argument
271 4
	 */
272 4
	protected function ensure_content_has_fields(array $fields_data)
273 2
	{
274
		if (!sizeof(array_filter($fields_data)))
275 2
		{
276
			throw new \blitze\sitemaker\exception\invalid_argument(array('content_fields', 'FIELD_MISSING'));
277
		}
278
	}
279
280
	/**
281
	 * @param int $forum_id
282
	 * @param string $old_langname
283 1
	 * @param string $new_langname
284
	 * @return void
285 1
	 */
286 1
	protected function handle_langname_change($forum_id, $old_langname, $new_langname)
287 1
	{
288 1
		if ($old_langname !== $new_langname)
289 1
		{
290 1
			$forum_name = $this->language->lang($new_langname);
291 1
			$sql = 'UPDATE ' . FORUMS_TABLE . " SET forum_name = '" . $this->db->sql_escape($forum_name) . "' WHERE forum_id = " . (int) $forum_id;
292
			$this->db->sql_query($sql);
293
		}
294
	}
295
296
	/**
297
	 * @param string $content_langname
298 1
	 * @param int $forum_perm_from
299
	 * @return int
300
	 */
301 1
	protected function create_content_forum($content_langname, $forum_perm_from)
302 1
	{
303 1
		$forum_data = array(
304 1
			'forum_type'	=> FORUM_POST,
305 1
			'forum_name'	=> $this->language->lang($content_langname),
306
			'forum_desc'	=> $this->language->lang('CONTENT_FORUM_EXPLAIN'),
307 1
			'parent_id'		=> (int) $this->config['blitze_content_forum_id'],
308
		);
309 1
310
		$this->forum_manager->add($forum_data, $forum_perm_from);
311
312
		return (int) $forum_data['forum_id'];
313
	}
314
315
	/**
316
	 * @param int $content_id
317 2
	 * @param array $fields_data
318
	 * @return void
319 2
	 */
320
	protected function handle_content_fields($content_id, array $fields_data)
321 2
	{
322 2
		$mapper = $this->mapper_factory->create('fields');
323 2
324
		$fields_ary = array_filter(array_keys($fields_data));
325 2
		$field_ids = $this->get_existing_field_ids($content_id);
326 2
		$max_id = $mapper->get_max_field_id();
327
328
		$form_fields = array();
329 2
		foreach ($fields_ary as $i => $field)
330 2
		{
331 2
			/** @var \blitze\content\model\entity\field $entity */
332 2
			$entity = $mapper->create_entity($fields_data[$field]);
333 2
			$entity->set_field_id(isset($field_ids[$field]) ? $field_ids[$field] : ++$max_id)
334 2
				->set_content_id($content_id)
335
				->set_field_order($i)
336 2
				->set_field_explain($fields_data[$field]['field_explain'], 'storage')
337 2
				->set_field_props($this->get_field_props($field));
338
339
			$form_fields[$field] = $entity->to_db();
340 2
		}
341
342
		// delete all fields for this content type
343 2
		$mapper->delete(array('content_id', '=', $content_id));
344 2
345
		// add the submitted fields
346
		$mapper->multi_insert($form_fields);
347
	}
348
349
	/**
350 2
	 * @param string $field
351
	 * @return array
352 2
	 */
353 2
	protected function get_field_props($field)
354 2
	{
355
		$field_props = $this->request->variable(array('field_props', $field), array('' => ''));
356 2
		$field_options = $this->request->variable(array('field_options', $field), array(0 => ''), true);
357 2
		$fields_defaults = $this->request->variable(array('field_defaults', $field), array(0 => ''), true);
358 2
359 2
		$field_props = array_filter($field_props, 'strlen');
360 2
		$field_props = array_merge($field_props, array_filter(array(
361
			'options'	=> array_filter($field_options, 'strlen'),
362 2
			'defaults'	=> array_filter($fields_defaults, 'strlen'),
363
		)));
364
365
		return $field_props;
366
	}
367
368
	/**
369 2
	 * @param int $content_id
370
	 * @return array
371 2
	 */
372 2
	protected function get_existing_field_ids($content_id)
373 2
	{
374 2
		$mapper = $this->mapper_factory->create('fields');
375
		$collection = $mapper->find(array(
376 2
			array('content_id', '=', $content_id),
377 2
		));
378
379 1
		$field_ids = array();
380 2
		foreach ($collection as $id => $entity)
381
		{
382 2
			$field_ids[$entity->get_field_name()] = $id;
383
		}
384
385
		return $field_ids;
386
	}
387
388
	/**
389 2
	 * @param array $fields_data
390
	 * @return array[]
391 2
	 */
392 2
	protected function get_field_types(array $fields_data)
393
	{
394 2
		$field_types = array();
395 2
		foreach ($fields_data as $field => $row)
396
		{
397 2
			$field_types[$row['field_type']][] = $field;
398
		}
399
400
		return $field_types;
401
	}
402
}
403