<?php namespace App\Modules\Socialmedia\Controllers;
use App\Modules\generic\Models\Mdl_generic;

use App\Modules\Socialmedia\Models\Mdl_socialMedia;

use CodeIgniter\Controller;

class Socialmedia extends Controller
{
   private $db;
 public $viewFile;
 //public $path;
 public $captcha; 
// public $path;
    /**
     * Constructor.
     */
    public function __construct()
    {
        //$db = \Config\Database::connect();
        $this->Mdl_socialMedia = new Mdl_socialMedia();
    }

/*
    function index(){	
        //parent::Controller();
		$query_1 = 'SELECT * FROM banner WHERE status = "1" ORDER BY createdDate desc';
		$data['banners'] = $this->Mdl_home->customQuery($query_1);

		// $query_2 = 'SELECT title, slug, SubString(content, 1, 80) AS content FROM announcement WHERE status = "1" ORDER BY createdDate desc LIMIT 3';
		// $data['announcement'] = $this->Mdl_home->customQuery($query_2);

		$query_2 = 'SELECT SubString(name, 1, 80)AS name, post_date  FROM circulars WHERE status = "1" ORDER BY post_date desc LIMIT 3';
		$data['announcement'] = $this->Mdl_home->customQuery($query_2);
		// echo '<pre>';
		// print_r($data['announcement']);exit;
		// $data['announcement'] = "No Data";

		$query_3 = 'SELECT country, SubString(enquiry, 1, 80) AS enquiry FROM business_leads WHERE status = "1" ORDER BY createdDate desc LIMIT 3';
		$data['business'] = $this->Mdl_home->customQuery($query_3);

		$query_3 = 'SELECT * FROM advertise WHERE status = "1" ORDER BY createdDate desc';
		$data['advertise'] = $this->Mdl_home->customQuery($query_3);

        //$data['magazine'] = $this->Mdl_home->getByValue("e_magazine","id",1);
        $data['magazine'] = $this->Mdl_home->getByValue("magazine","id",1);

		$template = 'home';
		$data['country'] = $this->Mdl_home->getAllRecords("country");
		$data['seo'] = $this->Mdl_home->getByValue("seo","page","home");
		$data['view'] = 'home';
		$data['scriptFile'] = 'home';
		$data['module'] = "home";
		$data['viewFile'] = 'home';
		
		$template = 'home';
		
		//echo Modules::run('template/'.$template, $data); 
		return view('template/'.$template, $data);
	}
	

*/


	/*
  **  Get Social Media URL
  */
	function getMediaUrl($media){
    if($media !== ""){
      $data = $this->Mdl_socialMedia->getByValue("social_media","name",$media);
      if($data !== "No Data"){
        return $data[0]->url;
      }else{
        return "javascript:void(0);";
      }
    }				
  }
  
  /*
  ** Listing Page
  */
  function viewall(){
    if(!Modules::run('security/isAdmin')){
      redirect('admin','refresh');
    }

    
    $admin = $this->session->userdata('admin');
    $arr = $admin['rights'];
    $rights = explode(",",$arr);

    if(!in_array("4",$rights)){
      redirect('admin/dashboard','refresh');
    }

    $template = 'admin';
    $data['scriptFile'] = 'admin-sm';
    $data['viewFile'] = 'list-sm';
    $data['module'] = "socialmedia";
    echo Modules::run('template/'.$template, $data);
  }

  /*
  ** Get List Of SM
  */
  function getSM(){
    $list = $this->Mdl_socialMedia->get_datatables();
    $data = array();
    $no = $_POST['start'];

    foreach ($list as $val) {
      $row = array();
      $url = base_url().'social-media/update/'.$val->id;
      $row[] = '<a class="btn waves-effect waves-light btn-success" href="'.$url.'">EDIT</a>';

      $row[] = $val->id;
      $row[] = $val->name;
      $row[] = $val->url;
      $data[] = $row;      
    }

    $output = array(
      "draw" => $_POST['draw'],
      "recordsTotal" => $this->Mdl_socialMedia->count_all(),
      "recordsFiltered" => $this->Mdl_socialMedia->count_filtered(),
      "data" => $data,
    );
    
    echo json_encode($output);
  }

  /*
  ** Update Page 
  */
  function update($id){
    if(!Modules::run('security/isAdmin')){
      redirect('admin','refresh');
    }

    
    $admin = $this->session->userdata('admin');
    $arr = $admin['rights'];
    $rights = explode(",",$arr);

    if(!in_array("4",$rights)){
      redirect('admin/dashboard','refresh');
    }

    $result = $this->Mdl_socialMedia->getByValue("social_media","id",$id);
    
    if($result == "No Data"){
      redirect('errors');
    }

    $data['social'] = $result;
    $data['viewFile'] = "update-sm";
    $data['scriptFile'] = "admin-sm";
    $data['module'] = "socialmedia";
    $template = 'admin';
    echo Modules::run('template/'.$template, $data);
  }

  /*
  ** Update Action 
  */
  function updateAction(){
    $content = $this->input->post();
    
    $this->form_validation->set_rules("url","url","trim|required|xss_clean|valid_url|max_length[100] ",
      array(
      'required' => "Please provide URL",
      'valid_url' => "Please enter valid URL",
      'max_length' => "Title is invalid."
    ));

    if($this->form_validation->run() == FALSE){
      $errors = $this->form_validation->error_array();
      echo json_encode($errors);
    }else{
      
      $data = array(
        'url' => strip_tags($content['url'])
      );

      $insert = $this->Mdl_socialMedia->update("social_media","id",$content['id'],$data);
      $result = array(
        "status"=>"success"
      );
      echo json_encode($result); exit;
    }
  }
}
