500, 'order' => array( array('Principle.short_name' => 'asc'), array('Attachment.name' => 'asc') ), 'conditions' => array('Attachment.archived' => 0) ); function index() { $this->Attachment->recursive = 1; $this->set('attachments', $this->paginate()); } function view($id = null) { if (!$id) { $this->Session->setFlash(__('Invalid Attachment.', true)); $this->redirect(array('action'=>'index')); } $this->set('attachment', $this->Attachment->read(null, $id)); $this->layout = 'pdf'; } function archived() { $this->set('archived', $this->Attachment->find('all', array('conditions'=> array('archived' => 1), 'order' => array( array('Principle.short_name' => 'asc'), array('Attachment.name' => 'asc') )))); } function add() { if (!empty($this->data)) { // Check if file was uploaded if (empty($this->data['Attachment']['file']['tmp_name'])) { $error = 'No file uploaded'; if (isset($this->data['Attachment']['file']['error'])) { $errorCodes = array( UPLOAD_ERR_INI_SIZE => 'File exceeds upload_max_filesize', UPLOAD_ERR_FORM_SIZE => 'File exceeds MAX_FILE_SIZE', UPLOAD_ERR_PARTIAL => 'File only partially uploaded', UPLOAD_ERR_NO_FILE => 'No file was uploaded', UPLOAD_ERR_NO_TMP_DIR => 'Missing temporary folder', UPLOAD_ERR_CANT_WRITE => 'Failed to write file to disk', UPLOAD_ERR_EXTENSION => 'File upload stopped by extension', ); $errorCode = $this->data['Attachment']['file']['error']; $error = isset($errorCodes[$errorCode]) ? $errorCodes[$errorCode] : 'Unknown error: ' . $errorCode; } $this->Session->setFlash(__('File upload error: ' . $error, true)); $principles = $this->Attachment->Principle->find('list'); $this->set(compact('products', 'principles')); return; } // Proxy the upload request to the Go application $goHost = getenv('GO_APP_HOST'); $goUrl = 'http://' . $goHost . '/go/attachments/upload'; // Prepare the multipart form data for the Go app $postFields = array(); $postFields['file'] = new CURLFile( $this->data['Attachment']['file']['tmp_name'], $this->data['Attachment']['file']['type'], $this->data['Attachment']['file']['name'] ); if (!empty($this->data['Attachment']['name'])) { $postFields['name'] = $this->data['Attachment']['name']; } if (!empty($this->data['Attachment']['description'])) { $postFields['description'] = $this->data['Attachment']['description']; } if (!empty($this->data['Attachment']['principle_id'])) { $postFields['principle_id'] = $this->data['Attachment']['principle_id']; } // Make the request to Go app $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $goUrl); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Accept: application/json' )); $response = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); $curlError = curl_error($ch); curl_close($ch); if ($httpCode == 201) { $this->Session->setFlash(__('The Attachment has been saved', true)); $this->redirect(array('action'=>'index')); } else { $errorMsg = 'The Attachment could not be saved.'; if ($curlError) { $errorMsg .= ' cURL Error: ' . $curlError; } elseif ($response) { $errorMsg .= ' Response: ' . $response; } else { $errorMsg .= ' HTTP Code: ' . $httpCode; } error_log('Attachment upload failed: ' . $errorMsg); $this->Session->setFlash(__($errorMsg, true)); } } $principles = $this->Attachment->Principle->find('list'); $this->set(compact('products', 'principles')); } function edit($id = null) { if (!$id && empty($this->data)) { $this->Session->setFlash(__('Invalid Attachment', true)); $this->redirect(array('action'=>'index')); } if (!empty($this->data)) { $attachment = $this->data; $existing = $this->Attachment->find('first', array('conditions'=>array('Attachment.id' => $this->data['Attachment']['id']))); //Process editing the PDFs. if(!empty($this->data['Attachment']['file']['tmp_name'])) { if(!empty($existing)) { //Delete old file unlink($existing['Attachment']['file']); } $attachment = $this->Attachment->process_attachment($this->data); if(!$attachment) { $this->Session->setFlash('The Attachment could not be saved. The filename exists'); } } else { $attachment['Attachment']['file'] = $existing['Attachment']['file']; } if ($this->Attachment->save($attachment)) { $this->Session->setFlash(__('The Attachment has been saved', true)); $this->redirect(array('action'=>'index')); } else { $this->Session->setFlash(__('The Attachment could not be saved. Please, try again.', true)); } } else { $this->data = $this->Attachment->read(null, $id); } $principles = $this->Attachment->Principle->find('list'); $this->set(compact('principles')); } function delete($id = null) { if (!$id) { $this->Session->setFlash(__('Invalid id for Attachment', true)); $this->redirect(array('action'=>'index')); } $attachment = $this->Attachment->read(null, $id); if ($this->Attachment->del($id)) { unlink($attachment['Attachment']['file']); $this->Session->setFlash(__('Attachment deleted', true)); $this->redirect(array('action'=>'index')); } } } ?>