Completed
Push — master ( 9b85c2...3f27cb )
by Mario
02:45
created

main::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 9
rs 9.6666
cc 1
eloc 7
nc 1
nop 6
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_warning_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 a user warning message
86
	 *
87
	 * @param string $lang_key
88
	 * @param string $args
89
	 *
90
	 * @return null
91
	 * @access protected
92
	 */
93
	protected function display_warning_message($lang_key, $args = '')
94
	{
95
		$message = call_user_func_array(array($this->user, 'lang'), array_merge(array(strtoupper($lang_key), $args))) . $this->adm_back_link_exists();
96
		trigger_error($message, E_USER_WARNING);
97
	}
98
99
	/**
100
	 * Checks if the adm_back_link function is loaded
101
	 *
102
	 * @return string
103
	 * @access protected
104
	 */
105
	protected function adm_back_link_exists()
106
	{
107
		return (function_exists('adm_back_link')) ? adm_back_link($this->u_action) : '';
108
	}
109
110
	/**
111
	 * Run function before do some alter some data in the database
112
	 *
113
	 * @param string $function_name
114
	 *
115
	 * @return bool
116
	 * @access private
117
	 */
118
	private function run_function_before_action($function_name)
119
	{
120
		$func_result = true;
121
		if ($function_name)
122
		{
123
			$func_result = (bool) call_user_func(array($this, $function_name));
124
		}
125
126
		return $func_result;
127
	}
128
129
	/**
130
	 * Save the current settings to the database
131
	 *
132
	 * This must be called before closing or any changes will not be saved!
133
	 * If adding a page (saving for the first time), you must call insert() or an exception will be thrown
134
	 *
135
	 * @param bool $required_fields
136
	 *
137
	 * @return \skouat\ppde\entity\main $this object for chaining calls; load()->set()->save()
138
	 * @access public
139
	 */
140
	public function save($required_fields)
141
	{
142
		if ($required_fields)
143
		{
144
			// The page already exists
145
			$this->display_warning_message($this->lang_key_prefix . '_NO_' . $this->lang_key_suffix);
146
		}
147
148
		$sql = 'UPDATE ' . $this->table_name . '
149
			SET ' . $this->db->sql_build_array('UPDATE', $this->data) . '
150
			WHERE ' . $this->table_schema['item_id']['name'] . ' = ' . $this->get_id();
151
		$this->db->sql_query($sql);
152
153
		return $this;
154
	}
155
156
	/**
157
	 * Get id
158
	 *
159
	 * @return int Item identifier
160
	 * @access public
161
	 */
162
	public function get_id()
163
	{
164
		return (isset($this->data[$this->table_schema['item_id']['name']])) ? (int) $this->data[$this->table_schema['item_id']['name']] : 0;
165
	}
166
167
	/**
168
	 * Check the Identifier of the called data exists in the database
169
	 *
170
	 * @param string $sql SQL Query
171
	 *
172
	 * @return bool
173
	 * @access public
174
	 */
175
	public function data_exists($sql)
176
	{
177
		$this->db->sql_query($sql);
178
		$this->set_id($this->db->sql_fetchfield($this->table_schema['item_id']['name']));
179
180
		return (bool) $this->data[$this->table_schema['item_id']['name']];
181
	}
182
183
	/**
184
	 * Set item Identifier
185
	 *
186
	 * @param int $id
187
	 *
188
	 * @return main $this object for chaining calls; load()->set()->save()
189
	 * @access public
190
	 */
191
	public function set_id($id)
192
	{
193
		$this->data[$this->table_schema['item_id']['name']] = (int) $id;
194
195
		return $this;
196
	}
197
198
	/**
199
	 * SQL Query to return the ID of selected currency
200
	 *
201
	 * @return string
202
	 * @access public
203
	 */
204
	public function build_sql_data_exists()
205
	{
206
		return 'SELECT ' . $this->table_schema['item_id']['name'] . '
207
 			FROM ' . $this->table_name . '
208
			WHERE ' . $this->table_schema['item_id']['name'] . ' = ' . $this->data[$this->table_name['item_id']['name']];
209
	}
210
211
	/**
212
	 * Load the data from the database
213
	 *
214
	 * @param int $id
215
	 *
216
	 * @return main $this object for chaining calls; load()->set()->save()
217
	 * @access public
218
	 */
219
	public function load($id)
220
	{
221
		$sql = 'SELECT *
222
			FROM ' . $this->table_name . '
223
			WHERE ' . $this->table_schema['item_id']['name'] . ' = ' . (int) $id;
224
		$result = $this->db->sql_query($sql);
225
		$this->data = $this->db->sql_fetchrow($result);
226
		$this->db->sql_freeresult($result);
227
228
		if ($this->data === false)
229
		{
230
			// A item does not exist
231
			$this->display_warning_message($this->lang_key_prefix . '_NO_' . $this->lang_key_suffix);
232
		}
233
234
		return $this;
235
	}
236
237
	/**
238
	 * Get Item name
239
	 *
240
	 * @return string Item name
241
	 * @access public
242
	 */
243
	public function get_name()
244
	{
245
		return (isset($this->data[$this->table_schema['item_name']['name']])) ? (string) $this->data[$this->table_schema['item_name']['name']] : '';
246
	}
247
248
	/**
249
	 * Set Item name
250
	 *
251
	 * @param string $name
252
	 *
253
	 * @return main $this object for chaining calls; load()->set()->save()
254
	 * @access public
255
	 */
256
	public function set_name($name)
257
	{
258
		// Set the item type on our data array
259
		$this->data[$this->table_schema['item_name']['name']] = (string) $name;
260
261
		return $this;
262
	}
263
264
	/**
265
	 * Set page url
266
	 *
267
	 * @param string $u_action Custom form action
268
	 *
269
	 * @return null
270
	 * @access public
271
	 */
272
	public function set_page_url($u_action)
273
	{
274
		$this->u_action = $u_action;
275
	}
276
277
	/**
278
	 * Check if required field are set
279
	 *
280
	 * @return bool
281
	 * @access public
282
	 */
283
	public function check_required_field()
284
	{
285
		return false;
286
	}
287
288
	/**
289
	 * Check we are in the ACP
290
	 *
291
	 * @return bool
292
	 * @access public
293
	 */
294
	public function is_in_admin()
295
	{
296
		return (defined('IN_ADMIN') && isset($this->user->data['session_admin']) && $this->user->data['session_admin']) ? IN_ADMIN : false;
297
	}
298
299
	/**
300
	 * Delete data from the database
301
	 *
302
	 * @param integer $id
303
	 * @param string  $action_before_delete
304
	 *
305
	 * @param string  $sql_where
306
	 *
307
	 * @return bool
308
	 * @access public
309
	 */
310
	public function delete($id, $action_before_delete = '', $sql_where = '')
311
	{
312
		if ($this->disallow_deletion($id) && empty($sql_where))
313
		{
314
			// The item selected does not exists
315
			$this->display_warning_message($this->lang_key_prefix . '_NO_' . $this->lang_key_suffix);
316
		}
317
318
		$this->run_function_before_action($action_before_delete);
319
320
		$where_clause = !empty($sql_where) ? $sql_where : ' WHERE ' . $this->table_schema['item_id']['name'] . ' = ' . (int) $id;
321
		// Delete data from the database
322
		$sql = 'DELETE FROM ' . $this->table_name . $where_clause;
323
		$this->db->sql_query($sql);
324
325
		return (bool) $this->db->sql_affectedrows();
326
	}
327
328
	/**
329
	 * Returns if we can proceed to item deletion
330
	 *
331
	 * @param integer $id
332
	 *
333
	 * @return bool
334
	 */
335
	private function disallow_deletion($id)
336
	{
337
		return empty($this->data[$this->table_schema['item_id']['name']]) || ($this->data[$this->table_schema['item_id']['name']] != $id);
338
	}
339
340
	/**
341
	 * Get data from the database
342
	 *
343
	 * @param string $sql
344
	 * @param array  $additional_table_schema
345
	 * @param int    $limit
346
	 * @param        $limit_offset
347
	 *
348
	 * @return array
349
	 * @access public
350
	 */
351
	public function get_data($sql, $additional_table_schema = array(), $limit = 0, $limit_offset = 0)
352
	{
353
		$entities = array();
354
		$result = $this->limit_query($sql, $limit, $limit_offset);
355
356
		while ($row = $this->db->sql_fetchrow($result))
357
		{
358
			// Import each row into an entity
359
			$entities[] = $this->import($row, $additional_table_schema);
360
		}
361
		$this->db->sql_freeresult($result);
362
363
		// Return all page entities
364
		return $entities;
365
	}
366
367
	/**
368
	 * Use query limit if requested
369
	 *
370
	 * @param string  $sql
371
	 * @param integer $limit
372
	 * @param integer $offset
373
	 *
374
	 * @return mixed
375
	 * @access private
376
	 */
377
	private function limit_query($sql, $limit, $offset)
378
	{
379
		return empty($limit) ? $this->db->sql_query($sql) : $this->db->sql_query_limit($sql, $limit, $offset);
380
	}
381
382
	/**
383
	 * Import and validate data
384
	 *
385
	 * Used when the data is already loaded externally.
386
	 * Any existing data on this page is over-written.
387
	 * All data is validated and an exception is thrown if any data is invalid.
388
	 *
389
	 * @param  array $data Data array, typically from the database
390
	 * @param array  $additional_table_schema
391
	 *
392
	 * @return object $this->data object
393
	 * @access public
394
	 */
395
	public function import($data, $additional_table_schema = array())
396
	{
397
		// Clear out any saved data
398
		$this->data = array();
399
400
		// add additional field to the table schema
401
		$this->table_schema = array_merge($this->table_schema, $additional_table_schema);
402
403
		// Go through the basic fields and set them to our data array
404
		foreach ($this->table_schema as $generic_field => $field)
405
		{
406
			// If the data wasn't sent to us, throw an exception
407
			if (!isset($data[$field['name']]))
408
			{
409
				$this->display_warning_message('EXCEPTION_INVALID_FIELD', $field['name']);
410
			}
411
412
			// settype passes values by reference
413
			$value = $data[$field['name']];
414
415
			// We're using settype to enforce data types
416
			settype($value, $field['type']);
417
418
			$this->data[$field['name']] = $value;
419
		}
420
		unset($field);
421
422
		return $this->data;
423
	}
424
}
425