Passed
Push — 4 ( 529852...87c3e3 )
by Steve
10:18
created

ElementalPageExtension::getElementsForSearch()   B

Complexity

Conditions 7
Paths 22

Size

Total Lines 30
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 17
nc 22
nop 0
dl 0
loc 30
rs 8.8333
c 0
b 0
f 0
1
<?php
2
3
namespace DNADesign\Elemental\Extensions;
4
5
use DNADesign\Elemental\Models\ElementalArea;
6
use SilverStripe\Control\Controller;
7
use SilverStripe\View\Parsers\HTML4Value;
8
use SilverStripe\View\SSViewer;
9
10
/**
11
 * @method ElementalArea ElementalArea()
12
 * @property int ElementalAreaID
13
 */
14
class ElementalPageExtension extends ElementalAreasExtension
15
{
16
    private static $has_one = [
0 ignored issues
show
introduced by
The private property $has_one is not used, and could be removed.
Loading history...
17
        'ElementalArea' => ElementalArea::class,
18
    ];
19
20
    private static $owns = [
0 ignored issues
show
introduced by
The private property $owns is not used, and could be removed.
Loading history...
21
        'ElementalArea',
22
    ];
23
24
    private static $cascade_duplicates = [
0 ignored issues
show
introduced by
The private property $cascade_duplicates is not used, and could be removed.
Loading history...
25
        'ElementalArea',
26
    ];
27
28
    /**
29
     * The delimiter to separate distinct elements in indexed content.
30
     *
31
     * When using the getElementsForSearch() method to index all elements in a single field,
32
     * a custom delimiter can be used help to avoid false positive results for phrase queries.
33
     *
34
     * @config
35
     * @var string
36
     */
37
    private static $search_index_element_delimiter = ' ';
0 ignored issues
show
introduced by
The private property $search_index_element_delimiter is not used, and could be removed.
Loading history...
38
39
    /**
40
     * Returns the contents of each ElementalArea has_one's markup for use in Solr or Elastic search indexing
41
     *
42
     * @return string
43
     */
44
    public function getElementsForSearch()
45
    {
46
        $oldThemes = SSViewer::get_themes();
47
        SSViewer::set_themes(SSViewer::config()->get('themes'));
48
        try {
49
            $output = [];
50
            foreach ($this->owner->hasOne() as $key => $class) {
51
                if ($class !== ElementalArea::class) {
52
                    continue;
53
                }
54
                /** @var ElementalArea $area */
55
                $area = $this->owner->$key();
56
                if ($area) {
57
                    foreach ($area->Elements() as $element) {
58
                        if ($element->getSearchIndexable()) {
59
                            $content = $element->getContentForSearchIndex();
60
                            if ($content) {
61
                                $output[] = $content;
62
                            }
63
                        }
64
                    }
65
                }
66
            }
67
        } finally {
68
            // Reset theme if an exception occurs, if you don't have a
69
            // try / finally around code that might throw an Exception,
70
            // CMS layout can break on the response. (SilverStripe 4.1.1)
71
            SSViewer::set_themes($oldThemes);
72
        }
73
        return implode($this->owner->config()->get('search_index_element_delimiter'), $output);
74
    }
75
76
    public function MetaTags(&$tags)
77
    {
78
        if (!Controller::has_curr()) {
79
            return;
80
        }
81
        $controller = Controller::curr();
82
        $request = $controller->getRequest();
83
        if ($request->getVar('ElementalPreview') !== null) {
84
            $html = HTML4Value::create($tags);
85
            $xpath = "//meta[@name='x-page-id' or @name='x-cms-edit-link']";
86
            $removeTags = $html->query($xpath);
87
            $body = $html->getBody();
88
            foreach ($removeTags as $tag) {
89
                $body->removeChild($tag);
90
            }
91
            $tags = $html->getContent();
92
        }
93
    }
94
}
95