<?php

/**
 * @package   Bookingforconnector
 * @copyright Copyright (c)2006-2016 Ipertrade
 * @license   GNU General Public License version 3, or later
 */
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
$pathbase = JPATH_SITE . DIRECTORY_SEPARATOR . 'components' . DIRECTORY_SEPARATOR . 'com_bookingforconnector' . DIRECTORY_SEPARATOR;
require_once $pathbase . '/helpers/htmlHelper.php';
$merchants = BFCHelper::getMerchantsSearch('', 0, 1, null, null);
if (!empty($merchants)) {
    $merchant = array_values($merchants)[0];
    $db = JFactory::getDBO();
    $uriMerchant = 'index.php?option=com_bookingforconnector&view=merchantdetails';
    $db->setQuery('SELECT id FROM #__menu WHERE link LIKE ' . $db->Quote($uriMerchant . '%') . ' AND (language=' . $db->Quote($this->language) . ' OR language=' . $db->Quote('*') . ') AND published = 1 LIMIT 1');
    $itemIdMerchant = $db->getErrorNum() ? 0 : intval($db->loadResult());
    //$itemIdMerchant = intval($db->loadResult());
    $uriMerchant .= '&merchantId=' . $merchant->MerchantId . ':' . BFCHelper::getSlug($merchant->Name);
    if ($itemIdMerchant != 0) {
        $uriMerchant .= '&Itemid=' . $itemIdMerchant;
    }
    $uriMerchantthanks = $uriMerchant . '&layout=thanks';
    $uriMerchant .= '&layout=contacts';
    $route = JRoute::_($uriMerchant);
    $routeThanks = JRoute::_($uriMerchantthanks);
    $cNationList = BFCHelper::parseArrayList(JTEXT::_('COM_BOOKINGFORCONNECTOR_VIEW_CONSTANTS_NATIONSLIST'));
    //$cLanguageList = BFCHelper::parseArrayList(JTEXT::_('COM_BOOKINGFORCONNECTOR_VIEW_CONSTANTS_LANGUAGESLIST'));
    $cultureCode = strtolower(substr($this->language, 0, 2));
    $nationCode = strlen($this->language) == 5 ? strtolower(substr($this->language, 3, 2)) : $cultureCode;
 function onContentSearch($text, $phrase = '', $ordering = '', $areas = null)
 {
     //If the array is not correct, return it:
     if (is_array($areas)) {
         if (!array_intersect($areas, array_keys($this->onContentSearchAreas()))) {
             return array();
         }
     }
     $section = JText::_('PLG_SEARCH_BOOKINGFORCONNECTOR');
     //Then load the parameters of the plugin.
     $pluginParams = $this->params;
     //Now define the parameters like this:
     $limit = $pluginParams->def('search_limit', 50);
     $direction = $pluginParams->def('direction', "asc");
     //Use the function trim to delete spaces in front of or at the back of the searching terms
     $text = trim($text);
     //Return Array when nothing was filled in.
     if ($text == '') {
         return array();
     }
     $wheres = array();
     switch ($phrase) {
         //search exact
         case 'exact':
             /*$text		= $db->Quote( '%'.$db->getEscaped( $text, true ).'%', false );
             		$where 		= '(' . implode( ') OR (', $wheres2 ) . ')';*/
             break;
             //search all or any
         //search all or any
         case 'all':
         case 'any':
             //set default
         //set default
         default:
             /*
             $words 	= explode( ' ', $text );
             $wheres = array();
             foreach ($words as $word)
             {
             $word		= $db->Quote( '%'.$db->getEscaped( $word, true ).'%', false );
             $wheres2 	= array();
             $wheres2[] 	= 'LOWER(a.name) LIKE '.$word;
             $wheres[] 	= implode( ' OR ', $wheres2 );
             }
             $where = '(' . implode( ($phrase == 'all' ? ') AND (' : ') OR ('), $wheres ) . ')';
             */
             break;
     }
     //ordering of the results
     switch ($ordering) {
         //alphabetic, ascending
         case 'alpha':
             $order = 'Name';
             break;
             //oldest first
         //oldest first
         case 'oldest':
             //popular first
         //popular first
         case 'popular':
             //newest first
         //newest first
         case 'newest':
             //default setting: alphabetic, ascending
         //default setting: alphabetic, ascending
         default:
             $order = 'Name';
     }
     //replace nameofplugin
     $rows = array();
     $merchants = BFCHelper::getMerchantsSearch($text, 0, $limit, $order, $direction);
     $document = JFactory::getDocument();
     $language = $document->getLanguage();
     /* we have to find the itemid for the target page */
     $db = JFactory::getDBO();
     $lang = JFactory::getLanguage()->getTag();
     $uri = 'index.php?option=com_bookingforconnector&view=merchantdetails';
     $db->setQuery('SELECT id FROM #__menu WHERE link LIKE ' . $db->Quote($uri . '%') . ' AND language=' . $db->Quote($lang) . ' LIMIT 1');
     $itemId = $db->getErrorNum() ? 0 : intval($db->loadResult());
     //The 'output' of the displayed link
     foreach ($merchants as $merchant) {
         //$rows[$key]->href = 'index.php?option=com_bookingforconnector&view=merchantdetails&merchantId=' . $merchant->MerchantId . ':' . BFCHelper::getSlug($merchant->Name);
         $rows[] = (object) array('href' => Jroute::_('index.php?Itemid=' . $itemId . '&option=com_bookingforconnector&view=merchantdetails&merchantId=' . $merchant->MerchantId . ':' . BFCHelper::getSlug($merchant->Name)), 'title' => $merchant->Name, 'created' => null, 'section' => $section, 'text' => BFCHelper::getLanguage($merchant->Description, $language), 'browsernav' => '0');
     }
     //Return the search results in an array
     return $rows;
 }