<?php
/**
 * NoNumber! Extension Manager View
 *
 * @package     NoNumber! Extension Manager
 * @version     1.4.0
 *
 * @author      Peter van Westen <peter@nonumber.nl>
 * @link        http://www.nonumber.nl
 * @copyright   Copyright (C) 2010 NoNumber! All Rights Reserved
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
 */

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

// Import VIEW object class
jimport( 'joomla.application.component.view' );

/**
 * NoNumber! Extension Manager View
 */
class NoNumberManagerViewDefault extends JView
{
	var $request_url = null;
	var $data = null;
	
	/**
	 * Display the view
	 */
	function display( $tpl = null )
	{
		$mainframe =& JFactory::getApplication();

		$uri =& JFactory::getURI();
		$this->request_url =$uri->toString();

		$model =& $this->getModel();
		$model->getData();

		// set document title
		$document =& JFactory::getDocument();
		$document->addStyleSheet( JURI::root( true ).'/administrator/components/com_nonumbermanager/css/nonumbermanager.css' );
		$document->setTitle( JText::_( 'NoNumber! Extension Manager' ) );

		JToolBarHelper::title( JText::_( 'Extension Management' ), 'nonumbermanager' );

		parent::display( $tpl );
	}

	/**
	 * Get the data (should be in model really)
	 */
	function getData()
	{
		$db =& JFactory::getDBO();
		$sql = "SELECT * FROM #__nonumber_licenses";
		$db->setQuery( $sql );
		return $db->loadObjectList( 'extension' );
	}

	/**
	 * Get the html for a table row (extension name + code)
	 */
	function outputCodeRow( $extension, $host, $i = 0 )
	{
		$id = $extension->id;
?>
		<tr id="row_<?php echo $id; ?>">
			<?php if ( $id == 'all' ) { ?>
				<td>
					<input type="checkbox" name="toggle" id="check_toggle" value="" onclick="checkAll(<?php echo $i; ?>);" />
				</td>
				<td>
					<span class="hasTip" title="<?php echo JText::_( 'Refresh' ); ?>"><a href="javascript://" onclick="nnManager.load_extensions()"><img src="<?php echo JURI::base(); ?>components/com_nonumbermanager/images/refresh.png" alt="<?php echo JText::_( 'Refresh' ); ?>" /></a></span>
				</td>
				<td>
					<?php echo $extension->name; ?>
				</td>
				<td colspan="6">
					<div id="link_installselected" style="display:none;;" class="hasTip" title="<?php echo JText::_( 'INSTALL SELECTED' ).'::'.JText::_( 'This will install/update all selected extensions' ); ?>">
						<div id="link_installall_ready" class="button2-left"><div class="blank nonumbermanager_button nonumbermanager_installall"><a id="link_installall_a" href="javascript://" onclick="nnManager.install_selected();"><?php echo JText::_( 'INSTALL SELECTED' ); ?></a></div></div>
					</div>
					<div id="link_installall" style="display:none;;" class="hasTip" title="<?php echo JText::_( 'INSTALL ALL' ).'::'.JText::_( 'This will install all extensions that are not already installed' ); ?>">
						<div id="link_installall_loading" class="button2-left"><div class="blank nonumbermanager_button nonumbermanager_installall"><a href="javascript://"><?php echo JText::_( 'INSTALL ALL' ); ?></a></div></div>
						<div id="link_installall_ready" class="button2-left" style="display:none;"><div class="blank nonumbermanager_button nonumbermanager_installall"><a id="link_installall_a" href="javascript://" onclick="nnManager.install_extensions( 'install' );"><?php echo JText::_( 'INSTALL ALL' ); ?></a></div></div>
					</div>
					<div id="link_updateall" style="display:none;" class="hasTip" title="<?php echo JText::_( 'UPDATE ALL' ).'::'.JText::_( 'This will update all extensions that are not up-to-date' ); ?>">
						<div id="link_updateall_loading" class="button2-left"><div class="blank nonumbermanager_button nonumbermanager_updateall"><a href="javascript://"><?php echo JText::_( 'UPDATE ALL' ); ?></a></div></div>
						<div id="link_updateall_ready" class="button2-left" style="display:none;"><div class="blank nonumbermanager_button nonumbermanager_updateall"><a id="link_updateall_a" href="javascript://" onclick="nnManager.install_extensions( 'update' );"><?php echo JText::_( 'UPDATE ALL' ); ?></a></div></div>
					</div>
				</td>
			<?php } else { ?>
				<td>
					<div class="loading loading_<?php echo $id; ?>" id="check_<?php echo $id; ?>_loading"><img src="<?php echo JURI::base(); ?>components/com_nonumbermanager/images/loading.gif" alt="" /></div>
					<div class="result result_<?php echo $id; ?>" id="check_<?php echo $id; ?>_check" style="display:none;">
						<input class="check_checkbox" type="checkbox" id="cb<?php echo $i; ?>" name="cid[]" value="" onclick="isChecked(this.checked);" />
					</div>
				</td>
				<td>
					<span class="hasTip" title="<?php echo JText::_( 'Refresh' ); ?>"><a href="javascript://" onclick="nnManager.load_extension( '<?php echo $id; ?>', '<?php echo $extension->alias; ?>' )"><img src="<?php echo JURI::base(); ?>components/com_nonumbermanager/images/refresh.png" alt="<?php echo JText::_( 'Refresh' ); ?>" /></a></span>
				</td>
				<td><a href="http://www.nonumber.nl/<?php echo $id; ?>" target="_blank" style="text-decoration: none;"><img src="<?php echo JURI::base(); ?>components/com_nonumbermanager/images/icons/<?php echo $id; ?>.png" alt="" /> <?php echo $extension->name; ?> <img src="<?php echo JURI::base(); ?>components/com_nonumbermanager/images/link.png" alt="" /></a></td>
				<td>
					<div class="loading loading_<?php echo $id; ?>" id="info_<?php echo $id; ?>_loading"><img src="<?php echo JURI::base(); ?>components/com_nonumbermanager/images/loading.gif" alt="" /></div>
					<div class="result result_<?php echo $id; ?>" id="info_<?php echo $id; ?>_icon" style="display:none;"><span><a href="http://www.nonumber.nl/<?php echo $id; ?>" target="_blank"><img src="<?php echo JURI::base(); ?>components/com_nonumbermanager/images/info.png" alt="" /></a></span></div>
					<div class="result result_<?php echo $id; ?>" id="info_<?php echo $id; ?>_failed" style="display:none;"><span class="hasTip" title="<?php echo JText::_( 'Retry' ); ?>"><a href="javascript://" onclick="nnManager.load_extension( '<?php echo $id; ?>', '<?php echo $extension->alias; ?>' )"><img src="<?php echo JURI::base(); ?>components/com_nonumbermanager/images/refresh.png" alt="<?php echo JText::_( 'Retry' ); ?>" /></a></span></div>
				</td>
				<td style="text-align:center">
					<div class="loading loading_<?php echo $id; ?>" id="installed_<?php echo $id; ?>_loading"><img src="<?php echo JURI::base(); ?>components/com_nonumbermanager/images/loading.gif" alt="" /></div>
					<div class="result result_<?php echo $id; ?>" id="installed_<?php echo $id; ?>_installed" style="display:none;"><span class="hasTip" title="<?php echo JText::_( 'Installed' ); ?>"><img src="<?php echo JURI::base(); ?>components/com_nonumbermanager/images/yes.png" alt="<?php echo JText::_( 'YES' ); ?>" /></span></div>
					<div class="result result_<?php echo $id; ?>" id="installed_<?php echo $id; ?>_notinstalled" style="display:none;"><span class="hasTip" title="<?php echo JText::_( 'Not installed' ); ?>"><img src="<?php echo JURI::base(); ?>components/com_nonumbermanager/images/no.png" alt="<?php echo JText::_( 'NO' ); ?>" /></span></div>
					<div class="result result_<?php echo $id; ?>" id="installed_<?php echo $id; ?>_failed" style="display:none;"><span class="hasTip" title="<?php echo JText::_( 'Retry' ); ?>"><a href="javascript://" onclick="nnManager.load_extension( '<?php echo $id; ?>', '<?php echo $extension->alias; ?>' )"><img src="<?php echo JURI::base(); ?>components/com_nonumbermanager/images/refresh.png" alt="<?php echo JText::_( 'Retry' ); ?>" /></a></span></div>
				</td>
				<td style="text-align:right">
					<div class="loading loading_<?php echo $id; ?>" id="version_<?php echo $id; ?>_loading"><img src="<?php echo JURI::base(); ?>components/com_nonumbermanager/images/loading.gif" alt="" /></div>
					<div class="result result_<?php echo $id; ?>" id="version_<?php echo $id; ?>_version" style="display:none;"></div>
					<div class="result result_<?php echo $id; ?>" id="version_<?php echo $id; ?>_failed" style="display:none;"><span class="hasTip" title="<?php echo JText::_( 'Retry' ); ?>"><a href="javascript://" onclick="nnManager.load_extension( '<?php echo $id; ?>', '<?php echo $extension->alias; ?>' )"><img src="<?php echo JURI::base(); ?>components/com_nonumbermanager/images/refresh.png" alt="<?php echo JText::_( 'Retry' ); ?>" /></a></span></div>
				</td>
				<td>
					<div class="loading loading_<?php echo $id; ?>" id="version_button_<?php echo $id; ?>_loading"><img src="<?php echo JURI::base(); ?>components/com_nonumbermanager/images/loading.gif" alt="" /></div>
					<div class="result result_<?php echo $id; ?>" id="version_button_<?php echo $id; ?>_update" style="display:none;"><div class="button2-left"><div class="blank nonumbermanager_button nonumbermanager_update"><a href="javascript://"><?php echo JText::_( 'Update to new version' ); ?> <strong class="new_version_number"></strong></a></div></div></div>
					<div class="result result_<?php echo $id; ?>" id="version_button_<?php echo $id; ?>_install" style="display:none;"><div class="button2-left"><div class="blank nonumbermanager_button nonumbermanager_install"><a href="javascript://"><?php echo JText::_( 'Install new version' ); ?> <strong class="new_version_number"></strong></a></div></div></div>
					<div class="result result_<?php echo $id; ?>" id="version_button_<?php echo $id; ?>_uptodate" style="display:none;text-align:center;"><a href="javascript://"><?php echo JText::_( 'Reinstall new version' ); ?> <strong class="new_version_number"></strong></a></div>
					<div class="result result_<?php echo $id; ?>" id="version_button_<?php echo $id; ?>_downgrade" style="display:none;text-align:center;"><a href="javascript://"><?php echo JText::_( 'Downgrade to stable version' ); ?> <strong class="new_version_number"></strong></a></div>
					<div class="result result_<?php echo $id; ?>" id="version_button_<?php echo $id; ?>_failed" style="display:none;text-align:center;"></div>
				</td>
				<td>
					<div class="loading loading_<?php echo $id; ?>" id="changelog_<?php echo $id; ?>_loading"><img src="<?php echo JURI::base(); ?>components/com_nonumbermanager/images/loading.gif" alt="" /></div>
					<div class="result result_<?php echo $id; ?>" id="changelog_<?php echo $id; ?>_icon" style="display:none;"><span><a href="http://www.nonumber.nl/<?php echo $id; ?>/changelog" target="_blank"><img src="<?php echo JURI::base(); ?>components/com_nonumbermanager/images/changelog.png" alt="" /></a></span></div>
					<div class="result result_<?php echo $id; ?>" id="changelog_<?php echo $id; ?>_failed" style="display:none;"><span class="hasTip" title="<?php echo JText::_( 'Retry' ); ?>"><a href="javascript://" onclick="nnManager.load_extension( '<?php echo $id; ?>', '<?php echo $extension->alias; ?>' )"><img src="<?php echo JURI::base(); ?>components/com_nonumbermanager/images/refresh.png" alt="<?php echo JText::_( 'Retry' ); ?>" /></a></span></div>
				</td>
				<td>
					<div class="loading loading_<?php echo $id; ?>" id="version_state_<?php echo $id; ?>_loading"><img src="<?php echo JURI::base(); ?>components/com_nonumbermanager/images/loading.gif" alt="" /></div>
					<div class="result result_<?php echo $id; ?>" id="version_state_<?php echo $id; ?>_update" style="color:#990000;display:none;"><img src="<?php echo JURI::base(); ?>components/com_nonumbermanager/images/note.png" alt="" /> <?php echo JText::_( 'New version available' ); ?></div>
					<div class="result result_<?php echo $id; ?>" id="version_state_<?php echo $id; ?>_install" style="display:none;"><?php echo JText::_( 'Not Installed' ); ?></div>
					<div class="result result_<?php echo $id; ?>" id="version_state_<?php echo $id; ?>_uptodate" style="color:#006600;display:none;"><img src="<?php echo JURI::base(); ?>components/com_nonumbermanager/images/yes.png" alt="" /> <?php echo JText::_( 'Version is up-to-date' ); ?></div>
					<div class="result result_<?php echo $id; ?>" id="version_state_<?php echo $id; ?>_downgrade" style="color:#006600;display:none;"><img src="<?php echo JURI::base(); ?>components/com_nonumbermanager/images/yes.png" alt="" /> <?php echo JText::_( 'Version is up-to-date' ); ?></div>
					<div class="result result_<?php echo $id; ?>" id="version_state_<?php echo $id; ?>_failed" style="color:#999999;display:none;">
						<img src="<?php echo JURI::base(); ?>components/com_nonumbermanager/images/no.png" alt="" /> <?php echo JText::_( 'Could not check version' ); ?>
						<span class="hasTip" title="<?php echo JText::_( 'Retry' ); ?>"><a href="javascript://" onclick="nnManager.load_extension( '<?php echo $id; ?>', '<?php echo $extension->alias; ?>' )"><img src="<?php echo JURI::base(); ?>components/com_nonumbermanager/images/refresh.png" alt="<?php echo JText::_( 'Retry' ); ?>" /></a></span>
					</div>
				</td>
			<?php } ?>
			<td id="license_field_<?php echo $id; ?>">
				<?php if ( $id != 'nonumbermanager' ) { ?>
					<div class="license_loading license_loading_<?php echo $id; ?>" id="license_field_<?php echo $id; ?>_loading"><img src="<?php echo JURI::base(); ?>components/com_nonumbermanager/images/loading.gif" alt="" /></div>
					<div class="license_result license_result_<?php echo $id; ?>" id="license_field_<?php echo $id; ?>_field" style="display:none;white-space:nowrap;">
						<input class="license_code" id="license_code_<?php echo $id; ?>" type="text" name="codes[<?php echo $id; ?>]" value="<?php echo $extension->code; ?>" onchange="this.style.color = '#666666';" style="width:100px;font-family:monospace;font-size:1.2em;color:#666666;" />
						<span class="hasTip" title="<?php echo JText::_( 'Save code' ); ?>"><a href="javascript://" onclick="nnManager.save_license( '<?php echo $id; ?>' )"><img src="<?php echo JURI::base(); ?>components/com_nonumbermanager/images/save.png" alt="<?php echo JText::_( 'Save' ); ?>" /></a></span>
					</div>
					<div class="license_result license_result_<?php echo $id; ?>" id="license_field_<?php echo $id; ?>_allvalid" style="display:none;white-space:nowrap;"><input class="license_code" id="license_code_<?php echo $id; ?>_allvalid" type="text" readonly="readonly" value="" style="width:100px;font-family:monospace;font-size:1.2em;background-color:#CCEECC;color:#99AA99;font-style:italic;" onfocus="this.blur()" /></div>
				<?php } ?>
			</td>
			<td id="license_state_<?php echo $id; ?>">
				<?php
					if ( $id != 'nonumbermanager' ) {
						// loading
						echo $this->getIcon( $extension, 'loading', 'loading.gif' );
						// local
						echo $this->getIcon( $extension, 'local', 'note.png', JText::_( 'The code cannot be checked' ) );
						if ( $host != 'localhost' ) {
							// all valid
							echo $this->getIcon( $extension, 'allvalid', 'yes.png', JText::_( 'The code is valid for all extensions' ) );
							// valid
							echo $this->getIcon( $extension, 'valid', 'yes.png', JText::sprintf( 'The code is valid', $host ) );
							// not valid
							echo $this->getIcon( $extension, 'invalid', 'no.png', JText::sprintf( 'The code is not valid', $host ) );
							// fail
							echo $this->getIcon( $extension, 'failed', 'note.png', JText::_( 'Retry' ) );
						}
					}
				?>
			</td>
		</tr>
<?php
	}

	function getIcon( $extension, $state = '', $icon = 'note.png', $text = '' ) {
		$extra = '';
		if ( $state == 'failed' ) {
			$extra = 'onmouseover="this.src=\''.JURI::base().'components/com_nonumbermanager/images/refresh.png\';" onmouseout="this.src=\''.JURI::base().'components/com_nonumbermanager/images/'.$icon.'\';"';
		}
		$html = '<img src="'.JURI::base().'components/com_nonumbermanager/images/'.$icon.'" alt="" '.$extra.' />';
		if ( $state == 'failed' ) {
			$html = '<a href="javascript://" onclick="nnManager.load_license( \''.$extension->id.'\' );"></a>';
		}
		if ( $text ) {
			$html = '<span class="hasTip" title="'.$text.'">'.$html .'</span>';
		}
		if ( $state == 'loading' ) {
			$html = '<div class="license_loading license_loading_'.$extension->id.'" id="license_state_'.$extension->id.'_'.$state.'">'.$html.'</div>';
		} else {
			$html = '<div class="license_result license_result_'.$extension->id.'" id="license_state_'.$extension->id.'_'.$state.'" style="display:none;">'.$html.'</div>';
		}
		return $html;
	}
}