<?php

class Trips extends Model
{

	const STATUS_PRIVATE = 'PRIVATE';
	const STATUS_PUBLIC = 'PUBLIC';
	const STATUS_DELETED = 'DELETED';

	function __construct($fromArray = array())
	{
		$this->_tableName_ = 'trips';
		parent::__construct($fromArray);
		$this->setRelation('tracks', 'id', 'Tracks', 'tripId', self::TO_MANY, '`id` ASC');
	}

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

	function distance()
	{
		return Geo::formatedDistance($this->distance);
	}

	/**
	 * Считает/пересчитывает длину трека и всех его фрагментов
	 */
	function calculateDist()
	{
		$tracks = $this->tracks;
		$distance = 0;

		foreach ($tracks as $track) {
			$distance += $track->calculateDist();
		}

		$this->distance = $distance;
		$this->save();

		return $distance;
	}
}
