Completed
Pull Request — master (#23)
by Mario
119:06
created

main   B

Complexity

Total Complexity 37

Size/Duplication

Total Lines 403
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 20
Bugs 5 Features 3
Metric Value
wmc 37
c 20
b 5
f 3
lcom 1
cbo 1
dl 0
loc 403
rs 8.6

20 Methods

Rating   Name   Duplication   Size   Complexity  
A get_id() 0 4 2
A get_name() 0 4 2
A set_page_url() 0 4 1
A check_required_field() 0 4 1
A is_in_admin() 0 4 4
A delete() 0 17 4
A disallow_deletion() 0 4 2
A limit_query() 0 4 2
A __construct() 0 9 1
A insert() 0 23 2
A display_error_message() 0 5 1
A run_function_before_action() 0 10 2
A save() 0 15 2
A data_exists() 0 7 1
A set_id() 0 6 1
A build_sql_data_exists() 0 6 1
A load() 0 17 2
A set_name() 0 7 1
A get_data() 0 15 2
B import() 0 29 3
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\entity;
12
13
abstract class main
14
{
15
	/** @type string */
16
	protected $u_action;
17
	/**
18
	 * Declare overridden properties
19
	 */
20
	protected $db;
21
	protected $user;
22
	protected $data;
23
	protected $lang_key_prefix;
24
	protected $lang_key_suffix;
25
	protected $table_name;
26
	protected $table_schema;
27
28
	/**
29
	 * Construct
30
	 *
31
	 * @param \phpbb\db\driver\driver_interface $db              Database object
32
	 * @param \phpbb\user                       $user            User object
33
	 * @param string                            $lang_key_prefix Prefix for the messages thrown by exceptions
34
	 * @param string                            $lang_key_suffix Suffix for the messages thrown by exceptions
35
	 * @param string                            $table_name      Table name
36
	 * @param array                             $table_schema    Array with column names to overwrite and type of data
37
	 *
38
	 * @access public
39
	 */
40
	public function __construct(\phpbb\db\driver\driver_interface $db, \phpbb\user $user, $lang_key_prefix = '', $lang_key_suffix = '', $table_name = '', $table_schema = array())
41
	{
42
		$this->db = $db;
43
		$this->user = $user;
44
		$this->lang_key_prefix = $lang_key_prefix;
45
		$this->lang_key_suffix = $lang_key_suffix;
46
		$this->table_name = $table_name;
47
		$this->table_schema = $table_schema;
48
	}
49
50
	/**
51
	 * Insert the item for the first time
52
	 *
53
	 * Will throw an exception if the item was already inserted (call save() instead)
54
	 *
55
	 * @param string $run_before_insert
56
	 *
57
	 * @return donation_pages $this object for chaining calls; load()->set()->save()
58
	 * @access public
59
	 */
60
	public function insert($run_before_insert = '')
61
	{
62
		if (!empty($this->data[$this->table_schema['item_id']['name']]))
63
		{
64
			// The page already exists
65
			$this->display_error_message($this->lang_key_prefix . '_EXIST');
66
		}
67
68
		// Run some stuff before insert data in database
69
		$this->run_function_before_action($run_before_insert);
70
71
		// Make extra sure there is no page_id set
72
		unset($this->data[$this->table_schema['item_id']['name']]);
73
74
		// Insert the page data to the database
75
		$sql = 'INSERT INTO ' . $this->table_name . ' ' . $this->db->sql_build_array('INSERT', $this->data);
76
		$this->db->sql_query($sql);
77
78
		// Set the page_id using the id created by the SQL insert
79
		$this->data[$this->table_schema['item_id']['name']] = (int) $this->db->sql_nextid();
80
81
		return $this;
82
	}
83
84
	/**
85
	 * Display Error message
86
	 *
87
	 * @param string $lang_key
88
	 * @param string $args
89
	 *
90
	 * @return null
91
	 * @access protected
92
	 */
93
	protected function display_error_message($lang_key, $args = '')
94
	{
95
		$message = call_user_func_array(array($this->user, 'lang'), array_merge(array(strtoupper($lang_key), $args))) . adm_back_link($this->u_action);
96
		trigger_error($message, E_USER_WARNING);
97
	}
98
99
	/**
100
	 * Run function before do some alter some data in the database
101
	 *
102
	 * @param string $function_name
103
	 *
104
	 * @return bool
105
	 * @access private
106
	 */
107
	private function run_function_before_action($function_name)
108
	{
109
		$func_result = true;
110
		if ($function_name)
111
		{
112
			$func_result = (bool) call_user_func(array($this, $function_name));
113
		}
114
115
		return $func_result;
116
	}
117
118
	/**
119
	 * Save the current settings to the database
120
	 *
121
	 * This must be called before closing or any changes will not be saved!
122
	 * If adding a page (saving for the first time), you must call insert() or an exception will be thrown
123
	 *
124
	 * @param bool $required_fields
125
	 *
126
	 * @return \skouat\ppde\entity\main $this object for chaining calls; load()->set()->save()
127
	 * @access public
128
	 */
129
	public function save($required_fields)
130
	{
131
		if ($required_fields)
132
		{
133
			// The page already exists
134
			$this->display_error_message($this->lang_key_prefix . '_NO_' . $this->lang_key_suffix);
135
		}
136
137
		$sql = 'UPDATE ' . $this->table_name . '
138
			SET ' . $this->db->sql_build_array('UPDATE', $this->data) . '
139
			WHERE ' . $this->table_schema['item_id']['name'] . ' = ' . $this->get_id();
140
		$this->db->sql_query($sql);
141
142
		return $this;
143
	}
144
145
	/**
146
	 * Get id
147
	 *
148
	 * @return int Item identifier
149
	 * @access public
150
	 */
151
	public function get_id()
152
	{
153
		return (isset($this->data[$this->table_schema['item_id']['name']])) ? (int) $this->data[$this->table_schema['item_id']['name']] : 0;
154
	}
155
156
	/**
157
	 * Check the Identifier of the called data exists in the database
158
	 *
159
	 * @param string $sql SQL Query
160
	 *
161
	 * @return bool
162
	 * @access public
163
	 */
164
	public function data_exists($sql)
165
	{
166
		$this->db->sql_query($sql);
167
		$this->set_id($this->db->sql_fetchfield($this->table_schema['item_id']['name']));
168
169
		return (bool) $this->data[$this->table_schema['item_id']['name']];
170
	}
171
172
	/**
173
	 * Set item Identifier
174
	 *
175
	 * @param int $id
176
	 *
177
	 * @return main $this object for chaining calls; load()->set()->save()
178
	 * @access public
179
	 */
180
	public function set_id($id)
181
	{
182
		$this->data[$this->table_schema['item_id']['name']] = (int) $id;
183
184
		return $this;
185
	}
186
187
	/**
188
	 * SQL Query to return the ID of selected currency
189
	 *
190
	 * @return string
191
	 * @access public
192
	 */
193
	public function build_sql_data_exists()
194
	{
195
		return 'SELECT ' . $this->table_schema['item_id']['name'] . '
196
 			FROM ' . $this->table_name . '
197
			WHERE ' . $this->table_schema['item_id']['name'] . ' = ' . $this->data[$this->table_name['item_id']['name']];
198
	}
199
200
	/**
201
	 * Load the data from the database
202
	 *
203
	 * @param int $id
204
	 *
205
	 * @return main $this object for chaining calls; load()->set()->save()
206
	 * @access public
207
	 */
208
	public function load($id)
209
	{
210
		$sql = 'SELECT *
211
			FROM ' . $this->table_name . '
212
			WHERE ' . $this->table_schema['item_id']['name'] . ' = ' . (int) $id;
213
		$result = $this->db->sql_query($sql);
214
		$this->data = $this->db->sql_fetchrow($result);
215
		$this->db->sql_freeresult($result);
216
217
		if ($this->data === false)
218
		{
219
			// A item does not exist
220
			$this->display_error_message($this->lang_key_prefix . '_NO_' . $this->lang_key_suffix);
221
		}
222
223
		return $this;
224
	}
225
226
	/**
227
	 * Get Item name
228
	 *
229
	 * @return string Item name
230
	 * @access public
231
	 */
232
	public function get_name()
233
	{
234
		return (isset($this->data[$this->table_schema['item_name']['name']])) ? (string) $this->data[$this->table_schema['item_name']['name']] : '';
235
	}
236
237
	/**
238
	 * Set Item name
239
	 *
240
	 * @param string $name
241
	 *
242
	 * @return main $this object for chaining calls; load()->set()->save()
243
	 * @access public
244
	 */
245
	public function set_name($name)
246
	{
247
		// Set the item type on our data array
248
		$this->data[$this->table_schema['item_name']['name']] = (string) $name;
249
250
		return $this;
251
	}
252
253
	/**
254
	 * Set page url
255
	 *
256
	 * @param string $u_action Custom form action
257
	 *
258
	 * @return null
259
	 * @access public
260
	 */
261
	public function set_page_url($u_action)
262
	{
263
		$this->u_action = $u_action;
264
	}
265
266
	/**
267
	 * Check if required field are set
268
	 *
269
	 * @return bool
270
	 * @access public
271
	 */
272
	public function check_required_field()
273
	{
274
		return false;
275
	}
276
277
	/**
278
	 * Check we are in the ACP
279
	 *
280
	 * @return bool
281
	 * @access public
282
	 */
283
	public function is_in_admin()
284
	{
285
		return (defined('IN_ADMIN') && isset($this->user->data['session_admin']) && $this->user->data['session_admin']) ? IN_ADMIN : false;
286
	}
287
288
	/**
289
	 * Delete data from the database
290
	 *
291
	 * @param integer $id
292
	 * @param string  $action_before_delete
293
	 *
294
	 * @param string  $sql_where
295
	 *
296
	 * @return bool
297
	 * @access public
298
	 */
299
	public function delete($id, $action_before_delete = '', $sql_where = '')
300
	{
301
		if ($this->disallow_deletion($id) && empty($sql_where))
302
		{
303
			// The item selected does not exists
304
			$this->display_error_message($this->lang_key_prefix . '_NO_' . $this->lang_key_suffix);
305
		}
306
307
		$this->run_function_before_action($action_before_delete);
308
309
		$where_clause = !empty($sql_where) ? $sql_where : ' WHERE ' . $this->table_schema['item_id']['name'] . ' = ' . (int) $id;
310
		// Delete data from the database
311
		$sql = 'DELETE FROM ' . $this->table_name . $where_clause;
312
		$this->db->sql_query($sql);
313
314
		return (bool) $this->db->sql_affectedrows();
315
	}
316
317
	/**
318
	 * Returns if we can proceed to item deletion
319
	 *
320
	 * @param integer $id
321
	 *
322
	 * @return bool
323
	 */
324
	private function disallow_deletion($id)
325
	{
326
		return empty($this->data[$this->table_schema['item_id']['name']]) || ($this->data[$this->table_schema['item_id']['name']] != $id);
327
	}
328
329
	/**
330
	 * Get data from the database
331
	 *
332
	 * @param string $sql
333
	 * @param array  $additional_table_schema
334
	 * @param int    $limit
335
	 * @param        $limit_offset
336
	 *
337
	 * @return array
338
	 * @throws \skouat\ppde\exception\invalid_argument
339
	 * @access public
340
	 */
341
	public function get_data($sql, $additional_table_schema = array(), $limit = 0, $limit_offset = 0)
342
	{
343
		$entities = array();
344
		$result = $this->limit_query($sql, $limit, $limit_offset);
345
346
		while ($row = $this->db->sql_fetchrow($result))
347
		{
348
			// Import each row into an entity
349
			$entities[] = $this->import($row, $additional_table_schema);
350
		}
351
		$this->db->sql_freeresult($result);
352
353
		// Return all page entities
354
		return $entities;
355
	}
356
357
	/**
358
	 * Use query limit if requested
359
	 *
360
	 * @param string  $sql
361
	 * @param integer $limit
362
	 * @param integer $offset
363
	 *
364
	 * @return mixed
365
	 * @access private
366
	 */
367
	private function limit_query($sql, $limit, $offset)
368
	{
369
		return empty($limit) ? $this->db->sql_query($sql) : $this->db->sql_query_limit($sql, $limit, $offset);
370
	}
371
372
	/**
373
	 * Import and validate data
374
	 *
375
	 * Used when the data is already loaded externally.
376
	 * Any existing data on this page is over-written.
377
	 * All data is validated and an exception is thrown if any data is invalid.
378
	 *
379
	 * @param  array $data Data array, typically from the database
380
	 * @param array  $additional_table_schema
381
	 *
382
	 * @return object $this->data object
383
	 * @throws \skouat\ppde\exception\invalid_argument
384
	 * @access public
385
	 */
386
	public function import($data, $additional_table_schema = array())
387
	{
388
		// Clear out any saved data
389
		$this->data = array();
390
391
		// add additional field to the table schema
392
		$this->table_schema = array_merge($this->table_schema, $additional_table_schema);
393
394
		// Go through the basic fields and set them to our data array
395
		foreach ($this->table_schema as $generic_field => $field)
396
		{
397
			// If the data wasn't sent to us, throw an exception
398
			if (!isset($data[$field['name']]))
399
			{
400
				throw new \skouat\ppde\exception\invalid_argument(array($field['name'], 'FIELD_MISSING'));
401
			}
402
403
			// settype passes values by reference
404
			$value = $data[$field['name']];
405
406
			// We're using settype to enforce data types
407
			settype($value, $field['type']);
408
409
			$this->data[$field['name']] = $value;
410
		}
411
		unset($field);
412
413
		return $this->data;
414
	}
415
}
416