1<?php 2/* 3 * vim:set softtabstop=4 shiftwidth=4 expandtab: 4 * 5 * LICENSE: GNU Affero General Public License, version 3 (AGPL-3.0-or-later) 6 * Copyright 2001 - 2020 Ampache.org 7 * 8 * This program is free software: you can redistribute it and/or modify 9 * it under the terms of the GNU Affero General Public License as published by 10 * the Free Software Foundation, either version 3 of the License, or 11 * (at your option) any later version. 12 * 13 * This program is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 * GNU Affero General Public License for more details. 17 * 18 * You should have received a copy of the GNU Affero General Public License 19 * along with this program. If not, see <https://www.gnu.org/licenses/>. 20 * 21 */ 22 23declare(strict_types=0); 24 25namespace Ampache\Repository\Model; 26 27use Ampache\Module\System\Dba; 28 29class TVShow_Episode extends Video 30{ 31 protected const DB_TABLENAME = 'tvshow_episode'; 32 33 public $original_name; 34 public $season; 35 public $episode_number; 36 public $summary; 37 38 public $f_link; 39 public $f_season; 40 public $f_season_link; 41 public $f_tvshow; 42 public $f_tvshow_link; 43 44 /** 45 * Constructor 46 * This pulls the tv show episode information from the database and returns 47 * a constructed object 48 * @param $episode_id 49 */ 50 public function __construct($episode_id) 51 { 52 parent::__construct($episode_id); 53 54 $info = $this->get_info($episode_id); 55 foreach ($info as $key => $value) { 56 $this->$key = $value; 57 } 58 59 return true; 60 } 61 62 public function getId(): int 63 { 64 return (int) $this->id; 65 } 66 67 /** 68 * garbage_collection 69 * 70 * This cleans out unused tv shows episodes 71 */ 72 public static function garbage_collection() 73 { 74 $sql = "DELETE FROM `tvshow_episode` USING `tvshow_episode` LEFT JOIN `video` ON `video`.`id` = `tvshow_episode`.`id` WHERE `video`.`id` IS NULL"; 75 Dba::write($sql); 76 } 77 78 /** 79 * insert 80 * Insert a new tv show episode and related entities. 81 * @param array $data 82 * @param array $gtypes 83 * @param array $options 84 * @return integer 85 */ 86 public static function insert(array $data, $gtypes = array(), $options = array()) 87 { 88 if (empty($data['tvshow'])) { 89 $data['tvshow'] = T_('Unknown'); 90 } 91 $tags = $data['genre']; 92 93 $tvshow = TvShow::check($data['tvshow'], $data['year'], $data['tvshow_summary']); 94 if ($options['gather_art'] && $tvshow && $data['tvshow_art'] && !Art::has_db((int)$tvshow, 'tvshow')) { 95 $art = new Art((int)$tvshow, 'tvshow'); 96 $art->insert_url($data['tvshow_art']); 97 } 98 $tvshow_season = TVShow_Season::check($tvshow, $data['tvshow_season']); 99 if ($options['gather_art'] && $tvshow_season && $data['tvshow_season_art'] && !Art::has_db($tvshow_season, 100 'tvshow_season')) { 101 $art = new Art($tvshow_season, 'tvshow_season'); 102 $art->insert_url($data['tvshow_season_art']); 103 } 104 105 if (is_array($tags)) { 106 foreach ($tags as $tag) { 107 $tag = trim((string)$tag); 108 if (!empty($tag)) { 109 Tag::add('tvshow_season', (int) $tvshow_season, $tag, false); 110 Tag::add('tvshow', (int) $tvshow, $tag, false); 111 } 112 } 113 } 114 115 $sdata = $data; 116 // Replace relation name with db ids 117 $sdata['tvshow'] = $tvshow; 118 $sdata['tvshow_season'] = $tvshow_season; 119 120 return self::create($sdata); 121 } 122 123 /** 124 * create 125 * This takes a key'd array of data as input and inserts a new tv show episode entry, it returns the record id 126 * @param array $data 127 * @return integer 128 */ 129 public static function create($data) 130 { 131 $sql = "INSERT INTO `tvshow_episode` (`id`, `original_name`, `season`, `episode_number`, `summary`) VALUES (?, ?, ?, ?, ?)"; 132 Dba::write($sql, array( 133 $data['id'], 134 $data['original_name'], 135 $data['tvshow_season'], 136 $data['tvshow_episode'], 137 $data['summary'] 138 )); 139 140 return $data['id']; 141 } 142 143 /** 144 * update 145 * This takes a key'd array of data as input and updates a tv show episode entry 146 * @param array $data 147 * @return integer 148 */ 149 public function update(array $data) 150 { 151 parent::update($data); 152 153 $original_name = isset($data['original_name']) ? $data['original_name'] : $this->original_name; 154 $tvshow_season = isset($data['tvshow_season']) ? $data['tvshow_season'] : $this->season; 155 $tvshow_episode = isset($data['tvshow_episode']) ? $data['tvshow_episode'] : $this->episode_number; 156 $summary = isset($data['summary']) ? $data['summary'] : $this->summary; 157 158 $sql = "UPDATE `tvshow_episode` SET `original_name` = ?, `season` = ?, `episode_number` = ?, `summary` = ? WHERE `id` = ?"; 159 Dba::write($sql, array($original_name, $tvshow_season, $tvshow_episode, $summary, $this->id)); 160 161 $this->original_name = $original_name; 162 $this->season = $tvshow_season; 163 $this->episode_number = $tvshow_episode; 164 $this->summary = $summary; 165 166 return $this->id; 167 } 168 169 /** 170 * format 171 * this function takes the object and formats some values 172 * @param boolean $details 173 * @return boolean 174 */ 175 public function format($details = true) 176 { 177 parent::format($details); 178 179 $season = new TVShow_Season($this->season); 180 $season->format($details); 181 182 $this->f_title = ($this->original_name ?: $this->f_title); 183 $this->f_link = '<a href="' . $this->link . '">' . scrub_out($this->f_title) . '</a>'; 184 $this->f_season = $season->f_name; 185 $this->f_season_link = $season->f_link; 186 $this->f_tvshow = $season->f_tvshow; 187 $this->f_tvshow_link = $season->f_tvshow_link; 188 189 $this->f_file = $this->f_tvshow; 190 if ($this->episode_number) { 191 $this->f_file .= ' - S' . sprintf('%02d', $season->season_number) . 'E' . sprintf('%02d', 192 $this->episode_number); 193 } 194 $this->f_file .= ' - ' . $this->f_title; 195 $this->f_full_title = $this->f_file; 196 197 return true; 198 } 199 200 /** 201 * get_keywords 202 * @return array 203 */ 204 public function get_keywords() 205 { 206 $keywords = parent::get_keywords(); 207 $keywords['tvshow'] = array( 208 'important' => true, 209 'label' => T_('TV Show'), 210 'value' => $this->f_tvshow 211 ); 212 $keywords['tvshow_season'] = array( 213 'important' => false, 214 'label' => T_('Season'), 215 'value' => $this->f_season 216 ); 217 if ($this->episode_number) { 218 $keywords['tvshow_episode'] = array( 219 'important' => false, 220 'label' => T_('Episode'), 221 'value' => $this->episode_number 222 ); 223 } 224 $keywords['type'] = array( 225 'important' => false, 226 'label' => null, 227 'value' => 'tvshow' 228 ); 229 230 return $keywords; 231 } 232 233 /** 234 * get_parent 235 * @return array 236 */ 237 public function get_parent() 238 { 239 return array('object_type' => 'tvshow_season', 'object_id' => $this->season); 240 } 241 242 /** 243 * get_release_item_art 244 * @return array 245 */ 246 public function get_release_item_art() 247 { 248 return array( 249 'object_type' => 'tvshow_season', 250 'object_id' => $this->season 251 ); 252 } 253 254 /** 255 * @return string 256 */ 257 public function get_description() 258 { 259 if (!empty($this->summary)) { 260 return $this->summary; 261 } 262 263 $season = new TVShow_Season($this->season); 264 265 return $season->get_description(); 266 } 267 268 /** 269 * display_art 270 * @param integer $thumb 271 * @param boolean $force 272 */ 273 public function display_art($thumb = 2, $force = false) 274 { 275 $episode_id = null; 276 $type = null; 277 278 if (Art::has_db($this->id, 'video')) { 279 $episode_id = $this->id; 280 $type = 'video'; 281 } else { 282 if (Art::has_db($this->season, 'tvshow_season')) { 283 $episode_id = $this->season; 284 $type = 'tvshow_season'; 285 } else { 286 $season = new TVShow_Season($this->season); 287 if (Art::has_db($season->tvshow, 'tvshow') || $force) { 288 $episode_id = $season->tvshow; 289 $type = 'tvshow'; 290 } 291 } 292 } 293 294 if ($episode_id !== null && $type !== null) { 295 Art::display($type, $episode_id, $this->get_fullname(), $thumb, $this->link); 296 } 297 } 298 299 /** 300 * Remove the video from disk. 301 */ 302 public function remove() 303 { 304 $deleted = parent::remove(); 305 if ($deleted) { 306 $sql = "DELETE FROM `tvshow_episode` WHERE `id` = ?"; 307 $deleted = Dba::write($sql, array($this->id)); 308 } 309 310 return $deleted; 311 } 312} 313