| 1 |
<?php if (! defined('ABSPATH')) exit; // Exit if accessed directly |
| 2 |
interface SH4_Notifications_IService |
| 3 |
{ |
| 4 |
public function register( $listenOn, $notificationId, $class, $defaultOn = TRUE ); |
| 5 |
|
| 6 |
public function findById( $notificationId ); |
| 7 |
public function findAll(); |
| 8 |
|
| 9 |
public function setOn( SH4_Calendars_Model $calendar, $notificationId ); |
| 10 |
public function setOff( SH4_Calendars_Model $calendar, $notificationId ); |
| 11 |
public function isOn( SH4_Calendars_Model $calendar, $notificationId ); |
| 12 |
|
| 13 |
public function getTemplate( SH4_Calendars_Model $calendar, $notificationId ); |
| 14 |
public function setTemplate( SH4_Calendars_Model $calendar, $notificationId, $template ); |
| 15 |
|
| 16 |
public function setOnForUser( HC3_Users_Model $user, SH4_Calendars_Model $calendar = NULL, $notificationId = NULL ); |
| 17 |
public function setOffForUser( HC3_Users_Model $user, SH4_Calendars_Model $calendar = NULL, $notificationId = NULL ); |
| 18 |
public function isOnForUser( HC3_Users_Model $user, SH4_Calendars_Model $calendar = NULL, $notificationId = NULL ); |
| 19 |
} |
| 20 |
|
| 21 |
class SH4_Notifications_Service implements SH4_Notifications_IService |
| 22 |
{ |
| 23 |
protected $notifications = array(); |
| 24 |
protected $calendars = array(); |
| 25 |
protected $listen = array(); |
| 26 |
|
| 27 |
public $self, $settings, $dic, $hooks; |
| 28 |
|
| 29 |
public function __construct( |
| 30 |
HC3_Hooks $hooks, |
| 31 |
HC3_Dic $dic, |
| 32 |
HC3_Settings $settings, |
| 33 |
|
| 34 |
SH4_Calendars_Query $calendarsQuery |
| 35 |
) |
| 36 |
{ |
| 37 |
$this->settings = $settings; |
| 38 |
$this->dic = $dic; |
| 39 |
|
| 40 |
$calendarsQuery = $hooks->wrap( $calendarsQuery ); |
| 41 |
$this->calendars = $calendarsQuery->findActive(); |
| 42 |
|
| 43 |
$this->self = $hooks->wrap( $this ); |
| 44 |
$this->hooks = $hooks; |
| 45 |
} |
| 46 |
|
| 47 |
public function register( $listenOn, $notificationId, $class, $defaultOn = TRUE ) |
| 48 |
{ |
| 49 |
$this->notifications[ $notificationId ] = $class; |
| 50 |
|
| 51 |
reset( $this->calendars ); |
| 52 |
foreach( $this->calendars as $calendar ){ |
| 53 |
$key = $this->_settingsKey( $calendar, $notificationId, 'on' ); |
| 54 |
$value = $defaultOn ? 1 : 0; |
| 55 |
$this->settings->init( $key, $value ); |
| 56 |
} |
| 57 |
|
| 58 |
$self = $this; |
| 59 |
|
| 60 |
if( ! isset($this->listen[$listenOn]) ){ |
| 61 |
$this->listen[$listenOn] = array(); |
| 62 |
|
| 63 |
$func = function($args) use ($listenOn, $self){ |
| 64 |
return $self->listen( $listenOn, $args ); |
| 65 |
}; |
| 66 |
|
| 67 |
$this->hooks |
| 68 |
->add( $listenOn, $func ) |
| 69 |
; |
| 70 |
} |
| 71 |
$this->listen[$listenOn][] = $notificationId; |
| 72 |
|
| 73 |
return $this; |
| 74 |
} |
| 75 |
|
| 76 |
public function listen( $listenOn, $args ) |
| 77 |
{ |
| 78 |
if( ! isset($this->listen[$listenOn]) ){ |
| 79 |
return; |
| 80 |
} |
| 81 |
|
| 82 |
$shift = $args[0]; |
| 83 |
$calendar = $shift->getCalendar(); |
| 84 |
|
| 85 |
foreach( $this->listen[$listenOn] as $notificationId ){ |
| 86 |
if( ! $this->isOn($calendar, $notificationId) ){ |
| 87 |
continue; |
| 88 |
} |
| 89 |
|
| 90 |
$template = $this->getTemplate( $calendar, $notificationId ); |
| 91 |
$notification = $this->findById( $notificationId ); |
| 92 |
|
| 93 |
$notification |
| 94 |
->execute( $shift, $template ) |
| 95 |
; |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
public function findAll() |
| 100 |
{ |
| 101 |
$ids = array_keys( $this->notifications ); |
| 102 |
foreach( $ids as $id ){ |
| 103 |
$return[ $id ] = $this->self->findById( $id ); |
| 104 |
} |
| 105 |
return $return; |
| 106 |
} |
| 107 |
|
| 108 |
public function findById( $notificationId ) |
| 109 |
{ |
| 110 |
$return = NULL; |
| 111 |
if( array_key_exists($notificationId, $this->notifications) ){ |
| 112 |
$returnClass = $this->notifications[ $notificationId ]; |
| 113 |
$return = $this->dic->make( $returnClass ); |
| 114 |
} |
| 115 |
|
| 116 |
return $return; |
| 117 |
} |
| 118 |
|
| 119 |
public function setOn( SH4_Calendars_Model $calendar, $notificationId ) |
| 120 |
{ |
| 121 |
$key = $this->_settingsKey( $calendar, $notificationId, 'on' ); |
| 122 |
$this->settings->set( $key, 1 ); |
| 123 |
return $this; |
| 124 |
} |
| 125 |
|
| 126 |
public function setOff( SH4_Calendars_Model $calendar, $notificationId ) |
| 127 |
{ |
| 128 |
$key = $this->_settingsKey( $calendar, $notificationId, 'on' ); |
| 129 |
$this->settings->set( $key, 0 ); |
| 130 |
return $this; |
| 131 |
} |
| 132 |
|
| 133 |
public function isOn( SH4_Calendars_Model $calendar, $notificationId ) |
| 134 |
{ |
| 135 |
$key = $this->_settingsKey( $calendar, $notificationId, 'on' ); |
| 136 |
$return = $this->settings->get( $key ); |
| 137 |
return $return; |
| 138 |
} |
| 139 |
|
| 140 |
public function setOnForUser( HC3_Users_Model $user, SH4_Calendars_Model $calendar = NULL, $notificationId = NULL ) |
| 141 |
{ |
| 142 |
$key = $this->_settingsKeyForUser( $user, 'on', $calendar, $notificationId ); |
| 143 |
|
| 144 |
$this->settings->set( $key, 1 ); |
| 145 |
return $this; |
| 146 |
} |
| 147 |
|
| 148 |
public function setOffForUser( HC3_Users_Model $user, SH4_Calendars_Model $calendar = NULL, $notificationId = NULL ) |
| 149 |
{ |
| 150 |
$key = $this->_settingsKeyForUser( $user, 'on', $calendar, $notificationId ); |
| 151 |
|
| 152 |
$this->settings->set( $key, 0 ); |
| 153 |
return $this; |
| 154 |
} |
| 155 |
|
| 156 |
public function isOnForUser( HC3_Users_Model $user, SH4_Calendars_Model $calendar = NULL, $notificationId = NULL ) |
| 157 |
{ |
| 158 |
$key = $this->_settingsKeyForUser( $user, 'on', $calendar, $notificationId ); |
| 159 |
|
| 160 |
$ret = $this->settings->get( $key ); |
| 161 |
if( null === $ret ){ |
| 162 |
$ret = true; |
| 163 |
} |
| 164 |
|
| 165 |
return $ret; |
| 166 |
} |
| 167 |
|
| 168 |
public function getTemplate( SH4_Calendars_Model $calendar, $notificationId ) |
| 169 |
{ |
| 170 |
$key = $this->_settingsKey( $calendar, $notificationId, 'template' ); |
| 171 |
$ret = $this->settings->get( $key ); |
| 172 |
|
| 173 |
if( ! $ret ){ |
| 174 |
$ret = ''; |
| 175 |
} |
| 176 |
|
| 177 |
if( ! strlen($ret) ){ |
| 178 |
$notification = $this->self->findById( $notificationId ); |
| 179 |
$ret = $notification->getDefaultTemplate(); |
| 180 |
} |
| 181 |
|
| 182 |
return $ret; |
| 183 |
} |
| 184 |
|
| 185 |
public function setTemplate( SH4_Calendars_Model $calendar, $notificationId, $template ) |
| 186 |
{ |
| 187 |
$key = $this->_settingsKey( $calendar, $notificationId, 'template' ); |
| 188 |
$this->settings->set( $key, $template ); |
| 189 |
return $this; |
| 190 |
} |
| 191 |
|
| 192 |
protected function _settingsKey( SH4_Calendars_Model $calendar, $notificationId, $pname ) |
| 193 |
{ |
| 194 |
$calendarId = $calendar->getId(); |
| 195 |
|
| 196 |
$return = array(); |
| 197 |
$return[] = 'notification'; |
| 198 |
$return[] = $notificationId; |
| 199 |
$return[] = $pname; |
| 200 |
$return[] = $calendarId; |
| 201 |
|
| 202 |
$return = join( '_', $return ); |
| 203 |
|
| 204 |
return $return; |
| 205 |
} |
| 206 |
|
| 207 |
protected function _settingsKeyForUser( HC3_Users_Model $user, $pname, SH4_Calendars_Model $calendar = NULL, $notificationId = NULL ) |
| 208 |
{ |
| 209 |
$calendarId = $calendar ? $calendar->getId() : 'x'; |
| 210 |
$notificationId = $notificationId ? $notificationId : 'x'; |
| 211 |
$userId = $user->getId(); |
| 212 |
|
| 213 |
$return = array(); |
| 214 |
$return[] = 'user'; |
| 215 |
$return[] = $userId; |
| 216 |
|
| 217 |
$return[] = 'notification'; |
| 218 |
$return[] = $notificationId; |
| 219 |
$return[] = $pname; |
| 220 |
$return[] = $calendarId; |
| 221 |
|
| 222 |
$return = join( '_', $return ); |
| 223 |
|
| 224 |
return $return; |
| 225 |
} |
| 226 |
} |