<?php

/*===============================================*\
|| ############################################# ||
|| # JAKWEB.CH / Version 2.0.2                 # ||
|| # ----------------------------------------- # ||
|| # Copyright 2022 JAKWEB All Rights Reserved # ||
|| ############################################# ||
\*===============================================*/

// Check if the file is accessed only via index.php if not stop the script from running
if (!defined('JAK_PREVENT_ACCESS')) die('No direct access!');

// Get the important database table
$jaktable = 'tags';
$jaktable2 = 'faq_article';
$jaktable3 = 'faq_categories';
$jaktable4 = 'blog';
$jaktable5 = 'support_tickets';

$SearchInput = "";
$searchfield = false;

// Now do the dirty work with the post vars
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['smart_search']) || !empty($page1)) {

    $jkp = $_POST;
    
    if (isset($jkp['smart_search'])) {
  
      if (empty($page1) && $jkp['smart_search'] == '' || $jkp['smart_search'] == $jkl['hd']) {
          $errors['e'] = $jkl['hd2'];
      }
  
      if (empty($errors['e']) && empty($page1) && strlen($jkp['smart_search']) < '2') {
          $errors['e1'] = $jkl['hd3'];
      }
      
  }

    if (count($errors) > 0) {
        $errors = $errors;
    } else {
    
      if (!empty($page1)) {
        $SearchInput = filter_var($page1, FILTER_SANITIZE_FULL_SPECIAL_CHARS);
      } else {
        $SearchInput = filter_var($jkp['smart_search'], FILTER_SANITIZE_FULL_SPECIAL_CHARS);
      }

      $searchresult = $searchblog = $searchsupport = array();

      // Client Access
      if (JAK_CLIENTID) {

        if (JAK_FAQ_A == 1) {

          // All access
          if ($jakclient->getVar("faq_cat") == 0) {

            // Search FAQ's
            $searchresult = $jakdb->select($jaktable2, ["[>]".$jaktable3 => ["catid" => "id"]], ["faq_article.id", "faq_article.lang", "faq_article.title", "faq_article.content", "faq_article.time", "faq_categories.class", "faq_categories.title(titlecat)"], ["AND" => ["faq_article.opid" => $_SESSION['opid'], "faq_article.active" => 1],
          "MATCH" => [
            "columns" => ["faq_article.content", "faq_article.title"],
            "keyword" => $SearchInput],
          "LIMIT" => 10
          ]);

          // Tags FAQ
            $searchtagsfaq = $jakdb->select($jaktable, ["[>]".$jaktable2 => ["faqid" => "id"], "[>]".$jaktable3 => ["faq_article.catid" => "id"]], ["faq_article.id", "faq_article.lang", "faq_article.title", "faq_article.content", "faq_article.time", "faq_categories.class", "faq_categories.title(titlecat)"], ["AND" => ["tags.opid" => $_SESSION['opid'],"faq_article.active" => 1, "tags.faqid[!]" => 0],
          "MATCH" => [
            "columns" => ["tags.tagname"],
            "keyword" => $SearchInput],
            "LIMIT" => 10
          ]);

          $searchresult = array_unique(array_merge($searchresult, $searchtagsfaq), SORT_REGULAR);

        // Only for certain categories
          } else {

            // Search FAQ's
            $searchresult = $jakdb->select($jaktable2, ["[>]".$jaktable3 => ["catid" => "id"]], ["faq_article.id", "faq_article.lang", "faq_article.title", "faq_article.content", "faq_article.time", "faq_categories.class", "faq_categories.title(titlecat)"], ["AND" => ["faq_article.opid" => $_SESSION['opid'], "faq_article.catid" => [$jakclient->getVar("faq_cat")], "faq_article.active" => 1],
          "MATCH" => [
            "columns" => ["faq_article.content", "faq_article.title"],
            "keyword" => $SearchInput],
          "LIMIT" => 10
          ]);

          // Tags FAQ
            $searchtagsfaq = $jakdb->select($jaktable, ["[>]".$jaktable2 => ["faqid" => "id"], "[>]".$jaktable3 => ["faq_article.catid" => "id"]], ["faq_article.id", "faq_article.lang", "faq_article.title", "faq_article.content", "faq_article.time", "faq_categories.class", "faq_categories.title(titlecat)"], ["AND" => ["tags.opid" => $_SESSION['opid'], "faq_article.catid" => [$jakclient->getVar("faq_cat")], "faq_article.active" => 1, "tags.faqid[!]" => 0],
          "MATCH" => [
            "columns" => ["tags.tagname"],
            "keyword" => $SearchInput],
            "LIMIT" => 10
          ]);

          $searchresult = array_unique(array_merge($searchresult, $searchtagsfaq), SORT_REGULAR);
          }
        }

        if (JAK_BLOG_A == 1)  {

          // Blog
        $searchblog = $jakdb->select($jaktable4, ["id", "lang", "title", "content", "time"], ["AND" => ["opid" => $_SESSION['opid'], "active" => 1],
        "MATCH" => [
          "columns" => ["content", "title"],
          "keyword" => $SearchInput],
          "LIMIT" => 10
        ]);

        // Tags Blog
          $searchtagsblog = $jakdb->select($jaktable, ["[>]".$jaktable4 => ["blogid" => "id"]], ["blog.id", "blog.lang", "blog.title", "blog.content", "blog.time"], ["AND" => ["tags.opid" => $_SESSION['opid'], "blog.active" => 1, "tags.blogid[!]" => 0],
        "MATCH" => [
          "columns" => ["tags.tagname"],
          "keyword" => $SearchInput],
          "LIMIT" => 10
        ]);

        $searchblog = array_unique(array_merge($searchblog, $searchtagsblog), SORT_REGULAR);

      }

      // Support
      if ($jakclient->getVar("support_dep") == 0) {
        $searchsupport = $jakdb->select($jaktable5, ["id", "depid", "name", "subject", "content", "initiated(time)"], ["AND" => ["OR" => ["clientid" => JAK_CLIENTID, "private" => 0], "opid" => $_SESSION['opid']],
          "MATCH" => [
            "columns" => ["content", "subject"],
            "keyword" => $SearchInput],
            "LIMIT" => 10
        ]);
      } else {
        $searchsupport = $jakdb->select($jaktable5, ["id", "depid", "name", "subject", "content", "initiated(time)"], ["AND" => ["OR" => ["clientid" => JAK_CLIENTID, "private" => 0, "depid" => explode(",", $jakclient->getVar("support_dep"))], "opid" => $_SESSION['opid']],
          "MATCH" => [
            "columns" => ["content", "subject"],
            "keyword" => $SearchInput],
            "LIMIT" => 10
        ]);
      }

      // Can see all active articles
      } elseif (JAK_USERID) {

        // Search FAQ's
        $searchresult = $jakdb->select($jaktable2, ["[>]".$jaktable3 => ["catid" => "id"]], ["faq_article.id", "faq_article.lang", "faq_article.title", "faq_article.content", "faq_article.time", "faq_categories.class", "faq_categories.title(titlecat)"], ["AND" => ["faq_article.opid" => $_SESSION['opid'], "faq_article.active" => 1],
      "MATCH" => [
        "columns" => ["faq_article.content", "faq_article.title"],
        "keyword" => $SearchInput],
      "LIMIT" => 10
      ]);

      // Tags FAQ
        $searchtagsfaq = $jakdb->select($jaktable, ["[>]".$jaktable2 => ["faqid" => "id"], "[>]".$jaktable3 => ["faq_article.catid" => "id"]], ["faq_article.id", "faq_article.lang", "faq_article.title", "faq_article.content", "faq_article.time", "faq_categories.class", "faq_categories.title(titlecat)"], ["AND" => ["tags.opid" => $_SESSION['opid'], "faq_article.active" => 1, "tags.faqid[!]" => 0],
        "MATCH" => [
          "columns" => ["tags.tagname"],
          "keyword" => $SearchInput],
          "LIMIT" => 10
      ]);

      $searchresult = array_unique(array_merge($searchresult, $searchtagsfaq), SORT_REGULAR);

        // Blog
      $searchblog = $jakdb->select($jaktable4, ["id", "lang", "title", "content", "time"], ["AND" => ["opid" => $_SESSION['opid'], "active" => 1],
        "MATCH" => [
          "columns" => ["content", "title"],
          "keyword" => $SearchInput],
          "LIMIT" => 10
      ]);

      // Tags Blog
        $searchtagsblog = $jakdb->select($jaktable, ["[>]".$jaktable4 => ["blogid" => "id"]], ["blog.id", "blog.lang", "blog.title", "blog.content", "blog.time"], ["AND" => ["tags.opid" => $_SESSION['opid'], "blog.active" => 1, "tags.blogid[!]" => 0],
        "MATCH" => [
          "columns" => ["tags.tagname"],
          "keyword" => $SearchInput],
          "LIMIT" => 10
      ]);

      $searchblog = array_unique(array_merge($searchblog, $searchtagsblog), SORT_REGULAR);

      // Support
      if (is_numeric($jakuser->getVar("support_dep")) && $jakuser->getVar("support_dep") != 0) {
        $searchsupport = $jakdb->select($jaktable5, ["id", "depid", "name", "subject", "content", "initiated(time)"], ["AND" => ["OR" => ["operatorid" => JAK_USERID, "depid" => $jakuser->getVar("support_dep")], "opid" => $_SESSION['opid']],
          "MATCH" => [
            "columns" => ["content", "subject"],
            "keyword" => $SearchInput],
            "LIMIT" => 10
        ]);
      } elseif (!((boolean)$jakuser->getVar("support_dep")) && $jakuser->getVar("support_dep") != 0) {
        $searchsupport = $jakdb->select($jaktable5, ["id", "depid", "name", "subject", "content", "initiated(time)"], ["AND" => ["OR" => ["operatorid" => JAK_USERID, "depid" => explode(",", $jakuser->getVar("support_dep"))], "opid" => $_SESSION['opid']],
          "MATCH" => [
            "columns" => ["content", "subject"],
            "keyword" => $SearchInput],
            "LIMIT" => 10
        ]);
      } else {
        $searchsupport = $jakdb->select($jaktable5, ["id", "depid", "name", "subject", "content", "initiated(time)"], ["AND" => ["OR" => ["operatorid" => JAK_USERID, "private" => 0], "opid" => $_SESSION['opid']],
          "MATCH" => [
            "columns" => ["content", "subject"],
            "keyword" => $SearchInput],
            "LIMIT" => 10
        ]);
      }

    // Can see categories for guests
      } else {

        if (JAK_FAQ_A == 1) {

          // FAQ
          $searchresult = $jakdb->select($jaktable2, ["[>]".$jaktable3 => ["catid" => "id"]], ["faq_article.id", "faq_article.lang", "faq_article.title", "faq_article.content", "faq_article.time", "faq_categories.class", "faq_categories.title(titlecat)"], ["AND" => ["faq_article.opid" => $_SESSION['opid'], "faq_categories.guesta" => 1, "faq_article.active" => 1],
        "MATCH" => [
          "columns" => ["faq_article.content", "faq_article.title"],
          "keyword" => $SearchInput],
          "LIMIT" => 10
        ]);

        // Tags FAQ
          $searchtagsfaq = $jakdb->select($jaktable, ["[>]".$jaktable2 => ["faqid" => "id"], "[>]".$jaktable3 => ["faq_article.catid" => "id"]], ["faq_article.id", "faq_article.lang", "faq_article.title", "faq_article.content", "faq_article.time", "faq_categories.class", "faq_categories.title(titlecat)"], ["AND" => ["faq_article.opid" => $_SESSION['opid'], "faq_categories.guesta" => 1, "faq_article.active" => 1, "tags.faqid[!]" => 0],
        "MATCH" => [
          "columns" => ["tags.tagname"],
          "keyword" => $SearchInput],
          "LIMIT" => 10
        ]);

        $searchresult = array_unique(array_merge($searchresult, $searchtagsfaq), SORT_REGULAR);

        }

        if (JAK_BLOG_A == 1)  {

          // Blog
        $searchblog = $jakdb->select($jaktable4, ["id", "lang", "title", "content", "time"], ["AND" => ["membersonly" => 0, "active" => 1],
        "MATCH" => [
          "columns" => ["content", "title"],
          "keyword" => $SearchInput],
          "LIMIT" => 10
        ]);

        // Tags Blog
          $searchtagsblog = $jakdb->select($jaktable, ["[>]".$jaktable4 => ["blogid" => "id"]], ["blog.id", "blog.lang", "blog.title", "blog.content", "blog.time"], ["AND" => ["tags.opid" => $_SESSION['opid'],"blog.membersonly" => 0, "blog.active" => 1, "tags.blogid[!]" => 0],
        "MATCH" => [
          "columns" => ["tags.tagname"],
          "keyword" => $SearchInput],
          "LIMIT" => 10
        ]);

        $searchblog = array_unique(array_merge($searchblog, $searchtagsblog), SORT_REGULAR);

      }

      if (JAK_TICKET_GUEST_WEB == 1) {

        // Support
        $searchsupport = $jakdb->select($jaktable5, ["id", "depid", "name", "subject", "content", "initiated(time)"], ["AND" => ["support_tickets.opid" => $_SESSION['opid'], "private" => 0],
        "MATCH" => [
          "columns" => ["content", "subject"],
          "keyword" => $SearchInput],
          "LIMIT" => 10
        ]);

      }

      }
                
    }
}

// Load the template
if (isset($JAK_CMS_PAGE["main_tpl"]) && !empty($JAK_CMS_PAGE["main_tpl"])) {
  include_once APP_PATH.'template/'.JAK_FRONT_TEMPLATE.'/tplblocks/search_tpl/'.$JAK_CMS_PAGE["main_tpl"];
} else {
    include_once APP_PATH.'template/'.JAK_FRONT_TEMPLATE.'/tplblocks/search_tpl/search.php';
}
?>
