Merge pull request #759 from mailpoet/deleted_lists_fix

Show deleted lists in newsletter listings [MAILPOET-489]
This commit is contained in:
mrcasual
2016-12-27 11:26:59 -05:00
committed by GitHub
3 changed files with 46 additions and 4 deletions

View File

@@ -312,7 +312,7 @@ class Newsletters extends APIEndpoint {
if($newsletter->type === Newsletter::TYPE_STANDARD) {
$newsletter
->withSegments()
->withSegments(true)
->withSendingQueue()
->withStatistics();
} else if($newsletter->type === Newsletter::TYPE_WELCOME) {
@@ -323,11 +323,11 @@ class Newsletters extends APIEndpoint {
} else if($newsletter->type === Newsletter::TYPE_NOTIFICATION) {
$newsletter
->withOptions()
->withSegments()
->withSegments(true)
->withChildrenCount();
} else if($newsletter->type === Newsletter::TYPE_NOTIFICATION_HISTORY) {
$newsletter
->withSegments()
->withSegments(true)
->withSendingQueue()
->withStatistics();
}

View File

@@ -199,8 +199,39 @@ class Newsletter extends Model {
);
}
function withSegments() {
function withSegments($incl_deleted = false) {
$this->segments = $this->segments()->findArray();
if($incl_deleted) {
$this->withDeletedSegments();
}
return $this;
}
// Intermediary table only
function segmentLinks() {
return $this->has_many(
__NAMESPACE__.'\NewsletterSegment',
'newsletter_id',
'id'
);
}
function withDeletedSegments() {
if(!empty($this->segments)) {
$segment_ids = Helpers::arrayColumn($this->segments, 'id');
$links = $this->segmentLinks()
->whereNotIn('segment_id', $segment_ids)->findArray();
$deleted_segments = array();
foreach($links as $link) {
$deleted_segments[] = array(
'id' => $link['segment_id'],
'name' => __('Deleted list', 'mailpoet')
);
}
$this->segments = array_merge($this->segments, $deleted_segments);
}
return $this;
}

View File

@@ -106,6 +106,17 @@ class NewsletterTest extends MailPoetTest {
expect($newsletter_segments[1]['name'])->equals('Segment 2');
}
function testItCanHaveDeletedSegments() {
$this->segment_2->delete();
$this->newsletter->withSegments(true);
$newsletter_segments = $this->newsletter->segments;
expect($newsletter_segments)->count(2);
expect($newsletter_segments[0]['id'])->equals($this->segment_1->id);
expect($newsletter_segments[0]['name'])->equals('Segment 1');
expect($newsletter_segments[1]['id'])->equals($this->segment_2->id);
expect($newsletter_segments[1]['name'])->contains('Deleted');
}
function testItCanHaveStatistics() {
$newsletter = $this->newsletter;
$sending_queue = SendingQueue::create();