Manage subscriptions
- make use of the SubscriberSegment::status column to keep track of unsubscriptions - unsubscribed segments now appear grayed out in the Subscribers listing - added unsubscribed_at after segment names when editing a subscriber - added date() method for Twig (uses WP's date format / date_offset) - fixed typo in Form iframe export - fixed unit test for Newsletters - updated selection component (JSX) to allow more customization
This commit is contained in:
@ -52,6 +52,11 @@ class Newsletter extends Model {
|
||||
);
|
||||
}
|
||||
|
||||
function withSegments() {
|
||||
$this->segments = $this->segments()->findArray();
|
||||
return $this;
|
||||
}
|
||||
|
||||
function options() {
|
||||
return $this->has_many_through(
|
||||
__NAMESPACE__.'\NewsletterOptionField',
|
||||
@ -67,6 +72,12 @@ class Newsletter extends Model {
|
||||
->findOne();
|
||||
}
|
||||
|
||||
function withSendingQueue() {
|
||||
$this->queue = $this->getQueue();
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
static function search($orm, $search = '') {
|
||||
return $orm->where_like('subject', '%' . $search . '%');
|
||||
}
|
||||
|
@ -38,15 +38,6 @@ class Segment extends Model {
|
||||
);
|
||||
}
|
||||
|
||||
function segmentFilters() {
|
||||
return $this->has_many_through(
|
||||
__NAMESPACE__.'\Filter',
|
||||
__NAMESPACE__.'\SegmentFilter',
|
||||
'segment_id',
|
||||
'filter_id'
|
||||
);
|
||||
}
|
||||
|
||||
function duplicate($data = array()) {
|
||||
$duplicate = parent::duplicate($data);
|
||||
|
||||
@ -76,6 +67,32 @@ class Segment extends Model {
|
||||
->delete();
|
||||
}
|
||||
|
||||
function withSubscribersCount() {
|
||||
$this->subscribers_count = SubscriberSegment::table_alias('relation')
|
||||
->where('relation.segment_id', $this->id)
|
||||
->join(
|
||||
MP_SUBSCRIBERS_TABLE,
|
||||
'subscribers.id = relation.subscriber_id',
|
||||
'subscribers'
|
||||
)
|
||||
->select_expr(
|
||||
'SUM(CASE subscribers.status WHEN "subscribed" THEN 1 ELSE 0 END)',
|
||||
'subscribed'
|
||||
)
|
||||
->select_expr(
|
||||
'SUM(CASE subscribers.status WHEN "unsubscribed" THEN 1 ELSE 0 END)',
|
||||
'unsubscribed'
|
||||
)
|
||||
->select_expr(
|
||||
'SUM(CASE subscribers.status WHEN "unconfirmed" THEN 1 ELSE 0 END)',
|
||||
'unconfirmed'
|
||||
)
|
||||
->findOne()
|
||||
->asArray();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
static function getWPUsers() {
|
||||
return self::where('type', 'wp_users')->findOne();
|
||||
}
|
||||
|
@ -291,7 +291,7 @@ class Subscriber extends Model {
|
||||
}
|
||||
}
|
||||
if($segment_ids !== false) {
|
||||
$subscriber->addToSegments($segment_ids);
|
||||
SubscriberSegment::setSubscriptions($subscriber, $segment_ids);
|
||||
}
|
||||
}
|
||||
return $subscriber;
|
||||
@ -314,6 +314,17 @@ class Subscriber extends Model {
|
||||
return $this;
|
||||
}
|
||||
|
||||
function withSegments() {
|
||||
$this->segments = $this->segments()->findArray();
|
||||
return $this;
|
||||
}
|
||||
|
||||
function withSubscriptions() {
|
||||
$this->subscriptions = SubscriberSegment::where('subscriber_id', $this->id())
|
||||
->findArray();
|
||||
return $this;
|
||||
}
|
||||
|
||||
function getCustomField($custom_field_id, $default = null) {
|
||||
$custom_field = SubscriberCustomField::select('value')
|
||||
->where('custom_field_id', $custom_field_id)
|
||||
|
@ -12,6 +12,28 @@ class SubscriberSegment extends Model {
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
static function setSubscriptions($subscriber, $segment_ids = array()) {
|
||||
if($subscriber->id > 0) {
|
||||
// unsubscribe from current subscriptions
|
||||
SubscriberSegment::where('subscriber_id', $subscriber->id)
|
||||
->whereNotIn('segment_id', $segment_ids)
|
||||
->findResultSet()
|
||||
->set('status', 'unsubscribed')
|
||||
->save();
|
||||
|
||||
// subscribe to segments
|
||||
foreach($segment_ids as $segment_id) {
|
||||
self::createOrUpdate(array(
|
||||
'subscriber_id' => $subscriber->id,
|
||||
'segment_id' => $segment_id,
|
||||
'status' => 'subscribed'
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
return $subscriber;
|
||||
}
|
||||
|
||||
static function filterWithCustomFields($orm) {
|
||||
$orm = $orm->select(MP_SUBSCRIBERS_TABLE.'.*');
|
||||
$customFields = CustomField::findArray();
|
||||
@ -37,6 +59,30 @@ class SubscriberSegment extends Model {
|
||||
return $orm->where('status', 'subscribed');
|
||||
}
|
||||
|
||||
static function createOrUpdate($data = array()) {
|
||||
$subscription = false;
|
||||
|
||||
if(isset($data['id']) && (int)$data['id'] > 0) {
|
||||
$subscription = self::findOne((int)$data['id']);
|
||||
}
|
||||
|
||||
if(isset($data['subscriber_id']) && isset($data['segment_id'])) {
|
||||
$subscription = self::where('subscriber_id', (int)$data['subscriber_id'])
|
||||
->where('segment_id', (int)$data['segment_id'])
|
||||
->findOne();
|
||||
}
|
||||
|
||||
if($subscription === false) {
|
||||
$subscription = self::create();
|
||||
$subscription->hydrate($data);
|
||||
} else {
|
||||
unset($data['id']);
|
||||
$subscription->set($data);
|
||||
}
|
||||
|
||||
return $subscription->save();
|
||||
}
|
||||
|
||||
static function createMultiple($segmnets, $subscribers) {
|
||||
$values = Helpers::flattenArray(
|
||||
array_map(function ($segment) use ($subscribers) {
|
||||
|
Reference in New Issue
Block a user