<?php
#CMS - CMS Made Simple
#(c)2004 by Ted Kulp (wishy@users.sf.net)
#Visit our homepage at: http://www.cmsmadesimple.org
#
#This program is free software; you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation; either version 2 of the License, or
#(at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.
#You should have received a copy of the GNU General Public License
#along with this program; if not, write to the Free Software
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

function smarty_function_rss2array($params, &$smarty)
{


  if (!isset($params["rss"])) {
    echo "Missing the rss-url. Bailing... ";
    return;
  }

  $file=file_get_contents($params["rss"]);
  if (!$file) {
    echo "Error reading the rss-feed from url: ".$params["rss"];
    return;
  }
  $xml = simplexml_load_string($file,null, LIBXML_NOCDATA);
  $json = json_encode($xml);
  $data= json_decode($json,TRUE);

  $info=array(
    "title"=>$data["channel"]["title"],
    "description"=>$data["channel"]["description"],
    "language"=>$data["channel"]["language"],
    "copyright"=>$data["channel"]["copyright"],
    "webMaster"=>$data["channel"]["webMaster"],
  );



  $items=$data["channel"]["item"];
  if (is_numeric($params["limit"])) {
    $info["limit"]=$params["limit"];
    if (is_numeric($params["offset"])) {
      $info["offset"]=$params["offset"];
      $items=array_slice($items,$params["offset"], $params["limit"]);
    } else {
      $items=array_slice($items,0, $params["limit"]);
    }
  } elseif (is_numeric($params["offset"])) {
    $info["offset"]=$params["offset"];
    $items=array_slice($items,$params["offset"]);
  }
  //echo count($items);

  $info["count"]=count($items);

  if (isset($params["feedinfoname"])) {
    $smarty->assign($params["feedinfoname"],$info);
  } else {
    $smarty->assign("rssfeedinfo",$info);
  }

  if (isset($params["feedname"])) {
    $smarty->assign($params["feedname"],$items);
  } else {
    $smarty->assign("rssfeed",$items);
  }
  
}

function smarty_cms_help_function_rss2array() {
  ?>
  <p>Populate a Smarty-array with the entries of a rss-feed:</p>
  <pre>
		{rss2array rss='https://myrssfeedurl.com' feedname='rssfeed'}
		{foreach from=$rssfeed item=entry}
    &lt;a href="{$entry->link}"&gt;{$entry->title}&lt;/a&gt;
		{/foreach}
	</pre>
  <p>Parameters:</p>
  <ul>
    <li>rss - (required) the url of the rss-feed</li>
    <li>feedname - (default: rssfeed) name of the smartyvar containing the entries</li>
    <li>feedinfoname - (default: rssfeedinfo) name of the smartyvar containing the general information about the feed</li>
    <li>limit - limit to this number of items</li>
    <li>offset - start showing from this offset</li>
  </ul>
  <?php
}

function smarty_cms_about_function_rss2array() {
  ?>
  <p>Author: Silmarillion</p>

  <p>Change History:</p>
  <ul>
    <li>initial version</li>
  </ul>
  <?php
}

?>
