<?php
/**
 * @version $Id$
 * @package    Contact_Enhanced
 * @subpackage Models
 * @author     Douglas Machado {@link http://ideal.fok.com.br}
 * @author     Created on 04-Dec-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('=;)');

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

class Contact_EnhancedsModelMessage extends JModel
{
	var $id	= null;
	
	function __construct()
	{
		parent::__construct();
		$this->id	= JRequest::getVar( 'cid', array(0) );
	}

	function store($data)
	{	
		$row =& $this->getTable('message');

		if (!$row->bind($data)) {
			return false;
		}

		if (!$row->check()) {
			return false;
		}

		if (!$row->store()) {
			return false;
		}

		return $row->id;
	}	
	
	
	function delete()
	{
		$cids		= JRequest::getVar( 'cid', array(0), 'post', 'array' );
		return $this->deleteMessages($cids);
	}
	
	function deleteMessages($cids)
	{
		$row		=& $this->getTable('Message');
		$mfields	=& $this->getTable('Messagefields');
  
		if (count( $cids )) {
			
			jimport('joomla.filesystem.file');
			
			foreach($cids as $cid) {
				// Get custom fields to delete
				$query	= 'SELECT * FROM #__contact_enhanced_message_fields '
							. ' WHERE message_id ='.$row->_db->Quote($cid)
							;
				$row->_db->setQuery($query);
				$fields		= $row->_db->loadObjectList();
				
				if(is_array($fields)){
					foreach($fields as $field){
						if($field->field_type=='multiplefiles'){
							$attachments	= explode('|',$field->value);
							if(is_array($attachments)){
								foreach($attachments as $attachment){
									JFile::delete(UPLOADED_FILE_PATH.trim($attachment));
								}
							}
						}
						if (!$mfields->delete( $field->id )) {
							$this->setError( $mfields->getErrorMsg() );
							return false;
						}
					}
				}
				
				
				if (!$row->delete( $cid )) {
					$this->setError( $row->getErrorMsg() );
					return false;
				}
				
				// Get custom fields to delete
				$query	= 'SELECT id FROM #__contact_enhanced_messages '
							. ' WHERE parent ='.$row->_db->Quote($cid)
							;
				$row->_db->setQuery($query);
				$fields		= $row->_db->loadResultArray();
				if( count($fields) > 0 ){
					$this->deleteMessages($fields);
				}
			}
		}
		return true;
	}

	function getData()
	{
		$id = JRequest::getVar('cid');
		$row =& $this->getTable('message');
		$row->load($id[0]);
		return $row;
	}
	
	function getRecordedFields()
	{
		$id		= JRequest::getVar('cid');
		$catid	= JRequest::getVar('catid');
		
		$query	= 'SELECT cf.* ' //, UNIX_TIMESTAMP(mf.date) AS unix_timestamp
					.	', (SELECT mf.id 	FROM #__contact_enhanced_message_fields mf  WHERE mf.message_id = '.$this->_db->Quote($id[0]).' AND  cf.id = mf.field_id) AS field_id '
					.	', (SELECT mf.value	FROM #__contact_enhanced_message_fields mf  WHERE mf.message_id = '.$this->_db->Quote($id[0]).' AND  cf.id = mf.field_id) AS field_value '
					.	' FROM #__contact_enhanced_cf cf '
						//.	' AND (cf.catid = '.$this->_db->Quote($catid).' OR cf.catid = '.$this->_db->Quote(0).' ) '
					.	' WHERE cf.type <> '.$this->_db->Quote('multiplefiles')
							.	' AND cf.iscore <> '.	$this->_db->Quote('1')
							.	' AND (cf.catid = '.		$this->_db->Quote($catid) .' OR cf.catid = 0)'
					.	' ORDER BY cf.ordering ASC '
				;
		
		$this->_db->setQuery($query);
		return ($this->_db->loadObjectList()); 
		
	}
	
	function getCustomFields($catid){
		$db			= & JFactory::getDBO();
		$query = "SELECT cf.id, cf.* "
		. "\n FROM #__contact_enhanced_cf AS cf"
		. "\n WHERE (cf.published > 0 OR (cf.published = 0 AND cf.iscore = 1) ) "
			. "\n AND (cf.catid = ". (int) $catid . " OR  cf.catid = 0 )"
		. "\n ORDER BY cf.catid, cf.ordering ASC"
		;
		$db->setQuery($query);
		return $db->loadObjectList();
	}
	
	function getAttachments(){
		$id		= JRequest::getVar('cid');
		$query	= 'SELECT * FROM #__contact_enhanced_message_fields '
					. 	' WHERE message_id ='.$this->_db->Quote($id[0])
						. ' AND field_type = '.$this->_db->Quote('multiplefiles')
						;
		return $this->_getList($query);
	}
	
	function getReplies(){
		$query	= 'SELECT * FROM #__contact_enhanced_messages '
					. 	' WHERE parent ='.$this->_db->Quote($this->id[0])
					.	' ORDER BY date '
					;
		
		//echo '<pre>'.$this->_db->getQuery(); 
		return $this->_getList($query);
	}
}
