More rewrites/fixes (not all is fixed) for ext-sponsor
[mailer.git] / inc / modules / guest / what-sponsor_reg.php
1 <?php
2 /************************************************************************
3  * Mailer v0.2.1-FINAL                                Start: 06/10/2005 *
4  * ===================                          Last change: 05/18/2008 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : what-sponsor_reg.php                             *
8  * -------------------------------------------------------------------- *
9  * Short description : Sponsor's registration form                      *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Anmeldeformular fuer Sponsoren                   *
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 - 2009 by Roland Haeder                           *
21  * Copyright (c) 2009, 2010 by Mailer Developer Team                    *
22  * For more information visit: http://www.mxchange.org                  *
23  *                                                                      *
24  * This program is free software; you can redistribute it and/or modify *
25  * it under the terms of the GNU General Public License as published by *
26  * the Free Software Foundation; either version 2 of the License, or    *
27  * (at your option) any later version.                                  *
28  *                                                                      *
29  * This program is distributed in the hope that it will be useful,      *
30  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
31  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
32  * GNU General Public License for more details.                         *
33  *                                                                      *
34  * You should have received a copy of the GNU General Public License    *
35  * along with this program; if not, write to the Free Software          *
36  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
37  * MA  02110-1301  USA                                                  *
38  ************************************************************************/
39
40 // Some security stuff...
41 if (!defined('__SECURITY')) {
42         die();
43 } // END - if
44
45 // Add description as navigation point
46 addMenuDescription('guest', __FILE__);
47
48 if ((!isExtensionActive('sponsor'))) {
49         loadTemplate('admin_settings_saved', false, generateExtensionInactiveNotInstalledMessage('sponsor'));
50         return;
51 } // END - if
52
53 // Create array for form errors (= missing data)
54 $formErrors = array();
55
56 if (isFormSent()) {
57         //
58         // Check submitted form data
59         //
60         // 1. Salutation / Surname / family name
61         if (!isPostRequestParameterSet('gender')) {
62                 // Surname is empty
63                 $formErrors[] = '{--SPONSOR_NO_GENDER_SELECTED--}';
64         } // END - if
65
66         if (!isPostRequestParameterSet('surname')) {
67                 // Surname is empty
68                 $formErrors[] = '{--SPONSOR_SURNAME_IS_EMPTY--}';
69         } // END - if
70
71         if (!isPostRequestParameterSet('family')) {
72                 // Surname is empty
73                 $formErrors[] = '{--SPONSOR_FAMILY_IS_EMPTY--}';
74         } // END - if
75
76         // 2. Company name
77         // 012                          3         32    23                         4           43    3                         4          4321    12                         3         32    2                          3           3210
78         if (((!isPostRequestParameterSet('company')) && ((isPostRequestParameterSet('tax_ident')) || (isPostRequestParameterSet('position')))) || ((isPostRequestParameterSet('company')) && (!isPostRequestParameterSet('tax_ident')))) {
79                 if (!isPostRequestParameterSet('company')) {
80                         // Company name is empty
81                         $formErrors[] = '{--SPONSOR_COMPANY_IS_EMPTY--}';
82                 } elseif (!isPostRequestParameterSet('tax_ident')) {
83                         // Tax ident number name is empty
84                         $formErrors[] = '{--SPONSOR_TAX_IDENT_IS_EMPTY--}';
85                 }
86
87                 if (!isPostRequestParameterSet('position')) {
88                         // Not fatal but not nice: position in company is empty
89                         $formErrors[] = '{--SPONSOR_POSITION_IS_EMPTY--}';
90                 } // END - if
91         } // END - if
92
93         // 3. Street and number
94         if (!isPostRequestParameterSet('street_nr1')) {
95                 // Street name and house number are empty
96                 $formErrors[] = '{--SPONSOR_STREET_NR1_IS_EMPTY--}';
97         } elseif ((isPostRequestParameterSet('street_nr2')) && (!isPostRequestParameterSet('street_nr1'))) {
98                 // 1st line for street is empty, but 2nd line not
99                 $formErrors[] = '{--SPONSOR_STREET_NR1_IS_EMPTY_2_NOT--}';
100         }
101
102         // 4. Country code
103         if (!isPostRequestParameterSet('country')) {
104                 // Country code is empty
105                 $formErrors[] = '{--kSPONSOR_COUNTRY_IS_EMPTY--}';
106         } elseif (strlen(postRequestParameter('country')) != 2) {
107                 // Country code is invalid
108                 $formErrors[] = '{--SPONSOR_COUNTRY_IS_INVALID--}';
109         }
110
111         // 3. ZIP code
112         if (!isPostRequestParameterSet('zip')) {
113                 // ZIP code is empty
114                 $formErrors[] = '{--SPONSOR_ZIP_IS_EMPTY--}';
115         } elseif (bigintval(postRequestParameter('zip')) != postRequestParameter('zip')) {
116                 // ZIP is invalid
117                 $formErrors[] = '{--SPONSOR_ZIP_IS_INVALID--}';
118                 setPostRequestParameter('zip', '');
119         }
120
121         // 4. City
122         if (!isPostRequestParameterSet('city')) {
123                 // City is empty
124                 $formErrors[] = '{--SPONSOR_CITY_IS_EMPTY--}';
125         } // END - if
126
127         // 5. Phone number
128         if (!isPostRequestParameterSet('phone')) {
129                 // City is empty
130                 $formErrors[] = '{--SPONSOR_PHONE_IS_EMPTY--}';
131         } // END - if
132
133         // 6. Homepage URL
134         if (!isPostRequestParameterSet('url')) {
135                 // Homepage URL is empty
136                 $formErrors[] = '{--SPONSOR_URL_IS_EMPTY--}';
137         } elseif (!isUrlValid(postRequestParameter('url'))) {
138                 // Homepage URL is invalid
139                 $formErrors[] = '{--SPONSOR_URL_IS_INVALID--}';
140                 setPostRequestParameter('url', '');
141         }
142
143         // 7. Light validation of email address
144         if ((!isPostRequestParameterSet('email')) || (postRequestParameter('email') == '@')) {
145                 // Email is invalid/empty
146                 $formErrors[] = '{--SPONSOR_EMAIL_IS_INVALID--}';
147         } elseif (isSponsorRegisteredWithEmail(postRequestParameter('email'))) {
148                 // Email already found in database!
149                 $formErrors[] = '{--SPONSOR_EMAIL_IS_ALREADY_REGISTERED--}';
150                 setPostRequestParameter('email', '');
151         }
152
153         // 8. Pay type selected?
154         if (!isPostRequestParameterSet('pay_type')) {
155                 // Not pay type selected
156                 $formErrors[] = '{--SPONSOR_NO_PAYTYPE_SELECTED--}';
157         } // END - if
158
159         // 9. Interval of mails
160         if (!isPostRequestParameterSet('warning_interval')) {
161                 // No warning interval selected
162                 $formErrors[] = '{--SPONSOR_NO_WARNING_INTERVAL_SELECTED--}';
163         } // END - if
164
165         // 10. Mail notifications disabled/enabled
166         if (!isPostRequestParameterSet('receive_warnings')) {
167                 // Option not selected!
168                 $formErrors[] = '{--SPONSOR_NO_RECEIVE_WARNINGS_SELECTED--}';
169         } // END - if
170
171         // Did he enter his password twice?
172         if (((!isPostRequestParameterSet('pass1')) || (!isPostRequestParameterSet('pass2'))) || ((postRequestParameter('pass1') != postRequestParameter('pass2')) && (isPostRequestParameterSet('pass1')) && (isPostRequestParameterSet('pass2')))) {
173                 if ((postRequestParameter('pass1') != postRequestParameter('pass2')) && (isPostRequestParameterSet('pass1')) && (isPostRequestParameterSet('pass2'))) {
174                         // Passwords missmatch
175                         $formErrors[] = '{--SPONSOR_PASSWORDS_MISMATCH--}';
176                 } else {
177                         if (!isPostRequestParameterSet('pass1')) {
178                                 // Password 1 is empty
179                                 $formErrors[] = '{--SPONSOR_PASSWORD1_EMPTY--}';
180                         }
181
182                         if (!isPostRequestParameterSet('pass2')) {
183                                 // Password 2 is empty
184                                 $formErrors[] = '{--SPONSOR_PASSWORD2_EMPTY--}';
185                         }
186                 }
187         } elseif (strlen(postRequestParameter('pass1')) < getConfig('pass_len')) {
188                 // Password is to short!
189                 $formErrors[] = '{--SPONSOR_PASSWORD_TOO_SHORT--}';
190         }
191
192         // Check if he has accepted the terms&conditions
193         if (!isPostRequestParameterSet('terms')) {
194                 // Homepage URL is empty
195                 $formErrors[] = '{--SPONSOR_TERMS_NOT_ACCEPTED--}';
196         } // END - if
197
198         // If there is something wrong/missing stop registration
199         if (count($formErrors) > 0) unsetPostRequestParameter('ok');
200 } // END - if
201
202 if ((isFormSent()) && (count($formErrors) == 0)) {
203         // Generate message array
204         $messageArray = array(
205                 'failed' => '{--SPONSOR_REGISTRATION_FAILED--}',
206                 'added'  => '{--SPONSOR_REGISTRATION_COMPLETED--}',
207         );
208
209         // Calulate points
210         $result = SQL_QUERY_ESC("SELECT
211         (`pay_rate` * `pay_min_count`) AS points, `pay_min_count` AS pay, `pay_currency` AS curr
212 FROM
213         `{?_MYSQL_PREFIX?}_sponsor_paytypes`
214 WHERE
215         `id`=%s
216 LIMIT 1",
217                 array(postRequestParameter('pay_type')), __FILE__, __LINE__);
218         list($points, $pay, $curr) = SQL_FETCHROW($result);
219
220         // Free memory
221         SQL_FREERESULT($result);
222
223         // Add points to array
224         postRequestParameter('points_amount', $points);
225         postRequestParameter('points_used'  , '0.00000');
226         postRequestParameter('last_pay'     , $pay);
227         postRequestParameter('last_curr'    , $curr);
228
229         // Register sponsor but never ever update here!
230         $message = doProcessSponsorFormRequest($messageArray);
231
232         // Display message
233         loadTemplate('admin_settings_saved', false, $message);
234 } else {
235         // Check for payment types
236         $result = SQL_QUERY('SELECT
237         `id`, `pay_name`, `pay_rate`, `pay_currency`, `pay_min_count`
238 FROM
239         `{?_MYSQL_PREFIX?}_sponsor_paytypes`
240 ORDER BY 
241         `pay_name` ASC', __FILE__, __LINE__);
242         if (!SQL_HASZERONUMS($result)) {
243                 // Load all types...
244                 $OUT = '';
245                 while ($content = SQL_FETCHARRAY($result)) {
246                         // Prepare content for output
247                         $content['res'] = translateComma($content['pay_rate'] * $content['pay_min_count']);
248
249                         // Load row template and switch color
250                         $OUT .= loadTemplate('guest_sponsor_payment_row', true, $content);
251                 } // END - while
252
253                 // Remember the list in table
254                 $content['payment_list'] = loadTemplate('guest_sponsor_payment', true, $OUT);
255
256                 // Init a lot array elements
257                 foreach (array('company','position','tax_ident','gender_m','gender_f','gender_c','surname','family','street_nr1','street_nr2','country','zip','city','phone','fax','cell','email','url','receive_warnings_y','receive_warnings_n','form_errors','refid') as $entry) {
258                         $content[$entry]   = '';
259                 } // END - foreach
260
261                 // Check for invalid entries
262                 if (count($formErrors) > 0) {
263                         // Some found... :-(
264                         foreach (array('company','position','tax_ident','surname','family','street_nr1','street_nr2','country','zip','city','phone','fax','cell','email','url') as $entry) {
265                                 $content[$entry]    = secureString(postRequestParameter($entry));
266                         } // END - foreach
267
268                         // Init gender
269                         foreach (array('m', 'f', 'c') as $gender) {
270                                 $content['gender_' . $gender] = '';
271                         } // END - foreach
272
273                         // Check for gender selection
274                         $content['gender_' . strtolower(postRequestParameter('gender'))] = ' selected="selected"';
275
276                         // Init receive selection
277                         $content['receive_warnings_y'] = '';
278                         $content['receive_warnings_n'] = '';
279
280                         // Check for receive_warnings
281                         $content['receive_warnings_' . strtolower(postRequestParameter('receive_warnings'))] = ' selected="selected"';
282                         $OUT = '';
283
284                         // Errors found?
285                         if (count($formErrors) > 0) {
286                                 $OUT = loadTemplate('admin_settings_saved', true, '{--SPONSOR_REGISTER_SOMETHING_MISSING--}');
287                                 $OUT .= '<ol>';
288                                 foreach ($formErrors as $error) {
289                                         $OUT .= '<li>' . $error . '</li>';
290                                 } // END - foreach
291
292                                 $OUT .= '</ol>';
293                         } // END - if
294
295                         $content['form_errors'] = $OUT;
296
297                         // @TODO Maybe a default referal id?
298                         $content['refid']       = 0;
299
300                         // Is the referal id valid?
301                         if ((isPostRequestParameterSet('refid')) && (isValidUserId(postRequestParameter('refid')))) {
302                                 // Transfer referal id
303                                 $content['refid']       = bigintval(postRequestParameter('refid'));
304                         } // END - if
305                 } // END - if
306
307                 // Display registration form
308                 loadTemplate('guest_sponsor_reg', false, $content);
309         } else {
310                 // Nothing added so far
311                 loadTemplate('admin_settings_saved', false, '{--SPONSOR_PAYMENT_LIST_IS_EMPTY--}');
312         }
313
314         // Free memory
315         SQL_FREERESULT($result);
316 }
317
318 // [EOF]
319 ?>