<?php
  /**
   * Copyright 2011 Zazavi - http://www.zazavi.com
   * Zazavi is free software released under the terms of the GNU General Public License.
   * You should have received a copy of the license.  If not, see <http://www.gnu.org/licenses/>.
   */
  
  if (!defined("_VALID_PHP"))
      die('Direct access to this location is not allowed.');
  
  class Rss
  {
	  private $mTable = "mod_rss_config";


      /**
       * Rss::__construct()
       * 
       * @return
       */
      function __construct()
      {
          $this->getConfig();
      }
	  
	  /**
	   * Rss::getConfig()
	   * 
	   * @return
	   */
	  private function getConfig()
	  {
		  global $db;
		  
		  $sql = "SELECT * FROM " . $this->mTable;
          $row = $db->first($sql);
          
          $this->url = $row['url'];
		  $this->title_trim = $row['title_trim'];
		  $this->show_body = $row['show_body'];
		  $this->body_trim = $row['body_trim'];
		  $this->show_date = $row['show_date'];
		  $this->dateformat = $row['dateformat'];
		  $this->perpage = $row['perpage'];
	  }
	  	  
	  /**
	   * Rss::processConfig()
	   * 
	   * @return
	   */
	  public function processConfig()
	  {
		  global $db, $core, $zazavisec;

		  if (empty($_POST['url']))
			  $core->msgs['url'] = MOD_RS_URL_R;
			  		  
		  if (empty($_POST['dateformat']))
			  $core->msgs['dateformat'] = MOD_RS_DATE_R;

		  if (empty($_POST['perpage']))
			  $core->msgs['perpage'] = MOD_RS_PP_R;
			  		  
		  if (empty($core->msgs)) {
			  $data = array(
					'url' => sanitize($_POST['url']), 
					'title_trim' => intval($_POST['title_trim']),
					'show_body' => intval($_POST['show_body']),
					'body_trim' => intval($_POST['body_trim']),
					'show_date' => intval($_POST['show_date']),
					'dateformat' => sanitize($_POST['dateformat']),
					'perpage' => intval($_POST['perpage'])
			  );
			  
			  $db->update($this->mTable, $data);
			  ($db->affected()) ? $zazavisec->writeLog(MOD_RS_UPDATED, "", "no", "module") . $core->msgOk(MOD_RS_UPDATED) :  $core->msgAlert(_SYSTEM_PROCCESS);
				 
		  } else
			  print $core->msgStatus();
	  }
	  
      /**
       * Rss::getDateFormat()
       * 
       * @return
       */ 
      public function getDateFormat()
	  {
		  $arr = array(
				'm-d-Y' => '12-21-2009 (MM-DD-YYYY)',
				'j-n-Y' => '21-12-2009 (D-M-YYYY)',
				'm-j-y' => '12-21-09 (MM-D-YY)',
				'j-n-y' => '21-12-09 (D-M-YY)',
				'M d Y' => 'Dec 21 2009',
				'F d, Y' => 'December 21, 2009',
				'd F Y H:i' => '21 December 2009 19:00',
				'F N, Y' => 'December 21st, 2009',
				'N F, Y' => '21st December, 2009',
				'l N F, Y' => 'Monday 21st December, 2009',
				'l N F Y h:i' => 'Monday 21st December 2009 07:00',
				'M d, M Y' => 'Mon. 12, December'
		  );
		  
		  $dformat = '';
		  foreach ($arr as $key => $val) {
              if ($key == $this->dateformat) {
                  $dformat .= "<option selected=\"selected\" value=\"" . $key . "\">" . $val . "</option>\n";
              } else
                  $dformat .= "<option value=\"" . $key . "\">" . $val . "</option>\n";
          }
          unset($val);
          return $dformat;
      }
  }
?>