<?php
defined('BASEPATH') or exit('No direct script access allowed');

class Page_object_title_picture_model extends Atapp_Model{

    private $lang;

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

    public function add()
    {
        $data = [
            'lang' => $this->config->item('system_default_language'),
            'cols' => 1,
            'title' => '[Untitled]',
        ];
        $this->db->insert('_page_object_title_picture_info', $data);
        $insert_id = $this->db->insert_id();
        foreach ($this->language_supported as $language) {
            if ($language == $this->config->item('system_default_language')) {
                continue;
            }
            $data = [
                'object_title_picture_info_id' => $insert_id,
                'lang' => $language,
                'title' => '[Untitled]',
            ];
            $this->db->insert('_page_object_title_picture_info_lang', $data);
        }
        return $insert_id;
    }

    public function get($id)
    {
        $result = $this->db->where('id', $id)
            ->from('_page_object_title_picture_info')
            ->get()->row_array();
        if ($this->lang != $this->config->item('system_default_language')) {
            $languages = $this->db->where('object_title_picture_info_id', $id)
                ->where('lang', $this->lang)
                ->get('_page_object_title_picture_info_lang')->row_array();
            if (! is_null($languages)) {
                $result['title'] = $languages['title'];
            }
        }
        $result['pictures'] = $this->db->where('title_picture_info_id', $result['id'])
            ->order_by('displayorder', 'ASC')
            ->from('_page_object_title_pictures')
            ->get()->result_array();
        if ($this->lang == $this->config->item('system_default_language')) {
            return $result;
        }
        foreach ($result['pictures'] as $idx => $picture) {
            $languages = $this->db->where('object_title_pictures_id', $picture['id'])
                ->where('lang', $this->lang)
                ->get('_page_object_title_pictures_lang')->row_array();
            if (! is_null($languages)) {
                $result['pictures'][$idx]['caption'] = $languages['caption'];
            }
        }
        return $result;
    }

    public function get_title_picture($id)
    {
        $result = $this->db->where('id', $id)
            ->from('_page_object_title_pictures')
            ->get()->row_array();
        if ($this->lang == $this->config->item('system_default_language')) {
            return $result;
        }
        $languages = $this->db->from('_page_object_title_pictures_lang')
            ->where(['lang' => $this->lang, 'object_title_pictures_id' => $id])
            ->get()->row_array();
        if (! is_null($languages)) {
            $result['caption'] = $languages['caption'];
        }
        return $result;
    }

    public function update_picture($params, $id)
    {
        if ($this->lang == $this->config->item('system_default_language')) {
            $data = [
                'caption' => $params['caption'],
            ];
            if ($params['path'] != '') {
                $data['path'] = $params['path'];
            }
            $this->db->where('id', $id)
                ->update('_page_object_title_pictures', $data);
            $pictures = $this->db->where(['object_title_pictures_id' => $id])
                ->get('_page_object_title_pictures_lang')->result_array();
            foreach ($pictures as $picture) {
                $data = [];
                if ($picture['caption'] == '') {
                    $data['caption'] = $params['caption'];
                }
                if (count($data) > 0) {
                    $this->db->where(['id' => $picture['id']])->update('_page_object_title_pictures_lang', $data);
                }
            }
        }
        else {
            $picture = $this->db->where(['id' => $id])
                ->get('_page_object_title_pictures')->row_array();
            $data = [];
            if ($picture['caption'] == '') {
                $data['caption'] = $params['caption'];
            }
            if ($params['path'] != '') {
                $data['path'] = $params['path'];
            }
            if (count($data) > 0) {
                $this->db->where(['id' => $id])->update('_page_object_title_pictures', $data);
            }
            $pictures = $this->db->where('object_title_pictures_id', $id)
                ->get('_page_object_title_pictures_lang')->result_array();

            foreach ($pictures as $picture) {
                $data = [];
                if ($picture['lang'] == $this->lang) {
                    $data = [
                        'caption' => $params['caption'],
                    ];
                }
                else {
                    if ($picture['caption'] == '') {
                        $data['caption'] = $params['caption'];
                    }
                }
                if (count($data) > 0) {
                    $this->db->where(['id' => $picture['id']])->update('_page_object_title_pictures_lang', $data);
                }
            }
        }

    }

    public function get_max_displayorder($id)
    {
        return $this->db->select_max('displayorder')
            ->from('_page_object_title_pictures')
            ->where(['title_picture_info_id' => $id])
            ->get()->row_array();
    }

    public function update($params, $id)
    {
        if ($this->lang == $this->config->item('system_default_language')) {
            $this->db->where('id', $id)
                ->update('_page_object_title_picture_info', $params);
            $titles = $this->db->where(['object_title_picture_info_id' => $id])
                ->get('_page_object_title_picture_info_lang')->result_array();
            foreach ($titles as $title) {
                $data = [];
                if ($title['title'] == '[Untitled]') {
                    $data['title'] = $params['title'];
                }
                if (count($data) > 0) {
                    $this->db->where(['id' => $title['id']])->update('_page_object_title_picture_info_lang', $data);
                }
            }
        }
        else {
            $title = $this->db->where(['id' => $id])
                ->get('_page_object_title_picture_info')->row_array();
            $data = [
                'cols' => $params['cols'],
            ];
            if ($title['title'] == '[Untitled]') {
                $data['title'] = $params['title'];
            }
            $this->db->where(['id' => $id])->update('_page_object_title_picture_info', $data);
            $titles = $this->db->where('object_title_picture_info_id', $id)
                ->get('_page_object_title_picture_info_lang')->result_array();
            foreach ($titles as $title) {
                $data = [];
                if ($title['lang'] == $this->lang) {
                    $data['title'] = $params['title'];
                }
                else {
                    if ($title['title'] == '[Untitled]') {
                        $data['title'] = $params['title'];
                    }
                }
                if (count($data) > 0) {
                    $this->db->where(['id' => $title['id']])->update('_page_object_title_picture_info_lang', $data);
                }
            }
        }
    }

    public function add_new_picture($params)
    {
        $data = [
            'lang' => $this->config->item('system_default_language'),
            'title_picture_info_id' => $params['title_picture_info_id'],
            'path' => $params['path'],
            'caption' => $params['caption'],
            'displayorder' => $params['displayorder'],
        ];
        $this->db->insert('_page_object_title_pictures', $data);
        $insert_id = $this->db->insert_id();
        foreach ($this->language_supported as $language) {
            if ($language == $this->config->item('system_default_language')) {
                continue;
            }
            $data = [
                'lang' => $language,
                'object_title_pictures_id' => $insert_id,
                'caption' => $params['caption'],
            ];
            $this->db->insert('_page_object_title_pictures_lang', $data);
        }
        return $insert_id;
    }

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

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

    function delete($id)
    {
        $this->db->delete('_page_object_title_picture_info', ['id' => $id]);
        $this->db->delete('_page_object_title_picture_info_lang', ['object_title_picture_info_id' => $id]);
        $pictures = $this->db->where(['title_picture_info_id' => $id])
            ->get('_page_object_title_pictures')->result_array();
        foreach ($pictures as $picture) {
            $this->db->delete('_page_object_title_pictures_lang', ['object_title_pictures_id' => $picture['id']]);
            $this->db->delete('_page_object_title_pictures', ['title_picture_info_id' => $id]);
        }
    }

    public function get_all_uploaded_pictures()
    {
        $pictures = [];
        $search = $this->picture_search_string;
        $landingmain = $this->db->like('path', $search)
            ->get('_page_object_title_pictures')->result_array();
        foreach ($landingmain as $row) {
            $pictures[] = $row['path'];
        }
        return $pictures;
    }
}
