<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * Description of seo_model
 *
 * @author Igor Ratsun
 */
class seo_model extends CI_Model {

    public function get_page_data($id_page = 0) {
        // Prepare data
        $id_page = intval($id_page);

        if ($id_page != 0) {
            $this->db->where('rtp.id_resource_type_page', $id_page);
        }
        
        // Hide private page
        if (!$this->rt_user->isAllowed('resmng', 'private')) {
            $this->db->where('r.private', FALSE);
        }

        $page_data = $this->db->
                        select('rtp.name, rtpd.id_resource_type_page')->
                        where('rtp.id_resource_type_page = rtpd.id_resource_type_page')->
                        where('rtp.id_resource_type_page = r.id_item')->
                        where('r.deleted', FALSE)->
                        where('r.id_resource_type', 3)->
                        order_by('rtp.name, rtpd.id_resource_type_page', 'ASC')->
                        group_by('rtp.name')->
                        get('resource_type_page rtp, resource_type_page_data rtpd, resource r')->result_array();

        if (empty($page_data)) {
            return FALSE;
        }

        $rs = array(
            'pages' => $page_data
        );

        $page = array_shift($page_data);

        $sql = $this->db->
                        select('lan.id_lan, lan.name, rtpd.seo_title, rtpd.seo_keywords, rtpd.seo_description')->
                        join('resource_type_page_data rtpd', 'rtpd.id_lan = lan.id_lan AND rtpd.id_resource_type_page = ' . $page['id_resource_type_page'], 'left')->
                        where('lan.active', TRUE)->
                        order_by('lan.id_lan', 'ASC')->
                        get('lan')->result_array();

        return array_merge($rs, $this->prepare_array($sql));
    }

    public function save() {
        $id_resource_page = intval($this->input->post('id_page'));
        if (empty($id_resource_page)) {
            return FALSE;
        }

        $lang = $this->db->
                        select('id_lan')->
                        where('active', TRUE)->
                        get('lan')->result_array();

        // Clear old data
        $this->db->delete('resource_type_page_data', array('id_resource_type_page' => $id_resource_page));

        // Insert data
        foreach ($lang as $v) {
            $this->db->insert('resource_type_page_data', array(
                'id_resource_type_page' => $id_resource_page,
                'id_lan' => $v['id_lan'],
                'seo_title' => $this->input->post('seo_title_' . $v['id_lan']),
                'seo_keywords' => $this->input->post('seo_keywords_' . $v['id_lan']),
                'seo_description' => $this->input->post('seo_description_' . $v['id_lan'])
            ));
        }

        return TRUE;
    }

    protected function prepare_array($array) {
        $rs = array(
            'seo_title' => array(),
            'seo_keywords' => array(),
            'seo_description' => array()
        );
        foreach ($array as $value) {
            $rs['seo_title'][] = array('name' => 'seo_title_' . $value['id_lan'], 'value' => $value['seo_title'], 'lang' => $value['name']);
            $rs['seo_keywords'][] = array('name' => 'seo_keywords_' . $value['id_lan'], 'value' => $value['seo_keywords'], 'lang' => $value['name']);
            $rs['seo_description'][] = array('name' => 'seo_description_' . $value['id_lan'], 'value' => $value['seo_description'], 'lang' => $value['name']);
        }

        return $rs;
    }

}

