<?php
/*
Copyright (C) 2010 Luis Eduardo da Silva Dias. All rights reserved.
Version Beta 1.0
jmodellesd.php 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 3 of the License, or
(at your option) any later version.
jmodellesd.php 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, see <http://www.gnu.org/licenses/>.
*/
defined('_JEXEC') or die();
jimport( 'joomla.application.component.model' );
class JModelLesd extends JModel
{
var $_id = null;
var $_data;
var $_order = array();
var $_total = null;
var $_pagination = null;
var $_search = null;
var $_filter_field = null;
function __construct()
{
parent::__construct();
global $mainframe, $option;
$limit = $mainframe->getUserStateFromRequest('global.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int');
$limitstart = JRequest::getVar('limitstart', 0, '', 'int');
$search = $mainframe->getUserStateFromRequest( $context.'search','search','','string' );
$limitstart = ($limit != 0 ? (floor($limitstart / $limit) * $limit) : 0);
$this->setState('limit', $limit);
$this->setState('limitstart', $limitstart);
$cid = JRequest::getVar('cid', array(0), '', 'array');
$this->setId((int)$cid[0]);
$this->_order[] = JRequest::getVar('filter_order', 'id', 'POST', 'cmd');
$this->_order[] = JRequest::getVar('filter_order_Dir', 'asc', 'POST', 'word');
$this->_search = $search;
}
function setId($id)
{
$this->_id = $id;
$this->_data = null;
}
function &getData()
{
if (empty( $this->_data )) {
$row =& JTable::getInstance($this->getName(), 'Table');
$row->load( $this->_id );
$this->_data = $row;
}
if (!$this->_data) {
$this->_data = & JTable::getInstance($this->getName(), 'Table');
}
return $this->_data;
}
function store()
{
$row =& $this->getTable();
$data = JRequest::get( 'post' );
if (!$row->bind($data)) {
$this->setError($this->_db->getErrorMsg());
return false;
}
if (!$row->check()) {
$this->setError($this->_db->getErrorMsg());
return false;
}
if (!$row->store()) {
$this->setError($this->_db->getErrorMsg());
return false;
}
return true;
}
function delete()
{
$cids = JRequest::getVar( 'cid', array(0), 'post', 'array' );
$row =& $this->getTable();
foreach($cids as $cid) {
if (!$row->delete( $cid )) {
$this->setError( $row->getErrorMsg() );
return false;
}
}
return true;
}
function getOrder() {
return $this->_order;
}
function _buildQuery()
{
$query = ' SELECT * FROM #__' . $this->getName() . ' ' . $this->_buildSearchBy() . $this->_buildContentOrderBy() ;
return $query;
}
function _buildSearchBy()
{
$db =& JFactory::getDBO();
if (!empty($this->_search)) {
$query = ' WHERE LOWER('.$this->_filter_field.') LIKE '.$db->Quote( '%'.$db->getEscaped( $this->_search, true ).'%', false );
} else {
$query = '';
}
return $query;
}
function _buildContentOrderBy()
{
return ' ORDER BY '.$this->_order[0].' '.$this->_order[1].' ';
}
function getGrid()
{
if (empty( $this->_data ))
{
$query = $this->_buildQuery();
$this->_data = $this->_getList( $query, $this->getState('limitstart'), $this->getState('limit') );
}
return $this->_data;
}
function getTotal()
{
if (empty($this->_total)) {
$query = $this->_buildQuery();
$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 lock($uid = null, &$row)
{
$key = 'checked_out';
if ( !in_array( $key, array_keys( $row->getProperties() ) ) ) {
return true;
}
if ($row->isCheckedOut($uid)) {
$this->setError(JText::_( 'Item is locked by another user' ));
return false;
} else {
if ( !$row->checkout($uid,$row->id)) {
$this->setError(JText::_( 'Error locking item' ));
return false;
}
}
return true;
}
function unlock($uid = null)
{
// read id input hidden from form
$this->_id = JRequest::getVar( 'id' );
$row =& $this->getTable();
$key = 'checked_out';
if ( !in_array( $key, array_keys( $row->getProperties() ) ) ) {
return true;
}
$row->load($this->_id);
// who locked before? same user?
if ( $uid == $row->checked_out ) {
if(!$row->checkin($this->_id)) {
$this->setError($this->_db->getErrorMsg());
return false;
}
} else {
return false;
}
return true;
}
}