Subversion Repositories LCARS

Rev

Rev 194 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
194 PointedEar 1
<?php
2
 
200 PointedEar 3
require_once 'models/databases/seriometer/tables/SeriesTable.php';
194 PointedEar 4
 
5
class Series extends \PointedEars\PHPX\Model
6
{
7
        /**
8
         * (non-PHPdoc)
9
         * @see \PointedEars\PHPX\Model::$_persistentTable
10
         */
11
        protected static $_persistentTable = 'SeriesTable';
12
 
13
        /**
14
         * (non-PHPdoc)
15
         * @see \PointedEars\PHPX\Model::$_persistentId
16
         */
17
        protected static $_persistentId = 'series_id';
18
 
19
        /**
20
         * (non-PHPdoc)
21
         * @see \PointedEars\PHPX\Model::$_persistentProperties
22
         */
23
        protected static $_persistentProperties = array(
24
                'title',
25
                'ignore',
26
                'channel_id',
27
                'last_seen',
28
                'seasons',
29
                'showtimes',
30
                'url'
31
        );
32
 
33
        protected static $_urns = array(
34
    'wiki' => 'http://de.wikipedia.org/wiki/'
35
        );
36
 
37
        /**
38
         * @var int
39
         */
40
  protected $_series_id;
41
 
42
  /**
43
   * @var string
44
   */
45
  protected $_title;
46
 
47
  /**
48
   * @var bool
49
   */
50
  protected $_ignore = false;
51
 
52
  /**
53
   * @var string
54
   */
55
  protected $_channel;
56
 
57
  /**
58
   * @var string
59
   */
60
  protected $_showtimes;
61
 
62
  /**
63
   * @var array
64
   */
65
  protected $_seen;
66
 
67
  /**
68
   * @var int
69
   */
70
  protected $_last_seen;
71
 
72
  /**
73
   * @var array[string]
74
   */
75
  protected $_seasons;
76
 
77
  /**
78
   * @var string
79
   */
80
  protected $_episode_list;
81
 
82
  /**
83
   * @var array
84
   */
85
  protected $_episodes;
86
 
87
  /**
88
   * @var string
89
   */
90
  protected $_url;
91
 
92
  /* Computed properties */
93
 
94
  /**
95
   * Number of seen episodes
96
   * @var int
97
   */
98
  protected $_count;
99
 
100
  /**
101
   * The total number of episodes of this series
102
   * @var int
103
   */
104
  protected $_total;
105
 
106
  /**
107
   * The percentage of seen episodes
108
   * @var double
109
   */
110
  protected $_percentage;
111
 
112
  /**
113
   * Season ranges, consisting of arrays of the number of
114
   * the first and last episode of a season.
115
   * @var array
116
   */
117
  protected $_season_ranges;
118
 
119
  public function setSeries_id ($value)
120
  {
121
        $this->_series_id = (int) $value;
122
        return $this;
123
  }
124
 
125
  public function setTitle ($value)
126
  {
127
        $this->_title = trim((string) $value);
128
        return $this;
129
  }
130
 
131
  public function setIgnore ($value)
132
  {
133
        $this->_ignore = (bool) $value;
134
        return $this;
135
  }
136
 
137
  public function setChannel ($value)
138
  {
139
        $this->_channel = trim((string) $value);
140
        return $this;
141
  }
142
 
143
  public function setShowtimes ($value)
144
  {
145
        $this->_showtimes = trim((string) $value);
146
        return $this;
147
  }
148
 
149
  public function setLast_seen ($value)
150
  {
151
        $this->_last_seen = ($value === null
152
            ? $value
153
             : (($time = strtotime($value . ' GMT')) !== false ? $time : null));
154
        return $this;
155
  }
156
 
157
  public function setSeasons ($value)
158
  {
159
        $this->_seasons = is_array($value) ? $value : explode(',', $value);
160
        return $this;
161
  }
162
 
163
  public function setEpisode_list ($value)
164
  {
165
        $episode_list = (string) $value;
166
 
167
        if ($episode_list !== null)
168
        {
169
          foreach (self::$_urns as $prefix => $urn)
170
          {
171
            if (preg_match("/^{$prefix}:/", $episode_list))
172
            {
173
              $episode_list = preg_replace("/^{$prefix}:/", $urn,
174
                str_replace(' ', '_', $episode_list));
175
            }
176
          }
177
        }
178
 
179
        $this->_episode_list = $episode_list;
180
 
181
        return $this;
182
  }
183
 
184
  public function setEpisodes ($value)
185
  {
186
        $this->_episodes = is_array($value) ? $value : null;
187
        return $this;
188
  }
189
 
190
  public function setUrl ($value)
191
  {
192
        $this->_url = (string) $value;
193
        return $this;
194
  }
195
 
196
  public function getCount ()
197
  {
198
    if ($this->_count === null)
199
    {
200
      $count = 0;
201
      $seen = $this->seen;
202
 
203
      if (is_array($seen))
204
      {
205
        $count = count($seen);
206
 
207
        foreach ($seen as $episode_or_range)
208
        {
209
          if (is_array($episode_or_range))
210
          {
211
            $count += $episode_or_range[1] - $episode_or_range[0];
212
          }
213
        }
214
      }
215
 
216
      $this->_count = $count;
217
    }
218
 
219
    return $this->_count;
220
  }
221
 
222
  /**
223
   * Returns the number of episodes of this series,
224
   * based on the season or episodes data, preferring the former.
225
   *
226
   * @return int
227
   */
228
  public function getTotal ()
229
  {
230
    if ($this->_total === null)
231
    {
232
      if (is_array($this->seasons))
233
      {
234
        $total = array_sum($this->seasons);
235
        $episode_count = 1;
236
        $this->_season_ranges = array();
237
 
238
        foreach ($this->seasons as $season_key => $season_length)
239
        {
240
          if (is_int($season_key))
241
          {
242
            $this->_season_ranges[$season_key + 1] =
243
              array($episode_count, $episode_count + $season_length - 1);
244
          }
245
 
246
          $episode_count += $season_length;
247
        }
248
      }
249
      else
250
      {
251
        $total = max(array_keys($this->episodes));
252
      }
253
 
254
      $this->_total = $total;
255
    }
256
 
257
    return $this->_total;
258
  }
259
 
260
  /**
261
   * Returns the percentage of seen episodes
262
   * @return double
263
   */
264
  public function getPercentage ()
265
  {
266
    if ($this->_percentage === null)
267
    {
268
      $this->_percentage = $this->count / $this->total * 100;
269
    }
270
 
271
    return $this->_percentage;
272
  }
273
}