mambax7/gwiki

View on GitHub
include/search.inc.php

Summary

Maintainability
A
3 hrs
Test Coverage
<?php

/**
 * include/search.inc.php - search gwiki pages
 *
 * This file is part of gwiki - geekwright wiki
 *
 * @param        $queryarray
 * @param        $andor
 * @param        $limit
 * @param        $offset
 * @param        $userid
 * @param null   $prefix
 * @return array
 * @copyright  Copyright © 2013 geekwright, LLC. All rights reserved.
 * @license    gwiki/docs/license.txt  GNU General Public License (GPL)
 * @since      1.0
 * @author     Richard Griffith <richard@geekwright.com>
 * @package    gwiki
 */
function gwiki_search($queryarray, $andor, $limit, $offset, $userid, $prefix = null)
{
    global $xoopsDB;

    $dir = \basename(\dirname(__DIR__));

    /** @var \XoopsModuleHandler $moduleHandler */
    $moduleHandler = xoops_getHandler('module');
    $module        = $moduleHandler->getByDirname($dir);
    $module_id     = $module->getVar('mid');
    /** @var \XoopsConfigHandler $configHandler */
    $configHandler = xoops_getHandler('config');
    $moduleConfig  = $configHandler->getConfigsByCat(0, $module->getVar('mid'));

    $baseurl = $moduleConfig['searchlink_template'];

    if ('' === $queryarray) {
        $args = '';
    } else {
        $args = implode('+', $queryarray); // template should require_once __DIR__   . '/&query='
    }

    $pagesetq = '';
    if (is_array($queryarray) && (count($queryarray) > 1)
        && 0 === substr_compare($queryarray[count($queryarray) - 1], '{pageset=', 0, 9)) {
        $pageset = array_pop($queryarray);
        $pageset = mb_substr($pageset, 9, -1);
        trigger_error($pageset);
        $pagesetq = " AND page_set_home = '{$pageset}' ";
    }

    $sql = 'SELECT DISTINCT * FROM ' . $xoopsDB->prefix('gwiki_pages') . ' WHERE active=1 ' . $pagesetq;
    if (is_array($queryarray) && ($count = count($queryarray))) {
        $sql .= " AND (title LIKE '%$queryarray[0]%' OR search_body LIKE '%$queryarray[0]%' OR meta_keywords LIKE '%$queryarray[0]%' OR meta_description LIKE '%$queryarray[0]%')";
        for ($i = 1; $i < $count; ++$i) {
            $sql .= " $andor (title LIKE '%$queryarray[$i]%' OR search_body LIKE '%$queryarray[$i]%' OR meta_keywords LIKE '%$queryarray[$i]%' OR meta_description LIKE '%$queryarray[$i]%')";
        }
    } else {
        $sql .= " AND uid='$userid'";
    }
    $sql .= ' ORDER BY lastmodified DESC';

    $items  = [];
    $result = $xoopsDB->query($sql, $limit, $offset);
    while (false !== ($myrow = $xoopsDB->fetchArray($result))) {
        $items[] = [
            'title' => $myrow['title'],
            'link'  => sprintf($baseurl, mb_strtolower($myrow['keyword']), $args),
            'time'  => $myrow['lastmodified'],
            'uid'   => $myrow['uid'],
            'image' => 'assets/images/search-result-icon.png',
        ];
    }

    return $items;
}