<?php
/**
 * SimpleForm2
 *
 * @package SimpleForm2
 * @author ZyX (allforjoomla.ru)
 * @copyright (C) 2010-2019 by ZyX (http://www.allforjoomla.ru)
 * @license: http://www.allforjoomla.ru/license
 *
 */
defined('_JEXEC') or die(':)');

class plgSystemSimpleform2 extends JPlugin {

    protected $_allowedTasks = array('sf2-send', 'sf2-captcha', 'sf2-getrecord', 'sf2-delrecord');

    public function onAfterInitialise() {
        $app = JFactory::getApplication();
        $action = $app->input->get('action', '');

        if (!in_array($action, $this->_allowedTasks)) {
            return true;
        }

        $this->_loadLang();
        JLoader::register('SF2Helper', JPATH_ROOT . '/modules/mod_simpleform2/helpers/sf2.php');
        JLoader::register('simpleForm2', JPATH_ROOT . '/modules/mod_simpleform2/models/form.php');
        JLoader::register('simpleForm2Element', JPATH_ROOT . '/modules/mod_simpleform2/models/form.element.php');
        JLoader::register('simpleForm2BaseModel', JPATH_ROOT . '/modules/mod_simpleform2/models/base.php');

        if ($this->_isAdmin()) {
            $this->_processBackendRequests();
        } else {
            $this->_processFrontendRequests();
        }
    }
    
    private function _isAdmin(){
        $app = JFactory::getApplication();
        return ((method_exists($app, 'isAdmin') && $app->isAdmin()) || (method_exists($app, 'isClient') && $app->isClient('administrator')));
    }

    private function _processBackendRequests() {
        $app = JFactory::getApplication();
        $action = $app->input->get('action', '');

        if ($action != 'sf2-getrecord' && $action != 'sf2-delrecord') {
            return true;
        }

        $responce = array(
            'status' => 'error',
            'message' => JText::_('MOD_SIMPLEFORM2_ERROR')
        );
        try {
            $db = JFactory::getDbo();
            $recordID = (int) $app->input->getInt('id', 0);

            switch ($action) {
                case 'sf2-getrecord':
                    $q = $db->getQuery(true);
                    $q->select('*');
                    $q->from($db->quoteName('#__sf2_records'));
                    $q->where($db->quoteName('id') . '=' . $recordID);
                    $db->setQuery($q);
                    $record = $db->loadAssoc();
                    if (is_array($record) && isset($record['id']) && (int) $record['id'] == $recordID) {
                        preg_match('~<body>(.*?)(?=</body>)~is', $record['message'], $match);
                        $responce = array(
                            'status' => 'success',
                            'html' => $match[1]
                        );
                    } else {
                        throw new \Exception(JText::_('MOD_SIMPLEFORM2_RECORD_NOT_FOUND'));
                    }
                    break;
                case 'sf2-delrecord':
                    $q = $db->getQuery(true);
                    $q->delete();
                    $q->from($db->quoteName('#__sf2_records'));
                    $q->where($db->quoteName('id') . '=' . $recordID);
                    $db->setQuery($q);
                    $db->execute();
                    $responce = array(
                        'status' => 'success',
                    );
                    break;
            }
        } catch (\Exception $e) {
            $responce = array(
                'status' => 'error',
                'message' => $e->getMessage()
            );
        }
        header('Content-Type: application/json; charset="utf-8"', true);
        echo json_encode($responce);
        $app->close();
    }

    private function _processFrontendRequests() {
        $app = JFactory::getApplication();
        $action = $app->input->get('action', '');

        if ($action != 'sf2-send' && $action != 'sf2-captcha') {
            return true;
        }

        if ($action == 'sf2-send' && empty($_SERVER['HTTP_X_REQUESTED_WITH'])) {
            // process only AJAX calls
            return;
        }

        $responce = array(
            'status' => 'error',
            'message' => JText::_('MOD_SIMPLEFORM2_ERROR')
        );
        try {

            $user = JFactory::getUser();
            $moduleID = (int) $app->input->getInt('moduleID', 0);

            if ($moduleID <= 0) {
                throw new \Exception(JText::_('MOD_SIMPLEFORM2_FORM_NOT_FOUND'));
            }
            $module = JTable::getInstance('module');
            $module->load($moduleID);
            if ((int) $module->id <= 0 || (int) $module->id != $moduleID) {
                throw new \Exception(JText::_('MOD_SIMPLEFORM2_FORM_NOT_FOUND'));
            }
            $params = new JRegistry;
            $params->loadString($module->params);

            $id = 'simpleForm2_' . $module->id;
            $okText = $params->get('okText', '');

            $form = new simpleForm2();
            $form->setID($id);
            $form->moduleID = (int) $module->id;
            $form->licenseKey = $params->get('domainKey', '');

            if (!$form->parse($params->get('simpleCode', ''))) {
                throw new \Exception('<b>' . JText::_('MOD_SIMPLEFORM2_ERROR_OCURED') . '</b><p>' . $form->getError() . '</p>');
            }
            switch ($action) {
                case 'sf2-send':
                    $form->bindInput($app->input->post);
                    $errors = $form->validate();
                    if (count($errors) > 0) {
                        $responce['elements'] = $errors;
                        throw new \Exception('<b>' . JText::_('MOD_SIMPLEFORM2_ERROR_OCURED') . '</b><ul class="sf2-error-list"><li>' . implode('</li><li>', array_values($errors)) . '</li></ul>');
                    } else {
                        SF2Helper::sendFormEmail($form, $params);
                        $form->onAfterSend();
                        $responce['status'] = 'success';
                        $responce['message'] = ($okText != '' ? $okText : JText::_('MOD_SIMPLEFORM2_FORM_SUCCEED'));
                        $responce['html'] = '<div class="sf2-message sf2-type-success">' . ($okText != '' ? $okText : JText::_('MOD_SIMPLEFORM2_FORM_SUCCEED')) . '</div>';
                    }
                    break;
                case 'sf2-captcha':
                    $captchaElem = $form->getCaptchaElement();
                    if ($captchaElem === false) {
                        throw new \Exception(JText::_('MOD_SIMPLEFORM2_FORM_HAS_NO_CAPTCHA'));
                    }
                    $captchaElem->renderImage();
                    $app->close();
                    break;
            }
        } catch (\Exception $e) {
            $responce = array(
                'status' => 'error',
                'message' => $e->getMessage()
            );
        }
        header('Content-Type: application/json; charset="utf-8"', true);
        echo json_encode($responce);
        $app->close();
    }
    
    public function onProcessLicenseKey(&$licenseKey){
        if(!empty($this->params->get('domainKey',''))){
            $licenseKey = $this->params->get('domainKey','');
        }
    }

    private function _loadLang() {
        $lang = JFactory::getLanguage();
        $lang->load('mod_simpleform2', JPATH_SITE);
    }

}
