00001 <?php
00002
00003
00004
00005 class NotificationsController extends ControllerBase {
00006
00007
00008
00009
00010
00011 public function get_routes() {
00012 return array(
00013 new ExactMatchRoute('https://user/notifications/', $this, 'users_notifications', new AccessRenderDecorator()),
00014 new ExactMatchRoute('https://user/notifications/settings/', $this, 'notifications_settings', new AccessRenderDecorator()),
00015 new ParameterizedRoute('https://notifications/feeds/{id_user:ui>}/{feed_token:s:40}', $this, 'notifications_feed', new NoCacheCacheManager()),
00016 new ParameterizedRoute('https://notifications/{id:ui>}/', $this, 'notifications_view', new AccessRenderDecorator()),
00017 new NotificationsExcludeRoute('https://notifications/exclude/{source:s}/{source_id:ui>}/{token:s}/', $this, 'notifications_exclude', new AccessRenderDecorator()),
00018
00019 new ExactMatchRoute('https://ajax/notifications/toggle', $this, 'notifications_ajax_toggle', new AccessRenderDecorator()),
00020
00021 new ParameterizedRoute('https://notifications/{id:ui>}/click/', $this, 'notifications_clicktrack'),
00022
00023 new ExactMatchRoute('notifications/digest', $this, 'notifications_digest', new ConsoleOnlyRenderDecorator()),
00024
00025 new CommandsRoute('https://process_commands/notifications/markallasread', $this, 'markallasread_cmd_handler', new AccessRenderDecorator()),
00026 );
00027 }
00028
00029
00030
00031
00032
00033
00034 public function action_users_notifications(PageData $page_data) {
00035 Load::models('notifications');
00036 $adapter = Notifications::create_user_adapter(Users::get_current_user()->id);
00037
00038 $view = ViewFactory::create_view(IViewFactory::CONTENT, 'notifications/my', $page_data);
00039 Load::tools('pager', 'filter');
00040
00041 $filter = new Filter($page_data, $adapter->get_filters());
00042 $filter->apply($adapter);
00043 $filter->prepare_view($view);
00044
00045 $pager = new Pager($page_data, $adapter->count(), Config::get_value(Config::ITEMS_PER_PAGE));
00046 $pager->apply($adapter);
00047 $pager->prepare_view($view);
00048
00049 $view->assign('notifications', $adapter->execute());
00050 $view->render();
00051 }
00052
00053
00054
00055
00056 public function action_notifications_view(PageData $page_data, $id) {
00057 Load::models('notifications');
00058 $n = Notifications::get($id);
00059 if ($n === false || !Users::is_current($n->get_user())) {
00060 return self::ACCESS_DENIED;
00061 }
00062
00063 $src = $page_data->get_get()->get_item('src', false);
00064 if ($src) {
00065 $src = strtoupper($src);
00066 if (key_exists($src, Notifications::get_read_sources())) {
00067 $cmd = CommandsFactory::create_command($n, 'markread', array('read_through' => $src, 'read_action' => 'click'));
00068 $cmd->execute();
00069 }
00070 }
00071
00072 $page_data->head->title = $n->get_title();
00073 $page_data->breadcrumb = WidgetBreadcrumb::output(array(
00074 WidgetActionLink::output(tr('Your Notifications', 'notifications'), 'users_notifications'),
00075 $n
00076 ));
00077
00078
00079 $view = ViewFactory::create_view(IViewFactory::CONTENT, 'notifications/view', $page_data);
00080 $view->assign('notification', $n);
00081 $view->render();
00082 }
00083
00084
00085
00086
00087 public function action_notifications_clicktrack(PageData $page_data, $id) {
00088 Load::models('notifications');
00089 $n = Notifications::get($id);
00090 if ($n === false) {
00091 return self::NOT_FOUND;
00092 }
00093
00094 $url = $page_data->get_get()->get_item('url', '');
00095 $src = $page_data->get_get()->get_item('src', false);
00096 $token = $page_data->get_get()->get_item('token', '');
00097 if ($token != $n->click_track_fingerprint($src, $url)) {
00098 return self::NOT_FOUND;
00099 }
00100
00101 $cmd = CommandsFactory::create_command($n, 'markread', array('read_through' => $src, 'read_action' => 'click'));
00102 $cmd->execute();
00103
00104 Url::create_with_fallback_host($url, Config::get_value(Config::URL_DOMAIN))->redirect(Url::TEMPORARY);
00105 }
00106
00107
00108
00109
00110 public function action_notifications_settings(PageData $page_data) {
00111 Load::models('notificationssettings');
00112 Load::tools('formhandler');
00113 $formhandler = new FormHandler('notificationssettings');
00114
00115 $user = Users::get_current_user();
00116 $settings = NotificationsSettings::get_for_user($user->id);
00117
00118 if ($page_data->has_post_data()) {
00119 $this->do_notifications_settings($page_data, $formhandler, $settings);
00120 }
00121
00122 $view = ViewFactory::create_view(IViewFactory::CONTENT, 'notifications/settings', $page_data);
00123 $view->assign('sources', NotificationsSettings::collect_sources($user));
00124 $view->assign('settings', $settings);
00125
00126 if (empty($settings)) {
00127 $settings = new DAONotificationssettings();
00128 $settings->set_default_values();
00129 }
00130 $formhandler->prepare_view($view, $settings);
00131
00132 $view->render();
00133 }
00134
00135
00136
00137
00138 protected function do_notifications_settings(PageData $page_data, FormHandler $formhandler, $settings) {
00139 $err = $formhandler->validate();
00140 if ($err->is_ok()) {
00141 $update = ($settings != false);
00142 $data = $page_data->get_post()->get_array();
00143 $data['id_user'] = Users::get_current_user()->id;
00144 if (!$update) {
00145 $settings = new DAONotificationssettings();
00146 }
00147 $data = $settings->unset_internals($data);
00148
00149 $cmd = $update
00150 ? CommandsFactory::create_command($settings, 'update', $data)
00151 : CommandsFactory::create_command('notificationssettings', 'create', $data);
00152 $err->merge($cmd->execute());
00153 }
00154 $formhandler->finish($err, tr('Your settings have been saved', 'notifications'));
00155 }
00156
00157
00158
00159
00160 public function action_notifications_feed(PageData $page_data, $id_user, $feed_token) {
00161 $page_data->in_history = false;
00162 Load::models('notificationssettings');
00163 $settings = NotificationsSettings::get_for_user($id_user);
00164 if ($settings === false || !$settings->feed_enable || $settings->feed_token != $feed_token) {
00165 return self::NOT_FOUND;
00166 }
00167
00168
00169 $dao = NotificationsSettings::create_feed_adapter($settings);
00170 $nots = array();
00171 $dao->find();
00172 while($dao->fetch()) {
00173 if ($settings->should_notification_be_processed($dao, NotificationsSettings::TYPE_FEED)) {
00174 $n = clone($dao);
00175 $nots[] = $n;
00176 $n->add_sent_as(Notifications::DELIVER_FEED);
00177 $cmd = CommandsFactory::create_command($n, 'update', array());
00178 $cmd->execute();
00179 }
00180 }
00181
00182 $view = ViewFactory::create_view(ViewFactoryMime::MIME, 'notifications/feed', $page_data);
00183 $view->assign('notifications', $nots);
00184 $view->render();
00185 }
00186
00187
00188
00189
00190 public function action_notifications_exclude(PageData $page_data, $source, $source_id, $token) {
00191 $data = array('source' => $source, 'source_id' => $source_id);
00192 $user = Users::get_current_user();
00193 if ($token != $user->create_token('exclude', $data)) {
00194 return self::NOT_FOUND;
00195 }
00196
00197 $data['id_user'] = $user->id;
00198 $cmd = CommandsFactory::create_command('notificationsexceptions', 'create', $data);
00199 $err = $cmd->execute();
00200 if ($err->is_ok()) {
00201 $err = new Message(tr('Notifications have been disabled for the given item', 'notifications'));
00202 }
00203 History::go_to(0, $err, ActionMapper::get_url('users_notifications'));
00204 }
00205
00206
00207
00208
00209 public function action_notifications_ajax_toggle(PageData $page_data) {
00210 $page_data->in_history = false;
00211 $page_data->page_template = 'emptypage';
00212 $id = $page_data->get_post()->get_item('id', false);
00213 $notification = DB::get_item('notifications', 'id', $id);
00214 if (!AccessControl::is_allowed('status', $notification)) {
00215 return self::ACCESS_DENIED;
00216 }
00217
00218 $new_status = ($notification->get_status() != Notifications::STATUS_NEW) ? Notifications::STATUS_NEW : Notifications::STATUS_READ;
00219 $cmd = CommandsFactory::create_command($notification, 'status', $new_status);
00220 $err = $cmd->execute();
00221 if ($err->is_ok()) {
00222 $view = ViewFactory::create_view(IViewFactory::MESSAGE, 'notifications/inc/item', $page_data);
00223 $view->assign('notification', $notification);
00224 $page_data->content = $view->render();
00225 }
00226 else {
00227 return self::INTERNAL_ERROR;
00228 }
00229 }
00230
00231
00232
00233
00234 public function action_notifications_digest(PageData $page_data) {
00235 $cmd = CommandsFactory::create_command('notifications', 'digest', false);
00236 $page_data->status = $cmd->execute();
00237 }
00238
00239 public function action_markallasread_cmd_handler(PageData $page_data) {
00240 $cmd = CommandsFactory::create_command('notifications', 'markallasread', false);
00241 if (!$cmd->can_execute(false)) {
00242 return CONTROLLER_ACCESS_DENIED;
00243 }
00244 $err = $cmd->execute();
00245 if ($err->is_ok()) {
00246 $err = new Message(tr('All notifications have been marked as read', 'notifications'));
00247 }
00248 History::go_to(0, $err);
00249 exit;
00250 }
00251 }