<?php

namespace worldfloraonline\controllers;

use Yii;
use common\models\LoginForm;
use common\models\PasswordResetRequestForm;
use common\models\ResetPasswordForm;
use worldfloraonline\models\SignupForm;
use common\models\ContactForm;
use yii\base\InvalidParamException;
use yii\web\BadRequestHttpException;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use yii\helpers\Url;
use yii\db\Connection;
use worldfloraonline\models\SearchForm;
use app\controllers\SearchController;
use DOMDocument;
use ggbn\SOLRQueryManager;
use Solarium;
use worldfloraonline\models\WebsitesearchForm;

/**
 * Site controller
 */
class WebsitesearchController extends Controller {
	
	
	
	public function actionSearch() {
		$model = new WebsitesearchForm();
		if (isset ( $_REQUEST ['WebsitesearchForm'] ))
		$content=$_REQUEST ['WebsitesearchForm']['content'];
		$config = SOLRQueryManager::getConfigWebsites ();
		// create a client instance
		$client = new Solarium\Client ( $config );
		// set the adapter to http
		$client->setAdapter ( 'Solarium\Core\Client\Adapter\Http' );
		
		// get a select query instance
		$query = $client->createSelect ();
		// set fields to fetch (this overrides the default setting 'all fields')
		
		
		$q = "content:".$content;
		
		$query->setQuery ( $q );
				
		// this executes the query and returns the result
		$raw_response = $client->select ( $query );

		$urls=[];
		foreach ( $raw_response as $doc) {
			$urls[]=$doc->url;
		}
		return $this->render ( 'webView', [
				"model" => $model,
				"searchedFor" => $content,
				"urls" => $urls
		] );
	}
	
	
}