<?php
/**
 * AgileBill - Open Billing Software
 *
 * This body of work is free software; you can redistribute it and/or
 * modify it under the terms of the Open AgileBill License
 * License as published at http://www.agileco.com/agilebill/license1-4.txt
 *
 * Originally authored by Tony Landis, AgileBill LLC
 *
 * Recent modifications by Deon George
 *
 * @author Deon George <deonATleenooksDOTnet>
 * @copyright 2009 Deon George
 * @link http://osb.leenooks.net
 *
 * @link http://www.agileco.com/
 * @copyright 2004-2008 Agileco, LLC.
 * @license http://www.agileco.com/agilebill/license1-4.txt
 * @author Tony Landis <tony@agileco.com>
 * @package AgileBill
 * @subpackage Modules:StaticPage
 */

/**
 * The main AgileBill Static Page Class
 *
 * @package AgileBill
 * @subpackage Modules:StaticPage
 */
class static_page extends OSB_module {
	/**
	 * Delete a record
	 */
	public function delete($VAR) {	
		$this->associate_DELETE = array();

		array_push($this->associated_DELETE,array('table'=>'static_page_translate','field'=>'static_page_id'));

		return parent::delete($VAR);
	}		

	/**
	 * Get a list of pages for a category
	 *
	 * @uses static_page_category
	 * @uses static_page_translate
	 */
	public function get_category_page_list($VAR) {
		include_once(PATH_MODULES.'static_page_category/static_page_category.inc.php');
		$spc = new static_page_category();

		# Check we have an ID and are authorised to see the pages in that ID.
		if (! isset($VAR['id']) || ! $spc->get_page_categories($VAR))
			return false;

		$db = &DB();

		$result = $db->Execute(
			sqlSelect($db,'static_page','id,name,date_expire,date_start',array('static_page_category_id'=>$VAR['id'],'status'=>1),'sort_order,date_orig,name'));

		if (! $result || $result->RecordCount() == 0)
			return false;

		include_once(PATH_MODULES.'static_page_translate/static_page_translate.inc.php');
		$spt = new static_page_translate();

		global $C_auth;
		$ii = 0;

		$pages = array();
		while (! $result->EOF) {
			$start = $result->fields['date_start'];
			$expire = $result->fields['date_expire'];

			# Check that it is not expired
			if (($start == '0' || $start <= time()+2) && ($expire == '0' || $expire >= time())) {

				# Get the translated name, for the current session language
				$translate = $spt->get_translated_page(array('id'=>$result->fields['id'],'language_id'=>SESS_LANGUAGE));

				if ($translate) {
					$translate['name'] = $result->fields['name'];
					array_push($pages,$translate);

				} else {
					# Get the translated name, for the default langauge
					$translate = $spt->get_translated_page(array('id'=>$result->fields['id'],'language_id'=>DEFAULT_LANGUAGE));

					if ($translate) {
						$translate['name'] = $result->fields['name'];
						array_push($pages,$translate);
					}
				}
			}

			$result->MoveNext();
		}

		return $pages;
	}

	public function page_list($VAR) {
		global $smarty;

		$smart = $this->get_category_page_list($VAR);

		if (! $smart || ! count($smart)) {
			 $smarty->assign('static_page_display',false);
			 return false;

		} else {
			$smarty->assign('static_page_display',true);
			$smarty->assign('static_page_results',$smart);
			return true;
		}
	}

	/**
	 * Show a page
	 */
	public function page_show($VAR) {	
		global $smarty;

		if (! isset($VAR['id']) && !isset($VAR['name'])) {
			$smarty->assign('static_page_display', false);
			return false;
		}

		# Get the page.
		$db = &DB();
		$page = $db->Execute($q=sqlSelect($db,'static_page','*',sprintf('status=1 AND (name=::%s:: OR id=::%s::)',@$VAR['name'],@$VAR['id'])));

		if (! $page || $page->RecordCount() == 0) {
			$smarty->assign('static_page_display',false);
			return false;
		}

		# Check we have an ID and are authorised to see the pages in that ID.
		$smart = $this->get_category_page_list(array('id'=>$page->fields['static_page_category_id']));
		if (! $smart) {
			$smarty->assign('static_page_display',false);
			return false;
		}

		$pageValid = false;
		foreach ($smart as $key => $values)
			if ($values['name'] == $page->fields['name']) {
				$pageValid = true;
				break;
			}

		if (! $pageValid) {
			 $smarty->assign('static_page_display',false);
			 return false;

		} else {
			$smarty->assign('static_page_display',true);
			$smarty->assign('static_page_results',$values);
			return true;
		}
	}
}
?>