1 | <?php |
||
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() |
||
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() |
||
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) |
||
265 | |||
266 | /** |
||
267 | * Check if required field are set |
||
268 | * |
||
269 | * @return bool |
||
270 | * @access public |
||
271 | */ |
||
272 | public function check_required_field() |
||
276 | |||
277 | /** |
||
278 | * Check we are in the ACP |
||
279 | * |
||
280 | * @return bool |
||
281 | * @access public |
||
282 | */ |
||
283 | public function is_in_admin() |
||
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 = '') |
||
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) |
||
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) |
||
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()) |
||
415 | } |
||
416 |