More use of EL, fix for admin links
[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
172         // Do we have an entry?
173         if (SQL_NUMROWS($result) == 1) {
174                 // Merge arrays
175                 $content = merge_array($content, SQL_FETCHARRAY($result));
176
177                 if (($content['holiday_active'] == 'Y') && (($content['holiday_activated'] + getConfig('holiday_lock')) < time())) {
178                         // Load data
179                         $result2 = SQL_QUERY_ESC("SELECT
180         `holiday_start`, `holiday_end`
181 FROM
182         `{?_MYSQL_PREFIX?}_user_holidays`
183 WHERE
184         `userid`=%s
185 LIMIT 1", array(getMemberId()), __FILE__, __LINE__);
186                         if (SQL_NUMROWS($result2) == 1) {
187                                 // Data was found so merge it
188                                 $content = merge_array($content, SQL_FETCHARRAY($result2));
189
190                                 // Prepare it for the template
191                                 $content['start'] = generateDateTime($content['holiday_start'], 3);
192                                 $content['end']   = generateDateTime($content['holiday_end']  , 3);
193
194                                 // Deactivate it now
195                                 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_data`
196 SET
197         `holiday_active`='N',
198         `holiday_activated`=0
199 WHERE
200         `userid`=%s
201 LIMIT 1",
202                                         array(getMemberId()), __FILE__, __LINE__);
203
204                                 // Remove entry
205                                 SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM
206         `{?_MYSQL_PREFIX?}_user_holidays`
207 WHERE
208         `userid`=%s
209 LIMIT 1",
210                                         array(getMemberId()), __FILE__, __LINE__);
211
212                                 // Send email to admin
213                                 sendAdminNotification(getMessage('HOLIDAY_ADMIN_DEAC_SUBJ'), 'admin_holiday_deactivated', $content, getMemberId());
214
215                                 // Display message to user
216                                 loadTemplate('admin_settings_saved', false, getMessage('HOLIDAY_MEMBER_DEACTIVATED_NOW'));
217                         } else {
218                                 // Display message to user
219                                 loadTemplate('admin_settings_saved', false, getMessage('HOLIDAY_MEMBER_CANNOT_DEACTIVATE'));
220                         }
221
222                         // Free result
223                         SQL_FREERESULT($result2);
224                 } elseif ($content['holiday_active'] == 'Y') {
225                         // To fast!
226                         loadTemplate('admin_settings_saved', false, getMessage('HOLIDAY_MEMBER_LOCKED'));
227                 }
228         } else {
229                 // User not found
230                 loadTemplate('admin_settings_saved', false, getMaskedMessage('HOLIDAY_MEMBER_NOT_STOPPED_404', getMemberId()));
231         }
232
233         // Free result
234         SQL_FREERESULT($result);
235 } // END - if
236
237 // If something is wrong or link in menu is just clicked display form
238 if ((!isFormSent()) && (!isPostRequestParameterSet('stop'))) {
239         // Check if user is in holiday...
240         $result = SQL_QUERY_ESC("SELECT
241         `holiday_active`, `holiday_activated`
242 FROM
243         `{?_MYSQL_PREFIX?}_user_data`
244 WHERE
245         `userid`=%s
246 LIMIT 1", array(getMemberId()), __FILE__, __LINE__);
247         $content = SQL_FETCHARRAY($result);
248         SQL_FREERESULT($result);
249
250         // Check for lock
251         if (($content['holiday_activated'] + getConfig('holiday_lock')) < time()) {
252                 // User can deactivate his holiday request
253                 switch ($content['holiday_active'])
254                 {
255                         case 'Y': // Display deactivation form
256                                 // Load starting and ending date
257                                 $result = SQL_QUERY_ESC("SELECT
258         `holiday_start`, `holiday_end`
259 FROM
260         `{?_MYSQL_PREFIX?}_user_holidays`
261 WHERE
262         `userid`=%s
263 LIMIT 1", array(getMemberId()), __FILE__, __LINE__);
264                                 if (SQL_NUMROWS($result) == 1) {
265                                         // Data was found so merge it
266                                         $content = merge_array($content, SQL_FETCHARRAY($result));
267
268                                         // Prepare it for the template
269                                         $content['start'] = generateDateTime($content['holiday_start'] , 3);
270                                         $content['end']   = generateDateTime($content['holiday_end']   , 3);
271                                         $content['lock']  = generateDateTime($content['holiday_activated'], 1);
272
273                                         // Load template
274                                         loadTemplate('member_holiday_deactivate', false, $content);
275                                 } else {
276                                         // Free memory
277                                         SQL_FREERESULT($result);
278
279                                         // Remove entry and reload URL
280                                         SQL_QUERY_ESC("UPDATE
281         `{?_MYSQL_PREFIX?}_user_data`
282 SET
283         `holiday_active`='N'
284 WHERE
285         `userid`=%s
286 LIMIT 1", array(getMemberId()), __FILE__, __LINE__);
287                                         redirectToUrl('modules.php?module=login&amp;what=holiday');
288                                         return;
289                                 }
290
291                                 // Free result
292                                 SQL_FREERESULT($result);
293                                 break;
294
295                         case 'N': // Display activation form
296                                 // Starting day
297                                 $content['start_day']   = addSelectionBox('day'  , date('d', (time() + getConfig('ONE_DAY'))), 'start');
298                                 $content['start_month'] = addSelectionBox('month', date('m', (time() + getConfig('ONE_DAY'))), 'start');
299                                 $content['start_year']  = addSelectionBox('year' , date('Y', (time() + getConfig('ONE_DAY'))), 'start');
300
301                                 // Calcualte ending date
302                                 $D = date('d', time() + getConfig('ONE_DAY') + (getConfig('ONE_DAY') * getConfig('holiday_max')));
303                                 $M = date('m', time() + getConfig('ONE_DAY') + (getConfig('ONE_DAY') * getConfig('holiday_max')));
304                                 $Y = date('Y', time() + getConfig('ONE_DAY') + (getConfig('ONE_DAY') * getConfig('holiday_max')));
305
306                                 // Ending day
307                                 $content['end_day']   = addSelectionBox('day'  , $D, 'end');
308                                 $content['end_month'] = addSelectionBox('month', $M, 'end');
309                                 $content['end_year']  = addSelectionBox('year' , $Y, 'end');
310
311                                 // Output form
312                                 loadTemplate('member_holiday_form', false, $content);
313                                 break;
314                 }
315         } else {
316                 // To fast!
317                 loadTemplate('admin_settings_saved', false, getMessage('HOLIDAY_MEMBER_LOCKED'));
318         }
319 } // END - if
320
321 // [EOF]
322 ?>