<?php
/**
 * @package Hessennews (Component, Module), pkg_hessennews.
 * @subpackage
 * @copyright Copyright (C) 2013 - 2013, www.hessennews.tv. All rights reserved.
 * @license http://www.hessennews.tv/index.php/component/content/article/145-intern-hessenreport/5041-nutzungsbedingungen-joomla-komponente
 * @author G@HService Volkmar Volli Schlothauer Berlin Neukölln
 * @authorUrl http://www.ghsvs.de
 * @authorEmail hntv@ghsvs.de
 * @creationDate 11-2013
 * @version 0.0.7
*/
?>
<?php
defined('_JEXEC') or die('Restricted access');
class pkg_hessennewsInstallerScript{
	function install($parent){
		echo '<p><strong>Do not forget to configure the component HessenNews before you use it the first time. Vergessen Sie nicht, die Komponente Hessennews vor der ersten Verwendung zu konfigurieren.</strong></p>';
	}
 /*
 * $parent is the class calling this method.
 * update runs after the database scripts are executed.
 * If the extension exists, then the update method is run.
 * If this returns false, Joomla will abort the update and undo everything already done.
 */
	function update($parent){
	}
 function preflight($type, $parent){
 }
	/**
	 * method to run after an install/update/uninstall method
	 *
	 * @return void
	 */
	function postflight($type, $parent){
		// $parent is the class calling this method
		// $type is the type of change (install, update or discover_install)
  if(strtolower($type)=='update' || strtolower($type)=='install'){
	 $params=$this->getParam();
		#echo '4654sd48sa7d98sD81s8d71dsa params '.print_r($params,true);
  $installedVersion=$this->getManifest('version');
		#echo '4654sd48sa7d98sD81s8d71dsa '.print_r($installedVersion,true);
  if(version_compare($installedVersion, '0.0.7', 'le')){
   if(!isset($params['hntv_robots'])){
    $params['hntv_robots']='noindex,follow';
    $this->saveParams($params);
   }
			#echo '4654sd48sa7d98sD81s8d71dsa $this->saveParams($params) '.print_r($params,true);exit;
			$updateLangFiles=array(
		 '/administrator/components/com_hessennews/language/de-DE/de-DE.com_hessennews.ini'
			=>'/administrator/language/de-DE/de-DE.com_hessennews.ini',
		 
			'/administrator/components/com_hessennews/language/en-GB/en-GB.com_hessennews.ini'
			=>'/administrator/language/en-GB/en-GB.com_hessennews.ini',
						
		 '/administrator/components/com_hessennews/language/de-DE/de-DE.com_hessennews.sys.ini'
			=>'/administrator/language/de-DE/de-DE.com_hessennews.sys.ini',
		 
			'/administrator/components/com_hessennews/language/en-GB/en-GB.com_hessennews.sys.ini'
			=>'/administrator/language/en-GB/en-GB.com_hessennews.sys.ini',
			
		 '/components/com_hessennews/language/de-DE/de-DE.com_hessennews.ini'
			=>'/language/de-DE/de-DE.com_hessennews.ini',
			
		 '/components/com_hessennews/language/en-GB/en-GB.com_hessennews.ini'
			=>'/language/en-GB/en-GB.com_hessennews.ini',
			
			'/modules/mod_hessennews_tv/language/de-DE/de-DE.mod_hessennews_tv.ini'
			=>'/language/de-DE/de-DE.mod_hessennews_tv.ini',
			
			'/modules/mod_hessennews_tv/language/en-GB/en-GB.mod_hessennews_tv.ini'
			=>'/language/en-GB/en-GB.mod_hessennews_tv.ini',
			
		 '/modules/mod_hessennews_tv/language/de-DE/de-DE.mod_hessennews_tv.sys.ini'
			=>'/language/de-DE/de-DE.mod_hessennews_tv.sys.ini',
			
		 '/modules/mod_hessennews_tv/language/en-GB/en-GB.mod_hessennews_tv.sys.ini'
			=>'/language/en-GB/en-GB.mod_hessennews_tv.sys.ini',
   );
   jimport('joomla.filesystem.file');
   foreach($updateLangFiles as $new=>$old){
    $new=JPATH_SITE.$new;
    $old=JPATH_SITE.$old;
    if(JFile::exists($new) && JFile::exists($old)){
     JFile::move($old, $old.'deprecated');
    }
   }
  }

   $parent->getParent()->setRedirectURL('index.php?option=com_hessennews');
		}
	}
	/*
     * Get variable(s) from the manifest file (actually, from the manifest cache).
     */
 function getManifest($what=false, $name='com_hessennews'){
		$db=JFactory::getDbo();
		$query=$db->getQuery(true);
		$query
			->select('manifest_cache')
			->from('#__extensions')
			->where('name='.$db->quote($name));
		$db->setQuery($query);
		$result=$db->loadResult();
		$params=json_decode($result, true);
		if(!$what)return $params;
		return $params[$what];
	}
 function getParam($what=false, $name='com_hessennews'){
		$db=JFactory::getDbo();
		$query=$db->getQuery(true);
		$query
			->select('params')
			->from('#__extensions')
			->where('name='.$db->quote($name));
		$db->setQuery($query);
		$result=$db->loadResult();
		$params=json_decode($result, true);
		if(!$what)return $params;
		return $params[$what];
	}
	function saveParams($paramsNew, $name='com_hessennews'){
		#echo '4654sd48sa7d98sD81s8d71dsa '.print_r($paramsNew,true);exit;
		if(!count($paramsNew))return false;
		$paramsNew=json_encode($paramsNew);
		$db=JFactory::getDbo();
  $db->setQuery('UPDATE #__extensions SET params='.$db->quote($paramsNew).' WHERE name='.$db->quote($name));
  $db->execute();
		return true;
	}
}
?>