00001 <?php
00002
00003
00004
00005 class ClickTrackInjecter {
00006 private $source;
00007 private $notification;
00008
00009 public function __construct($notification, $source) {
00010 $this->notification = $notification;
00011 $this->source = $source;
00012 }
00013
00014 public function callback($matches) {
00015
00016 $old_url = $matches[2];
00017 $new_url = $this->notification->create_click_track_link($this->source, $old_url);
00018 return "<a{$matches[1]}href=\"$new_url\"{$macthes[3]}>";
00019 }
00020 }
00021
00022
00023
00024
00025 class DAONotifications extends DataObjectTimestampedCached implements ISelfDescribing, IStatusHolder {
00026 public $id;
00027 public $id_user;
00028 public $title;
00029 public $message;
00030 public $source;
00031 public $source_id;
00032 public $source_data;
00033 public $sent_as;
00034 public $read_through;
00035 public $read_action;
00036 public $status;
00037
00038
00039
00040
00041 protected function create_table_object() {
00042 return new DBTable(
00043 'notifications',
00044 array_merge(array(
00045 new DBFieldInt('id', null, DBFieldInt::AUTOINCREMENT | DBFieldInt::UNSIGNED | DBField::NOT_NULL),
00046 new DBFieldInt('id_user', null, DBFieldInt::UNSIGNED),
00047 new DBFieldText('title', 200, null, DBField::NOT_NULL),
00048 new DBFieldText('message', DBFieldText::BLOB_LENGTH_SMALL, null, DBField::NOT_NULL),
00049 new DBFieldText('source', 100, Notifications::SOURCE_APP, DBField::NOT_NULL),
00050 new DBFieldInt('source_id', null, DBFieldInt::UNSIGNED),
00051 new DBFieldSerialized('source_data', DBFieldSerialized::BLOB_LENGTH_SMALL, null, DBField::NONE),
00052 new DBFieldSet('sent_as', array_keys(Notifications::get_delivery_methods()), null, DBField::NONE),
00053 new DBFieldEnum('read_through', array_keys(Notifications::get_read_sources()), Notifications::READ_UNKNOWN, DBField::NOT_NULL),
00054 new DBFieldText('read_action', 30, null, DBField::NONE),
00055 new DBFieldEnum('status', array_keys(Notifications::get_status()), Notifications::STATUS_NEW, DBField::NOT_NULL),
00056 ), $this->get_timestamp_field_declarations()
00057 ),
00058 'id',
00059 new DBRelation(
00060 'users',
00061 new DBFieldRelation('id_user', 'id'),
00062 DBRelation::NONE
00063 )
00064 );
00065 }
00066
00067
00068
00069
00070 public function add_sent_as($val) {
00071 DBFieldSet::set_set_value($this->sent_as, $val);
00072 }
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082 public function get_message($click_track_source = false) {
00083 $ret = $this->message;
00084 if ($click_track_source) {
00085 $injector = new ClickTrackInjecter($this, $click_track_source);
00086 $reg = '@<a(.*?)href="(.*?)"(.*?)>@';
00087 $ret = String::preg_replace_callback($reg, array($injector, 'callback'), $ret);
00088 }
00089 return $ret;
00090 }
00091
00092
00093
00094
00095
00096
00097 public function click_track_fingerprint($source, $url) {
00098 return sha1(
00099 $this->id .
00100 $this->id_user .
00101 $this->message .
00102 $source .
00103 $this->creationdate .
00104 $url .
00105 $this->title
00106 );
00107 }
00108
00109
00110
00111
00112 public function create_click_track_link($source, $url) {
00113 $new_url = Url::create(ActionMapper::get_url('clicktrack', $this));
00114 $new_url->replace_query_parameter('src', $source);
00115 $new_url->replace_query_parameter('url', $url);
00116 $new_url->replace_query_parameter('token', $this->click_track_fingerprint($source, $url));
00117 return $new_url->build();
00118 }
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132 protected function get_actions_for_context($context, $user, $params) {
00133 $ret = array();
00134 $arrStates = array_keys(Notifications::get_status());
00135 foreach($arrStates as $state) {
00136 $cmd = 'status[' . $state . ']';
00137 $desc = tr('Set ' . $state);
00138 $ret[$cmd] = $desc;
00139 }
00140 $ret['exclude'] = tr('End notification', 'notifications');
00141 return $ret;
00142 }
00143
00144
00145
00146
00147 public function get_filters() {
00148 $sources = array();
00149 foreach(Notifications::get_all_sources($this->id_user) as $source => $descr) {
00150 $sources[$source] = new DBFilterColumn('notifications.source', $source, $descr);
00151 }
00152 return array(
00153 new DBFilterGroup(
00154 'status',
00155 tr('Status', 'notifications'),
00156 array(
00157 'new' => new DBFilterColumn('notifications.status', Notifications::STATUS_NEW, tr('Unread', 'notifications')),
00158 'read' => new DBFilterColumn('notifications.status', Notifications::STATUS_READ, tr('Read', 'notifications')),
00159 ),
00160 'new'
00161 ),
00162 new DBFilterGroup(
00163 'source',
00164 tr('Source', 'notifications'),
00165 $sources
00166 )
00167 );
00168 }
00169
00170
00171
00172
00173
00174
00175 public function get_user() {
00176 return Users::get($this->id_user);
00177 }
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188 public function get_title() {
00189 return $this->title;
00190 }
00191
00192
00193
00194
00195
00196
00197 public function get_description() {
00198 return '';
00199 }
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210 public function set_status($status) {
00211 $this->status = $status;
00212 }
00213
00214
00215
00216
00217
00218
00219 public function get_status() {
00220 return $this->status;
00221 }
00222
00223
00224
00225
00226
00227
00228 public function is_active() {
00229 return $this->status == Notifications::STATUS_NEW;
00230 }
00231
00232
00233
00234
00235
00236
00237 public function is_unconfirmed() {
00238 return false;
00239 }
00240
00241
00242
00243
00244
00245
00246 public function is_deleted() {
00247 return false;
00248 }
00249
00250
00251
00252
00253
00254
00255 public function is_disabled() {
00256 return $this->status == Notifications::STATUS_READ;
00257 }
00258 }