Ref link fixed, nickname fixed, several rewrites, TODOs.txt updated:
[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 - 2008 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         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), '/inc') + 4) . '/security.php';
42         require($INC);
43 } elseif (!IS_MEMBER()) {
44         redirectToUrl('modules.php?module=index');
45 } elseif ((!EXT_IS_ACTIVE('holiday')) && (!IS_ADMIN())) {
46         addFatalMessage(__FILE__, __LINE__, generateExtensionInactiveNotInstalledMessage('holiday'));
47         return;
48 }
49
50 // Init content array
51 $content = array();
52
53 // Add description as navigation point
54 ADD_DESCR('member', __FILE__);
55
56 // Check for running mail orders in pool
57 $result1 = SQL_QUERY_ESC("SELECT
58         `timestamp`
59 FROM
60         `{!_MYSQL_PREFIX!}_pool`
61 WHERE
62         `sender`=%s
63 ORDER BY
64         `timestamp` DESC
65 LIMIT 1", array(getUserId()), __FILE__, __LINE__);
66
67 // Check for sent mail orders in stats
68 $result2 = SQL_QUERY_ESC("SELECT
69         `timestamp_ordered`
70 FROM
71         `{!_MYSQL_PREFIX!}_user_stats`
72 WHERE
73         `userid`=%s
74 ORDER BY
75         `timestamp_ordered` DESC
76 LIMIT 1", array(getUserId()), __FILE__, __LINE__);
77
78 if ((SQL_NUMROWS($result1) == 1) || (SQL_NUMROWS($result2) == 1)) {
79         // Mail order found!
80         $content = merge_array($content, SQL_FETCHARRAY($result1));
81         $content = merge_array($content, SQL_FETCHARRAY($result2));
82
83         // Fix missing entries
84         if (empty($content['timestamp'])) $content['timestamp'] = 0;
85         if (empty($content['timestamp_ordered'])) $content['timestamp_ordered'] = 0;
86
87         if ((($content['timestamp'] + getConfig('holiday_lock')) > time()) || (($content['timestamp_ordered'] + getConfig('holiday_lock')) > time())) {
88                 // Mail order is to close away!
89                 REQUEST_UNSET_POST('ok');
90                 REQUEST_UNSET_POST('stop');
91
92                 if (($content['timestamp'] + getConfig('holiday_lock')) > time()) {
93                         // Mail found in pool
94                         $stamp = $content['timestamp'];
95                 } else {
96                         // Mail found in stats
97                         $stamp = $content['timestamp_ordered'];
98                 }
99
100                 // Display message and exit here
101                 LOAD_TEMPLATE('admin_settings_saved', false, sprintf(getMessage('HOLIDAY_MEMBER_ORDER'), generateDateTime($stamp, '1')));
102                 return;
103         }
104 } // END - if
105
106 // Free memory
107 SQL_FREERESULT($result1);
108 SQL_FREERESULT($result2);
109
110 if (isFormSent()) {
111         // Check holiday request...
112         $START = mktime(0, 0, 0, REQUEST_POST('start_month'), REQUEST_POST('start_day'), REQUEST_POST('start_year'));
113         $content['holiday_end']   = mktime(0, 0, 0, REQUEST_POST('end_month')  , REQUEST_POST('end_day')  , REQUEST_POST('end_year')  );
114
115         // Test both values
116         $TEST = $content['holiday_end'] - $START;
117         if (($TEST < 0) || ($TEST > (getConfig('one_day') * getConfig('holiday_max'))) || ($START < time()) || ($content['holiday_end'] < time())) {
118                 // Time test failed
119                 REQUEST_UNSET_POST('ok');
120         } else {
121                 // Everything went okay so let's store his request and send mails
122                 SQL_QUERY_ESC("INSERT INTO `{!_MYSQL_PREFIX!}_user_holidays` (`userid`, `holiday_start`, `holiday_end`, `comments`) VALUES ('%s','%s','%s','%s')",
123                         array(getUserId(), $START, $content['holiday_end'], REQUEST_POST('comments')), __FILE__, __LINE__);
124
125                 // Activate holiday system
126                 SQL_QUERY_ESC("UPDATE
127         `{!_MYSQL_PREFIX!}_user_data`
128 SET
129         `holiday_active`='N', `holiday_activated`=UNIX_TIMESTAMP()
130 WHERE
131         `userid`=%s
132 LIMIT 1",
133                 array(getUserId()), __FILE__, __LINE__);
134
135                 // Prepare constants
136                 define('_START_DAY'  , REQUEST_POST('start_day'));
137                 define('_START_MONTH', $GLOBALS['month_descr'][REQUEST_POST('start_month')]);
138                 define('_START_YEAR' , REQUEST_POST('start_year'));
139                 define('_END_DAY'    , REQUEST_POST('end_day'));
140                 define('_END_MONTH'  , $GLOBALS['month_descr'][REQUEST_POST('end_month')]);
141                 define('_END_YEAR'   , REQUEST_POST('end_year'));
142
143                 // Send mail to member
144                 $message = LOAD_EMAIL_TEMPLATE('member_holiday_request', REQUEST_POST('comments'), getUserId());
145                 sendEmail(getUserId(), getMessage('HOLIDAY_MEMBER_SUBJECT'), $message);
146
147                 // Send mail to all admins
148                 sendAdminNotification(getMessage('HOLIDAY_ADMIN_SUBJECT'), 'admin_holiday_request', REQUEST_POST('comments'), getUserId());
149
150                 // Create task
151                 createNewTask('{--HOLIDAY_ADMIN_SUBJECT--}', $message, 'HOLIDAY_REQUEST', getUserId());
152
153                 // Display message
154                 LOAD_TEMPLATE('admin_settings_saved', false, getMessage('HOLIDAY_IS_ACTIVATED_NOW'));
155         }
156 } // END - if
157
158 // Holiday shall be ended now
159 if (REQUEST_ISSET_POST('stop')) {
160         // Okay, end the holiday here...
161         $result = SQL_QUERY_ESC("SELECT
162         `holiday_active`, `holiday_activated`
163 FROM
164         `{!_MYSQL_PREFIX!}_user_data`
165 WHERE
166         `userid`=%s
167 LIMIT 1", array(getUserId()), __FILE__, __LINE__);
168         $content = merge_array($content, SQL_FETCHARRAY($result));
169         SQL_FREERESULT($result);
170
171         if (($content['holiday_active'] == 'Y') && (($content['holiday_activated'] + getConfig('holiday_lock')) < time())) {
172                 // Load data
173                 $result = SQL_QUERY_ESC("SELECT
174         `holiday_start`, `holiday_end`
175 FROM
176         `{!_MYSQL_PREFIX!}_user_holidays`
177 WHERE
178         `userid`=%s
179 LIMIT 1", array(getUserId()), __FILE__, __LINE__);
180                 if (SQL_NUMROWS($result) == 1) {
181                         // Data was found
182                         $content = merge_array($content, SQL_FETCHARRAY($result));
183                         SQL_FREERESULT($result);
184
185                         // Prepare it for the template
186                         define('__HOLIDAY_START', generateDateTime($content['holiday_start'], '3'));
187                         define('__HOLIDAY_END'  , generateDateTime($content['holiday_end']  , '3'));
188
189                         // Deactivate it now
190                         SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_user_data`
191 SET
192         `holiday_active`='N',
193         `holiday_activated`=0
194 WHERE
195         `userid`=%s
196 LIMIT 1", array(getUserId()), __FILE__, __LINE__);
197
198                         // Remove entry
199                         SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM
200         `{!_MYSQL_PREFIX!}_user_holidays`
201 WHERE
202         `userid`=%s
203 LIMIT 1", array(getUserId()), __FILE__, __LINE__);
204
205                         // Send email to admin
206                         sendAdminNotification(getMessage('HOLIDAY_ADMIN_DEAC_SUBJ'), 'admin_holiday_deactivated', array(), getUserId());
207
208                         // Display message to user
209                         LOAD_TEMPLATE('admin_settings_saved', false, getMessage('HOLIDAY_MEMBER_DEACTIVATED_NOW'));
210                 } else {
211                         // Display message to user
212                         LOAD_TEMPLATE('admin_settings_saved', false, getMessage('HOLIDAY_MEMBER_CANNOT_DEACTIVATE'));
213                 }
214         } elseif ($content['holiday_active'] == 'Y') {
215                 // To fast!
216                 LOAD_TEMPLATE('admin_settings_saved', false, getMessage('HOLIDAY_MEMBER_LOCKED'));
217         }
218 } // END - if
219
220 // If something is wrong or link in menu is just clicked display form
221 if ((!isFormSent()) && (!REQUEST_ISSET_POST('stop'))) {
222         // Check if user is in holiday...
223         $result = SQL_QUERY_ESC("SELECT
224         `holiday_active`, `holiday_activated`
225 FROM
226         `{!_MYSQL_PREFIX!}_user_data`
227 WHERE
228         `userid`=%s
229 LIMIT 1", array(getUserId()), __FILE__, __LINE__);
230         $content = SQL_FETCHARRAY($result);
231         SQL_FREERESULT($result);
232
233         // Check for lock
234         if (($content['holiday_activated'] + getConfig('holiday_lock')) < time()) {
235                 // User can deactivate his holiday request
236                 switch ($content['holiday_active'])
237                 {
238                         case 'Y': // Display deactivation form
239                                 // Load starting and ending date
240                                 $result = SQL_QUERY_ESC("SELECT
241         `holiday_start`, `holiday_end`
242 FROM
243         `{!_MYSQL_PREFIX!}_user_holidays`
244 WHERE
245         `userid`=%s
246 LIMIT 1", array(getUserId()), __FILE__, __LINE__);
247                                 if (SQL_NUMROWS($result) == 1) {
248                                         // Data was found
249                                         $content = merge_array($content, SQL_FETCHARRAY($result));
250                                         SQL_FREERESULT($result);
251
252                                         // Prepare it for the template
253                                         define('__HOLIDAY_START', generateDateTime($content['holiday_start'] , '3'));
254                                         define('__HOLIDAY_END'  , generateDateTime($content['holiday_end']   , '3'));
255                                         define('__HOLIDAY_LOCK' , generateDateTime($content['holiday_activated'], '1'));
256
257                                         // Load template
258                                         LOAD_TEMPLATE('member_holiday_deactivate');
259                                 } else {
260                                         // Free memory
261                                         SQL_FREERESULT($result);
262
263                                         // Remove entry and reload URL
264                                         SQL_QUERY_ESC("UPDATE
265         `{!_MYSQL_PREFIX!}_user_data`
266 SET
267         `holiday_active`='N'
268 WHERE
269         `userid`=%s
270 LIMIT 1", array(getUserId()), __FILE__, __LINE__);
271                                         redirectToUrl('modules.php?module=login&amp;what=holiday');
272                                         return;
273                                 }
274                                 break;
275
276                         case 'N': // Display activation form
277                                 // Starting day
278                                 define('_START_DAY'  , ADD_SELECTION('day'  , date('d', (time() + getConfig('one_day'))), 'start'));
279                                 define('_START_MONTH', ADD_SELECTION('month', date('m', (time() + getConfig('one_day'))), 'start'));
280                                 define('_START_YEAR' , ADD_SELECTION('year' , date('Y', (time() + getConfig('one_day'))), 'start'));
281
282                                 // Calcualte ending date
283                                 $D = date('d', time() + getConfig('one_day') + (getConfig('one_day') * getConfig('holiday_max')));
284                                 $M = date('m', time() + getConfig('one_day') + (getConfig('one_day') * getConfig('holiday_max')));
285                                 $Y = date('Y', time() + getConfig('one_day') + (getConfig('one_day') * getConfig('holiday_max')));
286
287                                 // Ending day
288                                 define('_END_DAY'  , ADD_SELECTION('day'  , $D, 'end'));
289                                 define('_END_MONTH', ADD_SELECTION('month', $M, 'end'));
290                                 define('_END_YEAR' , ADD_SELECTION('year' , $Y, 'end'));
291
292                                 // Copy value from configuration
293                                 define('__HOLIDAY_MAX', getConfig('holiday_max'));
294
295                                 // Output form
296                                 LOAD_TEMPLATE('member_holiday_form');
297                                 break;
298                 }
299         } else {
300                 // To fast!
301                 LOAD_TEMPLATE('admin_settings_saved', false, getMessage('HOLIDAY_MEMBER_LOCKED'));
302         }
303 } // END - if
304
305 // [EOF]
306 ?>