init()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 15

Duplication

Lines 15
Ratio 100 %

Importance

Changes 0
Metric Value
dl 15
loc 15
rs 9.7666
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 *@author nicolaas [at] sunnysideup.co.nz
4
 *@package: dataobjectsorter
5
 *@description: allows you to edit one field in one record
6
 *
7
 **/
8
9
class DataObjectOneFieldOneRecordUpdateController extends DataObjectSortBaseClass
10
{
11
    private static $allowed_actions = 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...
12
        "onefieldform" => 'DATA_OBJECT_SORT_AND_EDIT_PERMISSION',
13
        "show" => 'DATA_OBJECT_SORT_AND_EDIT_PERMISSION',
14
        "save" => 'DATA_OBJECT_SORT_AND_EDIT_PERMISSION'
15
    );
16
17
    /**
18
     *
19
     * make sure to also change in routes if you change this link
20
     * @var string
21
     */
22
    private static $url_segment = 'dataobjectonefieldonerecordupdate';
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...
23
24
    /**
25
     * get a link
26
     * @param  string $ClassName
27
     * @param  string $FieldName
28
     * @param  string $recordID
29
     *
30
     * @return string
31
     */
32
    public static function popup_link_only($ClassName, $FieldName, $recordID)
33
    {
34
        DataObjectSorterRequirements::popup_link_requirements();
35
        return Injector::inst()->get('DataObjectOneFieldOneRecordUpdateController')
36
            ->Link('show/'.$ClassName."/".$FieldName).'?id='.$recordID;
37
    }
38
39
    /**
40
     * get a link
41
     * @param  string $ClassName
42
     * @param  string $FieldName
43
     * @param  string $recordID
44
     * @param  string $linkText
45
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

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...
46
     */
47 View Code Duplication
    public static function popup_link($ClassName, $FieldName, $recordID, $linkText = 'click here to edit')
0 ignored issues
show
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...
48
    {
49
        if ($link = self::popup_link_only($ClassName, $FieldName, $recordID)) {
50
            return '
51
                <a href="'.$link.'" class="modalPopUp modal-popup" data-width="800" data-height="600" data-rel="window.open(\''.$link.'\', \'sortlistFor'.$ClassName.$FieldName.$recordID.'\',\'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=600,height=600,left = 440,top = 200\'); return false;">'.$linkText.'</a>';
52
        }
53
    }
54
55 View Code Duplication
    public function init()
0 ignored issues
show
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...
56
    {
57
        //must set this first ...
58
        Config::inst()->update('SSViewer', 'theme_enabled', Config::inst()->get('DataObjectSorterRequirements', 'run_through_theme'));
59
        // Only administrators can run this method
60
        parent::init();
61
        DataObjectSorterRequirements::popup_requirements('onefieldonerecord');
62
        $url = Director::absoluteURL(
63
            Injector::inst()->get('DataObjectOneFieldOneRecordUpdateController')->Link('updatefield')
64
        );
65
        Requirements::customScript(
66
            "var DataObjectOneFieldOneRecordUpdateURL = '".$url."'",
67
            'DataObjectOneFieldOneRecordUpdateURL'
68
        );
69
    }
70
71
    public function onefieldform()
72
    {
73
        Versioned::set_reading_mode('Stage.Stage');
74
        $table = $this->SecureTableToBeUpdated();
75
        $field = $this->SecureFieldToBeUpdated();
76
        $record = $this->SecureRecordToBeUpdated();
77
        $obj = $table::get()->byID($record);
78
        if (! $obj) {
79
            user_error("record could not be found!", E_USER_ERROR);
80
        }
81
        if (! $obj->canEdit()) {
82
            return $this->permissionFailureStandard();
83
        }
84
        $FormField = $this->getFormField($obj, $field);
85
        if (!$FormField) {
86
            user_error("Form Field could not be Found", E_USER_ERROR);
87
        }
88
        $FormField->setValue($obj->$field);
89
        $form = new Form(
90
            $controller = $this,
91
            $name = "OneFieldForm",
92
            $fields = new FieldList(
93
                $FormField,
94
                new HiddenField("Table", "Table", $table),
95
                new HiddenField("Field", "Field", $field),
96
                new HiddenField("Record", "Record", $record)
97
            ),
98
            $actions = new FieldList(new FormAction("save", "save and close"))
99
        );
100
101
        return $form;
102
    }
103
104
    public function save($data, $form)
0 ignored issues
show
Unused Code introduced by
The parameter $form is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
105
    {
106
        $table = $this->SecureTableToBeUpdated();
107
        $field = $this->SecureFieldToBeUpdated();
108
        $record = $this->SecureRecordToBeUpdated();
109
        $obj = $table::get()->byID($record);
110
        if (! $obj->canEdit()) {
111
            return $this->permissionFailureStandard();
112
        }
113
        $obj->$field = $data[$field];
114
        $obj->write();
115
        return '
116
            <p>Your changes have been saved, please <a href="#" onclick="self.close(); return false;">close window</a>.</p>
117
            <script type="text/javascript">self.close();</script>';
118
    }
119
}
120