Renamed ifSqlHasZeroNums() to ifSqlHasZeroNumRows() and improved some queries.
[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 - 2015 by Mailer Developer Team                   *
20  * For more information visit: http://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         exit();
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 = sqlQueryEscaped("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 = sqlQueryEscaped("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 ((sqlNumRows($result1) == 1) || (sqlNumRows($result2) == 1)) {
79         // Mail order found
80         $content = merge_array($content, sqlFetchArray($result1));
81         $content = merge_array($content, sqlFetchArray($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'] + getHolidayLock()) > time()) || (($content['timestamp_ordered'] + getHolidayLock()) > time())) {
88                 // Mail order is to close away!
89                 unsetPostRequestElement('ok');
90                 unsetPostRequestElement('stop');
91
92                 if (($content['timestamp'] + getHolidayLock()) > 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 sqlFreeResult($result1);
108 sqlFreeResult($result2);
109
110 if (isFormSent()) {
111         // Check holiday request...
112         $content['holiday_start'] = mktime(0, 0, 0, postRequestElement('start_mo'), postRequestElement('start_da'), postRequestElement('start_ye'));
113         $content['holiday_end']   = mktime(0, 0, 0, postRequestElement('end_mo')  , postRequestElement('end_da')  , postRequestElement('end_ye')  );
114
115         // Test both values
116         $TEST = $content['holiday_end'] - $content['holiday_start'];
117         if (($TEST < 0) || ($TEST > (getOneDay() * getConfig('holiday_max'))) || ($content['holiday_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                 sqlQueryEscaped("INSERT INTO `{?_MYSQL_PREFIX?}_user_holidays` (`userid`, `holiday_start`, `holiday_end`, `comments`) VALUES ('%s','%s','%s','%s')",
123                         array(
124                                 getMemberId(),
125                                 $content['holiday_start'],
126                                 $content['holiday_end'],
127                                 postRequestElement('comments')
128                         ), __FILE__, __LINE__);
129
130                 // Activate holiday system
131                 sqlQueryEscaped("UPDATE
132         `{?_MYSQL_PREFIX?}_user_data`
133 SET
134         `holiday_active`='N',
135         `holiday_activated`=UNIX_TIMESTAMP()
136 WHERE
137         `userid`=%s
138 LIMIT 1",
139                         array(getMemberId()), __FILE__, __LINE__);
140
141                 // Prepare constants
142                 $content['start_day']   = bigintval(postRequestElement('start_day'));
143                 $content['start_month'] = $GLOBALS['month_descr'][postRequestElement('start_month')];
144                 $content['start_year']  = bigintval(postRequestElement('start_year'));
145                 $content['end_day']     = bigintval(postRequestElement('end_day'));
146                 $content['end_month']   = $GLOBALS['month_descr'][postRequestElement('end_month')];
147                 $content['end_year']    = bigintval(postRequestElement('end_year'));
148                 $content['comments']    = postRequestElement('comments');
149
150                 // Send mail to member
151                 $message = loadEmailTemplate('member_holiday_request', $content, getMemberId());
152                 sendEmail(getMemberId(), '{--MEMBER_HOLIDAY_SUBJECT--}', $message);
153
154                 // Send mail to all admins
155                 sendAdminNotification('{--ADMIN_HOLIDAY_SUBJECT--}', 'admin_holiday_request', $content, getMemberId());
156
157                 // Create task (we ignore the task id here)
158                 createNewTask('{--ADMIN_HOLIDAY_SUBJECT--}', $message, 'HOLIDAY_REQUEST', getMemberId());
159
160                 // Display message
161                 displayMessage('{--MEMBER_HOLIDAY_IS_ACTIVATED_NOW--}');
162         }
163 } // END - if
164
165 // Holiday shall be ended now
166 if (isPostRequestElementSet('stop')) {
167         // Okay, end the holiday here...
168         $result = sqlQueryEscaped("SELECT
169         `holiday_active`,
170         `holiday_activated`
171 FROM
172         `{?_MYSQL_PREFIX?}_user_data`
173 WHERE
174         `userid`=%s
175 LIMIT 1", array(getMemberId()), __FILE__, __LINE__);
176
177         // Is there an entry?
178         if (sqlNumRows($result) == 1) {
179                 // Merge arrays
180                 $content = merge_array($content, sqlFetchArray($result));
181
182                 if (($content['holiday_active'] == 'Y') && (($content['holiday_activated'] + getHolidayLock()) < time())) {
183                         // Load data
184                         $result2 = sqlQueryEscaped("SELECT
185         `holiday_start`,
186         `holiday_end`
187 FROM
188         `{?_MYSQL_PREFIX?}_user_holidays`
189 WHERE
190         `userid`=%s
191 LIMIT 1", array(getMemberId()), __FILE__, __LINE__);
192                         if (sqlNumRows($result2) == 1) {
193                                 // Data was found so merge it
194                                 $content = merge_array($content, sqlFetchArray($result2));
195
196                                 // Prepare it for the template
197                                 $content['start'] = generateDateTime($content['holiday_start'], 3);
198                                 $content['end']   = generateDateTime($content['holiday_end']  , 3);
199
200                                 // Deactivate it now
201                                 sqlQueryEscaped("UPDATE
202         `{?_MYSQL_PREFIX?}_user_data`
203 SET
204         `holiday_active`='N',
205         `holiday_activated`=0
206 WHERE
207         `userid`=%s
208 LIMIT 1",
209                                         array(getMemberId()), __FILE__, __LINE__);
210
211                                 // Remove entry
212                                 sqlQueryEscaped("DELETE LOW_PRIORITY FROM
213         `{?_MYSQL_PREFIX?}_user_holidays`
214 WHERE
215         `userid`=%s
216 LIMIT 1",
217                                         array(getMemberId()), __FILE__, __LINE__);
218
219                                 // Send email to admin
220                                 sendAdminNotification('{--ADMIN_HOLIDAY_DEACTIVATED_SUBJECT--}', 'admin_holiday_deactivated', $content, getMemberId());
221
222                                 // Display message to user
223                                 displayMessage('{--MEMBER_HOLIDAY_DEACTIVATED_NOW--}');
224                         } else {
225                                 // Display message to user
226                                 displayMessage('{--MEMBER_HOLIDAY_CANNOT_DEACTIVATE--}');
227                         }
228
229                         // Free result
230                         sqlFreeResult($result2);
231                 } elseif ($content['holiday_active'] == 'Y') {
232                         // To fast!
233                         displayMessage('{--MEMBER_HOLIDAY_LOCKED--}');
234                 }
235         } else {
236                 // User not found
237                 displayMessage('{--MEMBER_HOLIDAY_NOT_STOPPED_404--}');
238         }
239
240         // Free result
241         sqlFreeResult($result);
242 } // END - if
243
244 // If something is wrong or link in menu is just clicked display form
245 if ((!isFormSent()) && (!isPostRequestElementSet('stop'))) {
246         // Check if user is in holiday...
247         $result = sqlQueryEscaped("SELECT
248         `holiday_active`,
249         `holiday_activated`
250 FROM
251         `{?_MYSQL_PREFIX?}_user_data`
252 WHERE
253         `userid`=%s
254 LIMIT 1", array(getMemberId()), __FILE__, __LINE__);
255         $content = sqlFetchArray($result);
256         sqlFreeResult($result);
257
258         // Check for lock
259         if (($content['holiday_activated'] + getHolidayLock()) < time()) {
260                 // User can deactivate his holiday request
261                 switch ($content['holiday_active']) {
262                         case 'Y': // Display deactivation form
263                                 // Load starting and ending date
264                                 $result = sqlQueryEscaped("SELECT
265         `holiday_start`,
266         `holiday_end`
267 FROM
268         `{?_MYSQL_PREFIX?}_user_holidays`
269 WHERE
270         `userid`=%s
271 LIMIT 1", array(getMemberId()), __FILE__, __LINE__);
272                                 if (sqlNumRows($result) == 1) {
273                                         // Data was found so merge it
274                                         $content = merge_array($content, sqlFetchArray($result));
275
276                                         // Prepare it for the template
277                                         $content['start'] = generateDateTime($content['holiday_start'] , 3);
278                                         $content['end']   = generateDateTime($content['holiday_end']   , 3);
279                                         $content['lock']  = generateDateTime($content['holiday_activated'], 1);
280
281                                         // Load template
282                                         loadTemplate('member_holiday_deactivate', FALSE, $content);
283                                 } else {
284                                         // Free memory
285                                         sqlFreeResult($result);
286
287                                         // Remove entry and reload URL
288                                         sqlQueryEscaped("UPDATE
289         `{?_MYSQL_PREFIX?}_user_data`
290 SET
291         `holiday_active`='N'
292 WHERE
293         `userid`=%s
294 LIMIT 1",
295                                                 array(getMemberId()), __FILE__, __LINE__);
296                                         redirectToUrl('modules.php?module=login&amp;what=holiday');
297                                         return;
298                                 }
299
300                                 // Free result
301                                 sqlFreeResult($result);
302                                 break;
303
304                         case 'N': // Display activation form
305                                 // Cache timestamps
306                                 $startingStamp = time() + getOneDay();
307                                 $endingStamp   = $startingStamp + (getOneDay() * getConfig('holiday_max'));
308
309                                 // Starting day
310                                 $content['start_day']   = addSelectionBox('da', getDay($startingStamp)  , 'start');
311                                 $content['start_month'] = addSelectionBox('mo', getMonth($startingStamp), 'start');
312                                 $content['start_year']  = addSelectionBox('ye', getYear($startingStamp) , 'start');
313
314                                 // Ending day
315                                 $content['end_day']   = addSelectionBox('da', getDay($endingStamp)  , 'end');
316                                 $content['end_month'] = addSelectionBox('mo', getMonth($endingStamp), 'end');
317                                 $content['end_year']  = addSelectionBox('ye', getYear($endingStamp) , 'end');
318
319                                 // Output form
320                                 loadTemplate('member_holiday_form', FALSE, $content);
321                                 break;
322                 } // END - switch
323         } else {
324                 // To fast!
325                 displayMessage('{--MEMBER_HOLIDAY_LOCKED--}');
326         }
327 } // END - if
328
329 // [EOF]
330 ?>