Completed
Push — master ( 7bac57...c292ae )
by Nicolaas
02:02
created

CMSEditLinkField::setNameAppendix()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
4
class CMSEditLinkField extends ReadonlyField
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
5
{
6
7
    /**
8
     * what are we linking to?
9
     * @var DataObject
10
     */
11
    protected $linkedObject = null;
12
13
    /**
14
     * appendix for field name
15
     * @var String
16
     */
17
    protected $nameAppendix = '_CMSEditLink';
18
19
20
    /**
21
     *
22
     * @param string $name                 e.g. MyLinkedObjectID
23
     * @param string $title                e.g. My Fancy Title
24
     * @param DataObject $linkedObject     e.g. $this->MyLinkedObjectID()
25
     * @param string $methodOrVariable     (OPTIONAL) - e.g. MyFullTitle
26
     */
27
    public function __construct($name, $title, $linkedObject, $methodOrVariable = 'getTitle')
28
    {
29
        $name .= $this->nameAppendix;
30
        if ($linkedObject && $linkedObject->exists() && $linkedObject->hasMethod('CMSEditLink')) {
31
            $this->linkedObject = $linkedObject;
32
            if($this->linkedObject->hasMethod($methodOrVariable)) {
33
                $description = $this->linkedObject->$methodOrVariable();
34
            } elseif(isset($this->linkedObject->$methodOrVariable)) {
35
                $description = $this->linkedObject->$methodOrVariable;
36
            } else {
37
                $description = 'ERROR!';
38
                user_error($methodOrVariable.' does not exist on '.$this->linkedObject.' (as method or variable)');
39
            }
40
            $content = '<p class="cms-edit-link"><a href="'.$this->linkedObject->CMSEditLink().'">'.Convert::raw2xml($description).'</a></p>';
41
            $this->dontEscape = true;
42
43
            return parent::__construct($name, $title, $content);
0 ignored issues
show
Bug introduced by
Constructors do not have meaningful return values, anything that is returned from here is discarded. Are you sure this is correct?
Loading history...
44
        } else {
45
            return parent::__construct($name, $title);
0 ignored issues
show
Bug introduced by
Constructors do not have meaningful return values, anything that is returned from here is discarded. Are you sure this is correct?
Loading history...
46
        }
47
    }
48
49
    /**
50
     *
51
     *
52
     * @param string $s
53
     */
54
    function setNameAppendix($s)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
55
    {
56
        $this->nameAppendix = $s;
57
    }
58
59
60
}
61