field_controller   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 145
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 56
c 2
b 0
f 0
dl 0
loc 145
ccs 0
cts 74
cp 0
rs 10
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A set_prop() 0 7 2
A force_prop() 0 10 3
A handle() 0 26 2
A __construct() 0 7 1
A get_field_data() 0 41 1
1
<?php
2
/**
3
 *
4
 * @package sitemaker
5
 * @copyright (c) 2017 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\controller;
11
12
use Symfony\Component\HttpFoundation\JsonResponse;
13
14
class field_controller
15
{
16
	/** @var \phpbb\event\dispatcher_interface */
17
	protected $phpbb_dispatcher;
18
19
	/** @var \phpbb\request\request_interface */
20
	protected $request;
21
22
	/** @var \phpbb\template\template */
23
	protected $template;
24
25
	/** @var \blitze\sitemaker\services\auto_lang */
0 ignored issues
show
Bug introduced by
The type blitze\sitemaker\services\auto_lang 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...
26
	protected $auto_lang;
27
28
	/** @var \blitze\content\services\form\fields_factory */
29
	protected $fields_factory;
30
31
	/** @var string */
32
	protected $phpbb_admin_path;
33
34
	/**
35
	 * Constructor
36
	 *
37
	 * @param \phpbb\event\dispatcher_interface				$phpbb_dispatcher	Event dispatcher object
38
	 * @param \phpbb\request\request_interface				$request			Request object
39
	 * @param \phpbb\template\template						$template			Template object
40
	 * @param \blitze\sitemaker\services\auto_lang			$auto_lang			Auto add lang file
41
	 * @param \blitze\content\services\form\fields_factory	$fields_factory		Fields factory  object
42
	 */
43
	public function __construct(\phpbb\event\dispatcher_interface $phpbb_dispatcher, \phpbb\request\request_interface $request, \phpbb\template\template $template, \blitze\sitemaker\services\auto_lang $auto_lang, \blitze\content\services\form\fields_factory $fields_factory)
44
	{
45
		$this->phpbb_dispatcher = $phpbb_dispatcher;
46
		$this->request = $request;
47
		$this->template = $template;
48
		$this->auto_lang = $auto_lang;
49
		$this->fields_factory = $fields_factory;
50
	}
51
52
	/**
53
	 * @return \Symfony\Component\HttpFoundation\Response
54
	 */
55
	public function handle()
56
	{
57
		if ($this->request->is_ajax() === false)
58
		{
59
			redirect(generate_board_url());
60
		}
61
62
		$this->auto_lang->add('form_fields');
63
64
		$this->template->set_custom_style(array(
65
			array(
66
				'name' 		=> 'adm',
67
				'ext_path' 	=> 'adm/style/',
68
			)),
69
			array('ext/blitze/content/adm/style')
70
		);
71
72
		$this->template->assign_vars(array(
73
			'CONTENT_FIELDS' => array(array_change_key_case($this->get_field_data(), CASE_UPPER)),
74
		));
75
76
		$this->template->set_filenames(array(
77
			'field'	=> 'content_fields.html'
78
		));
79
80
		return new JsonResponse($this->template->assign_display('field'));
81
	}
82
83
	/**
84
	 * @return array
85
	 */
86
	protected function get_field_data()
87
	{
88
		$field_type = $this->request->variable('type', '');
89
		$fields_data = $this->request->variable('field_data', array('' => array('' => '')), true);
90
		$field_props = $this->request->variable('field_props', array('' => array('' => '')), true);
91
92
		$field_data = (array) array_pop($fields_data);
93
94
		// set defaults if adding new field
95
		$field_data += array(
96
			'field_detail_ldisp'	=> 1,
97
			'field_summary_ldisp'	=> 1,
98
			'field_detail_show'		=> 'body',
99
			'field_summary_show'	=> 'body',
100
		);
101
102
		/** @var \blitze\content\services\form\field\field_interface $field_instance */
103
		$field_instance = $this->fields_factory->get($field_type);
104
		$default_props = $field_instance->get_default_props();
105
106
		$field_data['field_type'] = $field_type;
107
		$field_data['type_label'] = $field_instance->get_langname();
108
		$field_data['field_props'] = array_replace_recursive($default_props,
109
			array_intersect_key((array) array_pop($field_props), $default_props)
110
		);
111
112
		$this->set_prop('options', $field_data);
113
		$this->set_prop('defaults', $field_data);
114
		$this->force_prop($field_type, $field_data);
115
116
		/**
117
		 * Event to modify field data
118
		 *
119
		 * @event blitze.content.field_controller.modify_data
120
		 * @var	array													field_data		Array containing field data
121
		 * @var	\blitze\content\services\form\field\field_interface		field_instance	Field instance
122
		 */
123
		$vars = array('field_data', 'field_instance');
124
		extract($this->phpbb_dispatcher->trigger_event('blitze.content.field_controller.modify_data', compact($vars)));
125
126
		return $field_data;
127
	}
128
129
	/**
130
	 * @param string $prop	options|defaults
131
	 * @param array $data
132
	 * @return void
133
	 */
134
	protected function set_prop($prop, array &$data)
135
	{
136
		$field_prop = $this->request->variable('field_' . $prop, array('' => array(0 => '')), true);
137
138
		if (null !== ($array = array_pop($field_prop)))
139
		{
140
			$data['field_props'][$prop] = $array;
141
		}
142
	}
143
144
	/**
145
	 * @param string $field_type
146
	 * @param array $data
147
	 * @return void
148
	 */
149
	protected function force_prop($field_type, array &$data)
150
	{
151
		switch ($field_type)
152
		{
153
			case 'checkbox':
154
				$data['field_props']['multi_select'] = true;
155
			break;
156
			case 'radio':
157
				$data['field_props']['multi_select'] = false;
158
			break;
159
		}
160
	}
161
}
162