<?php
class ControllerTmdformTmdformrecord extends Controller {
	private $error = array();

	public function index() {
		$this->load->language('tmdform/tmdformrecord');

		$this->document->setTitle($this->language->get('heading_title'));

		$this->load->model('tmdform/form');
		$this->load->model('tmdform/record');
		$this->load->model('localisation/language');
		$this->load->model('setting/store');

		$this->getForm();
	}

	public function getForm() {
		if (isset($this->request->get['fs_id'])) {
			$fs_id = $this->request->get['fs_id'];
		} else {
			$fs_id = '';
		}
		$data['heading_title'] = $this->language->get('heading_title');

		$data['text_form'] = !isset($this->request->get['category_id']) ? $this->language->get('text_add') : $this->language->get('text_edit');
		$data['text_detail'] = $this->language->get('text_detail');
		$data['text_custdetail'] = $this->language->get('text_custdetail');
		$data['text_fields'] = $this->language->get('text_fields');
		
		$data['entry_fieldname'] = $this->language->get('entry_fieldname');
		$data['entry_fieldvalue'] = $this->language->get('entry_fieldvalue');

		$data['button_cancel'] = $this->language->get('button_cancel');

		if (isset($this->error['warning'])) {
			$data['error_warning'] = $this->error['warning'];
		} else {
			$data['error_warning'] = '';
		}
		

		$url = '';

		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}

		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}

		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}

		$data['breadcrumbs'] = array();

		$data['breadcrumbs'][] = array(
			'text' => $this->language->get('text_home'),
			'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], true)
		);

		$data['breadcrumbs'][] = array(
			'text' => $this->language->get('heading_title'),
			'href' => $this->url->link('tmdform/tmdformrecord', 'token=' . $this->session->data['token'] . $url, true)
		);

		$data['cancel'] = $this->url->link('tmdform/record', 'token=' . $this->session->data['token'] . $url, true);
		
		$tmdrecord_info = $this->model_tmdform_record->getTmdRecord($this->request->get['fs_id']);
		//print_r($tmdrecord_info);die();
		$language_info = $this->model_localisation_language->getLanguage($tmdrecord_info['language_id']);
		if(isset($language_info['name'])){
			$lname = $language_info['name'];
		} else {
			$lname ='';
		}
		$store_info = $this->model_setting_store->getStore($tmdrecord_info['store_id']);
		if(isset($store_info['name'])){
			$sname = $store_info['name'];
		} else {
			$sname ='Default';
		}
		
		$data['sname'] 		= $sname;
		$data['lname'] 		= $lname;
		$data['customer_name']= $tmdrecord_info['customer_name'];
		$data['title'] 		= $tmdrecord_info['title'];
		$data['ip'] 		= $tmdrecord_info['ip'];
		$data['user_agent'] = (substr($tmdrecord_info['user_agent'], 0, 45));
		$data['date_added'] = $tmdrecord_info['date_added'];
		
		$data['field_infos'] = array();
		$filter_data=array(
			'fs_id'=>$this->request->get['fs_id']
		);
				
		$field_infos = $this->model_tmdform_record->getTmdFieldRecord($filter_data);
		
		foreach($field_infos as $fieldinfo){			
			if($fieldinfo['serialize']){
				$fieldinfos=unserialize($fieldinfo['value']);			
						
				$value='';
				foreach($fieldinfos as $field){
				
				
					$value .=$field.',';
				}
				$fieldinfo['value']=$value;
			}
			/* update code */
				$this->load->model('tool/upload');
				$type = $this->model_tmdform_form->getfieldtype($fieldinfo['field_id']);
				
				if($type=='file') {
				 $upload_info = $this->model_tool_upload->getUploadByCode($fieldinfo['value']);
					$fieldinfo['value']='<a style="color:#1e91cf" href="'.$this->url->link('tool/upload/download', 'token=' . $this->session->data['token'] . '&code=' . $upload_info['code'], true).'">'.$upload_info['name'].'</a>'; 
				}
			/* update code */
			
			$data['field_infos'][] = array(				
				'label'     =>$fieldinfo['label'],
				'value'     =>$fieldinfo['value']
				
			);
			
		}
		
		$data['header'] = $this->load->controller('common/header');
		$data['column_left'] = $this->load->controller('common/column_left');
		$data['footer'] = $this->load->controller('common/footer');

		$this->response->setOutput($this->load->view('tmdform/tmdformrecord_form', $data));
	}
	

}
