Completed
Push — develop ( 6a0b8a...755a17 )
by Daniel
07:17
created

field_controller::set_prop()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 0
cts 8
cp 0
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 2
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\request\request_interface */
17
	protected $request;
18
19
	/** @var \phpbb\template\template */
20
	protected $template;
21
22
	/** @var \blitze\sitemaker\services\auto_lang */
23
	protected $auto_lang;
24
25
	/** @var \blitze\content\services\form\fields_factory */
26
	protected $fields_factory;
27
28
	/** @var string */
29
	protected $phpbb_admin_path;
30
31
	/**
32
	 * Constructor
33
	 *
34
	 * @param \phpbb\request\request_interface				$request			Request object
35
	 * @param \phpbb\template\template						$template			Template object
36
	 * @param \blitze\sitemaker\services\auto_lang			$auto_lang			Auto add lang file
37
	 * @param \blitze\content\services\form\fields_factory	$fields_factory		Fields factory  object
38
	 */
39
	public function __construct(\phpbb\request\request_interface $request, \phpbb\template\template $template, \blitze\sitemaker\services\auto_lang $auto_lang, \blitze\content\services\form\fields_factory $fields_factory)
40
	{
41
		$this->request = $request;
42
		$this->template = $template;
43
		$this->auto_lang = $auto_lang;
44
		$this->fields_factory = $fields_factory;
45
	}
46
47
	/**
48
	 * @return \Symfony\Component\HttpFoundation\Response
49
	 */
50
	public function handle()
51
	{
52
		if ($this->request->is_ajax() === false)
53
		{
54
			redirect(generate_board_url());
55
		}
56
57
		$this->auto_lang->add('form_fields');
58
59
		$fields_template = 'content_fields.html';
60
61
		$this->template->set_custom_style($fields_template, 'ext/blitze/content/adm/style');
62
63
		$data = $this->get_field_data();
64
		$this->template->assign_var('CONTENT_FIELDS', array(array_change_key_case($data, CASE_UPPER)));
65
66
		$this->template->set_filenames(array(
67
			'field'	=> $fields_template
68
		));
69
70
		return new JsonResponse($this->template->assign_display('field'));
71
	}
72
73
	/**
74
	 * @return array
75
	 */
76
	protected function get_field_data()
77
	{
78
		$field_type = $this->request->variable('type', '');
79
		$fields_data = $this->request->variable('field_data', array('' => array('' => '')), true);
80
		$field_props = $this->request->variable('field_props', array('' => array('' => '')), true);
81
82
		$data = (array) array_pop($fields_data);
83
84
		// set defaults if adding new field
85
		$data += array(
86
			'field_detail_ldisp'	=> 1,
87
			'field_summary_ldisp'	=> 1,
88
		);
89
90
		/** @var /blitze/content/services/form/field/field_interface $object */
0 ignored issues
show
Documentation introduced by
The doc-type /blitze/content/services...m/field/field_interface could not be parsed: Unknown type name "/blitze/content/services/form/field/field_interface" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
91
		$field = $this->fields_factory->get($field_type);
92
		$default_props = $field->get_default_props();
93
94
		$data['field_type'] = $field_type;
95
		$data['type_label'] = $field->get_langname();
96
		$data['field_props'] = array_replace_recursive($default_props,
97
			array_intersect_key((array) array_pop($field_props), $default_props)
98
		);
99
100
		$this->set_prop('options', $data);
101
		$this->set_prop('defaults', $data);
102
		$this->force_prop($field_type, $data);
103
104
		return $data;
105
	}
106
107
	/**
108
	 * @param string $prop	options|defaults
109
	 * @param array $data
110
	 * @return void
111
	 */
112
	protected function set_prop($prop, array &$data)
113
	{
114
		$field_prop = $this->request->variable('field_' . $prop, array('' => array(0 => '')), true);
115
116
		if (null !== ($array = array_pop($field_prop)))
117
		{
118
			$data['field_props'][$prop] = $array;
119
		}
120
	}
121
122
	/**
123
	 * @param string $field_type
124
	 * @param array $data
125
	 * @return void
126
	 */
127
	protected function force_prop($field_type, array &$data)
128
	{
129
		switch ($field_type)
130
		{
131
			case 'checkbox':
132
				$data['field_props']['multi_select'] = true;
133
			break;
134
			case 'radio':
135
				$data['field_props']['multi_select'] = false;
136
			break;
137
		}
138
	}
139
}
140