Completed
Push — master ( 3c0a16...506e36 )
by Robbie
10s
created

ElementalPageExtension   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 49
rs 10
c 0
b 0
f 0
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A MetaTags() 0 13 3
A getElementsForSearch() 0 15 4
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
9
class ElementalPageExtension extends ElementalAreasExtension
10
{
11
    private static $has_one = [
0 ignored issues
show
introduced by
The private property $has_one is not used, and could be removed.
Loading history...
12
        'ElementalArea' => ElementalArea::class,
13
    ];
14
15
    private static $owns = [
0 ignored issues
show
introduced by
The private property $owns is not used, and could be removed.
Loading history...
16
        'ElementalArea',
17
    ];
18
19
    private static $cascade_duplicates = [
0 ignored issues
show
introduced by
The private property $cascade_duplicates is not used, and could be removed.
Loading history...
20
        'ElementalArea',
21
    ];
22
23
    /**
24
     * Returns the contents of each ElementalArea has_one's markup for use in Solr search indexing
25
     *
26
     * @return string
27
     */
28
    public function getElementsForSearch()
29
    {
30
        $output = [];
31
        foreach ($this->owner->hasOne() as $key => $class) {
32
            if ($class !== ElementalArea::class) {
33
                continue;
34
            }
35
36
            /** @var ElementalArea $area */
37
            $area = $this->owner->$key();
38
            if ($area) {
39
                $output[] = strip_tags($area->forTemplate());
40
            }
41
        }
42
        return implode($output);
43
    }
44
45
    public function MetaTags(&$tags)
46
    {
47
        $controller = Controller::curr();
48
        $request = $controller->getRequest();
49
        if ($request->getVar('ElementalPreview') !== null) {
50
            $html = HTML4Value::create($tags);
51
            $xpath = "//meta[@name='x-page-id' or @name='x-cms-edit-link']";
52
            $removeTags = $html->query($xpath);
53
            $body = $html->getBody();
54
            foreach ($removeTags as $tag) {
55
                $body->removeChild($tag);
56
            }
57
            $tags = $html->getContent();
58
        }
59
    }
60
}
61