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


$this->load->library('form_validation');

//Check if already have a valid account
$this->load->model('CMS_model');
$isExist = $this->CMS_model->isUserExist(
    array(
        "email" => $this->data['email'],
        "social_identifier" => $this->data['social_identifier']
    )
);

$schools = $this->CMS_model->getSchools();
$this->data['schools'] = $schools;

if($isExist){
    $isLogin = $this->ion_auth->login_by_provider($this->data['social_network'],$this->data['social_identifier']);
    if($isLogin){
        $this->jsData['alerts']['registered']['redirect'] = "category-select";
    }
    redirect('home', 'refresh');
}

if(!isset($this->data['activeTab'])){
    $this->data['activeTab'] = (isset($_GET['tab'])? (($_GET['tab'] == "login")? "login" : "register"): "login");
}


if(isset($this->data['logged']) && $this->data['logged'] == TRUE){
    $this->load->model('CMS_model');
    $user = $this->ion_auth->user()->row();
    $savedVideos = $this->CMS_model->getUserSavedVideos($user->id);
    $this->jsData['alerts']['registered'] = array(
        "title" => "Logged In",
        "text" => "Successfully Logged In; redirecting ...",
        "type" => "success"
    );

    if(!empty($savedVideos)){
        $this->jsData['alerts']['registered']['redirect'] = "my_videos";
    }else{
        $this->jsData['alerts']['registered']['redirect'] = "category-select";
    }
}

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