Completed
Push — develop ( 8a8fbb...b08540 )
by Mario
02:38
created

admin_main::required_settings()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 3
eloc 4
nc 2
nop 2
1
<?php
2
/**
3
 *
4
 * PayPal Donation extension for the phpBB Forum Software package.
5
 *
6
 * @copyright (c) 2015 Skouat
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace skouat\ppde\controller;
12
13
abstract class admin_main
14
{
15
	/** @var object \phpbb\config\config */
16
	protected $config;
17
	/** @var object Symfony\Component\DependencyInjection\ContainerInterface */
18
	protected $container;
19
	/** @var string */
20
	protected $id_prefix_name;
21
	/** @var string */
22
	protected $lang_key_prefix;
23
	/** @var \phpbb\log\log */
24
	protected $log;
25
	/** @var string */
26
	protected $module_name;
27
	/** @var bool */
28
	protected $preview;
29
	/** @var \phpbb\request\request */
30
	protected $request;
31
	/** @var bool */
32
	protected $submit;
33
	/** @var \phpbb\template\template */
34
	protected $template;
35
	/** @var string */
36
	protected $u_action;
37
	/** @var \phpbb\user */
38
	protected $user;
39
40
	/**
41
	 * Constructor
42
	 *
43
	 * @param string $lang_key_prefix Prefix for the messages thrown by exceptions
44
	 * @param string $id_prefix_name  Prefix name for identifier in the URL
45
	 * @param string $module_name     Name of the module currently used
46
	 *
47
	 * @access public
48
	 */
49
	public function __construct($module_name, $lang_key_prefix, $id_prefix_name)
50
	{
51
		$this->module_name = $module_name;
52
		$this->lang_key_prefix = $lang_key_prefix;
53
		$this->id_prefix_name = $id_prefix_name;
54
	}
55
56
	/**
57
	 * Parse data to the entity
58
	 *
59
	 * @param \skouat\ppde\entity\main $entity            The entity object
60
	 * @param string                   $run_before_insert Name of the function to call before SQL INSERT
61
	 *
62
	 * @return string $log_action
63
	 * @access public
64
	 */
65
	public function add_edit_data(\skouat\ppde\entity\main $entity, $run_before_insert = '')
66
	{
67
		if ($entity->get_id())
68
		{
69
			// Save the edited item entity to the database
70
			$entity->save($entity->check_required_field());
71
			$log_action = 'UPDATED';
72
		}
73
		else
74
		{
75
			// Insert the data to the database
76
			$entity->insert($run_before_insert);
77
78
			// Get the newly inserted identifier
79
			$id = $entity->get_id();
80
81
			// Reload the data to return a fresh entity
82
			$entity->load($id);
83
			$log_action = 'ADDED';
84
		}
85
86
		return $log_action;
87
	}
88
89
	/**
90
	 * Set data in the $entity object.
91
	 * Use call_user_func_array() to call $entity function
92
	 *
93
	 * @param \skouat\ppde\entity\main $entity The entity object
94
	 * @param array                    $data_ary
95
	 *
96
	 * @access public
97
	 */
98
	public function set_entity_data(\skouat\ppde\entity\main $entity, $data_ary)
99
	{
100
		foreach ($data_ary as $entity_function => $data)
101
		{
102
			// Calling the set_$entity_function on the entity and passing it $currency_data
103
			call_user_func_array(array($entity, 'set_' . $entity_function), array($data));
104
		}
105
		unset($data_ary, $entity_function, $data);
106
	}
107
108
	/**
109
	 * Set page url
110
	 *
111
	 * @param string $u_action Custom form action
112
	 *
113
	 * @return void
114
	 * @access public
115
	 */
116
	public function set_page_url($u_action)
117
	{
118
		$this->u_action = $u_action;
119
	}
120
121
	/**
122
	 * @param array $errors
123
	 *
124
	 * @return bool
125
	 * @access protected
126
	 */
127
	protected function can_submit_data(array $errors)
128
	{
129
		return $this->submit && empty($errors) && !$this->preview;
130
	}
131
132
	/**
133
	 * Trigger error message if data already exists
134
	 *
135
	 * @param \skouat\ppde\entity\main $entity The entity object
136
	 *
137
	 * @access protected
138
	 */
139
	protected function trigger_error_data_already_exists(\skouat\ppde\entity\main $entity)
140
	{
141
		if ($this->is_added_data_exists($entity))
142
		{
143
			// Show user warning for an already exist page and provide link back to the edit page
144
			$message = $this->user->lang[$this->lang_key_prefix . '_EXISTS'];
145
			$message .= '<br /><br />';
146
			$message .= $this->user->lang($this->lang_key_prefix . '_GO_TO_PAGE', '<a href="' . $this->u_action . '&amp;action=edit&amp;' . $this->id_prefix_name . '_id=' . $entity->get_id() . '">&raquo; ', '</a>');
147
			trigger_error($message . adm_back_link($this->u_action), E_USER_WARNING);
148
		}
149
	}
150
151
	/**
152
	 * @param \skouat\ppde\entity\main $entity The entity object
153
	 *
154
	 * @return bool
155
	 * @access protected
156
	 */
157
	protected function is_added_data_exists($entity)
158
	{
159
		return $entity->data_exists($entity->build_sql_data_exists()) && $this->request->variable('action', '') === 'add';
160
	}
161
162
	/**
163
	 * Check some settings before submitting data
164
	 *
165
	 * @param \skouat\ppde\entity\main $entity            The entity object
166
	 * @param string                   $field_name        Name of the entity function to call
167
	 * @param string|int               $value_cmp         Default value to compare with the call_user_func() return value
168
	 * @param bool                     $submit_or_preview Form submit or preview status
169
	 *
170
	 * @return array $errors
171
	 * @access protected
172
	 */
173
	protected function is_empty_data(\skouat\ppde\entity\main $entity, $field_name, $value_cmp, $submit_or_preview = false)
174
	{
175
		$errors = array();
176
177
		if (call_user_func(array($entity, 'get_' . $field_name)) == $value_cmp && $submit_or_preview)
178
		{
179
			$errors[] = $this->user->lang[$this->lang_key_prefix . '_EMPTY_' . strtoupper($field_name)];
180
		}
181
182
		return $errors;
183
	}
184
185
	/**
186
	 * Check if form is valid or not
187
	 *
188
	 * @param string $form_name
189
	 * @param bool   $submit_or_preview
190
	 *
191
	 * @return array
192
	 * @access protected
193
	 */
194
	protected function is_invalid_form($form_name, $submit_or_preview = false)
195
	{
196
		return (!check_form_key($form_name) && $submit_or_preview) ? array($this->user->lang['FORM_INVALID']) : array();
197
	}
198
199
	/**
200
	 * Get result of submit and preview expression
201
	 *
202
	 * @param bool $submit
203
	 * @param bool $preview
204
	 *
205
	 * @return bool
206
	 * @access protected
207
	 */
208
	protected function submit_or_preview($submit = false, $preview = false)
209
	{
210
		return (bool) $submit || $preview;
211
	}
212
213
	/**
214
	 * Show user a result message if AJAX was used
215
	 *
216
	 * @param string $message Text message to show to the user
217
	 *
218
	 * @return void
219
	 * @access protected
220
	 */
221
	protected function ajax_delete_result_message($message = '')
222
	{
223
		if ($this->request->is_ajax())
224
		{
225
			$json_response = new \phpbb\json_response;
226
			$json_response->send(array(
227
				'MESSAGE_TITLE' => $this->user->lang['INFORMATION'],
228
				'MESSAGE_TEXT'  => $message,
229
				'REFRESH_DATA'  => array(
230
					'time' => 3
231
				)
232
			));
233
		}
234
	}
235
236
	/**
237
	 * Return the entity ContainerInterface used by the ACP module in use
238
	 *
239
	 * @return object
240
	 * @access protected
241
	 */
242
	protected function get_container_entity()
243
	{
244
		return $this->container->get('skouat.ppde.entity.' . $this->module_name);
245
	}
246
247
	/**
248
	 * Set u_action output vars for display in the template
249
	 *
250
	 * @return void
251
	 * @access protected
252
	 */
253
	protected function u_action_assign_template_vars()
254
	{
255
		$this->template->assign_vars(array(
256
			'U_ACTION' => $this->u_action,
257
		));
258
	}
259
260
	/**
261
	 * Set error output vars for display in the template
262
	 *
263
	 * @param array $errors
264
	 *
265
	 * @return void
266
	 * @access protected
267
	 */
268
	protected function s_error_assign_template_vars($errors)
269
	{
270
		$this->template->assign_vars(array(
271
			'S_ERROR'   => (sizeof($errors)) ? true : false,
272
			'ERROR_MSG' => (sizeof($errors)) ? implode('<br />', $errors) : '',
273
		));
274
	}
275
276
	/**
277
	 * Check if a config value is true
278
	 *
279
	 * @param mixed  $config Config value
280
	 * @param string $type   (see settype())
281
	 * @param mixed  $default
282
	 *
283
	 * @return mixed
284
	 * @access protected
285
	 */
286
	protected function check_config($config, $type = 'boolean', $default = '')
287
	{
288
		// We're using settype to enforce data types
289
		settype($config, $type);
290
		settype($default, $type);
291
292
		return $config ? $config : $default;
293
	}
294
	/**
295
	 * Check if settings is required
296
	 *
297
	 * @param $settings
298
	 * @param $depend_on
299
	 *
300
	 * @return mixed
301
	 * @access protected
302
	 */
303
	protected function required_settings($settings, $depend_on)
304
	{
305
		if (empty($settings) && $depend_on == true)
306
		{
307
			trigger_error($this->user->lang($this->lang_key_prefix . '_MISSING') . adm_back_link($this->u_action), E_USER_WARNING);
308
		}
309
310
		return $settings;
311
	}
312
313
	/**
314
	 * Check if a settings depend on another.
315
	 *
316
	 * @param $config_name
317
	 *
318
	 * @return bool
319
	 * @access protected
320
	 */
321
	protected function depend_on($config_name)
322
	{
323
		return !empty($this->config[$config_name]) ? (bool) $this->config[$config_name] : false;
324
	}
325
}
326