<?php
/**
 * Created by PhpStorm.
 * User: lambasoft
 * Date: 8/29/17
 * Time: 8:09 AM
 */
function countArrayOccurrence($array,$search){
    $count = 0;
    foreach ($array as $item) {
        if ($item === $search) {
            $count++;
        }
    }
    return $count;
}

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

if($this->ion_auth->logged_in()){
    $user = $this->ion_auth->user()->row();
    if($this->input->get("videos") == "likes"){
        $this->data['videos'] = $this->CMS_model->getUserLikedVideos($user->id);
    }else{
        $this->data['videos'] = $this->CMS_model->getUserSavedVideos($user->id);
    }


    $categoriesID = array();
    foreach ($this->data['videos'] as $video) {
        $categoriesID = array_merge(array_map(function($item){
            return $item['id'];
        },$this->CMS_model->getVideoCategories($video['id'])),$categoriesID);
    }
    $_categoriesID = $categoriesID;
    $categoriesID = array_unique($categoriesID);
    $this->data['categories'] = $this->CMS_model->getCategories($categoriesID);

    $_count = count($this->data['categories']);
    for($i=0;$i < $_count; $i++){
        $this->data['categories'][$i]['category_count'] = countArrayOccurrence($_categoriesID, $this->data['categories'][$i]['id']);
    }

    $this->data['_categories'] = $this->CMS_model->getCategories();
}else{
    redirect("home");
}
