Refactor simple array usages in models

[MAILPOET-1495]
This commit is contained in:
Rostislav Wolný
2018-10-10 19:32:26 +02:00
parent 112f7b21d5
commit 436406f800
2 changed files with 26 additions and 22 deletions

View File

@ -14,15 +14,19 @@ class Form extends Model {
)); ));
} }
function getSettings() {
return is_serialized($this->settings) ? unserialize($this->settings) : $this->settings;
}
function getBody() {
return is_serialized($this->body) ? unserialize($this->body) : $this->body;
}
function asArray() { function asArray() {
$model = parent::asArray(); $model = parent::asArray();
$model['body'] = (is_serialized($this->body)) $model['body'] = $this->getBody();
? unserialize($this->body) $model['settings'] = $this->getSettings();
: $this->body;
$model['settings'] = (is_serialized($this->settings))
? unserialize($this->settings)
: $this->settings;
return $model; return $model;
} }
@ -40,15 +44,15 @@ class Form extends Model {
} }
function getFieldList() { function getFieldList() {
$form = $this->asArray(); $body = $this->getBody();
if(empty($form['body'])) { if(empty($body)) {
return false; return false;
} }
$skipped_types = array('html', 'divider', 'submit'); $skipped_types = array('html', 'divider', 'submit');
$fields = array(); $fields = array();
foreach((array)$form['body'] as $field) { foreach((array)$body as $field) {
if(empty($field['id']) if(empty($field['id'])
|| empty($field['type']) || empty($field['type'])
|| in_array($field['type'], $skipped_types) || in_array($field['type'], $skipped_types)
@ -66,17 +70,17 @@ class Form extends Model {
} }
function filterSegments(array $segment_ids = array()) { function filterSegments(array $segment_ids = array()) {
$form = $this->asArray(); $settings = $this->getSettings();
if(empty($form['settings']['segments'])) { if(empty($settings['segments'])) {
return array(); return array();
} }
if(!empty($form['settings']['segments_selected_by']) if(!empty($settings['segments_selected_by'])
&& $form['settings']['segments_selected_by'] == 'user' && $settings['segments_selected_by'] == 'user'
) { ) {
$segment_ids = array_intersect($segment_ids, $form['settings']['segments']); $segment_ids = array_intersect($segment_ids, $settings['segments']);
} else { } else {
$segment_ids = $form['settings']['segments']; $segment_ids = $settings['segments'];
} }
return $segment_ids; return $segment_ids;

View File

@ -367,12 +367,12 @@ class Newsletter extends Model {
if($duplicate->getErrors() === false) { if($duplicate->getErrors() === false) {
// create relationships between duplicate and segments // create relationships between duplicate and segments
$segments = $this->segments()->findArray(); $segments = $this->segments()->findMany();
if(!empty($segments)) { if(!empty($segments)) {
foreach($segments as $segment) { foreach($segments as $segment) {
$relation = NewsletterSegment::create(); $relation = NewsletterSegment::create();
$relation->segment_id = $segment['id']; $relation->segment_id = $segment->id;
$relation->newsletter_id = $duplicate->id; $relation->newsletter_id = $duplicate->id;
$relation->save(); $relation->save();
} }
@ -380,14 +380,14 @@ class Newsletter extends Model {
// duplicate options // duplicate options
$options = NewsletterOption::where('newsletter_id', $this->id) $options = NewsletterOption::where('newsletter_id', $this->id)
->findArray(); ->findMany();
if(!empty($options)) { if(!empty($options)) {
foreach($options as $option) { foreach($options as $option) {
$relation = NewsletterOption::create(); $relation = NewsletterOption::create();
$relation->newsletter_id = $duplicate->id; $relation->newsletter_id = $duplicate->id;
$relation->option_field_id = $option['option_field_id']; $relation->option_field_id = $option->option_field_id;
$relation->value = $option['value']; $relation->value = $option->value;
$relation->save(); $relation->save();
} }
} }
@ -426,12 +426,12 @@ class Newsletter extends Model {
if($notification_history->getErrors() === false) { if($notification_history->getErrors() === false) {
// create relationships between notification history and segments // create relationships between notification history and segments
$segments = $this->segments()->findArray(); $segments = $this->segments()->findMany();
if(!empty($segments)) { if(!empty($segments)) {
foreach($segments as $segment) { foreach($segments as $segment) {
$relation = NewsletterSegment::create(); $relation = NewsletterSegment::create();
$relation->segment_id = $segment['id']; $relation->segment_id = $segment->id;
$relation->newsletter_id = $notification_history->id; $relation->newsletter_id = $notification_history->id;
$relation->save(); $relation->save();
} }