Codeigniter shopping cart v1.1 Part 5: subscribers module

I will not use this module in the front page webshop. However this function will be handy to have for your email marketing and email newletters, so I have created a module.

Download Page

Subscribers module

You can see how to use this by looking closely at the version 1.0. When you open the front page it has a subscribe form.

Here I am only showing the back-end side.


Home of subscribers module shows ID, Name, Email and Actions.
We use jquery dataTables to show our information. This will allow us to sort by each column, display different number of rows and seach.

When you click ‘unsubscribe’ under Actions, it will delete its subscriber.

There will be a ‘Create new email’ and ‘Create new subscriber’ link on the top left.


‘Create new subscriber’ page has two fields to fill up, Name and Email. Both are set up as required.


‘Create new email’ page has fields for ‘Subject’ and ‘Message’. And a checkbox for ‘This is a test!’.

If it is ticked, it will only email to you. This will be useful for testing before the actual email.

Module structure

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

Database

CREATE TABLE IF NOT EXISTS `omc_subscribers` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `email` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

admin controller

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

Don’t forget to replace from and to parameters. And for more details, please read comments between codes.

<?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('Subscribers');
	// load model subscribers
	$this->load->model('MSubscribers');

	// Set breadcrumb
	$this->bep_site->set_crumb($this->lang->line('backendpro_subscribers'),'subscribers/admin');	

	// Loading Bep validation
	// $this->load->library('validation');

  }

  function index(){
	$data['title'] = "Manage Subscribers";
	$data['subscribers'] = $this->MSubscribers->getAllSubscribers();
	$data['header'] = $this->lang->line('backendpro_access_control');
	$data['page'] = $this->config->item('backendpro_template_admin') . "admin_subs_home";
	$data['module'] = 'subscribers';
	$this->load->view($this->_container,$data);
  }

  function delete($id){
  	// this can be used for users to unsubscribe themselves.
	$this->MSubscribers->removeSubscriber($id);
	$this->session->set_flashdata('message','Subscriber deleted');
	redirect('subscribers/admin/index','refresh');
  }

  function sendemail(){
  	$this->bep_assets->load_asset_group('TINYMCE');

  	$this->load->helper('file');
  	if ($this->input->post('subject')){
				$test = $this->input->post('test');
				$subject = $this->input->post('subject');
				$msg = $this->input->post('message');

				if ($test){
					$this->email->clear();
					// Replace with your email and company name
					$this->email->from('admin(at)google.com', 'Your company name');
					// Replace with your other email for testing
					$this->email->to('otheremail(at)gmail.com');
					$this->email->subject($subject);
					$this->email->message($msg);
					$this->email->send();
					$this->session->set_flashdata('message', "Test email sent");
					write_file('/tmp/email.log', $subject ."|||".$msg);
					// redirect wherever you want, it can be index page
					redirect('subscribers/admin/sendemail','refresh');
				}else{
					$subs = $this->MSubscribers->getAllSubscribers();
					foreach ($subs as $key => $list){
						// you need to change this link to your unsubscribe link
						$unsub = "<p><a href='". base_url()."welcome/unsubscribe/".$list['id']. "'>Unsubscribe</a></p>";
						$this->email->clear();
						// Replace with your email and company name
						$this->email->from('admin(at)gmail.com', 'Your company name');
						$this->email->to($list['email']);
						// Replace with your other email for checking or blank
						$this->email->bcc('otheremail(at)gmail.com');
						$this->email->subject($subject);
						$this->email->message($msg . $unsub);
						$this->email->send();
					}
					// You can use Bep's flashMsg('type','message') instead.
					$this->session->set_flashdata('message', count($subs) . " emails sent");
				}
				// redirect wherever you want.
				redirect('subscribers/admin/index','refresh');
  	}
		else
		{
				if ($this->session->flashdata('message') == "Test email sent"){
					$lastemail = read_file('/tmp/email.log');
					list($subj,$msg) = explode("|||",$lastemail);
					$data['subject'] = $subj;
					$data['msg'] = $msg;
				}else{
					$data['subject'] = '';
					$data['msg'] = '';
				}
				$data['title'] = "Send Email";
				// Set breadcrumb
				$this->bep_site->set_crumb($this->lang->line('userlib_subscriber_sendemail'),'subscriber/admin/sendemail');
				$data['header'] = $this->lang->line('backendpro_access_control');
				$data['page'] = $this->config->item('backendpro_template_admin') . "admin_subs_mail";
				$data['module'] = 'subscribers';
				$this->load->view($this->_container,$data);

  	}
  }

	function create_home(){
		$data['title'] = "Create Subscribers";

		// Set breadcrumb
		$this->bep_site->set_crumb($this->lang->line('userlib_subscriber_create'),'subscriber/admin/create_home');
		$data['header'] = $this->lang->line('backendpro_access_control');
		$data['page'] = $this->config->item('backendpro_template_admin') . "admin_subs_create";
		$data['module'] = 'subscribers';
		$this->load->view($this->_container,$data);

	}

	function create_sub(){
		$rules['name'] = 'trim|required';
		$rules['email'] = 'trim|required|valid_email';

		$this->validation->set_rules($rules);

		if ($this->validation->run() == FALSE)
			{
				$this->validation->output_errors();
				redirect('subscribers/admin/');
			}
			else
			{
					$this->MSubscribers->createSubscriber();
					flashMsg('success',$this->lang->line('userlib_sub_added'));
					redirect('subscribers/admin/','refresh');
			}
  }

}//end class
?>

msubscribers model

modules/subscribers/models/msubscribers.php

<?php

class MSubscribers extends Model{

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

function getSubscriber($id){
    $this->db->where('id',id_clean($id));
    $this->db->limit(1);
    $Q = $this->db->getwhere('omc_subscribers');
    if ($Q->num_rows() > 0){
      $data = $Q->row_array();
    }

    $Q->free_result();
    return $data;
 }

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

 function createSubscriber(){
	$this->db->where('email', $_POST['email']);
	$this->db->from('omc_subscribers');
	$ct = $this->db->count_all_results();

	if ($ct == 0){
		$data = array(
			'name' => db_clean($_POST['name']),
			'email' => db_clean($_POST['email'])
		);

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

 function updateSubscriber(){
	$data = array(
		'name' => db_clean($_POST['name']),
		'email' => db_clean($_POST['email'])

	);

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

 }

 function removeSubscriber($id){
 	$this->db->where('id', id_clean($id));
	$this->db->delete('omc_subscribers');

 } 

}//end class
?>

Views

admin_subs_create

modules/subscribers/views/admin/admin_subs_create.php

<?php

if ($this->session->flashdata('subscribe_msg')){
	echo "<div class='status_box'>";
	echo $this->session->flashdata('subscribe_msg');
	echo "</div>";
}
?>

<?php echo validation_errors(); ?>
<div id="emailsubs">
<?php echo form_open("subscribers/admin/create_sub"); ?>

<h5>*Name</h5>
<input type="text" name="name" id="name" value="<?php echo set_value('name'); ?>" size="40" />

<h5>*Email</h5>
<input type="text" name="email" id="email" value="<?php echo set_value('email'); ?>" size="40" />

<div><input type="submit" value="Subscribe" /></div>

<?php echo form_close(); ?>

</div>

admin_subs_home

modules/subscribers/views/admin/admin_subs_home.php

<h2><?php echo $title;?></h2>
<p><?php echo anchor("subscribers/admin/sendemail", "Create new email");?></p>
<p><?php echo anchor("subscribers/admin/create_home", "Create new subscriber");?></p>
<?php print displayStatus();?>
<?php
if ($this->session->flashdata('message')){
	echo "<div class='status_box'>".$this->session->flashdata('message')."</div>";
}

if (count($subscribers)){
	echo "<table id='tablesorter' class='tablesorter' border='1' cellspacing='0' cellpadding='3' width='800'>\n";
	echo "<thead>\n<tr valign='top'>\n";
	echo "<th>ID</th>\n<th>Name</th><th>Email</th><th>Actions</th>\n";
	echo "</tr>\n</thead>\n<tbody>\n";
	foreach ($subscribers as $key => $list){
		echo "<tr valign='top'>\n";
		echo "<td align='center'>".$list['id']."</td>\n";
		echo "<td>".$list['name']."</td>\n";
		echo "<td>".$list['email']."</td>\n";
		echo "<td align='center'>";
		echo anchor('subscribers/admin/delete/'.$list['id'],'unsubscribe');
		echo "</td>\n";
		echo "</tr>\n";
	}
	echo "</tbody>\n</table>";
}
?>

admin_subs_mail

modules/subscribers/views/admin/admin_subs_mail.php

<?php

if ($this->session->flashdata('message')){
	echo "<div class='status_box'>".$this->session->flashdata('message')."</div>";
}

echo form_open('subscribers/admin/sendemail');

echo "<p><label for='subject'>Subject</label><br/>";
$data = array('name' => 'subject', 'id' => 'subject', 'size' => 50, 'value'=>$subject);
echo form_input($data);
echo "</p>";

echo "<p><label for='message'>Message</label><br/>";
$data = array('name' => 'message', 'id' => 'message', 'rows' => 20, 'cols' => 50, 'value'=>$msg);
echo form_textarea($data);
echo "</p>";

echo "<p>".form_checkbox('test', 'true', TRUE) . " <b>This is a test!</b></p>";
echo form_submit('submit','send email');
echo form_close();

?>

5 comments to Codeigniter shopping cart v1.1 Part 5: subscribers 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>