Completed
Push — develop ( c77227...1859c0 )
by Daniel
14:00
created

field_controller::handle()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 0
cts 16
cp 0
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 14
nc 2
nop 0
crap 6
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 */
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
		$field_data = $this->get_field_data();
73
		$this->template->assign_var('CONTENT_FIELDS', array(array_change_key_case($field_data, CASE_UPPER)));
74
75
		$this->template->set_filenames(array(
76
			'field'	=> 'content_fields.html'
77
		));
78
79
		return new JsonResponse($this->template->assign_display('field'));
80
	}
81
82
	/**
83
	 * @return array
84
	 */
85
	protected function get_field_data()
86
	{
87
		$field_type = $this->request->variable('type', '');
88
		$fields_data = $this->request->variable('field_data', array('' => array('' => '')), true);
89
		$field_props = $this->request->variable('field_props', array('' => array('' => '')), true);
90
91
		$field_data = (array) array_pop($fields_data);
92
93
		// set defaults if adding new field
94
		$field_data += array(
95
			'field_detail_ldisp'	=> 1,
96
			'field_summary_ldisp'	=> 1,
97
			'field_detail_show'		=> 'body',
98
			'field_summary_show'	=> 'body',
99
		);
100
101
		/** @var \blitze\content\services\form\field\field_interface $field_instance */
102
		$field_instance = $this->fields_factory->get($field_type);
103
		$default_props = $field_instance->get_default_props();
104
105
		$field_data['field_type'] = $field_type;
106
		$field_data['type_label'] = $field_instance->get_langname();
107
		$field_data['field_props'] = array_replace_recursive($default_props,
108
			array_intersect_key((array) array_pop($field_props), $default_props)
109
		);
110
111
		$this->set_prop('options', $field_data);
112
		$this->set_prop('defaults', $field_data);
113
		$this->force_prop($field_type, $field_data);
114
115
		/**
116
		 * Event to modify field data
117
		 *
118
		 * @event blitze.content.field.modify_data
119
		 * @var	array													field_data		Array containing field data
120
		 * @var	\blitze\content\services\form\field\field_interface		field_instance	Field instance
121
		 */
122
		$vars = array('field_data', 'field_instance');
123
		extract($this->phpbb_dispatcher->trigger_event('blitze.content.field.modify_data', compact($vars)));
124
125
		return $field_data;
126
	}
127
128
	/**
129
	 * @param string $prop	options|defaults
130
	 * @param array $data
131
	 * @return void
132
	 */
133
	protected function set_prop($prop, array &$data)
134
	{
135
		$field_prop = $this->request->variable('field_' . $prop, array('' => array(0 => '')), true);
136
137
		if (null !== ($array = array_pop($field_prop)))
138
		{
139
			$data['field_props'][$prop] = $array;
140
		}
141
	}
142
143
	/**
144
	 * @param string $field_type
145
	 * @param array $data
146
	 * @return void
147
	 */
148
	protected function force_prop($field_type, array &$data)
149
	{
150
		switch ($field_type)
151
		{
152
			case 'checkbox':
153
				$data['field_props']['multi_select'] = true;
154
			break;
155
			case 'radio':
156
				$data['field_props']['multi_select'] = false;
157
			break;
158
		}
159
	}
160
}
161