<?php

App::uses('AppController', 'Controller');

class UserController extends AppController {

	/**
	 * This controller use User Model
	 *
	 * @var array
	 */
	 
	public $uses = array('DreamjobListGraducation', 'DreamjobWorker', 'DreamjobCompany', 'DreamjobPageText','DreamjobPageInh', 'DreamjobPageImage', 'DreamjobJobOpening', 'DreamjobCvEntry', 'DreamjobListCvCategory', 'DreamjobListGraducation', 'DreamjobListJob','DreamjobListKindofjob','DreamjobListBranch','Mannerofaddress','DreamjobISearch','User','AppUser','DreamjobListCity');
	public $components = array('MiconwareSession','RequestHandler');
	
	/**
	 * Displays a view
	 *
	 * @param mixed What page to display
	 * @return void
	 * @throws NotFoundException When the view file could not be found
	 *	or MissingViewException in debug mode.
	 */
	function beforeFilter(){
		$this->Security->unlockedActions[]="settings_isearch";
		$this->Security->unlockedActions[]="profil_edit";
		if($this->request->action=="settings_isearch")
			unset($this->request->data['_Token']['key']);
		parent::beforeFilter();
	}
	
	
	public function profil() {
		$this->MiconwareSession->init($this);
		
		$this->MiconwareSession->initWeb($this);
		$this->autoRender = true;
		$this->layout = 'default';
		$this->RequestHandler->setContent('html', 'application/html' );
		
		//self check
		$self_company = false;
		$self = $this->MiconwareSession->getWorker();
		if(!is_array($self) or count($self)<= 0){
			$self = $this->MiconwareSession->getCompany();
			$self_company = true;
		}
		
		//get Profil
		$hard_error = false;
		
		
		$company = false;
		$page = null;
		$profil = null;
		
		if(empty($this->request->params['id'])){
			$profil = $self;
			$company = $self_company;
		}else{
				$profil = $this->DreamjobWorker->find('first',array('conditions' => array('AppUser.id' => $this->request->params['id'])));
				if(!is_array($profil) or count($profil)<= 0){
					$profil = $this->DreamjobCompany->find('first',array('conditions' => array('AppUser.id' => $this->request->params['id'])));
					$company = true;
				}
				$this->set('id', $this->request->params['id']);
		}
		//get Page
		if(($this->MiconwareSession->isLoggedin()) or ($company) ){
			if(is_array($profil) or count($profil) > 0){
				if(!empty($this->request->params['page'])){
					$page = $this->DreamjobPageText->find('first',
							array('conditions' => array("DreamjobPageInh.id" => $this->request->params['page'],'DreamjobPageInh.user_id' => $profil['AppUser']['id']))
						);
					if(!is_array($page) or count($page)<= 0)
						$page = $this->DreamjobPageImage->find('first',
							array('conditions' => array("DreamjobPageInh.id" => $this->request->params['page'],'DreamjobPageInh.user_id' => $profil['AppUser']['id']))
						);
				}else{
					if(isset($profil['DreamjobWorker'])){
						$cv = $this->DreamjobCvEntry->find('all',array('conditions' => array('worker_id'=> $profil['AppUser']['id'])));
						if(!$self_company and $profil['AppUser']['id']==$self['AppUser']['id']){
							$cvp0 = $this->DreamjobListCvCategory->find('first',array('conditions' => array('DreamjobListCvCategory.position' => 0)));
							$this->set('cvp0', $cvp0);
							$this->set("cv",$cv);
						}
					}
				}
			}
		}else
			$hard_error = true;
		//error
		$this->set('edit', false);
		if(!isset( $profil['AppUser']['id']))
			$hard_error = true;
		if(!$company and $profil['AppUser']['id'] != $self['AppUser']['id'] or $hard_error){
			if(($company == $self_company) and !($this->MiconwareSession->isStaff()) or $hard_error){
				$hard_error = true;
				$this->MiconwareSession->setFlash(__('dreamjob.error.page.notFound'),'flash',array('alert'=>'danger'));	
				$this->set('error', 'dreamjob.error.noPermision');
				$this->render('/Home/error');
			}
		}elseif($profil['AppUser']['id'] == $self['AppUser']['id']){
			$this->set('edit', true);
			$this->set('MENU_PROFIL',true);
		}
		if(!empty($this->request->params['page']) and (!is_array($page) or count($page)<= 0))
			$this->MiconwareSession->setFlash(__('dreamjob.error.page.notFound'),'flash',array('alert'=>'danger'));
		//render
		$this->set('profil', $profil);
		$this->set('page',$page);
		if(!$hard_error)
			if($company){
				$con = $this->MiconwareSession->getOpeningCondition();
				$con['DreamjobJobOpening.company_id'] = $profil['AppUser']['id'];
				$openings = $this->DreamjobJobOpening->find('all',array('conditions' => $con));
				$this->set("openings",$openings);
				$this->render('/User/company');
			}else
				$this->render('/User/worker');
	}
	public function profil_edit(){
		$this->MiconwareSession->init($this);
		
		$this->MiconwareSession->initWeb($this);
		$this->RequestHandler->setContent('html', 'application/html' );
		$this->layout = 'default';
		
		$self = $this->MiconwareSession->getWorker();
		if(!empty($self) and count($self)> 0){
			if($this->request->is('post')){
				$tosave = array();
				if(isset($this->request->data['DreamjobCvEntry'])){
					$tosave = $this->request->data['DreamjobCvEntry'];
					foreach($tosave as $i => $a)
						$tosave[$i]['worker_id']=$self['AppUser']['id'];
				}
				$result = $this->DreamjobCvEntry->saveAndDelete($tosave,$self['DreamjobCvEntry']);
				if($result){
					$this->MiconwareSession->setFlash(__('dreamjob.settings.save'),'flash',array('alert'=>'success'));
				}else{
					$this->MiconwareSession->setFlash(__('dreamjob.settings.save.error'),'flash',array('alert'=>'danger'));
				}
			}
			$cvc = $this->DreamjobListCvCategory->find('all');
			$cve = $this->DreamjobCvEntry->find('all',array('conditions' => array('worker_id'=> $self['AppUser']['id'])));
			$cve2 = array('DreamjobCvEntry'=>null);
			foreach($cve as $i => $data)
				$cve2['DreamjobCvEntry'][$i] = $data['DreamjobCvEntry'];
			$this->request->data = $cve2;
			$this->set('cvc',$cvc);
			$this->set('cve',$cve);
			$this->set('profil',$self);
			$this->set('edit_show',true);
			$this->render('/User/worker');
		}else{
			$this->set('error', 'dreamjob.error.noPermision');
			$this->render('/Home/error');
		}
	}
	/*
	public function savePageDeleteJson(){
		$this->MiconwareSession->init($this);
		
		$this->MiconwareSession->initWeb($this);
		$this->RequestHandler->setContent('json', 'application/json' );
		
		$this->set('error', null);
		
		$profil = $this->MiconwareSession->getWorker();
		$company = false;
		if(empty($profil) or count($profil) <= 0){
			$profil = $this->MiconwareSession->getCompany();
			$company= true;
		}
		if(!empty($profil) and count($profil)> 0 and !empty($this->request->query['id'])){
			$data2 = array('DreamjobPageInh.id' => $this->request->query['id'],'DreamjobPageInh.user_id'=>$profil['AppUser']['id']);
			//$this->set('error', !$this->DreamjobPageInh->deleteAll($data2,false));
			$this->set('error', false);
		}else{
			$this->set('error', true);
		}
		$this->set('_serialize', array('error'));
	}*/
	public function savePageOrder(){
		$this->MiconwareSession->init($this);
		
		$this->MiconwareSession->initWeb($this);
		$this->autoRender = true;
		$this->layout = 'default';
		$this->RequestHandler->setContent('html', 'application/html' );
		
		$this->set('error', null);
		
		$profil = $this->MiconwareSession->getWorker();
		$company = false;
		if(empty($profil) or count($profil) <= 0){
			$profil = $this->MiconwareSession->getCompany();
			$company= true;
		}
		if(empty($profil) or count($profil) <= 0){
			$this->MiconwareSession->setFlash(__('dreamjob.error.page.notFound'),'flash',array('alert'=>'danger'));	
			$this->set('error', 'dreamjob.error.noPermision');
			$this->render('/Home/error');
		}else{
			if(isset($this->request->query['list']) and is_array($this->request->query['list'])){
				$i=10;
				$try = true;
				foreach($this->request->query['list'] as $pageid){
					if($try)
						$try=$this->DreamjobPageInh->updateAll(array('DreamjobPageInh.position'=>$i),array('DreamjobPageInh.id'=>$pageid,'DreamjobPageInh.user_id'=>$profil['AppUser']['id']));
					$i+=10;
				}
				if($try){
					$this->MiconwareSession->refreshCache();
					if($company)
						$profil = $this->MiconwareSession->getCompany();
					else
						$profil = $this->MiconwareSession->getWorker();
					$this->MiconwareSession->setFlash(__('dreamjob.error.page.order.saved'),'flash',array('alert'=>'success'));	
				}
				else
					$this->MiconwareSession->setFlash(__('dreamjob.error.page.order.error'),'flash',array('alert'=>'danger'));	
			}
			
			$this->set('profil',$profil);
			$this->render('/User/settings_pages');
		}
	}
	
	public function settings_account() {
		$this->MiconwareSession->init($this);
		
		$this->MiconwareSession->initWeb($this);
		$this->autoRender = true;
		$this->layout = 'default';
		$this->RequestHandler->setContent('html', 'application/html' );
		
		
		
		$profil = $this->MiconwareSession->getWorker();
		$company = false;
		if(empty($profil) or count($profil) <= 0){
			$profil = $this->MiconwareSession->getCompany();
			$company= true;
		}
			
		if(empty($profil) or count($profil) <= 0){
				$this->MiconwareSession->setFlash(__('dreamjob.error.page.notFound'),'flash',array('alert'=>'danger'));	
				$this->set('error', 'dreamjob.error.noPermision');
				$this->render('/Home/error');
		}else{
			if ($this->request->is('post')){
				if($this->request->data['User']['password1'] == $this->request->data['User']['password2']) {
					
					if($this->request->data['User']['password1']!='')
						$profil['User']['password'] = $this->MiconwareSession->create_hash($this->request->data['User']['password1']);
				
					$profil['DreamjobUser']['city_id'] = $this->request->data['DreamjobUser']['city_id'];
					$profil['DreamjobUser']['street'] = $this->request->data['DreamjobUser']['street'];
					$profil['DreamjobUser']['postcode'] = $this->request->data['DreamjobUser']['postcode'];
					$profil['AppUser']['bday'] = $this->request->data['AppUser']['bday'];
					$profil['AppUser']['first_name'] = $this->request->data['AppUser']['first_name'];
					$profil['AppUser']['last_name'] = $this->request->data['AppUser']['last_name'];
					
					$result = false;
					if($company){
						$profil['DreamjobCompany']['corporateform'] = $this->request->data['DreamjobCompany']['corporateform'];
						$profil['DreamjobCompany']['owner'] = $this->request->data['DreamjobCompany']['owner'];
						$profil['DreamjobCompany']['branch_id'] = $this->request->data['DreamjobCompany']['branch_id'];
						$profil['DreamjobCompany']['headcount'] = $this->request->data['DreamjobCompany']['headcount'];
						$profil['DreamjobCompany']['bank_details'] = $this->request->data['DreamjobCompany']['bank_details'];
						$profil['DreamjobCompany']['website'] = $this->request->data['DreamjobCompany']['website'];
						$result = $this->DreamjobCompany->saveSettings($profil);
					}else{
						$profil['AppUser']['mannerofaddress_id'] = $this->request->data['AppUser']['mannerofaddress_id'];
						$profil['DreamjobWorker']['country'] = $this->request->data['DreamjobWorker']['country'];
						$profil['DreamjobWorker']['iam'] = $this->request->data['DreamjobWorker']['iam'];
						$profil['DreamjobWorker']['graducation_id'] = $this->request->data['DreamjobWorker']['graducation_id'];
						$profil['DreamjobWorker']['searchhidden'] = $this->request->data['DreamjobWorker']['searchhidden'];
						$profil['DreamjobWorker']['workexperience'] = $this->request->data['DreamjobWorker']['workexperience'];
						$result = $this->DreamjobWorker->saveSettings($profil);
					}
				
				
					if($result){
						$this->MiconwareSession->setFlash(__('dreamjob.settings.save'),'flash',array('alert'=>'success'));
					}else{
						$this->MiconwareSession->setFlash(__('dreamjob.settings.save.error'),'flash',array('alert'=>'danger'));
					}
				
				}else if($this->request->data['User']['password1']!='' or $this->request->data['User']['password2']!=''){
					$this->MiconwareSession->setFlash(__('dreamjob.error.password.notEqual'),'flash',array('alert'=>'danger'));	
				}

			}
			$this->request->data = $profil;
			$this->set('profil',$profil);
			$this->set('graducations',$this->DreamjobListGraducation->find('list'));
			$this->set('branches',$this->DreamjobListBranch->find('list'));
			$this->set('mannerofaddresses',$this->Mannerofaddress->find('list'));
			$this->set('cities',$this->DreamjobListCity->find('list'));
			$this->render('/User/settings_account');
		}
	}
	public function settings_pageInh() {
		
		$this->MiconwareSession->init($this);
		
		$this->MiconwareSession->initWeb($this);
		$this->autoRender = true;
		$this->layout = 'default';
		$this->RequestHandler->setContent('html', 'application/html' );
		
		$result = false;
		
		$profil = $this->MiconwareSession->getWorker();
		if(empty($profil) or count($profil) <= 0)
			$profil = $this->MiconwareSession->getCompany();
			
		if(empty($profil) or count($profil) <= 0){
				$this->MiconwareSession->setFlash(__('dreamjob.error.page.notFound'),'flash',array('alert'=>'danger'));	
				$this->set('error', 'dreamjob.error.noPermision');
				$this->render('/Home/error');
		}else{
			//PAGE BEARBEITEN
			if(!empty($this->request->params['page'])){
				$image = false;
				$page = $this->DreamjobPageText->find('first',
						array('conditions' => array("DreamjobPageInh.id" => $this->request->params['page'],'DreamjobPageInh.user_id' => $profil['AppUser']['id']))
					);
				if(!is_array($page) or count($page)<= 0){
					$page = $this->DreamjobPageImage->find('first',
						array('conditions' => array("DreamjobPageInh.id" => $this->request->params['page'],'DreamjobPageInh.user_id' => $profil['AppUser']['id']))
					);
					$image = true;
				}
				if(!empty($page) and count($page)> 0){
					if($this->request->is('post')){
						var_dump($this->request);
						$this->request->data['DreamjobPageInh']['user_id']=$profil['AppUser']['id'];
						if($image){
							$this->request->data['DreamjobPageInh']['id']=$page['DreamjobPageInh']['id'];
							$this->request->data['DreamjobPageImage']['page_ptr_id']=$page['DreamjobPageInh']['id'];
							$result = $this->DreamjobPageImage->saveAndUpload($this->request->data);
						}else{
							$this->request->data['DreamjobPageInh']['id']=$page['DreamjobPageInh']['id'];
							$this->request->data['DreamjobPageText']['page_ptr_id']=$page['DreamjobPageInh']['id'];
							$result = $this->DreamjobPageText->saveAssociated($this->request->data);
						}
						if(!$result)
							$this->MiconwareSession->setFlash(__('dreamjob.page.save.error'),'flash',array('alert'=>'danger'));
					}else
						$this->request->data=$page;
					$this->set('page',$page);
				}else
					$this->MiconwareSession->setFlash(__('dreamjob.error.page.notFound'),'flash',array('alert'=>'danger'));
			}else{
				// PAGE LOESCHEN
				if(!empty($this->request->query['del'])){
					$image = false;
					$page = $this->DreamjobPageText->find('first',
							array('conditions' => array("DreamjobPageInh.id" => $this->request->query['del'],'DreamjobPageInh.user_id' => $profil['AppUser']['id']))
						);
					if(!is_array($page) or count($page)<= 0){
						$page = $this->DreamjobPageImage->find('first',
							array('conditions' => array("DreamjobPageInh.id" => $this->request->query['del'],'DreamjobPageInh.user_id' => $profil['AppUser']['id']))
						);
						$image = true;
					}
					$result = false;
					if(!empty($page) and count($page)> 0){
						$path = null;
						if($image){
							$result = $this->DreamjobPageImage->delete($page,false);
						}else
							$result = $this->DreamjobPageText->delete($page['DreamjobPageInh']['id'],false);
						
						if($result){
							$this->MiconwareSession->refreshCache();
							$profil = $this->MiconwareSession->getWorker();
							if(empty($profil) or count($profil) <= 0)
								$profil = $this->MiconwareSession->getCompany();
							$this->MiconwareSession->setFlash(__('dreamjob.pages.delete'),'flash',array('alert'=>'success'));
						}else{
							$this->MiconwareSession->setFlash(__('dreamjob.pages.delete.error'),'flash',array('alert'=>'danger'));
						}
					}
					$result = false;
				}
				// NEW PAGE
				if($this->request->is('post')){
					if(!empty($this->request->data['DreamjobPageText']['text']) and $this->request->data['DreamjobPageImage']['image']['error']==0){
						$this->MiconwareSession->setFlash(__('dreamjob.page.oneType'),'flash',array('alert'=>'danger'));
					}else{
						$this->request->data['DreamjobPageInh']['user_id']=$profil['AppUser']['id'];
						if($this->request->data['DreamjobPageImage']['image']['error']==0){
							unset($this->request->data['DreamjobPageText']);
							$this->DreamjobPageImage->create();
							$result = $this->DreamjobPageImage->saveAndUpload($this->request->data);
						}else{
							unset($this->request->data['DreamjobPageImage']);
							$this->DreamjobPageText->create();
							$result = $this->DreamjobPageText->saveAssociated($this->request->data);
						}
						
						if(!$result)
							$this->MiconwareSession->setFlash(__('dreamjob.page.save.error'),'flash',array('alert'=>'danger'));
					}
				}
			}
			if($result){
				$this->MiconwareSession->refreshCache();
				$this->MiconwareSession->setFlash(__('dreamjob.page.save'),'flash',array('alert'=>'success'));
				$profil = $this->MiconwareSession->getWorker();
				if(empty($profil) or count($profil) <= 0)
					$profil = $this->MiconwareSession->getCompany();
			}
			$this->set('profil',$profil);
			$this->render('/User/settings_pages');
		}
	}
	public function settings_isearch() {
		$this->MiconwareSession->init($this);
		
		$this->MiconwareSession->initWeb($this);
		$this->autoRender = true;
		$this->layout = 'default';
		$this->RequestHandler->setContent('html', 'application/html' );
		
		
		
		$profil = $this->MiconwareSession->getWorker();
			
		if(empty($profil) or count($profil) <= 0){
				$this->MiconwareSession->setFlash(__('dreamjob.error.page.notFound'),'flash',array('alert'=>'danger'));	
				$this->set('error', 'dreamjob.error.noPermision');
				$this->render('/Home/error');
		}else{
			if($this->request->is('post')){
				$tosave = array();
				if(isset($this->request->data['DreamjobISearch'])){
				$tosave = $this->request->data['DreamjobISearch'];
				foreach($tosave as $i => $a)
					$tosave[$i]['worker_id']=$profil['AppUser']['id'];
				}
				$result = $this->DreamjobISearch->saveAndDelete($tosave,$profil['DreamjobISearch']);
				if($result){
					$this->MiconwareSession->refreshCache();
					$profil = $this->MiconwareSession->getWorker();
					$this->MiconwareSession->setFlash(__('dreamjob.settings.save'),'flash',array('alert'=>'success'));
				}else{
					$this->MiconwareSession->setFlash(__('dreamjob.settings.save.error'),'flash',array('alert'=>'danger'));
				}
			}
			$this->request->data = $profil;
			$this->set('kindofjobs',$this->DreamjobListKindofjob->find('list'));
			$this->set('jobs',$this->DreamjobListJob->find('list'));
			$this->set('branches',$this->DreamjobListBranch->find('list'));
			$this->set('profil',$profil);
			$this->render('/User/settings_isearch');
		}
	}
	public function profil_delete() {
		$this->MiconwareSession->init($this);
		
		$this->MiconwareSession->initWeb($this);
		$this->autoRender = true;
		$this->layout = 'default';
		$this->RequestHandler->setContent('html', 'application/html' );
		
		
		
		$profil = $this->MiconwareSession->getWorker();
		$company = false;
		if(empty($profil) or count($profil) <= 0){
			$profil = $this->MiconwareSession->getCompany();
			$company= true;
		}
			
		if(empty($profil) or count($profil) <= 0){
				$this->MiconwareSession->setFlash(__('dreamjob.error.page.notFound'),'flash',array('alert'=>'danger'));	
				$this->set('error', 'dreamjob.error.noPermision');
				$this->render('/Home/error');
		}else{
			if ($this->request->is('post')){
				$key = $this->MiconwareSession->generateKey();
				$profil['User']['code'] = "d:".$key;
				$result = $this->User->sendDelete($profil);
				
				if($result){
					$this->MiconwareSession->setFlash(__('dreamjob.settings.delete.agree'),'flash',array('alert'=>'success'));
				}else{
					$this->MiconwareSession->setFlash(__('dreamjob.settings.delete.error'),'flash',array('alert'=>'danger'));
				}
			}
			$this->render('/User/settings_account_delete');
		}
	}
	public function avatar_upload() {
		$this->MiconwareSession->init($this);
		$this->layout = 'default';
		$this->MiconwareSession->initWeb($this);
		$this->RequestHandler->setContent('html', 'application/html' );
		
		
		
		$profil = $this->MiconwareSession->getApplication('AppUser');
		
		if(empty($profil) or count($profil) <= 0){
				$this->MiconwareSession->setFlash(__('dreamjob.error.page.notFound'),'flash',array('alert'=>'danger'));	
				$this->set('error', 'dreamjob.error.noPermision');
				$this->render('/Home/error');
		}else{
			if($this->request->is('put') or $this->request->is('post')){
				$data['AppUser']['id'] = $profil['AppUser']['id'];
				$data['AppUser']['take_systemwide'] = $profil['AppUser']['take_systemwide'];
				$data['AppUser']['user_id'] = $profil['AppUser']['user_id'];
				$data['AppUser']['avatar'] = $this->request->data['AppUser']['avatar'];
				$result = $this->AppUser->imageUpload($data);
				if($result){
					$this->MiconwareSession->setFlash(__('dreamjob.settings.avatar.done'),'flash',array('alert'=>'success'));
				}else{
					$this->MiconwareSession->setFlash(__('dreamjob.settings.avatar.error'),'flash',array('alert'=>'danger'));
				}
			}
			$this->request->data = $profil;
			$this->render('/User/settings_imageUpload');
		}
	}
	public function test() {
		$this->MiconwareSession->init($this);
		
		$this->MiconwareSession->initWeb($this);
//		$this->set('data', $this->DreamjobCompany->find('all'));
		$this->set('data', $this->DreamjobJobMsg->find('first'));
		$this->render('/User/test');
	}
}