- improved Listing in order to make it more DRY - form builder with all field types - added support for data array in ValidModel->set() - updated models to comply with Listing & Form methods
63 lines
1.4 KiB
PHP
63 lines
1.4 KiB
PHP
<?php
|
|
namespace MailPoet\Models;
|
|
|
|
if (!defined('ABSPATH')) exit;
|
|
|
|
class Newsletter extends Model {
|
|
public static $_table = MP_NEWSLETTERS_TABLE;
|
|
|
|
function __construct() {
|
|
parent::__construct();
|
|
|
|
$this->addValidations('subject', array(
|
|
'required' => 'subject_is_blank',
|
|
'isString' => 'subject_is_not_string'
|
|
));
|
|
$this->addValidations('body', array(
|
|
'required' => 'body_is_blank',
|
|
'isString' => 'body_is_not_string'
|
|
));
|
|
}
|
|
|
|
static function search($orm, $search = '') {
|
|
return $orm->where_like('subject', '%'.$search.'%');
|
|
}
|
|
|
|
static function groups() {
|
|
return array(
|
|
array(
|
|
'name' => 'all',
|
|
'label' => __('All'),
|
|
'count' => Newsletter::count()
|
|
)
|
|
);
|
|
}
|
|
|
|
static function group($orm, $group = null) {
|
|
}
|
|
|
|
public static function createOrUpdate($data = array()) {
|
|
$newsletter = false;
|
|
|
|
if(isset($data['id']) && (int)$data['id'] > 0) {
|
|
$newsletter = self::findOne((int)$data['id']);
|
|
}
|
|
|
|
if($newsletter === false) {
|
|
$newsletter = self::create();
|
|
$newsletter->hydrate($data);
|
|
} else {
|
|
unset($data['id']);
|
|
$newsletter->set($data);
|
|
}
|
|
|
|
$saved = $newsletter->save();
|
|
|
|
if($saved === false) {
|
|
return $newsletter->getValidationErrors();
|
|
} else {
|
|
return true;
|
|
}
|
|
}
|
|
}
|