<?php
///////////////////////////////////////////////////////////////////////////////////////////
///Script PHP/MYSQL of management of classifieds ads developed by Script PAG
///Script PAG all rights reserved. Use under license. http://www.script-pag.com
///////////////////////////////////////////////////////////////////////////////////////////

############################################################ 

use \modules as modules;
use \loader as loader;

abstract class abstractModule {
	/*** @var _name string*/
	private $_name;
	/*** @var _version string*/
	private $_version;
	/*** @var _category string*/
	public $_category;
	/*** @var _active int*/
	private $_active;
	/*** @var _cache array*/
	private $_cache;
	/*** @var _language string*/
	private $_language;
	
	//// Initaialize module front environment
	abstract public function init($action);
	//// Initaialize module admin environment
	abstract public function initSP($action);
	//// Install module method
	abstract public function install();
	//// Enable module method
	abstract public function enable();
	//// Disable module method
	abstract public function disable();
	//// Delete module method
	abstract public function del();
	
	//// Class constructor
	public function __construct($name, $version, $category, $cache = null, $language_variable = null) {
		$this->_name = $name;
		$this->_version = $version;
		$this->_category = $category;
		$this->_cache = is_string($cache) && strlen($cache) > 0 ? [$cache] : (is_array($cache) ? $cache : []);
		$this->_language = ($language_variable ? $language_variable : strtolower($this->_name)).'_language';
	}
	
	//// Return name value
	public function name() {
		return $this->_name;
	}
	
	//// Test if the module belogns to payment-method category
	public function payment() {
		return $this->_category === 'payment-method' ? true : false;
	}
	
	//// Return category value
	public function category() {
		return $this->_category;
	}
	
	//// Return version value
	public function version() {
		return $this->_version;
	}
	
	//// Return active value
	public function active($active = null) {
		if(isset($active)) $this->_active = (int) $active;
		return isset($active) ? $this : $this->_active;
	}
	
	//// Return language value
	public function language() {
		return $this->_language;
	}
	
	//// Return cache['routes'] value
	public function route($route, $lang = null) {
		$codelang = isset($lang) ? $lang : codelang();
		return isset($this->_cache['routes'][$codelang]) && isset($this->_cache['routes'][$codelang][$route]) ? $this->_cache['routes'][$codelang][$route] : null;
	}
	
	//// Set view value 
	public function view($name) {
		global $view;
		$filename = strtolower($this->_name).'/template/'.$name.'View.php';
		if(loader::exists($filename)) $view[] = (modules::mode() === 'SP' ? '../' : '').'includes/functions/'.$filename;
		return $this;
	}
	//// Add view, need complete path
	public function addView($filename) {
		global $view;
		if(loader::exists($filename)) $view[] = (modules::mode() === 'SP' ? '../' : '').$filename;
		return $this;
	}
	
	//// Add data to the view
	public function data($name, $value = null, $context = null) {
		return modules::addData($name, $value, $context);
	}
		
	//// Test if module has warning information
	public function warning() {		
		$warning = method_exists($this, '__settingsWarning') ? $this->__settingsWarning() : null;	
		return     strlen($warning) > 0 ? $warning : null;
	}
	
	//// Load module language file according to mode
	public function loadLanguage() {
		global $language, $language_adm;
		$mode = modules::mode();
		$variable = $this->_language;
		$filename = strtolower($this->_name).'/language/'.($mode === 'SP' ? 'admin' : 'front').'/';
		$sessionname = $mode === 'SP' ? 'lang' : 'code_lang';
		if($_SESSION[$sessionname] === 'fr') $filename .= 'french.php';
		elseif($_SESSION[$sessionname] === 'en') $filename .= 'english.php';
		elseif($_SESSION[$sessionname] === 'es') $filename .= 'spanish.php';
		elseif($_SESSION[$sessionname] === 'it') $filename .= 'italian.php';
		elseif($_SESSION[$sessionname] === 'ru') $filename .= 'russian.php';
		elseif($_SESSION[$sessionname] === 'ar') $filename .= 'arabic.php';
		$filename = loader::exists($filename);
		if($filename !== false && !language($variable)) {
			require_once $filename;
			if(isset($$variable) && is_array($$variable)) {
				modules::addLanguage($variable, $$variable);
				unset($$variable);
			}
		}
		return $this;
	}
	
	//// Load module language file according to mode
	public function loadRoutes() {
		$routes = [];
		if(method_exists($this, 'routes')) {
			$tmp = $this->routes();
			foreach($tmp as $key => $value) {
				if(isset($this->_cache['routes'][codelang()]) && isset($this->_cache['routes'][codelang()][$key])) {
					$routes[$this->_cache['routes'][codelang()][$key]] = $value;	
					$routes[$key] = $value;	
				} else $routes[$key] = $value;
			}
		}
		return $routes;
	}
	
	//// Get translation of url's slug
	public function routeTranslation() {
		global $cache_lang, $action;
		$translations = [];
		if(sizeof($cache_lang) > 1) {
			foreach($cache_lang as $lang) {
				if($lang['active'] == 1 && isset($this->_cache['routes'][$lang['code_lang']]) && isset($this->_cache['routes'][$lang['code_lang']][$action])) {
					$translations[$lang['code_lang']] = $this->_cache['routes'][$lang['code_lang']][$action];
				}
			}
		}
		return $translations;
	}
	
	//// Load module cache file
	public function loadCache($cachename = null) {
		$cache = [];
		if(isset($cachename) && strlen($cachename) > 0) {
			$filename = loader::exists(strtolower($this->_name).'/cache/'.$cachename.'.php');
			if($filename !== false) {
				require $filename;
				if(isset($$cachename) && is_array($$cachename)) {
					$cache = $$cachename;
					if(modules::ready()) modules::addCache($cachename, $$cachename);
					unset($$cachename);
				}
			}
		} else {
			if(is_array($this->_cache) && sizeof($this->_cache) > 0) {
				foreach($this->_cache as $cachename) {
					$filename = loader::exists(strtolower($this->_name).'/cache/'.$cachename.'.php');
					if($filename !== false) {
						require $filename;
						if(isset($$cachename) && is_array($$cachename)) {
							$cache += $$cachename;
							if(modules::ready()) modules::addCache($cachename, $$cachename);
							unset($$cachename);
						}
					}
				}
			}
		}
		$this->_cache = $cache;
		return $this;
	}
	
	//// Test if the class as called method
	public function methodExists($method) {
		return method_exists($this, $method);
	}
}