<?php
#-------------------------------------------------------------------------
#
# Author: Ben Malen, <ben@conceptfactory.com.au>
# Co-Maintainer: Simon Radford, <simon@conceptfactory.com.au>
# Web: www.conceptfactory.com.au
#
#-------------------------------------------------------------------------
#
# Maintainer since 2011: Jonathan Schmid, <hi@jonathanschmid.de>
# Web: www.jonathanschmid.de
#
#-------------------------------------------------------------------------
#
# ListIt is a CMS Made Simple module that enables the web developer to create
# multiple lists throughout a site. It can be duplicated and given friendly
# names for easier client maintenance.
#
# When duplicating this module change the 'ListIt' below to the new module
# name, e.g. NewName. Follow the CMSMS module naming conventions, a-z with no
# punctuation characters or spaces. Also change the name of this file to match
# - NewName.module.php. Finally change the name of the ListIt folder to the
# same NewName
#
#-------------------------------------------------------------------------

class 66166d336bd65e05e3c1a444cc987f1c extends ListIt2
{
	#---------------------
	# Attributes
	#---------------------
	
	private static $duplicate = true;
	
	#---------------------
	# Magic methods
	#---------------------		
	
	public function __construct()
	{	
		parent::__construct();
	}

	#---------------------
	# Module api methods
	#---------------------		

	public function GetName()
	{
		return get_class($this);
	}

	function AllowAutoUpgrade()
	{
		return FALSE;
	}

	public function GetDependencies()
	{
		return array('ListIt2' => '1.2');
	}
	
	public function SubPackage()
	{
		return FALSE;
	}	

    public function GetHeaderHTML()
    {
        $config = cmsms()->GetConfig();
        $modulename = $this->GetName();
		$moduledir = $this->GetParentURLPath();
        $incdir = $moduledir.'/js/';
        $admindir = $config['admin_url'];
        $userkey = get_secure_param();
        $jqueryui = '';
        if (version_compare(CMS_VERSION, '1.11-alpha0') < 0) {
            $jqueryui = '<link type="text/css" rel="stylesheet" href="'. $moduledir .'/css/jquery-ui-1.8.2.custom.css"></link>';
        }

        $tmpl = <<<EOT
{$jqueryui}
<link type="text/css" rel="stylesheet" href="{$moduledir}/css/backend.css"></link>
<script type="text/javascript">
<!--
var CMS_ADMIN_DIR = '{$admindir}';
var CMS_USER_KEY = '{$userkey}';
var MODULE_NAME = '{$modulename}';
-->
</script>
<script type="text/javascript" src="{$incdir}jquery.quicksearch.js"></script>
<script type="text/javascript" src="{$incdir}jquery.tablesorter.min.js"></script>
<script type="text/javascript" src="{$incdir}jquery.slug.js"></script>
<script type="text/javascript" src="{$incdir}listit.js"></script>
EOT;
        return $this->ProcessTemplateFromData($tmpl);
    }	

	public function DoAction($name, $id, $params, $returnid='')
	{
		global $CMS_ADMIN_PAGE;
		
		$config = cmsms()->GetConfig();
		$smarty = cmsms()->GetSmarty();
		$db = cmsms()->GetDb();
		
		$smarty->assign_by_ref('mod', $this);
		$smarty->assign('actionid', $id);
		$smarty->assign('returnid', $returnid);
		
		if($CMS_ADMIN_PAGE) {
		
			$themeObject = cmsms()->get_variable('admintheme'); // <- Change this to use cms_utils::get_theme_object(); when ever we set min version to 1.11	
			$smarty->assign_by_ref('themeObject', $themeObject);		
		}	
	
		if ($name != '') {
		
			$name = preg_replace('/[^A-Za-z0-9\-_+]/', '', $name);

			// Check if we have action in this
			$filename = $this->GetModulePath() . '/action.' . $name . '.php';
			if (@is_file($filename)) {
			
				include($filename);
				return;
			}			
			
			// Check parent if we don't
			$filename = $this->GetParentPath() . '/action.' . $name . '.php';
			if (@is_file($filename)) {
			
				include($filename);
				return;
			}			
		}
	}
	
	function Upgrade($oldversion, $newversion)
	{
		$config = cmsms()->GetConfig();
		$smarty = cmsms()->GetSmarty();
		$db = cmsms()->GetDb();
		
		$response = FALSE;
		
		$filename = $this->GetParentPath() . '/method.upgradeGlobals.php';
		if (@is_file($filename)) {
			
			$res = include($filename);
			if($res == 1 || $res == '') $response = TRUE;
		}
		
		$filename = $this->GetModulePath() . '/method.upgrade.php';
		if (@is_file($filename)) {
			
			$res = include($filename);
			if($res == 1 || $res == '') $response = TRUE;
		}
		
		// Check if module in our own control tables yet
		$query = "SELECT module_id FROM " . cms_db_prefix() . "module_" . parent::_GetModuleAlias() . "_instances WHERE module_name = ?";	
		$modstatus = $db->GetOne($query, array($this->GetName()));
		
		if(!$modstatus) $this->RegisterModule();		
	
		return $response;
	}	

	function Install()
	{
		$config = cmsms()->GetConfig();
		$smarty = cmsms()->GetSmarty();
		$db = cmsms()->GetDb();
		
		$response = FALSE;
		
		$filename = $this->GetParentPath() . '/method.installGlobals.php';
		if (@is_file($filename)) {
			
			$res = include($filename);
			if($res == 1 || $res == '') {
				$response = FALSE;
			} else {
				$response = $res;
			}
		}
		
		$filename = $this->GetModulePath() . '/method.install.php';
		if (@is_file($filename)) {
			
			$res = include($filename);
			if($res == 1 || $res == '') {
				$response = FALSE;
			} else {
				$response = $res;
			}
		}
		
		if(!$response) {
		
			$this->RegisterModule();
		}
	
		return $response;
	}

	function Uninstall()
	{
		$config = cmsms()->GetConfig();
		$smarty = cmsms()->GetSmarty();
		$db = cmsms()->GetDb();
		
		$response = FALSE;
		
		$filename = $this->GetParentPath() . '/method.uninstallGlobals.php';
		if (@is_file($filename)) {
			
			$res = include($filename);
			if($res == 1 || $res == '') {
				$response = FALSE;
			} else {
				$response = $res;
			}
		}
		
		$filename = $this->GetModulePath() . '/method.uninstall.php';
		if (@is_file($filename)) {
			
			$res = include($filename);
			if($res == 1 || $res == '') {
				$response = FALSE;
			} else {
				$response = $res;
			}
		}
		
		if(!$response) {
		
			$this->UnregisterModule();
		}		
	
		return $response;
	}	
	
	#---------------------
	# Manipulation methods
	#--------------------- 	
	
	public function ModLang()
	{
		$mod = cmsms()->GetModuleInstance('ListIt2');
	
		$this->LoadLangMethods();

		$args = func_get_args();
		array_unshift($args,'');
		if(is_object($mod)) {
			$args[0] = &$mod;
		} 
		else {
			$args[0] = &$this;
		}

		return call_user_func_array('cms_module_Lang', $args);
	}
	
	public function GetParentPath()
	{
		$config = cmsms()->GetConfig();
		return cms_join_path($config['root_path'], 'modules' , parent::GetName());
	}
	
	public function GetParentURLPath($use_ssl=false)
	{
		$config = cmsms()->GetConfig();
		return ($use_ssl?$config['ssl_url']:$config['root_url']) . '/modules/' . parent::GetName();
	}	

	#---------------------
	# Internal methods
	#--------------------- 		
	
	public function _GetModuleAlias()
	{
		return strtolower($this->GetName());
	}	
	
	#---------------------
	# Instance methods
	#--------------------- 		

	final private function RegisterModule()
	{
		$db = cmsms()->GetDb();
	
		$query  = 'INSERT INTO ' . cms_db_prefix() . 'module_' . parent::_GetModuleAlias() . '_instances (module_name) VALUES (?)';
		$result = $db->Execute($query, array($this->GetName()));
		
		if (!$result) die('FATAL SQL ERROR: ' . $db->ErrorMsg() . '<br/>QUERY: ' . $db->sql);
		
		return true;
	}
	
	final private function UnregisterModule()
	{
		$db = cmsms()->GetDb();

		$query = 'DELETE FROM ' . cms_db_prefix() . 'module_' . parent::_GetModuleAlias() . '_instances WHERE module_name = ?';
		$result = $db->Execute($query, array($this->GetName()));
		
		if (!$result) die('FATAL SQL ERROR: ' . $db->ErrorMsg() . '<br/>QUERY: ' . $db->sql);
		
		return true;		
	}	
		
} // end of class

?>