Files
piratepoet/lib/Models/Subscriber.php
Jonathan Labreuille 2a4945c956 Listing & Form & Refactoring
- finished implementing forms
- fixed form reset when clicking on new
- fixed responsive for select all checkbox and toggle item details
- refactored listing items' actions
- added Trash action
- cleaned up validations on models
- fixed syntax
- fixed tests
2015-09-16 12:14:04 +02:00

98 lines
2.2 KiB
PHP

<?php
namespace MailPoet\Models;
if(!defined('ABSPATH')) exit;
class Subscriber extends Model {
public static $_table = MP_SUBSCRIBERS_TABLE;
function __construct() {
parent::__construct();
$this->addValidations('email', array(
'required' => __('You need to enter your email address.'),
'isEmail' => __('Your email address is invalid.')
));
}
static function search($orm, $search = '') {
if(strlen(trim($search) === 0)) {
return $orm;
}
return $orm->where_raw(
'(`email` LIKE ? OR `first_name` LIKE ? OR `last_name` LIKE ?)',
array('%'.$search.'%', '%'.$search.'%', '%'.$search.'%')
);
}
static function groups() {
return array(
array(
'name' => 'all',
'label' => __('All'),
'count' => Subscriber::count()
),
array(
'name' => 'subscribed',
'label' => __('Subscribed'),
'count' => Subscriber::where('status', 'subscribed')->count()
),
array(
'name' => 'unconfirmed',
'label' => __('Unconfirmed'),
'count' => Subscriber::where('status', 'unconfirmed')->count()
),
array(
'name' => 'unsubscribed',
'label' => __('Unsubscribed'),
'count' => Subscriber::where('status', 'unsubscribed')->count()
)
);
}
static function group($orm, $group = null) {
if($group === null or !in_array(
$group,
array('subscribed', 'unconfirmed', 'unsubscribed')
)) {
return $orm;
}
return $orm->where('status', $group);
}
function segments() {
return $this->has_many_through(
__NAMESPACE__.'\Segment',
__NAMESPACE__.'\SubscriberSegment',
'subscriber_id',
'segment_id'
);
}
static function createOrUpdate($data = array()) {
$subscriber = false;
if(isset($data['id']) && (int)$data['id'] > 0) {
$subscriber = self::findOne((int)$data['id']);
}
if($subscriber === false) {
$subscriber = self::create();
$subscriber->hydrate($data);
} else {
unset($data['id']);
$subscriber->set($data);
}
$saved = $subscriber->save();
if($saved === false) {
return $subscriber->getValidationErrors();
} else {
return true;
}
}
}