<?php
/**
 * Pro Super Slider
 *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 3 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, see <http://www.gnu.org/licenses/>.
 *
 *@author ProJoom
 *@copyright (C) 2008 - 2012 ProJoom
 *@link http://www.projoom.com/extensions/pro-super-slider.html Official website
 **/
defined('_JEXEC') or die;
jimport('joomla.application.component.modeladmin');
class NovassModelSlide extends JModelAdmin
{
	protected $text_prefix = 'slide';
	protected function canDelete($record)
	{
		$user = JFactory::getUser();

		if (!empty($record->catid)) {
			return $user->authorise('core.delete', 'com_novass.category.'.(int) $record->catid);
		}
		else {
			return parent::canDelete($record);
		}
	}
	protected function canEditState($record)
	{
		$user = JFactory::getUser();
		if (!empty($record->catid)) {
			return $user->authorise('core.edit.state', 'com_novass.category.'.(int) $record->catid);
		}
		else {
			return parent::canEditState($record);
		}
	}
	public function getTable($type = 'Slide', $prefix = 'NovassTable', $config = array())
	{
		return JTable::getInstance($type, $prefix, $config);
	}
	public function getForm($data = array(), $loadData = true)
	{
		$form = $this->loadForm('com_novass.slide', 'slide', array('control' => 'jform', 'load_data' => $loadData));
		if (empty($form)) {
			return false;
		}
		if ($this->getState('slide.id')) {
			$form->setFieldAttribute('catid', 'action', 'core.edit');
		} else {
			$form->setFieldAttribute('catid', 'action', 'core.create');
		}

		if (!$this->canEditState((object) $data)) {
			$form->setFieldAttribute('ordering', 'disabled', 'true');
			$form->setFieldAttribute('publish_up', 'disabled', 'true');
			$form->setFieldAttribute('publish_down', 'disabled', 'true');
			$form->setFieldAttribute('state', 'disabled', 'true');
			$form->setFieldAttribute('ordering', 'filter', 'unset');
			$form->setFieldAttribute('publish_up', 'filter', 'unset');
			$form->setFieldAttribute('publish_down', 'filter', 'unset');
			$form->setFieldAttribute('state', 'filter', 'unset');
		}

		return $form;
	}
	protected function loadFormData()
	{
		$data = JFactory::getApplication()->getUserState('com_novass.edit.slide.data', array());

		if (empty($data)) {
			$data = $this->getItem();
			if ($this->getState('slide.id') == 0) {
				$app = JFactory::getApplication();
				$data->set('catid', JRequest::getInt('catid', $app->getUserState('com_novass.slide.filter.category_id')));
			}
		}

		return $data;
	}
	protected function getReorderConditions($table)
	{
		$condition = array();
		$condition[] = 'catid = '. (int) $table->catid;
		$condition[] = 'state >= 0';
		return $condition;
	}
}
