| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace DNADesign\Elemental\Extensions; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use DNADesign\Elemental\Models\BaseElement; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use DNADesign\Elemental\Models\ElementalArea; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use SilverStripe\CMS\Model\SiteTree; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use SilverStripe\Control\Controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use SilverStripe\View\Parsers\HTML4Value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use SilverStripe\View\SSViewer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * @method ElementalArea ElementalArea() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * @property int ElementalAreaID | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | class ElementalPageExtension extends ElementalAreasExtension | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     private static $has_one = [ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |         'ElementalArea' => ElementalArea::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     private static $owns = [ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         'ElementalArea', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     private static $cascade_duplicates = [ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         'ElementalArea', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * The delimiter to separate distinct elements in indexed content. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * When using the getElementsForSearch() method to index all elements in a single field, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * a custom delimiter can be used help to avoid false positive results for phrase queries. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * @config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     private static $search_index_element_delimiter = ' '; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * Returns the contents of each ElementalArea has_one's markup for use in Solr or Elastic search indexing | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @return string | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 45 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |     public function getElementsForSearch() | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |         $oldThemes = SSViewer::get_themes(); | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |         SSViewer::set_themes(SSViewer::config()->get('themes')); | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |         try { | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |             $output = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |             $this->loopThroughElements(function(BaseElement $element) use (&$output) { | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |                 if ($element->getSearchIndexable()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |                     $content = $element->getContentForSearchIndex(); | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |                     if ($content) { | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |                         $output[] = $content; | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |                     } | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |             }); | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |         } finally { | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |             // Reset theme if an exception occurs, if you don't have a | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |             // try / finally around code that might throw an Exception, | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |             // CMS layout can break on the response. (SilverStripe 4.1.1) | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |             SSViewer::set_themes($oldThemes); | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |         return implode($this->owner->config()->get('search_index_element_delimiter') ?? '', $output); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * @param array $anchors | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * @see SiteTree::getAnchorsOnPage() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     public function updateAnchorsOnPage(array &$anchors): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         if (!($this->owner instanceof SiteTree)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         $this->loopThroughElements(function(BaseElement $element) use (&$anchors) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             $anchors = array_merge($anchors, $element->getAnchorsInContent()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     public function MetaTags(&$tags) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         if (!Controller::has_curr()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         $controller = Controller::curr(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         $request = $controller->getRequest(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         if ($request->getVar('ElementalPreview') !== null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             $html = HTML4Value::create($tags); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             $xpath = "//meta[@name='x-page-id' or @name='x-cms-edit-link']"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |             $removeTags = $html->query($xpath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             $body = $html->getBody(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             foreach ($removeTags as $tag) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |                 $body->removeChild($tag); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             $tags = $html->getContent(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      * Call some function over all elements belonging to this page | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     private function loopThroughElements(callable $callback): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         foreach ($this->owner->hasOne() as $key => $class) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |             if ($class !== ElementalArea::class) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |             /** @var ElementalArea $area */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |             $area = $this->owner->$key(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |             if ($area) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |                 foreach ($area->Elements() as $element) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |                     $callback($element); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 121 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 122 |  |  |  |