<?php

class PhotoLinks extends Model
{

	function __construct($fromArray = array())
	{
		$this->_tableName_ = 'photoLinks';
		parent::__construct($fromArray);
	}

	static function model()
	{
		return new self();
	}

	static function addLink($photo, $url)
	{
		$parsedUrl = parse_url($url);
		$host = isset($parsedUrl['host']) ? $parsedUrl['host'] : '';
		$photoDetails = explode('_', $photo);
		$pointId = isset($photoDetails[0]) ? (int) $photoDetails[0] : 0;

		if ($pointId) {
			App::DB()->query('INSERT INTO photoLinks VALUES ("' . $photo . '", "' . $pointId . '","' . $url . '","' . $host . '") ON DUPLICATE KEY UPDATE url=url;');
		}
	}

	static function getCaption($photo)
	{
		$photolink = self::model()->getOne('photo', $photo);

		if (!$photolink) {
			return 'не определён';
		}

		return $photolink->host ? $photolink->host : $photolink->url;
	}

}
