modx.evo.custom /assets/snippets/ajaxSearch/classes/ajaxSearchConfig.class.inc.php

Language PHP Lines 201
MD5 Hash f36e640348f367ec3d5b679b4b188efc
Repository https://github.com/good-web-master/modx.evo.custom.git View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php
/* -----------------------------------------------------------------------------
* Snippet: AjaxSearch
* -----------------------------------------------------------------------------
* @package  AjaxSearchConfig
*
* @author       Coroico - www.modx.wangba.fr
* @version      1.9.2
* @date         05/12/2010
*
* Purpose:
*    The AjaxSearchConfig class contains all functions and data used to manage configuration context
*
*/

class AjaxSearchConfig {

    // public variables
    var $pgCharset;
    var $dbCharset;
    var $isAjax;
    var $cfg = array();
    var $dcfg = array();
    var $ucfg;
    var $bcfg = array();
    var $scfg = array();
    var $lang;

    // private variables
    // Conversion code name between html page character encoding and Mysql character encoding
    // Some others conversions should be added if needed. Otherwise Page charset = Database charset
    var $_pageCharset = array('utf8' => 'UTF-8', 'latin1' => 'ISO-8859-1', 'latin2' => 'ISO-8859-2', 'cp1251' => 'windows-1251');

    function AjaxSearchConfig($dcfg, $cfg) {
        global $modx;
        $this->dbCharset = $modx->db->config['charset'];
        $this->pcreModifier = ($this->dbCharset == "utf8") ? 'iu' : 'i';
        $this->dcfg = $dcfg;
        $this->cfg = $cfg;
    }
    /*
    * Init the configuration
    */
    function initConfig(&$msgErr) {
        $msgErr = '';
        if (!isset($_POST['ucfg'])) {

            $this->isAjax = false;

            $this->ucfg = $this->getUserConfig();

            $this->bcfg = array_merge($this->dcfg, (array)$this->ucfg);

            $this->scfg[DEFAULT_SITE][DEFAULT_SUBSITE] = array();
        } else {

            $this->isAjax = true;

            $this->ucfg = $this->parseUserConfig(strip_tags($_POST['ucfg']));

            $this->bcfg = array_merge($this->dcfg, (array)$this->ucfg);

            $this->cfg = $this->bcfg;

            $this->scfg[DEFAULT_SITE][DEFAULT_SUBSITE] = array();
        }

        $this->_loadLang();

        $valid = $this->_setCharset($msgErr);
        return $valid;
    }
    /*
    * Load the language file
    */
    function _loadLang() {
        $_lang = array();

        $language = 'english';
        include AS_PATH . "lang/{$language}.inc.php";

        if (($this->cfg['language'] != '') && ($this->cfg['language'] != $language)) {
            if (file_exists(AS_PATH . "lang/{$this->cfg['language']}.inc.php")) include AS_PATH . "lang/" . $this->cfg['language'] . ".inc.php";
        }
        $this->lang = $_lang;
    }
    /*
    * Display config arrays
    */
    function displayConfig(& $asUtil) {
        if ($asUtil->dbg) {
            if ($this->cfg['config']) $asUtil->dbgRecord($this->readConfigFile($this->cfg['config']), __FUNCTION__ . ' - ' . $this->cfg['config']);
            $asUtil->dbgRecord($this->cfg, __FUNCTION__ . ' - Config before parameter checking');
        }
    }
    /*
    * Set the Page charset
    */
    function _setCharset(&$msgErr) {
        $valid = false;
        $msgErr = '';

        $this->pgCharset = array_key_exists($this->dbCharset, $this->_pageCharset) ? $this->_pageCharset[$this->dbCharset] : $this->dbCharset;

        if (isset($this->dbCharset) && isset($this->_pageCharset[$this->dbCharset])) {

            if ($this->dbCharset == 'utf8' && !extension_loaded('mbstring')) {
                $msgErr = "AjaxSearch error: php_mbstring extension required";
            } else {
                if ($this->dbCharset == 'utf8' && $this->cfg['mbstring']) mb_internal_encoding("UTF-8");
                $this->pgCharset = $this->_pageCharset[$this->dbCharset];
                $valid = true;
            }
        } elseif (!isset($this->dbCharset)) {
            $msgErr = "AjaxSearch error: database_connection_charset not set. Check your MODx config file";
        } elseif (!strlen($this->dbCharset)) {
            $msgErr = "AjaxSearch error: database_connection_charset is null. Check your MODx config file";
        } else {
            // if you get this message, simply update the $pageCharset array in search.class.inc.php file
            // with the appropriate mapping between Mysql Charset and Html charset
            // eg: 'latin2' => 'ISO-8859-2'
            $msgErr = "AjaxSearch error: unknown database_connection_charset = {$this->dbCharset}<br />Add the appropriate Html charset mapping in the ajaxSearchConfig.class.inc.php file";
        }
        return $valid;
    }
    /*
    * Save the current configuration
    */
    function saveConfig($site, $subsite) {
        if (!isset($this->scfg[$site][$subsite])) $this->scfg[$site][$subsite] = array();
        foreach ($this->cfg as $key => $value) {
            if (!isset($this->bcfg[$key]) || ($this->bcfg[$key] != $value)) $this->scfg[$site][$subsite][$key] = $value;
        }
    }
    /*
    * Restore a named configuration
    */
    function restoreConfig($site, $subsite) {
        if (isset($this->scfg[$site][$subsite])) $this->cfg = array_merge($this->bcfg, $this->scfg[$site][$subsite]);
        else $this->cfg = array_merge($this->bcfg, $this->scfg[DEFAULT_SITE][DEFAULT_SUBSITE]);
    }
    /*
    * Choose the appropriate configuration for displaying results
    */
    function chooseConfig($site, $subsite, $display) {
        $s = ($display != MIXED) ? $site : DEFAULT_SITE;
        $ss = ($display != MIXED) ? $subsite : DEFAULT_SUBSITE;
        $this->restoreConfig($s, $ss);
    }
    /*
    * Create a config by merging site and category config
    */
    function addConfigFromCateg($site, $categ, $ctg) {
        if (($site) && ($categ) && (!isset($this->scfg[$site][$categ]))) {
            if (isset($this->scfg[$site][$DEFAULT_SUBSITE])) $s = $this->scfg[$site][$DEFAULT_SUBSITE];
            else $s = array();
            $this->scfg[$site][$categ] = array_merge((array)$s, (array)$ctg);
        }
    }
    /*
    * Get the non default configuration (advSearch and subSearch excepted)
    */
    function getUserConfig() {
        $ucfg = array();
        foreach ($this->cfg as $key => $value) {
            if ($key != 'subSearch' && $value != $this->dcfg[$key]) $ucfg[$key] = $this->cfg[$key];
        }
        return $ucfg;
    }
    /*
    * Parse the non default configuration from string
    */
    function parseUserConfig($strUcfg) {
        $ucfg = array();
        $pattern = '/&([^=]*)=`([^`]*)`/';
        preg_match_all($pattern, $strUcfg, $out);
        foreach ($out[1] as $key => $values) $ucfg[$out[1][$key]] = $out[2][$key];
        return $ucfg;
    }
    /*
    * Set the AjaxSearch snippet call
    */
    function setAsCall($ucfg) {
        $tpl = "&%s=`%s` ";
        $asCall = '';
        foreach ($ucfg as $key => $value) $asCall.= sprintf($tpl, $key, $value);
        return $asCall;
    }
    /*
    *  Read config file
    */
    function readConfigFile($config) {
        global $modx;
        $configFile = (substr($config, 0, 5) != "@FILE") ? AS_PATH . "configs/$config.config.php" : $modx->config['base_path'] . trim(substr($config, 5));
        $fh = fopen($configFile, 'r');
        $output = fread($fh, filesize($configFile));
        fclose($fh);
        return "\n" . $output;
    }
}
?>
Back to Top