DataObjectOneFieldUpdateController::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
 *@todo:
5
 *  pagination
6
 *  use scaffolding method (of some sort) to get right field type
7
 * (and many other things)
8
 *
9
 *@package: dataobjectsorter
10
 *@description: allows you to quickly review and update one field for all records
11
 * e.g. update price for all products
12
 * URL is like this
13
 * dataobjectonefieldupdate/update/$Action/$ID/$OtherID
14
 * dataobjectonefieldupdate/[show]/[updatefield]/[tablename]/[fieldname]
15
 *
16
 **/
17
18
class DataObjectOneFieldUpdateController extends DataObjectSortBaseClass
19
{
20
    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...
21
        "updatefield" => 'DATA_OBJECT_SORT_AND_EDIT_PERMISSION',
22
        "show" => 'DATA_OBJECT_SORT_AND_EDIT_PERMISSION'
23
    );
24
25
    /**
26
     *
27
     * make sure to also change in routes if you change this link
28
     * @var string
29
     */
30
    private static $url_segment = 'dataobjectonefieldupdate';
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...
31
32
    private static $page_size = 50;
33
34
    private static $field = null;
35
36
    private static $_objects = null;
37
38
    private static $_objects_without_field = null;
39
40
    /**
41
     *
42
     * @param  string $ClassName
43
     * @param  string $FieldName
44
     * @param  string $where
45
     * @param  string $sort
46
     * @param  string $titleField
47
     *
48
     * @return string
49
     */
50
    public static function popup_link_only($ClassName, $FieldName, $where = '', $sort = '', $titleField = "Title")
51
    {
52
        DataObjectSorterRequirements::popup_link_requirements();
53
        $params = array();
54
        if ($where) {
55
            $params["where"] = "where=".urlencode($where);
56
        }
57
        if ($sort) {
58
            $params["sort"] = "sort=".urlencode($sort);
59
        }
60
        if ($titleField) {
61
            $params["titlefield"] = "titlefield=".urlencode($titleField);
62
        }
63
        return Injector::inst()->get('DataObjectOneFieldUpdateController')
64
            ->Link('show/'.$ClassName."/".$FieldName).'?'.implode("&amp;", $params);
65
    }
66
67
    /**
68
     *
69
     * @param  string $ClassName
70
     * @param  string $FieldName
71
     * @param  string $where
72
     * @param  string $sort
73
     * @param  string $linkText
74
     * @param  string $titleField
75
     *
76
     * @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...
77
     */
78 View Code Duplication
    public static function popup_link($ClassName, $FieldName, $where = '', $sort = '', $linkText = 'click here to edit', $titleField = "Title")
0 ignored issues
show
Unused Code introduced by
The parameter $titleField 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...
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...
79
    {
80
        $link = self::popup_link_only($ClassName, $FieldName, $where, $sort, $titleField = "Title");
81
        if ($link) {
82
            return '
83
                <a href="'.$link.'" class="modalPopUp modal-popup" data-width="800" data-height="600" data-rel="window.open(\''.$link.'\', \'sortlistFor'.$ClassName.$FieldName.'\',\'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=600,height=600,left = 440,top = 200\'); return false;">'.$linkText.'</a>';
84
        }
85
    }
86
87
88 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...
89
    {
90
        //must set this first ...
91
        Config::inst()->update('SSViewer', 'theme_enabled', Config::inst()->get('DataObjectSorterRequirements', 'run_through_theme'));
92
        parent::init();
93
        DataObjectSorterRequirements::popup_requirements('onefield');
94
        $url = Director::absoluteURL(
95
            Injector::inst()->get('DataObjectOneFieldUpdateController')
96
                ->Link('updatefield')
97
        );
98
        Requirements::customScript(
99
            "var DataObjectOneFieldUpdateURL = '".$url."'",
100
            'DataObjectOneFieldUpdateURL'
101
        );
102
    }
103
104
    public function updatefield($request = null)
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...
105
    {
106
        Versioned::set_reading_mode('Stage.Stage');
107
        $updateMessage = "";
108
        $updateCount = 0;
109
        $table = $request->param("ID");
110
        $field = $request->param("OtherID");
111
        $titleField = $request->getVar('titlefield');
112
        $ids = explode(",", $request->getVar("id"));
113
        $newValue = $request->getVar("value");
114
        if ($memberID = Member::currentUserID()) {
0 ignored issues
show
Unused Code introduced by
$memberID is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
115
            if (class_exists($table) && count($ids) > 0 && ($newValue || $newValue == 0)) {
116
                foreach ($ids as $id) {
117
                    if (intval($id)) {
118
                        if ($obj = $table::get()->byID($id)) {
119
                            if ($obj->hasDatabaseField($field)) {
120
                                if ($obj->canEdit()) {
121
                                    $obj->$field = $newValue;
122
                                    if ($obj instanceof SiteTree) {
123
                                        $obj->writeToStage("Stage");
124
                                        $obj->publish("Stage", "Live");
0 ignored issues
show
Bug introduced by
The method publish() does not exist on SiteTree. Did you maybe mean canPublish()?

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...
125
                                    } else {
126
                                        $obj->write();
127
                                    }
128
                                    if ($titleField && $obj->hasDatabaseField($titleField)) {
129
                                        $title = $obj->$titleField;
130
                                    } elseif ($obj->hasMethod("Title")) {
131
                                        $title = $obj->Title();
132
                                    } elseif ($obj->hasMethod("getTitle")) {
133
                                        $title = $obj->getTitle();
134
                                    } elseif ($title = $obj->Title) {
135
                                        //do nothing
136
                                    } elseif ($title = $obj->Name) {
137
                                        //do nothing
138
                                    } else {
139
                                        $title = $obj->ID;
140
                                    }
141
                                    $dbField = $obj->stat('db');
0 ignored issues
show
Unused Code introduced by
$dbField is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
142
                                    $newValueObject = $obj->dbObject($field);
143
                                    if ($newValueObject->hasMethod('Nice')) {
144
                                        $newValueFancy = $newValueObject->Nice();
145
                                    } else {
146
                                        $newValueFancy = $newValueObject->Raw();
147
                                    }
148
                                    $updateCount++;
149
                                    $updateMessage .= "Record updated: <i class=\"fieldTitle\">$field</i>  for <i class=\"recordTitle\">".$title ."</i> updated to <i class=\"newValue\">".$newValueFancy."</i><br />";
150
                                }
151
                            } else {
152
                                user_error("field does not exist", E_USER_ERROR);
153
                            }
154
                        } else {
155
                            user_error("could not find record: $table, $id ", E_USER_ERROR);
156
                        }
157
                    }
158
                }
159
                if ($updateCount > 1) {
160
                    return "$updateCount records Updated";
161
                } else {
162
                    return $updateMessage;
163
                }
164
            } else {
165
                user_error("data object specified: '$table' or id count: '".count($ids)."' or newValue: '$newValue' is not valid", E_USER_ERROR);
166
            }
167
        } else {
168
            user_error("you need to be logged in to make the changes", E_USER_ERROR);
169
        }
170
    }
171
172
    //used in template
173
    public function DataObjectsToBeUpdated()
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...
174
    {
175
        Versioned::set_reading_mode('Stage.Stage');
176
        if (self::$_objects === null) {
177
            $table = $this->SecureTableToBeUpdated();
178
            $field = $this->SecureFieldToBeUpdated();
179
            $where = '';
180 View Code Duplication
            if (isset($this->requestParams["where"]) && $this->requestParams["where"]) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
181
                $where = urldecode($this->requestParams["where"]);
182
            }
183
            $sort = '';
184 View Code Duplication
            if (isset($this->requestParams["sort"]) && $this->requestParams["sort"]) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
185
                $sort = urldecode($this->requestParams["sort"]);
186
            }
187
            $titleField = 'Title';
0 ignored issues
show
Unused Code introduced by
$titleField is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
188
            if (isset($this->requestParams["titlefield"]) && $this->requestParams["titlefield"]) {
189
                $titleField = urldecode($this->requestParams["titlefield"]);
0 ignored issues
show
Unused Code introduced by
$titleField is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
190
            }
191
            $start = 0;
192
            if (isset($this->requestParams["start"])) {
193
                $start = intval($this->requestParams["start"]);
194
            }
195
196
            if (isset($_GET["debug"])) {
197
                print_r("SELECT * FROM $table $where SORT BY $sort LIMIT $start, ". Config::inst()->get("DataObjectOneFieldUpdateController", "page_size"));
198
            }
199
            $dataList = $table::get()->where($where)->sort($sort)->limit(1000);
200
            $ids = array();
201
            if ($dataList->count()) {
202
                foreach ($dataList as $obj) {
203
                    if ($obj->canEdit() && $obj->canView()) {
204
                        $ids[$obj->ID] = $obj->ID;
205
                    }
206
                }
207
            }
208
            $dataList = $table::get()->filter(array('ID' => $ids))->sort($sort)->limit(1000);
209
            $_objects = new PaginatedList($dataList, $this->request);
0 ignored issues
show
Documentation introduced by
$this->request is of type object<SS_HTTPRequest>, but the function expects a array.

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...
210
            $_objects->setPageLength(Config::inst()->get("DataObjectOneFieldUpdateController", "page_size"));
211
            $arrayList = ArrayList::create();
212
            if ($_objects->count()) {
213
                foreach ($_objects as $obj) {
214
                    $obj->FormField = $obj->dbObject($field)->scaffoldFormField();
215
                    $obj->FormField->setName($obj->ClassName."/".$obj->ID);
216
                    //3.0TODO Check that I work vvv.
217
                    $obj->FormField->addExtraClass("updateField");
218
                    $obj->FieldToBeUpdatedValue = $obj->$field;
219
                    $obj->FormField->setValue($obj->$field);
220
                    $title = $obj->getTitle();
221
                    $arrayList->push(new ArrayData(array("FormField" => $obj->FormField, "MyTitle" => $title)));
222
                }
223
            }
224
            self::$_objects = $arrayList;
225
            self::$_objects_without_field = $_objects;
226
        }
227
228
        return self::$_objects;
229
    }
230
231
    /**
232
     * retun a list of objects
233
     * we need it like this for pagination....
234
     * @return DataList
0 ignored issues
show
Documentation introduced by
Should the return type not be PaginatedList?

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...
235
     */
236
    public function PaginatedListItems()
237
    {
238
        $this->DataObjectsToBeUpdated();
239
        return self::$_objects_without_field;
240
    }
241
}
242