Extension 'ext-coupon' moved to branch, SQLs improved:
[mailer.git] / inc / modules / admin / what-unlock_sponsor.php
index e6924acb4f7d52700ac23069cd1f19d39b83f1de..3a1852f82561ac98ef5512ffd68d79ce115d7d1a 100644 (file)
  * $Date::                                                            $ *
  * $Tag:: 0.2.1-FINAL                                                 $ *
  * $Author::                                                          $ *
- * Needs to be in all Files and every File needs "svn propset           *
- * svn:keywords Date Revision" (autoprobset!) at least!!!!!!            *
  * -------------------------------------------------------------------- *
  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
+ * Copyright (c) 2009 - 2011 by Mailer Developer Team                   *
  * For more information visit: http://www.mxchange.org                  *
  *                                                                      *
  * This program is free software; you can redistribute it and/or modify *
 // Some security stuff...
 if ((!defined('__SECURITY')) || (!isAdmin())) {
        die();
-}
+} // END - if
 
 // Add description as navigation point
-addMenuDescription('admin', __FILE__);
+addYouAreHereLink('admin', __FILE__);
 
 // Check if admin has submitted form
 if (isFormSent()) {
        // Does he have selected at least one sponsor?
-       if (countSelection(postRequestParameter('id')) > 0) {
+       if (ifPostContainsSelections('id')) {
                // At least one entry selected
                foreach (postRequestParameter('id') as $id => $selected) {
                        // Secure id number and init $content
@@ -57,32 +56,49 @@ if (isFormSent()) {
                        );
 
                        // Load his personal data
-                       $result_main = SQL_QUERY_ESC("SELECT gender, surname, family, email, remote_addr, sponsor_created, points_amount, refid
-FROM `{?_MYSQL_PREFIX?}_sponsor_data`
-WHERE `status`='PENDING' AND id='%s' LIMIT 1",
+                       $result_main = SQL_QUERY_ESC("SELECT
+       `id`, `gender`, `surname`, `family`, `email`, `remote_addr`,
+       UNIX_TIMESTAMP(`sponsor_created`) AS `sponsor_created`,
+       `points_amount`, `points_used`,
+       (`points_amount` + `points_used`) AS `points`,
+       `refid`
+FROM
+       `{?_MYSQL_PREFIX?}_sponsor_data`
+WHERE
+       `status` IN('PENDING','UNCONFIRMED') AND
+       `id`=%s
+LIMIT 1",
                        array($content['id']), __FILE__, __LINE__);
 
+                       /// Is there an entry?
                        if (SQL_NUMROWS($result_main) == 1) {
                                // Load data
                                $content = SQL_FETCHARRAY($result_main);
 
                                // Check for open payments and close them
-                               $result = SQL_QUERY_ESC("SELECT so.admin_id, so.pay_count, so.pay_ordered, so.pay_status,
-sp.pay_name, sp.pay_rate, sp.pay_currency
-FROM `{?_MYSQL_PREFIX?}_sponsor_orders` AS so
-LEFT JOIN `{?_MYSQL_PREFIX?}_sponsor_paytypes` AS sp
-ON sp.id=so.payid
-WHERE so.sponsorid='%s'
-ORDER BY sp.pay_name",
-                               array($content['id']), __FILE__, __LINE__);
-                               if (SQL_NUMROWS($result) > 0) {
+                               $result = SQL_QUERY_ESC("SELECT
+       so.admin_id, so.pay_count,
+       UNIX_TIMESTAMP(so.pay_ordered) AS `pay_ordered`,
+       so.pay_status, sp.pay_name, sp.pay_rate, sp.pay_currency
+FROM
+       `{?_MYSQL_PREFIX?}_sponsor_orders` AS so
+LEFT JOIN
+       `{?_MYSQL_PREFIX?}_sponsor_paytypes` AS sp
+ON
+       sp.id=so.payment_id
+WHERE
+       so.sponsor_id=%s
+ORDER BY
+       sp.pay_name ASC",
+                                       array($content['id']), __FILE__, __LINE__);
+                               if (!SQL_HASZERONUMS($result)) {
                                        // Payment does exist
                                        while ($content2 = SQL_FETCHARRAY($result)) {
                                                // Merge both arrays
                                                $content = merge_array($content, $content2);
 
                                                // Set default email
-                                               $content['email'] = getMessage('SPONSOR_NO_ADMIN');
+                                               $content['email'] = '{--SPONSOR_NO_ADMIN--}';
                                                if ($content['admin_id'] > 0) {
                                                        // Load admin's email address for contact
                                                        $content['email'] = getAdminEmail($content['admin_id']);
@@ -90,10 +106,10 @@ ORDER BY sp.pay_name",
 
                                                // Transfer data to array
                                                $data = array(
-                                                       'admin_id'   => $content['email'],
-                                                       'order' => ($content['pay_count'] * $content['pay_rate']) . ' ' . $content['pay_currency'],
-                                                       'stamp' => generateDateTime($content['pay_ordered'], 2),
-                                                       'pname' => $content['pay_name'],
+                                                       'admin_id' => $content['email'],
+                                                       'order'    => ($content['pay_count'] * $content['pay_rate']) . ' ' . $content['pay_currency'],
+                                                       'stamp'    => generateDateTime($content['pay_ordered'], 2),
+                                                       'pname'    => $content['pay_name'],
                                                );
 
                                                // Load email template
@@ -101,115 +117,107 @@ ORDER BY sp.pay_name",
                                        }
                                } else {
                                        // No payments found
-                                       $content['message'] = getMessage('SPONSOR_NO_PAYMENTS_FOUND');
+                                       $content['message'] = '{--SPONSOR_NO_PAYMENTS_FOUND--}';
                                }
 
                                // Free memory
                                SQL_FREERESULT($result);
+                       } else {
+                               // Not found
+                               displayMessage(getMaskedMessage('ADMIN_SPONSOR_ACCOUNT_404_ALREADY_CONFIRMED', $content['id']));
+                               return;
                        }
 
                        // Free memory
                        SQL_FREERESULT($result_main);
 
                        // Unlock sponsor account
-                       SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_sponsor_data` SET `status`='CONFIRMED'
-WHERE `id`='%s' AND `status`='PENDING' LIMIT 1",
+                       SQL_QUERY_ESC("UPDATE
+       `{?_MYSQL_PREFIX?}_sponsor_data`
+SET
+       `status`='CONFIRMED'
+WHERE
+       `id`=%s AND
+       `status` IN('PENDING','UNCONFIRMED')
+LIMIT 1",
                        array($content['id']), __FILE__, __LINE__);
 
                        // Update, if applyable, referal count and points
-                       if (($content['refid'] > 0) && ($content['refid'] != $content['id'])) {
+                       if ((isValidUserId($content['refid'])) && ($content['refid'] != $content['id'])) {
                                // Update referal account
                                SQL_QUERY_ESC("UPDATE
        `{?_MYSQL_PREFIX?}_sponsor_data`
 SET
        `points_amount`=`points_amount`+{?sponsor_ref_points?}, `ref_count`=`ref_count`+1
 WHERE
-       `id`='%s'
+       `id`=%s
 LIMIT 1",
                                        array(bigintval($content['refid'])), __FILE__, __LINE__);
 
                                // Whas that update fine?
-                               if (SQL_AFFECTEDROWS() == 1) {
+                               if (!SQL_HASZEROAFFECTED()) {
                                        // Load referal's data
-                                       $result = SQL_QUERY_ESC("SELECT id, gender, surname, family, email,
-(points_amount - points_used) AS points, receive_warnings, ref_count AS refs
-FROM `{?_MYSQL_PREFIX?}_sponsor_data`
-WHERE `id`='%s' LIMIT 1",
+                                       $result = SQL_QUERY_ESC("SELECT
+       `id`, `gender`, `surname`, `family`, `email`,
+       (`points_amount` - `points_used`) AS `points`,
+       `receive_warnings`, `ref_count` AS refs
+FROM
+       `{?_MYSQL_PREFIX?}_sponsor_data`
+WHERE
+       `id`=%s
+LIMIT 1",
                                        array(bigintval($content['refid'])), __FILE__, __LINE__);
                                        $REFERAL = SQL_FETCHARRAY($result);
 
                                        // Send warnings out?
                                        if ($REFERAL['receive_warnings'] == 'Y') {
-                                               // Translate some data
-                                               $REFERAL['points']     = translateComma($REFERAL['points']);
-                                               $REFERAL['ref_points'] = translateComma(getConfig('sponsor_ref_points'));
-                                               $REFERAL['gender']     = translateGender($REFERAL['gender']);
-
                                                // Send notification to referal
-                                               $REF_MSG = loadEmailTemplate('sponsor_ref_notify', $REFERAL);
-                                               sendEmail($REFERAL['email'], getMessage('SPONSOR_REF_NOTIFY_SUBJ'), $REF_MSG);
-                                       }
+                                               $REFERAL_MSG = loadEmailTemplate('sponsor_ref_notify', $REFERAL);
+                                               sendEmail($REFERAL['email'], '{--SPONSOR_REFERAL_NOTIFY_SUBJECT--}', $REFERAL_MSG);
+                                       } // END - if
 
                                        // Free memory
                                        SQL_FREERESULT($result);
-                               }
-                       }
-
-                       // Transfer data to array
-                       $content['gender']   = translateGender($content['gender']);
-                       $content['sponsor']  = $content['id'];
-                       $content['points']   = translateComma($content['points_amount']);
+                               } // END - if
+                       } // END - if
 
                        // So let's send the email away
                        $message = loadEmailTemplate('sponsor_unlocked', $content);
-                       sendEmail($content['email'], getMessage('SPONSOR_UNLOCKED_SUBJ'), $message);
-               }
+                       sendEmail($content['email'], '{--SPONSOR_UNLOCKED_SUBJECT--}', $message);
+               } // END - foreach
        } else {
                // Nothing selected
-               loadTemplate('admin_settings_saved', false, getMessage('ADMIN_SPONSOR_NONE_SELECTED_UNLOCK'));
+               displayMessage('{--ADMIN_SPONSOR_NONE_SELECTED_UNLOCK--}');
        }
-
-       // Add seperator
-       outputHtml('<br />');
-}
+} // END - if
 
 // Begin listing of all pending sponsor accounts
 $result = SQL_QUERY("SELECT
-       `id`, `gender`, `surname`, `family`, `email`, `remote_addr`, `sponsor_created`
+       `id`, `gender`, `surname`, `family`, `email`, `remote_addr`,
+       UNIX_TIMESTAMP(`sponsor_created`) AS `sponsor_created`
 FROM
        `{?_MYSQL_PREFIX?}_sponsor_data`
 WHERE
-       `status`='PENDING'
+       `status` IN('PENDING','UNCONFIRMED')
 ORDER BY
        `id`", __FILE__, __LINE__);
 
-if (SQL_NUMROWS($result) > 0) {
+if (!SQL_HASZERONUMS($result)) {
        // Entries found so let's list them!
-       $OUT = ''; $SW = 2;
+       $OUT = '';
        while ($content = SQL_FETCHARRAY($result)) {
-               // Transfer data to array
-               // @TODO Rewritings: remote->remote_addr, created->sponsor_created in template
-               $content = array(
-                       'sw'      => $SW,
-                       'id'      => $content['id'],
-                       'gender'   => translateGender($content['gender']),
-                       'surname' => $content['surname'],
-                       'family'  => $content['family'],
-                       'email'   => generateEmailLink($content['email'], 'sponsor_data'),
-                       'remote'  => $content['remote_addr'],
-                       'created' => generateDateTime($content['sponsor_created'], 2),
-               );
+               // Translate some data
+               $content['sponsor_created'] = generateDateTime($content['sponsor_created'], 2);
 
                // Load row template and switch colors
                $OUT .= loadTemplate('admin_unlock_sponsor_row', true, $content);
-               $SW = 3 - $SW;
-       }
+       } // END - while
 
        // Load template
        loadTemplate('admin_unlock_sponsor', false, $OUT);
 } else {
        // No pending accounts found
-       loadTemplate('admin_settings_saved', false, getMessage('ADMIN_SPONSOR_NONE_PENDING'));
+       displayMessage('{--ADMIN_SPONSOR_NONE_PENDING--}');
 }
 
 // Free memory