<?php
if (!defined('entry')) {
    define('entry', true);
}
/* ===========================

  Sorbet CMS - A PHP based tumblelog CMS forked from Gelato CMS

  Sorbet CMS is a free software licensed under the GPL 3.0

  =========================== */
?>
<?php
require('../entry.php');
global $user, $conf, $tumble; $util;

if ($user->isAdmin()) {
    $message = '';
    $f = new feeds();
    if (isset($_POST['add'], $_POST['url'])) {
        $credits =(isset($_POST['credits']))? 1 : 0;
        if ($f->addFeed($_POST['url'], $_POST['type'], $_POST['source'], $credits)) {
            $message = __('Feed added');
            $messageStatus = 'exito';
        }
    }

    if (isset($_GET['delete']) && is_numeric($_GET['delete'])) {
        if ($f->removeFeed((int)$_GET['delete'])) {
            $message = __('Feed deleted');
            $messageStatus = 'error';
        }
    } ?>
	<!DOCTYPE html>
	<html>
	<head>
		<title><?php echo __("Feeds")?> - Sorbet CMS</title>
		<meta charset="utf-8" />
		<meta name="generator" content="Sorbet CMS <?= $util->version(); ?>" />
		<link rel="shortcut icon" href="<?php echo $conf->urlSorbet; ?>/images/favicon.ico" />
		<script language="javascript" type="text/javascript" src="<?php echo $conf->urlSorbet; ?>/admin/scripts/jquery.js"></script>
		<script language="javascript" type="text/javascript" src="<?php echo $conf->urlSorbet; ?>/admin/scripts/tools.js"></script>
		<script language="javascript" type="text/javascript" src="<?php echo $conf->urlSorbet; ?>/admin/scripts/jquery.validate.min.js"></script>
		<script type="text/javascript">
		$(document).ready(function(){
			$("#divMessages").fadeOut(5000,function(){
				$("#divMessages").css({display:"none"});
			});
			$("#source").change(function (){selectFeedType('<?php echo __("Feed Url:")?>','<?php echo __(" username:")?>')})

			$("#feedform").validate({
				rules: {
					url: {
						required: true
					}
				},
				errorElement: "span",
				errorClass: "validate_span",
				errorPlacement: function(label, element) {
					label.prependTo(element.prev())
				}
			});

		});
		</script>
		<style type="text/css" media="screen">
			@import "<?php echo $conf->urlSorbet; ?>/admin/css/style.css";
		</style>
	</head>

	<body>
		<div id="cont">
			<div id="head">
				<h1><a href="<?php echo $conf->urlSorbet; ?>/admin/index.php" title="<?php echo __("home")?> - Sorbet CMS">Sorbet CMS</a></h1>
				<ul id="nav">
					<li><a href="<?php echo $conf->urlSorbet; ?>/" title="<?php echo __("Take me to the tumblelog")?>"><?php echo __("Back to the Tumblelog")?></a></li>
			  	</ul>
			</div>
			<div id="main">

				<div class="box">
					<h3><?php echo __("Configure your feeds")?></h3>
					<ul class="menu manage">
					<li><a href="index.php"><?php echo __("Post")?></a></li>
                    <li><a href="options.php"><?php echo __("Options")?></a></li>
					</ul>

					<?php echo(!(empty($message))? '<div class="'.$messageStatus.'" id="divMessages"> '.$message.'</div>' : ''); ?>

					<div class="tabla">

						<form action="feeds.php" method="post" class="newpost" id="feedform">
						<fieldset>
						<ul>
							<li>
								<label for="source"><?php echo __("Import data from:")?></label>
								<select name="source" id="source">
                                	<option value="Rss" selected="selected">Rss/atom Feed</option>
                                    <option value="Twitter">Twitter</option>
                                    <option value="Youtube">Youtube</option>
                                    <option value="Last.fm">Last.fm</option>
                                    <option value="Tumblr">Tumblr</option>
									<option value="Del.icio.us">Del.icio.us</option>
                                    <option value="Wordpress.com">Wordpress.com</option>
                                    <option value="Blogger">Blogger</option>
                                    <option value="VOX">VOX</option>
                                </select>
							</li>
							<li id="import_as"><label for="type"><?php echo __("Import As:")?></label>
                                    <select name="type" id="type">
                                    	<option value="1" selected="selected"><?php echo __("Text")?></option>
                                    	<option value="2"><?php echo __("Photos")?></option>
                                    </select>
							</li>
							<li>
								<label for="url" id="url_label"><?php echo __("Feed Url (double check):")?></label>
								<input type="text" name="url" id="url" class="txt" />
							</li>
                            <li><label for="credits"><?php echo __('Show credits on each post?')?></label><input type="checkbox" name="credits" checked="checked" class="check" /></li>
							<li>
								<input name="add" type="submit" value="<?php echo __("Add"); ?>" />
							</li>
						</ul>
						</fieldset>
						</form>
						<div id="feedlist">
						  <ul>
						    <?php
                            $actual_feeds = $f->getFeedList();
    foreach ($actual_feeds as $feed) {
        if ($feed['error']>0) {
            echo '<li class="feederror"><a href="feeds.php?delete='.$feed['id_feed'].'" title="'.__('Delete this Feed').'" class="action"><img title="" alt="" src="css/images/delete.png"/></a><span class="status">'.__('Error updating').'</span> '.((!empty($feed['title']))? $feed['title'] : $feed['url']).'</li>';
        } else {
            echo '<li><a href="feeds.php?delete='.$feed['id_feed'].'" title="'.__('Delete this Feed').'" class="action"><img title="" alt="" src="css/images/delete.png"/></a><span class="status" title="'.__('Last update').': '.$feed['updated_at'].'">'.__('Importing').'</span> '.((!empty($feed['title']))? $feed['title'] : $feed['url']).'</li>';
        }
    } ?>
					      </ul>
					    </div>
				  </div>

					<div class="footer-box">&nbsp;</div>
				</div>
			</div>
			<div id="foot">
				Sorbet CMS - PHP Tumblelog Content Management System.
			</div>
		</div>
	</body>
	</html>
<?php

} else {
    header("Location: ".$conf->urlSorbet."/login.php");
}
?>
