Creating Customer Support Time Tracking Application with Codeigniter Part 5

This will be the final article of Customer Support Time Tracking app. And we are going to create Customersupport module for customers.

Redirecting members from home

Firstly we are going change customer login.
After logging in it leads to dashboard. But we need to redirect members to customersupport/admin.
Open system/application/controllers/admin/home.php and add the following at the line 34.

 

Demo

Note: Demo is available for Customer Pages Only.

Login details
email 1: cus7(a)gmail.com and pw: anneanne

email 2: cus2(a)gmail.com and pw: tonjetonje

Download page

// if group is not admin then redirect to customersupport/admin
	// group 1 is member 2 is admin
	$data['username']=$this->session->userdata('username');
	$username = $this->session->userdata('username');
	//$data['support_customers'] = $this->MSupport->getAllCustomerRecord($username);			

	$this->load->module_model('customersupport','MCustomer_support');
	$customer_records = $this->MCustomer_support->getCustomerGroupByUsername($username);
	$group = $customer_records['group'];

	if($group == 1){
	  	redirect('customersupport/admin','refresh');
	}

Creating customersupport module

Create folders and files as you see in the image.

 

Controllers

modules/customersupport/controllers/admin/admin.php

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

class Admin extends Support_Controller {
  function Admin(){
   	parent::Support_Controller();
		   // Check for access permission
			 check('Customer Support');

			// Load model
			$this->load->module_model('customersupport','MCustomer_support');
			$this->load->module_model('support','MSupport');
			$this->load->language('customer');
	}

	function index(){
			//check('Customer Support');
			$data['title'] = "Customer Support Home";
			// get the username and pull the customer records for the user
			$data['username']=$this->session->userdata('username');
			$username = $this->session->userdata('username');
			$customer_records = $this->MCustomer_support->getAllCustomerRecordByUsername($username);
			$data['customer_records'] = $customer_records;

			$data['totaltime'] = $this->MCustomer_support-> totalColumn($username,'time');
			$data['totalcredit'] = $this->MCustomer_support-> totalColumn($username, 'point_credit');
			$totaltime = $this->MCustomer_support-> totalColumn($username,'time');
			$totalcredit = $this->MCustomer_support-> totalColumn($username, 'point_credit');
			$data['total'] = $totalcredit['point_credit'] - $totaltime['time'];

			$data['header'] = $this->lang->line('customer_record');

			// This how Bep load views
			$data['page'] = $this->config->item('backendpro_template_admin') . "support_home";
			$data['module'] = 'customersupport';
			$this->load->view($this->_container,$data); 

	}

	function details($id){
		check('Customer Record');
		// we use the following variables in the view
		$data['title'] = "Support Details";
		$data['header'] = "Support Details" ;

		// get user name from the session, just to say Hi username!
		$data['username']=$this->session->userdata('username');
		$username = $this->session->userdata('username');

		// we should show details where customer_id matches. Otherwise a customer can see other customer's record as well
		$customer_records = $this->MCustomer_support->getAllCustomerRecordByUsername($username);
		$customer_id = $customer_records['0']['customer_id'];// this will give 2 or 3 etc.
		$id = $this->uri->segment(4);
		$details= $this->MCustomer_support->getRecordDetails($id, $customer_id);
		$data['details'] = $details;
		// when you type details/33 which is not the customer's record then it returns nothing
		// so if $data['details'] is empty  then redirect
		if (empty($details)){
			flashMsg('warning','The requested record does not exist');
		  	redirect('customersupport/admin/','refresh');
		}
		// Set breadcrumb
		$this->bep_site->set_crumb('Details','customersupport/admin/details');

		// This how Bep load views
		$data['page'] = $this->config->item('backendpro_template_admin') . "details";
		$data['module'] = 'customersupport';
		$this->load->view($this->_container,$data); 

	}

	function purchase_credit(){
		if ($this->input->post('order_support')){
			$customer_id = $this->input->post('customer_id');
			// info is filled out, do the followings
			// you need to send email
			$this->load->library('email');
			$company_name = $this->input->post('company_name');
			$customer_email = $this->input->post('email');
			$full_name = $this->input->post('full_name');
			$credit_purchased = $this->input->post('order_support');
			// Change to your email address
			$admin_email = 'admin(at)gmail.com';
			// Or pull from DB if you have the field
			// See Kaimono Kago for this
			// $admin_email = $this->preference->item('admin_email');

			$this->email->from($customer_email, $full_name);
			$this->email->to($admin_email);
			$this->email->subject('Customer Support Purchase');

			$body = $company_name . " purchase Customer Service with the amount of ".$credit_purchased;  

			$this->email->message($body);	

			$this->email->send();
			flashMsg('success','Thank you for your purchase. Your Support Purchase is emailed to Okada Design AS. We will get in touch as soon as possible. ');
		  	redirect('customersupport/admin/','refresh');

		}else{
			//check('Purchase Support');
			$data['title'] = "Order support credit";
			$data['header'] = "Order support credit";
			$username = $this->session->userdata('username');
			$data['customer_details']= $this->MCustomer_support->getSingleMemberProfileByUsername($username);

			// Set breadcrumb
				$this->bep_site->set_crumb('Purchase Support','customersupport/admin/purchase _credit');
			// This how Bep load views
			$data['page'] = $this->config->item('backendpro_template_admin') . "order_credit";
			$data['module'] = 'customersupport';
			$this->load->view($this->_container,$data);
		}
	}

}//end class
?>

Models

modules/customersupport/models/mcustomer_support.php

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

class MCustomer_support extends Base_model{

	function MCustomer_support(){
		parent::Base_model();
		$this->_TABLES = array( 'web_customer' => 'web_customer',
								'support_details' => 'support_details'
                                    );
	}

	function getAllCustomerRecordByUsername($username){
		$data = array();
		$this->db->select('support_details.customer_id, support_details.date, support_details.time, support_details.point_credit, support_details.note
		, support_details.id, support_details.details, support_details.by, be_users.username, be_users.group');
		$this->db->from('support_details');
		$this->db->join('be_users', 'be_users.id = support_details.customer_id');
		$this->db->where('be_users.username', $username);
		$query = $this->db->get();
		if ($query->num_rows() > 0){
	       foreach ($query->result_array() as $row){
	         $data[]=$row;
	       }
	    }
	    $query->free_result();
	    return $data; 

	}

	function totalColumn($username,$column){
		$data = array();
		$this->db->select_sum($column);
		$this->db->from('support_details');
		$this->db->join('be_users', 'be_users.id = support_details.customer_id');
		$this->db->where('be_users.username', $username);
		$query = $this->db->get();
		if ($query->num_rows() > 0){
	       foreach ($query->result_array() as $row){
	         $data=$row;
	       }
	    }
	    $query->free_result();
	    return $data;
	}

	function getRecordDetails($id, $customer_id){
		$data = array();
		$query = $this->fetch('support_details','*',NULL,array('id'=> $id, 'customer_id'=>$customer_id));
		 if ($query->num_rows() > 0){
	      	$data = $query->row_array();
	    }
	    $query->free_result();
	    return $data;
	}

	function admingetRecordDetails($id){
		$data = array();
		$query = $this->fetch('support_details','*',NULL,array('id'=> $id));
		 if ($query->num_rows() > 0){
	      	$data = $query->row_array();
	    }
	    $query->free_result();
	    return $data;
	}

	function getSingleMemberProfileByUsername($username){
		$data = array();
		$this->db->from('be_user_profiles');
		$this->db->join('be_users', 'be_users.id = be_user_profiles.user_id');
		$this->db->where('be_users.username', $username);
		$query = $this->db->get();
		if ($query->num_rows() > 0){
	       foreach ($query->result_array() as $row){
	         $data[]=$row;
	       }
	    }
	    $query->free_result();
	    return $data; 	    

	}

	function getCustomerGroupByUsername($username){
		$data = array();
		$this->db->select('group')->from('be_users')->where('username', $username);
		$query = $this->db->get();

		 if ($query->num_rows() > 0){
	      	$data = $query->row_array();
	    }
	    $query->free_result();
	    return $data;
	}

}

Views

modules/customersupport/views/admin/details.php

<?php print displayStatus();?>
<h2><?php echo $title;?></h2>
<p>
<?php
echo "Hello ". ucwords ($username). "!";
?>
</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($details)){
	echo "<table id='tablesorter' class='tablesorter' border='1' cellspacing='0' cellpadding='3' width='100%'>\n";
	echo "<thead>\n<tr valign='top'>\n";
	echo "<th>By</th>\n<th>Date</th><th>Time</th><th>Point Credit</th><th>Note</th>\n";
	echo "</tr>\n</thead>\n<tbody>\n";

		// print_r ($total);
		echo "<tr valign='top'>\n";
		echo "<td align='center'>".$details['by']."</td>\n";
		echo "<td>".$details['date']."</td>\n";

		echo "<td>".$details['time']. "</td>";
		echo "<td>".$details['point_credit']. "</td>";
		echo "<td>".$details['note']. "</td>";

		echo "</tr>\n";

	echo "</tbody>\n</table>";
	echo "<h4>Details</h4>";
	echo $details['details'];
}
?>

support_home.php

<?php print displayStatus();?>
<h2><?php echo $title;?></h2>
<p>
<?php
echo "Hello ". ucwords ($username). "!";
?>
</p>
<?php

echo "<h3>Total time spent: ".$totaltime['time']."</h3>";
echo "<h3>Total credit purchased: ".$totalcredit['point_credit']."</h3>";
echo "<h3>Total Points/Time Left: ".$total."</h3>";
echo "<h4>Support Details</h4>";

if (count($customer_records)){
	echo "<table id='tablesorter' class='tablesorter' border='1' cellspacing='0' cellpadding='3' width='100%'>\n";
	echo "<thead>\n<tr valign='top'>\n";
	echo "<th>By</th>\n<th>Date</th><th>Time</th><th>Point Credit</th><th>Note</th><th>Details</th>\n";
	echo "</tr>\n</thead>\n<tbody>\n";
	foreach ($customer_records as $list){
		// print_r ($total);
		echo "<tr valign='top'>\n";
		echo "<td align='center'>".$list['by']."</td>\n";
		echo "<td>".$list['date']."</td>\n";

		echo "<td>".$list['time']. "</td>";
		echo "<td>".$list['point_credit']. "</td>";
		echo "<td>".$list['note']. "</td>";
		echo "<td><a href=\"admin/details/".$list['id']. "\">Details</a></td>";
		echo "</tr>\n";
	}
	echo "</tbody>\n</table>";

}

?>

order_credit.php


<?php
echo "<h3>Order Credit</h3>";
echo $title."<br />";
foreach ($customer_details as $detail){
echo "Your company: ".$detail['company_name']."<br />\n";
echo "Your name: ".$detail['full_name']."<br />\n";
echo "Your email: ".$detail['email']."<br />\n";

echo form_open('customersupport/admin/purchase_credit');

echo form_radio('order_support', '60min 700Kr');
echo "60min 700Kr";
echo "<br/>\n";

echo form_radio('order_support', '120min 1300Kr');
echo "120min 1300Kr";
echo "<br/>\n";

echo form_radio('order_support', '180min 1800Kr');
echo "180min 1800Kr";
echo "<br/>\n";
echo form_hidden('id',$detail['id']);
echo form_hidden('company_name',$detail['company_name']);
echo form_hidden('full_name',$detail['full_name']);
echo form_hidden('email',$detail['email']);
echo form_submit('submit','Order Support Credit');
echo form_close();
}

Test Drive

 

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>