<?php
/*
	Stempora Web Framework
	Copyright (c) 2002-2014 Stempora. 
	All rights reserved.
		web:  www.stempora.com
		mail: support@stempora.com		
*/

// dependencies

/**
* description
*
* @library	
* @author	
* @since	
*/
class CJobs extends CPlugin{
	
	var $tplvars; 

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

		$this->name == "jobs";
	}

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

		parent::DoEvents();

		if ($_GET["mod"] == "jobs") {
			$sub = $_GET["sub"];
			$action = $_GET["action"];

			//read the module
			$this->tpl_module = $this->module->plugins["modules"]->getModuleInfo($_GET["module_id"]);
			$this->plugins["modules"]->ValidateModule($this->tpl_module);

			switch ($sub) {

				case "texts":
					return $this->__adminTexts();
				break;
			


				case "landing":
					$sub = "resumes";
				case "items":					
				case "fields":
				case "resumes":

					if (($sub == "resumes")  && ($_GET["item_id"])){
						$this->db->QueryUpdate(
							$this->tables["plugin:jobs_resumes"],
							array(
								"item_new"	=> "0",
							),
							"item_id={$_GET[item_id]}"
						);
					}
					

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

				case "download":
					return $this->DownloadResume();
				break;
			}
		}
	}


	/**
	* description
	*
	* @param
	*
	* @return
	*
	* @access
	*/
	function DownloadResume() {
		$item = $this->db->QFetchArray("SELECT * FROM {$this->tables['plugin:jobs_resumes']} WHERE item_id={$_GET[item_id]}");

		if (is_Array($item)) {

			$mime = new Cmime();
			$mime->Set("unknown");
			$mime->Filename($item["item_file_file"]);

			readfile("../upload/jobs/files/{$item[item_id]}.file");
			die();
		}
		
	}

	
	/**
	* description
	*
	* @param
	*
	* @return
	*
	* @access
	*/
	function onCreate($module_data) {
		global $_LANG_ID;

		$fields = array(

			array(
				"module_id"		=> $module_data["mod_id"],
				"lang_id"		=> $_LANG_ID,
				"item_field"	=> "first_name",
				"item_type"		=> "usertext",
				"item_required"	=> "1",
				"item_size"		=> "full",
				"item_title"	=> "First Name",
				"item_error_msg"=> "Please enter your first name!",
			),

			array(
				"module_id"		=> $module_data["mod_id"],
				"lang_id"		=> $_LANG_ID,
				"item_field"	=> "last_name",
				"item_type"		=> "usertext",
				"item_required"	=> "1",
				"item_size"		=> "full",
				"item_title"	=> "Last Name",
				"item_error_msg"=> "Please enter your last name!",
			),

			array(
				"module_id"		=> $module_data["mod_id"],
				"lang_id"		=> $_LANG_ID,
				"item_field"	=> "email",
				"item_type"		=> "useremail",
				"item_required"	=> "1",
				"item_size"		=> "full",
				"item_system"	=> "1",
				"item_title"	=> "Email",
				"item_error_msg"=> "Please enter your email!",
			),


			array(
				"module_id"		=> $module_data["mod_id"],
				"lang_id"		=> $_LANG_ID,
				"item_field"	=> "phone",
				"item_type"		=> "usertext",
				"item_required"	=> "0",
				"item_size"		=> "full",
				"item_system"	=> "0",
				"item_title"	=> "Phone",
			),

			array(
				"module_id"		=> $module_data["mod_id"],
				"lang_id"		=> $_LANG_ID,
				"item_field"	=> "job",
				"item_type"		=> "userjobs",
				"item_required"	=> "1",
				"item_size"		=> "full",
				"item_title"	=> "Job",
				"item_error_msg"=> "Please select the desired job!"
			),

			array(
				"module_id"		=> $module_data["mod_id"],
				"lang_id"		=> $_LANG_ID,
				"item_field"	=> "message",
				"item_type"		=> "usermessage",
				"item_required"	=> "1",
				"item_size"		=> "full",
				"item_title"	=> "Message",
				"item_error_msg"=> "Please enter your message!",

			),
		);


		//generate default form structure
		foreach ($fields as $key => $val) {

			$val["item_id"] = $this->db->QueryInsert(
				$this->tables["plugin:jobs_fields"] , 
				$val
			);

			$this->db->QueryUpdate(
				$this->tables["plugin:jobs_fields"] , 
				array(
					"item_order"	=> $val["item_id"]
				),
				"item_id ={$val[item_id]}"
			);

			$this->db->QueryInsert(
				$this->tables["plugin:jobs_fields_lang"] ,
				$val
			);
		}
		
	}

	function onDelete($module_data) {

		$tables  = array(
			"plugin:jobs_items" , 
			"plugin:jobs_items_lang",
			"plugin:jobs_fields",
			"plugin:jobs_fields_lang",
			"plugin:jobs_resumes",
		);

		foreach ($tables as $key => $val) {
			$this->db->Query(
				"DELETE FROM " . $this->tables[$val] . " WHERE module_id={$module_data[mod_id]}"
			);
		}

		parent::onDelete($module_data);	
	}

	
	/**
	* description
	*
	* @param
	*
	* @return
	*
	* @access
	*/
	function DashboardWidgets($mod_id) {
		global $base , $_CONF;

		$module = $this->module->plugins["modules"]->getModuleInfo($mod_id);		

		if (is_array($module)) {
			$_GET["module_name"] = $module["mod_name"] ;
			$_GET["module_id"] = $mod_id;
			$_CONF["forms"]["adminpath"] = _MODPATH . "jobs/forms/";

			$latest = new CSQLAdmin("dashboard/latest", $this->__parent_templates,$this->db,$this->tables,$extra);
			$content = $latest->DoEvents();
			if (!(stristr($content , "No available records.") && !$_GET["showempty"])) {

				return array(
					array(
						"width"	=> "6",
						"type"	=> "widget",
						"pos"	=> "1",
						"body"	=> $content
					),
				);
			}
		}
		

	}

}

?>