<?php

class Blog_model extends Atapp_Model
{

    private $lang;

    public function __construct()
    {
        parent::__construct();
        $this->lang = get_contact_language();
    }

    function get($include_hidden = false)
    {
        $result = $this->db->from('_blog')
        	->order_by("publish_date", "desc")
			->get()->result_array();
        $this->load->model('at-admin/Hashtag_model', 'hashtag');
        foreach ($result as $idx => $row) {
        	$result[$idx]['hashtag'] = $this->hashtag->get_blog_hashtag($row['id']);
		}
        return $result;
    }

    function get_id($id)
    {
        $result = $this->db->where('id', $id)
			->get('_blog')->row_array();
        $hashtag = $this->db->select('h.tag, h.color, h.bgcolor')
			->from('_blog_hashtag ht')
			->join('_hashtag h', 'ht.hashtag_id = h.id')
			->where('ht.blog_id', $id)
			->get()->result_array();
		$result['hashtag'] = [];
        foreach ($hashtag as $tag) {
			$result['hashtag'][] = [
				'tag' => $tag['tag'],
				'color' => $tag['color'],
				'bgcolor' => $tag['bgcolor'],
			];
		}
        return $result;
    }

	function get_by_title($title)
	{
		$this->db->where('url', $title);
		$result = $this->db->get('_blog')->row_array();
		$this->load->model('at-admin/Hashtag_model', 'hashtag');
		$result['hashtag'] = $this->hashtag->get_blog_hashtag($result['id']);
		return $result;
	}

	function viewed($blog_id) {
    	$this->db->set('views', 'views + 1', false);
    	$this->db->where(['blog_id' => $blog_id]);
    	$this->db->update('_blog_views', []);
	}

	function get_hot_blogs() {
    	$views = $this->db->order_by('views', 'desc')
			->limit(5)
			->get('_blog_views')->result_array();
		$blog_id = [];
		foreach ($views as $row) {
			$blog_id[] = $row['blog_id'];
		}
		return $this->db->where_in('id', $blog_id)
			->get('_blog')->result_array();

	}

	function insert($arr)
	{
		$data = [
			'title' => $arr['title'],
			'description' => $arr['description'],
			'video_url' => $arr['video_url'],
			'facebook_video_id' => $arr['facebook_video_id'],
			'featured_picture' => $arr['featured_picture'],
			'url' => sanitize($arr['title']),
			'publish_date' => $arr['publish_date'],
//			'lang' => $this->config->item('system_default_language'),
		];
		$this->db->insert('_blog', $data);
		$insertID = $this->db->insert_id();
		$this->load->model('at-admin/Hashtag_model', 'hashtag');
		$this->hashtag->update($arr['hashtag'], $insertID);
		$this->db->insert('_blog_views', [
			'blog_id' => $insertID,
			'views' => 0,
		]);
		return $insertID;

	}

	function update($id, $arr)
	{
		$data = [
			'title' => $arr['title'],
			'description' => $arr['description'],
			'facebook_video_id' => $arr['facebook_video_id'],
			'video_url' => $arr['video_url'],
			'url' => sanitize($arr['title']),
			'publish_date' => $arr['publish_date'],
		];
		if ($arr['featured_picture'] != '') {
			$data['featured_picture'] = $arr['featured_picture'];
		}
		$this->db->where('id', $id);
		$this->db->update('_blog', $data);
		$this->load->model('at-admin/Hashtag_model', 'hashtag');
		$this->hashtag->update($arr['hashtag'], $id);
	}

	function delete($id)
	{
		$this->db->delete('_treatment_submenu', ['id' => $id]);
	}

	function update_rank($id, $rank)
    {
        $data = array(
            'displayorder' => $rank
        );

        $this->db->where('id', $id);
        $this->db->update('_blog', $data);
    }

}
