/**
  * List of Related Episodes
  * 
  * @accessor
  * @dynamicAccessor episode.relatedEpisodes
  */
 public static function accessorRelatedEpisodes($return, $method_name, $episode, $post, $args = array())
 {
     $episodes = array();
     foreach (EpisodeRelation::get_related_episodes($episode->id, ['only_published' => true]) as $related_episode) {
         $episodes[] = new Template\Episode(Model\Episode::find_by_id($related_episode->id));
     }
     return $episodes;
 }
 public function update_episode_relations($post_id)
 {
     if (!$post_id || !isset($_POST['_podlove_meta']['related_episodes'])) {
         return;
     }
     $new_related_episodes = $_POST['_podlove_meta']['related_episodes'];
     $episode = \Podlove\Model\Episode::find_one_by_post_id($post_id);
     foreach (\Podlove\Modules\RelatedEpisodes\Model\EpisodeRelation::find_all_by_where('left_episode_id = ' . $episode->id . ' OR right_episode_id = ' . $episode->id) as $episode_relation) {
         $episode_relation->delete();
     }
     foreach ($new_related_episodes as $episode_relation) {
         $e = new \Podlove\Modules\RelatedEpisodes\Model\EpisodeRelation();
         $e->left_episode_id = $episode->id;
         $e->right_episode_id = $episode_relation;
         $e->save();
     }
 }
 /**
  * List of Related Episodes
  * 
  * @accessor
  * @dynamicAccessor episode.relatedEpisodes
  */
 public static function accessorRelatedEpisodes($return, $method_name, $episode, $post, $args = array())
 {
     $episodes = array();
     foreach (EpisodeRelation::get_related_episodes($episode->id) as $related_episode) {
         $episodes[] = new \Podlove\Template\Episode(\Podlove\Model\Episode::find_by_id($related_episode->id));
     }
     return $episodes;
 }
    public function episode_relation_form_callback($form_base_name = '_podlove_meta')
    {
        $existing_episodes = array();
        foreach (\Podlove\Model\Episode::find_all_by_time() as $episode) {
            $existing_episodes[$episode->id] = get_the_title($episode->post_id);
        }
        $episode = \Podlove\Model\Episode::find_one_by_post_id(get_the_ID());
        $existing_episode_relations = array_map(function ($episode) {
            return $episode->to_array();
        }, \Podlove\Modules\RelatedEpisodes\Model\EpisodeRelation::get_related_episodes($episode->id));
        ?>
			<div id="episode-relation-form">
				<table class="podlove_alternating" border="0" cellspacing="0">
					<thead>
						<tr>
							<th><?php 
        _e('Episode', 'podlove');
        ?>
</th>
							<th><?php 
        _e('Remove', 'podlove');
        ?>
</th>
						</tr>
					</thead>
					<tbody id="episode_relation_table_body" style="min-height: 50px;">
						<tr class="episode_relation_table_body_placeholder" style="display: none;">
							<td><em><?php 
        echo __('No episode relations were added yet.', 'podlove');
        ?>
</em></td>
						</tr>
					</tbody>
				</table>

				<div id="add_new_episode_relation_wrapper">
					<input class="button" id="add_new_episode_relation_button" value="+" type="button" />
				</div>
			</div>

			<script type="text/template" id="episode-relation-row-template">
			<tr class="podlove-episode-relation-table">
				<td>
					<select name="<?php 
        echo $form_base_name;
        ?>
[related_episodes][{{id}}]" id="<?php 
        echo $form_base_name;
        ?>
_related_episodes_{{id}}"  class="chosen-related-episodes podlove_episode_relation_episodes_dropdown">
					<?php 
        foreach ($existing_episodes as $episode_id => $episode_title) {
            ?>
						<option value="<?php 
            echo $episode_id;
            ?>
"><?php 
            echo $episode_title;
            ?>
</option>
					<?php 
        }
        ?>
					</select>
				</td>
				<td>
					<span class="episode_relation_remove">
						<i class="clickable podlove-icon-remove"></i>
					</span>
				</td>
			</tr>
			</script>
			<script type="text/javascript">
				var PODLOVE = PODLOVE || {};
				
				PODLOVE.related_episodes_existing_episode_relations = <?php 
        echo json_encode($existing_episode_relations);
        ?>
;
			</script>
			<?php 
    }
<?php

namespace Podlove\Modules\RelatedEpisodes\Model;

use Podlove\Model\Base;
use Podlove\Model\Episode;
class EpisodeRelation extends Base
{
    public static function get_related_episodes($episode_id = FALSE)
    {
        global $wpdb;
        if (!$episode_id) {
            return array();
        }
        $sql = sprintf('SELECT
			*
			FROM
			' . Episode::table_name() . ' e
			WHERE id IN (
				SELECT right_episode_id FROM ' . self::table_name() . ' WHERE left_episode_id = %1$d
				UNION
				SELECT left_episode_id FROM ' . self::table_name() . ' WHERE right_episode_id = %1$d
			)', $episode_id);
        return Episode::find_all_by_sql($sql);
    }
}
EpisodeRelation::property('id', 'INT NOT NULL AUTO_INCREMENT PRIMARY KEY');
EpisodeRelation::property('left_episode_id', 'INT');
EpisodeRelation::property('right_episode_id', 'INT');