public function replace() { $this->checkNonce(); if (empty($this->params['Title'])) { throw new Exception('Title parameter is required', 210); } if (empty($this->params['ElementSlug'])) { throw new Exception('ElementSlug parameter is required', 220); } $title = $this->params['Title']; $slug = SlugUtils::createSlug($title); $noderef = new NodeRef($this->ElementService->getBySlug($this->Request->getParameter('ElementSlug')), $slug); // create node $node = $noderef->generateNode(); $this->NodeMapper->defaultsOnNode($node); $node->Title = $title; $node->Slug = $slug; $this->getErrors()->throwOnError(); // edit existing record if ($this->RegulatedNodeService->refExists($noderef)) { $existing = $this->RegulatedNodeService->getByNodeRef($noderef); $existing->setNodePartials($node->getNodePartials()); $existing->setMetas($node->getMetas()); $existing->setOutTags($node->getOutTags()); $node = $this->RegulatedNodeService->edit($node); //create new record } else { $node = $this->RegulatedNodeService->quickAdd($node); } $node = $this->RegulatedNodeService->getByNodeRef($node->getNodeRef(), new NodePartials()); $this->bindToActionDatasource(array($node)); return new View($this->successView()); }
/** * Extracts files from the archive and stores the media as temporary-zipped-media nodes. * * @throws Exception * @param $params * @return array Array of created nodes */ protected function _extractArchive($params) { $nodes = array(); $workdir = FileSystemUtils::createWorkingDirectory(); try { list($sourceFile, $sourceFileName) = $this->getSourceFile($params); $zip = zip_open($sourceFile); if (is_resource($zip)) { while ($zip_entry = zip_read($zip)) { $entryFileName = basename(zip_entry_name($zip_entry)); $entryFile = rtrim($workdir, '/') . '/' . $entryFileName; if (preg_match('/^\\./', $entryFileName)) { continue; } $path = pathinfo($entryFile); if (empty($path['extension'])) { continue; } try { // Create the file if (zip_entry_open($zip, $zip_entry, 'r')) { if ($fd = @fopen($entryFile, 'w+')) { fwrite($fd, zip_entry_read($zip_entry, zip_entry_filesize($zip_entry))); fclose($fd); $nodeRef = new NodeRef($this->NodeRefService->oneFromAspect('@temporary-zipped-media')->getElement(), SlugUtils::createSlug($entryFileName)); $nodeRef = $this->NodeService->generateUniqueNodeRef($nodeRef, null, true); // create node $node = $nodeRef->generateNode(); $this->NodeMapper->defaultsOnNode($node); if ($node != null) { $node->Title = $entryFileName; $node->Slug = SlugUtils::createSlug($entryFileName); $this->getErrors()->throwOnError(); $this->RegulatedNodeService->quickAdd($node); $node = $this->RegulatedNodeService->getByNodeRef($node->getNodeRef(), new NodePartials()); // add the original media $file_params = $params; $file_params['File'] = $entryFileName; $file_params['NodeSlug'] = $node->Slug; $file_params['ElementSlug'] = 'temporary-zipped-media'; $file_params['_uploadedFiles']['file'] = new UploadedFile($entryFileName, $entryFile, zip_entry_filesize($zip_entry), FileSystemUtils::getMimetype($path['extension']), 0); $this->addOriginal($file_params); $this->_buildCmsThumbnail($node); $node = $this->RegulatedNodeService->getByNodeRef($node->getNodeRef(), new NodePartials('all', '#original.fields')); $nodes[] = $node; @unlink($entryFile); } } zip_entry_close($zip_entry); } } catch (Exception $e) { @unlink($entryFile); throw $e; } } zip_close($zip); } else { throw new Exception($this->_zipFileErrMsg($zip)); } } catch (Exception $e) { // clean up //@unlink($sourceFile); throw $e; } return $nodes; }