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