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