PluginProbe
ShiftController Employee Shift Scheduling / 4.9.26
ShiftController Employee Shift Scheduling v4.9.26
4.9.97 4.9.96 4.9.95 4.9.74 4.9.75 4.9.76 4.9.77 4.9.78 4.9.84 4.9.85 4.9.87 4.9.91 4.9.92 trunk 2.1.0 2.1.1 2.1.2 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 3.2.4 All 38 releases
shiftcontroller / sh4 / notifications / service.php

service.php in ShiftController Employee Shift Scheduling 4.9.26, at sh4/notifications/service.php

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