<?php
/**
 * @version		1.2 feedback $
 * @package		feedback
 * @copyright	Copyright © 2010 Mertonium. All rights reserved.
 * @license		GNU/GPL
 * @author		Mertonium
 * @author mail	john@mertonium.com
 * @website		http://mertonium.com
 *
 *
 * @MVC architecture generated by MVC generator tool at http://www.alphaplug.com
 */

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

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

class feedbackModelFeedback extends Jmodel
{
	function __construct(){
		parent::__construct();
		
		global $mainframe, $option;
		
		//pagination
		$limit = $mainframe->getUserStateFromRequest('global.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int');
		$limitstart = JRequest::getVar('limitstart', 0, '', 'int');
		$limitstart = ($limit != 0 ? (floor($limitstart / $limit) * $limit) : 0);
		
		$this->setState('limit', $limit);
		$this->setState('limitstart', $limitstart);
	}
	
	function _buildFeedbackQuery() {
		return 'SELECT * '
			  .'FROM #__feedback '
			  .'ORDER BY '. $this->_order_by.' '.$this->_dir
			  ;
	}
	
	function getTotal()	{
		//load if doesn't already exist
		if (empty($this->_total)) {
			$query = $this->_buildFeedbackQuery();
			$this->_total = $this->_getListCount($query);
		}
		
		return $this->_total;
	}
	
	function getPagination() {
		if (empty($this->_pagination)) {
			jimport('joomla.html.pagination');
			$this->_pagination = new JPagination($this->getTotal(), $this->getState('limitstart'), $this->getState('limit'));
		}
		
		return $this->_pagination;
	}
	
	function getFeedback($order_by = 'submitted_ts', $dir = 'ASC') {
		$this->_order_by = $order_by;
		$this->_dir = $dir;
		
		if (empty($this->_data)) {
			$query = $this->_buildFeedbackQuery();
			$this->_data = $this->_getList($query, $this->getState('limitstart'), $this->getState('limit'));
		}
		
		return $this->_data;
	}
	
	function getFeedbackentry($sid) {
		$sql = 'SELECT * '
			  .'FROM #__feedback  '
			  .'WHERE `id` = ' . intval($sid) . ' '
			  ;
		$this->_db->setQuery($sql);
		return $this->_db->loadObject();
	}

	function store($table='feedback', $newdata=null) {
		$row =& $this->getTable($table); 
		$data = (is_null($newdata)) ? JRequest::get('post') : $newdata;
	 
		// Bind the form fields to the table
		if (!$row->bind($data)) {
			$this->setError($this->_db->getErrorMsg());
			return false;
		}
	 
		// Make sure the record is valid
		if (!$row->check()) {
			$this->setError($this->_db->getErrorMsg());
			return false;
		}
	 
		// Store the data in the database
		if (!$row->store()) {
			$this->setError($this->_db->getErrorMsg());
			return false;
		}
	 
		return true;
	}
	 
	function delete($table='feedback') {
		$cids = JRequest::getVar( 'cid', array(0), 'post', 'array' );
		$row =& $this->getTable($table);
	 
		foreach($cids as $cid) {
			if (!$row->delete( $cid )) {
				$this->setError("error");				
				return false;
			}
		}	 
		return true;
	}
}
?>