Subversion Repositories LCARS

Rev

Rev 198 | Blame | Compare with Previous | Last modification | View Log | RSS feed

1
<?php

require_once 'models/databases/seriometer/tables/SeriesTable.php';

class Series extends \PointedEars\PHPX\Model
{
        /**
         * (non-PHPdoc)
         * @see \PointedEars\PHPX\Model::$_persistentTable
         */

        protected static $_persistentTable = 'SeriesTable';

        /**
         * (non-PHPdoc)
         * @see \PointedEars\PHPX\Model::$_persistentId
         */

        protected static $_persistentId = 'series_id';

        /**
         * (non-PHPdoc)
         * @see \PointedEars\PHPX\Model::$_persistentProperties
         */

        protected static $_persistentProperties = array(
                'title',
                'ignore',
                'channel_id',
                'last_seen',
                'seasons',
                'showtimes',
                'url'
        );

        protected static $_urns = array(
    'wiki' => 'http://de.wikipedia.org/wiki/'
        );

        /**
         * @var int
         */

  protected $_series_id;

  /**
   * @var string
   */

  protected $_title;

  /**
   * @var bool
   */

  protected $_ignore = false;

  /**
   * @var string
   */

  protected $_channel;

  /**
   * @var string
   */

  protected $_showtimes;

  /**
   * @var array
   */

  protected $_seen;

  /**
   * @var int
   */

  protected $_last_seen;

  /**
   * @var array[string]
   */

  protected $_seasons;

  /**
   * @var string
   */

  protected $_episode_list;

  /**
   * @var array
   */

  protected $_episodes;

  /**
   * @var string
   */

  protected $_url;

  /* Computed properties */

  /**
   * Number of seen episodes
   * @var int
   */

  protected $_count;

  /**
   * The total number of episodes of this series
   * @var int
   */

  protected $_total;

  /**
   * The percentage of seen episodes
   * @var double
   */

  protected $_percentage;

  /**
   * Season ranges, consisting of arrays of the number of
   * the first and last episode of a season.
   * @var array
   */

  protected $_season_ranges;

  public function setSeries_id ($value)
  {
        $this->_series_id = (int) $value;
        return $this;
  }

  public function setTitle ($value)
  {
        $this->_title = trim((string) $value);
        return $this;
  }

  public function setIgnore ($value)
  {
        $this->_ignore = (bool) $value;
        return $this;
  }

  public function setChannel ($value)
  {
        $this->_channel = trim((string) $value);
        return $this;
  }

  public function setShowtimes ($value)
  {
        $this->_showtimes = trim((string) $value);
        return $this;
  }

  public function setLast_seen ($value)
  {
        $this->_last_seen = ($value === null
            ? $value
             : (($time = strtotime($value . ' GMT')) !== false ? $time : null));
        return $this;
  }

  public function setSeasons ($value)
  {
        $this->_seasons = is_array($value) ? $value : explode(',', $value);
        return $this;
  }

  public function setEpisode_list ($value)
  {
        $episode_list = (string) $value;

        if ($episode_list !== null)
        {
          foreach (self::$_urns as $prefix => $urn)
          {
            if (preg_match("/^{$prefix}:/", $episode_list))
            {
              $episode_list = preg_replace("/^{$prefix}:/", $urn,
                str_replace(' ', '_', $episode_list));
            }
          }
        }

        $this->_episode_list = $episode_list;

        return $this;
  }

  public function setEpisodes ($value)
  {
        $this->_episodes = is_array($value) ? $value : null;
        return $this;
  }

  public function setUrl ($value)
  {
        $this->_url = (string) $value;
        return $this;
  }

  public function getCount ()
  {
    if ($this->_count === null)
    {
      $count = 0;
      $seen = $this->seen;

      if (is_array($seen))
      {
        $count = count($seen);

        foreach ($seen as $episode_or_range)
        {
          if (is_array($episode_or_range))
          {
            $count += $episode_or_range[1] - $episode_or_range[0];
          }
        }
      }

      $this->_count = $count;
    }

    return $this->_count;
  }

  /**
   * Returns the number of episodes of this series,
   * based on the season or episodes data, preferring the former.
   *
   * @return int
   */

  public function getTotal ()
  {
    if ($this->_total === null)
    {
      if (is_array($this->seasons))
      {
        $total = array_sum($this->seasons);
        $episode_count = 1;
        $this->_season_ranges = array();

        foreach ($this->seasons as $season_key => $season_length)
        {
          if (is_int($season_key))
          {
            $this->_season_ranges[$season_key + 1] =
              array($episode_count, $episode_count + $season_length - 1);
          }

          $episode_count += $season_length;
        }
      }
      else
      {
        $total = max(array_keys($this->episodes));
      }

      $this->_total = $total;
    }

    return $this->_total;
  }

  /**
   * Returns the percentage of seen episodes
   * @return double
   */

  public function getPercentage ()
  {
    if ($this->_percentage === null)
    {
      $this->_percentage = $this->count / $this->total * 100;
    }

    return $this->_percentage;
  }
}