Move dynamic segments from Premium plugin
[MAILPOET-2382]
This commit is contained in:
committed by
Jack Kitterhing
parent
0af9f09f50
commit
70a89b7939
114
lib/Models/DynamicSegment.php
Normal file
114
lib/Models/DynamicSegment.php
Normal file
@ -0,0 +1,114 @@
|
||||
<?php
|
||||
|
||||
namespace MailPoet\Premium\Models;
|
||||
|
||||
use MailPoet\Models\Segment as MailPoetSegment;
|
||||
use MailPoet\Premium\DynamicSegments\Filters\Filter;
|
||||
use MailPoet\WP\Functions as WPFunctions;
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
* @property string $name
|
||||
* @property string $description
|
||||
* @property string $created_at
|
||||
* @property string $updated_at
|
||||
*/
|
||||
class DynamicSegment extends MailPoetSegment {
|
||||
|
||||
const TYPE_DYNAMIC = 'dynamic';
|
||||
|
||||
/** @var Filter[] */
|
||||
private $filters = [];
|
||||
|
||||
/**
|
||||
* @return Filter[]
|
||||
*/
|
||||
public function getFilters() {
|
||||
return $this->filters;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Filter[] $filters
|
||||
*/
|
||||
public function setFilters(array $filters) {
|
||||
$this->filters = $filters;
|
||||
}
|
||||
|
||||
function save() {
|
||||
$this->set('type', DynamicSegment::TYPE_DYNAMIC);
|
||||
return parent::save();
|
||||
}
|
||||
|
||||
function dynamicSegmentFilters() {
|
||||
return $this->has_many(__NAMESPACE__ . '\DynamicSegmentFilter', 'segment_id');
|
||||
}
|
||||
|
||||
static function findAll() {
|
||||
$query = self::select('*');
|
||||
return $query->where('type', DynamicSegment::TYPE_DYNAMIC)
|
||||
->whereNull('deleted_at')
|
||||
->findMany();
|
||||
}
|
||||
|
||||
static function listingQuery(array $data = []) {
|
||||
$query = self::select('*');
|
||||
$query->where('type', DynamicSegment::TYPE_DYNAMIC);
|
||||
if (isset($data['group'])) {
|
||||
$query->filter('groupBy', $data['group']);
|
||||
}
|
||||
if (isset($data['search'])) {
|
||||
$query->filter('search', $data['search']);
|
||||
}
|
||||
return $query;
|
||||
}
|
||||
|
||||
static function groups() {
|
||||
return [
|
||||
[
|
||||
'name' => 'all',
|
||||
'label' => WPFunctions::get()->__('All', 'mailpoet-premium'),
|
||||
'count' => DynamicSegment::getPublished()->where('type', DynamicSegment::TYPE_DYNAMIC)->count(),
|
||||
],
|
||||
[
|
||||
'name' => 'trash',
|
||||
'label' => WPFunctions::get()->__('Trash', 'mailpoet-premium'),
|
||||
'count' => parent::getTrashed()->where('type', DynamicSegment::TYPE_DYNAMIC)->count(),
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
function delete() {
|
||||
DynamicSegmentFilter::where('segment_id', $this->id)->deleteMany();
|
||||
return parent::delete();
|
||||
}
|
||||
|
||||
static function bulkTrash($orm) {
|
||||
$count = parent::bulkAction($orm, function($ids) {
|
||||
$placeholders = join(',', array_fill(0, count($ids), '?'));
|
||||
DynamicSegment::rawExecute(join(' ', [
|
||||
'UPDATE `' . DynamicSegment::$_table . '`',
|
||||
'SET `deleted_at` = NOW()',
|
||||
'WHERE `id` IN (' . $placeholders . ')',
|
||||
]), $ids);
|
||||
});
|
||||
|
||||
return ['count' => $count];
|
||||
}
|
||||
|
||||
static function bulkDelete($orm) {
|
||||
$count = parent::bulkAction($orm, function($ids) {
|
||||
$placeholders = join(',', array_fill(0, count($ids), '?'));
|
||||
DynamicSegmentFilter::rawExecute(join(' ', [
|
||||
'DELETE FROM `' . DynamicSegmentFilter::$_table . '`',
|
||||
'WHERE `segment_id` IN (' . $placeholders . ')',
|
||||
]), $ids);
|
||||
DynamicSegment::rawExecute(join(' ', [
|
||||
'DELETE FROM `' . DynamicSegment::$_table . '`',
|
||||
'WHERE `id` IN (' . $placeholders . ')',
|
||||
]), $ids);
|
||||
});
|
||||
|
||||
return ['count' => $count];
|
||||
}
|
||||
|
||||
}
|
55
lib/Models/DynamicSegmentFilter.php
Normal file
55
lib/Models/DynamicSegmentFilter.php
Normal file
@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
namespace MailPoet\Premium\Models;
|
||||
|
||||
use MailPoet\Models\Model;
|
||||
use MailPoet\WP\Functions as WPFunctions;
|
||||
|
||||
/**
|
||||
* @property array|string|null $filter_data
|
||||
* @property string $segment_id
|
||||
*/
|
||||
class DynamicSegmentFilter extends Model {
|
||||
|
||||
public static $_table = MP_DYNAMIC_SEGMENTS_FILTERS_TABLE;
|
||||
|
||||
function save() {
|
||||
if (is_null($this->filter_data)) {
|
||||
$this->filter_data = [];
|
||||
}
|
||||
|
||||
if (!WPFunctions::get()->isSerialized($this->filter_data)) {
|
||||
$this->filter_data = serialize($this->filter_data);
|
||||
}
|
||||
|
||||
return parent::save();
|
||||
}
|
||||
|
||||
static function getAllBySegmentIds($segmentIds) {
|
||||
if (empty($segmentIds)) return [];
|
||||
$query = self::tableAlias('filters')
|
||||
->whereIn('filters.segment_id', $segmentIds);
|
||||
|
||||
$query->findMany();
|
||||
return $query->findMany();
|
||||
}
|
||||
|
||||
public function __get($name) {
|
||||
$value = parent::__get($name);
|
||||
if ($name === 'filter_data' && WPFunctions::get()->isSerialized($value)) {
|
||||
return unserialize($value);
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
|
||||
static function deleteAllBySegmentIds($segmentIds) {
|
||||
if (empty($segmentIds)) return;
|
||||
|
||||
$query = self::tableAlias('filters')
|
||||
->whereIn('segment_id', $segmentIds);
|
||||
|
||||
$query->deleteMany();
|
||||
|
||||
}
|
||||
|
||||
}
|
27
lib/Models/SubscribersInDynamicSegment.php
Normal file
27
lib/Models/SubscribersInDynamicSegment.php
Normal file
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace MailPoet\Premium\Models;
|
||||
|
||||
use MailPoet\Models\Subscriber;
|
||||
use MailPoet\Premium\DynamicSegments\Mappers\DBMapper;
|
||||
use MailPoet\Premium\DynamicSegments\Persistence\Loading\SingleSegmentLoader;
|
||||
|
||||
class SubscribersInDynamicSegment extends Subscriber {
|
||||
|
||||
static function listingQuery(array $data = []) {
|
||||
$query = self::select(self::$_table . '.*');
|
||||
$single_segment_loader = new SingleSegmentLoader(new DBMapper());
|
||||
$dynamic_segment = $single_segment_loader->load($data['filter']['segment']);
|
||||
foreach ($dynamic_segment->getFilters() as $filter) {
|
||||
$query = $filter->toSql($query);
|
||||
}
|
||||
if (isset($data['group'])) {
|
||||
$query->filter('groupBy', $data['group']);
|
||||
}
|
||||
if (isset($data['search']) && $data['search']) {
|
||||
$query->filter('search', $data['search']);
|
||||
}
|
||||
return $query;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user