public function getEpisode()
 {
     return \Podlove\Model\Episode::find_one_by_id($this->episode_id);
 }
    /**
     * Episodes
     *
     * Filter and order episodes with parameters:
     * 
     * - group: Filter by contribution group. Default: ''.
     * - role: Filter by contribution role. Default: ''.
     * - post_status: Publication status of the post. Defaults to 'publish'
     * - order: Designates the ascending or descending order of the 'orderby' parameter. Defaults to 'DESC'.
     *   - 'ASC' - ascending order from lowest to highest values (1, 2, 3; a, b, c).
     *   - 'DESC' - descending order from highest to lowest values (3, 2, 1; c, b, a).
     * - orderby: Sort retrieved episodes by parameter. Defaults to 'publicationDate'.
     *   - 'publicationDate' - Order by publication date.
     *   - 'recordingDate' - Order by recording date.
     *   - 'title' - Order by title.
     *   - 'slug' - Order by episode slug.
     *	 - 'limit' - Limit the number of returned episodes.
     */
    public function episodes($args = [])
    {
        return $this->with_blog_scope(function () use($args) {
            global $wpdb;
            $joins = "";
            if (isset($args['group']) && $args['group']) {
                $joins .= "INNER JOIN " . ContributorGroup::table_name() . " g ON g.id = ec.group_id AND g.slug = '" . esc_sql($args['group']) . "'";
            }
            if (isset($args['role']) && $args['role']) {
                $joins .= "INNER JOIN " . ContributorRole::table_name() . " r ON r.id = ec.role_id AND r.slug = '" . esc_sql($args['role']) . "'";
            }
            $where = "ec.contributor_id = " . (int) $this->id;
            if (isset($args['post_status']) && in_array($args['post_status'], get_post_stati())) {
                $where .= " AND p.post_status = '" . $args['post_status'] . "'";
            } else {
                $where .= " AND p.post_status = 'publish'";
            }
            // order
            $order_map = array('publicationDate' => 'p.post_date', 'recordingDate' => 'e.recordingDate', 'slug' => 'e.slug', 'title' => 'p.post_title');
            if (isset($args['orderby']) && isset($order_map[$args['orderby']])) {
                $orderby = $order_map[$args['orderby']];
            } else {
                $orderby = $order_map['publicationDate'];
            }
            if (isset($args['order'])) {
                $args['order'] = strtoupper($args['order']);
                if (in_array($args['order'], array('ASC', 'DESC'))) {
                    $order = $args['order'];
                } else {
                    $order = 'DESC';
                }
            } else {
                $order = 'DESC';
            }
            if (isset($args['limit'])) {
                $limit = ' LIMIT ' . (int) $args['limit'];
            } else {
                $limit = '';
            }
            $sql = '
				SELECT
					ec.episode_id
				FROM
					' . EpisodeContribution::table_name() . ' ec
					INNER JOIN ' . \Podlove\Model\Episode::table_name() . ' e ON e.id = ec.episode_id
					INNER JOIN ' . $wpdb->posts . ' p ON p.ID = e.post_id
					' . $joins . '
				WHERE ' . $where . '
				GROUP BY ec.episode_id
				ORDER BY ' . $orderby . ' ' . $order . $limit;
            $episode_ids = $wpdb->get_col($sql);
            return array_map(function ($episode_id) {
                return \Podlove\Model\Episode::find_one_by_id($episode_id);
            }, array_unique($episode_ids));
        });
    }
    public static function chart()
    {
        $episode = Model\Episode::find_one_by_id((int) $_REQUEST['episode']);
        $post = get_post($episode->post_id);
        ?>
		<div id="chart-zoom-selection" class="chart-menubar">
			<span>Zoom</span>
			<a href="#" data-hours="24" class="button button-secondary">1d</a>
			<a href="#" data-hours="168" class="button button-secondary">1w</a>
			<a href="#" data-hours="672" class="button button-secondary">4w</a>
			<a href="#" data-hours="0" class="button button-secondary">all</a>
		</div>

		<div id="chart-grouping-selection" class="chart-menubar">
			<span>Unit</span>
			<a href="#" data-hours="1" class="button button-secondary">1h</a>
			<a href="#" data-hours="2" class="button button-secondary">2h</a>
			<!-- <a href="#" data-hours="3" class="button button-secondary">3h</a> -->
			<a href="#" data-hours="4" class="button button-secondary">4h</a>
			<a href="#" data-hours="6" class="button button-secondary">6h</a>
			<a href="#" data-hours="12" class="button button-secondary">12h</a>
			<a href="#" data-hours="24" class="button button-secondary">1d</a>
			<a href="#" data-hours="168" class="button button-secondary">1w</a>
			<a href="#" data-hours="672" class="button button-secondary">4w</a>
		</div>

		<div id="episode-performance-chart" data-episode="<?php 
        echo $episode->id;
        ?>
">
		</div>

		<div id="episode-range-chart"></div>

		<section id="episode-source-chart-wrapper" class="chart-wrapper" data-tile-id="download_source">
			<div id="episode-source-chart">
				<h1>Download Source <a href="#" class="reset" style="display: none"><small>reset</small></a></h1>
			</div>
		</section>

		<section id="episode-context-chart-wrapper" class="chart-wrapper" data-tile-id="download_context">
			<div id="episode-context-chart">
				<h1>Download Context <a href="#" class="reset" style="display: none"><small>reset</small></a></h1>
			</div>
		</section>

		<section id="episode-asset-chart-wrapper" class="chart-wrapper" data-tile-id="asset">
			<div id="episode-asset-chart">
				<h1>Episode Asset <a href="#" class="reset" style="display: none"><small>reset</small></a></h1>
			</div>
		</section>

		<section id="episode-client-chart-wrapper" class="chart-wrapper" data-tile-id="podcast_client">
			<div id="episode-client-chart">
				<h1>Podcast Client <a href="#" class="reset" style="display: none"><small>reset</small></a></h1>
			</div>
		</section>

		<section id="episode-system-chart-wrapper" class="chart-wrapper" data-tile-id="operating_system">
			<div id="episode-system-chart">
				<h1>Operating System <a href="#" class="reset" style="display: none"><small>reset</small></a></h1>
			</div>
		</section>

		<section id="episode-weekday-chart-wrapper" class="chart-wrapper" data-tile-id="day_of_week">
			<div id="episode-weekday-chart">
				<h1>Day of Week <a href="#" class="reset" style="display: none"><small>reset</small></a></h1>
			</div>
		</section>

		<div style="clear: both"></div>

		<script type="text/javascript">
		var assetNames = <?php 
        $assets = Model\EpisodeAsset::all();
        echo json_encode(array_combine(array_map(function ($a) {
            return $a->id;
        }, $assets), array_map(function ($a) {
            return $a->title;
        }, $assets)));
        ?>
;
		</script>

		<style type="text/css">
		section.chart-wrapper {
			float: left;
			height: 320px;
		}

		section.chart-wrapper h1 {
			font-size: 14px;
			margin-left: 10px;
		}

		section.chart-wrapper div {
			width: 285px;
			height: 285px;
		}

		.chart-wrapper h1, 
		.chart-wrapper h1 small {
			line-height: 19px;
			height: 19px;
		}

		.chart-wrapper h1 a {
			text-decoration: none;
		}

		.chart-menubar:first-child { float: right; }
		.chart-menubar:last-child  { float: left; }

		.chart-menubar span { line-height: 26px; }

		#episode-performance-chart {
			float: none;
			height: 250px
		}

		#episode-range-chart {
			float: none;
			height: 80px;
			margin-top: -15px;
		}

		#episode-source-chart g.row text,
		#episode-context-chart g.row text,
		#episode-weekday-chart g.row text,
		#episode-client-chart g.row text,
		#episode-system-chart g.row text,
		#episode-asset-chart g.row text {
			fill: black;
		}
		</style>

		<?php 
    }