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

View File

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