<?php
/**
 * Created by PhpStorm.
 * User: lambasoft
 * Date: 8/29/17
 * Time: 8:09 AM
 */

$this->load->model('CMS_model');

if(!empty($this->input->get("search"))){
    $search = preg_replace("/[^a-zA-Z0-9 ]+/", "", $this->input->get("search"));
    $search = substr($search,0,30);

    $this->data['videos'] = $this->CMS_model->getVideosBySearch($search);
    $categoriesID = array();
    foreach ($this->data['videos'] as $video) {
        $categoriesID[] = $video['link_category_id'];
    }
    $categoriesID = array_unique($categoriesID);
    $this->data['categories'] = $this->CMS_model->getCategories($categoriesID);
    
}else{
    $categoriesID = empty($this->input->post("categories"))? $this->input->get("categories") : $this->input->post("categories");
    $this->load->library('session');
    $this->session->set_userdata('search_categories', $categoriesID);
    $categoriesID = explode(",",$categoriesID);
    if(!ctype_digit(implode('',$categoriesID))){
        $categoriesID = array();
    }

    $this->data['categories'] = $this->CMS_model->getCategories($categoriesID);
    $this->data['category'] = $this->CMS_model->getCategory($categoriesID);
    $this->data['videos'] = $this->CMS_model->getVideosByCategories($categoriesID);
}


$_count = count($this->data['categories']);
for($i=0;$i < $_count; $i++){
    $this->data['categories'][$i]['category_count'] = 0;
    foreach ($this->data['videos'] as $video) {
        if($this->data['categories'][$i]['id'] == $video['link_category_id']){
            $this->data['categories'][$i]['category_count']  += 1;
        }
    }
}

$this->data['videos_pick'] = $this->CMS_model->getVideosPick();
$this->data['_categories'] = $this->CMS_model->getCategories();

$this->load->library('session');
$this->session->set_userdata(
    array(
        "search_videos" => array_map(function($item){ return $item['id']; },$this->data['videos']),
        "search_categories" => $categoriesID
    )
);


include("sliders.php");