Codeigniter shopping cart v1.1 Part 4: pages module

Home of Page module shows ID, Name, Full Path, Status and Actions.

Download Page

Pages module

We use jquery dataTables to show our information. This will allow us to sort by each column, display different number of rows and seach.

If you click ‘active’ in a Status column, it will change to inactive. And vice versa for inactive.

There will be edit and delete button to do each action.


There will be Name, Keywords, Description, Path/FURL, content fields and Status drop-down in ‘Create Page’.
Keywords can be set up to show keywords in each meta tag.
And description as well.
You need to set Path which will be used in URL. You need to use one word or use under score _ for this purpose.

We use TinyMCE for the content area.

And active and inactive will be in Status drop-down.


Edit Page will have the same field as the Create Page.

Module structure

You can see the page structure in this image. Please click to enlarge the image.

Database

CREATE TABLE IF NOT EXISTS `omc_page` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL DEFAULT '',
  `keywords` varchar(255) NOT NULL DEFAULT '',
  `description` varchar(255) NOT NULL DEFAULT '',
  `path` varchar(255) NOT NULL DEFAULT '',
  `content` text NOT NULL,
  `status` enum('active','inactive') NOT NULL DEFAULT 'active',
  `category_id` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=40 ;

admin controller

Please read the comments in the following code. modules/pages/controllers/admin.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Admin extends Admin_Controller {
	function Admin(){
		   parent::Admin_Controller();
		   // Check for access permission
			check('Pages');
			// Load modules/menus/models/MMenus
			$this->load->module_model('menus','MMenus');
			// Load pages model
			$this->load->model('MPages');
			// Set breadcrumb
			$this->bep_site->set_crumb($this->lang->line('backendpro_pages'),'pages/admin');
	}

	function index(){
			// we use the following variables in the view
			$data['title'] = "Manage Pages";
			$data['pages'] = $this->MPages->getAllPages();
			$data['header'] = $this->lang->line('backendpro_access_control');
			// This how Bep load views
			$data['page'] = $this->config->item('backendpro_template_admin') . "admin_pages_home";
			$data['module'] = 'pages';
			$this->load->view($this->_container,$data);
	}

	function create(){
		// We need TinyMCE, so load it
	  	$this->bep_assets->load_asset_group('TINYMCE');
	   	if ($this->input->post('name')){
	   		// if info is filled in then do this
	  		$this->MPages->addPage();
	  		// This is CI way to show flashdata
	  		// $this->session->set_flashdata('message','Page created');
	  		// But here we use Bep way to display flash msg
	  		flashMsg('success','Page created');
	  		// and redirect to this index page
	  		redirect('pages/admin/index','refresh');
	  	}else{
	  		// this must be first visit to the creat page
			$data['title'] = "Create Page";
			$data['menus'] = $this->MMenus->getAllMenusDisplay();
			// Set breadcrumb
			$this->bep_site->set_crumb($this->lang->line('userlib_page_create'),'pages/admin/create');
			$data['header'] = $this->lang->line('backendpro_access_control');
			// Setting up page and telling which module
			$data['page'] = $this->config->item('backendpro_template_admin') . "admin_pages_create";
			$data['module'] = 'pages';
			$this->load->view($this->_container,$data);
		}
	}

	function edit($id=0){
			// we are using TinyMCE here, so load it.
		  	$this->bep_assets->load_asset_group('TINYMCE');
		  	if ($this->input->post('name')){
		  		// info is filled out, so the followings
		  		$this->MPages->updatePage();
		  		// This is CI way to show flashdata
		  		// $this->session->set_flashdata('message','Page updated');
		  		// But here we use Bep way to display flash msg
	  			flashMsg('success','Page updated');
		  		redirect('pages/admin/index','refresh');
		  	}else{
		  		// set variables here
				$data['title'] = "Edit Page";
				$data['page'] = $this->config->item('backendpro_template_admin') . "admin_pages_edit";
				$data['pagecontent'] = $this->MPages->getPage($id);
				if (!count($data['page'])){
					// if page is not specified redirect to index
					redirect('pages/admin/index','refresh');
				}
				$data['menus'] = $this->MMenus->getAllMenusDisplay();
				// Set breadcrumb
				$this->bep_site->set_crumb($this->lang->line('userlib_page_edit'),'pages/admin/edit');
				$data['header'] = $this->lang->line('backendpro_access_control');
				$data['module'] = 'pages';
				$this->load->view($this->_container,$data);
			}
	}

	function delete($id){
			$this->MPages->deletePage($id);
			// CI way
			// $this->session->set_flashdata('message','Page deleted');
			flashMsg('success','Page deleted');
			redirect('pages/admin/index','refresh');
	}

	function changePageStatus($id){
		$this->MPages->changePageStatus($id);
		// CI way
		// $this->session->set_flashdata('message','Page status changed');
		flashMsg('success','Page status changed');
		redirect('pages/admin/index','refresh');
	}

}//end class
?>

mpages model

modules/pages/models/mpages.php

<?php

class MPages extends Model{

	function MPages(){
		parent::Model();
	}

	function getPage($id){
	    $data = array();
	    $this->db->where('id',id_clean($id));
	    $this->db->limit(1);
	    $Q = $this->db->get('omc_page');
	    if ($Q->num_rows() > 0){
	      	$data = $Q->row_array();
	    }
	    $Q->free_result();
	    return $data;
	}

	function getPagePath($path){
	    $data = array();
	    $this->db->where('path',db_clean($path));
	    $this->db->where('status', 'active');
	    $this->db->limit(1);
	    $Q = $this->db->get('omc_page');
	    if ($Q->num_rows() > 0){
	      	$data = $Q->row_array();
	    }else{
			$data = array();// this prevent visiting unexistent page
		}
	    $Q->free_result();
	    return $data;
	}

	function getAllPages(){
	     $data = array();
	     $Q = $this->db->get('omc_page');
	     if ($Q->num_rows() > 0){
	       	foreach ($Q->result_array() as $row){
	         	$data[] = $row;
	       	}
	    }
	    $Q->free_result();
	    return $data;
	}

	function getAllPageswithnone(){
	     $data[0] = 'none';
	     $Q = $this->db->get('omc_page');
	     if ($Q->num_rows() > 0){
	       	foreach ($Q->result_array() as $row){
	         	$data[$row['id']] = $row['name'];
	       }
	    }
	    $Q->free_result();
	    return $data;
	}

	 function getAllPathwithnone(){
	     $data[0] = 'none';
	     $Q = $this->db->get('omc_page');
	     if ($Q->num_rows() > 0){
	       	foreach ($Q->result_array() as $row){
	        	$data[$row['path']] = $row['path'];
	       	}
	    }
	    $Q->free_result();
	    return $data;
	 }

	 function addPage(){
		$data = array(
			'name' => db_clean($_POST['name']),
			'keywords' => db_clean($_POST['keywords']),
			'description' => db_clean($_POST['description']),
			'status' => db_clean($_POST['status'],8),
			'path' => db_clean($_POST['path']),
			'content' => $_POST['content'],
		);

		$this->db->insert('omc_page', $data);
	 }

	 function updatePage(){
		$data = array(
			'name' => db_clean($_POST['name']),
			'keywords' => db_clean($_POST['keywords']),
			'description' => db_clean($_POST['description']),
			'status' => db_clean($_POST['status'],8),
			'path' => db_clean($_POST['path']),
			'content' => $_POST['content'],
	);

	 	$this->db->where('id', id_clean($_POST['id']));
		$this->db->update('omc_page', $data);	

	 }

	 function deletePage($id){
	 	$this->db->where('id', id_clean($id));
		$this->db->delete('omc_page');
	 }

	 function changePageStatus($id){
		// getting status of page
		$pageinfo = array();
		$pageinfo = $this->getPage($id);
		$status = $pageinfo['status'];
		if($status =='active'){
			$data = array('status' => 'inactive');
			$this->db->where('id', id_clean($id));
			$this->db->update('omc_page', $data);
		}else{
			$data = array('status' => 'active');
			$this->db->where('id', id_clean($id));
			$this->db->update('omc_page', $data);
		}
	 }

}

?>

Views

admin_pages_create

modules/pages/views/admin/admin_pages_create.php

<h2><?php echo $title;?></h2>
<div id="create_edit">
<?php

echo form_open('pages/admin/create');
echo "\n<p><label for='pname'>Name</label><br/>\n";
$data = array('name'=>'name','id'=>'pname','size'=>25);
echo form_input($data) ."</p>\n";

echo "<p><label for='short'>Keywords</label><br/>\n";
$data = array('name'=>'keywords','id'=>'short','size'=>40);
echo form_input($data) ."</p>\n";

echo "<p><label for='desc'>Description</label><br/>\n";
$data = array('name'=>'description','id'=>'desc','size'=>40);
echo form_input($data) ."</p>\n";

echo "<p><label for='fpath'>Path/FURL Use one word or with under line _ .</label><br/>\n";
$data = array('name'=>'path','id'=>'fpath','size'=>50);
echo form_input($data) ."</p>\n";

echo "<p><label for='long'>Content</label><br/>\n";
$data = array('name'=>'content','id'=>'long','rows'=>5, 'cols'=>'40');
echo form_textarea($data) ."</p>\n";

echo "<p><label for='status'>Status</label><br/>\n";
$options = array('active' => 'active', 'inactive' => 'inactive');
echo form_dropdown('status',$options) ."</p>\n";

echo form_submit('submit','create page');
echo form_close();

?>
</div>

admin_pages_edit

modules/pages/views/admin/admin_pages_create.php

<h2><?php echo $title;?></h2>
<div id="create_edit">
<?php

echo form_open('pages/admin/edit');
echo "<p><label for='pname'>Name</label><br/>";
$data = array('name'=>'name','id'=>'pname','size'=>25, 'value' => $pagecontent['name']);
echo form_input($data) ."</p>";

echo "<p><label for='short'>Keywords</label><br/>";
$data = array('name'=>'keywords','id'=>'short','size'=>40, 'value' => $pagecontent['keywords']);
echo form_input($data) ."</p>";

echo "<p><label for='desc'>Description</label><br/>";
$data = array('name'=>'description','id'=>'desc','size'=>40, 'value' => $pagecontent['description']);
echo form_input($data) ."</p>";

echo "<p><label for='fpath'>Path/FURL</label><br/>";
$data = array('name'=>'path','id'=>'fpath','size'=>50, 'value' => $pagecontent['path']);
echo form_input($data) ."</p>";

echo "<p><label for='long'>Content</label><br/>";
$data = array('name'=>'content','id'=>'long','rows'=>5, 'cols'=>'40', 'value' => $pagecontent['content']);
echo form_textarea($data) ."</p>";

echo "<p><label for='status'>Status</label><br/>";
$options = array('active' => 'active', 'inactive' => 'inactive');
echo form_dropdown('status',$options,$pagecontent['status']) ."</p>";

echo form_hidden('id',$pagecontent['id']);
echo form_submit('submit','update page');
echo form_close();

?>
</div>

admin_pages_home

modules/pages/views/admin/admin_pages_home.php

<?php print displayStatus();?>
<h2><?php echo $title;?></h2>
<p>
<?php
echo anchor("pages/admin/create", "Create new page");
?>
</p>
<?php
/*
 This is how CI display flash data. but we don't use it. 

if ($this->session->flashdata('message')){
	echo "<div class='status_box'>".$this->session->flashdata('message')."</div>";
}
*/
if (count($pages)){
	echo "<table id='tablesorter' class='tablesorter' border='1' cellspacing='0' cellpadding='3' width='100%'>\n";
	echo "<thead>\n<tr valign='top'>\n";
	echo "<th>ID</th>\n<th>Name</th><th>Full Path</th><th>Status</th><th>Actions</th>\n";
	echo "</tr>\n</thead>\n<tbody>\n";
	foreach ($pages as $key => $list){
		echo "<tr valign='top'>\n";
		echo "<td align='center'>".$list['id']."</td>\n";
		echo "<td>".$list['name']."</td>\n";
		echo "<td>";
   		if (!preg_match("/\.html$/",$list['path'])){
  			$list['path'] .= ".html";
  		}		

		if ($list['category_id'] == 0){
			echo "/". $list['path'];
		}else{
			echo "/". $cats[$list['category_id']]. "/". $list['path'];
		}
		echo "</td>";
		echo "<td align='center'>";
		echo anchor('pages/admin/changePageStatus/'.$list['id'],$list['status'], array('class' => $list['status']));
		echo "</td>\n";
		echo "<td align='center'>";
		echo anchor('pages/admin/edit/'.$list['id'],'edit');
		echo " | ";
		echo anchor('pages/admin/delete/'.$list['id'],'delete');
		echo "</td>\n";
		echo "</tr>\n";
	}
	echo "</tbody>\n</table>";
}
?>

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>