<?php
/**
 * Класс для генерации микроразметки schema.org
 */
class Schema extends Buyers
{
    protected $region;
    protected $country;
    protected $city;
    protected $postalCode;
    protected $additionalType;
    protected $priceRange;
    protected $openingHours;
    protected $logo;
    protected $latitude;
    protected $longitude;

    public function __construct()
    {
        $this->region = $this->settings->schema_region;
        $this->country = $this->settings->schema_country;
        $this->city = $this->settings->schema_city;
        $this->postalCode = $this->settings->schema_code;
        $this->additionalType = $this->settings->site_name;
    	$this->priceRange = $this->settings->schema_price_range;
    	$this->openingHours = $this->settings->schema_worktime;
        $this->logo = "/".$this->settings->site_logo_url;
    	$this->latitude = $this->settings->schema_lat;
    	$this->longitude = $this->settings->schema_lng;
    }

    protected function set_header($type, $context = true)
	{
        $header = new stdClass();
        if($context)
            $header->{"@context"} = "https://schema.org";
        $header->{"@type"} = $type;
        return $header;
    }

    protected function get_crumb_item($name, $url, $pos)
	{
        $crumb = $this->set_header("ListItem", false);
        $crumb->position = $pos;
        $crumb->item = new stdClass();
        $crumb->item->{"@id"} = $url;
        $crumb->item->name = $name;
        return $crumb;
    }

    public function create_breadcrumbs($page)
	{
		$microdata = $this->set_header("BreadcrumbList");
		$microdata->name = "Bread Crumbs";
		$elements = array();
		$elements[] = $this->get_crumb_item("Главная", $this->config->root_url, 0);
        $path = $this->pages->get_path($page->id);
		foreach($path AS $key => $item)
			$elements[] = $this->get_crumb_item($item->name, $this->config->root_url."/".$item->url, ($key+1));
		$microdata->itemListElement = $elements;
		return json_encode($microdata, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
	}

	public function create_local_business($page)
	{
		$microdata = $this->set_header("LocalBusiness");
		$microdata->url = $this->config->root_url;
		$microdata->name = $this->settings->site_name;
		$microdata->email = $this->settings->company_email;
		$microdata->telephone = $this->settings->company_phone;
		$microdata->image = $this->config->root_url . "/design/" . $this->settings->theme . $this->logo;
		$microdata->additionalType = $this->additionalType;
		$microdata->priceRange = $this->priceRange;
		$microdata->openingHours = $this->openingHours;

		$microdata->geo = new stdClass();
		$microdata->geo->{"@type"} = 'GeoCoordinates';
		$microdata->geo->latitude = $this->latitude;
		$microdata->geo->longitude = $this->longitude;

		$microdata->address = new stdClass();
		$microdata->address->{"@type"} = 'PostalAddress';
		$microdata->address->addressRegion = $this->city;
		$microdata->address->postalCode = $this->postalCode;
		$microdata->address->addressCountry = $this->country;
		$microdata->address->addressLocality = $this->city;
		$microdata->address->streetAddress = $this->settings->company_address;

		return json_encode($microdata, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
	}

    public function create_webpage($page)
	{
        $microdata = $this->set_header("WebPage");
        $microdata->name = $page->name;
        if(empty($page->image))
            $microdata->primaryImageOfPage = $this->config->root_url . "/design/" . $this->settings->theme . $this->logo;
        else
            $microdata->primaryImageOfPage = $this->config->root_url . "/" . $page->image->filepath;
		$microdata->description = !empty($page->description) ? $page->description : $page->meta_description;
		return json_encode($microdata, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
    }

}
