Fixes for sql_patches removal vs. password reset of admin
[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  *                                                                      *
14  * -------------------------------------------------------------------- *
15  * Copyright (c) 2003 - 2008 by Roland Haeder                           *
16  * For more information visit: http://www.mxchange.org                  *
17  *                                                                      *
18  * This program is free software; you can redistribute it and/or modify *
19  * it under the terms of the GNU General Public License as published by *
20  * the Free Software Foundation; either version 2 of the License, or    *
21  * (at your option) any later version.                                  *
22  *                                                                      *
23  * This program is distributed in the hope that it will be useful,      *
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
26  * GNU General Public License for more details.                         *
27  *                                                                      *
28  * You should have received a copy of the GNU General Public License    *
29  * along with this program; if not, write to the Free Software          *
30  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
31  * MA  02110-1301  USA                                                  *
32  ************************************************************************/
33
34 // Some security stuff...
35 if (ereg(basename(__FILE__), $_SERVER['PHP_SELF']))
36 {
37         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
38         require($INC);
39 }
40  elseif (!IS_LOGGED_IN())
41 {
42         LOAD_URL("modules.php?module=index");
43 }
44  elseif ((!EXT_IS_ACTIVE("holiday")) && (!IS_ADMIN()))
45 {
46         ADD_FATAL(EXTENSION_PROBLEM_EXT_INACTIVE, "holiday");
47         return;
48 }
49
50 // Add description as navigation point
51 ADD_DESCR("member", basename(__FILE__));
52
53 // Check for running mail orders in pool
54 $result1 = SQL_QUERY_ESC("SELECT timestamp FROM "._MYSQL_PREFIX."_pool
55 WHERE sender=%d ORDER BY timestamp DESC LIMIT 1", array($GLOBALS['userid']), __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=%d ORDER BY timestamp_ordered DESC LIMIT 1", array($GLOBALS['userid']), __FILE__, __LINE__);
60
61 if ((SQL_NUMROWS($result1) == 1) || (SQL_NUMROWS($result2) == 1))
62 {
63         // Mail order found!
64         list($stamp1) = SQL_FETCHROW($result1);
65         list($stamp2) = SQL_FETCHROW($result2);
66         if (empty($stamp1)) $stamp1 = 0;
67         if (empty($stamp2)) $stamp2 = 0;
68
69         if ((($stamp1 + $_CONFIG['holiday_lock']) > time()) || (($stamp2 + $_CONFIG['holiday_lock']) > time()))
70         {
71                 // Mail order is to close away!
72                 unset($_POST['ok']);
73                 unset($_POST['stop']);
74
75                 if (($stamp1 + $_CONFIG['holiday_lock']) > time())
76                 {
77                         // Mail found in pool
78                         $stamp = $stamp1;
79                 }
80                  else
81                 {
82                         // Mail found in stats
83                         $stamp = $stamp2;
84                 }
85
86                 // Display message and exit here
87                 LOAD_TEMPLATE("admin_settings_saved", false, HOLIDAY_MEMBER_ORDER_1.MAKE_DATETIME($stamp, "1").HOLIDAY_MEMBER_ORDER_2);
88                 return;
89         }
90 }
91
92 // Free memory
93 SQL_FREERESULT($result1);
94 SQL_FREERESULT($result2);
95
96 if (isset($_POST['ok']))
97 {
98         // Check holiday request...
99         $START = mktime(0, 0, 0, $_POST['start_month'], $_POST['start_day'], $_POST['start_year']);
100         $END   = mktime(0, 0, 0, $_POST['end_month']  , $_POST['end_day']  , $_POST['end_year']  );
101
102         // Test both values
103         $TEST = $END - $START;
104         if (($TEST < 0) || ($TEST > (ONE_DAY * $_CONFIG['holiday_max'])) || ($START < time()) || ($END < time()))
105         {
106                 // Time test failed
107                 unset($_POST['ok']);
108         }
109          else
110         {
111                 // Everything went okay so let's store his request and send mails
112                 $result = SQL_QUERY_ESC("INSERT INTO "._MYSQL_PREFIX."_user_holidays (userid, holiday_start, holiday_end, comments) VALUES ('%s', '%s', '%s', '%s')",
113                  array($GLOBALS['userid'], $START, $END, $_POST['comments']), __FILE__, __LINE__);
114
115                 // Activate holiday system
116                 $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_user_data
117 SET holiday_active='N', holiday_activated=UNIX_TIMESTAMP()
118 WHERE userid=%d LIMIT 1",
119                  array($GLOBALS['userid']), __FILE__, __LINE__);
120
121                 // Prepare constants
122                 define('_START_DAY'  , $_POST['start_day']);
123                 define('_START_MONTH', $MONTH_DESCR[$_POST['start_month']]);
124                 define('_START_YEAR' , $_POST['start_year']);
125                 define('_END_DAY'    , $_POST['end_day']);
126                 define('_END_MONTH'  , $MONTH_DESCR[$_POST['end_month']]);
127                 define('_END_YEAR'   , $_POST['end_year']);
128
129                 // Send mail to member
130                 $msg = LOAD_EMAIL_TEMPLATE("member_holiday_request", $_POST['comments'], $GLOBALS['userid']);
131                 SEND_EMAIL($GLOBALS['userid'], HOLIDAY_MEMBER_SUBJECT, $msg);
132
133                 // Send mail to all admins
134                 $msg = LOAD_EMAIL_TEMPLATE("admin_holiday_request", $_POST['comments'], $GLOBALS['userid']);
135                 if (GET_EXT_VERSION("admins") >= "0.4.1")
136                 {
137                         SEND_ADMIN_EMAILS_PRO(HOLIDAY_ADMIN_SUBJECT, "admin_holiday_request", $_POST['comments'], $GLOBALS['userid']);
138                 }
139                  else
140                 {
141                         SEND_ADMIN_EMAILS(HOLIDAY_ADMIN_SUBJECT, $msg);
142                 }
143
144                 // Create task
145                 $result = SQL_QUERY_ESC("INSERT INTO "._MYSQL_PREFIX."_task_system (userid, assigned_admin, status, task_type, subject, text, task_created) VALUES ('%s', '0', 'NEW', 'HOLIDAY_REQUEST', '%s', '%s', UNIX_TIMESTAMP())",
146                  array($GLOBALS['userid'], HOLIDAY_ADMIN_SUBJECT, addslashes($msg)), __FILE__, __LINE__);
147
148                 // Display message
149                 LOAD_TEMPLATE("admin_settings_saved", false, HOLIDAY_IS_ACTIVATED_NOW);
150         }
151 }
152
153 // Holiday shall be ended now
154 if (isset($_POST['stop']))
155 {
156         // Okay, end the holiday here...
157         $result = SQL_QUERY_ESC("SELECT holiday_active, holiday_activated FROM "._MYSQL_PREFIX."_user_data
158 WHERE userid=%d LIMIT 1", array($GLOBALS['userid']), __FILE__, __LINE__);
159         list($active, $locked) = SQL_FETCHROW($result);
160         SQL_FREERESULT($result);
161
162         if (($active == "Y") && (($locked + $_CONFIG['holiday_lock']) < time()))
163         {
164                 // Load data
165                 $result = SQL_QUERY_ESC("SELECT holiday_start, holiday_end FROM "._MYSQL_PREFIX."_user_holidays
166 WHERE userid=%d LIMIT 1", array($GLOBALS['userid']), __FILE__, __LINE__);
167                 if (SQL_NUMROWS($result) == 1)
168                 {
169                         // Data was found
170                         list($start, $end) = SQL_FETCHROW($result);
171                         SQL_FREERESULT($result);
172
173                         // Prepare it for the template
174                         define('__HOLIDAY_START', MAKE_DATETIME($start, "3"));
175                         define('__HOLIDAY_END'  , MAKE_DATETIME($end  , "3"));
176
177                         // Deactivate it now
178                         $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_user_data
179 SET holiday_active='N', holiday_activated='0'
180 WHERE userid=%d LIMIT 1", array($GLOBALS['userid']), __FILE__, __LINE__);
181
182                         // Remove entry
183                         $result = SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM "._MYSQL_PREFIX."_user_holidays
184 WHERE userid=%d LIMIT 1", array($GLOBALS['userid']), __FILE__, __LINE__);
185
186                         // Send email to admin
187                         if (GET_EXT_VERSION("admins") >= "0.4.1")
188                         {
189                                 SEND_ADMIN_EMAILS_PRO(HOLIDAY_ADMIN_DEAC_SUBJ, "admin_holiday_deactivated", "", $GLOBALS['userid']);
190                         }
191                          else
192                         {
193                                 $msg = LOAD_EMAIL_TEMPLATE("admin_holiday_deactivated", "", $GLOBALS['userid']);
194                                 SEND_ADMIN_EMAILS(HOLIDAY_ADMIN_DEAC_SUBJ, $msg);
195                         }
196
197                         // Display message to user
198                         LOAD_TEMPLATE("admin_settings_saved", false, HOLIDAY_MEMBER_DEACTIVATED_NOW);
199                 }
200                  else
201                 {
202                         // Display message to user
203                         LOAD_TEMPLATE("admin_settings_saved", false, HOLIDAY_MEMBER_CANNOT_DEACTIVATE);
204                 }
205         }
206          elseif ($active == "Y")
207         {
208                 // To fast!
209                 LOAD_TEMPLATE("admin_settings_saved", false, HOLIDAY_MEMBER_LOCKED);
210         }
211 }
212
213 // If something is wrong or link in menu is just clicked display form
214 if ((!isset($_POST['ok'])) && (!isset($_POST['stop'])))
215 {
216         // Check if user is in holiday...
217         $result = SQL_QUERY_ESC("SELECT holiday_active, holiday_activated FROM "._MYSQL_PREFIX."_user_data
218 WHERE userid=%d LIMIT 1", array($GLOBALS['userid']), __FILE__, __LINE__);
219         list($active, $locked) = SQL_FETCHROW($result);
220         SQL_FREERESULT($result);
221
222         // Check for lock
223         if (($locked + $_CONFIG['holiday_lock']) < time())
224         {
225                 // User can deactivate his holiday request
226                 switch ($active)
227                 {
228                 case 'Y': // Display deactivation form
229                         // Load starting and ending date
230                         $result = SQL_QUERY_ESC("SELECT holiday_start, holiday_end FROM "._MYSQL_PREFIX."_user_holidays
231 WHERE userid=%d LIMIT 1", array($GLOBALS['userid']), __FILE__, __LINE__);
232                         if (SQL_NUMROWS($result) == 1)
233                         {
234                                 // Data was found
235                                 list($start, $end) = SQL_FETCHROW($result);
236                                 SQL_FREERESULT($result);
237
238                                 // Prepare it for the template
239                                 define('__HOLIDAY_START', MAKE_DATETIME($start , "3"));
240                                 define('__HOLIDAY_END'  , MAKE_DATETIME($end   , "3"));
241                                 define('__HOLIDAY_LOCK' , MAKE_DATETIME($locked, "1"));
242
243                                 // Load template
244                                 LOAD_TEMPLATE("member_holiday_deactivate");
245                         }
246                          else
247                         {
248                                 // Free memory
249                                 SQL_FREERESULT($result);
250
251                                 // Remove entry and reload URL
252                                 $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_user_data
253 SET holiday_active='N'
254 WHERE userid=%d LIMIT 1", array($GLOBALS['userid']), __FILE__, __LINE__);
255                                 LOAD_URL("modules.php?module=login&amp;what=holiday");
256                                 return;
257                         }
258                         break;
259
260                 case 'N': // Display activation form
261                         // Starting day
262                         define('_START_DAY'  , ADD_SELECTION("day"  , date("d", (time() + ONE_DAY)), "start"));
263                         define('_START_MONTH', ADD_SELECTION("month", date("m", (time() + ONE_DAY)), "start"));
264                         define('_START_YEAR' , ADD_SELECTION("year" , date('Y', (time() + ONE_DAY)), "start"));
265
266                         // Calcualte ending date
267                         $D = date("d", time() + ONE_DAY + (ONE_DAY * $_CONFIG['holiday_max']));
268                         $M = date("m", time() + ONE_DAY + (ONE_DAY * $_CONFIG['holiday_max']));
269                         $Y = date('Y', time() + ONE_DAY + (ONE_DAY * $_CONFIG['holiday_max']));
270
271                         // Ending day
272                         define('_END_DAY'  , ADD_SELECTION("day"  , $D, "end"));
273                         define('_END_MONTH', ADD_SELECTION("month", $M, "end"));
274                         define('_END_YEAR' , ADD_SELECTION("year" , $Y, "end"));
275
276                         // Copy value from configuration
277                         define('__HOLIDAY_MAX', $_CONFIG['holiday_max']);
278
279                         // Output form
280                         LOAD_TEMPLATE("member_holiday_form");
281                         break;
282                 }
283         }
284          else
285         {
286                 // To fast!
287                 LOAD_TEMPLATE("admin_settings_saved", false, HOLIDAY_MEMBER_LOCKED);
288         }
289 }
290 //
291 ?>