Extension ext-earning introduced (unfinished), renamings:
[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  * -------------------------------------------------------------------- *
18  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
19  * Copyright (c) 2009 - 2011 by Mailer Developer Team                   *
20  * For more information visit: http://www.mxchange.org                  *
21  *                                                                      *
22  * This program is free software; you can redistribute it and/or modify *
23  * it under the terms of the GNU General Public License as published by *
24  * the Free Software Foundation; either version 2 of the License, or    *
25  * (at your option) any later version.                                  *
26  *                                                                      *
27  * This program is distributed in the hope that it will be useful,      *
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
30  * GNU General Public License for more details.                         *
31  *                                                                      *
32  * You should have received a copy of the GNU General Public License    *
33  * along with this program; if not, write to the Free Software          *
34  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
35  * MA  02110-1301  USA                                                  *
36  ************************************************************************/
37
38 // Some security stuff...
39 if (!defined('__SECURITY')) {
40         die();
41 } elseif (!isMember()) {
42         redirectToIndexMemberOnlyModule();
43 }
44
45 // Add description as navigation point
46 addYouAreHereLink('member', __FILE__);
47
48 if ((!isExtensionActive('holiday')) && (!isAdmin())) {
49         displayMessage('{%pipe,generateExtensionInactiveNotInstalledMessage=holiday%}');
50         return;
51 } // END - if
52
53 // Init content array
54 $content = array();
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(getMemberId()), __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(getMemberId()), __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                 unsetPostRequestElement('ok');
90                 unsetPostRequestElement('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                 displayMessage('{%message,MEMBER_HOLIDAY_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, postRequestElement('start_month'), postRequestElement('start_day'), postRequestElement('start_year'));
113         $content['holiday_end']   = mktime(0, 0, 0, postRequestElement('end_month')  , postRequestElement('end_day')  , postRequestElement('end_year')  );
114
115         // Test both values
116         $TEST = $content['holiday_end'] - $START;
117         if (($TEST < 0) || ($TEST > (getOneDay() * getConfig('holiday_max'))) || ($START < time()) || ($content['holiday_end'] < time())) {
118                 // Time test failed
119                 unsetPostRequestElement('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(getMemberId(), $START, $content['holiday_end'], postRequestElement('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(getMemberId()), __FILE__, __LINE__);
134
135                 // Prepare constants
136                 $content['start_day']   = bigintval(postRequestElement('start_day'));
137                 $content['start_month'] = $GLOBALS['month_descr'][postRequestElement('start_month')];
138                 $content['start_year']  = bigintval(postRequestElement('start_year'));
139                 $content['end_day']     = bigintval(postRequestElement('end_day'));
140                 $content['end_month']   = $GLOBALS['month_descr'][postRequestElement('end_month')];
141                 $content['end_year']    = bigintval(postRequestElement('end_year'));
142                 $content['comments']    = postRequestElement('comments');
143
144                 // Send mail to member
145                 $message = loadEmailTemplate('member_holiday_request', $content, getMemberId());
146                 sendEmail(getMemberId(), '{--MEMBER_HOLIDAY_SUBJECT--}', $message);
147
148                 // Send mail to all admins
149                 sendAdminNotification('{--ADMIN_HOLIDAY_SUBJECT--}', 'admin_holiday_request', $content, getMemberId());
150
151                 // Create task (we ignore the task id here)
152                 createNewTask('{--ADMIN_HOLIDAY_SUBJECT--}', $message, 'HOLIDAY_REQUEST', getMemberId());
153
154                 // Display message
155                 displayMessage('{--MEMBER_HOLIDAY_IS_ACTIVATED_NOW--}');
156         }
157 } // END - if
158
159 // Holiday shall be ended now
160 if (isPostRequestElementSet('stop')) {
161         // Okay, end the holiday here...
162         $result = SQL_QUERY_ESC("SELECT
163         `holiday_active`,`holiday_activated`
164 FROM
165         `{?_MYSQL_PREFIX?}_user_data`
166 WHERE
167         `userid`=%s
168 LIMIT 1", array(getMemberId()), __FILE__, __LINE__);
169
170         // Do we have an entry?
171         if (SQL_NUMROWS($result) == 1) {
172                 // Merge arrays
173                 $content = merge_array($content, SQL_FETCHARRAY($result));
174
175                 if (($content['holiday_active'] == 'Y') && (($content['holiday_activated'] + getConfig('holiday_lock')) < time())) {
176                         // Load data
177                         $result2 = SQL_QUERY_ESC("SELECT
178         `holiday_start`,`holiday_end`
179 FROM
180         `{?_MYSQL_PREFIX?}_user_holidays`
181 WHERE
182         `userid`=%s
183 LIMIT 1", array(getMemberId()), __FILE__, __LINE__);
184                         if (SQL_NUMROWS($result2) == 1) {
185                                 // Data was found so merge it
186                                 $content = merge_array($content, SQL_FETCHARRAY($result2));
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('{--ADMIN_HOLIDAY_DEACTIVATED_SUBJECT--}', 'admin_holiday_deactivated', $content, getMemberId());
212
213                                 // Display message to user
214                                 displayMessage('{--MEMBER_HOLIDAY_DEACTIVATED_NOW--}');
215                         } else {
216                                 // Display message to user
217                                 displayMessage('{--MEMBER_HOLIDAY_CANNOT_DEACTIVATE--}');
218                         }
219
220                         // Free result
221                         SQL_FREERESULT($result2);
222                 } elseif ($content['holiday_active'] == 'Y') {
223                         // To fast!
224                         displayMessage('{--MEMBER_HOLIDAY_LOCKED--}');
225                 }
226         } else {
227                 // User not found
228                 displayMessage('{--MEMBER_HOLIDAY_NOT_STOPPED_404--}');
229         }
230
231         // Free result
232         SQL_FREERESULT($result);
233 } // END - if
234
235 // If something is wrong or link in menu is just clicked display form
236 if ((!isFormSent()) && (!isPostRequestElementSet('stop'))) {
237         // Check if user is in holiday...
238         $result = SQL_QUERY_ESC("SELECT
239         `holiday_active`,`holiday_activated`
240 FROM
241         `{?_MYSQL_PREFIX?}_user_data`
242 WHERE
243         `userid`=%s
244 LIMIT 1", array(getMemberId()), __FILE__, __LINE__);
245         $content = SQL_FETCHARRAY($result);
246         SQL_FREERESULT($result);
247
248         // Check for lock
249         if (($content['holiday_activated'] + getConfig('holiday_lock')) < time()) {
250                 // User can deactivate his holiday request
251                 switch ($content['holiday_active']) {
252                         case 'Y': // Display deactivation form
253                                 // Load starting and ending date
254                                 $result = SQL_QUERY_ESC("SELECT
255         `holiday_start`,`holiday_end`
256 FROM
257         `{?_MYSQL_PREFIX?}_user_holidays`
258 WHERE
259         `userid`=%s
260 LIMIT 1", array(getMemberId()), __FILE__, __LINE__);
261                                 if (SQL_NUMROWS($result) == 1) {
262                                         // Data was found so merge it
263                                         $content = merge_array($content, SQL_FETCHARRAY($result));
264
265                                         // Prepare it for the template
266                                         $content['start'] = generateDateTime($content['holiday_start'] , 3);
267                                         $content['end']   = generateDateTime($content['holiday_end']   , 3);
268                                         $content['lock']  = generateDateTime($content['holiday_activated'], 1);
269
270                                         // Load template
271                                         loadTemplate('member_holiday_deactivate', false, $content);
272                                 } else {
273                                         // Free memory
274                                         SQL_FREERESULT($result);
275
276                                         // Remove entry and reload URL
277                                         SQL_QUERY_ESC("UPDATE
278         `{?_MYSQL_PREFIX?}_user_data`
279 SET
280         `holiday_active`='N'
281 WHERE
282         `userid`=%s
283 LIMIT 1",
284                                                 array(getMemberId()), __FILE__, __LINE__);
285                                         redirectToUrl('modules.php?module=login&amp;what=holiday');
286                                         return;
287                                 }
288
289                                 // Free result
290                                 SQL_FREERESULT($result);
291                                 break;
292
293                         case 'N': // Display activation form
294                                 // Cache timestamps
295                                 $startingStamp = time() + getOneDay();
296                                 $endingStamp   = $startingStamp + (getOneDay() * getConfig('holiday_max'));
297
298                                 // Starting day
299                                 $content['start_day']   = addSelectionBox('day'  , getDay($startingStamp)  , 'start');
300                                 $content['start_month'] = addSelectionBox('month', getMonth($startingStamp), 'start');
301                                 $content['start_year']  = addSelectionBox('year' , getYear($startingStamp) , 'start');
302
303                                 // Ending day
304                                 $content['end_day']   = addSelectionBox('day'  , getDay($endingStamp)  , 'end');
305                                 $content['end_month'] = addSelectionBox('month', getMonth($endingStamp), 'end');
306                                 $content['end_year']  = addSelectionBox('year' , getYear($endingStamp) , 'end');
307
308                                 // Output form
309                                 loadTemplate('member_holiday_form', false, $content);
310                                 break;
311                 } // END - switch
312         } else {
313                 // To fast!
314                 displayMessage('{--MEMBER_HOLIDAY_LOCKED--}');
315         }
316 } // END - if
317
318 // [EOF]
319 ?>