<?php
	require_once( $GLOBALS["site_root"]."/includes/urlgen/Sites.inc.php" );

	class ConstantsModel {
		protected $oTemplate = null;
		protected $sLanguage = "hu";

		public function __construct( $obj = null , $lang = "hu" ) {
			$this->oTemplate = $obj;
			if (!$obj) return;
			$this->sLanguage = $lang;
	
			// setting default params
			$this->oTemplate->setParam( "tick" , sprintf( "<img src=\"/images/icons/tick_icon.png\" alt=\"%s\" />" , __("_yes_") ) );

			$this->oTemplate->setParam( "server_prefix" , SitesURL::renderURLLang( $lang ) );
		}

		public function getConstants() {
			$query = sprintf( "select * from article_constants" );
			$data = FNTH_db_get_resultset( $query );
			return $data;
		}

		protected function tstampFormat( &$data , $prefix , $tstamp_prefix = true ) {
			if ($tstamp_prefix) {
				if (!isset( $data[$prefix."tstamp"] )) return;
				$data[$prefix."date"]=fo3_format_date_to_screen( $data[$prefix."tstamp"] );
				$data[$prefix."datetime"]=fo3_format_datetime_to_screen( $data[$prefix."tstamp"] );
				$data[$prefix."time"]=substr( fo3_format_datetime_to_screen( $data[$prefix."tstamp"] ) , 11 , 5 );
			} else {
				if (!isset( $data[$prefix] )) return;
				$data[$prefix."date"]=fo3_format_date_to_screen( $data[$prefix] );
				$data[$prefix."datetime"]=fo3_format_datetime_to_screen( $data[$prefix] );
				$data[$prefix."time"]=substr( fo3_format_datetime_to_screen( $data[$prefix] ) , 11 , 5 );
			}
		}

		public function addSystemParametersToTemplate() {
			require_once( $GLOBALS["site_root"] . "/includes/models/SystemParameters.inc.php" );
			$sm = new SystemParametersModel();
			$sysparams = $sm->getAllVars();
			$this->oTemplate->setParam( "sysparams" , $sysparams );
		}

		public function addUserConstantsToTemplate( $user_id , $variable_name = "user" , $user = null ) {
			require_once( $GLOBALS["site_root"] . "/includes/models/User.inc.php" );
			$um = new UserModel();
			if (!isset( $user ))
				$user = $um->getUserFromUserId( $user_id );
			$this->tstampFormat( $user , "reg_" );
			$this->oTemplate->setParam( $variable_name , $user );
			return $user;
		}

		public function addTodoConstantsToTemplate( $user_id ) {
			require_once( $GLOBALS["site_root"] . "/includes/models/TodoList.inc.php" );
			require_once( $GLOBALS["site_root"] . "/includes/widgets/TodoPanel.inc.php" );
			$tp = new TodoPanel( "todo" );
			$tp->setUserId( $user_id );
			$this->oTemplate->setParam( "todo_html" , $tp->render() );
		}

		public function addMemberConstantsToTemplate( $member_id , $variable_name = "member" , $member = null ) {
			require_once( $GLOBALS["site_root"] . "/includes/models/Member.inc.php" );
			$mm = new MemberModel();
			if (!isset( $member ))
				$member = $mm->getMemberFromMemberId( $member_id );
			// timestamps
			$this->tstampFormat( $member , "status_sent" );
			$this->tstampFormat( $member , "paid_" );
			$this->tstampFormat( $member , "expires_" );

			// prices
			require_once( $GLOBALS["site_root"] . "/includes/models/SystemParameters.inc.php" );
			$sm = new SystemParametersModel();
			$prefixes = array( "" , "prolong_" );
			$lengths = array( "6m" , "12m" );
			$currencies = array( "huf" , "eur" );

			$is_active = $mm->isMemberActiveSubscriber( $member_id , $member );

			$mt = $member["member_type"];
			foreach ($prefixes as $prefix) {
				foreach ($lengths as $length) {
					foreach ($currencies as $currency) {
						$name = sprintf( "%s%s_%s_%s_price",
							$prefix,
							$mt,
							$length,
							$currency );
						$newname = sprintf( "price_%s%s_%s",
							$prefix,
							$length,
							$currency );
						$member[$newname] = $sm->getVar( $name );
						if (
							($is_active && $prefix=="prolong_") ||
							(!$is_active && $prefix=="")) {
							$curname = sprintf( "current_price_%s_%s" , $length , $currency );
							$member[$curname]=$member[$newname];
						}
					}
				}
			}
			$member["member_type_text"]=__("users_".$mt,"attr");

			if (isset( $member["paid_tstamp"] )) {
				$member["has_active_subscription"]=($member["expires_tstamp"]>fo3_date( "Y-m-d H:i:s" )) ? "t" : "f";
			} else {
				$member["has_active_subscription"]="f";
			}
			$this->oTemplate->setParam( $variable_name , $member );
		}

		public function addJobConstantsToTemplate( $job_id , $variable_name = "job" , $job = false ) {
			if (!$job) {
				require_once( $GLOBALS["site_root"] . "/includes/models/Job.inc.php" );
				$jm = new JobModel();
				$job = $jm->getJobFromId( $job_id , true , $this->sLanguage );
			}
			$this->tstampFormat( $job , "c" );
			$this->tstampFormat( $job , "closed_at" , false );
			$this->tstampFormat( $job , "revoked_at" , false );
			$this->tstampFormat( $job , "sent_at" , false );

			$this->oTemplate->setParam( $variable_name , $job );
		}

		public function addJobQuoteConstantsToTemplate( $job_quote_id , $variable_name = "job_quote" , $job_quote = false ) {
			if (!$job_quote) {
				require_once( $GLOBALS["site_root"] . "/includes/models/JobQuote.inc.php" );
				$jqm = new JobQuoteModel();
				$job_quote = $jqm->getJobQuoteFromJobQuoteId( $job_quote_id );
			}
			$this->oTemplate->setParam( $variable_name , $job_quote );
		}

		public function addCreditcardConstantsToTemplate( $cc_tran_id , $variable_name = "cc_tran" , $cc_tran = false ) {
			if (!$cc_tran) {
				require_once( $GLOBALS["site_root"] . "/includes/models/CreditCard.inc.php" );
				$ccm = new CreditCardModel();
				$cc_tran = $ccm->getTransactionFromTransactionId( $cc_tran_id );
			}
			$this->oTemplate->setParam( $variable_name , $cc_tran );
		}

		public function addForumTopicConstantsToTemplate( $forum_topic_id , $variable_name = "forum_topic" , $forum_topic = false ) {
			if (!$forum_topic_id) return;
			if (!$forum_topic) {
				require_once( $GLOBALS["site_root"] . "/includes/models/Forum.inc.php" );
				$fm = new ForumModel();
				$forum_topic = $fm->getTopicFromId( $forum_topic_id );
			}
			$this->oTemplate->setParam( $variable_name , $forum_topic );
		}

		public function addForumPostConstantsToTemplate( $forum_post_id , $variable_name = "forum_post" , $forum_post = false ) {
			if (!$forum_post_id) return;
			if (!$forum_post) {
				require_once( $GLOBALS["site_root"] . "/includes/models/Forum.inc.php" );
				$fm = new ForumModel();
				$forum_post = $fm->getForumPostFromId( $forum_post_id );
			}
			$this->oTemplate->setParam( $variable_name , $forum_post );
		}
		
	}

?>
