Completed
Branch master (915db5)
by Mario
03:03
created

admin_main::get_container_entity()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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