Completed
Pull Request — master (#70)
by
unknown
06:39
created

dPublishState()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 0
1
<?php
2
3
/**
4
 * Virtual Linked Element.
5
 *
6
 * As elemental is based on widgets which have a natural has_one relation to an
7
 * object, this is a workaround for allowing the same element to be linked to
8
 * multiple pages.
9
 *
10
 * {@see ElementalGridFieldAddExistingAutocompleter}
11
 *
12
 * @package elemental
13
 */
14
class ElementVirtualLinked extends BaseElement
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...
15
{
16
    /**
17
     * @var string
18
     */
19
    private static $title = 'Virtual linked Block';
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $title is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
20
21
    /**
22
     * @var string
23
     */
24
    private static $singular_name = 'Virtual linked Block';
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $singular_name is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
25
26
    private static $has_one = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $has_one is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
27
        'LinkedElement' => 'BaseElement'
28
    );
29
30
    public function getTitle()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
31
    {
32
        return $this->LinkedElement()->getTitle();
0 ignored issues
show
Bug introduced by
The method LinkedElement() does not exist on ElementVirtualLinked. Did you maybe mean getVirtualLinkedElements()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
33
    }
34
35
    public function i18n_singular_name()
36
    {
37
        return _t(__CLASS__, $this->LinkedElement()->config()->title);
0 ignored issues
show
Bug introduced by
The method LinkedElement() does not exist on ElementVirtualLinked. Did you maybe mean getVirtualLinkedElements()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
38
    }
39
40
    public function __construct($record = null, $isSingleton = false, $model = null)
41
    {
42
        parent::__construct($record, $isSingleton, $model);
43
        $this->LinkedElement()->setVirtualOwner($this);
0 ignored issues
show
Bug introduced by
The method LinkedElement() does not exist on ElementVirtualLinked. Did you maybe mean getVirtualLinkedElements()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
44
    }
45
46
    public function getCMSFields()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
47
    {
48
        $message = sprintf(
49
            '<p>%s</p><p><a href="%2$s">%2$s</a></p>',
50
            _t('ElementVirtualLinked.DESCRIBE', 'This is a virtual copy of a block. To edit, visit'),
51
            $this->LinkedElement()->getEditLink()
0 ignored issues
show
Bug introduced by
The method LinkedElement() does not exist on ElementVirtualLinked. Did you maybe mean getVirtualLinkedElements()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
52
        );
53
54
        $fields = new FieldList(
55
            new TabSet('Root', $main = new Tab('Main'))
56
        );
57
58
        if ($this->isInvalidPublishState()) {
59
            $warning = 'Error: The original block is not published. This block will not work on the live site until you click the link below and publish it.';
60
            $main->push(new LiteralField('WarningHeader', '<p class="message error">' .$warning. '</p>'));
61
        }
62
        $main->push(new LiteralField('Existing', $message));
63
64
        $this->extend('updateCMSFields', $fields);
65
66
        return $fields;
67
    }
68
69
70
    /**
71
     * Detect when a user has published a ElementVirtualLinked block
72
     * but has not published the LinkedElement block.
73
     */
74
    public function isInvalidPublishState()
75
    {
76
        $block = $this->LinkedElement();
0 ignored issues
show
Bug introduced by
The method LinkedElement() does not exist on ElementVirtualLinked. Did you maybe mean getVirtualLinkedElements()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
77
        return (!$block->isPublished() && $this->isPublished());
0 ignored issues
show
Documentation Bug introduced by
The method isPublished does not exist on object<ElementVirtualLinked>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
78
    }
79
80
    public function getCMSPublishedState()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
81
    {
82
        if ($this->isInvalidPublishState()) {
83
            $colour = '#C00';
84
            $text = 'Error';
85
            $html = new HTMLText('PublishedState');
86
            $html->setValue(sprintf(
87
                '<span style="color: %s;">%s</span>',
88
                $colour,
89
                htmlentities($text)
90
            ));
91
            return $html;
92
        }
93
94
        $publishedState = null;
95
        foreach ($this->extension_instances as $instance) {
96
            if (method_exists($instance, 'getCMSPublishedState')) {
97
                $instance->setOwner($this);
98
                $publishedState = $instance->getCMSPublishedState();
99
                $instance->clearOwner();
100
                break;
101
            }
102
        }
103
        return $publishedState;
104
    }
105
}
106
107
class ElementVirtualLinked_Controller extends BaseElement_Controller
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
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...
108
{
109
110
    protected $controllerClass;
111
112
    public function __construct($widget)
113
    {
114
        parent::__construct($widget);
115
116
        $controllerClass = get_class($this->LinkedElement()) . '_Controller';
0 ignored issues
show
Documentation Bug introduced by
The method LinkedElement does not exist on object<ElementVirtualLinked_Controller>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
117
        if (class_exists($controllerClass)) {
118
            $this->controllerClass = $controllerClass;
119
        } else {
120
            $this->controllerClass = 'BaseElement_Controller';
121
        }
122
    }
123
124
    /**
125
     * Returns the current widget in scope rendered into its' holder
126
     *
127
     * @return HTML
0 ignored issues
show
Documentation introduced by
Should the return type not be HTMLText?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
128
     */
129
    public function WidgetHolder()
130
    {
131
        return $this->renderWith("ElementHolder_VirtualLinked");
132
    }
133
134
    public function __call($method, $arguments)
135
    {
136
        try {
137
            $retVal = parent::__call($method, $arguments);
138
        } catch (Exception $e) {
139
            $controller = new $this->controllerClass($this->LinkedElement());
0 ignored issues
show
Documentation Bug introduced by
The method LinkedElement does not exist on object<ElementVirtualLinked_Controller>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
140
            $retVal = call_user_func_array(array($controller, $method), $arguments);
141
        }
142
        return $retVal;
143
    }
144
145 View Code Duplication
    public function hasMethod($action)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
146
    {
147
        if (parent::hasMethod($action)) {
148
            return true;
149
        }
150
151
        $controller = new $this->controllerClass($this->LinkedElement());
0 ignored issues
show
Documentation Bug introduced by
The method LinkedElement does not exist on object<ElementVirtualLinked_Controller>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
152
        return $controller->hasMethod($action);
153
    }
154
155 View Code Duplication
    public function hasAction($action)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
156
    {
157
        if (parent::hasAction($action)) {
158
            return true;
159
        }
160
161
        $controller = new $this->controllerClass($this->LinkedElement());
0 ignored issues
show
Documentation Bug introduced by
The method LinkedElement does not exist on object<ElementVirtualLinked_Controller>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
162
        return $controller->hasAction($action);
163
    }
164
165 View Code Duplication
    public function checkAccessAction($action)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
166
    {
167
        if (parent::checkAccessAction($action)) {
168
            return true;
169
        }
170
171
        $controller = new $this->controllerClass($this->LinkedElement());
0 ignored issues
show
Documentation Bug introduced by
The method LinkedElement does not exist on object<ElementVirtualLinked_Controller>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
172
        return $controller->checkAccessAction($action);
173
    }
174
}
175