00001 <?php
00002
00003
00004
00005 class NotificationsSettings {
00006 const TYPE_MAIL = 'mail';
00007 const TYPE_DIGEST = 'digest';
00008 const TYPE_FEED = 'feed';
00009
00010
00011
00012
00013
00014
00015 public static function get_for_user($user_id) {
00016 return DB::get_item('notificationssettings', 'id_user', $user_id);
00017 }
00018
00019
00020
00021
00022
00023
00024
00025 public static function collect_sources(DAOUsers $user) {
00026 $ret = array(
00027 Notifications::SOURCE_ALL => tr(Notifications::SOURCE_ALL, 'notifications'),
00028 Notifications::SOURCE_APP => tr(Notifications::SOURCE_APP, 'notifications'),
00029 );
00030 EventSource::Instance()->invoke_event('notifications_collect_sources', $user, $ret);
00031
00032 $dao = new DAONotifications();
00033 $dao->id_user = $user->id;
00034
00035 $query = $dao->create_select_query();
00036 $query->set_fields('source');
00037 $query->set_policy(DBQuerySelect::DISTINCT);
00038
00039 $result = DB::query($query);
00040 while($row = $result->fetch()) {
00041 $s = $row['source'];
00042 if (!array_key_exists($s, $ret)) {
00043 $ret[$s] = tr($s);
00044 }
00045 }
00046
00047 return $ret;
00048 }
00049
00050
00051
00052
00053
00054
00055 public static function create_feed_adapter(DAONotificationssettings $settings) {
00056 $dao = self::create_notification_adapter($settings, self::TYPE_FEED);
00057
00058 $dao->add_where('creationdate', '>=', time() - GyroDate::ONE_DAY);
00059 if ($dao->count() < 10) {
00060 $dao = self::create_notification_adapter($settings, self::TYPE_FEED);
00061 $dao->limit(10);
00062 }
00063 $dao->sort('creationdate', DataObjectBase::DESC);
00064 return $dao;
00065 }
00066
00067
00068
00069
00070
00071
00072 public static function create_digest_adapter(DAONotificationssettings $settings) {
00073 $dao = self::create_notification_adapter($settings, self::TYPE_DIGEST);
00074
00075 $dao->add_where('creationdate', '>=', $settings->digest_last_sent);
00076 $dao->sort('creationdate', DataObjectBase::DESC);
00077 return $dao;
00078 }
00079
00080
00081
00082
00083
00084
00085
00086 private static function create_notification_adapter(DAONotificationssettings $settings, $type) {
00087
00088 $dao = Notifications::create_user_adapter($settings->id_user);
00089 $sources = $settings->get_settings_for_type($type);
00090
00091 if (count($sources) == 0 || !$settings->is_type_enabled($type)) {
00092 $dao->add_where('1 = 2');
00093 }
00094 else {
00095 if (!in_array(Notifications::SOURCE_ALL, $sources)) {
00096 $dao->add_where('source', DBWhere::OP_IN, $sources);
00097 }
00098 }
00099 return $dao;
00100 }
00101
00102
00103
00104
00105
00106
00107 public static function create_possible_digest_adapter() {
00108
00109
00110
00111
00112 Load::models('notifications');
00113
00114 $dao = new DAONotificationssettings();
00115 $dao->digest_enable = true;
00116
00117 $n = new DAONotifications();
00118 $dao->join($n, array(
00119 new DBJoinCondition($dao, 'id_user', $n, 'id_user'),
00120 new DBWhere($dao, 'notifications.creationdate >= notificationssettings.digest_last_sent')
00121 ));
00122
00123 return $dao;
00124 }
00125 }