Save custom fields on subscribe

- added methods to get/set a specific custom field
- added method to get all custom fields (and assign each custom field to the subscriber's instance)
- fixed zIndex of form editor's toolbar (footer was positioned above, preventing click)
This commit is contained in:
Jonathan Labreuille
2016-01-19 17:02:05 +01:00
parent 7bd25660df
commit daec56191f
4 changed files with 77 additions and 2 deletions

View File

@ -125,6 +125,7 @@ handle_icon = '../img/handle.png'
float: none float: none
#mailpoet_form_toolbar #mailpoet_form_toolbar
z-index: 999
position: absolute position: absolute
width: 400px width: 400px

View File

@ -242,11 +242,18 @@ class Subscriber extends Model {
foreach($data as $key => $value) { foreach($data as $key => $value) {
if(strpos($key, 'cf_') === 0) { if(strpos($key, 'cf_') === 0) {
$custom_fields[substr($key, 3)] = $value; $custom_fields[(int)substr($key, 3)] = $value;
unset($data[$key]); unset($data[$key]);
} }
} }
if(!empty($custom_fields)) {
foreach($custom_fields as $custom_field_id => $value) {
$subscriber->setCustomField($custom_field_id, $value);
}
}
if($subscriber === false) { if($subscriber === false) {
$subscriber = static::create(); $subscriber = static::create();
$subscriber->hydrate($data); $subscriber->hydrate($data);
@ -254,10 +261,50 @@ class Subscriber extends Model {
$subscriber->set($data); $subscriber->set($data);
} }
$subscriber->save(); $subscriber->save();
return $subscriber; return $subscriber;
} }
function getCustomFields() {
$relation = CustomField::select('id')->findArray();
if(empty($relation)) return $this;
$custom_field_ids = Helpers::arrayColumn($relation, 'id');
$custom_fields = SubscriberCustomField::select('id')
->select('value')
->whereIn('custom_field_id', $custom_field_ids)
->where('subscriber_id', $this->id())
->findMany();
foreach($custom_fields as $custom_field) {
$this->{'cf_'.$custom_field->id()} = $custom_field->value;
}
return $this;
}
function getCustomField($custom_field_id, $default = null) {
$custom_field = SubscriberCustomField::select('value')
->where('custom_field_id', $custom_field_id)
->where('subscriber_id', $this->id())
->findOne();
if($custom_field === false) {
return $default;
} else {
return $custom_field->value;
}
}
function setCustomField($custom_field_id, $value) {
return SubscriberCustomField::createOrUpdate(array(
'subscriber_id' => $this->id(),
'custom_field_id' => $custom_field_id,
'value' => $value
));
}
static function bulkMoveToList($orm, $data = array()) { static function bulkMoveToList($orm, $data = array()) {
$segment_id = (isset($data['segment_id']) ? (int)$data['segment_id'] : 0); $segment_id = (isset($data['segment_id']) ? (int)$data['segment_id'] : 0);
$segment = Segment::findOne($segment_id); $segment = Segment::findOne($segment_id);

View File

@ -12,6 +12,33 @@ class SubscriberCustomField extends Model {
parent::__construct(); parent::__construct();
} }
static function createOrUpdate($data = array()) {
$custom_field = CustomField::findOne($data['custom_field_id'])->asArray();
if($custom_field === false) return false;
if($custom_field['type'] === 'date') {
if(is_array($data['value'])) {
$day = (isset($data['value']['day']) ? : 1);
$month = (isset($data['value']['month']) ? : 1);
$year = (isset($data['value']['year']) ? : 1970);
$data['value'] = mktime(0, 0, 0, $month, $day, $year);
}
}
$relation = self::where('custom_field_id', $data['custom_field_id'])
->where('subscriber_id', $data['subscriber_id'])
->findOne();
if($relation === false) {
$relation = self::create();
$relation->hydrate($data);
} else {
$relation->set($data);
}
return $relation->save();
}
static function createMultiple($values) { static function createMultiple($values) {
$values = array_map('array_values', $values); $values = array_map('array_values', $values);
return self::rawExecute( return self::rawExecute(

View File

@ -24,7 +24,7 @@ class Subscribers {
} else { } else {
$segments = $subscriber->segments()->findArray(); $segments = $subscriber->segments()->findArray();
$subscriber = $subscriber->asArray(); $subscriber = $subscriber->getCustomFields()->asArray();
$subscriber['segments'] = array_map(function($segment) { $subscriber['segments'] = array_map(function($segment) {
return $segment['id']; return $segment['id'];
}, $segments); }, $segments);