<?php
/**
 * @package     Joomla.Administrator
 * @subpackage  com_miniorange_importexportusers
 *
 * @copyright   Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 */
/**
 * AccountSetup Controller
 *
 * @package     Joomla.Administrator
 * @subpackage  com_miniorange_importexportusers
 * @since       0.0.9
 */
defined('_JEXEC') or die('Restricted access');

class MiniorangeImportExportUsersControllerAccountsetup extends JControllerForm
{
    function __construct()
    {
        $this->view_list = 'accountsetup';
        parent::__construct();
    }

    function saveOTP()
    {
        $db = JFactory::getDbo();
        $query = $db->getQuery(true);
        $tab = JFactory::getApplication()->input->post->getArray()['otp'];

        $fields = array(
            $db->quoteName('enable_otp') . ' = ' . $db->quote(isset($tab) ? $tab : 0));

        $conditions = array($db->quoteName('id') . ' = 1');

        $query->update($db->quoteName('#__miniorange_importexport_customer_details'))->set($fields)->where($conditions);
        $db->setQuery($query);
        $result = $db->execute();
        $this->setRedirect('index.php?option=com_miniorange_importexportusers&tab=configuration', 'Your configuration has been saved successfully.');

    }

    function customerLoginForm()
    {
        $db = JFactory::getDbo();
        $query = $db->getQuery(true);
        // Fields to update.
        $fields = array(
            $db->quoteName('login_status') . ' = ' . $db->quote(1),
            $db->quoteName('password') . ' = ' . $db->quote(''),
            $db->quoteName('email_count') . ' = ' . $db->quote(0),
            $db->quoteName('sms_count') . ' = ' . $db->quote(0),
        );

        // Conditions for which records should be updated.
        $conditions = array(
            $db->quoteName('id') . ' = 1'
        );

        $query->update($db->quoteName('#__miniorange_importexport_customer_details'))->set($fields)->where($conditions);
        $db->setQuery($query);
        $db->execute();
        $this->setRedirect('index.php?option=com_miniorange_importexportusers&view=accountsetup&tab-panel=account');
    }

    function verifyCustomer()
    {
        $post = JFactory::getApplication()->input->post->getArray();

        $email = '';
        $password = '';

        if (MoImportExportUtility::check_empty_or_null($post['email']) || MoImportExportUtility::check_empty_or_null($post['password'])) {
            JFactory::getApplication()->enqueueMessage(4711, 'All the fields are required. Please enter valid entries.');
            return;
        } else {
            $email = $post['email'];
            $password = $post['password'];
        }

        $customer = new MoImportExportCustomer();
        $content = $customer->get_customer_key($email, $password);

        $customerKey = json_decode($content, true);
        if (strcasecmp($customerKey['apiKey'], 'CURL_ERROR') == 0) {
            $this->setRedirect('index.php?option=com_miniorange_importexportusers&view=accountsetup', $customerKey['token'], 'error');
        } else if (json_last_error() == JSON_ERROR_NONE) {
            if (isset($customerKey['id']) && isset($customerKey['apiKey']) && !empty($customerKey['id']) && !empty($customerKey['apiKey'])) {
                $this->save_customer_configurations($email, $customerKey['id'], $customerKey['apiKey'], $customerKey['token'], $customerKey['phone']);
                $this->setRedirect('index.php?option=com_miniorange_importexportusers&view=accountsetup&tab-panel=license_page', 'Your account has been retrieved successfully. Now you can upgrade your plan.');
            } else {
                $this->setRedirect('index.php?option=com_miniorange_importexportusers&view=accountsetup', 'There was an error in fetching your details. Please try again.', 'error');
            }
        } else {
            $this->setRedirect('index.php?option=com_miniorange_importexportusers&view=accountsetup&tab-panel=account', 'Invalid username or password. Please try again.', 'error');
        }
    }

    function save_customer_configurations($email, $id, $apiKey, $token, $phone)
    {
        $db = JFactory::getDbo();
        $query = $db->getQuery(true);
        // Fields to update.
        $fields = array(
            $db->quoteName('email') . ' = ' . $db->quote($email),
            $db->quoteName('customer_key') . ' = ' . $db->quote($id),
            $db->quoteName('api_key') . ' = ' . $db->quote($apiKey),
            $db->quoteName('customer_token') . ' = ' . $db->quote($token),
            $db->quoteName('admin_phone') . ' = ' . $db->quote($phone),
            $db->quoteName('login_status') . ' = ' . $db->quote(0),
            $db->quoteName('registration_status') . ' = ' . $db->quote('SUCCESS'),
            $db->quoteName('password') . ' = ' . $db->quote(''),
            $db->quoteName('email_count') . ' = ' . $db->quote(0),
            $db->quoteName('sms_count') . ' = ' . $db->quote(0),
        );

        // Conditions for which records should be updated.
        $conditions = array(
            $db->quoteName('id') . ' = 1'
        );

        $query->update($db->quoteName('#__miniorange_importexport_customer_details'))->set($fields)->where($conditions);
        $db->setQuery($query);
        $result = $db->execute();
    }

    function ResetAccount()
    {
        $db = JFactory::getDbo();
        $query = $db->getQuery(true);
        $fields = array(
            $db->quoteName('email') . ' = ' . $db->quote(''),
            $db->quoteName('password') . ' = ' . $db->quote(''),
            $db->quoteName('customer_key') . ' = ' . $db->quote(''),
            $db->quoteName('admin_phone') . ' = ' . $db->quote(''),
            $db->quoteName('customer_token') . ' = ' . $db->quote(''),
            $db->quoteName('api_key') . ' = ' . $db->quote(''),
            $db->quoteName('registration_status') . ' = ' . $db->quote(''),
            $db->quoteName('login_status') . ' = ' . $db->quote(0),
            $db->quoteName('transaction_id') . ' = ' . $db->quote(''),
            $db->quoteName('email_count') . ' = ' . $db->quote(0),
            $db->quoteName('sms_count') . ' = ' . $db->quote(0),
        );
        // Conditions for which records should be updated.
        $conditions = array(
            $db->quoteName('id') . ' = 1'
        );

        $query->update($db->quoteName('#__miniorange_importexport_customer_details'))->set($fields)->where($conditions);
        $db->setQuery($query);
        $db->execute();
        $this->setRedirect('index.php?option=com_miniorange_importexportusers&view=accountsetup&tab-panel=account');
    }

    function exportUsers()
    {
        $post = JFactory::getApplication()->input->post->getArray();

        if (count($post) == 0) {
            $this->setRedirect('index.php?option=com_miniorange_importexportusers&view=accountsetup&tab-panel=exportsettings');
            return;
        }

        $enable_export_users = isset($post['enable_export_users']) ? $post['enable_export_users'] : 0;

        MoImportExportUtility::updateDBValue('#__miniorange_exportusers', 'enable_export_users', $enable_export_users);

        $message = 'Export Users configuration has been saved successfully.';
        $this->setRedirect('index.php?option=com_miniorange_importexportusers&view=accountsetup&tab-panel=exportsettings', $message);

        $user_data = MoImportExportUtility::readUsersTableData();

        if(isset($post['export_groups']) && $post['export_groups'] == 'Export Groups')
        {
            MoImportExportUtility::exportUserGroups();
        }
        $this->download_file($user_data);
    }

    function download_file($user_data)
    {
        MoImportExportUtility::downloadCSVFile($user_data);
    }

    function registerCustomer()
    {
        //validate and sanitize

        $email = '';
        $phone = '';
        $password = '';
        $confirmPassword = '';


        $password = (JFactory::getApplication()->input->post->getArray()["password"]);
        $confirmPassword = (JFactory::getApplication()->input->post->getArray()["confirmPassword"]);

        $email = (JFactory::getApplication()->input->post->getArray()["email"]);

        if (MoImportExportUtility::check_empty_or_null($email) || MoImportExportUtility::check_empty_or_null($password) || MoImportExportUtility::check_empty_or_null($confirmPassword)) {
            $this->setRedirect('index.php?option=com_miniorange_importexportusers&view=accountsetup&tab-panel=account', 'All the fields are required. Please enter valid entries.', 'error');
            return;
        } else if (strlen($password) < 6 || strlen($confirmPassword) < 6) {    //check password is of minimum length 6
            $this->setRedirect('index.php?option=com_miniorange_importexportusers&view=accountsetup&tab-panel=account', 'Choose a password with minimum length 6.', 'error');
            return;
        } else {
            $email = JFactory::getApplication()->input->post->getArray()["email"];
            $email = strtolower($email);
            $phone = JFactory::getApplication()->input->post->getArray()["phone"];
            $password = JFactory::getApplication()->input->post->getArray()["password"];
            $confirmPassword = JFactory::getApplication()->input->post->getArray()["confirmPassword"];
        }

        if (strcmp($password, $confirmPassword) == 0) {

            $db = JFactory::getDbo();
            $query = $db->getQuery(true);
            // Fields to update.
            $fields = array(
                $db->quoteName('email') . ' = ' . $db->quote($email),
                $db->quoteName('admin_phone') . ' = ' . $db->quote($phone),
                $db->quoteName('password') . ' = ' . $db->quote($password),

            );

            // Conditions for which records should be updated.
            $conditions = array(
                $db->quoteName('id') . ' = 1'
            );

            $query->update($db->quoteName('#__miniorange_importexport_customer_details'))->set($fields)->where($conditions);
            $db->setQuery($query);
            $result = $db->execute();

            $customer = new MoImportExportCustomer();
            $content = json_decode($customer->check_customer($email), true);
            if (strcasecmp($content['status'], 'CUSTOMER_NOT_FOUND') == 0) {
                $auth_type = 'EMAIL';
                $content = json_decode($customer->send_otp_token($auth_type, $email), true);
                if (strcasecmp($content['status'], 'SUCCESS') == 0) {

                    $db = JFactory::getDbo();
                    $query = $db->getQuery(true);
                    $fields = array(
                        $db->quoteName('email_count') . ' = ' . $db->quote(1),
                        $db->quoteName('transaction_id') . ' = ' . $db->quote($content['txId']),
                        $db->quoteName('login_status') . ' = ' . $db->quote(0),
                        $db->quoteName('registration_status') . ' = ' . $db->quote('MO_OTP_DELIVERED_SUCCESS')
                    );
                    // Conditions for which records should be updated.
                    $conditions = array(
                        $db->quoteName('id') . ' = 1'
                    );

                    $query->update($db->quoteName('#__miniorange_importexport_customer_details'))->set($fields)->where($conditions);
                    $db->setQuery($query);
                    $result = $db->execute();

                    $this->setRedirect('index.php?option=com_miniorange_importexportusers&view=accountsetup&tab-panel=account', 'A One Time Passcode has been sent <b>( 1 )</b> to <b>' . $email . '</b>. Please enter the OTP below to verify your email. ');


                } else {

                    $db = JFactory::getDbo();
                    $query = $db->getQuery(true);
                    $fields = array(
                        $db->quoteName('login_status') . ' = ' . $db->quote(0),
                        $db->quoteName('registration_status') . ' = ' . $db->quote('MO_OTP_DELIVERED_FAILURE')
                    );
                    // Conditions for which records should be updated.
                    $conditions = array(
                        $db->quoteName('id') . ' = 1'
                    );

                    $query->update($db->quoteName('#__miniorange_importexport_customer_details'))->set($fields)->where($conditions);
                    $db->setQuery($query);
                    $result = $db->execute();

                    $this->setRedirect('index.php?option=com_miniorange_importexportusers&view=accountsetup&tab-panel=account', 'There was an error in sending email. Please click on Resend OTP to try again. ', 'error');


                }
            } else if (strcasecmp($content['status'], 'CURL_ERROR') == 0) {

                $db = JFactory::getDbo();
                $query = $db->getQuery(true);
                $fields = array(
                    $db->quoteName('login_status') . ' = ' . $db->quote(0),
                    $db->quoteName('registration_status') . ' = ' . $db->quote('MO_OTP_DELIVERED_FAILURE')
                );
                // Conditions for which records should be updated.
                $conditions = array(
                    $db->quoteName('id') . ' = 1'
                );

                $query->update($db->quoteName('#__miniorange_importexport_customer_details'))->set($fields)->where($conditions);
                $db->setQuery($query);
                $result = $db->execute();

                $this->setRedirect('index.php?option=com_miniorange_importexportusers&view=accountsetup', $content['statusMessage'], 'error');

            } else {
                $content = $customer->get_customer_key($email, $password);
                $customerKey = json_decode($content, true);
                if (json_last_error() == JSON_ERROR_NONE) {
                    $this->save_customer_configurations($email, $customerKey['id'], $customerKey['apiKey'], $customerKey['token'], $customerKey['phone']);
                    $this->setRedirect('index.php?option=com_miniorange_importexportusers&view=accountsetup&tab-panel=account', 'Your account has been retrieved successfully, now you can <strong>upgrade</strong> to licensed version.');
                } else {
                    $db = JFactory::getDbo();
                    $query = $db->getQuery(true);
                    $fields = array(
                        $db->quoteName('login_status') . ' = ' . $db->quote(1),
                        $db->quoteName('registration_status') . ' = ' . $db->quote(0)
                    );
                    // Conditions for which records should be updated.
                    $conditions = array(
                        $db->quoteName('id') . ' = 1'
                    );

                    $query->update($db->quoteName('#__miniorange_importexport_customer_details'))->set($fields)->where($conditions);
                    $db->setQuery($query);
                    $result = $db->execute();

                    $this->setRedirect('index.php?option=com_miniorange_importexportusers&view=accountsetup&tab-panel=account', 'You already have an account with miniOrange. Please enter a valid password. ', 'error');

                }
            }

        } else {
            $db = JFactory::getDbo();
            $query = $db->getQuery(true);
            $fields = array(
                $db->quoteName('login_status') . ' = ' . $db->quote(0)
            );
            // Conditions for which records should be updated.
            $conditions = array(
                $db->quoteName('id') . ' = 1'
            );

            $query->update($db->quoteName('#__miniorange_importexport_customer_details'))->set($fields)->where($conditions);
            $db->setQuery($query);
            $result = $db->execute();
            $this->setRedirect('index.php?option=com_miniorange_importexportusers&view=accountsetup&tab-panel=account', 'Password and Confirm password do not match.', 'error');
        }
    }

    function validateOtp()
    {

        $otp_token = JFactory::getApplication()->input->post->getArray()["otp_token"];
        //validation and sanitization
        //$otp_token = '';
        if (MoImportExportUtility::check_empty_or_null($otp_token)) {
            $this->setRedirect('index.php?option=com_miniorange_importexportusers&view=accountsetup&tab-panel=account', 'Please enter a valid OTP.', 'error');
            return;
        }

        $db = JFactory::getDbo();
        $query = $db->getQuery(true);
        $query->select('transaction_id');
        $query->from($db->quoteName('#__miniorange_importexport_customer_details'));
        $query->where($db->quoteName('id') . " = 1");

        $db->setQuery($query);
        $transaction_id = $db->loadResult();

        $customer = new MoImportExportCustomer();
        $content = json_decode($customer->validate_otp_token($transaction_id, $otp_token), true);
        if (strcasecmp($content['status'], 'SUCCESS') == 0) {
            $customerKey = json_decode($customer->create_customer(), true);

            $db = JFactory::getDbo();
            $query = $db->getQuery(true);
            $fields = array(
                $db->quoteName('email_count') . ' = ' . $db->quote(0),
                $db->quoteName('sms_count') . ' = ' . $db->quote(0)
            );
            // Conditions for which records should be updated.
            $conditions = array(
                $db->quoteName('id') . ' = 1'
            );
            $query->update($db->quoteName('#__miniorange_importexport_customer_details'))->set($fields)->where($conditions);
            $db->setQuery($query);
            $result = $db->execute();

            if (strcasecmp($customerKey['status'], 'CUSTOMER_USERNAME_ALREADY_EXISTS') == 0) {    //admin already exists in miniOrange

                if (json_last_error() == JSON_ERROR_NONE) {
                    $this->save_customer_configurations($customerKey['email'], $customerKey['id'], $customerKey['apiKey'], $customerKey['token'], $customerKey['phone']);
                    $this->setRedirect('index.php?option=com_miniorange_importexportusers&view=accountsetup&tab-panel=account', 'Your account has been retrieved successfully, now you can <strong>upgrade</strong> to licensed version.');
                } else {
                    $db = JFactory::getDbo();
                    $query = $db->getQuery(true);
                    $fields = array(
                        $db->quoteName('login_status') . ' = ' . $db->quote(1),
                        $db->quoteName('password') . ' = ' . $db->quote(''),
                    );
                    // Conditions for which records should be updated.
                    $conditions = array(
                        $db->quoteName('id') . ' = 1'
                    );

                    $query->update($db->quoteName('#__miniorange_importexport_customer_details'))->set($fields)->where($conditions);
                    $db->setQuery($query);
                    $result = $db->execute();

                    $this->setRedirect('index.php?option=com_miniorange_importexportusers&view=accountsetup&tab-panel=account', 'You already have an account with miniOrange. Please enter a valid password.', 'error');

                }
            } else if (strcasecmp($customerKey['status'], 'SUCCESS') == 0) {

                //registration successful
                $this->save_customer_configurations($customerKey['email'], $customerKey['id'], $customerKey['apiKey'], $customerKey['token'], $customerKey['phone']);
                $this->setRedirect('index.php?option=com_miniorange_importexportusers&view=accountsetup&tab-panel=license_page', 'Your account has been created successfully.');
            } else if (strcmp($customerKey['message'], 'Email is not enterprise email.') == 0) {

                $db = JFactory::getDbo();
                $query = $db->getQuery(true);
                $fields = array(
                    $db->quoteName('registration_status') . ' = ' . $db->quote(''),
                    $db->quoteName('email') . ' = ' . $db->quote(''),
                    $db->quoteName('password') . ' = ' . $db->quote(''),
                    $db->quoteName('transaction_id') . ' = ' . $db->quote(''),
                );
                // Conditions for which records should be updated.
                $conditions = array(
                    $db->quoteName('id') . ' = 1'
                );

                $query->update($db->quoteName('#__miniorange_importexport_customer_details'))->set($fields)->where($conditions);
                $db->setQuery($query);
                $result = $db->execute();

                $this->setRedirect('index.php?option=com_miniorange_importexportusers&view=accountsetup&tab-panel=account', 'There was an error creating an account for you. You may have entered an invalid Email-Id. <br><b>(We discourage the use of disposable emails)</b><br>
												Please try again with a valid email.', 'error');

            }
            //update_option('mo_usync_local_password', '');
        } else if (strcasecmp($content['status'], 'CURL_ERROR') == 0) {
            $db = JFactory::getDbo();
            $query = $db->getQuery(true);
            $fields = array(
                $db->quoteName('registration_status') . ' = ' . $db->quote('MO_OTP_VALIDATION_FAILURE')
            );
            // Conditions for which records should be updated.
            $conditions = array(
                $db->quoteName('id') . ' = 1'
            );

            $query->update($db->quoteName('#__miniorange_importexport_customer_details'))->set($fields)->where($conditions);
            $db->setQuery($query);
            $result = $db->execute();
            $this->setRedirect('index.php?option=com_miniorange_importexportusers&view=accountsetup&tab-panel=account', $content['statusMessage'], 'error');

        } else {
            $db = JFactory::getDbo();
            $query = $db->getQuery(true);
            $fields = array(
                $db->quoteName('registration_status') . ' = ' . $db->quote('MO_OTP_VALIDATION_FAILURE')
            );
            // Conditions for which records should be updated.
            $conditions = array(
                $db->quoteName('id') . ' = 1'
            );

            $query->update($db->quoteName('#__miniorange_importexport_customer_details'))->set($fields)->where($conditions);
            $db->setQuery($query);
            $result = $db->execute();
            $this->setRedirect('index.php?option=com_miniorange_importexportusers&view=accountsetup&tab-panel=account', 'Invalid one time passcode. Please enter a valid OTP.', 'error');

        }
    }

    function resendOtp()
    {
        $customer = new MoImportExportCustomer();
        $auth_type = 'EMAIL';

        $db = JFactory::getDbo();
        $query = $db->getQuery(true);
        $query->select('email');
        $query->from($db->quoteName('#__miniorange_importexport_customer_details'));
        $query->where($db->quoteName('id') . " = 1");

        $db->setQuery($query);
        $email = $db->loadResult();

        $content = json_decode($customer->send_otp_token($auth_type, $email), true);
        if (strcasecmp($content['status'], 'SUCCESS') == 0) {

            $customer_details = MoImportExportUtility::getCustomerDetails();
            $email_count = $customer_details['email_count'];
            $admin_email = $customer_details['email'];

            if ($email_count != '' && $email_count >= 1) {
                $email_count = $email_count + 1;

                $db = JFactory::getDbo();
                $query = $db->getQuery(true);
                $fields = array(
                    $db->quoteName('email_count') . ' = ' . $db->quote($email_count),
                    $db->quoteName('transaction_id') . ' = ' . $db->quote($content['txId']),
                    $db->quoteName('registration_status') . ' = ' . $db->quote('MO_OTP_DELIVERED_SUCCESS')
                );
                // Conditions for which records should be updated.
                $conditions = array(
                    $db->quoteName('id') . ' = 1'
                );

                $query->update($db->quoteName('#__miniorange_importexport_customer_details'))->set($fields)->where($conditions);
                $db->setQuery($query);
                $result = $db->execute();

                $this->setRedirect('index.php?option=com_miniorange_importexportusers&view=accountsetup&tab-panel=account', 'Another One Time Passcode has been sent <b>( ' . $email_count . ' )</b> to <b>' . ($admin_email) . '</b>. Please enter the OTP below to verify your email.');

            } else {
                $db = JFactory::getDbo();
                $query = $db->getQuery(true);
                $fields = array(
                    $db->quoteName('email_count') . ' = ' . $db->quote(1),
                    $db->quoteName('transaction_id') . ' = ' . $db->quote($content['txId']),
                    $db->quoteName('registration_status') . ' = ' . $db->quote('MO_OTP_DELIVERED_SUCCESS')
                );
                // Conditions for which records should be updated.
                $conditions = array(
                    $db->quoteName('id') . ' = 1'
                );

                $query->update($db->quoteName('#__miniorange_importexport_customer_details'))->set($fields)->where($conditions);
                $db->setQuery($query);
                $result = $db->execute();
                $this->setRedirect('index.php?option=com_miniorange_importexportusers&view=accountsetup&tab-panel=account', 'An OTP has been sent to <b>' . ($admin_email) . '</b>. Please enter the OTP below to verify your email.');

            }

        } else if (strcasecmp($content['status'], 'CURL_ERROR') == 0) {

            $db = JFactory::getDbo();
            $query = $db->getQuery(true);
            $fields = array(
                $db->quoteName('registration_status') . ' = ' . $db->quote('MO_OTP_DELIVERED_FAILURE')
            );
            // Conditions for which records should be updated.
            $conditions = array(
                $db->quoteName('id') . ' = 1'
            );

            $query->update($db->quoteName('#__miniorange_importexport_customer_details'))->set($fields)->where($conditions);
            $db->setQuery($query);
            $result = $db->execute();
            $this->setRedirect('index.php?option=com_miniorange_importexportusers&view=accountsetup', $content['statusMessage'], 'error');

        } else {
            $db = JFactory::getDbo();
            $query = $db->getQuery(true);
            $fields = array(
                $db->quoteName('registration_status') . ' = ' . $db->quote('MO_OTP_DELIVERED_FAILURE')
            );
            // Conditions for which records should be updated.
            $conditions = array(
                $db->quoteName('id') . ' = 1'
            );

            $query->update($db->quoteName('#__miniorange_importexport_customer_details'))->set($fields)->where($conditions);
            $db->setQuery($query);
            $result = $db->execute();
            $this->setRedirect('index.php?option=com_miniorange_importexportusers&view=accountsetup', 'There was an error in sending email. Please click on Resend OTP to try again.', 'error');

        }
    }

    function cancelform()
    {
        $db = JFactory::getDbo();
        $query = $db->getQuery(true);
        $fields = array(
            $db->quoteName('email') . ' = ' . $db->quote(''),
            $db->quoteName('registration_status') . ' = ' . $db->quote(''),
            $db->quoteName('login_status') . ' = ' . $db->quote(0),
            $db->quoteName('email_count') . ' = ' . $db->quote(0),
            $db->quoteName('sms_count') . ' = ' . $db->quote(0),
        );
        // Conditions for which records should be updated.
        $conditions = array(
            $db->quoteName('id') . ' = 1'
        );

        $query->update($db->quoteName('#__miniorange_importexport_customer_details'))->set($fields)->where($conditions);
        $db->setQuery($query);
        $result = $db->execute();
        $this->setRedirect('index.php?option=com_miniorange_importexportusers&view=accountsetup&tab-panel=account');

    }

    function phoneVerification()
    {
        $phone = JFactory::getApplication()->input->post->getArray()['phone_number'];
        $phone = str_replace(' ', '', $phone);

        $pattern = "/[\+][0-9]{1,3}[0-9]{10}/";

        if (preg_match($pattern, $phone, $matches, PREG_OFFSET_CAPTURE)) {
            $auth_type = 'SMS';
            $customer = new MoImportExportCustomer();
            $send_otp_response = json_decode($customer->send_otp_token($auth_type, $phone));
            if ($send_otp_response->status == 'SUCCESS') {

                $db = JFactory::getDbo();
                $query = $db->getQuery(true);
                $query->select('sms_count');
                $query->from($db->quoteName('#__miniorange_importexport_customer_details'));
                $query->where($db->quoteName('id') . " = 1");

                $db->setQuery($query);
                $sms_count = $db->loadResult();

                if ($sms_count != '' && $sms_count >= 1) {
                    $sms_count = $sms_count + 1;
                    $db = JFactory::getDbo();
                    $query = $db->getQuery(true);
                    $fields = array(
                        $db->quoteName('sms_count') . ' = ' . $db->quote($sms_count),
                        $db->quoteName('transaction_id') . ' = ' . $db->quote($send_otp_response->txId)
                    );
                    // Conditions for which records should be updated.
                    $conditions = array(
                        $db->quoteName('id') . ' = 1'
                    );

                    $query->update($db->quoteName('#__miniorange_importexport_customer_details'))->set($fields)->where($conditions);
                    $db->setQuery($query);
                    $result = $db->execute();

                    $this->setRedirect('index.php?option=com_miniorange_importexportusers&view=accountsetup&tab-panel=account', 'Another One Time Passcode has been sent <b>(' . $sms_count . ')</b> for verification to ' . $phone);


                } else {
                    $db = JFactory::getDbo();
                    $query = $db->getQuery(true);
                    $fields = array(
                        $db->quoteName('sms_count') . ' = ' . $db->quote(1),
                        $db->quoteName('transaction_id') . ' = ' . $db->quote($send_otp_response->txId)
                    );
                    // Conditions for which records should be updated.
                    $conditions = array(
                        $db->quoteName('id') . ' = 1'
                    );

                    $query->update($db->quoteName('#__miniorange_importexport_customer_details'))->set($fields)->where($conditions);
                    $db->setQuery($query);
                    $result = $db->execute();
                    $this->setRedirect('index.php?option=com_miniorange_importexportusers&view=accountsetup&tab-panel=account', 'A One Time Passcode has been sent ( <b>1</b> ) for verification to ' . $phone);
                }

            } else {
                $this->setRedirect('index.php?option=com_miniorange_importexportusers&view=accountsetup&tab-panel=account', 'An error occurred while sending OTP to phone. Please try again.');
            }
        } else {

            $this->setRedirect('index.php?option=com_miniorange_importexportusers&view=accountsetup&tab-panel=account', 'Please enter the phone number in the following format: <b>+##country code## ##phone number##', 'error');
        }
    }

    function contactUs()
    {

        $post = JFactory::getApplication()->input->post->getArray();

        if (count($post) == 0) {
            $this->setRedirect('index.php?option=com_miniorange_importexportusers&view=accountsetup');
            return;
        }

        $query_email = isset($post['query_email']) ? $post['query_email'] : '';
        $query = isset($post['query_support']) ? $post['query_support'] : '';
        $phone = isset($post['query_phone']) ? $post['query_phone'] : '';

        if (MoImportExportUtility::check_empty_or_null($query_email) || MoImportExportUtility::check_empty_or_null($query)) {
            $this->setRedirect('index.php?option=com_miniorange_importexportusers&view=accountsetup', 'Please submit your query with email.', 'error');
            return;
        } else {
            $contact_us = new MoImportExportCustomer();
            $submited = json_decode($contact_us->submit_contact_us($query_email, $phone, $query), true);
            if (json_last_error() == JSON_ERROR_NONE) {
                if (is_array($submited) && array_key_exists('status', $submited) && $submited['status'] == 'ERROR') {
                    $this->setRedirect('index.php?option=com_miniorange_importexportusers&view=accountsetup', $submited['message'], 'error');
                } else {
                    if ($submited == false) {
                        $this->setRedirect('index.php?option=com_miniorange_importexportusers&view=accountsetup', 'Your query could not be submitted. Please try again.', 'error');
                    } else {
                        $this->setRedirect('index.php?option=com_miniorange_importexportusers&view=accountsetup', 'Thanks for getting in touch! We will get back to you shortly.');
                    }
                }
            }
        }
    }
}