<?php
/**
 * @package BILLETS
 * @link 	http://www.dioscouri.com
 * @license GNU/GPLv2
 */

// no direct access
defined('_JEXEC') or die('Restricted access');

jimport( 'joomla.application.component.model' );

/**
 * Billets Model
 *
 * @package BILLETS
 */
class BilletsModelAddon extends JModel {

	var $_data;

	/**
	 * constructor
	 * @return array Array of objects containing the data from the database
	 */	
	function __construct() {
		parent::__construct();

		$array = JRequest::getVar('cid',  0, '', 'array');
		$this->setId((int)$array[0]);
	}

	/**
	 * Method to set the identifier
	 *
	 * @access	public
	 * @param	int identifier
	 * @return	void
	 */
	function setId($id) {
		// Set id and wipe data
		$this->_id		= $id;
		$this->_data	= null;
	}
	
	/**
	 * Returns the query
	 * @return string The query to be used to retrieve the rows from the database
	 */
	function _buildQuery() {
		// grab record
		$query = "SELECT r.* FROM #__billets_addons as r "
		. " WHERE `id` = '".$this->_id."' "
		;
		
		return $query;
	}
	
	/**
	 * Retrieves the data
	 * @return object containing the record/data from the database
	 */
	function getData() {
		global $mainframe;
		// load the data if it doesn't already exist
		if (empty( $this->_data )) {
			$row = new billetsAddonEntry ( $this->_db );
			$row->load( $this->_id );
			$this->_data = $row;
			
			$type = $row->type;
			$filename = $row->filename;
			
			// change folder
			switch ($type) {
				case "mod":
					$baseDir = JPATH_COMPONENT_ADMINISTRATOR.DS.'modules';
					$dirname = "modules";
				  break;
				case "lang":
					$baseDir = JPATH_COMPONENT_SITE.DS.'languages';
					$dirname = "languages";
				  break;
				default:
					$link = 'index.php?option='._THISCOMPONENT.'&controller=addons&type='.$type;
					$msg = JText::_( _BILLETS_NOFILETYPESELECTED );
					$mainframe->redirect( $link, $msg, 'notice' );					
					return false;
				  break;
			} // end switch type
		
			// if file not writable, fail and give notice
			if ( !is_writable( $baseDir . DS . $filename . ".php" ) ) {
				$link = 'index.php?option='._THISCOMPONENT.'&controller=addons&type='.$type;
				$msg = JText::_( _BILLETS_NOTWRITEABLEFILE );
				$mainframe->redirect( $link, $msg, 'notice' );					
				return false;
			}
		
			// if file exists, grab contents and send for editing
			if (file_exists( $baseDir . DS . $filename . ".php" )) { 
				// get the contents of the file 
				$contents = file_get_contents( $baseDir . DS . $filename . ".php" );
			} else {
				$link = 'index.php?option='._THISCOMPONENT.'&controller=addons&type='.$type;
				$msg = JText::_( _BILLETS_NOFILE );
				$mainframe->redirect( $link, $msg, 'notice' );					
				return false;
			}
			
			$this->_content = $contents;
			$this->_filename = $filename . ".php";
				
		}
		return $this->_data;
	}

	/**
	 * Retrieves the filename
	 * @return object containing the record/data from the database
	 */
	function getFilename() {
		// load the data if it doesn't already exist
		if (empty( $this->_data )) {
			$row = new billetsAddonEntry ( $this->_db );
			$row->load( $this->_id );
			$this->_data = $row;					
		} else {		
			$row = $this->_data;
		}

		return $row->filename . ".php";
	}

	/**
	 * Retrieves the content
	 * @return object containing the record/data from the database
	 */
	function getContent() {
		// load the data if it doesn't already exist
		if (empty( $this->_data )) {
			$row = new billetsAddonEntry ( $this->_db );
			$row->load( $this->_id );
			$this->_data = $row;					
		} else {		
			$row = $this->_data;
		}			

			$type = $row->type;
			$filename = $row->filename;

			// change folder
			switch ($type) {
				case "mod":
					$baseDir = JPATH_COMPONENT_ADMINISTRATOR.DS.'modules';
					$dirname = "modules";
				  break;
				case "lang":
					$baseDir = JPATH_COMPONENT_SITE.DS.'languages';
					$dirname = "languages";
				  break;
				default:
					$link = 'index.php?option='._THISCOMPONENT.'&controller=addons&type='.$type;
					$msg = JText::_( _BILLETS_NOFILETYPESELECTED );
					$mainframe->redirect( $link, $msg, 'notice' );					
					return false;
				  break;
			} // end switch type

		
			// if file not writable, fail and give notice
			if ( !is_writable( $baseDir . DS . $filename . ".php" ) ) {
				$link = 'index.php?option='._THISCOMPONENT.'&controller=addons&type='.$type;
				$msg = JText::_( _BILLETS_NOTWRITEABLEFILE );
				$mainframe->redirect( $link, $msg, 'notice' );					
				return false;
			}
		
			// if file exists, grab contents and send for editing
			if (file_exists( $baseDir . DS . $filename . ".php" )) { 
				// get the contents of the file 
				$contents = file_get_contents( $baseDir . DS . $filename . ".php" );
			} else {
				$link = 'index.php?option='._THISCOMPONENT.'&controller=addons&type='.$type;
				$msg = JText::_( _BILLETS_NOFILE );
				$mainframe->redirect( $link, $msg, 'notice' );					
				return false;
			}
				
		return $contents;
	}

	/**
	 * Method to store a record
	 *
	 * @access	public
	 * @return	boolean	True on success
	 */
	function store() {
		global $mainframe;
		$database = &JFactory::getDBO();

		$filecontent = JArrayHelper::getValue( $_POST, 'filecontent', '', '' );
		$id = intval( JArrayHelper::getValue( $_POST, 'id', '', '' ) );
		// $filecontent = JRequest::getVar( 'filecontent', '', 'POST', 'STRING', '4' ); // allow HTML

		if (!$filecontent) {			
			$link = 'index.php?option='._THISCOMPONENT.'&controller=addons&type='.$this->_type;
			$msg = _BILLETS_ERROR."+"._BILLETS_CONTENTEMPTY;
			$mainframe->redirect( $link, $msg, 'notice' );
			return false;
		}

		$row = new billetsAddonEntry( $database );
		// load the row from the db table
		$row->load( (int)$id);

		$type = $row->type;
		$filename = $row->filename;
		
		// change folder
		switch ($type) {
				case "mod":
					$baseDir = JPATH_COMPONENT_ADMINISTRATOR.DS.'modules';
					$dirname = "modules";
				  break;
				case "lang":
					$baseDir = JPATH_COMPONENT_SITE.DS.'languages';
					$dirname = "languages";
				  break;
				default:
					$link = 'index.php?option='._THISCOMPONENT.'&controller=addons&type='.$this->_type;
					$msg = _BILLETS_NOFILETYPESELECTED;
					$mainframe->redirect( $link, $msg, 'notice' );
					return false;
				  break;
		} // end switch type

		// if file not writable, fail and give notice
		if ( !is_writable( $baseDir . DS . $filename . ".php" ) ) {
			$link = 'index.php?option='._THISCOMPONENT.'&controller=addons&type='.$this->_type;
			$msg = _BILLETS_NOTWRITEABLEFILE;
			$mainframe->redirect( $link, $msg, 'notice' );
			return false;
		}
					
		// if file not exists, fail and notify
		if (!file_exists( $baseDir . DS . $filename . ".php" )) { 
			$link = 'index.php?option='._THISCOMPONENT.'&controller=addons&type='.$this->_type;
			$msg = _BILLETS_NOFILE;
			$mainframe->redirect( $link, $msg, 'notice' );
			return false;
		}
	
		// putthe contents of the file 
		if ( file_put_contents( $baseDir . DS . $filename . ".php", stripslashes( $filecontent ) ) ) {
			$link = 'index.php?option='._THISCOMPONENT.'&controller=addons&type='.$this->_type;
			$msg = _BILLETS_ADDONUPDATED;
			$mainframe->redirect( $link, $msg, 'message' );
			return true;
		} else {
			$link = 'index.php?option='._THISCOMPONENT.'&controller=addons&type='.$this->_type;
			$msg = _BILLETS_ADDONUPDATEFAILED;
			$mainframe->redirect( $link, $msg, 'notice' );
			return false;
		}


		//		$model = $this->getModel('hello');
		//
		//		if ($model->store($post)) {
		//			$msg = JText::_( 'Greeting Saved!' );
		//		} else {
		//			$msg = JText::_( 'Error Saving Greeting' );
		//		}
		//
		//		// Check the table in so it can be edited.... we are done with it anyway
		//		$link = 'index.php?option=com_hello';
		// 		$this->setRedirect($link, $msg);
		return true;
	}
	
	/**
	 * Method to publish/unpublish a record
	 *
	 */
	function publish() {
		$cids = JRequest::getVar( 'cid', array(0), 'post', 'array' );
	    // $row =& $this->getTable();

		// grab info
		$addon = new billetsAddonEntry ($this->_db);
		$addon->load( (int)$cids[0] );
	
		if (!$addon->id) {
			$this->setError( _BILLETS_ADDONUPDATEFAILED );
			return false;
		}
	
		// change published
		if ($addon->published == 1) { $addon->published = 0; } else { $addon->published = 1; }
		

		if (!$addon->store()) {
			$this->setError( $addon->getError() );
			return false;
		}
	
	    return true;
	}
	
		
}
// ************************************************************************
