· 7 years ago · Jan 18, 2019, 11:28 AM
1<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2
3/**
4 * Q2 Module
5 *
6 * Prototype for basic (but full-fledged) CMS logic.
7 */
8
9class Q2_module extends [KUSI ANTON]_Module {
10
11 public function __construct()
12 {
13 parent::__construct();
14
15 $this->module = __CLASS__;
16 $this->info = 'Q2 module';
17 $this->authors = '[KUSI ANTON]';
18
19 $this->dependencies = array(
20 'auth',
21 'user_module',
22 'content'
23 );
24
25 $this->validate();
26 $this->load_assets();
27
28 $this->table = array(
29 'pages' => $this->prefix . '_q2_pages',
30 'content_links' => $this->prefix . '_q2_clinks'
31 );
32
33 $this->load_config();
34 $this->install();
35
36 $json_file = __DIR__ . '/website/structure.json.php';
37
38 if( ! file_exists($json_file))
39 {
40 show_error('Q2: Structure JSON file missing!');
41 }
42
43 include $json_file;
44
45 $this->structure = json_decode($json, true);
46 $this->page_id = 0;
47
48 $this->url_prefix = $this->config_item('url_prefix');
49 $this->ok_roles = $this->config_item('ok_roles');
50 }
51
52
53 /////////////////////
54 // Public methods //
55 /////////////////////
56
57
58 /**
59 * Does the currently logged in user have the rights to administer pages?
60 * @uses user_module
61 * @return boolean
62 */
63 public function has_rights()
64 {
65 return has_role($this->ok_roles);
66 }
67
68
69 /**
70 * Get module's admin index view
71 * @return string HTML view
72 */
73 public function get_admin_index()
74 {
75 return $this->load_view('admin_index', array('templates' => $this->structure['templates']));
76 }
77
78
79 /**
80 * Get module's admin form for creating a new sub page
81 * @param integer $parent_id Possible parent page ID
82 * @return string HTML view
83 */
84 public function get_admin_subpage_form($parent_id = 0)
85 {
86 return $this->load_view('admin_subpage_form', array('templates' => $this->structure['templates']));
87 }
88
89
90 /**
91 * Resolve URI string to page ID
92 * @param string $uri i.e. 'some_page/sub_page'
93 * @return integer Page ID
94 */
95 public function uri_to_page_id($uri)
96 {
97 $this->CI->db->select('id');
98 $this->CI->db->from($this->table['pages']);
99 $this->CI->db->where('uri', $uri);
100 $this->CI->db->where('status', 1);
101 if($this->has_rights() === false)
102 {
103 $this->CI->db->where('preview', 0);
104 }
105 $data = $this->CI->db->get()->result_array();
106
107 if(empty($data))
108 {
109 // "404"
110 return false;
111 }
112
113 $this->page_id = $data[0]['id'];
114
115 return $data[0]['id'];
116 }
117
118
119 /**
120 * Render a page
121 * @param integer $id Page ID
122 * @return string HTML view (template)
123 */
124 public function render_page($id)
125 {
126 // Get some info on our page first
127 $this->CI->db->select('*');
128 $this->CI->db->from($this->table['pages']);
129 $this->CI->db->where('id', $id);
130 $data = $this->CI->db->get()->result_array();
131 $page = $data[0];
132
133 // What type are we (template)?
134 $template_id = $page['template_id'];
135
136 $page_data = $this->get_page_data($id);
137
138 $my_contents = array();
139
140 // Get our contents
141 $my_contents = $this->get_contents_for_page($id);
142
143 $rendered = $this->load_view('templates/' . $template_id, array('page' => $page_data, 'data' => $my_contents));
144
145 $this->current_page_id = $id;
146
147 return $rendered;
148 }
149
150
151 /**
152 * Deactivate a page
153 * @param integer $id Page ID
154 * @return null
155 */
156 public function delete_page($id)
157 {
158 $this->CI->db->where('id', $id);
159 $this->CI->db->set('status', '0');
160 $this->CI->db->update($this->table['pages']);
161
162 // TODO: Get rid of all of my child pages, too?
163 }
164
165
166 /**
167 * Get all DB cols for a page
168 * @param integer $id Page ID
169 * @return array
170 */
171 public function get_page_data($id)
172 {
173 $this->CI->db->select('*');
174 $this->CI->db->from($this->table['pages']);
175 $this->CI->db->where('id', $id);
176 $data = $this->CI->db->get()->result_array();
177
178 return empty($data) ? false : $data[0];
179 }
180
181
182 /**
183 * Get content IDs for a page
184 * @param integer $id Page ID
185 * @return array Assoc. array with field name as key and content ID as val
186 */
187 public function get_contents_for_page($id)
188 {
189 $this->CI->db->select('*');
190 $this->CI->db->from($this->table['content_links']);
191 $this->CI->db->where('page_id', $id);
192 $data = $this->CI->db->get()->result_array();
193
194 $formatted = array();
195
196 foreach($data as $i)
197 {
198 $name = $i['name'];
199 unset($i['name']);
200 $formatted[$name] = $i;
201 }
202
203 return $formatted;
204 }
205
206
207 /**
208 * Create a new page
209 * @param array $data
210 * @return integer ID of newly created row
211 */
212 public function create_page($data)
213 {
214 // Do we have a parent?
215 $parent_id = $data['parent_id'];
216 if($parent_id > 0)
217 {
218 // We do have a parent. Check its uri
219 $parent_data = $this->get_page_data($parent_id);
220 $data['uri'] = $parent_data['uri'] . '/' . $data['uri'];
221 }
222
223 // Throw this away
224 unset($data['parent_id']);
225
226 // Check that the URI is unique
227 $running_number = 1;
228 while( ! $this->uri_is_unique($data['uri']))
229 {
230 if($running_number > 1)
231 {
232 $data['uri'] = substr_replace($data['uri'] , '', -1);
233 }
234 $data['uri'] = $segment . $running_number;
235 $running_number ++;
236 }
237
238 // Insert the new page row
239 $this->CI->db->insert($this->table['pages'], $data);
240
241 // Grab the freshly created page's PK
242 $page_id = mysql_insert_id();
243
244 // Create placeholders etc.
245 $fields = $this->get_fields_for_template($data['template_id']);
246 foreach($fields as $id => $i)
247 {
248 $placeholder_content = ' - '; // TODO
249 $this->add_content($page_id, $id, $placeholder_content);
250 }
251
252 return $page_id;
253 }
254
255
256 /**
257 * Change the preview col for a page
258 * @param integer $id Page ID
259 * @param boolean $bool Preview = 1/0?
260 * @return null
261 */
262 public function change_preview_status_for_page($id, $bool = false)
263 {
264 $new_value = $bool === true ? '1' : '0';
265
266 $this->CI->db->where('id', $id);
267 $this->CI->db->set('preview', $new_value);
268 $this->CI->db->update($this->table['pages']);
269 }
270
271
272 /**
273 * Add a content link row for certain page
274 * @uses content (module)
275 * @param integer $page_id Page ID
276 * @param string $id Content alias, i.e. 'title'
277 * @param string $content Placeholder string for empty content
278 * @return null
279 */
280 public function add_content($page_id, $id, $content)
281 {
282 $content_id = $this->CI->content->create_content($content);
283
284 $data['page_id'] = $page_id;
285 $data['name'] = $id;
286 $data['content_id'] = $content_id;
287
288 $this->CI->db->insert($this->table['content_links'], $data);
289 }
290
291
292 /**
293 * Get fields for template ID
294 * @param integer $template_id Template ID
295 * @return array
296 */
297 public function get_fields_for_template($template_id)
298 {
299 $template = $this->structure['templates'][$template_id];
300 $fields = $template['content_types'];
301
302 return $fields;
303 }
304
305
306 /**
307 * Is the given URI string unique?
308 * @param string $uri i.e. 'sub_page'
309 * @return boolean
310 */
311 public function uri_is_unique($uri)
312 {
313 $this->CI->db->select('*');
314 $this->CI->db->from($this->table['pages']);
315 $this->CI->db->where('uri', $uri);
316 $this->CI->db->where('status', 1);
317 $data = $this->CI->db->get()->result_array();
318
319 return empty($data) ? true : false;
320 }
321
322
323 /**
324 * Get URI for page
325 * @param integer $page_id Page ID
326 * @return string URI string, i.e. 'page/sub_page'
327 */
328 public function get_uri_by_id($page_id)
329 {
330 $this->CI->db->select('uri');
331 $this->CI->db->from($this->table['pages']);
332 $this->CI->db->where('id', $page_id);
333 $data = $this->CI->db->get()->result_array();
334
335 return empty($data) ? false : $data[0]['uri'];
336 }
337
338
339 /**
340 * Get set URL prefix
341 * @return string i.e. 'p/'
342 */
343 public function get_url_prefix()
344 {
345 return $this->url_prefix;
346 }
347
348
349 /**
350 * TODO: Placeholder
351 * @param [type] $page_id [description]
352 * @return [type] [description]
353 */
354 public function get_meta_for_page($page_id)
355 {
356 // TODO...
357 }
358
359
360 /**
361 * Get view for page controls
362 * @return string HTML view
363 */
364 public function get_page_tools()
365 {
366 if( ! $this->has_rights() or $this->page_id === 0)
367 {
368 return;
369 }
370
371 $current_page_id = $this->page_id;
372
373 return $this->load_view('admin_page_tools', array('current_page_id' => $current_page_id));
374 }
375
376
377 /**
378 * Update URI col for page
379 * @param integer $id Page ID
380 * @param string $uri i.e. 'page/sub_page'
381 * @return null
382 */
383 public function update_page_uri($id, $uri)
384 {
385 $this->CI->db->where('id', $id);
386 $this->CI->db->set('uri', $uri);
387 $this->CI->db->update($this->table['pages']);
388
389 // TODO: If this page has kids, their URI strings need
390 // to be updated too!
391 }
392
393
394 /////////////////////
395 // Private methods //
396 /////////////////////
397
398
399 private function install()
400 {
401 if( ! $this->CI->config->item('enable_module_installs'))
402 {
403 return;
404 }
405
406 if($this->CI->db->table_exists($this->table['pages']))
407 {
408 // We're good
409 return;
410 }
411
412 // Create tables
413 $this->CI->db->query("
414 CREATE TABLE IF NOT EXISTS `" . $this->table['pages'] . "` (
415 `id` int(11) NOT NULL AUTO_INCREMENT,
416 `template_id` varchar(50) DEFAULT 'default',
417 `sort_order` int(11) DEFAULT '0',
418 `uri` varchar(50) DEFAULT NULL,
419 `preview` tinyint(1) DEFAULT '1',
420 `status` tinyint(1) DEFAULT '1',
421 PRIMARY KEY (`id`),
422 KEY `uri` (`uri`,`preview`),
423 KEY `asd` (`template_id`,`status`)
424 ) ENGINE=MyISAM AUTO_INCREMENT=18 DEFAULT CHARSET=latin1
425
426 CREATE TABLE IF NOT EXISTS `" . $this->table['content_links'] . "` (
427 `name` varchar(20) DEFAULT NULL,
428 `rich` tinyint(1) DEFAULT '1',
429 `file` tinyint(1) DEFAULT '0',
430 `page_id` int(11) DEFAULT NULL,
431 `content_id` int(11) NOT NULL,
432 PRIMARY KEY (`content_id`),
433 KEY `-` (`name`,`page_id`)
434 ) ENGINE=MyISAM DEFAULT CHARSET=latin1
435 ");
436 }
437
438}
439
440// EOF