/**
  * Returns true if 'value' is found in array 'array'
  *
  * Expected Params:
  *  value string The item to search for in the array
  *  array array  The array to search
  *
  * @return boolean true if value is in array
  */
 public function inArray()
 {
     $needle = $this->getParameter('value');
     $haystack = $this->getParameter('array');
     if ($haystack instanceof Meta) {
         $haystack = $haystack->getMetaValue();
     }
     if (is_string($haystack)) {
         $haystack = StringUtils::smartExplode($haystack);
     }
     return in_array($needle, (array) $haystack);
 }
 protected function _xmodule()
 {
     $element = $this->ElementService->getBySlug($this->globals['INPUT_ELEMENT']);
     $ignoreAspects = array();
     $ignore = $this->getParameter('ignore');
     if (!empty($ignore)) {
         $ignoreAspects = StringUtils::smartExplode($ignore);
     }
     $aspects = $element->getAspects();
     $ordered = array();
     foreach ((array) $aspects as $aspect) {
         if (in_array($aspect['Slug'], $ignoreAspects)) {
             continue;
         }
         $plugin = $this->PluginService->getByID($aspect['PluginID']);
         $ordered[$plugin->Priority][] = $aspect;
     }
     ksort($ordered);
     foreach ($ordered as $priority => $aspects) {
         foreach ($aspects as $aspect) {
             $template = $this->getTemplate($aspect);
             if ($this->TemplateService->fileExists($template)) {
                 $this->xhtml[] = StringUtils::l("{% template {$template}?inherit=true %}");
             }
         }
     }
     $str = StringUtils::l("{% begin contents %}");
     if (!empty($this->js)) {
         $str .= StringUtils::l('<script type="text/javascript">');
         $str .= StringUtils::l();
         $str .= StringUtils::l('    $(document).ready(function() {');
         $str .= StringUtils::l();
         foreach ((array) $this->js as $line) {
             $str .= StringUtils::l($line);
         }
         $str .= StringUtils::l();
         $str .= StringUtils::l('    });');
         $str .= StringUtils::l();
         $str .= StringUtils::l('</script>');
     }
     if (!empty($this->xhtml)) {
         foreach ((array) $this->xhtml as $line) {
             $str .= StringUtils::l($line);
         }
     }
     $str .= StringUtils::l("{% end %}");
     return $str;
 }
 public function delete(NodeRef $nodeRef)
 {
     if (!$nodeRef->isFullyQualified()) {
         throw new Exception('Cannot delete node from index without fully-qualified NodeRef');
     }
     if (substr($this->Elements, 0, 1) == '@') {
         if (!$nodeRef->getElement()->hasAspect($this->Elements)) {
             return;
         }
     } else {
         if (!in_array($nodeRef->getElement()->getSlug(), StringUtils::smartExplode($this->Elements))) {
             return;
         }
     }
     if (array_key_exists('' . $nodeRef, $this->markedForReindex)) {
         unset($this->markedForReindex['' . $nodeRef]);
     }
     if (array_key_exists('' . $nodeRef, $this->markedForDeletion)) {
         return;
     }
     $this->markedForDeletion['' . $nodeRef] = $nodeRef;
     $this->bindReindex();
 }
 public function normalizeNodeQuery(NodeQuery $nodeQuery)
 {
     if ($nodeQuery->getParameter('NodeQuery.normalized') == null) {
         $this->Events->trigger('Node.normalizeQuery', $nodeQuery);
         $nodeQuery->setParameter('NodeQuery.normalized', true);
     }
     //
     //        if($nodeQuery->hasParameter('NodePartials.eq') && $nodeQuery->hasParameter('NodeRefs.normalized') && $nodeQuery->hasParameter('NodeRefs.fullyQualified'))
     //            return $nodeQuery;
     if (!$nodeQuery->hasParameter('NodePartials.eq')) {
         $nodeQuery->setParameter('NodePartials.eq', new NodePartials($nodeQuery->getParameter('Meta.select'), $nodeQuery->getParameter('OutTags.select'), $nodeQuery->getParameter('InTags.select')));
     }
     // NODEREFS
     if ($nodeQuery->hasParameter('NodeRefs.in')) {
         $noderefsin = $nodeQuery->getParameter('NodeRefs.in');
         if (is_string($noderefsin)) {
             $nodeRefsIn = array();
             $noderefsin = explode(',', $noderefsin);
             foreach ($noderefsin as $noderefstr) {
                 $noderef = $this->parseFromString(trim($noderefstr));
                 if (!empty($noderef)) {
                     $nodeRefsIn[] = $noderef;
                 }
             }
         } else {
             $nodeRefsIn = $noderefsin;
         }
         $nodeQuery->setParameter('NodeRefs.normalized', $nodeRefsIn);
         if (!$nodeQuery->hasParameter('NodeRefs.fullyQualified')) {
             $nodeQuery->setParameter('NodeRefs.fullyQualified', true);
         }
         return $nodeQuery;
         //            return array($nodeRefsIn, $nodePartials,
         //                $nodeQuery->hasParameter('NodeRefs.fullyQualified')?$nodeQuery->getParameter('NodeRefs.fullyQualified'):true);
     }
     $nodeRefs = array();
     if (!$nodeQuery->hasParameter('Elements.in')) {
         throw new NodeException('Unable to query nodes without Elements.in specified');
     }
     $elementSlugs = $nodeQuery->getParameter('Elements.in');
     if (!is_array($elementSlugs)) {
         $elementSlugs = StringUtils::smartExplode($elementSlugs);
     }
     //        $firstElement = null;
     $sites = array();
     if ($nodeQuery->hasParameter('Sites.in')) {
         $siteSlugs = $nodeQuery->getParameter('Sites.in');
         if (!is_array($siteSlugs)) {
             $siteSlugs = StringUtils::smartExplode($siteSlugs);
         }
         $sites = $this->SiteService->multiGetBySlug($siteSlugs);
     }
     $elements = array();
     foreach ($elementSlugs as $ek => $elementSlug) {
         if (substr($elementSlug, 0, 1) == '@') {
             $aspectSlug = substr($elementSlug, 1);
             $aspect = $this->AspectService->getBySlug($aspectSlug);
             if ($aspect->ElementMode == 'one' || $aspect->ElementMode == 'anchored') {
                 $elements = array_merge($elements, (array) $this->ElementService->findAllWithAspect($aspectSlug));
                 unset($elementSlugs[$ek]);
             } else {
                 if (!empty($sites)) {
                     foreach ((array) $sites as $site) {
                         $elements = array_merge($elements, (array) $this->ElementService->findAllWithAspect($aspectSlug, $site->getSlug()));
                     }
                 } else {
                     $elements = array_merge($elements, (array) $this->ElementService->findAllWithAspect($aspectSlug));
                 }
             }
         } else {
             $elements[] = $this->ElementService->getBySlug($elementSlug);
         }
     }
     if (empty($elements)) {
         throw new NoElementsException('No elements found for expression [' . $nodeQuery->getParameter('Elements.in') . '].');
     }
     $allFullyQualified = false;
     //        $allSites = array();
     //        if($nodeQuery->hasParameter('Sites.in')) {
     //            $siteSlugs = $nodeQuery->getParameter('Sites.in');
     //            if(!is_array($siteSlugs))
     //                $siteSlugs = StringUtils::smartExplode($siteSlugs);
     //
     //            $allSites = $this->SiteService->multiGetBySlug($siteSlugs);
     //        } else if ($nodeQuery->hasParameter('SiteIDs.in')) {
     //            $siteIDs = $nodeQuery->getParameter('SiteIDs.in');
     //            if(!is_array($siteIDs))
     //                $siteIDs = StringUtils::smartExplode($siteIDs);
     //
     //            foreach($siteIDs as $siteID)
     //                $allSites[] = $this->SiteService->getByID($siteID);
     //
     //        } else {
     //            $allSites = $this->SiteService->findAll()->getResults();
     //        }
     foreach ($elements as $element) {
         //            if($element->isAnchored() && $nodeQuery->getParameter('Elements.ignoreAnchoredSite') == null){
         //                $sites = array($element->getAnchoredSite());
         //            } else {
         //                $sites = $allSites;
         //            }
         //            foreach($sites as $site)
         //            {
         if (($slugs = $nodeQuery->getParameter('Slugs.in')) != null) {
             if (!is_array($slugs)) {
                 $slugs = StringUtils::smartExplode($slugs);
             }
             $allFullyQualified = true;
             foreach ($slugs as $slug) {
                 $nodeRefs[] = new NodeRef($element, SlugUtils::createSlug($slug, true));
             }
         } else {
             $nodeRefs[] = new NodeRef($element);
         }
         //            }
     }
     $nodeQuery->setParameter('NodeRefs.normalized', $nodeRefs);
     //if(!$nodeQuery->hasParameter('NodeRefs.fullyQualified'))
     $nodeQuery->setParameter('NodeRefs.fullyQualified', $allFullyQualified);
     //return array($nodeRefs, $nodePartials, $allFullyQualified);
     return $nodeQuery;
 }
 /**
  * Recursive function that build the menu tree for each menu item.
  *
  * @param CMSNavItem $parent        The parent item
  * @param string     $classes       Any css classes that should be applied to the items generated
  * @param string     $format_string A format string that determines how menu items will appear in the html
  * @param boolean    $is_top_level  If TRUE, we're processing the top level items
  *
  * @return string
  */
 private function _buildMenuTree($parent, $classes = '', $format_string = '%s', $is_top_level = false)
 {
     // Permissions check
     if (!empty($parent->Permissions)) {
         $permissions = StringUtils::smartExplode($parent->Permissions);
         foreach ($permissions as $perm) {
             if (!$this->Permissions->checkPermission(trim($perm))) {
                 return '';
             }
         }
     }
     if ($parent->Enabled == false) {
         return '';
     }
     $contains_current_uri = false;
     $content = '';
     if (($this->requestURI == $parent->URI || stripos($this->requestURI, $parent->URI) === 0) && !$this->matchedCurrentURI) {
         $contains_current_uri = true;
         $this->matchedCurrentURI = true;
     }
     if (!empty($parent->DoAddLinksFor)) {
         try {
             // Look up all items
             $add_links = array();
             $add_menu_items = StringUtils::smartExplode($parent->DoAddLinksFor);
             foreach ($add_menu_items as $element_or_aspect) {
                 $add_links = array_merge($add_links, $this->lookupElementsForNav(trim($element_or_aspect)));
             }
             $uri = $parent->URI;
             if (substr($uri, -1) != '/') {
                 $uri .= '/';
             }
             // Build a submenu with all items.
             if (count($add_links) == 1) {
                 $content .= "\t<a href='{$uri}{$add_links[0]['URI']}/'>";
                 $content .= sprintf($format_string, 'Add ' . $add_links[0]['Name']);
                 $content .= "</a>\n";
             } elseif (count($add_links) > 1) {
                 $content .= "\t<a class='daddy'>{$parent->Label}</a>\n";
                 $content .= "<ul>\n";
                 foreach ($add_links as $link) {
                     $content .= "<li><a href='{$uri}{$link['URI']}/'>{$link['Name']}</a></li>\n";
                 }
                 $content .= "</ul>\n";
             }
         } catch (Exception $e) {
         }
     } else {
         $content .= "\t<a href='{$parent->URI}'>";
         $content .= sprintf($format_string, $parent->Label);
         $content .= "</a>\n";
         $ccontent = '';
         if (count($parent->Children) > 0) {
             foreach ($parent->Children as $kid) {
                 $res = $this->_buildMenuTree($kid);
                 if (!empty($res)) {
                     list($child_content, $child_has_uri) = $res;
                     if ($child_has_uri) {
                         $contains_current_uri = true;
                     }
                     $ccontent .= $child_content;
                 }
             }
         }
         if (!empty($ccontent)) {
             $content .= "<ul>\n";
             $content .= $ccontent;
             $content .= "</ul>\n";
         }
     }
     $item = "<li id='nav-{$parent->Slug}' class='{$classes}'>\n{$content}</li>\n";
     if (!$is_top_level) {
         return array($item, $contains_current_uri);
     } else {
         return "<li id='nav-{$parent->Slug}' class='{$classes} " . ($contains_current_uri ? "selected" : "") . "'>\n{$content}</li>\n";
     }
 }
 protected function purgeAll()
 {
     $interval = 1000;
     // Allow the caller to specify a delay in ms between purge calls.
     // (to keep database load to a minimum if running this over a long period of time...)
     $delay = $this->Request->getParameter('delay');
     if (!empty($delay)) {
         $delay = intval($delay) * 1000;
     }
     $elements = array();
     $elementSlugs = $this->Request->getRequiredParameter('elements');
     if ($elementSlugs == 'all') {
         $elements = $this->ElementService->findAll()->getResults();
     } else {
         $elementSlugs = StringUtils::smartExplode($elementSlugs);
         foreach ($elementSlugs as $slug) {
             $elements[] = $this->ElementService->getBySlug($slug);
         }
     }
     // Support passing a list of element slugs that will not be purged, even if the "elements" attribute is "all".
     $ignoreElementSlugs = $this->Request->getParameter('ignore_elements');
     if (!empty($ignoreElementSlugs)) {
         $ignoreElementSlugs = StringUtils::smartExplode($ignoreElementSlugs);
         foreach ($ignoreElementSlugs as $slug) {
             // Do this step just to validate that the user has passed in real element slugs and not made any typos.
             $this->ElementService->getBySlug($slug);
         }
     }
     foreach ($elements as $element) {
         // Skip over any elements we've been told to ignore.
         if (!empty($ignoreElementSlugs) && in_array($element->getSlug(), $ignoreElementSlugs)) {
             continue;
         }
         $nq = new NodeQuery();
         $nq->setParameter('NodeRefs.only', true);
         $nq->setParameter('Elements.in', $element->getSlug());
         $nq->setParameter('Status.eq', 'deleted');
         $nq->setLimit($interval);
         $nq = $this->NodeService->findAll($nq, true);
         $nodes = $nq->getResults();
         $count = 0;
         while (count($nodes) > 0) {
             foreach ($nodes as $nodeRef) {
                 ++$count;
                 try {
                     $this->NodeService->purge($nodeRef);
                     echo $count, ". " . $nodeRef, " purged\n";
                     if ($count % 10 == 0) {
                         $this->TransactionManager->commit()->begin();
                         echo "commit\n";
                     }
                 } catch (Exception $e) {
                     echo "Exception caught: " . $e->getMessage() . "\n";
                 }
                 unset($nodeRef);
                 if ($delay > 0) {
                     usleep($delay);
                 }
             }
             $this->TransactionManager->commit()->begin();
             $nq->clearResults();
             $nodes = $this->NodeService->findAll($nq, true)->getResults();
         }
     }
 }
 public function populateDefaults($elements = 'all')
 {
     $log = '';
     if ($elements == 'all') {
         $elements = $this->ElementService->findAll()->getResults();
     } else {
         $elementSlugs = StringUtils::smartExplode($elements);
         $elements = array();
         foreach ($elementSlugs as $eSlug) {
             $elements[] = $this->ElementService->getBySlug($eSlug);
         }
     }
     foreach ($elements as $element) {
         $offset = 0;
         $nq = new NodeQuery();
         $nq->setParameter('Elements.in', $element->getSlug());
         $nq->setParameter('Meta.select', 'all');
         $nq->setLimit(1000);
         $nq->setOffset($offset);
         $nq->asObjects();
         $nq->isRetrieveTotalRecords(true);
         $nq = $this->NodeService->findAll($nq, true);
         $nodes = $nq->getResults();
         $tCount = $nq->getTotalRecords();
         $count = 0;
         while (count($nodes) > 0) {
             foreach ($nodes as $node) {
                 $this->NodeMapper->defaultsOnNode($node);
                 $this->NodeService->edit($node);
                 ++$count;
             }
             $offset += 1000;
             $nq = new NodeQuery();
             $nq->setParameter('Elements.in', $element->getSlug());
             $nq->setParameter('Meta.select', 'all');
             $nq->setLimit(1000);
             $nq->setOffset($offset);
             $nq->asObjects();
             $nodes = $this->NodeService->findAll($nq)->getResults();
         }
         $log .= 'Updated defaults on ' . $count . ' of ' . $tCount . ' ' . $element->getSlug() . " nodes.\n";
     }
     return $log;
 }