<?php
/**
 * @version $Id$
 * @package    Contact_Enhanced
 * @subpackage Views
 * @author     Douglas Machado {@link http://ideal.fok.com.br}
 * @author     Created on 28-Jul-09
 * @license		GNU/GPL, see license.txt
 * Contact Enhanced  is free software. This version may have been modified pursuant
 * to the GNU General Public License, and as distributed it includes or
 * is derivative of works licensed under the GNU General Public License or
 * other free or open source software licenses.
 */

//--No direct access
defined('_JEXEC') or die('Direct Access NOT Allowed. Please try to access the main page');

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

class Contact_EnhancedsViewcustomvalue extends JView
{
	function display($tpl = null)
	{
		JHTML::stylesheet( 'contact_enhanced.css', 'administrator/components/com_contact_enhanced/assets/css/' );
		
		//Data from model
		$item		=& $this->get('Data');
		$categories	=& $this->get('Categories');
		$types		=& $this->get('Types');
		$isNew		= ($item->id < 1);
		$text		= $isNew ? JText::_( 'New' ) : JText::_( 'Edit' );
		$lists		= array();
		$db 		=& JFactory::getDBO();
		
		JToolBarHelper::save();
		JToolBarHelper::apply();
		JToolBarHelper::cancel();
		JToolBarHelper::title(   '&nbsp;&nbsp;' .JText::_( 'Custom Value Editor' ).': <small><small>[ ' . $text.' ]</small></small>', 'install');
		
		
		$query = "SELECT ordering AS value, text"
					. " FROM #__contact_enhanced_cv"
					. " WHERE category = ". $db->Quote($item->category)
					. " ORDER BY ordering"
				;
	
		if($isNew){
			$lists['ordering'] 		= $this->specificordering($item, '', $query,9999 );
			$item->published		= 1;
		}else{
			$lists['ordering'] 		= $this->specificordering( $item, $item->id, $query );
		}
		
		// build the html radio buttons for published
		$lists['published'] 		= JHTML::_('select.booleanlist',  'published', '', $item->published );

		$this->assignRef('item'			, $item);
		$this->assignRef('categories'	, $categories);
		$this->assignRef('types'		, $types);
		$this->assignRef('lists'		, $lists);
		parent::display($tpl);
	}
	
	/**
	* Build the select list for Ordering of a specified Table
	*/
	function specificordering( &$row, $id, $query, $neworder = 0 )
	{
		$db =& JFactory::getDBO();

		if ( $id ) {
			$order = JHTML::_('list.genericordering',  $query );
			$ordering = JHTML::_('select.genericlist',   $order, 'ordering', 'class="inputbox" size="1"', 'value', 'text', intval( $row->ordering ) );
		} else {
			if ( $neworder ) {
				$text = JText::_( 'descNewItemsFirst' );
			} else {
				$text = JText::_( 'descNewItemsLast' );
			}
			$ordering = '<input type="hidden" name="ordering" value="'. $neworder .'" />'. $text;
		}
		return $ordering;
	}
}
