public function editAction() { $types = $this->config->fa->upload_types->toArray(); $id = (int) $this->getParam('id'); if ($id !== 0) { // Edit existing record. // TODO: Reimplement administrator access. $record = Upload::getRepository()->findOneBy(array('id' => $id, 'user_id' => $this->user->id)); if (!$record instanceof Upload) { throw new \FA\Exception('Submission ID not found!'); } $edit_mode = true; $type = $record->submission_type; } else { // Create new submission. $type = $this->getParam('type'); // Show type selector if no type is specified. if (empty($type) || !isset($types[$type])) { $this->view->types = $types; return $this->view->pick('uploads/select'); } $edit_mode = false; $record = NULL; } $type_info = $types[$type]; $form_config = $this->current_module_config->forms->uploads_edit->toArray(); // Create mode changes if (!$edit_mode) { $form_config['groups']['files']['elements']['submission'][1]['required'] = true; unset($form_config['groups']['files']['elements']['submission'][1]['description']); unset($form_config['groups']['files']['elements']['rebuild_thumbnail']); } // Changes to the form based on submission type. if (isset($type_info['category'])) { $form_config['groups']['metadata']['elements']['category'][1]['default'] = $type_info['category']; } if ($type !== Upload::TYPE_IMAGE) { unset($form_config['groups']['files']['elements']['rebuild_thumbnail']); } $form_config['groups']['files']['elements']['submission'][1]['allowedTypes'] = $type_info['types']; // Create the form class. $form = new \FA\Form($form_config); // Populate the form (if available). if ($record instanceof Upload) { $form->setDefaults($record->toArray(TRUE, TRUE)); } // Handle form submission. if ($_POST && $this->request->hasFiles() && $form->isValid(array_merge($_POST, $_FILES))) { $data = $form->getValues(); if (!$record instanceof Upload) { $record = new Upload(); $record->upload_type = $type; $record->user = $this->user; $record->is_hidden = true; // Hide until properly populated. $record->save(); // Immediately save to generate IDs used in next steps. } $record->fromArray($data); // Begin file handling. \IMagick::setResourceLimit(\Imagick::RESOURCETYPE_MEMORY, 32); \IMagick::setResourceLimit(\Imagick::RESOURCETYPE_MAP, 32); $files = $form->getFiles($this->request); $imagine = new Imagine(); $submission_file = $files['submission'][0]; $thumbnail_file = null; $thumbnail_paths = array(); $preview_file = null; $preview_paths = array(); if ($submission_file) { $submission_paths = $record->generatePaths($submission_file->getName()); // Create the proper artwork directory if it doesn't exist. $submission_dir = dirname($submission_paths['full']['path']); @mkdir($submission_dir); if ($type == Upload::TYPE_IMAGE) { // Handle image uploads. $submission_image = $imagine->open($submission_file->getTempName()); $is_animated = count($submission_image->layers()) > 1; // So, it seems Imagine really loves to screw up GIFs, so lets avoid that if ($is_animated) { $dest_path = $submission_paths['full']['path']; // Copying this instead of moving due to the file being reused by preview/thumbnail copy($submission_file->getTempName(), $dest_path); } else { $submission_image->save($submission_paths['full']['path'], array('animated' => $is_animated)); } // Make this file the thumbnail if no other is specified. if (empty($files['thumbnail']) && (!$edit_mode || $data['rebuild_thumbnail'])) { $thumbnail_file = $submission_file; $thumbnail_paths = $submission_paths; } // Set up the preview parameters $preview_file = $submission_file; $preview_paths = $submission_paths; } else { // Handle non-images. Way simpler, right? $dest_path = $submission_paths['full']['path']; $submission_file->moveTo($dest_path); // Prevent the file from being deleted below. $submission_file = null; } $record->setFull($submission_paths['full']['base']); } // Use the thumbnail field if supplied. if (!empty($files['thumbnail'])) { $thumbnail_file = $files['thumbnail'][0]; $thumbnail_paths = $record->generatePaths($thumbnail_file->getName()); } // If we haven't set a preview image/path, then use the thumbnail if possible if (is_null($preview_file)) { $preview_file = $thumbnail_file; $preview_paths = $thumbnail_paths; } // Process either the uploaded thumbnail, or resize the original file to be our preview. if ($preview_file) { // Generate "small" size thumbnail. $preview_size = new Box(self::MAX_PREVIEW_SIZE, self::MAX_PREVIEW_SIZE); self::_saveAsJPG($imagine, $preview_file, $preview_paths['small']['path'], 90, $preview_size); $record->setSmall(self::_changeExtension($preview_paths['small']['base'], 'jpg')); } // Process either the uploaded thumbnail, or thumbnailize the original file. if ($thumbnail_file) { // Generate "thumb" size thumbnail. $thumbnail_size = new Box(self::MAX_THUMB_SIZE, self::MAX_THUMB_SIZE); self::_saveAsJPG($imagine, $thumbnail_file, $thumbnail_paths['thumbnail']['path'], 90, $thumbnail_size); $record->setThumbnail(self::_changeExtension($thumbnail_paths['thumbnail']['base'], 'jpg')); } // Delete the temp files (if not already moved). if ($submission_file) { @unlink($submission_file->getTempName()); } if ($thumbnail_file) { @unlink($thumbnail_file->getTempName()); } // Unhide the record that was hidden earlier. if (!$edit_mode) { $record->is_hidden = false; } $record->save(); $view_url = $this->url->get('view/' . $record->id); $view_link = 'You can <a href="' . $view_url . '" target="_blank_">view your submission\'s public page here</a>.'; if ($edit_mode) { $this->alert('<b>Submission Updated!</b><br>' . $view_link, 'green'); } else { $this->alert('<b>New Submission Uploaded!</b><br>' . $view_link, 'green'); } return $this->redirectFromHere(array('action' => 'index', 'id' => NULL, 'type' => NULL)); } // Render the main form. $this->view->type_info = $type_info; $this->view->form = $form; }