b233e861a4523a6cb18a55599600a0d3b7cd907d
[mailer.git] / inc / modules / member / what-holiday.php
1 <?php
2 /************************************************************************
3  * MXChange v0.2.1                                    Start: 07/24/2004 *
4  * ================                             Last change: 07/31/2004 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : what-holiday.php                                 *
8  * -------------------------------------------------------------------- *
9  * Short description : Holiday requests                                 *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Urlaubsschaltungen                               *
12  * -------------------------------------------------------------------- *
13  * $Revision::                                                        $ *
14  * $Date::                                                            $ *
15  * $Tag:: 0.2.1-FINAL                                                 $ *
16  * $Author::                                                          $ *
17  * Needs to be in all Files and every File needs "svn propset           *
18  * svn:keywords Date Revision" (autoprobset!) at least!!!!!!            *
19  * -------------------------------------------------------------------- *
20  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
21  * For more information visit: http://www.mxchange.org                  *
22  *                                                                      *
23  * This program is free software; you can redistribute it and/or modify *
24  * it under the terms of the GNU General Public License as published by *
25  * the Free Software Foundation; either version 2 of the License, or    *
26  * (at your option) any later version.                                  *
27  *                                                                      *
28  * This program is distributed in the hope that it will be useful,      *
29  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
30  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
31  * GNU General Public License for more details.                         *
32  *                                                                      *
33  * You should have received a copy of the GNU General Public License    *
34  * along with this program; if not, write to the Free Software          *
35  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
36  * MA  02110-1301  USA                                                  *
37  ************************************************************************/
38
39 // Some security stuff...
40 if (!defined('__SECURITY')) {
41         die();
42 } elseif (!isMember()) {
43         redirectToIndexMemberOnlyModule();
44 }
45
46 // Add description as navigation point
47 addMenuDescription('member', __FILE__);
48
49 if ((!isExtensionActive('holiday')) && (!isAdmin())) {
50         loadTemplate('admin_settings_saved', false, generateExtensionInactiveNotInstalledMessage('holiday'));
51         return;
52 } // END - if
53
54 // Init content array
55 $content = array();
56
57 // Check for running mail orders in pool
58 $result1 = SQL_QUERY_ESC("SELECT
59         `timestamp`
60 FROM
61         `{?_MYSQL_PREFIX?}_pool`
62 WHERE
63         `sender`=%s
64 ORDER BY
65         `timestamp` DESC
66 LIMIT 1", array(getUserId()), __FILE__, __LINE__);
67
68 // Check for sent mail orders in stats
69 $result2 = SQL_QUERY_ESC("SELECT
70         `timestamp_ordered`
71 FROM
72         `{?_MYSQL_PREFIX?}_user_stats`
73 WHERE
74         `userid`=%s
75 ORDER BY
76         `timestamp_ordered` DESC
77 LIMIT 1", array(getUserId()), __FILE__, __LINE__);
78
79 if ((SQL_NUMROWS($result1) == 1) || (SQL_NUMROWS($result2) == 1)) {
80         // Mail order found!
81         $content = merge_array($content, SQL_FETCHARRAY($result1));
82         $content = merge_array($content, SQL_FETCHARRAY($result2));
83
84         // Fix missing entries
85         if (empty($content['timestamp'])) $content['timestamp'] = 0;
86         if (empty($content['timestamp_ordered'])) $content['timestamp_ordered'] = 0;
87
88         if ((($content['timestamp'] + getConfig('holiday_lock')) > time()) || (($content['timestamp_ordered'] + getConfig('holiday_lock')) > time())) {
89                 // Mail order is to close away!
90                 unsetPostRequestElement('ok');
91                 unsetPostRequestElement('stop');
92
93                 if (($content['timestamp'] + getConfig('holiday_lock')) > time()) {
94                         // Mail found in pool
95                         $stamp = $content['timestamp'];
96                 } else {
97                         // Mail found in stats
98                         $stamp = $content['timestamp_ordered'];
99                 }
100
101                 // Display message and exit here
102                 loadTemplate('admin_settings_saved', false, sprintf(getMessage('HOLIDAY_MEMBER_ORDER'), generateDateTime($stamp, '1')));
103                 return;
104         }
105 } // END - if
106
107 // Free memory
108 SQL_FREERESULT($result1);
109 SQL_FREERESULT($result2);
110
111 if (isFormSent()) {
112         // Check holiday request...
113         $START = mktime(0, 0, 0, postRequestElement('start_month'), postRequestElement('start_day'), postRequestElement('start_year'));
114         $content['holiday_end']   = mktime(0, 0, 0, postRequestElement('end_month')  , postRequestElement('end_day')  , postRequestElement('end_year')  );
115
116         // Test both values
117         $TEST = $content['holiday_end'] - $START;
118         if (($TEST < 0) || ($TEST > (getConfig('ONE_DAY') * getConfig('holiday_max'))) || ($START < time()) || ($content['holiday_end'] < time())) {
119                 // Time test failed
120                 unsetPostRequestElement('ok');
121         } else {
122                 // Everything went okay so let's store his request and send mails
123                 SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_user_holidays` (`userid`, `holiday_start`, `holiday_end`, `comments`) VALUES ('%s','%s','%s','%s')",
124                         array(getUserId(), $START, $content['holiday_end'], postRequestElement('comments')), __FILE__, __LINE__);
125
126                 // Activate holiday system
127                 SQL_QUERY_ESC("UPDATE
128         `{?_MYSQL_PREFIX?}_user_data`
129 SET
130         `holiday_active`='N', `holiday_activated`=UNIX_TIMESTAMP()
131 WHERE
132         `userid`=%s
133 LIMIT 1",
134                 array(getUserId()), __FILE__, __LINE__);
135
136                 // Prepare constants
137                 $content['start_day']   = bigintval(postRequestElement('start_day'));
138                 $content['start_month'] = $GLOBALS['month_descr'][postRequestElement('start_month')];
139                 $content['start_year']  = bigintval(postRequestElement('start_year'));
140                 $content['end_day']     = bigintval(postRequestElement('end_day'));
141                 $content['end_month']   = $GLOBALS['month_descr'][postRequestElement('end_month')];
142                 $content['end_year']    = bigintval(postRequestElement('end_year'));
143                 $content['comments']    = secureString(postRequestElement('comments'));
144
145                 // Send mail to member
146                 $message = loadEmailTemplate('member_holiday_request', $content, getUserId());
147                 sendEmail(getUserId(), getMessage('HOLIDAY_MEMBER_SUBJECT'), $message);
148
149                 // Send mail to all admins
150                 sendAdminNotification(getMessage('HOLIDAY_ADMIN_SUBJECT'), 'admin_holiday_request', $content, getUserId());
151
152                 // Create task
153                 createNewTask('{--HOLIDAY_ADMIN_SUBJECT--}', $message, 'HOLIDAY_REQUEST', getUserId());
154
155                 // Display message
156                 loadTemplate('admin_settings_saved', false, getMessage('HOLIDAY_IS_ACTIVATED_NOW'));
157         }
158 } // END - if
159
160 // Holiday shall be ended now
161 if (isPostRequestElementSet('stop')) {
162         // Okay, end the holiday here...
163         $result = SQL_QUERY_ESC("SELECT
164         `holiday_active`, `holiday_activated`
165 FROM
166         `{?_MYSQL_PREFIX?}_user_data`
167 WHERE
168         `userid`=%s
169 LIMIT 1", array(getUserId()), __FILE__, __LINE__);
170         $content = merge_array($content, SQL_FETCHARRAY($result));
171         SQL_FREERESULT($result);
172
173         if (($content['holiday_active'] == 'Y') && (($content['holiday_activated'] + getConfig('holiday_lock')) < time())) {
174                 // Load data
175                 $result = SQL_QUERY_ESC("SELECT
176         `holiday_start`, `holiday_end`
177 FROM
178         `{?_MYSQL_PREFIX?}_user_holidays`
179 WHERE
180         `userid`=%s
181 LIMIT 1", array(getUserId()), __FILE__, __LINE__);
182                 if (SQL_NUMROWS($result) == 1) {
183                         // Data was found
184                         $content = merge_array($content, SQL_FETCHARRAY($result));
185                         SQL_FREERESULT($result);
186
187                         // Prepare it for the template
188                         $content['start'] = generateDateTime($content['holiday_start'], '3');
189                         $content['end']   = generateDateTime($content['holiday_end']  , '3');
190
191                         // Deactivate it now
192                         SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_data`
193 SET
194         `holiday_active`='N',
195         `holiday_activated`=0
196 WHERE
197         `userid`=%s
198 LIMIT 1",
199                                 array(getUserId()), __FILE__, __LINE__);
200
201                         // Remove entry
202                         SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM
203         `{?_MYSQL_PREFIX?}_user_holidays`
204 WHERE
205         `userid`=%s
206 LIMIT 1",
207                                 array(getUserId()), __FILE__, __LINE__);
208
209                         // Send email to admin
210                         sendAdminNotification(getMessage('HOLIDAY_ADMIN_DEAC_SUBJ'), 'admin_holiday_deactivated', $content, getUserId());
211
212                         // Display message to user
213                         loadTemplate('admin_settings_saved', false, getMessage('HOLIDAY_MEMBER_DEACTIVATED_NOW'));
214                 } else {
215                         // Display message to user
216                         loadTemplate('admin_settings_saved', false, getMessage('HOLIDAY_MEMBER_CANNOT_DEACTIVATE'));
217                 }
218         } elseif ($content['holiday_active'] == 'Y') {
219                 // To fast!
220                 loadTemplate('admin_settings_saved', false, getMessage('HOLIDAY_MEMBER_LOCKED'));
221         }
222 } // END - if
223
224 // If something is wrong or link in menu is just clicked display form
225 if ((!isFormSent()) && (!isPostRequestElementSet('stop'))) {
226         // Check if user is in holiday...
227         $result = SQL_QUERY_ESC("SELECT
228         `holiday_active`, `holiday_activated`
229 FROM
230         `{?_MYSQL_PREFIX?}_user_data`
231 WHERE
232         `userid`=%s
233 LIMIT 1", array(getUserId()), __FILE__, __LINE__);
234         $content = SQL_FETCHARRAY($result);
235         SQL_FREERESULT($result);
236
237         // Check for lock
238         if (($content['holiday_activated'] + getConfig('holiday_lock')) < time()) {
239                 // User can deactivate his holiday request
240                 switch ($content['holiday_active'])
241                 {
242                         case 'Y': // Display deactivation form
243                                 // Load starting and ending date
244                                 $result = SQL_QUERY_ESC("SELECT
245         `holiday_start`, `holiday_end`
246 FROM
247         `{?_MYSQL_PREFIX?}_user_holidays`
248 WHERE
249         `userid`=%s
250 LIMIT 1", array(getUserId()), __FILE__, __LINE__);
251                                 if (SQL_NUMROWS($result) == 1) {
252                                         // Data was found
253                                         $content = merge_array($content, SQL_FETCHARRAY($result));
254                                         SQL_FREERESULT($result);
255
256                                         // Prepare it for the template
257                                         $content['start'] = generateDateTime($content['holiday_start'] , '3');
258                                         $content['end']   = generateDateTime($content['holiday_end']   , '3');
259                                         $content['lock']  = generateDateTime($content['holiday_activated'], '1');
260
261                                         // Load template
262                                         loadTemplate('member_holiday_deactivate', false, $content);
263                                 } else {
264                                         // Free memory
265                                         SQL_FREERESULT($result);
266
267                                         // Remove entry and reload URL
268                                         SQL_QUERY_ESC("UPDATE
269         `{?_MYSQL_PREFIX?}_user_data`
270 SET
271         `holiday_active`='N'
272 WHERE
273         `userid`=%s
274 LIMIT 1", array(getUserId()), __FILE__, __LINE__);
275                                         redirectToUrl('modules.php?module=login&amp;what=holiday');
276                                         return;
277                                 }
278                                 break;
279
280                         case 'N': // Display activation form
281                                 // Starting day
282                                 $content['start_day']   = addSelectionBox('day'  , date('d', (time() + getConfig('ONE_DAY'))), 'start');
283                                 $content['start_month'] = addSelectionBox('month', date('m', (time() + getConfig('ONE_DAY'))), 'start');
284                                 $content['start_year']  = addSelectionBox('year' , date('Y', (time() + getConfig('ONE_DAY'))), 'start');
285
286                                 // Calcualte ending date
287                                 $D = date('d', time() + getConfig('ONE_DAY') + (getConfig('ONE_DAY') * getConfig('holiday_max')));
288                                 $M = date('m', time() + getConfig('ONE_DAY') + (getConfig('ONE_DAY') * getConfig('holiday_max')));
289                                 $Y = date('Y', time() + getConfig('ONE_DAY') + (getConfig('ONE_DAY') * getConfig('holiday_max')));
290
291                                 // Ending day
292                                 $content['end_day']   = addSelectionBox('day'  , $D, 'end');
293                                 $content['end_month'] = addSelectionBox('month', $M, 'end');
294                                 $content['end_year']  = addSelectionBox('year' , $Y, 'end');
295
296                                 // Output form
297                                 loadTemplate('member_holiday_form');
298                                 break;
299                 }
300         } else {
301                 // To fast!
302                 loadTemplate('admin_settings_saved', false, getMessage('HOLIDAY_MEMBER_LOCKED'));
303         }
304 } // END - if
305
306 // [EOF]
307 ?>