PageRenderTime 488ms CodeModel.GetById 220ms app.highlight 90ms RepoModel.GetById 172ms app.codeStats 0ms

/halogy/application/modules/pages/models/pages_model.php

https://bitbucket.org/haloweb/halogy-1.0/
PHP | 724 lines | 473 code | 141 blank | 110 comment | 51 complexity | a7b53db8d5b4f0c3e161486aaedf0d02 MD5 | raw file
  1<?php
  2/**
  3 * Halogy
  4 *
  5 * A user friendly, modular content management system for PHP 5.0
  6 * Built on CodeIgniter - http://codeigniter.com
  7 *
  8 * @package		Halogy
  9 * @author		Haloweb Ltd.
 10 * @copyright	Copyright (c) 2008-2011, Haloweb Ltd.
 11 * @license		http://halogy.com/license
 12 * @link		http://halogy.com/
 13 * @since		Version 1.0
 14 * @filesource
 15 */
 16
 17// ------------------------------------------------------------------------
 18
 19class Pages_model extends Model {
 20
 21	var $siteID;
 22	
 23	function Pages_model()
 24	{
 25		parent::Model();
 26		
 27		$this->table = 'pages';
 28
 29		if (!$this->siteID)
 30		{
 31			$this->siteID = SITEID;
 32		}
 33	}
 34
 35	function get_pages()
 36	{
 37		$this->db->where('pages.siteID', $this->siteID);
 38		$this->db->where('deleted', 0);
 39
 40		// if user has limited access, find those pages
 41		if (!in_array('pages_all', $this->permission->permissions))
 42		{
 43			$this->db->where('groupID', $this->session->userdata('groupID'));
 44		}
 45
 46		// join versions
 47		$this->db->select('pages.*, page_versions.userID', FALSE);
 48		$this->db->join('page_versions', 'pages.draftID = page_versions.versionID', 'left');
 49
 50		$this->db->order_by('pageOrder');		
 51
 52		$query = $this->db->get($this->table);
 53		
 54		if ($query->num_rows() > 0)
 55		{
 56			return $query->result_array();
 57		}
 58		else
 59		{
 60			return FALSE;
 61		}
 62	}
 63
 64	function get_page_parents()
 65	{
 66		// default where
 67		$this->db->where('pages.siteID', $this->siteID);
 68		$this->db->where('deleted', 0);
 69
 70		// if user has limited access, find those pages
 71		if (!in_array('pages_all', $this->permission->permissions))
 72		{
 73			$this->db->where('groupID', $this->session->userdata('groupID'));
 74		}
 75
 76		// where parent is set
 77		$this->db->where('parentID', 0);
 78
 79		// find out if its modified
 80		$this->db->select(' (SELECT COUNT(*) FROM '.$this->db->dbprefix.'page_blocks WHERE '.$this->db->dbprefix.'page_blocks.versionID = '.$this->db->dbprefix.'pages.draftID AND '.$this->db->dbprefix.'page_blocks.dateCreated > dateModified) AS newBlocks', FALSE);
 81		
 82		// find out if its modified
 83		$this->db->select(' (SELECT COUNT(*) FROM '.$this->db->dbprefix.'page_versions WHERE '.$this->db->dbprefix.'page_versions.pageID = '.$this->db->dbprefix.'pages.pageID AND '.$this->db->dbprefix.'page_versions.dateCreated > datePublished) AS newVersions', FALSE);
 84
 85		// join versions
 86		$this->db->select('pages.*, page_versions.userID', FALSE);
 87		$this->db->join('page_versions', 'pages.draftID = page_versions.versionID', 'left');
 88		
 89		$this->db->order_by('pageOrder', 'asc');
 90		
 91		$query = $this->db->get('pages');
 92		
 93		if ($query->num_rows())
 94		{
 95			return $query->result_array();
 96		}
 97		else
 98		{
 99			return FALSE;
100		}		
101	}
102
103	function get_page_children($pageID = '')
104	{
105		// default where
106		$this->db->where('pages.siteID', $this->siteID);
107		$this->db->where('deleted', 0);
108
109		// if user has limited access, find those pages
110		if (!in_array('pages_all', $this->permission->permissions))
111		{
112			$this->db->where('groupID', $this->session->userdata('groupID'));
113		}
114		
115		// get page by ID
116		$this->db->where('parentID', $pageID);
117		
118		// find out if its modified
119		$this->db->select(' (SELECT COUNT(*) FROM '.$this->db->dbprefix.'page_blocks WHERE '.$this->db->dbprefix.'page_blocks.versionID = '.$this->db->dbprefix.'pages.draftID AND '.$this->db->dbprefix.'page_blocks.dateCreated > dateModified) AS newBlocks', FALSE);
120		
121		// find out if its modified
122		$this->db->select(' (SELECT COUNT(*) FROM '.$this->db->dbprefix.'page_versions WHERE '.$this->db->dbprefix.'page_versions.pageID = '.$this->db->dbprefix.'pages.pageID AND '.$this->db->dbprefix.'page_versions.dateCreated > datePublished) AS newVersions', FALSE);		
123
124		// join versions
125		$this->db->select('pages.*, page_versions.userID', FALSE);
126		$this->db->join('page_versions', 'pages.draftID = page_versions.versionID', 'left');
127		
128		$this->db->order_by('pageOrder', 'asc');
129		
130		$query = $this->db->get('pages');
131		
132		if ($query->num_rows())
133		{
134			return $query->result_array();
135		}
136		else
137		{
138			return FALSE;
139		}		
140	}
141
142	function get_template($templateID = '')
143	{
144		// default where
145		$this->db->where('t1.siteID', $this->siteID, FALSE);
146		$this->db->where('t1.deleted', 0, FALSE);
147		$this->db->where('t1.templateID', $templateID, FALSE);
148
149		// select
150		$this->db->select('t1.*, t2.body, t2.dateCreated, t2.userID');
151
152		// join revisions
153		$this->db->join('template_versions t2', 't2.versionID = t1 . versionID', 'left');
154		
155		// get em
156		$query = $this->db->get('templates t1', 1);
157		
158		if ($query->num_rows())
159		{
160			return $query->row_array();
161		}
162		else
163		{
164			return FALSE;
165		}
166	}
167
168	function get_templates($type = '')
169	{
170		// default where
171		$this->db->where('t1.siteID', $this->siteID, FALSE);
172		$this->db->where('t1.deleted', 0, FALSE);
173
174		// don't show modules
175		if ($type == 'page')
176		{
177			$this->db->where('modulePath', '');
178		}
179		elseif ($type == 'module')
180		{
181			$this->db->where('modulePath !=', '');
182		}
183		
184		// select
185		$this->db->select('t1.*, t2.body, t2.dateCreated, t2.userID');
186
187		// join revisions
188		$this->db->join('template_versions t2', 't2.versionID = t1 . versionID', 'left');
189
190		// order
191		$this->db->order_by('modulePath', 'asc');
192		$this->db->order_by('templateName', 'asc');
193		
194		// get all templates		
195		$query = $this->db->get('templates t1');
196		
197		if ($query->num_rows())
198		{
199			return $query->result_array();
200		}
201		else
202		{
203			return FALSE;
204		}
205	}
206
207	function get_template_count($templateID)
208	{
209		// count
210		$this->db->select('COUNT(*) as numPages');
211
212		// default where
213		$this->db->where(array('siteID' => $this->siteID, 'deleted' => 0));
214
215		// where
216		$this->db->where('templateID', $templateID);
217		
218		$query = $this->db->get('pages');
219	
220		if ($query->num_rows())
221		{
222			$row = $query->row_array();
223			return $row['numPages'];
224		}
225		else
226		{
227			return false;
228		}
229	}
230
231	function get_template_versions($templateID)
232	{
233		$this->db->where('objectID', $templateID);
234
235		$this->db->order_by('dateCreated', 'desc');
236	
237		$query = $this->db->get('template_versions', 30);
238		
239		// get data
240		if ($query->num_rows())
241		{
242			return $query->result_array();
243		}
244		else
245		{
246			return false;
247		}		
248	}
249
250	function get_include($includeRef = '', $includeID = '')
251	{
252		// default where
253		$this->db->where('t1.siteID', $this->siteID, FALSE);
254		$this->db->where('t1.deleted', 0, FALSE);
255
256		// get by reference
257		if ($includeRef)
258		{
259			$this->db->where('includeRef', $includeRef);
260		}
261
262		// get by ID
263		elseif ($includeID)
264		{
265			$this->db->where('includeID', $includeID);
266		}
267
268		// or fail
269		else
270		{
271			return FALSE;
272		}
273
274		// select
275		$this->db->select('t1.*, t2.body, t2.dateCreated, t2.userID');
276
277		// join revisions
278		$this->db->join('include_versions t2', 't2.versionID = t1 . versionID', 'left');
279		
280		// get em
281		$query = $this->db->get('includes t1', 1);
282		
283		if ($query->num_rows())
284		{
285			return $query->row_array();
286		}
287		else
288		{
289			return FALSE;
290		}		
291	}
292
293	function get_includes($type = '')
294	{
295		// default where
296		$this->db->where('t1.siteID', $this->siteID, FALSE);
297		$this->db->where('t1.deleted', 0, FALSE);
298
299		// get by type
300		if ($type)
301		{
302			$this->db->where('type', $type);
303		}
304		
305		// select
306		$this->db->select('t1.*, t2.body, t2.dateCreated, t2.userID');
307
308		// join revisions
309		$this->db->join('include_versions t2', 't2.versionID = t1 . versionID', 'left');
310
311		// order
312		$this->db->order_by('includeRef', 'asc');
313		
314		// get all includes		
315		$query = $this->db->get('includes t1');
316		
317		if ($query->num_rows())
318		{
319			return $query->result_array();
320		}
321		else
322		{
323			return FALSE;
324		}
325	}
326
327	function get_include_versions($includeID)
328	{
329		$this->db->where('objectID', $includeID);
330
331		$this->db->order_by('dateCreated', 'desc');
332	
333		$query = $this->db->get('include_versions', 30);
334		
335		// get data
336		if ($query->num_rows())
337		{
338			return $query->result_array();
339		}
340		else
341		{
342			return FALSE;
343		}		
344	}
345
346	function add_temp_page()
347	{
348		// create the page
349		$this->db->set('siteID', $this->siteID);
350		$this->db->set('dateCreated', date("Y-m-d H:i:s"));
351		$this->db->set('pageName', '');
352		$this->db->set('deleted', 1);
353		$this->db->insert('pages');
354		$pageID = $this->db->insert_id();
355
356		// create the draft
357		$this->core->add_draft($pageID);
358
359		return $pageID;
360	}
361
362	function add_page_nav($pageName, $path)
363	{
364		// check nav isnt there already
365		$this->db->where('siteID', $this->siteID);
366		$this->db->where('deleted', 0);
367		if ($path == 'home')
368		{
369			$this->db->where('(uri = "/" OR uri = "home")');
370		}
371		else
372		{
373			$this->db->where('uri', $path);
374		}
375		$query = $this->db->get('navigation', 1);
376
377		// add nav
378		if (!$query->num_rows())
379		{
380			$this->db->set('siteID', $this->siteID);
381			$this->db->set('dateCreated', date("Y-m-d H:i:s"));			
382			$this->db->set('uri', $path);
383			$this->db->set('navName', $pageName);
384			$this->db->insert('navigation');
385		}
386
387		return TRUE;
388	}
389	
390	function import_template($file, $body)
391	{		
392		// set flags
393		$success = FALSE;
394		$includes = FALSE;		
395
396		// get file info
397		$filenames = explode('.', $file);
398		$filename = substr($file, 0, strpos($file, '.'.end($filenames)));
399		$extension = end($filenames);
400		
401		// add html template (and includes)
402		if ($extension == 'html' || $extension == 'htm')
403		{
404			// parse template
405			$body = preg_replace('/<title>(.*)<\/title>/i', '<title>{page:title}</title>', $body);
406			$body = preg_replace('/meta name="keywords" content="(.*)"/i', 'meta name="keywords" content="{page:keywords}"', $body);
407			$body = preg_replace('/meta name="description" content="(.*)"/i', 'meta name="description" content="{page:description}"', $body);
408			$body = preg_replace('/<!--NAVIGATION-->/i', '{navigation}', $body);
409
410			// get template name
411			$templateName = url_title(trim(ucfirst($filename)));
412
413			// see if template is a module
414			$module = (in_array(strtolower($templateName), $this->template->moduleTemplates)) ? strtolower($templateName) : false;
415
416			// get theme name
417			$theme = '';
418			$themeRef = '';
419			if (preg_match('/meta name="theme" content="(.*)"|meta content="(.*)" name="theme"/i', $body, $matches))
420			{
421				$theme = substr(trim($matches[1]), 0, 15);
422				$themeRef = url_title(trim(strtolower($theme))).'-';
423			}		
424
425			// find out if header is in there
426			if (preg_match('/<!--CONTENT-->/i', $body))
427			{
428				$split = preg_split('/<!--CONTENT-->/i', $body);
429				$header = preg_replace('/<!--CONTENT-->/i', '', $split[0]);
430				$content = $split[1];
431
432				// get file name
433				$includeRef = $themeRef.'header';
434
435				$this->add_include($includeRef, $header, 'H');
436
437				$includes = TRUE;
438			}
439
440			// find out if footer is in there
441			if (preg_match('/<!--ENDCONTENT-->/i', $body))
442			{
443				$split = preg_split('/<!--ENDCONTENT-->/i', $body);
444				$footer = $split[1];
445
446				// remove footer from content
447				$content = str_replace($footer, '', $content);
448				$content = preg_replace('/<!--ENDCONTENT-->/i', '', $content);
449				
450				// get file name
451				$includeRef = $themeRef.'footer';
452
453				$this->add_include($includeRef, $footer, 'H');
454
455				$includes = TRUE;
456			}
457
458			// put the header and footer tags in
459			if ($includes)
460			{
461				$content = "{include:".$themeRef."header}\n\n$content\n\n{include:".$themeRef."footer}";
462			}
463
464			// otherwise just set the template content
465			else
466			{
467				$content = $body;
468			}
469
470			// look for blocks
471			preg_match_all('/<!--BLOCK-->/i', $content, $matches);
472			for ($x=0; $x<sizeof($matches[0]); $x++)
473			{
474				$content = preg_replace('/<!--BLOCK-->/i', '{block'.($x+1).'}', $content, 1);
475			}
476			
477			// get file name
478			$templateName = ($theme) ? '['.$theme.'] '.$templateName : $templateName;
479
480			$templateID = $this->add_template($templateName, $content, $module);			
481		}
482
483		// add css file
484		elseif ($extension == 'css')
485		{
486			// get file name
487			$includeRef = $filename.'.css';
488
489			$this->add_include($includeRef, $body, 'C');
490		}
491
492		// add js file
493		elseif ($extension == 'js')
494		{
495			// get file name
496			$includeRef = $filename.'.js';
497
498			$this->add_include($includeRef, $body, 'J');
499		}
500
501		return (@$templateID) ? $templateID : TRUE;
502	}
503
504	function add_template($templateName, $body, $module = '')
505	{
506		// find out if template exists
507		$this->db->where('siteID', $this->siteID);
508		$this->db->where('deleted', 0);
509		if ($module)
510		{
511			$this->db->where('modulePath', $module);
512		}
513		else
514		{
515			$this->db->where('templateName', $templateName);			
516			$this->db->where('modulePath', '');
517		}
518		$query = $this->db->get('templates', 1);
519
520		// add template version
521		if ($query->num_rows())
522		{
523			$row = $query->row_array();
524			$templateID = $row['templateID'];
525
526			// add template version
527			$versionID = $this->add_template_version($templateID, $body);
528		}
529
530		// add template
531		else
532		{
533			$this->db->set('siteID', $this->siteID);
534			$this->db->set('dateCreated', date("Y-m-d H:i:s"));
535			$this->db->set('templateName', $templateName);
536			if ($module)
537			{
538				$this->db->set('modulePath', $module);
539			}
540			$this->db->insert('templates');
541			$templateID = $this->db->insert_id();
542
543			// add template version
544			$versionID = $this->add_template_version($templateID, $body);
545		}
546
547		return $templateID;
548	}
549
550	function add_template_version($templateID, $body = '')
551	{
552		// set body
553		$body = ($this->input->post('body')) ? $this->input->post('body') : $body;
554
555		// filter body
556		$body = htmlentities(iconv('UTF-8', 'UTF-8//IGNORE', $body), NULL, 'UTF-8');
557		$body = html_entity_decode($body, NULL, 'UTF-8');
558		
559		// check page
560		if (!$data = $this->get_template($templateID))
561		{
562			return FALSE;
563		}
564
565		// check version is not the same as latest one
566		if ($versions = $this->get_template_versions($templateID))
567		{
568			if ($versions[0]['body'] == $body)
569			{	
570				return FALSE;
571			}
572		}
573
574		// check version is not the same as current one
575		if ($data['body'] == $body)
576		{			
577			return FALSE;
578		}
579		
580		// add version
581		$this->db->set('objectID', $templateID);
582		$this->db->set('dateCreated', date("Y-m-d H:i:s"));
583		$this->db->set('userID', $this->session->userdata('userID'));
584		$this->db->set('body', $body);
585		$this->db->set('siteID', $this->siteID);
586		
587		$this->db->insert('template_versions');
588
589		// get version ID
590		$versionID = $this->db->insert_id();
591
592		// update page draft
593		$this->db->set('versionID', $versionID);
594		$this->db->where('siteID', $this->siteID);
595		$this->db->where('templateID', $templateID);
596		$this->db->update('templates');
597		
598		return $versionID;
599	}
600
601	function revert_template($templateID, $versionID)
602	{
603		// update the template with version
604		$this->db->set('versionID', $versionID);
605		$this->db->where('templateID', $templateID);
606		$this->db->update('templates');
607
608		return TRUE;
609	}
610
611	function add_include($includeRef, $body, $type, $siteID = TRUE)
612	{
613		// find out if include exists
614		if ($siteID)
615		{
616			$this->db->where('siteID', $this->siteID);
617		}
618		$this->db->where('deleted', 0);
619		$this->db->where('includeRef', $includeRef);
620		$query = $this->db->get('includes', 1);
621
622		// add include version
623		if ($query->num_rows())
624		{
625
626			$row = $query->row_array();
627
628			// add template version
629			$versionID = $this->add_include_version($row['includeID'], $body);
630		}
631
632		// add include
633		else
634		{
635			if ($siteID)
636			{
637				$this->db->set('siteID', $this->siteID);
638			}
639			$this->db->set('dateCreated', date("Y-m-d H:i:s"));
640			$this->db->set('includeRef', $includeRef);
641			$this->db->set('type', $type);
642			$this->db->insert('includes');
643			$includeID = $this->db->insert_id();
644
645			// add template version
646			$versionID = $this->add_include_version($includeID, $body);
647		}
648
649		return TRUE;
650	}	
651
652	function add_include_version($includeID, $body = '')
653	{
654		// set body
655		$body = ($this->input->post('body')) ? $this->input->post('body') : $body;
656
657		// filter body
658		$body = htmlentities($body, NULL, 'UTF-8');
659		$body = html_entity_decode($body, NULL, 'UTF-8');
660		
661		// check page
662		if (!$data = $this->get_include(NULL, $includeID))
663		{
664			return FALSE;
665		}
666
667		// check version is not the same as latest one
668		if ($versions = $this->get_include_versions($includeID))
669		{
670			if ($versions[0]['body'] == $body)
671			{	
672				return FALSE;
673			}
674		}
675
676		// check version is not the same as current one
677		if ($data['body'] == $body)
678		{			
679			return FALSE;
680		}
681		
682		// add version
683		$this->db->set('objectID', $includeID);
684		$this->db->set('dateCreated', date("Y-m-d H:i:s"));
685		$this->db->set('userID', $this->session->userdata('userID'));
686		$this->db->set('body', $body);
687		$this->db->set('siteID', $this->siteID);
688		
689		$this->db->insert('include_versions');
690
691		// get version ID
692		$versionID = $this->db->insert_id();
693
694		// update page draft
695		$this->db->set('versionID', $versionID);
696		$this->db->where('siteID', $this->siteID);
697		$this->db->where('includeID', $includeID);
698		$this->db->update('includes');
699		
700		return $versionID;
701	}
702
703	function revert_include($includeID, $versionID)
704	{
705		// update the include with version
706		$this->db->set('versionID', $versionID);
707		$this->db->where('includeID', $includeID);
708		$this->db->update('includes');
709
710		return TRUE;
711	}
712
713	function update_children($pageID)
714	{
715		// update page draft
716		$this->db->set('parentID', 0);
717		$this->db->where('siteID', $this->siteID);
718		$this->db->where('parentID', $pageID);
719		$this->db->update('pages');
720
721		return TRUE;
722	}
723	
724}