<?php
class GeneralsettingsController extends AppController {

	var $name = 'Generalsettings';

	var $components = array('FileUpload');
	
	function admin_index() {
		$this->Generalsetting->recursive = 0;
		$this->set('generalsettings', $this->Generalsetting->find('first'));
	}

	function admin_add() {

		if (!empty($this->request->data)) {
			
			if($this->request->data['Generalsetting']['logoimage']['name'] != null){				
				$target_path = WWW_ROOT."files".DS."logo";
				$target_path_for_database ="files"."/"."logo/";
				
				$filename = $this->request->data['Generalsetting']['logoimage']['name'];
				$tmpname  = $this->request->data['Generalsetting']['logoimage']['tmp_name'];
				$filesize = $this->request->data['Generalsetting']['logoimage']['size'];
				$filetype = $this->request->data['Generalsetting']['logoimage']['type'];							
				$logo = $this->FileUpload->upload($filename, $tmpname, $filesize, $target_path,'jpg,png,gif,jpeg');
				if(strlen($logo) > 0){
					$this->request->data['Generalsetting']['logo'] = $target_path_for_database.$logo;						
				}
				
			}//End
			
			if($this->request->data['Generalsetting']['boardimage']['name'] != null){				
				$target_path = WWW_ROOT."files".DS."logo";
				$target_path_for_database ="files"."/"."logo/";
				
				$filename = $this->request->data['Generalsetting']['boardimage']['name'];
				$tmpname  = $this->request->data['Generalsetting']['boardimage']['tmp_name'];
				$filesize = $this->request->data['Generalsetting']['boardimage']['size'];
				$filetype = $this->request->data['Generalsetting']['boardimage']['type'];							
				$blogo = $this->FileUpload->upload($filename, $tmpname, $filesize, $target_path,'jpg,png,gif,jpeg');
				if(strlen($blogo) > 0){
					$this->request->data['Generalsetting']['boardlogo'] = $target_path_for_database.$blogo;						
				}
				
			}//End
			
			$this->Generalsetting->create();
			if ($this->Generalsetting->save($this->request->data)) {
				$this->Session->setFlash(__('The generalsetting has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The generalsetting could not be saved. Please, try again.', true));
				$this->redirect(array('action'=>'add'));
			}
		}
	}

	function admin_edit($id = null) {
		
		if (!$id && empty($this->request->data)) {
			$this->Session->setFlash(__('Invalid generalsetting', true));
			$this->redirect(array('action' => 'index'));
		}
		
		if (!empty($this->request->data)) {
			
			//Check here the image array is empty or not
			if($this->request->data['Generalsetting']['logoimage']['name'] != null){				
				$target_path = WWW_ROOT."files".DS."logo";
				$target_path_for_database ="files"."/"."logo/";
				
				$filename = $this->request->data['Generalsetting']['logoimage']['name'];
				$tmpname  = $this->request->data['Generalsetting']['logoimage']['tmp_name'];
				$filesize = $this->request->data['Generalsetting']['logoimage']['size'];
				$filetype = $this->request->data['Generalsetting']['logoimage']['type'];							
				$logo = $this->FileUpload->upload($filename, $tmpname, $filesize, $target_path,'jpg,png,gif,jpeg');

				if(strlen($logo) > 0){
					if(!empty($this->request->data['Generalsetting']['logo'])){
						if(unlink($this->request->data['Generalsetting']['logo'])){
							$this->request->data['Generalsetting']['logo'] = $target_path_for_database.$logo;
						}
					}else{
					$this->request->data['Generalsetting']['logo'] = $target_path_for_database.$logo;
					}
				}
						
			}//End
			
			//Check here the image array is empty or not
			if($this->request->data['Generalsetting']['boardimage']['name'] != null){				
				$target_path = WWW_ROOT."files".DS."logo";
				$target_path_for_database ="files"."/"."logo/";
				
				$filename = $this->request->data['Generalsetting']['boardimage']['name'];
				$tmpname  = $this->request->data['Generalsetting']['boardimage']['tmp_name'];
				$filesize = $this->request->data['Generalsetting']['boardimage']['size'];
				$filetype = $this->request->data['Generalsetting']['boardimage']['type'];							
				$filename = $this->FileUpload->upload($filename, $tmpname, $filesize, $target_path,'jpg,png,gif,jpeg');
				if(strlen($filename) > 0){
					if(!empty($this->request->data['Generalsetting']['boardlogo'])){
						if(unlink($this->request->data['Generalsetting']['boardlogo'])){
							$this->request->data['Generalsetting']['boardlogo'] = $target_path_for_database.$filename;
						}
					}else{
						$this->request->data['Generalsetting']['boardlogo'] = $target_path_for_database.$filename;
					}
				}
						
			}//End
							
			if ($this->Generalsetting->save($this->request->data)) {
				$this->Session->setFlash(__('The generalsetting has been updated', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The generalsetting could not be saved. Please, try again.', true));
				$this->redirect(array('action'=>'edit',$this->request->data['Generalsetting']['id']));
			}
		}
		if (empty($this->request->data)) {
			$this->request->data = $this->Generalsetting->read(null, $id);
		}
	}

}
?>