Subscribe in comments
- added Subscriber::subscribe($user, $segment_ids) - refactored Router\Subscribers->subscribe() method to account for new method - added Form\Subscribe class to handle subscription in comments - updated Basics settings page (changed "list" to "segment")
This commit is contained in:
@ -32,6 +32,65 @@ class Subscriber extends Model {
|
||||
return parent::delete();
|
||||
}
|
||||
|
||||
function addToSegments(array $segment_ids = array()) {
|
||||
$segments = Segment::whereIn('id', $segment_ids)->findMany();
|
||||
foreach($segments as $segment) {
|
||||
$association = SubscriberSegment::create();
|
||||
$association->subscriber_id = $this->id;
|
||||
$association->segment_id = $segment->id;
|
||||
$association->save();
|
||||
}
|
||||
}
|
||||
|
||||
static function subscribe($subscriber_data = array(), $segment_ids = array()) {
|
||||
if(empty($subscriber_data) or empty($segment_ids)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$subscriber = static::createOrUpdate($subscriber_data);
|
||||
|
||||
if($subscriber !== false && $subscriber->id() > 0) {
|
||||
$signup_confirmation = Setting::getValue('signup_confirmation', array());
|
||||
$has_signup_confirmation = true;
|
||||
if(array_key_exists('enabled', $signup_confirmation)) {
|
||||
$has_signup_confirmation = filter_var(
|
||||
$signup_confirmation['enabled'],
|
||||
FILTER_VALIDATE_BOOLEAN
|
||||
);
|
||||
}
|
||||
|
||||
// restore deleted subscriber
|
||||
if($subscriber->deleted_at !== NULL) {
|
||||
$subscriber->setExpr('deleted_at', 'NULL');
|
||||
}
|
||||
|
||||
if($has_signup_confirmation === false) {
|
||||
// auto subscribe when signup confirmation is turned off
|
||||
$subscriber->set('status', 'subscribed');
|
||||
} else {
|
||||
// reset status of existing subscribers if signup confirmation
|
||||
// is turned on
|
||||
if($subscriber->isNew() === false) {
|
||||
// existing subscriber
|
||||
if($subscriber->status !== 'subscribed') {
|
||||
$subscriber->set('status', 'unconfirmed');
|
||||
}
|
||||
}
|
||||
|
||||
// send confirmation email to unconfirmed subscribers
|
||||
if($subscriber->status === 'unconfirmed') {
|
||||
// TODO: send signup confirmation email
|
||||
}
|
||||
}
|
||||
|
||||
if($subscriber->save()) {
|
||||
$subscriber->addToSegments($segment_ids);
|
||||
}
|
||||
}
|
||||
|
||||
return $subscriber;
|
||||
}
|
||||
|
||||
static function search($orm, $search = '') {
|
||||
if(strlen(trim($search) === 0)) {
|
||||
return $orm;
|
||||
@ -181,17 +240,52 @@ class Subscriber extends Model {
|
||||
$subscriber = false;
|
||||
|
||||
if(isset($data['id']) && (int)$data['id'] > 0) {
|
||||
$subscriber = self::findOne((int)$data['id']);
|
||||
$subscriber = static::findOne((int)$data['id']);
|
||||
unset($data['id']);
|
||||
}
|
||||
|
||||
if($subscriber === false && !empty($data['email'])) {
|
||||
$subscriber = static::where('email', $data['email'])->findOne();
|
||||
if($subscriber !== false) {
|
||||
unset($data['email']);
|
||||
}
|
||||
}
|
||||
|
||||
if($subscriber === false) {
|
||||
$subscriber = self::create();
|
||||
$subscriber = static::create();
|
||||
$subscriber->hydrate($data);
|
||||
} else {
|
||||
$subscriber->set($data);
|
||||
}
|
||||
|
||||
// TODO: Cf
|
||||
/*
|
||||
// custom fields
|
||||
$custom_fields = array();
|
||||
foreach($data as $key => $value) {
|
||||
if(strpos($key, 'cf_') === 0) {
|
||||
$custom_fields[substr($key, 3)] = $value;
|
||||
unset($data[$key]);
|
||||
}
|
||||
}
|
||||
|
||||
// add custom fields
|
||||
if(!empty($custom_fields)) {
|
||||
foreach($custom_fields as $custom_field_id => $value) {
|
||||
if(is_array($value)) {
|
||||
// date
|
||||
$value = mktime(0, 0, 0, $value['month'], $value['day'], $value['year']);
|
||||
}
|
||||
$subscriber_custom_field = SubscriberCustomField::create();
|
||||
$subscriber_custom_field->hydrate(array(
|
||||
'subscriber_id' => $subscriber->id(),
|
||||
'custom_field_id' => $custom_field_id,
|
||||
'value' => $value
|
||||
));
|
||||
$subscriber_custom_field->save();
|
||||
}
|
||||
}*/
|
||||
|
||||
$subscriber->save();
|
||||
return $subscriber;
|
||||
}
|
||||
|
Reference in New Issue
Block a user