<?php
/**
 * Smart Report Maker
 * Version 9.0.0
 * Author : Webuccino 
 * All copyrights are preserved to Webuccino
 * URL : http://mysqlreports.com/
 *
 */
if (! defined ( "DIRECTACESS" ))
	exit ( "No direct script access allowed" );
class Template {
	public $path; //full path to the templates directory
	public $title;
	public $dir_name; //directory name of the template

	
	public function __construct($template_path,$title){
		$this->path = $template_path;
		$this->dir_name = basename($this->path);		
		$this->title = ( $title != "") ? $title : "Untitled";		
	}
	
	public function load() {
		
		defined("DIRECTACESS") or define("DIRECTACESS",1);
		require_once("config-init.php");
		require ($this->path."config.php");
		$_SESSION[Session_report_Settings_key]["datasource"] = $datasource;
		$_SESSION[Session_report_Settings_key]["table"] = $table;
		$_SESSION[Session_report_Settings_key]["tables_filters"] = $tables_filters;
			
		
			foreach ( $fields as $k => $field ) {
				if (strstr ( $field, "(" )) {
					$realValue = substr ( substr ( $field, strpos ( $field, '(`' ) + 2 ), 0, strpos ( substr ( $field, strpos ( $field, '(`' ) + 2 ), '`)' ) );
					$_SESSION[Session_report_Settings_key]["affected_column"] = $affected_column;
					$_SESSION[Session_report_Settings_key]["function"] = $function;
					$_SESSION[Session_report_Settings_key]["groupby_column"] = $groupby_column;
					$field = $realValue;
					$fields [$k] = $field;
				}
			}
			$_SESSION[Session_report_Settings_key]["fields"] = $fields;
		
		$_SESSION[Session_report_Settings_key]["relationships"] = $relationships;
		$_SESSION[Session_report_Settings_key]["sql"] = $sql;
		$_SESSION[Session_report_Settings_key]["fields2"] = $fields2;
		$_SESSION[Session_report_Settings_key]["labels"] =  $labels ;
			
		$_SESSION[Session_report_Settings_key]["group_by"] = $group_by;
		$_SESSION[Session_report_Settings_key]["sort_by"] = $sort_by;
		$_SESSION[Session_report_Settings_key]["records_per_page"] = $records_per_page;
		$_SESSION[Session_report_Settings_key]["layout"] = $layout;
		$_SESSION[Session_report_Settings_key]["style_name"] = $style_name;
			
		$_SESSION[Session_report_Settings_key]["title"] = $title;
		$_SESSION[Session_report_Settings_key]["header"] = $header;
		$_SESSION[Session_report_Settings_key]["footer"] = $footer;
		$_SESSION[Session_report_Settings_key]["category"] =$category;
		$_SESSION[Session_report_Settings_key]["chkSearch"] = $chkSearch;
		
                $_SESSION[Session_report_Settings_key]["affected_column"] = $affected_column;
			
		
		
		
		
		$_SESSION[Session_report_Settings_key]["is_mobile"] = $is_mobile;
		$_SESSION[Session_report_Settings_key]["images_path"] = $images_path;
		$_SESSION[Session_report_Settings_key]["thumnail_max_width"] = $thumnail_max_width;
		$_SESSION[Session_report_Settings_key]["thumnail_max_height"] = $thumnail_max_height;
		$_SESSION[Session_report_Settings_key]["show_real_size_image"] = $show_real_size_image;
		$_SESSION[Session_report_Settings_key]["show_realsize_in_popup"] = $show_realsize_in_popup;
		$_SESSION[Session_report_Settings_key]["language"] = $language;
                if(!empty($cells)){
		$_SESSION[Session_report_Settings_key]["cells"] = $this->reverse_cells ( $cells );
                }
		$_SESSION[Session_report_Settings_key]["conditional_formating"] = $conditional_formating;
			
		$_SESSION[Session_wizard_key]["page_key"] = 'step_6';
		$_SESSION[Session_wizard_key]["active_pages"] = array (
				'step_5_1',
				'step_6'
		);
		$_SESSION['srp_subtotals_enabled']=$sub_totals_enabled;
		$_SESSION[Session_report_Settings_key]['sub_totals'] = $sub_totals;
		$_SESSION['srp_filters_grouping'] = $filters_grouping;
		

		return true;
	}
	
	//prepare the cells array to be loaded in the session . 
	private function reverse_cells($cells) {
		$arr = array (
				array () 
		);
		$index = 0;
		foreach ( $cells as $k => $v ) {
			$arr [$index] ["column"] = $k;
			if (strstr ( $v, "append" )) {
				$temp = explode ( "-", $v );
				$arr [$index] ["appendedText"] = $temp [2];
				$arr [$index] ["cellType"] = $temp [0] . "-" . $temp [1];
			} else {
				$arr [$index] ["cellType"] = $v;
				$arr [$index] ["appendedText"] = "";
			}
			$index ++;
		}
		return $arr;
	}
}