/** * Upload a new file for this user if one is present in the form data, or if * not, check if one has been uploaded previously. If there's one known * about, return its name. Uses session to persist an uploaded file's * details between form submission attempts, in the case the overall form * doesn't validate. * * @todo Move somewhere else. * * @return array Array indicating boolean true for success plus and an * associative array with uploaded file information (or empty array if no * file), or boolean false for failure and an array of error messages. */ private function _uploadPersistentCompanyApplicationFile() { // For storing original filename intact $session = new Zend_Session_Namespace('homelet_connect_referencing'); $tempFile = "{$this->_params->connect->tempPrivatePath}companyApp_{$this->_agentSchemeNumber}_{$this->_agentId}"; // Is a new file being sent? $upload = new Zend_File_Transfer('http'); if ($upload->isUploaded()) { $upload->getValidator('Upload')->setMessages(array(Zend_Validate_File_Upload::INI_SIZE => 'The uploaded file size exceeds system maximum (' . ini_get('upload_max_filesize') . ')', Zend_Validate_File_Upload::FORM_SIZE => 'The uploaded file size exceeds the HTML form maximum', Zend_Validate_File_Upload::PARTIAL => 'The uploaded file was only partially uploaded', Zend_Validate_File_Upload::NO_FILE => 'No file was uploaded', Zend_Validate_File_Upload::NO_TMP_DIR => 'Missing a temporary folder', Zend_Validate_File_Upload::CANT_WRITE => 'Failed to write file to disk', Zend_Validate_File_Upload::EXTENSION => 'File upload stopped by extension', Zend_Validate_File_Upload::UNKNOWN => 'Unknown upload error')); $upload->addValidator('Count', true, 1); $upload->addValidator('Size', false, $this->_params->connect->companyapps->fileUpload->maxSize->file); $upload->getValidator('Size')->setMessages(array(Zend_Validate_File_Size::TOO_SMALL => 'File \'%value%\' below minimum size', Zend_Validate_File_Size::TOO_BIG => 'File \'%value%\' above maximum size')); $upload->addValidator('MimeType', false, $this->_params->connect->companyapps->fileUpload->mimeTypes); $upload->getValidator('MimeType')->setMessages(array(Zend_Validate_File_MimeType::FALSE_TYPE => 'File \'%value%\' of incorrect MIME type')); $upload->addValidator('Extension', true, $this->_params->connect->companyapps->fileUpload->extensions); $upload->getValidator('Extension')->setMessages(array(Zend_Validate_File_Extension::FALSE_EXTENSION => 'File \'%value%\' of incorrect extension')); if ($upload->isValid()) { // First delete any old file that may have been previously // uploaded $this->_deleteCompanyApplicationFile(); // Upload new one $session->companyAppFile->originalFilename = $upload->getFileName(null, false); $upload->addFilter('Rename', $tempFile); if ($upload->receive()) { $session->companyAppFile->uploadedFile = $tempFile; } else { unset($session->companyAppFile); } } else { // Send back validation messages return array(false, $upload->getMessages()); } } // Is there one stored, perhaps already? If yes, return original // filename $returnVal = array(); if (isset($session->companyAppFile->originalFilename)) { $returnVal = array('originalName' => $session->companyAppFile->originalFilename, 'pathToFile' => $tempFile); } return array(true, $returnVal); }