public function tagGalleryPhotos()
 {
     $galleryNodeRef = $this->NodeRefService->parseFromString('media-gallery:test-gallery');
     $interval = 25;
     $offset = 0;
     $nq = new NodeQuery();
     $nq->setParameter('Elements.in', 'image');
     $nq->setParameter('NodeRefs.only', true);
     $nq->setOrderBy('ActiveDate', 'DESC');
     $nq->setLimit($interval);
     $nq->setOffset($offset);
     $nq = $this->NodeService->findAll($nq, true);
     $nodes = $nq->getResults();
     while (count($nodes) > 0) {
         foreach ($nodes as $node) {
             $tag = new Tag($node->getElement()->getSlug(), $node->getSlug(), '#media-gallery-items');
             try {
                 $this->NodeService->addOutTag($galleryNodeRef, $tag);
                 echo $node->Slug . "\n";
             } catch (Exception $e) {
                 echo $node->Slug . " error: " . $e->getMessage() . "\n";
             }
         }
         $offset = $offset + $interval;
         unset($nodes);
         $nq->setLimit($interval);
         $nq->setOffset($offset);
         $nq->clearResults();
         $nq = $this->NodeService->findAll($nq, true);
         $nodes = $nq->getResults();
     }
 }
 protected function getNodeRef()
 {
     $noderef = $this->Request->getParameter('nodeRef');
     if ($noderef == null) {
         $elementSlug = $this->Request->getParameter('ElementSlug');
         $nodeSlug = $this->Request->getParameter('NodeSlug');
     } else {
         if (($i = strpos($noderef, '#')) === FALSE) {
             return $this->NodeRefService->parseFromString($noderef);
         } else {
             return array($this->NodeRefService->parseFromString(substr($noderef, 0, $i)), substr($noderef, $i));
         }
     }
     if (empty($elementSlug) && empty($nodeSlug)) {
         return null;
     }
     $element = $this->ElementService->getBySlug($elementSlug);
     if ($nodeSlug == '') {
         $noderef = new NodeRef($element);
     } else {
         $noderef = new NodeRef($element, $nodeSlug);
     }
     return $noderef;
 }
 /**
  * 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;
 }