Skip to content

Instantly share code, notes, and snippets.

@capir
Created June 10, 2013 12:58
Show Gist options
  • Save capir/5748508 to your computer and use it in GitHub Desktop.
Save capir/5748508 to your computer and use it in GitHub Desktop.
PHP: CI Libraries
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$config['apikey'] = '';//'xxxxxx-xxxxxx-xxxxx-xxxxx-xxxxx';
$config['tags'] = array();//array('tag');
$config['track_opens'] = true;
$config['track_clicks'] = true;
$config['auto_text'] = true;
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once APPPATH.'third_party/Mandrill.php';
/**
* @version 1.6
* @todo add Tags √
* @todo send templates √
* @todo reset params √
* @todo config trackopens/clicks √
* @todo auto_text √
*/
class Mandrill_api extends Mandrill {
protected $ci;
public function __construct()
{
$this->ci =& get_instance();
$this->ci->config->load('mandrill', true);
parent::__construct($this->ci->config->item('apikey','mandrill'));
$this->debug = false;//ENVIRONMENT == 'development';
$this->messages = new MY_Mandrill_Messages($this);
}
public function call($url, $params) {
$r = parent::call($url, $params);
return isset($r[0]['status']) && $r[0]['status'] == 'sent';
}
public function log($msg)
{
if($this->debug)
log_message('debug', $msg);
}
}
class MY_Mandrill_Messages extends Mandrill_Messages
{
private $method;
private $ci;
private $init = array();
public function __construct($master)
{
parent::__construct($master);
$this->ci =& get_instance();
$this->init['tags'] = $this->ci->config->item('tags','mandrill');
$this->init['track_opens'] = $this->ci->config->item('track_opens','mandrill');
$this->init['track_clicks'] = $this->ci->config->item('track_clicks','mandrill');
$this->init['auto_text'] = $this->ci->config->item('auto_text','mandrill');
$this->init();
}
private function init()
{
$this->method = array();
// Tags
$this->method['send']['message']['tags'] = $this->init['tags'];
$this->method['sendTemplate']['message']['tags'] = $this->init['tags'];
// Trackers
$this->method['send']['message']['track_opens'] = $this->init['track_opens'];
$this->method['send']['message']['track_clicks'] = $this->init['track_clicks'];
$this->method['sendTemplate']['message']['track_opens'] = $this->init['track_opens'];
$this->method['sendTemplate']['message']['track_clicks'] = $this->init['track_clicks'];
// Other
$this->method['send']['message']['auto_text'] = $this->init['auto_text'];
$this->method['sendTemplate']['message']['auto_text'] = $this->init['auto_text'];
}
public function addTag($tag = null)
{
$tag = is_string($tag) ? (array)$tag : tag;
if(!is_array($tag)) return $this;
foreach($tag as $t)
if(!in_array($t, $this->method['send']['message']['tags'])) {
$this->method['send']['message']['tags'][] = $t;
$this->method['sendTemplate']['message']['tags'][] = $t;
}
return $this;
}
public function from($from, $name = '')
{
$this->method['send']['message']['from_email'] = $from;
$this->method['sendTemplate']['message']['from_email'] = $from;
if(!empty($name)){
$this->method['send']['message']['from_name'] = $name;
$this->method['sendTemplate']['message']['from_name'] = $name;
}
return $this;
}
public function to($to, $name = '')
{
if(empty($this->method['send']['message']['to'])){
$this->method['send']['message']['to'] = array();
$this->method['sendTemplate']['message']['to'] = array();
}
if(is_array($to)){
$this->method['send']['message']['to'] = array_merge($this->method['send']['message']['to'], $to);
$this->method['sendTemplate']['message']['to'] = array_merge($this->method['sendTemplate']['message']['to'], $to);
}
else{
$this->method['send']['message']['to'][] = array('email'=>$to,'name'=>$name);
$this->method['sendTemplate']['message']['to'][] = array('email'=>$to,'name'=>$name);
}
return $this;
}
public function subject($subject)
{
$this->method['send']['message']['subject'] = $subject;
$this->method['sendTemplate']['message']['subject'] = $subject;
return $this;
}
public function alt_body($alt)
{
$this->method['send']['message']['text'] = $alt;
$this->method['sendTemplate']['message']['text'] = $alt;
return $this;
}
public function body($html)
{
$this->method['send']['message']['html'] = $html;
return $this;
}
public function setTemplate($template_name, $template_content)
{
$this->method['sendTemplate']['template_name'] = $template_name;
foreach($template_content as $c)
$this->method['sendTemplate']['template_content'][] = array('name' => key($c), 'content' => reset($c));
return $this;
}
public function setGlobalMergeVars($merge_vars)
{
foreach($merge_vars as $c){
$this->method['send']['message']['global_merge_vars'][] = array('name' => key($c), 'content' => reset($c));
$this->method['sendTemplate']['message']['global_merge_vars'][] = array('name' => key($c), 'content' => reset($c));
}
return $this;
}
public function send()
{
if(empty($this->method['send']['message']))
return $this->master->log('Nu am parametri');
$pars = $this->method['send'];
// var_dump($this->method['send']['message']);exit;
$this->init();
return parent::send($pars['message']);
}
public function sendTemplate()
{
$template_name = $this->method['sendTemplate']['template_name'];
$template_content = $this->method['sendTemplate']['template_content'];
$message = $this->method['sendTemplate']['message'];
if(empty($template_name) ||empty($template_content) ||empty($message))
return $this->master->log('Nu am parametri');
// var_dump($this->method['sendTemplate']) ;exit;
$this->init();
return parent::sendTemplate($template_name, $template_content, $message);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment