00001 <?php
00002
00003
00004
00005 class Notifications {
00006 const SOURCE_ALL = 'all';
00007 const SOURCE_APP = 'app';
00008
00009 const STATUS_NEW = 'NEW';
00010 const STATUS_READ = 'READ';
00011
00012 const DELIVER_MAIL = 'MAIL';
00013 const DELIVER_DIGEST = 'DIGEST';
00014 const DELIVER_FEED = 'FEED';
00015
00016 const READ_UNKNOWN = 'UNKNOWN';
00017 const READ_MARK_MANUALLY = 'MANUALLY';
00018 const READ_MARK_AUTO = 'AUTO';
00019 const READ_MARK_ALL = 'ALL';
00020 const READ_MAIL = 'MAIL';
00021 const READ_DIGEST = 'DIGEST';
00022 const READ_FEED = 'FEED';
00023 const READ_CONTENT = 'CONTENT';
00024
00025
00026
00027
00028
00029
00030 public static function get($id) {
00031 return DB::get_item('notifications', 'id', $id);
00032 }
00033
00034
00035
00036
00037
00038
00039 public static function get_status() {
00040 return array(
00041 self::STATUS_NEW => tr(self::STATUS_NEW, 'notifications'),
00042 self::STATUS_READ => tr(self::STATUS_READ, 'notifications')
00043 );
00044 }
00045
00046
00047
00048
00049
00050
00051 public static function get_delivery_methods() {
00052 return array(
00053 self::DELIVER_MAIL => tr(self::DELIVER_MAIL, 'notifications'),
00054 self::DELIVER_FEED => tr(self::DELIVER_FEED, 'notifications'),
00055 self::DELIVER_DIGEST => tr(self::DELIVER_DIGEST, 'notifications'),
00056 );
00057 }
00058
00059
00060
00061
00062
00063
00064 public static function get_read_sources() {
00065 return array(
00066 self::READ_UNKNOWN => tr(self::READ_UNKNOWN, 'notifications'),
00067 self::READ_MARK_MANUALLY => tr(self::READ_MARK_MANUALLY, 'notifications'),
00068 self::READ_MARK_AUTO => tr(self::READ_MARK_AUTO, 'notifications'),
00069 self::READ_MARK_ALL => tr(self::READ_MARK_ALL, 'notifications'),
00070 self::READ_MAIL => tr(self::READ_MAIL, 'notifications'),
00071 self::READ_FEED => tr(self::READ_FEED, 'notifications'),
00072 self::READ_DIGEST => tr(self::READ_DIGEST, 'notifications'),
00073 self::READ_CONTENT => tr(self::READ_CONTENT, 'notifications'),
00074 );
00075 }
00076
00077
00078
00079
00080
00081
00082 public static function create_user_adapter($id_user) {
00083 $ret = new DAONotifications();
00084 $ret->id_user = $id_user;
00085 $ret->sort('creationdate', DataObjectBase::DESC);
00086 return $ret;
00087 }
00088
00089
00090
00091
00092
00093
00094 public static function create_unread_user_adapter($id_user) {
00095 $ret = self::create_user_adapter($id_user);
00096 $ret->status = self::STATUS_NEW;
00097 return $ret;
00098 }
00099
00100
00101
00102
00103
00104
00105 public static function get_latest($user_id, $num) {
00106 $dao = self::create_unread_user_adapter($user_id);
00107 $dao->limit(0, $num);
00108 return $dao->find_array();
00109 }
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119 public static function create(DAOUsers $user, $params, &$created) {
00120 $ret = new Status();
00121 $params['id_user'] = $user->id;
00122 $params['title'] = self::compute_title(Arr::get_item($params, 'message', ''), Arr::get_item($params, 'title', ''));
00123 $cmd = CommandsFactory::create_command('notifications', 'create', $params);
00124 $ret->merge($cmd->execute());
00125 $created = $cmd->get_result();
00126 return $ret;
00127 }
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137 public static function notify_single_user(DAOUsers $user, $message, $title = '', $source = 'app', $params = array()) {
00138 $ret = new Status();
00139 $params['title'] = self::compute_title($message, $title);
00140 $params['message'] = $message;
00141 $params['source'] = $source;
00142
00143 $cmd = CommandsFactory::create_command($user, 'notify', $params);
00144 if ($cmd->can_execute($user)) {
00145 $ret->merge($cmd->execute());
00146 }
00147 return $ret;
00148 }
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158 public static function notify_some_users($arr_users, $message, $title = '', $source = 'app', $params = array()) {
00159 $ret = new Status();
00160 $already_notified_ids = array();
00161 $title = self::compute_title($message, $title);
00162 foreach($arr_users as $user) {
00163 $user_id = $user->id;
00164 if (!in_array($user_id, $already_notified_ids)) {
00165 $ret->merge(self::notify_single_user($user, $message, $title, $source, $params));
00166 if ($ret->is_error()) {
00167 break;
00168 }
00169 $already_notified_ids[] = $user_id;
00170 }
00171 }
00172 return $ret;
00173 }
00174
00175
00176
00177
00178
00179
00180
00181
00182 public static function notify_all_users($message, $title = '', $source = 'app', $params = array()) {
00183 $params['title'] = self::compute_title($message, $title);
00184 $params['message'] = $message;
00185 $params['source'] = $source;
00186 $cmd = CommandsFactory::create_command('users', 'notifyall', $params);
00187 return $cmd->execute();
00188 }
00189
00190
00191
00192
00193 private static function compute_title($message, $title) {
00194 if (empty($title)) {
00195 $title = String::substr_word(String::clear_html($message), 0, 150) . '...';
00196 }
00197 return $title;
00198 }
00199
00200
00201
00202
00203
00204
00205 public static function get_all_sources($id_user) {
00206 $dao = new DAONotifications();
00207 $dao->id_user = $id_user;
00208 $query = $dao->create_select_query();
00209 $query->set_fields('source');
00210 $query->set_policy(DBQuerySelect::DISTINCT);
00211
00212 $result = DB::query($query);
00213 $ret = array();
00214 while($row = $result->fetch()) {
00215 $ret[$row['source']] = self::translate_source($row['source']);
00216 }
00217 return $ret;
00218 }
00219
00220
00221
00222
00223 public static function translate_source($src) {
00224 $ret = $src;
00225 switch($src) {
00226 case self::SOURCE_ALL:
00227 case self::SOURCE_APP:
00228 $ret = tr($src, 'notifications');
00229 break;
00230 default:
00231 EventSource::Instance()->invoke_event('notifications_translate', $src, $ret);
00232 break;
00233 }
00234 return $ret;
00235 }
00236
00237
00238
00239
00240
00241
00242
00243
00244 public static function existing($params) {
00245 $ret = false;
00246 $source_id = Arr::get_item($params, 'source_id', false);
00247 $id_user = Arr::get_item($params, 'id_user', false);
00248 $source = Arr::get_item($params, 'source', false);
00249 if ($source_id && $id_user && $source) {
00250 $dao = new DAONotifications();
00251 $dao->id_user = $id_user;
00252 $dao->source = $source;
00253 $dao->source_id = $source_id;
00254 $dao->status = self::STATUS_NEW;
00255 if ($dao->find(DataObjectBase::AUTOFETCH)) {
00256 $ret = $dao;
00257 }
00258 }
00259 return $ret;
00260 }
00261
00262
00263
00264
00265
00266
00267 public static function mark_as_read_by_source_id($id_user, $source, $source_id, $read_through = self::READ_CONTENT, $read_action = '') {
00268 if ($source_id && $id_user && $source) {
00269 $params = array(
00270 'read_through' => $read_through,
00271 'read_action' => $read_action
00272 );
00273 $dao = new DAONotifications();
00274 $dao->id_user = $id_user;
00275 $dao->source = $source;
00276 $dao->source_id = $source_id;
00277 $dao->status = self::STATUS_NEW;
00278 $dao->find();
00279 while($dao->fetch()) {
00280 $cmd = CommandsFactory::create_command(clone($dao), 'markread', $params);
00281 $cmd->execute();
00282 }
00283 }
00284 }
00285 }