<?php

class CMaintenance extends CPlugin{
	
	var $tplvars; 

	function CMaintenance() {
		//$this->CPlugin($db, $tables , $templates);

		$this->name = "maintenance";
	}

	function DoEvents(){
		global $base, $_CONF, $_TSM , $_VARS , $_USER , $_BASE , $_SESS;

		parent::DoEvents();

		if ($_GET["mod"] == "maintenance") {

			$sub = $_GET["sub"];
			$action = $_GET["action"];

			//read the module
			$this->tpl_module = $this->module->plugins["modules"]->LoadDefaultModule("maintenance");
			global $_MAINTENANCE_MODULE;
			$_MAINTENANCE_MODULE  = $this->tpl_module;

			switch ($sub) {

				case "messages":
				case "images":
					$data = new CSQLAdmin($sub, $this->__parent_templates,$this->db,$this->tables,$extra);
					$this->PrepareFields($data->forms["forms"]);
					return $data->DoEvents();
				break;


				case "settings":

					switch ($_GET["a"]) {
						case "language":
							urlredirect("index.php?mod=modules&sub=default-language&action=details&module_id={$this->tpl_module['module_id']}&t=u");
						break;

						case "advanced":
							urlredirect("index.php?mod=modules&sub=default-settings&action=details&module_id={$this->tpl_module['module_id']}&t=u");
						break;

						default:
							urlredirect("index.php?mod=modules&sub=default&action=details&module_id={$this->tpl_module['module_id']}&t=u");
						break;

					}
					
				break;

				case "texts":
					$_GET["module_id"] = $this->tpl_module["module_id"];
					return $this->__adminTexts();
				break;

				default:
					$sub = "users";
				case "users":
					$data = new CSQLAdmin($sub, $this->__parent_templates,$this->db,$this->tables,$extra);
					$this->PrepareFields($data->forms["forms"]);


					$data->functions = array( 
							"onstore_prepare" => array(&$this , "StoreUser"),
							"onstore" => array(&$this , "StoreUser"),
					);					

					return $data->DoEvents();
				break;
			}
		}
	}

	
	function StoreUser($record) {
		//check if is a new user
		if (!$record["user_id"]) {

			$_POST["user_date"]				= time();
			$_POST["user_key"]				= md5($_POST["user_email"] . time());

			//update the record
			$record = $_POST;


			if ($_POST["send_mail"]) {
				$id = $this->module->plugins["mail"]->SendMail(
					$this->module->plugins["mail"]->GetMail(
						$this->tpl_module["settings"]["set_notification_mail"],
						$record
					)
				);			
			}
			

		}

		//notification emails if the status was changed
		if ($record["user_id"] && $_POST["send_mail"]) {


			if (!$record["user_key"]) {

				$data = $this->db->QFetchArray("SELECT user_key FROM {$this->tables['plugin:maintenance_users']} WHERE user_id={$record[user_id]}");

				$record = array_merge(
					$record , 
					$data
				);
			}
			
			$id = $this->module->plugins["mail"]->SendMail(
				$this->module->plugins["mail"]->GetMail(
					$this->tpl_module["settings"]["set_notification_mail"],
					$record
				)
			);			

			//hell knows why this happends
			$_POST["send_mail"] = 0;



		}
		

	}


	/**
	* description
	*
	* @param
	*
	* @return
	*
	* @access
	*/
	function NoticeBar() {
		$tpl = $this->module->plugins["modules"]->LoadDefaultModule("maintenance");

		if ($tpl["settings"]["set_active"]) {
			$template = new CTemplateDynamic($this->tpl_path . "notice.htm");

			return $template->blockReplace("Main" , array("module" => $tpl["module_id"]));
		}
		

	}
	
}

?>