diff --git a/lib/Config/Migrator.php b/lib/Config/Migrator.php index 4dacf1a795..d80d8ea493 100644 --- a/lib/Config/Migrator.php +++ b/lib/Config/Migrator.php @@ -177,6 +177,7 @@ class Migrator { function newsletters() { $attributes = array( 'id mediumint(9) NOT NULL AUTO_INCREMENT,', + 'hash varchar(150) NULL DEFAULT NULL,', 'parent_id mediumint(9) NULL,', 'subject varchar(250) NOT NULL DEFAULT "",', 'type varchar(20) NOT NULL DEFAULT "standard",', diff --git a/lib/Models/Newsletter.php b/lib/Models/Newsletter.php index 795765086b..319962d100 100644 --- a/lib/Models/Newsletter.php +++ b/lib/Models/Newsletter.php @@ -11,7 +11,6 @@ class Newsletter extends Model { const TYPE_WELCOME = 'welcome'; const TYPE_NOTIFICATION = 'notification'; const TYPE_NOTIFICATION_HISTORY = 'notification_history'; - // standard newsletters const STATUS_DRAFT = 'draft'; const STATUS_SCHEDULED = 'scheduled'; @@ -19,6 +18,7 @@ class Newsletter extends Model { const STATUS_SENT = 'sent'; // automatic newsletters status const STATUS_ACTIVE = 'active'; + const NEWSLETTER_HASH_LENGTH = 6; function __construct() { parent::__construct(); @@ -37,6 +37,12 @@ class Newsletter extends Model { ? json_encode($this->body) : $this->body ); + + $this->set('hash', + ($this->hash) + ? $this->hash + : self::generateHash($this->id) + ); return parent::save(); } @@ -647,4 +653,16 @@ class Newsletter extends Model { } return $orm->findMany(); } -} + + static function getByHash($hash) { + return parent::where('hash', $hash) + ->findOne(); + } + + static function generateHash($id = null) { + if(!is_null($id)) { + return substr(md5(AUTH_KEY . $id), 0, self::NEWSLETTER_HASH_LENGTH); + } + return false; + } +} \ No newline at end of file