<?php
/**
 * @version $Id$
 * @package    Contact_Enhanced
 * @subpackage Controllers
 * @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.
 * @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.controller');

class Contact_EnhancedsControllercustomvalues extends JController
{
	var $cid;
	var $publish;

	function __construct()
	{
		parent::__construct();
		// Register Extra tasks
		$this->registerTask( 'add'  , 	'edit' );
		$this->registerTask( 'apply', 	'save' );
		$this->registerTask( 'unpublish', 	'publish');

		$this->cid = JRequest::getVar( 'cid', array(0), '', 'array' );
		JArrayHelper::toInteger($this->cid, array(0));
	}
	function _buildQuery()
	{
		$this->_query = 'UPDATE #__contact_enhanced_cv'
		. ' SET published = ' . (int) $this->publish
		. ' WHERE id IN ( '. $this->cids .' )'		
		;
		return $this->_query;     
	}
	function edit()
	{
		JRequest::setVar( 'view', 'customvalue' );
		JRequest::setVar( 'layout', 'form'  );
		JRequest::setVar('hidemainmenu', 1);
		parent::display();
	}

	function cancel()
	{
		$msg = JText::_( 'Operation Cancelled' );
		$this->setRedirect( 'index.php?option=com_contact_enhanced&controller=customvalues&view=customvalues', $msg );
	}
	function publish()
	{
		$cid		= JRequest::getVar( 'cid', array(), '', 'array' );
		$this->publish	= ( $this->getTask() == 'publish' ? 1 : 0 );

		JArrayHelper::toInteger($cid);
		if (count( $cid ) < 1)
		{
			$action = $this->publish ? 'publish' : 'unpublish';
			JError::raiseError(500, JText::_( 'Select an item to' .$action, true ) );
		}

		$this->cids = implode( ',', $cid );

		$query = $this->_buildQuery();
		$db = &JFactory::getDBO();
		$db->setQuery($query);
		if (!$db->query())
		{
			JError::raiseError(500, $db->getErrorMsg() );
		}
		$link = 'index.php?option=com_contact_enhanced&controller=customvalues&view=customvalues';
		$this->setRedirect($link, $msg);
	}

	function save()
	{
		$post	= JRequest::get('post');
		$cid	= JRequest::getVar( 'cid', array(0), 'post', 'array' );
		#_ECR_SMAT_DESCRIPTION_CONTROLLER1_
		$post['id'] 	= (int) $cid[0];

		$model = $this->getModel( 'customvalue' );
		if (( $id = $model->store($post) )) {
			$msg = JText::_( 'Item Saved' );
		} else {
			$msg = JText::_( 'Error Saving Item' );
		}

		if(JRequest::getVar( 'task') == 'apply'){
			$link = 'index.php?option=com_contact_enhanced&controller=customvalues&task=edit&cid[]='.$id;
		}else{
			$link = 'index.php?option=com_contact_enhanced&controller=customvalues&view=customvalues';
		}
		$this->setRedirect( $link, $msg );
	}
	
	function copy()
	{
		$cid	= JRequest::getVar( 'cid', array(0), 'post', 'array' );

		$model	= $this->getModel( 'customvalue' );
		$row	= $model->getTable('customvalue');
		
		foreach($cid as $id){
			$row->load($id);
			$row->id		= NULL;
			$row->published	= 0;
			$row->text		= $row->text.' - '.JText::_('Copy'); 
			
			if ( $row->store($post) ) {
				$msg = JText::_( 'Item(s) successfully copied' );
			} else {
				$msg = JText::_( 'Error Copying Item' );
			}
		}
		$link = 'index.php?option=com_contact_enhanced&controller=customvalues&view=customvalues';
		$this->setRedirect( $link, $msg );
	}

	function remove()
	{
		$model = $this->getModel('customvalue');
		if(!$model->delete()) {
			$msg = JText::_( 'Error Deleting Item' );
		} else {
			$cids = JRequest::getVar( 'cid', array(0), 'post', 'array' );
			foreach($cids as $cid) {
				$msg .= JText::_( 'Item Deleted '.' : '.$cid.' - ' );
			}			
		}

		$this->setRedirect( 'index.php?option=com_contact_enhanced&controller=customvalues&view=customvalues', $msg );
	}

	function _reOrder($direction)
	{
		global $mainframe;
		
		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );

		// Initialize variables
		$db	= & JFactory::getDBO();
		$cid	= JRequest::getVar( 'cid', array(), 'post', 'array' );

		if (isset( $cid[0] ))
		{
			$row = & JTable::getInstance('customvalue', 'Table');
			$row->load( (int) $cid[0] );
			$row->move($direction);

			$cache = & JFactory::getCache('com_contact_enhanced');
			$cache->clean();
		}

		$mainframe->redirect('index.php?option=com_contact_enhanced&controller=customvalues&view=customvalues');
	}//function

	function saveorder()
	{
		global $mainframe;

		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );

		// Initialize variables
		$db =& JFactory::getDBO();
		$cid	= JRequest::getVar( 'cid', array(), 'post', 'array' );

		$total		= count( $cid );
		$order 	= JRequest::getVar( 'order', array(0), 'post', 'array' );
		JArrayHelper::toInteger($order, array(0));

		$row		=& JTable::getInstance('customvalue', 'Table');
		$groupings	= array();
		
		// update ordering values
		for( $i=0; $i < $total; $i++ )
		{
			$row->load( (int) $cid[$i] );
			$groupings[] = $row->category;
			// track sections
			if ($row->ordering != $order[$i])
			{
				$row->ordering = $order[$i];
				if (!$row->store())
				{
					JError::raiseError(500, $db->getErrorMsg());
				}
			}
		}//for
	
		// execute updateOrder for each parent group
		$groupings = array_unique( $groupings );
		foreach ($groupings as $group){
			$row->reorder('category = '. $db->Quote($group) );
		}
	
		$msg 	= JText::_( 'New ordering saved' );
		$mainframe->redirect('index.php?option=com_contact_enhanced&controller=customvalues&view=customvalues', $msg);
	}//function

	function orderup()
	{	
		if ($this->order == 'desc')
			$this->_reOrder(1);
		else
			$this->_reOrder(-1);			
	}//function

	function orderdown()
	{
		if ($this->order == 'desc')
			$this->_reOrder(-1);
		else
			$this->_reOrder(1);
	}//function
}
