Heacy rewrite/cleanup:
[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  * -------------------------------------------------------------------- *
18  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
19  * Copyright (c) 2009 - 2013 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 } // END - if
42
43 // Add description as navigation point
44 addYouAreHereLink('guest', __FILE__);
45
46 if ((!isExtensionActive('sponsor'))) {
47         displayMessage('{%pipe,generateExtensionInactiveNotInstalledMessage=sponsor%}');
48         return;
49 } // END - if
50
51 // Create array for form errors (= missing data)
52 $formErrors = array();
53
54 if (isFormSent()) {
55         //
56         // Check submitted form data
57         //
58         // 1. Salutation / Surname / family name
59         if (!isPostRequestElementSet('gender')) {
60                 // Surname is empty
61                 array_push($formErrors, '{--SPONSOR_NO_GENDER_SELECTED--}');
62         } // END - if
63
64         if (!isPostRequestElementSet('surname')) {
65                 // Surname is empty
66                 array_push($formErrors, '{--SPONSOR_SURNAME_EMPTY--}');
67         } // END - if
68
69         if (!isPostRequestElementSet('family')) {
70                 // Surname is empty
71                 array_push($formErrors, '{--SPONSOR_FAMILY_EMPTY--}');
72         } // END - if
73
74         // 2. Company name
75         // 012                          3         32    23                         4           43    3                         4          4321    12                         3         32    2                          3           3210
76         if (((!isPostRequestElementSet('company')) && ((isPostRequestElementSet('tax_ident')) || (isPostRequestElementSet('position')))) || ((isPostRequestElementSet('company')) && (!isPostRequestElementSet('tax_ident')))) {
77                 if (!isPostRequestElementSet('company')) {
78                         // Company name is empty
79                         array_push($formErrors, '{--SPONSOR_COMPANY_EMPTY--}');
80                 } elseif (!isPostRequestElementSet('tax_ident')) {
81                         // Tax ident number name is empty
82                         array_push($formErrors, '{--SPONSOR_TAX_IDENT_EMPTY--}');
83                 }
84
85                 if (!isPostRequestElementSet('position')) {
86                         // Not fatal but not nice: position in company is empty
87                         array_push($formErrors, '{--SPONSOR_POSITION_EMPTY--}');
88                 } // END - if
89         } // END - if
90
91         // 3. Street and number
92         if (!isPostRequestElementSet('street_nr1')) {
93                 // Street name and house number are empty
94                 array_push($formErrors, '{--SPONSOR_STREET_NR1_EMPTY--}');
95         } elseif ((isPostRequestElementSet('street_nr2')) && (!isPostRequestElementSet('street_nr1'))) {
96                 // 1st line for street is empty, but 2nd line not
97                 array_push($formErrors, '{--SPONSOR_STREET_NR1_EMPTY_2_NOT--}');
98         }
99
100         // 4. Country code
101         if (!isPostRequestElementSet('country')) {
102                 // Country code is empty
103                 array_push($formErrors, '{--kSPONSOR_COUNTRY_EMPTY--}');
104         } elseif (strlen(postRequestElement('country')) != 2) {
105                 // Country code is invalid
106                 array_push($formErrors, '{--SPONSOR_COUNTRY_IS_INVALID--}');
107         }
108
109         // 3. ZIP code
110         if (!isPostRequestElementSet('zip')) {
111                 // ZIP code is empty
112                 array_push($formErrors, '{--SPONSOR_ZIP_EMPTY--}');
113         } elseif (bigintval(postRequestElement('zip')) != postRequestElement('zip')) {
114                 // ZIP is invalid
115                 array_push($formErrors, '{--SPONSOR_ZIP_IS_INVALID--}');
116                 setPostRequestElement('zip', '');
117         }
118
119         // 4. City
120         if (!isPostRequestElementSet('city')) {
121                 // City is empty
122                 array_push($formErrors, '{--SPONSOR_CITY_EMPTY--}');
123         } // END - if
124
125         // 5. Phone number
126         if (!isPostRequestElementSet('phone')) {
127                 // City is empty
128                 array_push($formErrors, '{--SPONSOR_PHONE_EMPTY--}');
129         } // END - if
130
131         // 6. Homepage URL
132         if (!isPostRequestElementSet('url')) {
133                 // Homepage URL is empty
134                 array_push($formErrors, '{--SPONSOR_URL_EMPTY--}');
135         } elseif (!isUrlValid(postRequestElement('url'))) {
136                 // Homepage URL is invalid
137                 array_push($formErrors, '{--SPONSOR_URL_IS_INVALID--}');
138                 setPostRequestElement('url', '');
139         }
140
141         // 7. Light validation of email address
142         if ((!isPostRequestElementSet('email')) || (postRequestElement('email') == '@')) {
143                 // Email is invalid/empty
144                 array_push($formErrors, '{--SPONSOR_EMAIL_IS_INVALID--}');
145         } elseif (isSponsorRegisteredWithEmail(postRequestElement('email'))) {
146                 // Email already found in database!
147                 array_push($formErrors, '{--SPONSOR_EMAIL_IS_ALREADY_REGISTERED--}');
148                 setPostRequestElement('email', '');
149         }
150
151         // 8. Pay type selected?
152         if (!isPostRequestElementSet('pay_type')) {
153                 // Not pay type selected
154                 array_push($formErrors, '{--SPONSOR_NO_PAYTYPE_SELECTED--}');
155         } // END - if
156
157         // 9. Interval of mails
158         if (!isPostRequestElementSet('warning_interval')) {
159                 // No warning interval selected
160                 array_push($formErrors, '{--SPONSOR_NO_WARNING_INTERVAL_SELECTED--}');
161         } // END - if
162
163         // 10. Mail notifications disabled/enabled
164         if (!isPostRequestElementSet('receive_warnings')) {
165                 // Option not selected!
166                 array_push($formErrors, '{--SPONSOR_NO_RECEIVE_WARNINGS_SELECTED--}');
167         } // END - if
168
169         // Did he enter his password twice?
170         if (((!isPostRequestElementSet('password1')) || (!isPostRequestElementSet('password2'))) || ((postRequestElement('password1') != postRequestElement('password2')) && (isPostRequestElementSet('password1')) && (isPostRequestElementSet('password2')))) {
171                 if ((postRequestElement('password1') != postRequestElement('password2')) && (isPostRequestElementSet('password1')) && (isPostRequestElementSet('password2'))) {
172                         // Passwords missmatch
173                         array_push($formErrors, '{--SPONSOR_PASSWORDS_MISMATCH--}');
174                 } else {
175                         if (!isPostRequestElementSet('password1')) {
176                                 // Password 1 is empty
177                                 array_push($formErrors, '{--SPONSOR_PASSWORD1_EMPTY--}');
178                         }
179
180                         if (!isPostRequestElementSet('password2')) {
181                                 // Password 2 is empty
182                                 array_push($formErrors, '{--SPONSOR_PASSWORD2_EMPTY--}');
183                         }
184                 }
185         } elseif (strlen(postRequestElement('password1')) < getMinPasswordLength()) {
186                 // Password is to short!
187                 array_push($formErrors, '{--SPONSOR_PASSWORD_TOO_SHORT--}');
188         }
189
190         // Check if he has accepted the terms&conditions
191         if (!isPostRequestElementSet('terms')) {
192                 // Homepage URL is empty
193                 array_push($formErrors, '{--SPONSOR_TERMS_NOT_ACCEPTED--}');
194         } // END - if
195
196         // If there is something wrong/missing stop registration
197         if (isFilledArray($formErrors)) unsetPostRequestElement('ok');
198 } // END - if
199
200 if ((isFormSent()) && (!isFilledArray($formErrors))) {
201         // Generate message array
202         $messageArray = array(
203                 'failed' => '{--SPONSOR_REGISTRATION_FAILED--}',
204                 'added'  => '{--SPONSOR_REGISTRATION_COMPLETED--}',
205         );
206
207         // Calulate points
208         $result = sqlQueryEscaped('SELECT
209         (`pay_rate` * `pay_min_count`) AS `points`,
210         `pay_min_count`,
211         `pay_currency`
212 FROM
213         `{?_MYSQL_PREFIX?}_sponsor_paytypes`
214 WHERE
215         `id`=%s
216 LIMIT 1',
217                 array(postRequestElement('pay_type')), __FILE__, __LINE__);
218         list($points, $pay, $curr) = sqlFetchRow($result);
219
220         // Free memory
221         sqlFreeResult($result);
222
223         // Add points to array
224         postRequestElement('points_amount', $points);
225         postRequestElement('points_used'  , '0.00000');
226         postRequestElement('last_payment' , $pay);
227         postRequestElement('last_currency', $curr);
228
229         // Register sponsor but never ever update here!
230         $message = doProcessSponsorFormRequest($messageArray);
231
232         // Display message
233         displayMessage($message);
234 } else {
235         // Check for payment types
236         $result = sqlQuery('SELECT
237         `id`,
238         `pay_name`,
239         `pay_rate`,
240         `pay_currency`,
241         `pay_min_count`
242 FROM
243         `{?_MYSQL_PREFIX?}_sponsor_paytypes`
244 ORDER BY
245         `pay_name` ASC', __FILE__, __LINE__);
246         if (!ifSqlHasZeroNums($result)) {
247                 // Load all types...
248                 $OUT = '';
249                 while ($content = sqlFetchArray($result)) {
250                         // Prepare content for output
251                         $content['res'] = translateComma($content['pay_rate'] * $content['pay_min_count']);
252
253                         // Load row template and switch color
254                         $OUT .= loadTemplate('guest_sponsor_payment_row', TRUE, $content);
255                 } // END - while
256
257                 // Remember the list in table
258                 $content['payment_list'] = loadTemplate('guest_sponsor_payment', TRUE, $OUT);
259
260                 // Init a lot array elements
261                 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) {
262                         $content[$entry]   = '';
263                 } // END - foreach
264
265                 // Check for invalid entries
266                 if (isFilledArray($formErrors)) {
267                         // Some found... :-(
268                         foreach (array('company','position','tax_ident','surname','family','street_nr1','street_nr2','country','zip','city','phone','fax','cell','email','url') as $entry) {
269                                 $content[$entry]    = postRequestElement($entry);
270                         } // END - foreach
271
272                         // Init receive selection
273                         $content['receive_warnings_y'] = '';
274                         $content['receive_warnings_n'] = '';
275
276                         // Check for receive_warnings
277                         $content['receive_warnings_' . strtolower(postRequestElement('receive_warnings'))] = ' selected="selected"';
278                         $OUT = '';
279
280                         // Errors found?
281                         if (isFilledArray($formErrors)) {
282                                 $OUT = displayMessage('{--SPONSOR_REGISTER_SOMETHING_MISSING--}', TRUE);
283                                 $OUT .= '<ol>';
284                                 foreach ($formErrors as $error) {
285                                         $OUT .= '<li>' . $error . '</li>';
286                                 } // END - foreach
287
288                                 $OUT .= '</ol>';
289                         } // END - if
290
291                         $content['form_errors'] = $OUT;
292
293                         // @TODO Maybe a default referral id?
294                         $content['refid'] = NULL;
295
296                         // Is the referral id valid?
297                         if ((isPostRequestElementSet('refid')) && (isValidId(postRequestElement('refid')))) {
298                                 // Transfer referral id
299                                 $content['refid'] = bigintval(postRequestElement('refid'));
300                         } // END - if
301                 } // END - if
302
303                 // Display registration form
304                 loadTemplate('guest_sponsor_reg', FALSE, $content);
305         } else {
306                 // Nothing added so far
307                 displayMessage('{--SPONSOR_PAYMENT_LIST_EMPTY--}');
308         }
309
310         // Free memory
311         sqlFreeResult($result);
312 }
313
314 // [EOF]
315 ?>