Shopping cart v1.1 Part 8: customers module

Home of customers module shows Customer ID, First name, Last name, Phone Number, Email, Address, City and Actions.

Manage Customers Page Overview

Download Page

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

Actions

There are edit and delete links under Actions. When you click ‘edit’ it will take you to ‘Edit Csutomer’ page.
And ‘delete’ link will delete the customer.

Create Customer and Edit Customer Page Overview


Both of Create Customer page and Edit Customer page have fields for Customer First Name, Customer Last Name, Phone Number, Email, Password, Password Confirm, Address, City and Post code.
As you can see in the controller, Customer First Name, Password, Password Confirm and Email are required. I have not included to avoid a duplicate of email. If you wish to do so, you need to add if statement to either the controller of model. I have checkCustomer function in the model to check if there is a email already in the database. So you can use it if you wish.

Database

CREATE TABLE IF NOT EXISTS `omc_customer` (
`customer_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`password` varchar(50) NOT NULL,
`customer_first_name` varchar(50) NOT NULL,
`customer_last_name` varchar(50) NOT NULL,
`phone_number` int(10) unsigned NOT NULL,
`email` varchar(50) NOT NULL,
`address` varchar(50) NOT NULL,
`city` varchar(50) NOT NULL,
`post_code` int(10) unsigned NOT NULL,
PRIMARY KEY (`customer_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

Module structure

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

admin controller

Please read the comments in the following code. modules/customers/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('Customers');
	// load MCustomers model
	$this->load->model('MCustomers');
   // Set breadcrumb
	$this->bep_site->set_crumb($this->lang->line('backendpro_customers'),'customers/admin');

  }

  function index(){
	$data['title'] = "Manage Customers";
	$data['customers'] = $this->MCustomers->getAllCustomers();
	$data['header'] = $this->lang->line('backendpro_access_control');
	$data['page'] = $this->config->item('backendpro_template_admin') . "admin_customers_home";
	$data['module'] = 'customers';
	$this->load->view($this->_container,$data);
  }

  function create(){
   	if ($this->input->post('customer_first_name')){
   		$rules['customer_first_name'] = 'required';
		$rules['password'] = 'required';
		$rules['passconf'] =  'required';
		$rules['email'] = 'required';
		$this->validation->set_rules($rules);

   	if ($this->validation->run() == FALSE)
		{
			$this->validation->output_errors();
			redirect('customers/admin/create','refresh');
		}
		else
		{
			$this->MCustomers->addCustomer();
	  		flashMsg('success','Customer created');
	  		redirect('customers/admin/index','refresh');
		}
  	}else{
		$data['title'] = "Create Customer";
		// Set breadcrumb
		$this->bep_site->set_crumb($this->lang->line('userlib_customer_create'),'customers/admin/create');
		$data['header'] = $this->lang->line('backendpro_access_control');
		$data['page'] = $this->config->item('backendpro_template_admin') . "admin_customers_create";
		$data['module'] = 'customers';
		$this->load->view($this->_container,$data);
	}
  }

  function edit($id=0){
  	if ($this->input->post('customer_first_name')){
  		$this->MCustomers->updateCustomer();
  		flashMsg('success','Customer editted');
  		redirect('customers/admin/index','refresh');
  	}else{
		$data['title'] = "Edit Customer";
		$data['page'] = $this->config->item('backendpro_template_admin') . "admin_customers_edit";
		$data['customer'] = $this->MCustomers->getCustomer($id);
		if (!count($data['customer'])){
			redirect('admin/customers/index','refresh');
		}
		$data['header'] = $this->lang->line('backendpro_access_control');
		// Set breadcrumb
		$this->bep_site->set_crumb($this->lang->line('userlib_customer_edit'),'customers/admin/edit');
		$data['module'] = 'customers';
		$this->load->view($this->_container,$data);
	}
  }

	function delete($id){
	/**
	 * When you delete customers, it will affect on omc_order table and it will affect omc_order_table_items
	 * Check if the customer has orders, if yes, then go back with warning to delete the order first.
	 *
	 */
		$order_orphans = $this->MCustomers->checkOrphans($id);
		if (count($order_orphans)){
			// $this->session->set_userdata($order_orphans);
			flashMsg('warning','Customer can\'t be deleted');
			flashMsg('warning',$order_orphans);
			redirect('customers/admin/index/','refresh');
		}else{
		    $this->MCustomers->deleteCustomer($id);
			flashMsg('success','Customer deleted');
			redirect('customers/admin/index','refresh');
		}
  	}

	function changeUserStatus($id){
		$this->MAdmins->changeCustomerStatus($id);
		flashMsg('success','User status changed');
		redirect('admins/admin/index','refresh');
  	}
}
?>

mcustomers model

<?php

class MCustomers extends Model{

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

	function getCustomer($id){
      $data = array();
      $options = array('customer_id' => id_clean($id));
      $Q = $this->db->getwhere('omc_customer',$options,1);
      if ($Q->num_rows() > 0){
        $data = $Q->row_array();
      }
      $Q->free_result();
      return $data;
	}

	function getCustomerByEmail($e){
      $data = array();
      $options = array('email' => $e);
      $Q = $this->db->getwhere('omc_customer',$options,1);
      if ($Q->num_rows() > 0){
        $data = $Q->row_array();
      }
      $Q->free_result();
      return $data;
	}

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

	function getCustomers(){
     $data = array();
     return $this->db->get('omc_customer');
	}

	function addCustomer(){
     $data = array(
     				'customer_first_name' => db_clean($_POST['customer_first_name'],25),
					'customer_last_name' => db_clean($_POST['customer_last_name'],25),
					'phone_number' => db_clean($_POST['phone_number'],15),
                    'email' => db_clean($_POST['email'],50),
					'address' => db_clean($_POST['address'],50),
					'city' => db_clean($_POST['city'],25),
					'post_code' => db_clean($_POST['post_code'],10),
     				'password' => db_clean(dohash($_POST['password']),16)
     );
	  $this->db->insert('omc_customer',$data);
	}

	function checkCustomer($e){
		$numrow = 0;
		$this->db->select('customer_id');
		$this->db->where('email',db_clean($e));
		$this->db->limit(1);
		$Q = $this->db->get('omc_customer');
		if ($Q->num_rows() > 0){
			$numrow = TRUE;
			return $numrow;
		}else{
			$numrow = FALSE;
			return $numrow;
		}
	}

	function verifyCustomer($e,$pw){
		$this->db->where('email',db_clean($e,50));
		$this->db->where('password', db_clean(dohash($pw),16));
		$this->db->limit(1);
		$Q = $this->db->get('omc_customer');
		if ($Q->num_rows() > 0){
			$row = $Q->row_array();
			$_SESSION['customer_id'] = $row['customer_id'];
			$_SESSION['customer_first_name'] = $row['customer_first_name'];
			$_SESSION['customer_last_name'] = $row['customer_last_name'];
			$_SESSION['phone_number'] = $row['phone_number'];
			$_SESSION['email'] = $row['email'];
			$_SESSION['address'] = $row['address'];
			$_SESSION['city'] = $row['city'];
			$_SESSION['post_code'] = $row['post_code'];
		}else{
			// $_SESSION['customer_id'] = 0; // this will eliminate error
		}
	}

	function updateCustomer(){
      $data = array('customer_first_name' => db_clean($_POST['customer_first_name'],25),
					'customer_last_name' => db_clean($_POST['customer_last_name'],25),
					'phone_number' => db_clean($_POST['phone_number'],15),
                    'email' => db_clean($_POST['email'],50),
					'address' => db_clean($_POST['address'],50),
					'city' => db_clean($_POST['city'],25),
					'post_code' => db_clean($_POST['post_code'],10),
                    'password' => db_clean(dohash($_POST['password']),16)
                    );
	  $this->db->where('customer_id',id_clean($_POST['customer_id']));
	  $this->db->update('omc_customer',$data);	

	}

	function deleteCustomer($id){
 		$this->db->where('customer_id', id_clean($id));
		$this->db->delete('omc_customer');
	}

	function checkOrphans($id){
		$data = array();
		$this->db->where('customer_id',id_clean($id));
		$Q = $this->db->get('omc_order');
		if ($Q->num_rows() > 0){
		   foreach ($Q->result_array() as $key=>$row){
			 $data[$key] = $row;
		   }
		$Q->free_result();
		return $data;
		}

 }

	function changeCustomerStatus($id){
		// getting status
		$userinfo = array();
		$userinfo = $this->getUser($id);
		$status = $userinfo['status'];
		if($status =='active'){
			$data = array('status' => 'inactive');
			$this->db->where('id', id_clean($id));
			$this->db->update('omc_customer', $data);
		}else{
			$data = array('status' => 'active');
			$this->db->where('id', id_clean($id));
			$this->db->update('omc_admin', $data);
	}
 }

}

?>

Views

admin_customers_create

modules/customers/views/admin/admin_customers_create.php

<h2><?php echo $title;?></h2>

<?php
echo form_open('customers/admin/create');
echo "<p><label for='customer_first_name'>Customer First Name</label><br/>";
$data = array('name'=>'customer_first_name','id'=>'fname','size'=>25);
echo form_input($data) ."</p>\n";

echo "<p><label for='customer_last_name'>Customer Last Name</label><br/>";
$data = array('name'=>'customer_last_name','id'=>'lname','size'=>25);
echo form_input($data) ."</p>\n";

echo "<p><label for='phone_number'>Phone Number</label><br/>";
$data = array('name'=>'phone_number','id'=>'phone','size'=>15);
echo form_input($data) ."</p>\n";

echo "<p><label for='email'>Email</label><br/>";
$data = array('name'=>'email','id'=>'email','size'=>50);
echo form_input($data) ."</p>\n";

echo "<p>Password</p>";
echo '<input type="text" name="password" value="" size="50" />';

echo "<p>Password Confirm</p>";
echo '<input type="text" name="passconf" value="" size="50" />';

echo "<p><label for='address'>Address</label><br/>";
$data = array('name'=>'address','id'=>'address','size'=>50);
echo form_input($data) ."</p>\n";

echo "<p><label for='city'>City</label><br/>";
$data = array('name' => 'city', 'id' => 'city', 'size'=>25);
echo form_input($data) ."</p>\n";

echo "<p><label for='post_code'>Post code</label><br/>";
$data = array('name' => 'post_code', 'id' => 'post_code', 'size'=>10);
echo form_input($data) ."</p>\n";

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

?>

admin_customers_edit

modules/customers/views/admin/admin_customers_edit.php

<h2><?php echo $title;?></h2>

<?php
echo form_open('customers/admin/edit');
echo "<p><label for='customer_first_name'>Customer First Name</label><br/>";
$data = array('name'=>'customer_first_name','id'=>'fname','size'=>25, 'value'=>$customer['customer_first_name']);
echo form_input($data) ."</p>";

echo "<p><label for='customer_last_name'>Customer Last Name</label><br/>";
$data = array('name'=>'customer_last_name','id'=>'lname','size'=>25, 'value'=>$customer['customer_last_name']);
echo form_input($data) ."</p>";

echo "<p><label for='phone_number'>Phone Number</label><br/>";
$data = array('name'=>'phone_number','id'=>'phone','size'=>25, 'value'=>$customer['phone_number']);
echo form_input($data) ."</p>";

echo "<p><label for='email'>Email</label><br/>";
$data = array('name'=>'email','id'=>'email','size'=>50, 'value'=>$customer['email']);
echo form_input($data) ."</p>";

echo "<p>Password</p>";
echo '<input type="text" name="password" value="" size="50" />';

echo "<p>Password Confirm</p>";
echo '<input type="text" name="passconf" value="" size="50" />';

echo "<p><label for='address'>Shipping address</label><br/>";
$data = array('name'=>'address','id'=>'address','size'=>50, 'value'=>$customer['address']);
echo form_input($data) ."</p>";

echo "<p><label for='city'>City</label><br/>";
$data = array('name'=>'city','id'=>'city','size'=>50, 'value'=>$customer['city']);
echo form_input($data) ."</p>";

echo "<p><label for='post_code'>Post code</label><br/>";
$data = array('name'=>'post_code','id'=>'post','size'=>10, 'value'=>$customer['post_code']);
echo form_input($data) ."</p>";

echo form_hidden('customer_id',$customer['customer_id']);
echo form_submit('submit','update customer');
echo form_close();

?>

admin_customers_home

modules/customers/views/admin/admin_customers_home.php

<?php print displayStatus();?>
<h2><?php echo $title;?></h2>
<p><?php echo anchor("customers/admin/create", "Create new customer");?>
<?php

if (count($customers)){
	echo "<table id='tablesorter' class='tablesorter' border='1' cellspacing='0' cellpadding='3' width='100%'>\n";
	echo "<thead>\n<tr valign='top'>\n";
	echo "<th>Customer ID</th>\n<th>First name</th><th>Last name</th><th>Phone Number</th><th>Email</th><th>Address</th><th>City</th><th>Actions</th>\n";
	echo "</tr>\n</thead>\n<tbody>\n";
	foreach ($customers as $key => $list){
		echo "<tr valign='top'>\n";
		echo "<td align='center'>".$list['customer_id']."</td>\n";
		echo "<td align='center'>".$list['customer_first_name']."</td>\n";
		echo "<td align='center'>".$list['customer_last_name']."</td>\n";
		echo "<td align='center'>".$list['phone_number']."</td>\n";
		echo "<td align='center'>".$list['email']."</td>\n";
		echo "<td align='center'>".$list['address']."</td>\n";
		echo "<td align='center'>".$list['city']."</td>\n";
		echo "<td align='center'>";
		echo anchor('customers/admin/edit/'.$list['customer_id'],'edit');
		echo " | ";
		echo anchor('customers/admin/delete/'.$list['customer_id'],'delete');
		echo "</td>\n";
		echo "</tr>\n";
	}
	echo "</tbody>\n</table>";
}
?>

4 comments to Shopping cart v1.1 Part 8: customers module

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>