* @author        Andreas Brauneis
 * @version
 *   Version 2.50.1, 01.11.2012<br/>
 *
 * Laden von Neuen Modulen der IPSLibrary
 *
 */
/** @}*/
?>

<table border=1>
  <tr><th>Modul</th><th>Version</th><th>Laden</th><th>Beschreibung</th></tr>
<?php 
IPSUtils_Include("IPSModuleManager.class.php", "IPSLibrary::install::IPSModuleManager");
$moduleManager = new IPSModuleManager();
$knownModules = $moduleManager->VersionHandler()->GetKnownModules();
$installedModules = $moduleManager->VersionHandler()->GetInstalledModules();
$html = '';
foreach ($knownModules as $module => $data) {
    $infos = $moduleManager->GetModuleInfos($module);
    $html .= '  <tr>';
    $html .= GetTableData($module, '', 'Module', $module);
    $html .= GetTableData($infos['Version']);
    if (array_key_exists($module, $installedModules)) {
        $html .= '<td></td>';
    } elseif ($processing) {
        $html .= '<td>processing ...</td>';
    } else {
        $html .= '<td><input type="button" name="Text" value="Modul Laden" onclick="trigger_button(\'Load\', \'' . $module . '\', \'\')"></td>';
    }
    $html .= GetTableData($infos['Description']);
	 *
	 * @file          IPSTwilight_Installation.ips.php
	 * @author        Andreas Brauneis
	 * @version
	 *  Version 2.50.1, 13.02.2012<br/>
	 *
	 */

	if (!isset($moduleManager)) {
		IPSUtils_Include ('IPSModuleManager.class.php', 'IPSLibrary::install::IPSModuleManager');

		echo 'ModuleManager Variable not set --> Create "default" ModuleManager'.PHP_EOL;
		$moduleManager = new IPSModuleManager('IPSTwilight');
	}

	$moduleManager->VersionHandler()->CheckModuleVersion('IPS','2.50');
	$moduleManager->VersionHandler()->CheckModuleVersion('IPSModuleManager','2.50.2');
	$moduleManager->VersionHandler()->CheckModuleVersion('IPSLogger','2.50.1');

	IPSUtils_Include ("IPSInstaller.inc.php",              "IPSLibrary::install::IPSInstaller");
	IPSUtils_Include ("IPSTwilight_Configuration.inc.php", "IPSLibrary::config::modules::Weather::IPSTwilight");

	$WFC10_Enabled        = $moduleManager->GetConfigValue('Enabled', 'WFC10');
	$WFC10_ConfigId       = $moduleManager->GetConfigValueIntDef('ID', 'WFC10', GetWFCIdDefault());
	$WFC10_Path           = $moduleManager->GetConfigValue('Path', 'WFC10');
	$WFC10_TabPaneItem    = $moduleManager->GetConfigValue('TabPaneItem', 'WFC10');
	$WFC10_TabPaneParent  = $moduleManager->GetConfigValue('TabPaneParent', 'WFC10');
	$WFC10_TabPaneName    = $moduleManager->GetConfigValue('TabPaneName', 'WFC10');
	$WFC10_TabPaneIcon    = $moduleManager->GetConfigValue('TabPaneIcon', 'WFC10');
	$WFC10_TabPaneOrder   = $moduleManager->GetConfigValueInt('TabPaneOrder', 'WFC10');
	$WFC10_TabItem        = $moduleManager->GetConfigValue('TabItem', 'WFC10');
 *
 */
/** @}*/
?>

<table border=1>
  <tr><th>Modul</th><th>Aktuelle<BR>Version</th><th>Neue<BR>Version</th><th>Update</th><th>&Auml;nderungen</th></tr>
<?php 
IPSUtils_Include("IPSModuleManager.class.php", "IPSLibrary::install::IPSModuleManager");
$moduleManager = new IPSModuleManager();
$modules = $moduleManager->VersionHandler()->GetKnownUpdates();
$html = '';
foreach ($modules as $idx => $module) {
    $moduleManager = new IPSModuleManager($module);
    $infos = $moduleManager->GetModuleInfos();
    $changes = $moduleManager->VersionHandler()->GetChangeList($module);
    $text = '<table><tr><td><div style="text-align:left; color:grey; padding-left:10px; padding-right:10px;">Aktuelles Repository</div></td>' . '<td><div style="text-align:left; color:grey; padding-left:10px; padding-right:10px;">' . $infos['LastRepository'] . '</div></td></tr>';
    $text .= '<tr><td><div style="text-align:left; color:grey; padding-left:10px; padding-right:10px;">Neues Repository</div></td>' . '<td><div style="text-align:left; color:grey; padding-left:10px; padding-right:10px;">' . $infos['Repository'] . '</div></td></tr>';
    foreach ($changes as $version => $change) {
        $text .= '<tr><td><div style="text-align:left; color:white; padding-left:10px; padding-right:10px;">Version ' . $version . '</div></td>' . '<td><div style="text-align:left; color:white; padding-left:10px; padding-right:10px;">' . htmlentities($change, ENT_COMPAT, 'ISO-8859-1') . '</div></td></tr>';
    }
    $text .= '</table>';
    $html .= '  <tr>';
    $html .= GetTableData($module, '', 'Module', $module);
    $html .= GetTableData($infos['CurrentVersion']);
    $html .= GetTableData($infos['Version']);
    if (!$processing) {
        $html .= '<td><input type="button" name="Text" value="Update" onclick="trigger_button(\'Update\', \'' . $module . '\', \'\')"></td>';
    } else {
        $html .= '<td>processing ...</td>';
    }
<table border=1>
  <tr><th>Modul</th><th>Aktuelle<BR>Version</th><th>Verf&uuml;gbare<BR>Version</th><th>Status</th><th>Beschreibung</th></tr>
<?php 
IPSUtils_Include("IPSModuleManager.class.php", "IPSLibrary::install::IPSModuleManager");
$moduleManager = new IPSModuleManager();
$modules = $moduleManager->GetInstalledModules();
$html = '';
$updateButton = false;
foreach ($modules as $module => $version) {
    $moduleManager = new IPSModuleManager($module);
    $infos = $moduleManager->GetModuleInfos();
    $infos['LastRepository'] = str_replace('https://raw.github.com/', '...', $infos['LastRepository']);
    $infos['AvailableVersion'] = '';
    $versionAttribute = '';
    $stateAttribute = '';
    if ($moduleManager->VersionHandler()->CompareVersionsNewer($infos['CurrentVersion'], $infos['Version'])) {
        $infos['AvailableVersion'] = $infos['Version'];
        $versionAttribute = 'color:red;';
        $updateButton = true;
    }
    if ($infos['State'] != 'OK') {
        $stateAttribute = 'color:red;';
    }
    $html .= '  <tr>';
    $html .= GetTableData(null, null, $module, '', 'Module', $module);
    $html .= GetTableData($infos, 'CurrentVersion');
    $html .= GetTableData($infos, 'AvailableVersion', '', $versionAttribute, 'Updates');
    $html .= GetTableData($infos, 'State', '', $stateAttribute);
    //$html .=  GetTableData($infos, 'LastRepository');
    //$html .=  GetTableData($infos, 'Repository');
    $html .= GetTableData($infos, 'Description');
				IPSLogger_Inf(__file__, 'IPSModuleManagerGUI - Installation of Module '.$module);
				$moduleManager = new IPSModuleManager($module);
				$moduleManager->InstallModule();
			}
			IPSModuleManagerGUI_SetPage(IPSMMG_ACTION_MODULE, $module);
			break;
		case IPSMMG_ACTION_STORE:
			IPSModuleManagerGUI_StoreParameters($module, $_GET);
			IPSModuleManagerGUI_SetPage(IPSMMG_ACTION_MODULE, $module);
			break;
		case 'Refresh':
			break;
		case 'SearchUpdates':
			if (IPSModuleManagerGUI_GetLock($action, true)) {
				$moduleManager = new IPSModuleManager('', '', sys_get_temp_dir(), true);
				$moduleManager->VersionHandler()->BuildKnownModules();
			}
			break;
		case 'UpdateAll':
			if (IPSModuleManagerGUI_GetLock($action, true)) {
				IPSLogger_Inf(__file__, 'IPSModuleManagerGUI - Update of all Modules');
				$moduleManager = new IPSModuleManager();
				$moduleManager->UpdateAllModules();
			}
			break;
		case 'Update':
			if (IPSModuleManagerGUI_GetLock($action, true)) {
				IPSLogger_Inf(__file__, 'IPSModuleManagerGUI - Update of Module '.$module.' from Repository "'.$repository.'"');
				$moduleManager = new IPSModuleManager($module, $repository);
				$moduleManager->UpdateModule();
			}
	 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
	 * GNU General Public License for more details.
	 *
	 * You should have received a copy of the GNU General Public License
	 * along with the IPSLibrary. If not, see http://www.gnu.org/licenses/gpl.txt.
	 */    

	/**@ingroup ipsmodulemanagergui
	 * @{
	 *
	 * @file          IPSModuleManagerGUI_SearchUpdates.ips.php
	 * @author        Andreas Brauneis
	 * @version
	 *  Version 2.50.1, 29.10.2012<br/>
	 *
	 * Sucht nach Module Updates der IPSLibrary
	 *
	 */

	include_once "IPSModuleManagerGUI.inc.php";

	IPSUtils_Include ("IPSModuleManager.class.php", "IPSLibrary::install::IPSModuleManager");

	$moduleManager = new IPSModuleManager('', '', sys_get_temp_dir(), true);
	$versionHandler = $moduleManager->VersionHandler();
	$versionHandler->BuildKnownModules();

	IPSModuleManagerGUI_SetPage(IPSMMG_ACTION_OVERVIEW);
	
    /** @}*/
?>
	function SearchLastRepositories() {
		IPSUtils_Include ("IPSModuleManager.class.php", "IPSLibrary::install::IPSModuleManager");
		$moduleManager = $_IPS['MODULEMANAGER'];
		$infos    = $moduleManager->GetModuleInfos();
		$modules  = $moduleManager->GetInstalledModules();

		//print_r($modules);
		foreach ($modules as $module=>$data) {
			$downloadFile = IPS_GetKernelDir().'scripts\\IPSLibrary\\install\\DownloadListFiles\\'.$module.'_FileList.ini';
			$configFile   = IPS_GetKernelDir().'scripts\\IPSLibrary\\install\\InitializationFiles\\'.$module.'.ini';
			if (!file_exists($downloadFile) or !file_exists($configFile)) {
				$moduleManager->LogHandler()->Debug('Remove Module "'.$module.'" from InstalledModules (No Ini Files found)');
				$versionHandler = new IPSFileVersionHandler($module);
				$versionHandler->DeleteModule();
			} else {
				$moduleManager  = new IPSModuleManager($module, '', sys_get_temp_dir(), true);
				$versionHandler = $moduleManager->VersionHandler();
				$repository     = $versionHandler->GetModuleRepository();

				// Search current Repository
				if ($repository=='') {
					$files = scandir(IPS_GetKernelDir().'\\logs\\', 1);
					foreach ($files as $file) {
						// Found LogFile
						if ($repository<>'') {
							break;
						} elseif (strpos($file,'IPSModuleManager_')!==false) {
							$fileContent = file_get_contents(IPS_GetKernelDir().'\\logs\\'.$file);
							$lines = explode(PHP_EOL, $fileContent);
							$line1 = '';
							$line2 = '';
							if (count($lines)>0) {
								$line1 = $lines[0];
							}
							if (count($lines)>1) {
								$line2 = $lines[1];
							}

							// Found LogFile for Module
							if (   strpos($line1,'Set Version '.$module.'=')!==false
							    or strpos($line2,''.$module.'_FileList.ini')!==false) {
								//echo 'found '.$module; //return;

								// Search Repository
								foreach ($lines as $idx=>$line) {

									// Found Repository
									if (strpos($line,' --> '.IPS_GetKernelDir().'scripts\\IPSLibrary\\install\\DownloadListFiles\\'.$module.'_FileList.ini')!==false) {
										$start = strpos($line,'Copy ')+5;
										$end   = strpos($line,'IPSLibrary',strpos($line,'IPSLibrary')+1);
										$repository = substr($line, $start, $end-$start);
										break;
									}
								}
							}
						}
					}
					$moduleManager->LogHandler()->Debug('Add Last Repository: '.$module.'='.$repository);
					$versionHandler->SetModuleRepository($repository);
				}
				echo $module.'='.$repository.PHP_EOL;
			}
		}
	}
if (!$processing) {
    if (!$moduleInstalled) {
        $html .= '<input type="button" name="Text" value="Modul Laden"       onclick="trigger_button(\'Load\', \'' . $module . '\', \'\')">';
    } else {
        $html .= '<input type="button" name="Text" value="Modul Update"       onclick="trigger_button(\'Update\', \'' . $module . '\', \'\')">';
        $html .= '<input type="button" name="Text" value="Modul Installieren" onclick="trigger_button(\'Install\',\'' . $module . '\', \'\')">';
        $html .= '<input type="button" name="Text" value="Installations Wizard" onclick="trigger_button(\'Wizard\',\'' . $module . '\', \'\')">';
        $html .= '<input type="button" name="Text" value="Modul L&ouml;schen" onclick="trigger_button(\'Delete\', \'' . $module . '\', \'\')">';
    }
} else {
    echo 'processing ...';
}
$html .= '</p>';
// Required Modules
$html .= '<h5>Ben&ouml;tigte Module</h5>';
$modules = $moduleManager->VersionHandler()->GetRequiredModules($module);
$html .= '<table>';
foreach ($modules as $subModule => $version) {
    $html .= '<tr><td><div style="text-align:left; color:grey; padding-left:10px; padding-right:10px;">' . '<a style="" href="#" onClick="trigger_button(\'Module\',\'' . $subModule . '\',\'\')">' . $subModule . '</a></div></td>' . '<td><div style="text-align:left; color:grey; padding-left:10px; padding-right:10px;">' . $version . '</div></td></tr>';
}
$html .= '</table>';
// Versions
$html .= '<h5>Repository Versionen</h5>';
$modules = $moduleManager->VersionHandler()->GetRepositoryVersions($module);
$html .= '<table>';
foreach ($modules as $repository => $version) {
    $html .= '<tr><td><div style="text-align:left; color:grey; padding-left:10px; padding-right:10px;">' . $repository . '</div></td>' . '<td><div style="text-align:left; color:grey; padding-left:10px; padding-right:10px;">' . $version . '</div></td></tr>';
}
$html .= '</table>';
// ChangeList
$html .= '<h5>Liste der &Auml;nderungen</h5>';