<?php

/******************************************************************************
 *
 *	PROJECT: Flynax Classifieds Software
 *	VERSION: 4.7.0
 *	LISENSE: FL4HLD2A8696 - https://www.flynax.com/flynax-software-eula.html
 *	PRODUCT: Real Estate Agency
 *	DOMAIN : immofil.com
 *	FILE   : MASSMAILER_NEWSLETTER.INC.PHP
 *
 *	This script is a commercial software and any kind of using it must be
 *	coordinate with Flynax Owners Team and be agree to Flynax License Agreement
 *
 *	This block may not be removed from this file or any other files with out
 *	permission of Flynax respective owners.
 *
 *	Copyrights Flynax Classifieds Software | 2019
 *	https://www.flynax.com
 *
 ******************************************************************************/

/* ext js action */
if ($_GET['q'] == 'ext') {
    /* system config */
    require_once '../../../includes/config.inc.php';
    require_once RL_ADMIN_CONTROL . 'ext_header.inc.php';
    require_once RL_LIBS . 'system.lib.php';

    /* date update */
    if ($_GET['action'] == 'update') {
        $reefless->loadClass('Actions');

        $type = $rlValid->xSql($_GET['type']);
        $field = $rlValid->xSql($_GET['field']);
        $value = $rlValid->xSql(nl2br($_GET['value']));
        $id = $rlValid->xSql($_GET['id']);
        $key = $rlValid->xSql($_GET['key']);

        $updateData = array(
            'fields' => array(
                $field => $value,
            ),
            'where' => array(
                'ID' => $id,
            ),
        );

        $rlActions->updateOne($updateData, 'massmailer');
        exit;
    }

    /* data read */
    $limit = $rlValid->xSql($_GET['limit']);
    $start = $rlValid->xSql($_GET['start']);

    $sql = "SELECT SQL_CALC_FOUND_ROWS * FROM `" . RL_DBPREFIX . "massmailer` WHERE `Status` <> 'trash' LIMIT {$start}, {$limit}";
    $data = $rlDb->getAll($sql);

    foreach ($data as $key => $value) {
        $data[$key]['Status'] = $GLOBALS['lang'][$data[$key]['Status']];
    }

    $count = $rlDb->getRow("SELECT FOUND_ROWS() AS `count`");

    $reefless->loadClass("Json");

    $output['total'] = $count['count'];
    $output['data'] = $data;

    echo $rlJson->encode($output);
} elseif ($_GET['q'] == 'ext2') {
    /* system config */
    require_once '../../../includes/config.inc.php';
    require_once RL_ADMIN_CONTROL . 'ext_header.inc.php';
    require_once RL_LIBS . 'system.lib.php';

    /* date update */
    if ($_GET['action'] == 'update') {
        $reefless->loadClass('Actions');

        $type = $rlValid->xSql($_GET['type']);
        $field = $rlValid->xSql($_GET['field']);
        $value = $rlValid->xSql(nl2br($_GET['value']));
        $id = $rlValid->xSql($_GET['id']);
        $key = $rlValid->xSql($_GET['key']);

        $updateData = array(
            'fields' => array(
                $field => $value,
            ),
            'where' => array(
                'ID' => $id,
            ),
        );

        $rlActions->updateOne($updateData, 'subscribers');
        exit;
    }

    /* data read */
    $limit = $rlValid->xSql($_GET['limit']);
    $start = $rlValid->xSql($_GET['start']);
    $filter_fields = array('name', 'email', 'subscribed_from', 'account_type', 'date_from', 'date_to');

    if (!empty($_GET['name'])) {
        $subscribers_filters = "LOWER(`Name`) LIKE '%" . $_GET['name'] . "%' AND ";
        $accounts_filters = "(LOWER(`First_name`) LIKE '%" . $_GET['name'] . "%' OR LOWER(`Username`) LIKE '%" . $_GET['name'] . "%') AND ";
        $contacts_filters = "LOWER(`Name`) LIKE '%" . $_GET['name'] . "%' AND ";
    }
    if (!empty($_GET['email'])) {
        $subscribers_filters .= "`Mail` LIKE '%" . $_GET['email'] . "%' AND ";
        $accounts_filters .= "`Mail` LIKE '%" . $_GET['email'] . "%' AND ";
        $contacts_filters .= "`Email` LIKE '%" . $_GET['email'] . "%' AND ";
    }
    if (!empty($_GET['account_type']) && $_GET['subscribed_from'] == 2) {
        $accounts_filters .= "`Type` = '" . $_GET['account_type'] . "' AND ";
    }
    if (!empty($_GET['date_from'])) {
        $subscribers_filters .= "UNIX_TIMESTAMP(`Date`) > UNIX_TIMESTAMP('" . $_GET['date_from'] . "') AND ";
        $accounts_filters .= "UNIX_TIMESTAMP(`Date`) > UNIX_TIMESTAMP('" . $_GET['date_from'] . "') AND ";
        $contacts_filters .= "UNIX_TIMESTAMP(`Date`) > UNIX_TIMESTAMP('" . $_GET['date_from'] . "') AND ";
    }
    if (!$_GET['date_to']) {
        $subscribers_filters .= substr($subscribers_filters, 0, -4);
        $accounts_filters .= substr($accounts_filters, 0, -4);
        $contacts_filters .= substr($contacts_filters, 0, -4);
    }
    if (!empty($_GET['date_to'])) {
        $subscribers_filters .= "UNIX_TIMESTAMP(`Date`) < UNIX_TIMESTAMP('" . $_GET['date_to'] . "') ";
        $accounts_filters .= "UNIX_TIMESTAMP(`Date`) < UNIX_TIMESTAMP('" . $_GET['date_to'] . "') ";
        $contacts_filters .= "UNIX_TIMESTAMP(`Date`) < UNIX_TIMESTAMP('" . $_GET['date_to'] . "') ";
    }
    if (!empty($subscribers_filters)) {
        $subscribers_filters = 'WHERE ' . $subscribers_filters;
    }
    if (!empty($accounts_filters)) {
        $accounts_filters = 'AND ' . $accounts_filters;
    }
    if (!empty($contacts_filters)) {
        $contacts_filters = "AND " . $contacts_filters;
    }

    if ($_GET['subscribed_from'] == 1) {
        $sql = "SELECT SQL_CALC_FOUND_ROWS `ID`, `Name`, `Mail`, `Date`, '' AS `Type`, 'Subscribers' as `subscribe_type`, `Status` FROM `" . RL_DBPREFIX . "subscribers` {$subscribers_filters} LIMIT {$start}, {$limit}";
    } elseif ($_GET['subscribed_from'] == 2) {
        $sql = "SELECT SQL_CALC_FOUND_ROWS `ID`, IF(`First_name` OR `Last_name`, CONCAT(`First_name`,' ', `Last_name`), `Username`) AS `Name`, `Mail`, `Date`, `Type`, 'Accounts' as `subscribe_type`, `Status` FROM `" . RL_DBPREFIX . "accounts` WHERE `Status` = 'active' AND `Subscribe` = '1' {$accounts_filters} LIMIT {$start}, {$limit}";
    } elseif ($_GET['subscribed_from'] == 3) {
        $sql = "SELECT SQL_CALC_FOUND_ROWS `ID`, `Name`, `Email` AS `Mail`, `Date`, '' AS `Type`, 'Contacts' as `subscribe_type`, `Status` FROM `" . RL_DBPREFIX . "contacts` WHERE `Subscribe` = '1' {$contacts_filters} LIMIT {$start}, {$limit}";
    } else {
        $sql = "SELECT SQL_CALC_FOUND_ROWS * FROM
        ((SELECT `ID`, `Name`, `Mail`, `Date`, '' AS `Type`, 'Subscribers' as `subscribe_type`, `Status` FROM `" . RL_DBPREFIX . "subscribers` {$subscribers_filters}) UNION (
            SELECT `ID` AS `sid`, IF(`First_name` OR `Last_name`, CONCAT(`First_name`,' ', `Last_name`), `Username`) AS `Name`, `Mail`, SUBSTRING_INDEX(`Date`, ' ', 1) AS `Date`, `Type`, 'Accounts' as `subscribe_type`, `Status` FROM `" . RL_DBPREFIX . "accounts` WHERE `Status` = 'active' AND `Subscribe` = '1' {$accounts_filters}) UNION (
            SELECT `ID` AS `sid`, `Name`, `Email` AS `Mail`, SUBSTRING_INDEX(`Date`, ' ', 1) AS `Date`, '' AS `Type`, 'Contacts' as `subscribe_type`, `Status` FROM `" . RL_DBPREFIX . "contacts` WHERE `Subscribe` = '1' {$contacts_filters})) AS `T1` LIMIT {$start}, {$limit}";
    }
    $data = $rlDb->getAll($sql);

    $count = $rlDb->getRow("SELECT FOUND_ROWS() AS `count`");

    $sql = "SELECT * FROM
        ((SELECT `ID`,`Mail`, 'subscriber' FROM `" . RL_DBPREFIX . "subscribers`) UNION (
            SELECT `ID`,`Mail`, 'account' FROM `" . RL_DBPREFIX . "accounts` WHERE `Status` <> 'trash' AND `Subscribe` = '1') UNION (
            SELECT `ID`,`Email` AS `Mail`, 'contact' FROM `" . RL_DBPREFIX . "contacts` WHERE `Subscribe` = '1' )) AS `T1`";

    $emails = $rlDb->getAll($sql);

    $reefless->loadClass('MassmailerNewsletter', null, 'massmailer_newsletter');
    $emails = $rlMassmailerNewsletter->arrayValueRecursive('Mail', $emails);
    $duplicate_emails = $rlMassmailerNewsletter->returndup($emails);
    foreach ($data as $key => $value) {

        if ($value['subscribe_type'] == 'Accounts') {
            $data[$key]['ID'] = $data[$key]['ID'] . '_accounts';
        } elseif ($value['subscribe_type'] == 'Contacts') {
            $data[$key]['ID'] = $data[$key]['ID'] . '_contacts';
        }

        $data[$key]['Status'] = $GLOBALS['lang'][$data[$key]['Status']];
        if ($data[$key]['subscribe_type'] == 'Subscribers') {
            $data[$key]['From'] = $lang['massmailer_newsletter_newsletter'];
            $data[$key]['dev_subscriber'] = 'subscribers';
        } elseif ($data[$key]['subscribe_type'] == 'Accounts') {
            $data[$key]['From'] = $lang['accounts'] . ' (' . $lang['account_types+name+' . $data[$key]['Type']] . ')';
            $data[$key]['dev_subscriber'] = 'accounts';
        } else {
            $data[$key]['From'] = $lang['contacts'];
            $data[$key]['dev_subscriber'] = 'contacts';
        }

        $data[$key]['Name'] = ucwords($data[$key]['Name']);

        if (in_array($data[$key]['Mail'], $duplicate_emails)) {
            $data[$key]['Mail'] = "<b style='color: red;'>" . $data[$key]['Mail'] . "</b> - <em>" . $lang['massmailer_newsletter_duplicate_email'] . '</em>';
        }
    }

    $reefless->loadClass('Json');
    $output['total'] = $count['count'];
    $output['data'] = $data;

    echo $rlJson->encode($output);
} else {
    if ($_GET['action']) {
        switch ($_GET['action']) {
            case 'add':
                $bcAStep = $lang['massmailer_newsletter_add_massmailer'];
                break;
            case 'edit':
                $bcAStep = $lang['massmailer_newsletter_edit_massmailer'];
                break;
            case 'send':
                $bcAStep = $lang['massmailer_newsletter_send'];
                break;
        }
    } elseif ($_GET['page'] == 'newsletter') {
        $bcAStep = $lang['massmailer_newsletter_newsletter'];
    }

    if ($_GET['action'] == 'add' || $_GET['action'] == 'edit') {
        /* get all languages */
        $allLangs = $GLOBALS['languages'];
        $rlSmarty->assign_by_ref('allLangs', $allLangs);

        if ($_GET['action'] == 'add' && !$_POST['fromPost']) {
            $reefless->loadClass('Mail');
            $mail_tpl = $GLOBALS['rlMail']->getEmailTemplate('massmailer_massmail_example');
            $_POST['subject'] = $mail_tpl['subject'];
            $_POST['body'] = $mail_tpl['body'];
        }

        /* get account types */
        $rlDb->setTable('account_types');
        $account_types = $rlDb->fetch(array('Key'), array('Status' => 'active'), "AND `Key` <> 'visitor'");
        $account_types = $rlLang->replaceLangKeys($account_types, 'account_types', array('name'), RL_LANG_CODE, 'admin');
        $accounts_count = 0;
        foreach ($account_types as $key => $val) {
            $ac_type_count = $rlDb->getRow("SELECT COUNT(`ID`) AS 'count' FROM `" . RL_DBPREFIX . "accounts` WHERE `Type` = '{$val['Key']}' AND `Status` = 'active'");
            $accounts_count = $accounts_count + $ac_type_count['count'];
            $account_types[$key]['count'] = $ac_type_count['count'];
        }

        $rlSmarty->assign_by_ref('account_types', $account_types);

        $other_type_count['subscribers'] = $rlDb->getRow("SELECT COUNT(`ID`) AS 'count' FROM `" . RL_DBPREFIX . "subscribers` WHERE `Status` = 'active'");
        $other_type_count['contacts'] = $rlDb->getRow("SELECT COUNT(DISTINCT `Email`) AS 'count' FROM `" . RL_DBPREFIX . "contacts` WHERE `Subscribe` = '1'");
        $other_type_count['total_accounts'] = $accounts_count;

        $rlSmarty->assign_by_ref('other_type_count', $other_type_count);

        unset($l_email_variables[3]);
        
        if ($_GET['action'] == 'edit' && !$_POST['fromPost']) {
            $massmailer = $rlDb->fetch('*', array('ID' => $_GET['massmailer']), " AND `Status` <> 'trash'", 1, 'massmailer', 'row');

            $_POST['subject'] = $massmailer['Subject'];
            $_POST['body'] = $massmailer['Body'];
            $_POST['type'] = explode(',', $massmailer['Recipients_accounts']);
            $_POST['massmailer_key'] = $massmailer['Key'];
            $_POST['status'] = $massmailer['Status'];
            $_POST['site_accounts'] = $massmailer['Recipients_accounts'];
            $_POST['newsletters_accounts'] = $massmailer['Recipients_newsletter'];
            $_POST['contact_us'] = $massmailer['Recipients_contact_us'];
        }

        if (isset($_POST['fromPost'])) {
            if ($_GET['action'] == 'add') {
                $massmailer_key = $rlValid->str2key($_POST['massmailer_key']);
                if (empty($massmailer_key)) {
                    $errors[] = str_replace('{field}', "<b>" . $lang['key'] . "</b>", $lang['notice_field_empty']);
                    $error_fields[] = 'massmailer_key';
                }
            }

            if (empty($_POST['subject'])) {
                $errors[] = str_replace('{field}', "<b>" . $lang['massmailer_newsletter_subject'] . "</b>", $lang['notice_field_empty']);
                $error_fields[] = 'subject';
            }

            if (empty($_POST['body'])) {
                $errors[] = str_replace('{field}', "<b>" . $lang['massmailer_newsletter_body'] . "</b>", $lang['notice_field_empty']);
            }

            if (!empty($errors)) {
                $rlSmarty->assign_by_ref('errors', $errors);
            } else {
                if ($_GET['action'] == 'add') {
                    $data = array(
                        'Key' => $massmailer_key,
                        'Status' => $_POST['status'],
                        'Date' => 'NOW()',
                        'Subject' => trim($_POST['subject']),
                        'Body' => trim($_POST['body']),
                        'Recipients_newsletter' => $_POST['newsletters_accounts'] ? 1 : 0,
                        'Recipients_accounts' => implode(',', $_POST['type']),
                        'Recipients_contact_us' => $_POST['contact_us'] ? 1 : 0,
                    );

                    $action = $rlActions->insertOne($data, 'massmailer');

                    $message = $lang['massmailer_newsletter_added'];
                    $aUrl = array("controller" => $controller);

                    if ($action) {
                        $reefless->loadClass('Notice');
                        $rlNotice->saveNotice($message);
                        $reefless->redirect($aUrl);
                    }
                } elseif ($_GET['action'] == 'edit') {
                    if (empty($_POST['subject'])) {
                        $errors[] = str_replace('{field}', "<b>" . $lang['massmailer_newsletter_subject'] . "</b>", $lang['notice_field_empty']);
                    }

                    if (empty($_POST['body'])) {
                        $errors[] = str_replace('{field}', "<b>" . $lang['massmailer_newsletter_body'] . "</b>", $lang['notice_field_empty']);
                    }

                    if (!empty($errors)) {
                        $rlSmarty->assign_by_ref('errors', $errors);
                    } else {
                        $update_data = array(
                            'fields' => array(
                                'Status' => $_POST['status'],
                                'Date' => 'NOW()',
                                'Subject' => trim($_POST['subject']),
                                'Body' => trim($_POST['body']),
                                'Recipients_newsletter' => $_POST['newsletters_accounts'] ? 1 : 0,
                                'Recipients_accounts' => implode(',', $_POST['type']),
                                'Recipients_contact_us' => $_POST['contact_us'] ? 1 : 0,
                            ),
                            'where' => array('ID' => $_GET['massmailer']),
                        );

                        $action = $rlActions->updateOne($update_data, 'massmailer');

                        $message = $lang['massmailer_newsletter_edited'];
                        $aUrl = array("controller" => $controller);
                        if ($action) {
                            $reefless->loadClass('Notice');
                            $rlNotice->saveNotice($message);
                            $reefless->redirect($aUrl);
                        }
                    }
                }
            }
        }
    } elseif ($_GET['action'] == 'send') {
        $massmailer = $rlDb->fetch('*', array('ID' => $_GET['massmailer']), " AND `Status` <> 'trash'", 1, 'massmailer', 'row');
        if ($massmailer['Recipients_newsletter']) {
            $massmailer['Recipients_newsletter_count'] = $rlDb->getRow("SELECT COUNT(`ID`) AS `Count` FROM `" . RL_DBPREFIX . "subscribers` WHERE `Status` = 'active'");
            $massmailer['Recipients_newsletter_emails'] = $rlDb->getAll("SELECT `Mail` FROM `" . RL_DBPREFIX . "subscribers` WHERE `Status` = 'active'");
        }
        if ($massmailer['Recipients_contact_us']) {
            $massmailer['Recipients_contact_us_count'] = $rlDb->getRow("SELECT COUNT(DISTINCT `Email`) AS `Count` FROM `" . RL_DBPREFIX . "contacts` WHERE `Status` <> 'trash' AND `Subscribe` = '1'");
            $massmailer['Recipients_contact_us_emails'] = $rlDb->getAll("SELECT `Email` AS `Mail` FROM `" . RL_DBPREFIX . "contacts` WHERE `Status` <> 'trash' AND `Subscribe` = '1'");
        }
        //get accounts
        if (!empty($massmailer['Recipients_accounts'])) {
            $account_types = explode(",", $massmailer['Recipients_accounts']);
            foreach ($account_types as $key => $val) {
                $account_type[]['Key'] = $val;
                $count = $rlDb->getRow("SELECT COUNT(`ID`) AS `Count` FROM `" . RL_DBPREFIX . "accounts` WHERE `Status` = 'active' AND `Type` = '{$val}' AND `Subscribe` = '1'");
                $massmailer['Recipients_accounts_count'][$val] = $count['Count'];

                $massmailer['Recipients_accounts_emails'][$val] = $rlDb->getAll("SELECT `Mail` FROM `" . RL_DBPREFIX . "accounts` WHERE `Status` = 'active' AND `Type` = '{$val}' AND `Subscribe` = '1'");
            }
            $account_types = $rlLang->replaceLangKeys($account_type, 'account_types', array('name'), RL_LANG_CODE, 'admin');

            $massmailer['Recipients_accounts'] = $account_types;
        }
        $rlSmarty->assign_by_ref('massmailer_form', $massmailer);
    }
    /* register ajax methods */
    $reefless->loadClass('MassmailerNewsletter', null, 'massmailer_newsletter');

    $rlXajax->registerFunction(array('deleteMassmailerNewsletter', $rlMassmailerNewsletter, 'ajaxDeleteMassmailerNewsletter'));
    $rlXajax->registerFunction(array('deleteNewsletter', $rlMassmailerNewsletter, 'ajaxDeleteNewsletter'));
    $rlXajax->registerFunction(array('massmailerSave', $rlMassmailerNewsletter, 'ajaxMassmailerSave'));

    if ($_GET['page'] == 'newsletter') {
        $account_types = $rlDb->getAll("SELECT `ID`, `Key` FROM `" . RL_DBPREFIX . "account_types` WHERE `Status` = 'active'");
        foreach ($account_types as $key => $val) {
            $account_types[$key]['name'] = $lang['account_types+name+' . $val['Key']];
        }
        $rlSmarty->assign_by_ref('account_types', $account_types);

        $rlXajax->registerFunction(array('deleteSubscriber', $rlMassmailerNewsletter, 'ajaxDeleteSubscrider'));
    }
}
