Completed
Push — master ( b4e5c2...6db701 )
by Daniel
05:37 queued 03:12
created

DMSGridFieldAddNewButton   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 7
dl 0
loc 74
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
C getHTMLFragments() 0 36 8
A setDocumentSetId() 0 5 1
A getDocumentSetId() 0 4 1
1
<?php
2
3
class DMSGridFieldAddNewButton extends GridFieldAddNewButton implements GridField_HTMLProvider
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...
4
{
5
    /**
6
     * The document set ID that the document should be attached to
7
     *
8
     * @var int
9
     */
10
    protected $documentSetId;
11
12
    /**
13
     * Overriding the parent method to change the template that the DMS add button will be rendered with
14
     *
15
     * @param  GridField $gridField
16
     * @return array
17
     */
18
    public function getHTMLFragments($gridField)
19
    {
20
        $singleton = singleton($gridField->getModelClass());
21
22
        if (!$singleton->canCreate()) {
23
            return array();
24
        }
25
26
        if (!$this->buttonName) {
27
            // provide a default button name, can be changed by calling {@link setButtonName()} on this component
28
            $objectName = $singleton->i18n_singular_name();
29
            $this->buttonName = _t('GridField.Add', 'Add {name}', array('name' => $objectName));
0 ignored issues
show
Documentation introduced by
array('name' => $objectName) is of type array<string,?,{"name":"?"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
30
        }
31
32
        $link = singleton('DMSDocumentAddController')->Link();
33
        if ($this->getDocumentSetId()) {
34
            $link = Controller::join_links($link, '?dsid=' . $this->getDocumentSetId());
35
36
            // Look for an associated page, but only share it if we're editing in a page context
37
            $set = DMSDocumentSet::get()->byId($this->getDocumentSetId());
38
            if ($set && $set->exists() && $set->Page()->exists()
39
                && Controller::curr() instanceof CMSPageEditController
40
            ) {
41
                $link = Controller::join_links($link, '?page_id=' . $set->Page()->ID);
42
            }
43
        }
44
45
        $data = new ArrayData(array(
46
            'NewLink' => $link,
47
            'ButtonName' => $this->buttonName,
48
        ));
49
50
        return array(
51
            $this->targetFragment => $data->renderWith('DMSGridFieldAddNewButton'),
52
        );
53
    }
54
55
    /**
56
     * Set the document set ID that this document should be attached to
57
     *
58
     * @param  int $id
59
     * @return $this
60
     */
61
    public function setDocumentSetId($id)
62
    {
63
        $this->documentSetId = $id;
64
        return $this;
65
    }
66
67
    /**
68
     * Get the document set ID that this document should be attached to
69
     *
70
     * @return int
71
     */
72
    public function getDocumentSetId()
73
    {
74
        return $this->documentSetId;
75
    }
76
}
77