<?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;
$message_id = 7;
$messages = $this->Message->MessageDocument->find('list', array(
'conditions' => array('message_id' => 7),
'fields' => array('id', 'filename')
));
$this->_download($messages, FOLDER_DOCUMENTS);
// $this->_create_zip_and_download($messages, FOLDER_DOCUMENTS, 'download.zip');
}
public function _download($files, $folder_path, $file_name = 'download.zip') {
$result = $this->_createZipArchive($files, $folder_path);
if ($result == true) {
header("Content-Disposition: attachment; filename=\"".$file_name."\"");
header("Content-Length: ".filesize($file_name));
readfile($file_name);
exit();
}
}
public function _createZipArchive($files = array(), $destination = '', $overwrite = false) {
if(file_exists($destination) && !$overwrite) { return false; }
$validFiles = array();
if(is_array($files)) {
foreach($files as $file) {
if(file_exists($file)) {
$validFiles[] = $file;
}
}
}
if(count($validFiles)) {
$zip = new ZipArchive();
if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) == true) {
foreach($validFiles as $file) {
$zip->addFile($file,$file);
}
$zip->close();
return file_exists($destination);
}else{
return false;
}
}else{
return false;
}
}
public function _create_zip_and_download($files, $files_path, $zip_filename) {
$zip = new ZipArchive();
if ($zip->open($zip_filename, ZipArchive::CREATE) === TRUE) {
foreach ($files as $file) {
$zip->addFile($files_path . $file, $files_path . $file);
}
$zip->close();
// Download the created zip file
header("Content-type: application/zip");
header("Content-Disposition: attachment; filename = $zipFileName");
header("Content-Length: ".filesize($zipFileName));
header("Pragma: no-cache");
header("Expires: 0");
readfile("$zipFileName");
exit;
}
}
public function agent_download_test() {
$this->autoRender = false;
$images = array(
'449216012020020404.pdf',
'453616012020020135.pdf'
);
$this->_download($images, FOLDER_DOCUMENTS);
/// echo $this->_create_zip_and_download($images, FOLDER_DOCUMENTS, 'download.zip');
}
}