PageRenderTime 111ms CodeModel.GetById 60ms app.highlight 15ms RepoModel.GetById 25ms app.codeStats 7ms

/halogy/application/modules/blog/controllers/admin.php

https://bitbucket.org/haloweb/halogy-1.0/
PHP | 381 lines | 242 code | 65 blank | 74 comment | 32 complexity | 9090458279d66baca8a87aabef9b7fa2 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 Admin extends Controller {
 20
 21	// set defaults
 22	var $includes_path = '/includes/admin';				// path to includes for header and footer
 23	var $redirect = '/admin/blog/viewall';				// default redirect
 24	var $permissions = array();
 25
 26	function Admin()
 27	{
 28		parent::Controller();
 29
 30		// check user is logged in, if not send them away from this controller
 31		if (!$this->session->userdata('session_admin'))
 32		{
 33			redirect('/admin/login/'.$this->core->encode($this->uri->uri_string()));
 34		}
 35		
 36		// get permissions and redirect if they don't have access to this module
 37		if (!$this->permission->permissions)
 38		{
 39			redirect('/admin/dashboard/permissions');
 40		}
 41		if (!in_array($this->uri->segment(2), $this->permission->permissions))
 42		{
 43			redirect('/admin/dashboard/permissions');
 44		}
 45
 46		// get siteID, if available
 47		if (defined('SITEID'))
 48		{
 49			$this->siteID = SITEID;
 50		}
 51
 52		//  load models and libs
 53		$this->load->model('blog_model', 'blog');
 54		$this->load->library('tags');
 55	}
 56	
 57	function index()
 58	{
 59		redirect($this->redirect);
 60	}
 61	
 62	function viewall()
 63	{
 64		// default where
 65		$where = array();
 66
 67		// set by userID if 'access all' permission is not set
 68		if (!in_array('blog_all', $this->permission->permissions))
 69		{
 70			$where['userID'] = $this->session->userdata('userID');
 71		}
 72		
 73		// grab data and display
 74		$output = $this->core->viewall('blog_posts', $where, array('dateCreated', 'desc'));
 75
 76		$this->load->view($this->includes_path.'/header');
 77		$this->load->view('admin/viewall',$output);
 78		$this->load->view($this->includes_path.'/footer');
 79	}
 80
 81	function add_post()
 82	{
 83		// check permissions for this page
 84		if (!in_array('blog_edit', $this->permission->permissions))
 85		{
 86			redirect('/admin/dashboard/permissions');
 87		}
 88
 89		// get values
 90		$output['data'] = $this->core->get_values('blog_posts');	
 91
 92		// get categories
 93		$output['categories'] = $this->blog->get_categories();
 94
 95		if (count($_POST))
 96		{		
 97			// required
 98			$this->core->required = array(
 99				'postTitle' => array('label' => 'Title', 'rules' => 'required|trim'),
100				'body' => 'Body'
101			);
102			
103			// tidy tags
104			$tags = '';
105			if ($this->input->post('tags'))
106			{
107				foreach (explode(',', $this->input->post('tags')) as $tag)
108				{
109					$tags[] = ucwords(trim(strtolower(str_replace('-', ' ', $tag))));
110				}
111				$tags = implode(', ', $tags);
112			}
113		
114			// set date
115			$this->core->set['dateCreated'] = date("Y-m-d H:i:s");
116			$this->core->set['userID'] = $this->session->userdata('userID');
117			$this->core->set['uri'] = url_title(strtolower($this->input->post('postTitle')));
118			$this->core->set['tags'] = $tags;
119			
120			// update
121			if ($this->core->update('blog_posts'))
122			{
123				$postID = $this->db->insert_id();
124	
125				// update categories
126				$this->blog->update_cats($postID, $this->input->post('catsArray'));
127
128				// update tags
129				$this->tags->update_tags('blog_posts', $postID, $tags);
130							
131				// where to redirect to
132				redirect($this->redirect);
133			}
134		}
135
136		// templates
137		$this->load->view($this->includes_path.'/header');
138		$this->load->view('admin/add_post', $output);
139		$this->load->view($this->includes_path.'/footer');
140	}
141
142	function edit_post($postID)
143	{
144		// check permissions for this page
145		if (!in_array('blog_edit', $this->permission->permissions))
146		{
147			redirect('/admin/dashboard/permissions');
148		}
149		
150		// set object ID
151		$objectID = array('postID' => $postID);
152
153		// get values
154		$output['data'] = $this->core->get_values('blog_posts', $objectID);	
155
156		// get categories
157		$output['categories'] = $this->blog->get_categories();
158
159		// get categories for this post
160		$output['data']['categories'] = $this->blog->get_cats_for_post($postID);
161		
162		if (count($_POST))
163		{
164			// required
165			$this->core->required = array(
166				'postTitle' => array('label' => 'Title', 'rules' => 'required|trim'),
167				'body' => 'Body'
168			);
169	
170			// set date
171			if ($this->input->post('publishDate'))
172			{
173				$seconds = dateFmt($output['data']['dateCreated'], 'H:i:s');
174				$this->core->set['dateCreated'] = date("Y-m-d H:i:s", strtotime($this->input->post('publishDate').' '.$seconds));
175			}
176			
177			// tidy tags
178			$tags = '';
179			if ($this->input->post('tags'))
180			{
181				foreach (explode(',', $this->input->post('tags')) as $tag)
182				{
183					$tags[] = ucwords(trim(strtolower(str_replace('-', ' ', $tag))));
184				}
185				$tags = implode(', ', $tags);
186			}
187
188			// set stuff
189			$this->core->set['dateModified'] = date("Y-m-d H:i:s");
190			$this->core->set['uri'] = url_title(strtolower($this->input->post('postTitle')));
191			$this->core->set['tags'] = $tags;
192			
193			// update
194			if ($this->core->update('blog_posts', $objectID))
195			{
196				// update categories
197				$this->blog->update_cats($postID, $this->input->post('catsArray'));
198
199				// update tags
200				$this->tags->update_tags('blog_posts', $postID, $tags);
201							
202				// set success message
203				$this->session->set_flashdata('success', TRUE);					
204
205				// view page
206				if ($this->input->post('view'))
207				{
208					redirect('/blog/'.dateFmt($output['data']['dateCreated'], 'Y/m').'/'.url_title(strtolower($this->input->post('postTitle'))));
209				}
210				else
211				{																	
212					// where to redirect to
213					redirect('/admin/blog/edit_post/'.$postID);
214				}				
215			}
216		}
217
218		// set message
219		if ($this->session->flashdata('success'))
220		{
221			$output['message'] = '<p>Your changes were saved.</p>';
222		}
223
224		// templates
225		$this->load->view($this->includes_path.'/header');
226		$this->load->view('admin/edit_post', $output);
227		$this->load->view($this->includes_path.'/footer');
228	}
229
230	function delete_post($objectID)
231	{
232		// check permissions for this page
233		if (!in_array('blog_delete', $this->permission->permissions))
234		{
235			redirect('/admin/dashboard/permissions');
236		}		
237		
238		if ($this->core->soft_delete('blog_posts', array('postID' => $objectID)))
239		{
240			// remove category mappings
241			$this->blog->update_cats($objectID);
242			
243			// where to redirect to
244			redirect($this->redirect);
245		}
246	}
247
248	function preview()
249	{
250		// get parsed body
251		$html = $this->template->parse_body($this->input->post('body'));
252
253		// filter for scripts
254		$html = preg_replace('/<script(.*)<\/script>/is', '<em>This block contained scripts, please refresh page.</em>', $html);
255
256		// output
257		$this->output->set_output($html);
258	}
259
260	function comments()
261	{
262		// grab data and display
263		$output['comments'] = $this->blog->get_latest_comments();
264
265		$this->load->view($this->includes_path.'/header');
266		$this->load->view('admin/comments',$output);
267		$this->load->view($this->includes_path.'/footer');
268	}
269
270	function approve_comment($commentID)
271	{
272		if ($this->blog->approve_comment($commentID))
273		{
274			redirect('/admin/blog/comments');
275		}
276	}
277
278	function delete_comment($objectID)
279	{
280		// check permissions for this page
281		if (!in_array('blog_edit', $this->permission->permissions))
282		{
283			redirect('/admin/dashboard/permissions');
284		}
285				
286		if ($this->core->soft_delete('blog_comments', array('commentID' => $objectID)))
287		{
288			// where to redirect to
289			redirect('/admin/blog/comments/');
290		}
291	}
292
293	function categories()
294	{
295		// check permissions for this page
296		if (!in_array('blog_cats', $this->permission->permissions))
297		{
298			redirect('/admin/dashboard/permissions');
299		}
300
301		// get values
302		$output = $this->core->get_values('blog_cats');		
303
304		// get categories
305		$output['categories'] = $this->blog->get_categories();
306
307		if (count($_POST))
308		{				
309			// required fields
310			$this->core->required = array('catName' => 'Category name');
311	
312			// set date
313			$this->core->set['dateCreated'] = date("Y-m-d H:i:s");
314			$this->core->set['catSafe'] = url_title(strtolower(trim($this->input->post('catName'))));
315	
316			// update
317			if ($this->core->update('blog_cats'))
318			{
319				// where to redirect to
320				redirect('/admin/blog/categories');
321			}
322		}
323
324		$this->load->view($this->includes_path.'/header');
325		$this->load->view('admin/categories',$output);
326		$this->load->view($this->includes_path.'/footer');
327	}
328
329	function edit_cat()
330	{
331		// check permissions for this page
332		if (!in_array('blog_cats', $this->permission->permissions))
333		{
334			redirect('/admin/dashboard/permissions');
335		}
336
337		// go through post and edit each list item
338		$listArray = $this->core->get_post();
339		if (count($listArray))
340		{
341			foreach($listArray as $ID => $value)
342			{
343				if ($ID != '' && sizeof($value) > 0 && $value['catName'])
344				{	
345					// set object ID
346					$objectID = array('catID' => $ID);
347					$this->core->set['catName'] = $value['catName'];
348					$this->core->set['catSafe'] = url_title(strtolower(trim($value['catName'])));
349					$this->core->update('blog_cats', $objectID);
350				}
351			}
352		}
353
354		// where to redirect to
355		redirect('/admin/blog/categories');		
356	}	
357
358	function delete_cat($catID)
359	{
360		// check permissions for this page
361		if (!in_array('blog_cats', $this->permission->permissions))
362		{
363			redirect('/admin/dashboard/permissions');
364		}
365				
366		// where
367		$objectID = array('catID' => $catID);	
368		
369		if ($this->core->soft_delete('blog_cats', $objectID))
370		{
371			// where to redirect to
372			redirect('/admin/blog/categories');
373		}		
374	}
375
376	function order($field = '')
377	{
378		$this->core->order(key($_POST), $field);
379	}	
380
381}