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