Completed
Push — develop ( 4a255f...c77227 )
by Daniel
07:47
created

field   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 169
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 0

Test Coverage

Coverage 95%

Importance

Changes 0
Metric Value
wmc 15
lcom 3
cbo 0
dl 0
loc 169
ccs 38
cts 40
cp 0.95
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A set_field_id() 0 8 2
A set_field_label() 0 5 1
A set_field_explain() 0 10 3
A get_field_explain() 0 13 3
A set_field_props() 0 12 3
A get_field_props() 0 11 3
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\model\entity;
11
12
use blitze\sitemaker\model\base_entity;
13
14
/**
15
 * @method integer get_field_id()
16
 * @method object set_content_id($content_id)
17
 * @method integer get_content_id()
18
 * @method object set_field_name($field_name)
19
 * @method integer get_field_name()
20
 * @method integer get_field_label()
21
 * @method object set_field_type($field_type)
22
 * @method string get_field_type()
23
 * @method object set_field_mod_only($mod_only)
24
 * @method boolean get_field_mod_only()
25
 * @method object set_field_required($required_field)
26
 * @method boolean get_field_required()
27
 * @method object set_field_summary_show($summary_show)
28
 * @method string get_field_summary_show()
29
 * @method object set_field_detail_show($detail_show)
30
 * @method string get_field_detail_show()
31
 * @method object set_field_summary_ldisp($summary_ldisp)
32
 * @method boolean get_field_summary_ldisp()
33
 * @method object set_field_detail_ldisp($detail_ldisp)
34
 * @method boolean get_field_detail_ldisp()
35
 * @method object set_exp_uid($uid)
36
 * @method string get_exp_uid()
37
 * @method object set_exp_bitfield($bitfield)
38
 * @method string get_exp_bitfield()
39
 * @method object set_exp_options($options)
40
 * @method integer get_exp_options()
41
 * @method object set_field_order($order)
42
 * @method string get_field_order()
43
 */
44
final class field extends base_entity
45
{
46
	/** @var integer */
47
	protected $field_id;
48
49
	/** @var integer */
50
	protected $content_id;
51
52
	/** @var string */
53
	protected $field_name;
54
55
	/** @var string */
56
	protected $field_label;
57
58
	/** @var string */
59
	protected $field_explain = '';
60
61
	/** @var string */
62
	protected $field_type;
63
64
	/** @var string */
65
	protected $field_props = '';
66
67
	/** @var boolean */
68
	protected $field_mod_only = false;
69
70
	/** @var boolean */
71
	protected $field_required = false;
72
73
	/** @var string */
74
	protected $field_summary_show = '';
75
76
	/** @var string */
77
	protected $field_detail_show = '';
78
79
	/** @var integer */
80
	protected $field_summary_ldisp = 1;
81
82
	/** @var integer */
83
	protected $field_detail_ldisp = 1;
84
85
	/** @var string */
86
	protected $field_exp_uid = '';
87
88
	/** @var string */
89
	protected $field_exp_bitfield = '';
90
91
	/** @var integer */
92
	protected $field_exp_options = 7;
93
94
	/** @var integer */
95
	protected $field_order = 0;
96
97
	/** @var array */
98
	protected $required_fields = array('content_id', 'field_name', 'field_label', 'field_type');
99
100
	/** @var array */
101
	protected $db_fields = array(
102
		'content_id',
103
		'field_name',
104
		'field_label',
105
		'field_explain',
106
		'field_type',
107
		'field_props',
108
		'field_mod_only',
109
		'field_required',
110
		'field_summary_show',
111
		'field_detail_show',
112
		'field_summary_ldisp',
113
		'field_detail_ldisp',
114
		'field_exp_uid',
115
		'field_exp_bitfield',
116
		'field_exp_options',
117
		'field_order',
118
	);
119
120
	/**
121
	 * Set field ID
122
	 * @param int $field_id
123
	 * @return $this
124
	 */
125 28
	public function set_field_id($field_id)
126
	{
127 28
		if (!$this->field_id)
128 28
		{
129 28
			$this->field_id = (int) $field_id;
130 28
		}
131 28
		return $this;
132
	}
133
134
	/**
135
	 * Set field label
136
	 * @param string $label
137
	 * @return $this
138
	 */
139 28
	public function set_field_label($label)
140
	{
141 28
		$this->field_label = utf8_ucfirst(trim($label));
142 28
		return $this;
143
	}
144
145
	/**
146
	 * @param string $explain
147
	 * @param string $mode
148
	 * @return $this
149
	 */
150 27
	public function set_field_explain($explain, $mode = '')
151
	{
152 27
		$this->field_explain = $explain;
153
154 27
		if ($this->field_explain && $mode === 'storage')
155 27
		{
156
			generate_text_for_storage($this->field_explain, $this->field_exp_uid, $this->field_exp_bitfield, $this->field_exp_options, true, true, true);
157
		}
158 27
		return $this;
159
	}
160
161
	/**
162
	 * @param string $mode
163
	 * @return string|array
164
	 */
165 24
	public function get_field_explain($mode = 'display')
166
	{
167 24
		if ($mode === 'edit')
168 24
		{
169 1
			$data = generate_text_for_edit($this->field_explain, $this->field_exp_uid, $this->field_exp_options);
170 1
			return $data['text'];
171
		}
172
		else
173
		{
174 24
			$parse_flags = ($this->field_exp_bitfield ? OPTION_FLAG_BBCODE : 0) | OPTION_FLAG_SMILIES;
175 24
			return generate_text_for_display($this->field_explain, $this->field_exp_uid, $this->field_exp_bitfield, $parse_flags);
176
		}
177
	}
178
179
	/**
180
	 * Set field properties
181
	 * @param array|string $props
182
	 * @return $this
183
	 */
184 27
	public function set_field_props($props)
185
	{
186 27
		if (!is_array($props))
187 27
		{
188 26
			$this->field_props = $props;
189 26
		}
190 3
		else if (sizeof($props))
191 3
		{
192 3
			$this->field_props = json_encode($props);
193 3
		}
194 27
		return $this;
195
	}
196
197
	/**
198
	 * Get field settings
199
	 * @return array
200
	 */
201 24
	public function get_field_props()
202
	{
203 24
		$field_props = ($this->field_props) ? json_decode($this->field_props, true) : array();
204
205 24
		if (in_array($this->field_type, array('radio', 'checkbox', 'select')))
206 24
		{
207 19
			$field_props['options'] = array_filter(array_combine($field_props['options'], $field_props['options']), 'strlen');
208 19
		}
209
210 24
		return $field_props;
211
	}
212
}
213