<?php

//global $Debug;  $Debug = true;
class NewsAdminView extends Slot {
	var $SectionID = 49;
	var $ModuleID = 2;
	var $reqfield = '<img src="/i/must.gif">&nbsp;';
	var $company = array();
	var $newstype = array();
	var $upload_formats = array(
		'image/jpeg' => 'jpg',
		'image/pjpeg' => 'jpg',
		'image/gif' => 'gif',
		'image/png' => 'gif',
	);
	var $upload_formats_check = array(
		'image/jpeg',
		'image/pjpeg',
		'image/gif',
		'image/png',
	);
	var $upload_settings = array(
		'prew_width' => '',
		'prew_height' => '',
		'big_width' => '118',
		'big_height' => '87',
		'middle_width' => '',
		'middle_height' => '',
		'small_width' => '',
		'small_height' => '',
		'3d_width' => '',
		'3d_height' => '',
		'image_width' => '',
		'image_height' => '',
	);

	function __construct($pquery) {
		parent::__construct($pquery);
	}

	function init() { 
		switch(@$this->LocalTarget[0]) {
			case 'Search': 
				$request = array('pageRecordsQty'=>'20','curPageNumber'=> !empty($this->LocalTarget[1])? $this->LocalTarget[1]+1 : 1);
				if(!empty($this->Request)) {
					header("Location: ".$this->Home.'/Registar/NewsAdmin/Search/'.$this->getRequestFormField('company').'/'.$this->getRequestFormField('type').$this->SID);
				} else if(isset($this->LocalTarget[1])) {
						if(!empty($this->LocalTarget[1])) $search['company'] = $this->LocalTarget[1];
						if(!empty($this->LocalTarget[2])) $search['type'] = $this->LocalTarget[2];
						$request = array('conditions' => $search,'pageRecordsQty'=>'20','curPageNumber'=>(isset($this->LocalTarget[3])? $this->LocalTarget[3]+1 : 1));
						$this->Action->processEvent($this->SectionID,'select_stories',$request,$this);
						$this->getPageControl($this->Home.'/Registar/NewsAdmin/Search/'.$this->LocalTarget[1].'/'.$this->LocalTarget[2]);
					} else header("Location: ".$this->Home.'/Registar/NewsAdmin'.$this->SID);
			break;
			case 'Edit': case 'Delete':
				if(!empty($this->LocalTarget[1]) && validate(intval($this->LocalTarget[1]))) {
					$this->Action->processEvent($this->SectionID,'open_story',array('id' => $this->LocalTarget[1]),$this);
					if(!empty($this->PreparedTemplates['_error'])) header("Location: ".$this->Home.'/Registar/NewsAdmin'.$this->SID);
					else $this->Scheme = 'action';
				}
				else header("Location: ".$this->Home.'/Registar/NewsAdmin'.$this->SID);
			break;
			case 'Add': $this->Scheme = 'action'; break;
			default:                
				$request = array('conditions' => array(),'pageRecordsQty' => '20', 'sortColumn'=>'date' , 'sortType' =>'DESC');
				$request['curPageNumber'] = empty($this->LocalTarget[0]) || !validate(intval($this->LocalTarget[0]))? 1 : intval($this->LocalTarget[0]+1);
				$this->Action->processEvent($this->SectionID,'select_stories',$request,$this);
				$this->getPageControl($this->Home.'/Registar/NewsAdmin');
			break;
		}
		if(!empty($this->Request)) {
			switch($this->getRequestField('step')) {
				case 'Add': case 'Edit':
					$this->validateForm()
					and $this->Action->processEvent($this->SectionID,'save_news',$this->getRequestField('form'),$this)
					and header("Location: ".$this->Home.'/Registar/NewsAdmin'.$this->SID);
				break;
				case 'unlock':
	 					header("Location: ".$this->Home.'/Registar/NewsAdmin'.$this->SID);
				break;				
				case 'Delete':
					$this->validateForm()
					and $this->Action->processEvent($this->SectionID,'delete_story',$this->getRequestField('form'),$this)
					and header("Location: ".$this->Home.'/Registar/NewsAdmin'.$this->SID);
				break;
			}
		}
		$this->setSchemeTemplate($this->Scheme);
	}

	function saveFiles($array) { 
		$path = $this->ImagePath.'/news/'.$array['id'];
		if($this->getRequestField('step')=='add' || !file_exists($path)) {
			umask(0002); mkdir($path,0775);
		}
		
		if(!file_exists($path.'/images')) {
			umask(0002); mkdir($path.'/images',0775);
		}
		$this->saveImages($array,$path,'images','image_big',3,'jpg');
	}
	
	function saveImages($object, $path, $folder, $array, $type, $ext) {
		if(!empty($this->Request[$array])) {
			$count = $max = 0;
			$images = array();
			if(!empty($object['_files'])) {
				foreach($object['_files'] as $value)
					if($value['filetype']==$type) $count+=1;
			}
			for($i = 1; $i <= $count; $i++) {
				if(isset($this->Request[$array]['name'][$i])) {
					if(!empty($this->Request[$array]['tmp_name'][$i])) {
						copy($this->Request[$array]['tmp_name'][$i],$path.'/'.$folder.'/'.date2pg($i).'.'.$ext);
						chmod($path.'/'.$folder.'/'.date2pg($i).'.'.$ext,0664);
					}
					$images[] = date2pg($i);
				}
				else if(file_exists($path.'/'.$folder.'/'.date2pg($i).'.'.$ext)) {
					unlink($path.'/'.$folder.'/'.date2pg($i).'.'.$ext);
				}
			}
			$count = $max = count($images);
			for($i = 0; $i < $count; $i++)
				if(file_exists($path.'/'.$folder.'/'.$images[$i].'.'.$ext))
					rename($path.'/'.$folder.'/'.$images[$i].'.'.$ext,$path.'/'.$folder.'/'.date2pg($i+1).'.'.$ext);
			foreach($this->Request[$array]['name'] as $key => $value) {
				if($key > $count) {
					if(!empty($this->Request[$array]['tmp_name'][$key])) {
						$max+=1;
						copy($this->Request[$array]['tmp_name'][$key],$path.'/'.$folder.'/'.date2pg($max).'.'.$ext);
						chmod($path.'/'.$folder.'/'.date2pg($max).'.'.$ext,0664);
					}
				}
			}
		}
		else {
			chdir($path.'/'.$folder);
			$dir = dir('.');
			$dir->rewind();
			while($file = $dir->read()) if(is_file($file)) unlink($path.'/'.$folder.'/'.$file);
		}
	}
		
	function validateForm() {
		switch($this->getRequestField('step')) {
			case 'Add': case 'Edit':
				if(!is_FullText(stripslashes($this->Request['form']['story_langs']['0']['title']),4,255)) $this->errors['title'] = 'The news title you entered is not a valid string';
				if(!validate($this->getRequestFormField('company'))) $this->errors['company'] = 'Please select company';
				//----------it is upcoming event
				if ($this->Request['form']['type'] == 2){	// it is upcoming event
					if(!is_FullText(stripslashes($this->Request['form']['place']),3,255)) $this->errors['place'] = 'The place you entered is not a valid string';
					if(!is_FullText(stripslashes($this->Request['form']['city']),2,255)) $this->errors['city'] = 'The city you entered is not a valid string';
					$date = DateTime::createFromFormat('Y-m-d', $this->Request['form']['date']);
					$dateto= DateTime::createFromFormat('Y-m-d', $this->Request['form']['dateto']);
					if ( $date > $dateto )  $this->errors['date'] = 'The date cannot be more than date to ';
				} else {					// its news
					$this->Request['form']['place'] = "";
					$this->Request['form']['city'] = "";
					$this->Request['form']['dateto']  = Null;
					
					if(!is_AlphaNumDefis(stripslashes($this->Request['form']['url_name']),1,1024)) $this->errors['url_name'] = 'The news URL name is not valid (do not use slashes)';
						else $this->Request['form']['url_name']  = strtolower($this->Request['form']['url_name']);				
					$number_image = !empty($this->Request['image_big'])? array_max(array_keys($this->Request['image_big']['name'])) : 0;
					for($i = 1; $i <= $number_image; $i++) {
						if(!empty($this->Request['image_big']['name'][$i])) {
							if(!$this->Request['image_big']['size'][$i]) $this->errors['image_big'][$i] = 'Error uploading - big image is empty';
							else {
								$size = GetImageSize($this->Request['image_big']['tmp_name'][$i]);
										$this->Request['form']['image'][$i]['image_type'] = 'jpg';
										$this->Request['form']['image'][$i]['_tmp_name'] = $this->Request['image_big']['tmp_name'][$i];
										$this->Request['form']['image'][$i]['_number'] = $i;
							}
						} else if(isset($this->Request['image_big']['name'][$i])) {
								$this->Request['form']['image'][$i]['_number'] = $i;
						  	   }
					}
					if (isset($this->Request['form']['links'])) {
						$this->Request['form']['links'] = array_values($this->Request['form']['links']);	// order keys
						foreach($this->Request['form']['links'] as $key => $value) {
							if(!checkURL($value['link']) && !isWebPath($value['link'])) {
								$this->errors['link'] = 'One of the link you entered is not valid or empty';
								break;
							}
						}
					}
				}
			break;
		}
		if(count($this->errors) > 0) return FALSE;
		return TRUE;
	}

	function fillElements() {
		switch($this->Scheme) {
			case 'default': $this->fillList(); break;
			case 'action': $this->fillAction(); break;
		}
	}
	
	function fillList() { 
			foreach($this->PreparedTemplates['curPage'] as $key => $value) {
				$date = new DateTime($value['date']); 
				$HeaderDate = $date->format('F j, Y'); 
				$this->TempFillEls['news'][] = array(
					'title' => $value['story_langs']['0']['title'],
					'description' => $value['story_langs']['0']['short_text'],
					'id' => $value['id'],
					'date' => $HeaderDate,
					'sort_order' => $value['sort_order'],
					'type' => $value['type'] =='1'?"News":"Upcoming Event",
				);
			}
		$this->company = mergeArrays(array('0' => ' All companies'),$this->Db->SelectGuide(array('company g'),array('g.id', 'g.name'),array('1' => '1')));
		$this->newstype = mergeArrays(array('0' => ' All types'),$this->Db->SelectGuide(array('newstype g'),array('g.id', 'g.name'),array('1' => '1')));
		$this->TempFillEls['company'] = inputSelect('form[company]',$this->company,(!empty($this->LocalTarget[1])? $this->LocalTarget[1] : 0));			
		$this->TempFillEls['type'] = inputSelect('form[type]',$this->newstype,(!empty($this->LocalTarget[2])? $this->LocalTarget[2] : 0));
	}

	function fillAction() {
		$this->company = mergeArrays(array('0' => ' --- Select Company --- '), $this->Action->Server->getGuide('company')  );
		$this->newstype = $this->Action->Server->getGuide('newstype');
		switch($this->LocalTarget[0]) {
			case 'Add': {
					$this->TempFillEls['reqfield'] = $this->reqfield;
					$this->TempFillEls['sub_action'] = 'Add';
					$this->TempFillEls['id'] = '';
					$this->TempFillEls['title'] = inputTextArea('form[story_langs][0][title]',(!empty($this->Request)? stripslashes($this->Request['form']['story_langs']['0']['title']) : ''),$this->correctSize('65'),2);
					$this->TempFillEls['short_desc'] = inputTextArea('form[story_langs][0][short_text]',(!empty($this->Request)? stripslashes($this->Request['form']['story_langs']['0']['short_text']) : ''),$this->correctSize('65'),6);
					//$this->TempFillEls['full_desc'] = inputTextArea('form[story_langs][0][text]',(!empty($this->Request)? stripslashes($this->Request['form']['story_langs']['0']['text']) : ''),$this->correctSize('65'),18);
					//$this->TempFillEls['full_desc_name'] = 'form[story_langs][0][text]';
					$this->TempFillEls['image'] = inputFile('image');
					$this->TempFillEls['url_name'] = inputText('form[url_name]',(!empty($this->Request)? stripslashes($this->Request['form']['url_name']) : ''),$this->correctSize('67'));
					$this->TempFillEls['place'] = inputText('form[place]',(!empty($this->Request)? $this->Request['form']['place'] : ''),$this->correctSize('67'));
					$this->TempFillEls['city'] = inputText('form[city]',(!empty($this->Request)? $this->Request['form']['city'] : ''),$this->correctSize('67'));
					$this->TempFillEls['date'] =  inputText('form[date]',     !empty($this->Request['form']['date'])?    $this->Request['form']['date']  :   date ('Y-m-d')   , 19, ' style="width: 130px;" readonly', 19);
					$this->TempFillEls['dateto'] =  inputText('form[dateto]', !empty($this->Request['form']['dateto'])?  $this->Request['form']['dateto']  : date ('Y-m-d')   , 19, ' style="width: 130px;" readonly', 19);
					$this->TempFillEls['image_buttons'] = array(array());
					$this->TempFillEls['link_buttons'] = array(array());
					$this->TempFillEls['company'] = inputSelect('form[company]',$this->company, !empty($this->Request)?$this->Request['form']['company']:1  );
					$this->TempFillEls['newstype'] = inputSelect('form[type]',$this->newstype,  !empty($this->Request)?$this->Request['form']['type']:1  ,  'onchange="controlType();" id="newstype" ' );
					$this->TempFillEls['type'] = !empty($this->Request['form']['type'])? ( $this->Request['form']['type']==1?"News":"Upcoming Event"): "News/Upcoming Event";
					$this->TempFillEls['imagesvisibility'] =  !empty($this->Request['form']['type'])? ($this->Request['form']['type']==1?"table-row":"none"  ):'table-row';
					$this->TempFillEls['eventvisibility'] = !empty($this->Request['form']['type'])? ($this->Request['form']['type']==2?"table-row":"none"  ):'none';
					if(!empty($this->PreparedTemplates)) {
						$this->TempFillEls['id'] = $this->PreparedTemplates['id'];
						$this->TempFillEls['title'] = inputTextArea('form[story_langs][0][title]',(!empty($this->Request)? stripslashes($this->Request['form']['story_langs']['0']['title']) : $this->PreparedTemplates['story_langs']['0']['title']),$this->correctSize('65'),2);
						$this->TempFillEls['short_desc'] =inputTextArea('form[story_langs][0][short_text]',(!empty($this->Request)? stripslashes($this->Request['form']['story_langs']['0']['short_text']) : $this->PreparedTemplates['story_langs']['0']['short_text']),$this->correctSize('65'),6);
						//$this->TempFillEls['full_desc'] =inputTextArea('form[story_langs][0][text]',(!empty($this->Request)? stripslashes($this->Request['form']['story_langs']['0']['text']) : $this->PreparedTemplates['story_langs']['0']['text']),$this->correctSize('65'),18);
						$this->TempFillEls['url_name'] = inputText('form[url_name]',(!empty($this->Request)? stripslashes($this->Request['form']['url_name']) : ucwords($this->PreparedTemplates['url_name'])),$this->correctSize('65'));			
						$this->TempFillEls['place'] = inputText('form[place]',(!empty($this->Request)? $this->Request['form']['place'] : $this->PreparedTemplates['place']),$this->correctSize('65'));
						$this->TempFillEls['city'] = inputText('form[city]',(!empty($this->Request)? $this->Request['form']['city'] : $this->PreparedTemplates['city']),$this->correctSize('65'));
						$this->TempFillEls['date'] =  inputText('form[date]',     !empty($this->PreparedTemplates['date'])?    $this->PreparedTemplates['date']  :   date ('Y-m-d')   , 19, ' style="width: 130px;" readonly', 19);
						$this->TempFillEls['dateto'] =  inputText('form[dateto]', !empty($this->PreparedTemplates['dateto'])?  $this->PreparedTemplates['dateto']:   date ('Y-m-d')   , 19, ' style="width: 130px;" readonly', 19);
					}
					$number_image = !empty($this->Request['image_big']) && count($this->Request['image_big']['name']) > 1? count($this->Request['image_big']['name']) : 1;
					for($i = 1; $i <= $number_image; $i++) {
						$this->TempFillEls['images'][$i] = array(
							'num' => $i,
							'big_setting' => 'Jpg, '.$this->upload_settings['big_width'].'x'.$this->upload_settings['big_height'],
							'big_image' => '&nbsp;',
							'image_big_upload' => inputFile('image_big['.$i.']'),
							'image_big_error' => !empty($this->errors['image_big'][$i])? $this->printError($this->errors['image_big'][$i]) : '',
							'delete_image' => array(array('num' => $i)),
						);
					}
					$number_link = !empty($this->Request['form']['links']) && count($this->Request['form']['links']) > 0? count($this->Request['form']['links']) : 0;
					for($i = 0; $i < $number_link; $i++) {
						$this->TempFillEls['links'][$i] = array(
							'num' => $i+1,
							'title' => inputText('form[links]['.$i.'][title]',(!empty($this->Request)? $this->Request['form']['links'][$i]['title'] : ''),$this->correctSize('55')),
							'link' => inputText('form[links]['.$i.'][link]',(!empty($this->Request)? $this->Request['form']['links'][$i]['link'] : ''),$this->correctSize('55')),
							'delete_link' => array(array('num' => $i)),
						);
					}
			break;
			}
			case 'Edit': {
					$this->PreparedTemplates = $this->Action->getTemplate($this->SectionID,'open_story');
					$path = '/news/'.$this->PreparedTemplates['id'];
					$files = array('prew' => array(),'big' => array(),'image' => array());
					foreach($this->PreparedTemplates['files'] as $key => $value) {
						switch($value['filetype']) {
							case 2: $files['big'] = array('image_link' => $value['image_link'],'name' => 'big.'.$value['imagetype']); break;
							case 3: $files['image'][$value['number']] = array('name' => date2pg($value['number']),'ext' => $value['imagetype']); break;
						}
					}
					$this->TempFillEls['reqfield'] = $this->reqfield;
					$this->TempFillEls['sub_action'] = 'Update';
					$this->TempFillEls['id'] = '';
					$this->TempFillEls['title'] = inputTextArea('form[story_langs][0][title]',(!empty($this->Request)? stripslashes($this->Request['form']['story_langs']['0']['title']) : ''),$this->correctSize('65'),2);
					$this->TempFillEls['short_desc'] = inputTextArea('form[story_langs][0][short_text]',(!empty($this->Request)? stripslashes($this->Request['form']['story_langs']['0']['short_text']) : ''),$this->correctSize('65'),6);
					$this->TempFillEls['image'] = inputFile('image');
					$this->TempFillEls['url_name'] = inputText('form[url_name]',(!empty($this->Request)? stripslashes($this->Request['form']['url_name']) : ''),$this->correctSize('67'));
					$this->TempFillEls['place'] = inputText('form[place]',(!empty($this->Request)? $this->Request['form']['place'] : ''),$this->correctSize('67'));
					$this->TempFillEls['city'] = inputText('form[city]',(!empty($this->Request)? $this->Request['form']['city'] : ''),$this->correctSize('67'));
					$this->TempFillEls['date'] =  inputText('form[date]',     !empty($this->Request['form']['date'])?    $this->Request['form']['date']  :   date ('Y-m-d')   , 19, ' style="width: 130px;" readonly', 19);
					$this->TempFillEls['dateto'] =  inputText('form[dateto]',     !empty($this->Request['form']['dateto'])?    $this->Request['form']['dateto']  :   date ('Y-m-d')   , 19, ' style="width: 130px;" readonly', 19);
					$this->TempFillEls['image_buttons'] = array(array());
					$this->TempFillEls['link_buttons'] = array(array());
					$this->TempFillEls['company'] = inputSelect('form[company]',$this->company,(!empty($this->Request)? $this->Request['form']['company'] : $this->PreparedTemplates['company']));
					$this->TempFillEls['newstype'] = $this->newstype[$this->PreparedTemplates['type']].inputHidden('form[type]', $this->PreparedTemplates['type']);
					$this->TempFillEls['type'] = $this->newstype[$this->PreparedTemplates['type']];
					$this->TempFillEls['imagesvisibility'] =  $this->PreparedTemplates['type']==1?'table-row':"none";
					$this->TempFillEls['eventvisibility'] =  $this->PreparedTemplates['type']==2?'table-row':"none";					
					if(!empty($this->PreparedTemplates)) {
						$this->TempFillEls['id'] = $this->PreparedTemplates['id'];
						$this->TempFillEls['title'] = inputTextArea('form[story_langs][0][title]',(!empty($this->Request)? stripslashes($this->Request['form']['story_langs']['0']['title']) : $this->PreparedTemplates['story_langs']['0']['title']),$this->correctSize('65'),2);
						$this->TempFillEls['short_desc'] =inputTextArea('form[story_langs][0][short_text]',(!empty($this->Request)? stripslashes($this->Request['form']['story_langs']['0']['short_text']) : $this->PreparedTemplates['story_langs']['0']['short_text']),$this->correctSize('65'),6);
						//$this->TempFillEls['full_desc'] =inputTextArea('form[story_langs][0][text]',(!empty($this->Request)? stripslashes($this->Request['form']['story_langs']['0']['text']) : $this->PreparedTemplates['story_langs']['0']['text']),$this->correctSize('65'),18);
						$this->TempFillEls['url_name'] = inputText('form[url_name]',(!empty($this->Request)? stripslashes($this->Request['form']['url_name']) : ucwords($this->PreparedTemplates['url_name'])),$this->correctSize('65'));			
						$this->TempFillEls['place'] = inputText('form[place]',(!empty($this->Request)? $this->Request['form']['place'] : $this->PreparedTemplates['place']),$this->correctSize('65'));
						$this->TempFillEls['city'] = inputText('form[city]',(!empty($this->Request)? $this->Request['form']['city'] : $this->PreparedTemplates['city']),$this->correctSize('65'));
						$this->TempFillEls['date'] =  inputText('form[date]',     !empty($this->PreparedTemplates['date'])?    $this->PreparedTemplates['date']  :   date ('Y-m-d')   , 19, ' style="width: 130px;" readonly', 19);
						$this->TempFillEls['dateto'] =  inputText('form[dateto]',     !empty($this->PreparedTemplates['dateto'])?    $this->PreparedTemplates['dateto']  :   date ('Y-m-d')   , 19, ' style="width: 130px;" readonly', 19);
					}
					
					$number_image = !empty($this->Request['image_big']) && count($this->Request['image_big']['name']) > 1? count($this->Request['image_big']['name']) : (!empty($files['image']) && count($files['image']) > 1? count($files['image']) : 1);
					for($i = 1; $i <= $number_image; $i++) {
						$this->TempFillEls['images'][$i] = array(
							'num' => $i,
							'big_setting' => 'Jpg, '.$this->upload_settings['big_width'].'x'.$this->upload_settings['big_height'],
							'big_image' => !empty($files['image'][$i])? (file_exists($this->ImagePath.$path.'/images/'.$files['image'][$i]['name'].'.jpg')? makeImage($this->ImagePath.$path.'/images/'.$files['image'][$i]['name'].'.jpg', $this->ImageWPath.$path.'/images/'.$files['image'][$i]['name'].'.jpg'.'?'.time(),'','',array(),0, array('0' => '280', '1' => '100' ) ,'big_image'.$i.'img') : '&nbsp;') : '&nbsp;',
							'image_big_upload' => inputFile('image_big['.$i.']'),
							'image_big_error' => !empty($this->errors['image_big'][$i])? $this->printError($this->errors['image_big'][$i]) : '',
							'delete_image' => array(array('num' => $i)),
						);
					}
					$number_link = !empty($this->Request['form']['links']) && count($this->Request['form']['links']) > 0? count($this->Request['form']['links']) : count($this->PreparedTemplates['links']);
					for($i = 0; $i < $number_link; $i++) {
						$this->TempFillEls['links'][$i] = array(
							'num' => $i+1,
							'title' => inputText('form[links]['.$i.'][title]',(!empty($this->Request)? $this->Request['form']['links'][$i]['title'] : $this->PreparedTemplates['links'][$i]['title']),$this->correctSize('55')),
							'link' => inputText('form[links]['.$i.'][link]',(!empty($this->Request)? $this->Request['form']['links'][$i]['link'] : $this->PreparedTemplates['links'][$i]['link']),$this->correctSize('55')),
							'delete_link' => array(array('num' => $i)),
						);
					}
			break;
			}
			case 'Delete': {
					$this->PreparedTemplates = $this->Action->getTemplate($this->SectionID,'open_story');
					$path = '/news/'.$this->PreparedTemplates['id'];
					$files = array('prew' => array(),'big' => array(),'image' => array(),'middle' => array());
					foreach($this->PreparedTemplates['files'] as $key => $value) {
						switch($value['filetype']) {
							case 1: $files['prew'] = array('image_link' => $value['image_link'],'name' => 'prew.'.$value['imagetype']); break;
							case 2: $files['big'] = array('image_link' => $value['image_link'],'name' => 'big.'.$value['imagetype']); break;
						}
					}
					$this->TempFillEls['reqfield'] = $this->reqfield;
					$this->TempFillEls['sub_action'] = 'Delete';
					//$this->TempFillEls['full_desc_name'] = 'form[story_langs][0][text]';
					$this->TempFillEls['image'] = '---';
					$this->TempFillEls['image_buttons'] = array();
					$this->TempFillEls['link_buttons'] = array();
					$this->TempFillEls['company'] = $this->company[$this->PreparedTemplates['company']['id']];
					$this->TempFillEls['newstype'] = $this->newstype[$this->PreparedTemplates['type']['id']];
					$this->TempFillEls['type'] = $this->newstype[$this->PreparedTemplates['type']['id']];
					$this->TempFillEls['imagesvisibility'] = $this->PreparedTemplates['type']['id']==1?'table-row':"none";
					$this->TempFillEls['eventvisibility'] =  $this->PreparedTemplates['type']['id']==2?'table-row':"none";
					if(!empty($this->PreparedTemplates)) {
						$this->TempFillEls['id'] = $this->PreparedTemplates['id'];
						$this->TempFillEls['title'] = $this->PreparedTemplates['story_langs']['0']['title'];
						$this->TempFillEls['short_desc'] = $this->PreparedTemplates['story_langs']['0']['short_text'];
						//$this->TempFillEls['full_desc'] = $this->PreparedTemplates['story_langs']['0']['text'];
						$this->TempFillEls['url_name'] = ucwords($this->PreparedTemplates['url_name']);			
						$this->TempFillEls['place'] = $this->PreparedTemplates['place'];
						$this->TempFillEls['city'] = $this->PreparedTemplates['city'];
						$this->TempFillEls['date'] =  $this->PreparedTemplates['date'];
						$this->TempFillEls['dateto'] =$this->PreparedTemplates['dateto'];
					}
					$number_image = !empty($files['image']) && count($files['image']) > 1? count($files['image']) : 1;
					for($i = 1; $i <= $number_image; $i++) {
						$this->TempFillEls['images'][$i] = array(
							'num' => $i,
							'big_setting' => 'Jpg, '.$this->upload_settings['big_width'].'x'.$this->upload_settings['big_height'],
							'big_image' => !empty($files['image'][$i][1])? (file_exists($this->ImagePath.$path.'/images/'.$files['image'][$i]['name'].'.jpg')? makeImage($this->ImagePath.$path.'/images/'.$files['image'][$i]['name'].'.jpg',$this->ImageWPath.$path.'/images/'.$files['image'][$i]['name'].'.jpg'.'?'.time(),'','',array(),0,'','big_image'.$i.'img') : ' --- Empty --- ') : ' --- Empty --- ',
						);
					}
					$number_link = !empty($this->PreparedTemplates['links']) && count($this->PreparedTemplates['links']) > 0? count($this->PreparedTemplates['links']) : 0;
					for($i = 0; $i < $number_link; $i++) {
						$this->TempFillEls['links'][$i] = array(
							'num' => $i+1,
							'title' => $this->PreparedTemplates['links'][$i]['title'],
							'link' => $this->PreparedTemplates['links'][$i]['link'],
						);
					}
			break;
			}
		}
		$this->TempFillEls['new_param_id_image'] = $number_image;
		$this->TempFillEls['new_param_id_link'] = $number_link;
		
		$this->TempFillEls['action'] = $this->LocalTarget[0];
		$this->PreparedTemplates = $this->Action->getTemplate($this->SectionID,'save_news');
		!empty($this->PreparedTemplates['_error']) and $this->errors['model'] = $this->globalLangContent['error_model'][$this->PreparedTemplates['_error']];
		if(count($this->errors) > 0) $this->errors['header'] = 'There were some errors.<br>Please see below for details.';
		foreach($this->errors as $k => $v) $this->TempFillEls[$k.'_error'] = $k=='header'? '<strong>'.$v.'</strong><p>' : $this->printError($v);
		$this->PreparedTemplates = $this->Action->clearTemplate($this->SectionID,'save_news');
		
	}	
	
}
?>