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 = '') |
||
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 = '') |
||
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() |
||
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) |
||
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) |
||
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) |
||
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) |
||
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() |
||
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() |
||
287 | |||
288 | /** |
||
289 | * Check we are in the ACP |
||
290 | * |
||
291 | * @return bool |
||
292 | * @access public |
||
293 | */ |
||
294 | public function is_in_admin() |
||
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 = '') |
||
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) |
||
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) |
||
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) |
||
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()) |
||
424 | } |
||
425 |