00001 <?php
00002 Load::models('notifications');
00003
00004
00005
00006
00007 class DAONotificationssettings extends DataObjectCached {
00008 public $id;
00009 public $id_user;
00010 public $mail_enable;
00011 public $mail_settings;
00012 public $digest_enable;
00013 public $digest_settings;
00014 public $digest_last_sent;
00015 public $feed_enable;
00016 public $feed_settings;
00017 public $feed_token;
00018
00019
00020
00021
00022 protected function create_table_object() {
00023 return new DBTable(
00024 'notificationssettings',
00025 array(
00026 new DBFieldInt('id', null, DBFieldInt::AUTOINCREMENT | DBFieldInt::UNSIGNED | DBField::NOT_NULL),
00027 new DBFieldInt('id_user', null, DBFieldInt::UNSIGNED),
00028 new DBFieldBool('mail_enable', true, DBField::NOT_NULL),
00029 new DBFieldSerialized('mail_settings', DBFieldSerialized::BLOB_LENGTH_SMALL, array(Notifications::SOURCE_ALL), DBField::NONE),
00030 new DBFieldBool('digest_enable', false, DBField::NOT_NULL),
00031 new DBFieldSerialized('digest_settings', DBFieldSerialized::BLOB_LENGTH_SMALL, array(Notifications::SOURCE_ALL), DBField::NONE),
00032 new DBFieldDateTime('digest_last_sent', null, DBField::INTERNAL),
00033 new DBFieldBool('feed_enable', false, DBField::NOT_NULL),
00034 new DBFieldSerialized('feed_settings', DBFieldSerialized::BLOB_LENGTH_SMALL, array(Notifications::SOURCE_ALL), DBField::NONE),
00035 new DBFieldText('feed_token', 40, null, DBField::INTERNAL)
00036 ),
00037 'id',
00038 new DBRelation('users', new DBFieldRelation('id_user', 'id'))
00039 );
00040 }
00041
00042
00043
00044
00045
00046
00047 public function validate() {
00048
00049 if ($this->feed_enable === false) {
00050 $this->feed_token = new DBNull();
00051 }
00052 else if ($this->feed_enable === true && (empty($this->feed_token) || $this->feed_token instanceof DBNull)) {
00053 $this->feed_token = $this->create_feed_token();
00054 }
00055
00056 if ($this->digest_enable === false) {
00057 $this->digest_last_sent = new DBNull();
00058 }
00059 else if ($this->digest_enable === true && (empty($this->digest_last_sent) || $this->digest_last_sent instanceof DBNull)) {
00060 $this->digest_last_sent = time();
00061 }
00062
00063
00064 return parent::validate();
00065 }
00066
00067 public function get_user() {
00068 return Users::get($this->id_user);
00069 }
00070
00071
00072
00073
00074 public function is_type_enabled($type) {
00075 $ret = false;
00076 switch($type) {
00077 case NotificationsSettings::TYPE_FEED:
00078 $ret = $this->feed_enable;
00079 break;
00080 case NotificationsSettings::TYPE_MAIL:
00081 $ret = $this->mail_enable;
00082 break;
00083 case NotificationsSettings::TYPE_DIGEST:
00084 $ret = $this->digest_enable;
00085 break;
00086 }
00087 return $ret;
00088 }
00089
00090
00091
00092
00093 public function get_settings_for_type($type) {
00094 $ret = array();
00095 switch($type) {
00096 case NotificationsSettings::TYPE_FEED:
00097 $ret = Arr::force($this->feed_settings, false);
00098 break;
00099 case NotificationsSettings::TYPE_MAIL:
00100 $ret = Arr::force($this->mail_settings, false);
00101 break;
00102 case NotificationsSettings::TYPE_DIGEST:
00103 $ret = Arr::force($this->digest_settings, false);
00104 break;
00105 }
00106 return $ret;
00107 }
00108
00109
00110
00111
00112
00113 public function should_notification_be_processed(DAONotifications $n, $type) {
00114 $ret = $this->source_matches($n->source, $type);
00115
00116 return $ret;
00117 }
00118
00119
00120
00121
00122 public function source_matches($source, $type) {
00123 $ret = false;
00124 if ($this->is_type_enabled($type)) {
00125 $settings = $this->get_settings_for_type($type);
00126 if (in_array(Notifications::SOURCE_ALL, $settings)) {
00127 $ret = true;
00128 }
00129 else {
00130 $ret = in_array($source, $settings);
00131 }
00132 }
00133 return $ret;
00134 }
00135
00136
00137
00138
00139
00140
00141 protected function create_feed_token() {
00142 $user = Users::get($this->id_user);
00143 $seed = rand(1000000, 9999999);
00144 if ($user) {
00145 $seed .= $user->password . $user->creationdate;
00146 }
00147 return Common::create_token($seed);
00148 }
00149
00150
00151
00152
00153
00154
00155 public function is_feed_enabled() {
00156 return $this->feed_enable && $this->feed_token;
00157 }
00158 }