DataObjectSortBaseClass::init()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
4
5
6
class DataObjectSortBaseClass extends Controller implements PermissionProvider
7
{
8
    private static $url_handlers = 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...
9
        '$Action//$ID/$OtherID/$ThirdID/$FourthID/$FifthID' => 'handleAction',
10
    );
11
12
    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...
13
        "show" => 'DATA_OBJECT_SORT_AND_EDIT_PERMISSION'
14
    );
15
16
    /**
17
     * Permission for user management.
18
     *
19
     * @var string
20
     */
21
    const CAN_DO_STUFF = 'DATA_OBJECT_SORT_AND_EDIT_PERMISSION';
22
23
    public function providePermissions()
24
    {
25
        return array(
26
            DataObjectSortBaseClass::CAN_DO_STUFF => array(
27
                'name' => _t(
28
                    'DataObjectSortBaseClass.PERMISSION_MANAGE_USERS_DESCRIPTION',
29
                    'Quick updates and edits'
30
                ),
31
                'help' => _t(
32
                    'DataObjectSortBaseClass.PERMISSION_MANAGE_USERS_HELP',
33
                    'Allows for certain data to be sorted, edited, etc... This is around quick edits'
34
                ),
35
                'category' => _t('DataObjectSortBaseClass.PERMISSIONS_CATEGORY', 'Miscellaneous'),
36
                'sort' => 100
37
            )
38
        );
39
    }
40
41
42
    public function init()
43
    {
44
        // Only administrators can run this method
45
        parent::init();
46
        if (! Permission::check("DATA_OBJECT_SORT_AND_EDIT_PERMISSION")) {
47
            return $this->permissionFailureStandard();
48
        }
49
    }
50
51
    public function show()
52
    {
53
        return array();
54
    }
55
56
57
    /**
58
     *
59
     * @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...
60
     */
61
    protected function SecureFieldToBeUpdated()
62
    {
63
        if (isset($_POST["Field"])) {
64
            return addslashes($_POST["Field"]);
65
        }
66
        $field = $this->getRequest()->param("OtherID");
67
        if ($table = $this->SecureTableToBeUpdated()) {
68
            if ($tableObject = DataObject::get_one($table)) {
69
                if ($tableObject->hasDatabaseField($field)) {
70
                    return $field;
71
                } else {
72
                    user_error("$field does not exist on $table", E_USER_ERROR);
73
                }
74
            } else {
75
                user_error("there are no records in $table", E_USER_ERROR);
76
            }
77
        } else {
78
            user_error("there is no table specified", E_USER_ERROR);
79
        }
80
    }
81
82
    /**
83
     *
84
     * @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...
85
     */
86
    protected function SecureTableToBeUpdated()
87
    {
88
        if (isset($_POST["Table"])) {
89
            $table = addslashes($_POST["Table"]);
90
        } else {
91
            $table = $this->getRequest()->param("ID");
92
        }
93
        if (class_exists($table)) {
94
            return $table;
95
        } else {
96
            user_error("could not find record: $table", E_USER_ERROR);
97
        }
98
    }
99
100
101
    /**
102
     *
103
     * @return int
104
     */
105
    protected function SecureRecordToBeUpdated()
106
    {
107
        if (isset($_POST["Record"])) {
108
            return intval($_POST["Record"]);
109
        }
110
        if (isset($_GET["id"])) {
111
            $record = $_GET["id"];
112
            return intval($record);
113
        }
114
        return 0;
115
    }
116
117
118
    /**
119
     *
120
     *
121
     * @param  DataObject $obj       [description]
122
     * @param  string $fieldName     [description]
123
     * @return FormField
124
     */
125
    protected function getFormField($obj, $fieldName)
126
    {
127
        if (!self::$field) {
128
            self::$field  = $obj->dbObject($fieldName)->scaffoldFormField($obj->Title);
129
        }
130
        return self::$field;
131
    }
132
133
    /**
134
     *
135
     * @return string
136
     */
137
    protected function HumanReadableTableName()
138
    {
139
        return singleton($this->SecureTableToBeUpdated())->plural_name();
140
    }
141
142
    /**
143
     *
144
     * @return string
145
     */
146
    public function Link($action = null)
147
    {
148
        $link = Config::inst()->get($this->class, 'url_segment').'/';
149
        if ($action) {
150
            $link .= "$action/";
151
        }
152
        return $link;
153
    }
154
155
    public function permissionFailureStandard()
156
    {
157
        return Security::permissionFailure($this, _t('Security.PERMFAILURE', ' This page is secured and you need administrator rights to access it. Enter your credentials below and we will send you right along.'));
158
    }
159
}
160