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/MessagesController.php

<?php
App::uses('AppController', 'Controller');

class MessagesController extends AppController
{

	public $components = array('Paginator', 'Session', 'Flash');

	public function beforeFilter()
	{
		parent::beforeFilter();
		$this->Auth->allow();
	}

	public function admin_index($user_token = null)
	{
		$applicants = array();
		if (!empty($user_token)) {

			if ($this->request->is('post')) {
				$applicant_id = $this->request->data['ApplicantFilter']['applicant_id'];

				$messages = $this->Message->find('all', array(
					'conditions' => array('applicant_id' => $applicant_id)
				));

				pr($messages);

			}

			$this->loadModel('Applicant');
			$applicants = $this->Applicant->find('list', array(
				'conditions' => array('status_type_id' => 1),
				'fields' => array('id', 'Applicant.fullname'),
				'order' => 'Applicant.id DESC'
			));

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

	public function agent_index($user_token = null) {
		if(!empty($user_token)) {

		}
	}

	public function agent_download($message_id = null) {

		$this->autoRender = false;
		$attachments = $this->Message->MessageDocument->find('list', array(
			'conditions' => array('message_id' => $message_id),
			'fields' => array('id', 'filename')
		));
		if (!empty($attachments)) {
			$this->_create_zip_and_download($attachments, FOLDER_DOCUMENTS);
		}
	}

	public function applicant_index() {
		$allowed_extensions = array('jpg', 'jpeg', 'png', 'pdf');
		$allowed_size = 2000000; // 2 MB
		$target_directory = FOLDER_DOCUMENTS;
		$errors = array();
		$success = array();

		$user = $this->Auth->User();

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

			$this->loadModel('Applicant');
			$added_by_user_id = $this->Applicant->field('added_by_user_id', array('id' => $user['UserApplicant']['applicant_id']));

			$this->request->data['Message']['from_user_id'] = $user['id']; // @ User.id
			$this->request->data['Message']['to_user_id'] = $added_by_user_id; // @ send to Applicant.added_by_user_id
			$this->request->data['Message']['applicant_id'] = $user['UserApplicant']['applicant_id']; // @ Applicant.id

			$document_data = array();
			$msg_documents = $this->request->data['MessageDocument']['files'];

			foreach ($msg_documents as $document) {

				$file_name = $document['name'];
				$file_tmp = $document['tmp_name'];
				$file_size = $document['size'];
				$file_error = $document['error'];

				if(!empty($file_name)) {

					if (!$file_error) {
						// @check extension
						$file_ext = explode('.', $file_name);
						$file_act_ext = strtolower(end($file_ext));

						if (in_array($file_act_ext, $allowed_extensions)) {

							$uid_file_name = md5(uniqid().date('Y-m-d H:i:s')) . '.' . $file_act_ext;

							// @check size
							if($file_size <= $allowed_size) {
								// @todo uncomment
								$is_moved = move_uploaded_file($file_tmp, $target_directory . '/' . $uid_file_name);
								if ($is_moved) {
									$document_data[] = array(
										'filename' => $uid_file_name,
										'original_filename' => $file_name
									);
									$success[] = array(
										'message' => 'File format and file extension is good.',
										'file' => $document
									);
								} else {
									// @error file does not moved.
									$errors[] = array(
										'message' => 'File does not moved.',
										'file' => $document
									);
								}
							} else {

								// @error exceeded size
								$errors[] = array(
									'message' => 'File Size Exceeded.',
									'file' => $document
								);
							}

						} else {
							// @error extension doesn't allow
							$errors[] = array(
								'message' => 'File extension does not allowed',
								'file' => $document
							);
						}
					} else {
						$errors[] = array(
							'message' => 'Issue in file, Please check your file.',
							'file' => $document
						);
					}
				}
			}

			if (empty($errors)) {

				if (count($document_data) > 0) {
					$this->request->data['MessageDocument'] = $document_data;
				} else {
					unset($this->request->data['MessageDocument']);
				}

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

					// @send email to admin

					//

					$this->Flash->success(__('The Message send successfully.'));
					$this->redirect('/applicant-dashboard');

				} else {

					if (count($document_data) > 0) {
						foreach ($document_data as $item) {
							unlink($target_directory . '/' . $item['filename']);
						}
					}
					$this->Flash->error(__('The Message could not be saved. Please, try again.'));

				}
			} else {
				$this->Flash->error(__('The Message could not be saved. There are some errors.'));
			}
		}

		$applicant_id = $user['UserApplicant']['applicant_id'];


		$msg_data = $this->Message->find('all', array(
			'conditions' => array('applicant_id' => $applicant_id),
			'order' => 'Message.id DESC'
		));


		$messages = array();

		if(!empty($msg_data)) {

			foreach ($msg_data as $msg) {

				$date = date('d M Y', strtotime($msg['Message']['created']));
				$messages[$date][] = $msg;
			}
		}

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

		$this->set('errors', $errors);
		$this->set('success', $success);
	}

	public function applicant_download($msg_doc_id, $msg_doc_name) {
		$this->autoRender = false;

		$original_filename = $this->Message->MessageDocument->field(
			'original_filename',
			array(
				'filename' => $msg_doc_name,
				'id' => $msg_doc_id
			)
		);

		if (!empty($original_filename)) {
			$this->_download_single_file($msg_doc_name, $original_filename);
		}
	}

	public function applicant_download_all($message_id) {
		$this->autoRender = false;
		$attachments = $this->Message->MessageDocument->find('list', array(
			'conditions' => array('message_id' => $message_id),
			'fields' => array('filename', 'original_filename')
		));

		if (!empty($attachments)) {
			$this->_create_zip_and_download($attachments, FOLDER_DOCUMENTS);
		}
	}

	public function _download_single_file($filename, $original_filename) {
		$file_path = FOLDER_DOCUMENTS . $filename;
		if (file_exists($file_path)) {
			header('Content-Description: File Transfer');
			header('Content-Type: application/force-download');
			header("Content-Disposition: attachment; filename=\"" . basename($original_filename) . "\";");
			header('Content-Transfer-Encoding: binary');
			header('Expires: 0');
			header('Cache-Control: must-revalidate');
			header('Pragma: public');
			header('Content-Length: ' . filesize($file_path));
			ob_clean();
			flush();
			readfile($file_path); //showing the path to the server where the file is to be download
			exit;
		}
	}

	public function _create_zip_and_download($files, $files_path, $zip_filename = 'download.zip') {
		$zip_file_path = FOLDER_DOCUMENTS . $zip_filename;

		$zip = new ZipArchive();
		if ($zip->open($zip_file_path, ZipArchive::CREATE) === TRUE) {

			foreach ($files as $filename => $original_filename) {
				$zip->addFile($files_path . $filename, $original_filename);
			}
			$zip->close();

			// Download the created zip file
			header("Content-type: application/zip");
			header("Content-Disposition: attachment; filename = $zip_filename");
			header("Content-Length: ".filesize($zip_file_path));
			header("Pragma: no-cache");
			header("Expires: 0");
			readfile("$zip_file_path");

			unlink($zip_file_path);
			exit;
		}
	}
}