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

/**
 * Class of abstract_sitemap
 *
 * @author r.ratsun <r.ratsun@gmail.com>
 */
abstract class abstract_sitemap
{
    /**
     * @var string
     */
    const LIBRARY_SUFIX = '_sitemap_api';

    /**
     * @var array
     */
    protected $map = [];

    /**
     * @var string
     */
    protected $module_name = null;

    /**
     * @var string
     */
    protected $changefreq = 'weekly';

    /**
     * @var string
     */
    protected $priority = '0.5';

    /**
     * @var array
     */
    protected $basepage = [
        'id'    => 0,
        'alias' => '',
        'name'  => ''
    ];

    /**
     * Construct
     */
    public function __construct()
    {
        // prepare module name
        $this->prepare_module_name();

        // set static data
        $this->set_static();

        // get data
        $this->get_data();
    }

    /**
     * Get module map
     *
     * @return array
     */
    public function get_module_map()
    {

        return (array) $this->map;
    }

    /**
     * Prepare module name
     */
    protected function prepare_module_name()
    {
        if (is_null($this->module_name)) {
            $this->module_name = str_replace(self::LIBRARY_SUFIX, '', get_class($this));
        }
    }

    /**
     * Set static data
     */
    protected function set_static()
    {

        $CI         = & get_instance();
        $static_val = $CI->db->
                select('rtp.name, sc.changefreq,sc.priority,rtp.alias,sc.basepage')->
                where('sc.module_name', $this->module_name)->
                join('resource_type_page rtp', 'rtp.id_resource_type_page = sc.basepage AND rtp.active = 1', 'left')->
                get('sitemap_config sc')->row_array();

        if (!empty($static_val['changefreq'])) {
            $this->changefreq = $static_val['changefreq'];
        }

        if (!empty($static_val['priority'])) {
            $this->priority = $static_val['priority'];
        }

        if (!empty($static_val['basepage'])) {
            $this->basepage['id'] = $static_val['basepage'];
        }

        if (!empty($static_val['alias'])) {
            $this->basepage['alias'] = $static_val['alias'];
        }
        if (!empty($static_val['name'])) {
            $this->basepage['name'] = $static_val['name'];
        }
    }

    /**
     * Add map
     *
     * @param sitemap_item $map
     * @return abstract_sitemap
     */
    protected function add_map($map)
    {
        $this->map[] = $map;

        return $this;
    }

    /**
     * Get module sitemap data
     */
    protected function get_data()
    {
        
    }
}
