コード例 #1
0
ファイル: wikiplugin_list.php プロジェクト: rjsmelo/tiki
function wikiplugin_list($data, $params)
{
    global $prefs;
    static $i;
    $i++;
    $unifiedsearchlib = TikiLib::lib('unifiedsearch');
    $query = new Search_Query();
    if (!isset($params['searchable_only']) || $params['searchable_only'] == 1) {
        $query->filterIdentifier('y', 'searchable');
    }
    $unifiedsearchlib->initQuery($query);
    $matches = WikiParser_PluginMatcher::match($data);
    $tsret = applyTablesorter($matches, $query);
    $builder = new Search_Query_WikiBuilder($query);
    $builder->enableAggregate();
    if (!empty($tsret['max']) || !empty($_GET['numrows'])) {
        $max = !empty($_GET['numrows']) ? $_GET['numrows'] : $tsret['max'];
        $builder->wpquery_pagination_max($query, $max);
    }
    $builder->apply($matches);
    $paginationArguments = $builder->getPaginationArguments();
    if (!empty($_REQUEST[$paginationArguments['sort_arg']])) {
        $query->setOrder($_REQUEST[$paginationArguments['sort_arg']]);
    }
    if (!($index = $unifiedsearchlib->getIndex())) {
        return '';
    }
    $result = $query->search($index);
    $result->setId('wplist-' . $i);
    $resultBuilder = new Search_ResultSet_WikiBuilder($result);
    $resultBuilder->setPaginationArguments($paginationArguments);
    $resultBuilder->apply($matches);
    $builder = new Search_Formatter_Builder();
    $builder->setPaginationArguments($paginationArguments);
    $builder->setId('wplist-' . $i);
    $builder->setCount($result->count());
    $builder->setTsOn($tsret['tsOn']);
    $builder->apply($matches);
    $result->setTsSettings($builder->getTsSettings());
    $formatter = $builder->getFormatter();
    $result->setTsOn($tsret['tsOn']);
    $out = $formatter->format($result);
    return $out;
}
コード例 #2
0
function wikiplugin_customsearch($data, $params)
{
    global $prefs;
    static $instance_id = null;
    if (empty($params['wiki']) && empty($params['tpl'])) {
        return tra('Template is not specified');
    } elseif (!empty($params['wiki']) && !TikiLib::lib('tiki')->page_exists($params['wiki'])) {
        return tra('Template page not found');
    }
    if (isset($params['id'])) {
        $id = $params['id'];
    } else {
        if ($instance_id === null) {
            $instance_id = 0;
        } else {
            $instance_id++;
        }
        $id = (string) $instance_id;
    }
    if (isset($params['recalllastsearch']) && $params['recalllastsearch'] == 1 && (!isset($_REQUEST['forgetlastsearch']) || $_REQUEST['forgetlastsearch'] != 'y')) {
        $recalllastsearch = 1;
    } else {
        $recalllastsearch = 0;
    }
    $defaults = array();
    $plugininfo = wikiplugin_customsearch_info();
    foreach ($plugininfo['params'] as $key => $param) {
        $defaults["{$key}"] = $param['default'];
    }
    $params = array_merge($defaults, $params);
    if (!isset($_REQUEST["offset"])) {
        $offset = 0;
    } else {
        $offset = (int) $_REQUEST["offset"];
    }
    if (isset($_REQUEST['maxRecords'])) {
        $maxRecords = (int) $_REQUEST['maxRecords'];
    } elseif ($recalllastsearch && !empty($_SESSION["customsearch_{$id}"]['maxRecords'])) {
        $maxRecords = (int) $_SESSION["customsearch_{$id}"]['maxRecords'];
    } else {
        $maxRecords = (int) $prefs['maxRecords'];
        $maxDefault = true;
    }
    if (!empty($_REQUEST['sort_mode'])) {
        $sort_mode = $_REQUEST['sort_mode'];
    } elseif ($recalllastsearch && !empty($_SESSION["customsearch_{$id}"]['sort_mode'])) {
        $sort_mode = $_SESSION["customsearch_{$id}"]['sort_mode'];
    } else {
        $sort_mode = '';
    }
    $definitionKey = md5($data);
    $matches = WikiParser_PluginMatcher::match($data);
    $query = new Search_Query();
    if (!isset($params['searchable_only']) || $params['searchable_only'] == 1) {
        $query->filterIdentifier('y', 'searchable');
    }
    $builder = new Search_Query_WikiBuilder($query);
    $builder->apply($matches);
    $paginationArguments = $builder->getPaginationArguments();
    // Use maxRecords set in LIST parameters rather then global default if set.
    if (isset($maxDefault) && $maxDefault) {
        if (!empty($paginationArguments['max'])) {
            $maxRecords = $paginationArguments['max'];
        }
    }
    // setup AJAX pagination
    $paginationArguments['offset_jsvar'] = "customsearch_{$id}.offset";
    $paginationArguments['sort_jsvar'] = "customsearch_{$id}.sort_mode";
    $paginationArguments['_onclick'] = "\$('#customsearch_{$id}').submit();return false;";
    $builder = new Search_Formatter_Builder();
    $builder->setPaginationArguments($paginationArguments);
    $builder->apply($matches);
    $formatter = $builder->getFormatter();
    $facets = new Search_Query_FacetWikiBuilder();
    $facets->apply($matches);
    $cachelib = TikiLib::lib('cache');
    $cachelib->cacheItem($definitionKey, serialize(array('query' => $query, 'formatter' => $formatter, 'facets' => $facets)), 'customsearch');
    if (!empty($params['wiki'])) {
        $wikitpl = "tplwiki:" . $params['wiki'];
    } else {
        $wikitpl = $params['tpl'];
    }
    $wikicontent = TikiLib::lib('smarty')->fetch($wikitpl);
    TikiLib::lib('parser')->parse_wiki_argvariable($wikicontent);
    $matches = WikiParser_PluginMatcher::match($wikicontent);
    $fingerprint = md5($wikicontent);
    $sessionprint = "customsearch_{$id}_{$fingerprint}";
    if (isset($_SESSION[$sessionprint]) && $_SESSION[$sessionprint] != $fingerprint) {
        unset($_SESSION["customsearch_{$id}"]);
    }
    $_SESSION[$sessionprint] = $fingerprint;
    // important that offset from session is set after fingerprint check otherwise blank page might show
    if ($recalllastsearch && !isset($_REQUEST['offset']) && !empty($_SESSION["customsearch_{$id}"]["offset"])) {
        $offset = (int) $_SESSION["customsearch_{$id}"]["offset"];
    }
    $options = array('searchfadetext' => tr('Loading...'), 'searchfadediv' => $params['searchfadediv'], 'results' => empty($params['destdiv']) ? "#customsearch_{$id}_results" : "#{$params['destdiv']}", 'autosearchdelay' => isset($params['autosearchdelay']) ? max(1500, (int) $params['autosearchdelay']) : 0, 'searchonload' => (int) $params['searchonload'], 'requireinput' => (bool) $params['requireinput'], 'origrequireinput' => (bool) $params['requireinput'], 'forcesortmode' => (bool) $params['forcesortmode']);
    /**
     * NOTES: Search Execution
     *
     * There is a global delay on execution of 1 second. This makes sure
     * multiple submissions will never trigger multiple requests.
     *
     * There is an additional autosearchdelay configuration that can trigger the search
     * on field change rather than explicit request. Explicit requests will still work.
     */
    $script = "\nvar customsearch = {\n\toptions: " . json_encode($options) . ",\n\tid: " . json_encode($id) . ",\n\toffset: 0,\n\tsearchdata: {},\n\tdefinition: " . json_encode((string) $definitionKey) . ",\n\tautoTimeout: null,\n\tadd: function (fieldId, filter) {\n\t\tthis.searchdata[fieldId] = filter;\n\t\tthis.auto();\n\t},\n\tremove: function (fieldId) {\n\t\tdelete this.searchdata[fieldId];\n\t\tthis.auto();\n\t},\n\tload: function () {\n\t\tthis._executor(this);\n\t},\n\tauto: function () {\n\t},\n\t_executor: delayedExecutor(1000, function (cs) {\n\t\tvar selector = '#' + cs.options.searchfadediv;\n\t\tif (cs.options.searchfadediv.length <= 1 && \$(selector).length === 0) {\n\t\t\tselector = '#customsearch_' + cs.id;\n\t\t}\n\n\t\t\$(selector).tikiModal(cs.options.searchfadetext);\n\n\t\tcs._load(function (data) {\n\t\t\t\$(selector).tikiModal();\n\t\t\t\$(cs.options.results).html(data);\n\t\t\t\$(document).trigger('pageSearchReady');\n\t\t});\n\t\tcs.store_query = '';\n\t}),\n\tinit: function () {\n\t\tvar that = this;\n\t\tif (that.options.searchonload) {\n\t\t\tthat.load();\n\t\t}\n\n\t\tif (that.options.autosearchdelay) {\n\t\t\tthat.auto = delayedExecutor(that.options.autosearchdelay, function () {\n\t\t\t\tif (that.options.requireinput && (!\$('#customsearch_{$id}').find(':text').val() || \$('#customsearch_{$id}').find(':text').val().indexOf('...') > 0)) {\n\t\t\t\t\treturn false;\n\t\t\t\t}\n\t\t\t\tthat.load();\n\t\t\t});\n\t\t}\n\t}\n};\n\$('#customsearch_{$id}').click(function() {\n\tcustomsearch.offset = 0;\n});\n\$('#customsearch_{$id}').submit(function() {\n\tif (customsearch.options.requireinput && (!\$(this).find(':text').val() || \$(this).find(':text').val().indexOf('...') > 0)) {\n\t\talert(tr('Please enter a search query'));\n\t\treturn false;\n\t}\n\tif (customsearch.options.origrequireinput != customsearch.options.requireinput) {\n\t\tcustomsearch.options.requireinput = customsearch.options.origrequireinput;\n\t}\n\tcustomsearch.load();\n\treturn false;\n});\n\nwindow.customsearch_{$id} = customsearch;\n\$(document).trigger('formSearchReady');\n";
    $parser = new WikiParser_PluginArgumentParser();
    $dr = 0;
    foreach ($matches as $match) {
        $name = $match->getName();
        $arguments = $parser->parse($match->getArguments());
        $key = $match->getInitialStart();
        $fieldid = "customsearch_{$id}_{$key}";
        if (isset($arguments['id'])) {
            $fieldid = $arguments['id'];
        }
        if ($name == 'sort' && !empty($arguments['mode']) && empty($sort_mode)) {
            $sort_mode = $arguments['mode'];
            $match->replaceWith('');
            continue;
        }
        if (!empty($arguments['_field']) && !empty($arguments['_filter']) && $arguments['_filter'] == 'content') {
            $filter = $arguments['_field'];
        } elseif (empty($arguments['_field']) && !empty($arguments['_filter']) && $arguments['_filter'] == 'content') {
            $filter = 'content';
        } else {
            $filter = '';
        }
        if ($filter && !empty($_REQUEST['default'][$filter])) {
            $default = $_REQUEST['default'][$filter];
        } elseif ($recalllastsearch && isset($_SESSION["customsearch_{$id}"][$fieldid])) {
            $default = $_SESSION["customsearch_{$id}"][$fieldid];
        } elseif (!empty($arguments['_default'])) {
            if (strpos($arguments['_default'], ',') !== false) {
                $default = explode(',', $arguments['_default']);
            } else {
                $default = $arguments['_default'];
            }
        } else {
            $default = '';
        }
        if ($name == 'categories') {
            $parent = $arguments['_parent'];
            if (!empty($_REQUEST['defaultcat'][$parent])) {
                $default = $_REQUEST['defaultcat'][$parent];
            }
        }
        $function = "cs_design_{$name}";
        if (function_exists($function)) {
            if (isset($arguments['_group'])) {
                $fieldname = "customsearch_{$id}_gr" . $arguments['_group'];
            } elseif (isset($arguments['_textrange'])) {
                $fieldname = "customsearch_{$id}_textrange" . $arguments['_textrange'];
            } elseif (isset($arguments['_daterange'])) {
                $fieldname = "customsearch_{$id}_daterange" . $arguments['_daterange'];
            } else {
                $fieldname = $fieldid;
            }
            $html = $function($id, $fieldname, $fieldid, $arguments, $default, $script);
            if ($params['trimlinefeeds']) {
                $html = trim($html);
            }
            $match->replaceWith($html);
        }
        if ($name == 'daterange') {
            $dr++;
        }
    }
    $callbackScript = null;
    if (!empty($params['callbackscript']) && TikiLib::lib('tiki')->page_exists($params['callbackscript'])) {
        $callbackscript_tpl = "wiki:" . $params['callbackscript'];
        $callbackScript = TikiLib::lib('smarty')->fetch($callbackscript_tpl);
    }
    //get iconset icon if daterange is one of the fields
    if ($dr) {
        $smarty = TikiLib::lib('smarty');
        $smarty->loadPlugin('smarty_function_js_insert_icon');
        $iconinsert = smarty_function_js_insert_icon(['type' => 'jscalendar', 'return' => 'y'], $smarty);
    } else {
        $iconinsert = '';
    }
    global $page;
    $script .= "\ncustomsearch._load = function (receive) {\n\tvar datamap = {\n\t\tdefinition: this.definition,\n\t\tadddata: \$.toJSON(this.searchdata),\n\t\tsearchid: this.id,\n\t\toffset: customsearch.offset,\n\t\tmaxRecords: this.maxRecords,\n\t\tstore_query: this.store_query,\n\t\tpage: " . json_encode($page) . ",\n\t\trecalllastsearch: {$recalllastsearch}\n\t};\n\tif (!customsearch.options.forcesortmode && \$('#customsearch_{$id}').find(':text').val() && \$('#customsearch_{$id}').find(':text').val().indexOf('...') <= 0) {\n\t\tcustomsearch.sort_mode = 'score_desc';\n\t}\n\tif (customsearch.sort_mode) {\n\t\t// blank sort_mode is not allowed by Tiki input filter\n\t\tdatamap.sort_mode = customsearch.sort_mode;\n\t}\n\t\$.ajax({\n\t\ttype: 'POST',\n\t\turl: \$.service('search_customsearch', 'customsearch'),\n\t\tdata: datamap,\n\t\tdataType: 'html',\n\t\tsuccess: function(data) {\n\t\t\treceive(data);\n\t\t\t{$callbackScript};\n\t\t},\n\t\terror: function ( jqXHR, textStatus, errorThrown ) {\n\t\t\tvar selector = '#' + customsearch.options.searchfadediv;\n\t\t\tif (customsearch.options.searchfadediv.length <= 1 && \$(selector).length === 0) {\n\t\t\t\tselector = '#customsearch_' + customsearch.id;\n\t\t\t}\n\t\t\t\$(selector).tikiModal();\n\n\t\t\t\$('#customsearch_{$id}').showError(jqXHR)\n\t\t}\n\t});\n};\ncustomsearch.sort_mode = " . json_encode($sort_mode) . ";\ncustomsearch.offset = {$offset};\ncustomsearch.maxRecords = {$maxRecords};\ncustomsearch.store_query ='';\ncustomsearch.init();\n{$iconinsert}";
    TikiLib::lib('header')->add_jq_onready($script);
    if ($params['customsearchjs']) {
        TikiLib::lib('header')->add_jsfile('lib/jquery_tiki/customsearch.js');
    }
    $out = '<div id="customsearch_' . $id . '_form"><form id="customsearch_' . $id . '">' . $matches->getText() . '</form></div>';
    if (empty($params['destdiv'])) {
        $out .= '<div id="customsearch_' . $id . '_results"></div>';
    }
    if (!empty($params['wiki'])) {
        return $out;
    } else {
        // If using smarty tpl should assume it's all HTML
        $out = str_replace('~np~', '', $out);
        $out = str_replace('~/np~', '', $out);
        return '~np~' . $out . '~/np~';
    }
}