<?php
/**
 *		sabreTools Module for plugin to Drupal 7 Framework
 *
 */


/*
 *	  CLASS:  YearList
 *    
*/

class YearList {
	private $list = array();
	private $app_name = '';
  
  const INCL_ALL = -1;
	const NO_SELECT_OPT = -2;
	
	function __construct() {
		$this->init();
	}
	
	private function init() {
		
		try {
			$this->app_name = ((variable_get(SABRE_APP_CODE, SABRE_APP_CODE_QMS) == SABRE_APP_CODE_QMS) ? 
													t(SABRE_APP_NAME_QMS) : t(SABRE_APP_NAME_SCHEDULER) );
			
			$cache_name = ST_YEAR_LIST;  // lang code -- default to English for now
			$list_cache = cache_get($cache_name);  
			if ( isset($list_cache->data) && ($list_cache->data <> '') ) {
				$this->list = $list_cache->data;
			}
			else {
					// the view month sent to the server is always in English, display name may change with i18n enabled
				$this->list = array(
					//'0' => '- ' . t('Select') . ' -',
					2012 => '2012',
					2013 => '2013',
					2014 => '2014',
					2015 => '2015',
					2016 => '2016',
					2017 => '2017',
					2018 => '2018',
					2019 => '2019',
					2020 => '2020',
					2021 => '2021',
					2022 => '2022',
					2023 => '2023',
					2024 => '2024',
					2025 => '2025',
					2026 => '2026',
					2027 => '2027',
					2028 => '2028',
					2029 => '2029',
					2030 => '2030',
				);
					
				// cache the list values for the next time
				cache_set($cache_name, $this->list, 'cache', 0);
			}
		}
		catch (Exception $e) {
			watchdog($this->app_name, 'YearList::init() ' . $e->getMessage(), array(), WATCHDOG_ERROR);
		} 
	}
	
	public function get($flag = 0) {
		if ($flag == self::NO_SELECT_OPT) {
			return $this->list;
		}
		return array('0' => '- ' . t('Select') . ' -') + $this->list;
	}
	
	public function getName($id) {
		if ( $id && isset($this->list[$id])  ) {
			return $this->list[$id];
		}
		return '';
	}
	
	public function reload() {
		_st_clear_cache(ST_YEAR_LIST); 
		
		unset($this->list);
		$this->init();
	}
	
} // end - class YearList
