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