MSV FM

dot.antimicrobial@66.96.161.157: ~ $
Path : /hermes/bosweb/b1705/silverkeyhomesinc.ca/dev_admin_dawncs/app/Controller/
File Upload :
Current < : /hermes/bosweb/b1705/silverkeyhomesinc.ca/dev_admin_dawncs/app/Controller/ApplicantsController.php

<?php
App::uses('AppController', 'Controller');
App::uses('CakeEmail', 'Network/Email');

/**
 * Applicants Controller
 *
 * @property Applicant $Applicant
 * @property PaginatorComponent $Paginator
 * @property SessionComponent $Session
 * @property FlashComponent $Flash
 */
class ApplicantsController extends AppController
{

	/**
	 * Components
	 *
	 * @var array
	 */
	public $components = array('Paginator', 'Session', 'Flash', 'Qimage');

	public function beforeFilter()
	{
		parent::beforeFilter();

		if ($this->Auth->user('id')) {
			$this->Auth->allow();
		} else {
			$this->Auth->allow('index', 'profile');
		}
	}

	/**
	 * index method
	 *
	 * @return void
	 */
	public function index()
	{

		if ($this->request->is('post')) {

			$this->set($this->request->data);

			if ($this->Applicant->validates()) {

				$applicant = $this->request->data['Applicant'];

				$this->Applicant->recursive = 0;
				$data = $this->Applicant->find('first', array(
					'conditions' => array(
						'Applicant.file_number' => trim(strtoupper($applicant['filenumber'])),
						'Applicant.token' => trim($applicant['secretkey']),
						'Applicant.is_deleted' => false
					)
				));

				if (empty($data)) {

					$this->Flash->error(__('Invalid Applicant Credential.'));

				} else {

					$this->set('data', $data);

					$this->loadModel('ApplicantComment');
					$this->ApplicantComment->recursive = -1;
					$applicant_comments = $this->ApplicantComment->find('all', array(
						'conditions' => array(
							'applicant_id' => $data['Applicant']['id'],
							'is_deleted' => false
						), 'order' => 'id DESC'));

					$this->set('applicant_comments', $applicant_comments);
					$this->render('profile');
				}

			} else {

				$this->Flash->error(__('Invalid Applicant Credential.'));
			}
		}
	}

	public function profile()
	{

	}

	/**
	 * view method
	 *
	 * @param string $id
	 * @return void
	 * @throws NotFoundException
	 */
	public function view($id = null)
	{
		if (!$this->Applicant->exists($id)) {
			throw new NotFoundException(__('Invalid applicant'));
		}
		$options = array('conditions' => array('Applicant.' . $this->Applicant->primaryKey => $id));
		$this->set('applicant', $this->Applicant->find('first', $options));
	}

	public function admin_list()
	{
		if ($this->request->is('post')) {
			$status_type_id = $this->request->data['Applicant']['status_type_id'];
			if ($status_type_id) {
				$this->paginate = array('conditions' => array('status_type_id' => $status_type_id));
			}
		} else {
			$this->paginate = array('conditions' => array('status_type_id' => 1));
		}

		$statusTypes = $this->Applicant->StatusType->find('list');
		$this->set(compact('statusTypes'));

		$this->Applicant->recursive = 0;

		$this->set('applicants', $this->Paginator->paginate());
	}

	public function admin_index()
	{
		if ($this->request->is('post')) {
			$status_type_id = $this->request->data['Applicant']['status_type_id'];
			$category_id = $this->request->data['Applicant']['category_id'];
			$search = trim($this->request->data['Applicant']['search']);
			$agent_id = $this->request->data['Applicant']['agent_id'];
			$vendor_id = $this->request->data['Applicant']['vendor_id'];

			$conditions = array('is_deleted' => false);

			if ($search) {
				$conditions += array('firstname LIKE' => '%' . $search . '%');
			}
			if ($status_type_id) {
				$conditions += array('status_type_id' => $status_type_id);
				// $this->paginate = array('conditions' => array('status_type_id' => $status_type_id));
			}
			if ($category_id) {
				$conditions += array('category_id' => $category_id);
			}
			if ($agent_id && $vendor_id) {
				$conditions += array('AND' => array(
					'OR' => array('agent_user_id' => $agent_id, 'vendor_id' => $vendor_id)
				)
				);
			} else if ($agent_id) {
				$conditions += array('OR' => array('agent_user_id' => $agent_id));
			} else if ($vendor_id) {
				$conditions += array('OR' => array('vendor_id' => $vendor_id));
			}

			$this->paginate = array('conditions' => $conditions);

		} else {
			$this->paginate = array('conditions' => array('status_type_id' => 1, 'is_deleted' => false));
		}

		$statusTypes = $this->Applicant->StatusType->find('list');
		$this->set(compact('statusTypes'));

		$this->Applicant->recursive = 0;
		$this->paginate += array(
			'recursive' => 0,
			'fields' => array('Applicant.*', 'User.name', 'User.token', 'Vendor.token', 'Vendor.fullname', 'Vendor.id'),
			'order' => array(
				'Applicant.created' => 'DESC'
			),
			'limit' => 50
		);

		$this->set('applicants', $this->Paginator->paginate());
		$this->set('categories', $this->Applicant->Category->find('list', array('order' => 'Category.name ASC')));
		$this->set('agents', $this->Applicant->User->find('list', array('conditions' => array('group_id' => 2, 'active' => true))));
		$this->set('vendors', $this->Applicant->Vendor->find('list'));
	}

	/**
	 * add method
	 *
	 * @return void
	 */
	public function admin_add()
	{

		if ($this->request->is('post')) {
			$data['file'] = $this->request->data['Applicant']['file'];
			$data['path'] = FOLDER_APPLICANT;

			if ($data['file']['size'] > 0) {

				$filename = $this->Qimage->copy($data);
				$this->request->data['Applicant']['photo'] = $filename;
				// $this -> request -> data['Applicant']['original_filename'] = $this -> request -> data['Applicant']['file']['name'];

				unset($this->request->data['Applicant']['file']);
			}
			$this->request->data['Applicant']['added_by_user_id'] = $this->Auth->user('id');
			$this->request->data['Applicant']['file_number'] = strtoupper($this->request->data['Applicant']['file_number']);
			$this->request->data['Applicant']['token'] = md5(date("Y-m-d H:i:s"));

			/// pr($this->request->data); die;

			$this->Applicant->create();
			if ($this->Applicant->save($this->request->data)) {

				if ($this->request->data['Applicant']['vendor_id']) {

					$this->loadModel('VendorApplicantFee');
					$this->request->data['VendorApplicantFee']['applicant_id'] = $this->Applicant->id;
					$this->request->data['VendorApplicantFee']['vendor_id'] = $this->request->data['Applicant']['vendor_id'];
					$this->VendorApplicantFee->create();
					if ($this->VendorApplicantFee->save($this->request->data['VendorApplicantFee'])) {
						$this->Flash->success(__('The applicant has been saved.'));
						$this->redirect('/' . $this->params['prefix'] . '/add_applicant');
					}

				} else {
					$this->Flash->success(__('The applicant has been saved.'));
					$this->redirect('/' . $this->params['prefix'] . '/add_applicant');
				}
			} else {

				if (isset($filename)) {
					unlink(FOLDER_APPLICANT . $filename);
				}

				$this->Flash->error(__('The applicant could not be saved. Please, try again.'));
			}
		}
		$statusTypes = $this->Applicant->StatusType->find('list');
		$agentUsers = $this->Applicant->User->find('list', array(
			'fields' => array('id', 'name'),
			'conditions' => array('group_id' => AGENT),
			'order' => 'name ASC'
		));
		$vendors = $this->Applicant->Vendor->find('list', array(
			'fields' => array('id', 'fullname'),
			'order' => 'fullname ASC'
		));
		$this->set(compact('statusTypes', 'agentUsers', 'vendors'));
		$this->set('categories', $this->Applicant->Category->find('list', array('order' => 'Category.name ASC')));
		/*
		$this->Applicant->recursive = 0;
		$this->set('applicants', $this->Paginator->paginate());*/
	}

	/**
	 * @param null $token
	 */
	public function admin_edit_login($token = null)
	{
		/**
		 * get token, fetch applicant id
		 * get uid, fetch user detail from users table
		 *
		 */

		if ($this->request->is(array('post'))) {

			$this->loadModel('User');
			$this->loadModel('ApplicantEmail');

			$decoded_password = uniqid();

			$applicant = $this->Applicant->find('first', array('recursive' => -1, 'conditions' => array('token' => $token)));

			$data = $this->request->data['ApplicantCredential'];

			if (empty(trim($data['uid']))) {

				// @first time sending login credential
				$user_data['User'] = array(
					'username' => $applicant['Applicant']['email'],
					'password' => $decoded_password,
					'group_id' => APPLICANT,
					'name' => $applicant['Applicant']['lastname'] . ', ' . $applicant['Applicant']['firstname'],
					'active' => $applicant['Applicant']['status_type_id'] == STATUS_IN_PROGRESS,
					'token' => md5(date('Y-m-d H:i:s'))
				);
				$user_data['UserApplicant'] = array(
					'applicant_id' => $applicant['Applicant']['id']
				);

				if ($this->User->saveAll($user_data)) {
					/**
					 * send email
					 * insert into emails table
					 * redirect
					 */
					// @send email
					$content = $applicant['Applicant'];
					$subject = WEBSITE_NAME . ' - File Number: ' . $applicant['Applicant']['file_number'];
					$Email = new CakeEmail();
					$Email->template('applicant_login_detail');
					$Email->emailFormat('html');
					$Email->viewVars(array('content' => $content));
					$Email->from(array('info@dawnimmigration.com'));
					$Email->to('anil.k@smartmeds.ca');
					/// $Email->to($data['Applicant']['email']);
					$Email->subject($subject);

					if ($Email->send()) {
						// @insert into email table
						$applicant_email['ApplicantEmail'] = array(
							'applicant_id' => $applicant['Applicant']['id'],
							'applicant_name' =>  $applicant['Applicant']['lastname'] . ', ' . $applicant['Applicant']['firstname'],
							'user_id' => $this->Auth->User('id'),
							'user_name' => $this->Auth->User('name')
						);

						if ($this->ApplicantEmail->save($applicant_email['ApplicantEmail'])) {
							$this->Flash->success(__('The applicant login credential sent successfully.'));
						}
					} else {

					}


				}
			} else {

				$encoded_password = Security::hash($decoded_password, null, true);
				$query = "UPDATE users SET password = '" . $encoded_password . "' WHERE token = '" . $data['uid'] . "'";
				$this->User->query($query);
				/**
				 * send email
				 * insert into emails table
				 * redirect
				 */

				$this->Flash->success(__('The applicant login credential sent successfully...'));
			}
		}
		$this->redirect('/admin/edit_applicant/' . $token);


	}

	public function admin_edit($token = null)
	{
		$user_token = '';

		if ($this->request->is(array('post', 'put'))) {

			$data['file'] = $this->request->data['Applicant']['file'];
			$data['path'] = FOLDER_APPLICANT;

			$unlink_flag = false;
			if ($data['file']['size'] > 0) {
				// To get previous image name
				$unlink_file = $this->Applicant->field('photo', array('token' => $token));
				$filename = $this->Qimage->copy($data);
				$this->request->data['Applicant']['photo'] = $filename;
				// $this -> request -> data['Applicant']['original_filename'] = $this -> request -> data['Applicant']['file']['name'];
				unset($this->request->data['Applicant']['file']);
				$unlink_flag = true;
			}
			$this->request->data['Applicant']['file_number'] = strtoupper($this->request->data['Applicant']['file_number']);

			$this->request->data['Applicant']['token'] = md5(date("Y-m-d H:i:s"));

			if ($this->Applicant->save($this->request->data['Applicant'])) {

				// @update users table - status
				$this->loadModel('UserApplicant');
				$user_applicant = $this->UserApplicant->find('first', array('recursive' => -1, 'conditions' => array('applicant_id' => $this->request->data['Applicant']['id'])));

				if(!empty($user_applicant)) {
					$active = 1;
					if($this->request->data['Applicant']['status_type_id'] == STATUS_CLOSED) {
						$active = 0;
					}
					$this->loadModel('User');
					$query = "UPDATE users SET active = $active WHERE id = " . $user_applicant['UserApplicant']['user_id'];
					pr($query);
					$this->User->query($query);
				}
				//

				if ($this->request->data['Applicant']['vendor_id']) {

					$this->loadModel('VendorApplicantFee');

					$query = "DELETE FROM vendor_applicant_fees WHERE vendor_id = " . $this->request->data['Applicant']['vendor_id'] . " AND applicant_id = " . $this->Applicant->id;
					$this->VendorApplicantFee->query($query);
					$this->request->data['VendorApplicantFee']['applicant_id'] = $this->Applicant->id;
					$this->request->data['VendorApplicantFee']['vendor_id'] = $this->request->data['Applicant']['vendor_id'];
					// $this->VendorApplicantFee->create();
					if ($this->VendorApplicantFee->save($this->request->data['VendorApplicantFee'])) {
						$this->Flash->success(__('The applicant has been saved.'));
						if ($unlink_flag == true) unlink(FOLDER_APPLICANT . $unlink_file);
						$this->redirect('/' . $this->params['prefix'] . '/edit_applicant/' . $this->request->data['Applicant']['token']);
					}

				} else {
					$this->Flash->success(__('The applicant has been saved.'));
					if ($unlink_flag == true) unlink(FOLDER_APPLICANT . $unlink_file);
					$this->redirect('/' . $this->params['prefix'] . '/edit_applicant/' . $this->request->data['Applicant']['token']);
				}
			} else {

				if (isset($filename)) {
					unlink(FOLDER_APPLICANT . $filename);
				}

				$this->Flash->error(__('The applicant could not be saved. Please, try again.'));
			}
		} else {

			// @process to remove notifications
			// $this->loadModel('Notification');
			// $this->Notification->query("DELETE FROM notifications WHERE module_reference = '$token'");

			$options = array(
				'fields' => array('Applicant.*', 'VendorApplicantFee.*', 'UserApplicant.*'),
				'conditions' => array('Applicant.token' => $token), 'recursive' => 0
			);
			$this->request->data = $this->Applicant->find('first', $options);

			if(empty($this->request->data)) {
				$this->redirect('/admin/applicants');
			}

			if (!empty($this->request->data['UserApplicant']['user_id'])) {
				$this->loadModel('User');
				$userInfo = $this->User->find('first', array('recursive' => -1, 'conditions' => array('User.id' => $this->request->data['UserApplicant']['user_id'])));
				$user_token = $userInfo['User']['token'];
			}
		}
		$statusTypes = $this->Applicant->StatusType->find('list');
		$agentUsers = $this->Applicant->User->find('list', array(
			'fields' => array('id', 'name'),
			'conditions' => array('group_id' => AGENT),
			'order' => 'name ASC'
		));
		$vendors = $this->Applicant->Vendor->find('list', array(
			'fields' => array('id', 'fullname'),
			'order' => 'fullname ASC'
		));
		$this->set(compact('statusTypes', 'agentUsers', 'vendors', 'user_token'));
		$this->set('categories', $this->Applicant->Category->find('list', array('order' => 'Category.name ASC')));
	}

	public function admin_view($token = null)
	{

		$this->Applicant->unbindModel(
			array('belongsTo' => array('Vendor'))
		);
		$applicant_info = $this->Applicant->find('first', array(
			'conditions' => array('Applicant.token' => $token),
			'fields' => array(
				'Applicant.id', 'Applicant.fullname', 'Applicant.email', 'Applicant.photo', 'Applicant.agent_user_id',
				'StatusType.id', 'StatusType.name', 'User.name', 'Category.name'
			),
			'recursive' => 0
		));

		$this->loadModel('Message');

		if ($this->request->is(array('post', 'put'))) {


			$files = $this->request->data['MessageDocument']['files'];

			// @validation description & file upload
			// todo
			//

			$this->request->data['Message']['applicant_id'] = $applicant_info['Applicant']['id'];
			$this->request->data['Message']['from_user_id'] = $this->Auth->user('id');
			$this->request->data['Message']['to_user_id'] = $applicant_info['Applicant']['agent_user_id'];

			if (!empty($files)) {

				foreach ($files as $key => $file) {
					$file_data = array();
					if ($file['size'] > 0) {
						$file_data['file'] = $file;
						$file_data['path'] = FOLDER_DOCUMENTS;

						$filename = $this->Qimage->copy($file_data);
						$original_filename = $file['name'];

						$this->request->data['MessageDocument'][$key]['filename'] = $filename;
						$this->request->data['MessageDocument'][$key]['original_filename'] = $original_filename;
					}
				}
				unset($this->request->data['MessageDocument']['files']);
			}

			// @save data

			$this->Message->create();
			if ($this->Message->saveAll($this->request->data)) {

				// @process to add into notification
				$this->loadModel('Notification');
				$notificaton = array();
				$notificaton['Notification'] = array(
					'module_id' => MOD_MESSAGE,
					'from_user_id' => $this->Auth->user('id'),
					'to_user_id' => $applicant_info['Applicant']['agent_user_id'],
					'module_reference' => $token,
					'description' => 'Message from ' . $this->Auth->user('name') . ' for Applicant: ' . $applicant_info['Applicant']['fullname']
				);
				$this->Notification->save($notificaton);
				//

				$this->Flash->success(__('The messsage successfully sent.'));
				$this->redirect('/' . $this->params['prefix'] . '/view_applicant/' . $token);
			} else {
				if (!empty($this->request->data['MessageDocument'])) {
					foreach ($this->request->data['MessageDocument'] as $file) {
						unlink(FOLDER_DOCUMENTS . $file['filename']);
					}
				}
			}
		}

		$users = $this->Applicant->User->find('list', array(
			'conditions' => array('active' => true),
			'fields' => array('id', 'name')
		));
		$this->set('users', $users);

		$data = $this->Message->find('all', array(
			'conditions' => array('applicant_id' => $applicant_info['Applicant']['id']),
			'recursive' => 1,
			'order' => 'Message.id DESC'
		));
		$this->set('data', $data);
		$this->set('applicant', $applicant_info);

	}

	public function agent_view($token = null)
	{

		$this->Applicant->unbindModel(
			array('belongsTo' => array('Vendor'))
		);
		$applicant_info = $this->Applicant->find('first', array(
			'conditions' => array('Applicant.token' => $token),
			'fields' => array(
				'Applicant.id', 'Applicant.fullname', 'Applicant.email', 'Applicant.photo', 'Applicant.agent_user_id',
				'StatusType.id', 'StatusType.name', 'User.name', 'Category.name'
			),
			'recursive' => 0
		));

		$this->loadModel('Message');
		if ($this->request->is(array('post', 'put'))) {

			$files = $this->request->data['MessageDocument']['files'];

			// @validation description & file upload
			// todo
			//
			$this->request->data['Message']['applicant_id'] = $applicant_info['Applicant']['id'];
			$this->request->data['Message']['from_user_id'] = $this->Auth->user('id');


			if (!empty($files)) {

				foreach ($files as $key => $file) {
					$file_data = array();
					if ($file['size'] > 0) {
						$file_data['file'] = $file;
						$file_data['path'] = FOLDER_DOCUMENTS;

						$filename = $this->Qimage->copy($file_data);
						$original_filename = $file['name'];

						$this->request->data['MessageDocument'][$key]['filename'] = $filename;
						$this->request->data['MessageDocument'][$key]['original_filename'] = $original_filename;
					}
				}
				unset($this->request->data['MessageDocument']['files']);
			}

			// @save data

			$this->Message->create();
			if ($this->Message->saveAll($this->request->data)) {

				// @process to add into notification
				$this->loadModel('Notification');
				$notificaton = array();
				$notificaton['Notification'] = array(
					'module_id' => MOD_MESSAGE,
					'from_user_id' => $this->Auth->user('id'),
					'to_user_id' => $this->request->data['Message']['to_user_id'],
					'module_reference' => $token,
					'description' => 'Message from ' . $this->Auth->user('name') . ' for Applicant: ' . $applicant_info['Applicant']['fullname']
				);
				$this->Notification->save($notificaton);
				//


				$this->Flash->success(__('The messsage successfully sent.'));
				$this->redirect('/' . $this->params ['prefix'] . '/view_applicant/' . $token);
			} else {
				if (!empty($this->request->data['MessageDocument'])) {
					foreach ($this->request->data['MessageDocument'] as $file) {
						unlink(FOLDER_DOCUMENTS . $file['filename']);
					}
				}
			}
		}

		$users_data = $this->Applicant->User->find('all', array(
			'conditions' => array('User.active' => true, 'User.id !=' => 1),
			'recursive' => -1
		));

		$users = array();
		$admin = array();
		$agents = array();
		foreach ($users_data as $row) {
			if ($row['User']['group_id'] == 1) {
				$admin[$row['User']['id']] = $row['User']['name'];
			} else {
				$agents[$row['User']['id']] = $row['User']['name'];
			}
			// @photo is by default empty
			// for dynamic change the given line
			$users[$row['User']['id']] = array('name' => $row['User']['name'], 'photo' => 'empty.jpg');
		}

		$this->set('users', $users);
		$this->set('agents', $agents);
		$this->set('admin', $admin);

		$data = $this->Message->find('all', array(
			'conditions' => array('applicant_id' => $applicant_info['Applicant']['id']),
			'recursive' => 1,
			'order' => 'Message.id DESC'
		));
		$this->set('data', $data);
		$this->set('applicant', $applicant_info);
	}

	/**
	 * admin_delete method
	 *
	 * @param string $id
	 * @return void
	 * @throws NotFoundException
	 */
	public function admin_delete($token = null)
	{
		$this->Applicant->recursive = -1;
		$is_exist = $this->Applicant->find('count', array('conditions' => array('token' => $token), 'recursive' => -1));

		if ($is_exist > 0) {

			$query = "UPDATE applicants SET is_deleted = true WHERE token = '$token'";

			$this->Applicant->query($query);
			$this->Flash->success(__('The applicant has been deleted.'));
		} else {
			throw new NotFoundException(__('Invalid applicant'));
		}

		$this->redirect('/admin-dashboard');
	}

	public function admin_add_comment($token = null)
	{
		if ($this->request->is('post')) {
			$this->Applicant->ApplicantComment->create();
			if ($this->Applicant->ApplicantComment->save($this->request->data)) {

				//@ send email to applicant
				//@ end

				$this->Flash->success(__('The applicant comment has been saved.'));
				return $this->redirect('/' . $this->params['prefix'] . '/applicant_comment/' . $token);
			} else {
				$this->Flash->error(__('The applicant comment could not be saved. Please, try again.'));
			}
		}

		$comments = $this->Applicant->find('first', array(
			'conditions' => array('Applicant.token' => $token),
			'fields' => array(
				'Applicant.*'
			)
		));
		$this->set('comments', $comments);
	}

	public function admin_delete_comment($token = null, $id = null)
	{
		$this->loadModel('ApplicantComment');
		$this->ApplicantComment->id = $id;
		if (!$this->ApplicantComment->exists()) {
			throw new NotFoundException(__('Invalid Information, Please contact to ' . EMAIL_ID));
		}
		$this->request->allowMethod('post', 'delete');
		if ($this->ApplicantComment->delete()) {
			$this->Flash->success(__('The Applicant Comment has been deleted.'));
		} else {
			$this->Flash->error(__('The Applicant Comment could not be deleted. Please, try again.'));
		}
		$this->redirect('/' . $this->params['prefix'] . '/applicant_comment/' . $token);
	}

	public function admin_send_email($id = null)
	{

		if ($id == null) return false;
		$this->Applicant->id = $id;

		if (!$this->Applicant->exists()) {
			throw new NotFoundException(__('Invalid applicant'));
		}
		$this->Applicant->recursive = -1;
		$data = $this->Applicant->find('first', array('conditions' => array('id' => $id)));
		$content = $data['Applicant'];
		$subject = WEBSITE_NAME . ' - File Number: ' . $data['Applicant']['file_number'];
		$Email = new CakeEmail();
		$Email->template('applicant', 'applicant');
		$Email->emailFormat('html');
		$Email->viewVars(array('content' => $content));
		$Email->from(array('info@dawnimmigration.com'));
		$Email->to($data['Applicant']['email']);
		$Email->subject($subject);

		if ($Email->send()) {
			$this->Flash->success(__('Successfully email sent to ' . $data['Applicant']['email']));

		} else {
			$this->Flash->error(__('Email id is not valid. Please, try again.'));
		}
		$this->redirect(array('action' => 'list'));
	}

	//@common functions for admin | agent
	private function _add()
	{
		if ($this->request->is('post')) {
			if ($this->Auth->user('group_id') == AGENT) {
				$this->request->data['Applicant']['is_agent'] = true;
			}
			$this->request->data['Applicant']['added_by_user_id'] = $this->Auth->user('id');

			$data['file'] = $this->request->data['Applicant']['file'];
			$data['path'] = FOLDER_APPLICANT;
			if ($data['file']['size'] > 0) {
				$filename = $this->Qimage->copy($data);
				$this->request->data['Applicant']['photo'] = $filename;
				unset($this->request->data['Applicant']['file']);
			}
			$this->request->data['Applicant']['file_number'] = strtoupper($this->request->data['Applicant']['file_number']);
			$this->request->data['Applicant']['token'] = md5(date("Y-m-d H:i:s"));
			$this->Applicant->create();
			if ($this->Applicant->save($this->request->data)) {
				$this->Flash->success(__('New applicant successfully added.'));
				$this->redirect('/' . $this->params['prefix'] . '/');
			} else {
				if (isset($filename)) {
					unlink(FOLDER_APPLICANT . $filename);
				}
				$this->Flash->error(__('The applicant could not be saved. Please, try again.'));
			}
		}
	}

	private function _status_list()
	{
		$statusTypes = $this->Applicant->StatusType->find('list');
		$this->set(compact('statusTypes'));
	}

	//@Agents Actions
	public function agent_index()
	{
		if ($this->request->is('post')) {
			$status_type_id = $this->request->data['Applicant']['status_type_id'];
			$category_id = $this->request->data['Applicant']['category_id'];
			$search = trim($this->request->data['Applicant']['search']);

			$conditions = array();

			if ($search) {
				$conditions += array('firstname LIKE' => '%' . $search . '%');
			}
			if ($status_type_id) {
				$conditions += array('status_type_id' => $status_type_id);
				// $this->paginate = array('conditions' => array('status_type_id' => $status_type_id));
			}
			if ($category_id) {
				$conditions += array('category_id' => $category_id);
			}
			$conditions += array('agent_user_id' => $this->Auth->user('id'));
			$this->paginate = array('conditions' => $conditions);

		} else {
			$this->paginate = array('conditions' => array(
				'status_type_id' => 1,
				'agent_user_id' => $this->Auth->user('id')
			));
		}

		$statusTypes = $this->Applicant->StatusType->find('list');
		$this->set(compact('statusTypes'));

		$this->paginate += array(
			'recursive' => -1,
			'order' => array(
				'Applicant.created' => 'DESC'
			)
		);
		$this->set('applicants', $this->Paginator->paginate());
		$this->set('categories', $this->Applicant->Category->find('list', array('order' => 'Category.name ASC')));
	}

	public function agent_add()
	{
		if ($this->request->is('post')) {
			$data['file'] = $this->request->data['Applicant']['file'];
			$data['path'] = FOLDER_APPLICANT;

			if ($data['file']['size'] > 0) {
				$filename = $this->Qimage->copy($data);
				$this->request->data['Applicant']['photo'] = $filename;
				unset($this->request->data['Applicant']['file']);
			}
			$this->request->data['Applicant']['agent_user_id'] = $this->Auth->user('id');
			$this->request->data['Applicant']['added_by_user_id'] = $this->Auth->user('id');
			$this->request->data['Applicant']['file_number'] = strtoupper($this->request->data['Applicant']['file_number']);
			$this->request->data['Applicant']['token'] = md5(date("Y-m-d H:i:s"));
			$this->Applicant->create();
			if ($this->Applicant->save($this->request->data)) {

				// @process to add into notification
				$this->loadModel('Notification');
				$notificaton = array();
				$notificaton['Notification'] = array(
					'module_id' => MOD_APPLICANT,
					'from_user_id' => $this->Auth->user('id'),
					'to_user_id' => 3, // tmp fixed to info@dawnimmigration.com user
					'module_reference' => $this->request->data['Applicant']['token'],
					'description' => 'New Applicant Added By ' . $this->Auth->user('name')
				);
				$this->Notification->save($notificaton);
				//

				$this->Flash->success(__('The applicant has been saved.'));
				return $this->redirect('/' . $this->params->prefix . '/add_applicant');
			} else {
				if (isset($filename)) {
					unlink(FOLDER_APPLICANT . $filename);
				}
				$this->Flash->error(__('The applicant could not be saved. Please, try again.'));
			}
		}
		$statusTypes = $this->Applicant->StatusType->find('list');
		$this->set(compact('statusTypes'));
		$this->set('categories', $this->Applicant->Category->find('list', array('order' => 'Category.name ASC')));
	}

	public function agent_edit($token = null)
	{

		if ($this->request->is(array('post', 'put'))) {

			$data['file'] = $this->request->data['Applicant']['file'];
			$data['path'] = FOLDER_APPLICANT;

			$unlink_flag = false;
			if ($data['file']['size'] > 0) {
				// To get previous image name
				$unlink_file = $this->Applicant->field('photo', array('token' => $token));
				$filename = $this->Qimage->copy($data);
				$this->request->data['Applicant']['photo'] = $filename;
				// $this -> request -> data['Applicant']['original_filename'] = $this -> request -> data['Applicant']['file']['name'];
				unset($this->request->data['Applicant']['file']);
				$unlink_flag = true;
			}
			$this->request->data['Applicant']['file_number'] = strtoupper($this->request->data['Applicant']['file_number']);

			$this->request->data['Applicant']['token'] = md5(date("Y-m-d H:i:s"));

			if ($this->Applicant->save($this->request->data)) {
				$this->Flash->success(__('The applicant has been saved.'));
				if ($unlink_flag == true) unlink(FOLDER_APPLICANT . $unlink_file);
				$this->redirect('/' . $this->params ['prefix'] . '/applicants');
			} else {

				if (isset($filename)) {
					unlink(FOLDER_APPLICANT . $filename);
				}

				$this->Flash->error(__('The applicant could not be saved. Please, try again.'));
			}
		} else {
			$options = array('conditions' => array('Applicant.token' => $token));
			$this->request->data = $this->Applicant->find('first', $options);
		}
		$statusTypes = $this->Applicant->StatusType->find('list');
		$agentUsers = $this->Applicant->User->find('list', array(
			'fields' => array('id', 'name'),
			'conditions' => array('group_id' => AGENT),
			'order' => 'name ASC'
		));
		$vendors = $this->Applicant->Vendor->find('list', array(
			'fields' => array('id', 'fullname'),
			'order' => 'fullname ASC'
		));
		$this->set(compact('statusTypes', 'agentUsers', 'vendors'));
		$this->set('categories', $this->Applicant->Category->find('list', array('order' => 'Category.name ASC')));
	}

	public function agent_upload($token = null)
	{
		if ($token == null) {
			$this->redirect('/' . $this->params ['prefix'] . '/applicants');
		}
		if ($this->request->is(array('post', 'put'))) {

			pr($this->request->data);

		}

		$options = array(
			'conditions' => array('Applicant.token' => $token),
			'fields' => array('Applicant.*', 'StatusType.*', 'Category.*'),
			'recursive' => 0
		);
		$applicant = $this->Applicant->find('first', $options);
		// pr($applicant);
		$this->set(compact('applicant'));

	}


	public function admin_download()
	{
		$this->layout = '';

	}

	// @applicant actions
	public function applicant_index()
	{

	}
}