<?php

// no direct access
defined('_JEXEC') or die('Restricted access');

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

/**
 * HTML Article Element View class for the Content component
 *
 * @package		Contact Enhanced
 * @since 1.5
 */
class Contact_EnhancedsViewElement extends JView
{
	function display()
	{
		global $mainframe;

		// Initialize variables
		$db			= &JFactory::getDBO();
		$nullDate	= $db->getNullDate();

		$document	= & JFactory::getDocument();
		$document->setTitle(JText::_('Element Selection'));

		JHTML::_('behavior.modal');

		$template = $mainframe->getTemplate();
		$document->addStyleSheet("templates/$template/css/general.css");

	
		$rows	= $this->_getRows(JRequest::getCmd('elemType'));

		JHTML::_('behavior.tooltip');
		?>
		<form 
			action="index.php?option=com_contact_enhanced&amp;task=element&amp;tmpl=component&amp;object=id&amp;elemType=<?php 
				echo JRequest::getCmd('elemType'); 
				?>&amp;elemVar1=<?php echo JRequest::getCmd('elemVar1');
				?>&amp;elemVar2=<?php echo JRequest::getCmd('elemVar2');
				?>&amp;elemVar3=<?php echo JRequest::getCmd('elemVar3');
				?>&amp;elemVar4=<?php echo JRequest::getCmd('elemVar4');
				?>&amp;elemVar5=<?php echo JRequest::getCmd('elemVar5'); 
				?>" method="post" name="adminForm">

			<table class="adminlist" cellspacing="1">
			<thead>
				<tr>
					<th width="5">
						<?php echo JText::_( 'Num' ); ?>
					</th>
					<th class="title">
						<?php echo JText::_( 'Title' ); ?>
					</th>
				</tr>
			</thead>
			
			<tbody>
			<?php
			$k = 0;
			for ($i=0, $n=count( $rows ); $i < $n; $i++)
			{
				$row = &$rows[$i];

				$link 	= '';
				?>
				<tr class="<?php echo "row$k"; ?>">
					<td>
						<?php echo ($i+1); ?>
					</td>
					<td>
						<a style="cursor: pointer;" onclick="window.parent.jSelectItem('<?php echo $row->id; ?>', '<?php echo str_replace(array("'", "\""), array("\\'", ""),$row->title); ?>', '<?php echo JRequest::getVar('object'); ?>');">
							<?php echo htmlspecialchars($row->title, ENT_QUOTES, 'UTF-8'); ?></a>
					</td>
				
				</tr>
				<?php
				$k = 1 - $k;
			}
			?>
			</tbody>
			</table>

		<input type="hidden" name="boxchecked" value="0" />
		<input type="hidden" name="filter_order" value="<?php echo $lists['order']; ?>" />
		<input type="hidden" name="filter_order_Dir" value="<?php echo $lists['order_Dir']; ?>" />
		</form>
		<?php
	}

	function _getRows($type)
	{
		global $mainframe;

		// Initialize variables
		$db		= &JFactory::getDBO();

		$option				= JRequest::getCmd( 'option' );
		$rows	= array();
		
		switch ($type) {
			case 'campaignmonitor':
				require_once JPATH_ROOT.DS.'components'.DS.'com_contact_enhanced'.DS.'helper'.DS.'CMBase.php';
				//JLoader::register('CMBase', JPATH_ROOT.DS.'components'.DS.'com_contact_enhanced'.DS.'helper'.DS.'CMBase.php');
				$apiKey	= JRequest::getCmd('elemVar1');
				$clientID= JRequest::getCmd('elemVar2');
				if(!$clientID OR !$apiKey){
					echo '<h1>'.JText::_('You must enter a valid API Key and a API Client ID').'</h1>'; exit;	
				}
				$cm = new CampaignMonitor( $apiKey );
				//Optional statement to include debugging information in the result
				$cm->debug_level = 1;
				$listTypes	= $cm->clientGetLists($clientID);
				$this->cmGetOption($rows,$listTypes);
				//testArray($rows);
			break;

		}
		
		
		return $rows;
	}
	function cmGetOption(&$rows,$list,$value_name='ListID', $text_name='Name') {
		//testArray($list,false);
		if(!isset($list[$value_name]) AND is_array($list)){
			foreach ($list as $value)
			$this->cmGetOption($rows,$value,$value_name,$text_name);
			//return $this->cmGetOption($rows,$value,$value_name,$text_name);
		}elseif (is_array($list)){
			$row		= new JObject();
			$row->id	= $list[$value_name];
			$row->title	= $list[$text_name];
			$rows[]		= $row;
		}
		
		//return $rows;
	}
}
