CMS for the late garbage.fm
1<?php
2
3class EpisodesController extends ApplicationController {
4 static $caches_page = array("home", "index", "show", "rss");
5
6 static $episodes_per_page = 10;
7
8 public function home() {
9 $page = 0;
10 if (array_key_exists("page", $this->params))
11 $page = intval($this->params["page"]);
12
13 $this->find_episodes($page);
14 $this->render(array("action" => "index"));
15 }
16
17 public function index() {
18 $this->find_episodes();
19 $this->page_title = "Episodes";
20 }
21
22 public function rss() {
23 $this->find_episodes();
24
25 header("Content-type: application/rss+xml; charset=UTF-8");
26 $this->render(array("action" => "rss"), array("layout" => false));
27 }
28
29 public function rss_with_pending() {
30 $this->find_episodes(-1, true);
31
32 header("Content-type: application/rss+xml; charset=UTF-8");
33 $this->render(array("action" => "rss"), array("layout" => false,
34 "with_pending" => true));
35 }
36
37 public function show() {
38 $this->episode = Episode::find_by_episode_and_is_pending(
39 $this->params["id"], false);
40 if (!$this->episode)
41 throw new \ActiveRecord\RecordNotFound("can't find episode "
42 . $this->params["id"]);
43
44 $this->next_episode = Episode::find_by_episode_and_is_pending(
45 $this->episode->episode + 1, false);
46 if ($this->episode->episode > 0)
47 $this->prev_episode = Episode::find_by_episode(
48 $this->episode->episode - 1);
49
50 $this->page_title = $this->episode->episode . ": "
51 . $this->episode->title;
52
53 $this->meta_headers = array(
54 "og:title" => strtolower(Settings::fetch()->name) . "["
55 . $this->episode->episode . "]: " . $this->episode->title,
56 "og:image" => $this->episode->secure_artwork_url,
57 "twitter:card" => "player",
58 "twitter:site" => "@" . Settings::fetch()->twitter_username,
59 "twitter:player" => Settings::fetch()->secure_url
60 . "episodes/twitter_card/" . $this->episode->episode,
61 "twitter:player:width" => "290",
62 "twitter:player:height" => "58",
63 "twitter:player:stream" => $this->episode->secure_mp3_url,
64 "twitter:player:stream:content_type" => "audio/mpeg",
65 );
66 }
67
68 public function twitter_card() {
69 $this->show();
70
71 $this->render(array("action" => "twitter_card", "layout" => false));
72 }
73
74 protected function find_episodes($page = -1, $pending = false) {
75 $count = 0;
76 if ($pending)
77 $count = Episode::count();
78 else
79 $count = Episode::count_by_is_pending(false);
80
81 $this->pages = ceil($count / static::$episodes_per_page) - 1;
82
83 $conds = array("order" => "episode DESC");
84 if (!$pending)
85 $conds["conditions"] = "is_pending = 0";
86
87 if ($page == -1) {
88 /* return all episodes */
89 /* TODO: should we still limit it to something? */
90 } else {
91 $this->page = intval($page);
92
93 if ($this->page < 0 || $this->page > $this->pages)
94 throw new \ActiveRecord\RecordNotFound("invalid page "
95 . $this->page);
96
97 $conds["limit"] = static::$episodes_per_page;
98 $conds["offset"] = (static::$episodes_per_page * $this->page);
99 }
100
101 $this->episodes = Episode::find("all", $conds);
102 }
103}
104
105?>