| 1 |
<?php if (! defined('ABSPATH')) exit; // Exit if accessed directly |
| 2 |
interface HC3_INotificator |
| 3 |
{ |
| 4 |
public function queue( HC3_Users_Model $user, $key, $msg ); |
| 5 |
public function send(); |
| 6 |
|
| 7 |
public function setOn(); |
| 8 |
public function setOff(); |
| 9 |
public function isOn(); |
| 10 |
|
| 11 |
public function setOnForUser( HC3_Users_Model $user ); |
| 12 |
public function setOffForUser( HC3_Users_Model $user ); |
| 13 |
public function isOnForUser( HC3_Users_Model $user ); |
| 14 |
} |
| 15 |
|
| 16 |
class HC3_Notificator implements HC3_INotificator |
| 17 |
{ |
| 18 |
protected $_queue = array(); |
| 19 |
protected $off = FALSE; |
| 20 |
|
| 21 |
public $hooks; |
| 22 |
public $self; |
| 23 |
public $email; |
| 24 |
public $settings; |
| 25 |
public $translate; |
| 26 |
public $usersQuery; |
| 27 |
public $auth; |
| 28 |
|
| 29 |
public $loaded = false; |
| 30 |
|
| 31 |
public function __construct( |
| 32 |
HC3_Hooks $hooks, |
| 33 |
|
| 34 |
HC3_Post $post, |
| 35 |
|
| 36 |
HC3_Email $email, |
| 37 |
HC3_Translate $translate, |
| 38 |
HC3_Settings $settings, |
| 39 |
|
| 40 |
HC3_Users_Query $usersQuery, |
| 41 |
HC3_Auth $auth |
| 42 |
) |
| 43 |
{ |
| 44 |
$this->hooks = $hooks; |
| 45 |
$this->self = $hooks->wrap( $this ); |
| 46 |
$this->email = $email; |
| 47 |
|
| 48 |
$this->settings = $settings; |
| 49 |
$this->translate = $hooks->wrap( $translate ); |
| 50 |
$this->usersQuery = $hooks->wrap( $usersQuery ); |
| 51 |
$this->auth = $hooks->wrap( $auth ); |
| 52 |
} |
| 53 |
|
| 54 |
public function isOn() |
| 55 |
{ |
| 56 |
$this->loadConf(); |
| 57 |
$ret = $this->off ? false : true; |
| 58 |
return $ret; |
| 59 |
} |
| 60 |
|
| 61 |
public function isOff() |
| 62 |
{ |
| 63 |
$this->loadConf(); |
| 64 |
$ret = $this->off ? true : false; |
| 65 |
return $ret; |
| 66 |
} |
| 67 |
|
| 68 |
public function loadConf() |
| 69 |
{ |
| 70 |
if( ! $this->loaded ){ |
| 71 |
$isOff = $this->session()->getUserdata( 'noNotification' ); |
| 72 |
$this->off = $isOff ? true : false; |
| 73 |
$this->loaded = true; |
| 74 |
} |
| 75 |
|
| 76 |
return $this; |
| 77 |
} |
| 78 |
|
| 79 |
public function setOff() |
| 80 |
{ |
| 81 |
if( $this->isOff() ){ |
| 82 |
return $this; |
| 83 |
} |
| 84 |
|
| 85 |
$this->off = true; |
| 86 |
$this->session()->setUserdata('noNotification', 1); |
| 87 |
|
| 88 |
return $this; |
| 89 |
} |
| 90 |
|
| 91 |
public function setOn() |
| 92 |
{ |
| 93 |
if( ! $this->isOff() ){ |
| 94 |
return $this; |
| 95 |
} |
| 96 |
|
| 97 |
$this->off = false; |
| 98 |
$this->session()->setUserdata('noNotification', 0); |
| 99 |
|
| 100 |
return $this; |
| 101 |
} |
| 102 |
|
| 103 |
public function session() |
| 104 |
{ |
| 105 |
return HC3_Session::instance(); |
| 106 |
} |
| 107 |
|
| 108 |
public function queue( HC3_Users_Model $user, $key, $msg ) |
| 109 |
{ |
| 110 |
if( $this->isOff() ){ |
| 111 |
return $this; |
| 112 |
} |
| 113 |
|
| 114 |
$to = $user->getEmail(); |
| 115 |
if( ! $to ){ |
| 116 |
return $this; |
| 117 |
} |
| 118 |
|
| 119 |
if( ! $this->self->isOnForUser($user) ){ |
| 120 |
return $this; |
| 121 |
} |
| 122 |
|
| 123 |
$key = $to . '-' . $key; |
| 124 |
if( ! isset($this->_queue[$key]) ){ |
| 125 |
$this->_queue[$key] = array(); |
| 126 |
} |
| 127 |
|
| 128 |
$this->_queue[$key][] = array( $to, $msg ); |
| 129 |
|
| 130 |
return $this; |
| 131 |
} |
| 132 |
|
| 133 |
public function send() |
| 134 |
{ |
| 135 |
if( $this->off ){ |
| 136 |
return $this; |
| 137 |
} |
| 138 |
|
| 139 |
$log = array(); |
| 140 |
|
| 141 |
$currentUserId = $this->auth->getCurrentUserId(); |
| 142 |
$currentUser = $this->usersQuery->findById( $currentUserId ); |
| 143 |
|
| 144 |
foreach( $this->_queue as $key => $msgs ){ |
| 145 |
// $_start = microtime( true ); |
| 146 |
$finalMsg = array(); |
| 147 |
foreach( $msgs as $m ){ |
| 148 |
list( $to, $msg ) = $m; |
| 149 |
|
| 150 |
$msg = explode("\n", $msg); |
| 151 |
$subj = array_shift( $msg ); |
| 152 |
$msg = join("\n", $msg); |
| 153 |
|
| 154 |
$finalMsg[] = $msg; |
| 155 |
} |
| 156 |
$finalMsg = join( "<br>\n\n", $finalMsg ); |
| 157 |
|
| 158 |
$finalSubj = $subj; |
| 159 |
if( count($this->_queue[$key]) > 1 ){ |
| 160 |
$finalSubj .= ' (' . count($this->_queue[$key]) . ')'; |
| 161 |
} |
| 162 |
|
| 163 |
$finalSubj = $this->translate->translate( $finalSubj ); |
| 164 |
$finalMsg = $this->translate->translate( $finalMsg ); |
| 165 |
|
| 166 |
if( defined('HC3_PROFILER') && HC3_PROFILER ){ |
| 167 |
$log[] = array( get_class($this->email) . '->send()', array($to, $finalSubj, $finalMsg) ); |
| 168 |
} |
| 169 |
else { |
| 170 |
$this->email->send( $to, $finalSubj, $finalMsg ); |
| 171 |
} |
| 172 |
|
| 173 |
// $_duration = microtime( true ) - $_start; |
| 174 |
// $_duration = number_format( $_duration, 4 ) . ' sec'; |
| 175 |
// $_thisMsg = 'email to ' . $to . ' took ' . $_duration; |
| 176 |
// echo $_thisMsg . '<br>'; |
| 177 |
// $_msg[] = $_thisMsg; |
| 178 |
|
| 179 |
if( defined('HC3_PROFILER') && HC3_PROFILER ){ |
| 180 |
$msg = '__Email Sent To__' . ': ' . $to; |
| 181 |
$this->session()->setFlashdata( 'message', $msg, TRUE ); |
| 182 |
// $this->log( $log ); |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
// if( $_msg ){ |
| 187 |
// exit; |
| 188 |
// } |
| 189 |
|
| 190 |
$this->_queue = array(); |
| 191 |
|
| 192 |
if( $log ){ |
| 193 |
$this->log( $log ); |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
public function log( $logs ) |
| 198 |
{ |
| 199 |
$now = time(); |
| 200 |
$date = date( "F j, Y, g:i a", $now ); |
| 201 |
|
| 202 |
$out = array(); |
| 203 |
|
| 204 |
foreach( $logs as $log ){ |
| 205 |
list( $transport, $content ) = $log; |
| 206 |
|
| 207 |
$out[] = $transport; |
| 208 |
$out[] = $date; |
| 209 |
|
| 210 |
if( ! is_array($content) ){ |
| 211 |
$content = array( $content ); |
| 212 |
} |
| 213 |
foreach( $content as $c ){ |
| 214 |
$out[] = $c; |
| 215 |
} |
| 216 |
|
| 217 |
$out[] = ""; |
| 218 |
} |
| 219 |
|
| 220 |
$out = join("\n", $out); |
| 221 |
|
| 222 |
if( ! defined('HC3_ROOT_PATH') ){ |
| 223 |
return; |
| 224 |
} |
| 225 |
|
| 226 |
$outFile = HC3_ROOT_PATH . '/emaillog.txt'; |
| 227 |
$fp = fopen( $outFile, 'a' ); |
| 228 |
fwrite( $fp, $out . "\n\n" ); |
| 229 |
fclose( $fp ); |
| 230 |
} |
| 231 |
|
| 232 |
public function setOnForUser( HC3_Users_Model $user ) |
| 233 |
{ |
| 234 |
$key = $this->_settingsKeyForUser( $user, 'on' ); |
| 235 |
|
| 236 |
$this->settings->set( $key, 1 ); |
| 237 |
return $this; |
| 238 |
} |
| 239 |
|
| 240 |
public function setOffForUser( HC3_Users_Model $user ) |
| 241 |
{ |
| 242 |
$key = $this->_settingsKeyForUser( $user, 'on' ); |
| 243 |
|
| 244 |
$this->settings->set( $key, 0 ); |
| 245 |
return $this; |
| 246 |
} |
| 247 |
|
| 248 |
public function isOnForUser( HC3_Users_Model $user ) |
| 249 |
{ |
| 250 |
$key = $this->_settingsKeyForUser( $user, 'on' ); |
| 251 |
|
| 252 |
$return = $this->settings->get( $key ); |
| 253 |
if( NULL === $return ){ |
| 254 |
$return = TRUE; |
| 255 |
} |
| 256 |
|
| 257 |
return $return; |
| 258 |
} |
| 259 |
|
| 260 |
protected function _settingsKeyForUser( HC3_Users_Model $user, $pname ) |
| 261 |
{ |
| 262 |
$userId = $user->getId(); |
| 263 |
|
| 264 |
$return = array(); |
| 265 |
$return[] = 'notificator'; |
| 266 |
$return[] = 'user'; |
| 267 |
$return[] = $userId; |
| 268 |
|
| 269 |
$return[] = $pname; |
| 270 |
|
| 271 |
$return = join( '_', $return ); |
| 272 |
|
| 273 |
return $return; |
| 274 |
} |
| 275 |
} |