PHP File Manager
Editing File: Newsletter.php
<?php /* * Copyright (C) Wayne Purton-Smith - All Rights Reserved * Unauthorized copying of this file or removing this paragraph, via any medium is strictly prohibited * Proprietary and confidential * Written by Wayne Purton-Smith <waynepurtonsmith@hotmail.co.uk> February 2014 */ class Newsletter extends CI_Model { public function subscribe($email = NULL, $full_name = NULL, $name_required = false) { if(($email = strtolower(remove_whitespace($email))) != '') { $full_name = nullify(format_name($full_name)); if($name_required && $full_name === NULL) { return $this->common->setResponseMessage('Full name is required'); } if($this->isSubscribed($email)) { return $this->common->setResponseMessage('Email is already subscribed to the mailing list'); } if(($customer_info = $this->customers->get(NULL, array('email' => $email)))) { $unsubscribe_key = generate_unique_identifier(18, 'customers', 'unsubscribe_key'); $result = $this->db->query($this->db->update_string('customers', array ( 'subscribed_newsletter' => true, 'unsubscribe_key' => $unsubscribe_key ), "`customer_id` = " . $customer_info->customer_id)); if($this->db->affected_rows() === 1) { return $this->common->setResponseMessage('Successfully subscribed to the mailing list', true); } } else { do { $unsubscribe_key = generate_unique_identifier(18, 'newsletter_list', 'unsubscribe_key'); } while((bool) $this->customers->get(NULL, array('unsubscribe_key' => $unsubscribe_key)) === true); $this->db->query($this->db->insert_string('newsletter_list', array ( 'name' => $full_name, 'email' => $email, 'unsubscribe_key' => $unsubscribe_key ))); if($this->db->affected_rows() === 1) { return $this->common->setResponseMessage('Successfully subscribed to the mailing list', true); } } return $this->common->setResponseMessage('Could not subscribe to the mailing list - please try again'); } return $this->common->setResponseMessage('Email address is required'); ; } public function isSubscribed($email = NULL) { if(($email = strtolower(remove_whitespace($email))) != '') { $sql = "SELECT 1 FROM `newsletter_list` WHERE `email` = ? LIMIT 1"; $result = $this->db->query($sql, array($email)); if($result->num_rows()) return true; return (bool) $this->customers->get(NULL, array('email' => $email)); } return false; } public function unsubscribe($unsubscribe_key = NULL, $email = NULL) { if(($unsubscribe_key = remove_whitespace($unsubscribe_key)) != '') { $email = (($email = strtolower(remove_whitespace($email))) != '') ? $email : NULL; $sql = "SELECT `subscription_id` FROM `newsletter_list` WHERE `unsubscribe_key` = ?"; $sql .= ($email !== NULL) ? $sql .= " AND `email` = ?" : ''; $sql .= " LIMIT 1"; $result = $this->db->query($sql, array($unsubscribe_key, $email)); if($result->num_rows()) { $this->db->query("DELETE FROM `newsletter_list` WHERE `subscription_id` = " . $result->row('subscription_id')); if($this->db->affected_rows() === 1) { return $this->common->setResponseMessage('Email was successfully unsubscribed from the mailing list', true); } return $this->common->setResponseMessage('Could not unsubscribe email from the mailing list - please try again'); } else { if(($customer_info = $this->customers->get(NULL, array('unsubscribe_key' => $unsubscribe_key, 'email' => $email)))) { $this->db->query($this->db->update_string('customers', array('subscribed_newsletter' => false, 'unsubscribe_key' => NULL), "`customer_id` = " . $customer_info->customer_id)); if($this->db->affected_rows() === 1) { return $this->common->setResponseMessage('Email was successfully unsubscribed from the mailing list', true); } return $this->common->setResponseMessage('Could not unsubscribe email from the mailing list - please try again'); } } return $this->common->setResponseMessage('Could not find the matching subscription from the mailing list'); } return false; } }
Cancel