<?php
class Calculate {
	var $data = array();
  
  	function __construct(&$locator) {
		$this->database =& $locator->get('database');
	
    	$results = $this->database->getRows("select * from extension where type = 'calculate'");
	
		foreach ($results as $result) {
			$file  = DIR_EXTENSION . $result['directory'] . '/' . $result['filename'];
			$class = 'Calculate' . str_replace('_', '', $result['code']);
			
			if (file_exists($file)) {
				require_once($file);

				$this->data[$result['code']] = new $class($locator);
			}
		}
  	}
	
  	function getTotals() {
		$sort_order = array();
		
		foreach (array_keys($this->data) as $key) {
			$sort_order[$this->data[$key]->getSortOrder()] = $key;
		}
		
		ksort($sort_order);
		
		$total_data = array();
		
		$i = 0;
		
		foreach ($sort_order as $key) {
	  		$results = $this->data[$key]->calculate();
	  	  	
	  		foreach ($results as $result) {
	    		$total_data[] = array(
		  			'title'      => $result['title'],
		  			'text'       => $result['text'],
		  			'value'      => $result['value'],
					'sort_order' => $i
	    		); 
				
				$i++;
	  		}
		}
		
		return $total_data;
  	}
}
?>