Instantly share code, notes, and snippets.
Created
April 17, 2012 21:34
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
Save rsanchez/2409228 to your computer and use it in GitHub Desktop.
CartThrob Entry Author Email Notifications Extension
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); | |
/** | |
* CartThrob CartThrob Entry Author Email Notifications Extension | |
* | |
* @package ExpressionEngine | |
* @subpackage CartThrob | |
* @category Extension | |
* @author Rob Sanchez | |
* @link http://cartthrob.com | |
*/ | |
class Cartthrob_entry_author_email_notifications_ext | |
{ | |
public $settings = array(); | |
public $description = ''; | |
public $docs_url = 'http://cartthrob.com'; | |
public $name = 'CartThrob Entry Author Email Notifications'; | |
public $settings_exist = 'y'; | |
public $version = '1.0.0'; | |
private $EE; | |
public function __construct($settings = '') | |
{ | |
$this->EE =& get_instance(); | |
$this->settings = $settings; | |
} | |
public function settings() | |
{ | |
return array( | |
'from' => '', | |
'from_name' => '', | |
'plaintext' => array('r', array('0' => 'html', '1' => 'plain_text'), '0'), | |
'subject' => '', | |
'message' => array('t', '', ''), | |
); | |
} | |
public function activate_extension() | |
{ | |
// Setup custom settings in this array. | |
$this->settings = array(); | |
$data = array( | |
'class' => __CLASS__, | |
'method' => 'cartthrob_on_authorize', | |
'hook' => 'cartthrob_on_authorize', | |
'settings' => serialize($this->settings), | |
'version' => $this->version, | |
'enabled' => 'y' | |
); | |
$this->EE->db->insert('extensions', $data); | |
} | |
public function cartthrob_on_authorize() | |
{ | |
static $cache = array(); | |
if (empty($this->settings['message']) || empty($this->settings['subject']) || empty($this->settings['from']) || ! $this->EE->cartthrob->cart->order('purchased_items')) | |
{ | |
return; | |
} | |
foreach ($this->EE->cartthrob->cart->order('items') as $item) | |
{ | |
if (empty($item['entry_id'])) | |
{ | |
continue; | |
} | |
if ( ! isset($cache[$item['entry_id']])) | |
{ | |
$query = $this->EE->db->select('members.email, channel_titles.title') | |
->join('channel_titles', 'channel_titles.author_id = members.member_id') | |
->where('channel_titles.entry_id', $item['entry_id']) | |
->get('members'); | |
$cache[$item['entry_id']] = $query->row(); | |
} | |
if ( ! $cache[$item['entry_id']]) | |
{ | |
continue; | |
} | |
$this->EE->load->library('cartthrob_emails'); | |
$subject_vars = array('title' => $cache[$item['entry_id']]->title); | |
$subject = $this->EE->cartthrob_emails->parse( | |
$this->settings['subject'], | |
$subject_vars, | |
array( | |
'ORDER_ID' => $this->EE->cartthrob->cart->order('order_id'), | |
'ENTRY_ID' => $item['entry_id'], | |
) | |
); | |
$order_vars = $this->EE->cartthrob->cart->order(); | |
$message = $this->EE->cartthrob_emails->parse( | |
$this->settings['message'], | |
$order_vars, | |
array( | |
'ORDER_ID' => $this->EE->cartthrob->cart->order('order_id'), | |
'ENTRY_ID' => $item['entry_id'], | |
), | |
TRUE //run through full parsing engine | |
); | |
$this->EE->cartthrob_emails->send_email( | |
$this->settings['from'], | |
( ! empty($this->settings['from_name'])) ? $this->settings['from_name'] : $this->settings['from'], | |
$cache[$item['entry_id']]->email, | |
$subject, | |
$message, | |
! empty($this->settings['plaintext']) | |
); | |
} | |
} | |
public function disable_extension() | |
{ | |
$this->EE->db->delete('extensions', array('class', __CLASS__)); | |
} | |
public function update_extension($current = '') | |
{ | |
if ($current == '' OR $current == $this->version) | |
{ | |
return FALSE; | |
} | |
} | |
} | |
/* End of file ext.cartthrob_entry_author_email_notifications.php */ | |
/* Location: /system/expressionengine/third_party/cartthrob_entry_author_email_notifications/ext.cartthrob_entry_author_email_notifications.php */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment