DataObjectSorterController::init()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
/**
4
 * @author nicolaas [at] sunnysideup.co.nz
5
 * @description: allows you to sort dataobjects, you need to provide them in this way: http://www.mysite.com/dataobjectsorter/[dataobjectname]/
6
 *
7
 *
8
 *
9
 * @package: dataobjectsorter
10
 **/
11
12
class DataObjectSorterController extends DataObjectSortBaseClass
13
{
14
15
    /**
16
     * standard SS variable
17
     *
18
     */
19
    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...
20
        "sort" => 'DATA_OBJECT_SORT_AND_EDIT_PERMISSION',
21
        "dosort" => 'DATA_OBJECT_SORT_AND_EDIT_PERMISSION'
22
    );
23
24
    /**
25
     *
26
     * make sure to also change in routes if you change this link
27
     * @var string
28
     */
29
    private static $url_segment = 'dataobjectsorter';
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...
30
31
32
    /**
33
     * returns a link for sorting objects. You can use this in the CMS like this....
34
     * <code>
35
     * if(class_exists("DataObjectSorterController")) {
36
     * 	$fields->addFieldToTab("Root.Position", new LiteralField("AdvertisementsSorter", DataObjectSorterController::popup_link("Advertisement", $filterField = "", $filterValue = "", $linkText = "sort ".Advertisement::$plural_name, $titleField = "FullTitle")));
37
     * }
38
     * else {
39
     * 	$fields->addFieldToTab("Root.Position", new NumericField($name = "Sort", "Sort index number (the lower the number, the earlier it shows up"));
40
     * }
41
     * </code>
42
     *
43
     * @param String $className - DataObject Class Name you want to sort
44
     * @param String | Int $filterField - Field you want to filter for OR ParentID number (i.e. you are sorting children of Parent with ID = $filterField)
45
     * @param String $filterValue - filter field should be equal to this integer OR string. You can provide a list of IDs like this: 1,2,3,4 where the filterFiel is probably equal to ID or MyRelationID
46
     * @param String $linkText - text to show on the link
0 ignored issues
show
Bug introduced by
There is no parameter named $linkText. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
47
     * @param String $titleField - field to show in the sort list. This defaults to the DataObject method "getTitle", but you can use "name" or something like that.
48
     *
49
     * @return String - html
50
     */
51
    public static function popup_link_only($className, $filterField = "", $filterValue = "", $titleField = "")
52
    {
53
        DataObjectSorterRequirements::popup_link_requirements();
54
        $link = Injector::inst()->get('DataObjectSorterController')->Link('sort/'.$className);
55
        if ($filterField) {
56
            $link .= $filterField.'/';
57
        }
58
        if ($filterValue) {
59
            $link .= $filterValue.'/';
60
        }
61
        if ($titleField) {
62
            $link .= $titleField.'/';
63
        }
64
        return $link;
65
    }
66
    /**
67
     * returns a link for sorting objects. You can use this in the CMS like this....
68
     * <code>
69
     * if(class_exists("DataObjectSorterController")) {
70
     * 	$fields->addFieldToTab("Root.Position", new LiteralField("AdvertisementsSorter", DataObjectSorterController::popup_link("Advertisement", $filterField = "", $filterValue = "", $linkText = "sort ".Advertisement::$plural_name, $titleField = "FullTitle")));
71
     * }
72
     * else {
73
     * 	$fields->addFieldToTab("Root.Position", new NumericField($name = "Sort", "Sort index number (the lower the number, the earlier it shows up"));
74
     * }
75
     * </code>
76
     *
77
     * @param String $className - DataObject Class Name you want to sort
78
     * @param String | Int $filterField - Field you want to filter for OR ParentID number (i.e. you are sorting children of Parent with ID = $filterField)
79
     * @param String $filterValue - filter field should be equal to this integer OR string. You can provide a list of IDs like this: 1,2,3,4 where the filterFiel is probably equal to ID or MyRelationID
80
     * @param String $linkText - text to show on the link
81
     * @param String $titleField - field to show in the sort list. This defaults to the DataObject method "getTitle", but you can use "name" or something like that.
82
     *
83
     * @return String - html
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...
84
     */
85 View Code Duplication
    public static function popup_link($className, $filterField = "", $filterValue = "", $linkText = "sort this list", $titleField = "")
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...
86
    {
87
        $link = self::popup_link_only($className, $filterField, $filterValue, $titleField);
88
        if ($link) {
89
            return '
90
            <a href="'.$link.'" class="modalPopUp modal-popup" data-width="800" data-height="600" data-rel="window.open(\''.$link.'\', \'sortlistFor'.$className.$filterField.$filterValue.'\',\'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=600,height=600,left = 440,top = 200\'); return false;">'.$linkText.'</a>';
91
        }
92
    }
93
94
    public function init()
95
    {
96
        Config::inst()->update('SSViewer', 'theme_enabled', Config::inst()->get('DataObjectSorterRequirements', 'run_through_theme'));
97
        parent::init();
98
        if (Director::is_ajax()) {
99
        } else {
100
            DataObjectSorterRequirements::popup_requirements('sorter');
101
        }
102
    }
103
104
    /**
105
     * the standard action...
106
     * no need to add anything here now
107
     */
108
    public function sort()
109
    {
110
        return array();
111
    }
112
113
114
115
    /**
116
     * runs the actual sorting...
117
     */
118
    public function dosort($request)
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...
119
    {
120
        Versioned::set_reading_mode('Stage.Stage');
121
        $class = $request->param("ID");
122
        if ($class) {
123
            if (class_exists($class)) {
124
                $obj = DataObject::get_one($class);
125
                return $obj->dodataobjectsort($request->requestVar('dos'));
126
            } else {
127
                user_error("$class does not exist", E_USER_WARNING);
128
            }
129
        } else {
130
            user_error("Please make sure to provide a class to sort e.g. http://www.sunnysideup.co.nz/dataobjectsorter/MyLongList - where MyLongList is the DataObject you want to sort.", E_USER_WARNING);
131
        }
132
    }
133
134
    private static $_children_cache_for_sorting = null;
135
136
    /**
137
     * runs the actual sorting...
138
     * @return Object - return dataobject set of items to be sorted
139
     */
140
    public function Children()
141
    {
142
        if (self::$_children_cache_for_sorting === null) {
143
            $class = $this->request->param("ID");
144
            if ($class) {
145
                if (class_exists($class)) {
146
                    $where = '';
0 ignored issues
show
Unused Code introduced by
$where 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...
147
                    $filterField = Convert::raw2sql($this->request->param("OtherID"));
148
                    $filterValue = Convert::raw2sql($this->request->param("ThirdID"));
149
                    $titleField = Convert::raw2sql($this->request->param("FourthID"));
150
                    $objects = $class::get();
151
                    if ($filterField && $filterValue) {
152
                        $filterValue = explode(",", $filterValue);
153
                        $objects = $objects->filter(array($filterField => $filterValue));
154
                    } elseif (is_numeric($filterField)) {
155
                        $objects = $objects->filter(array("ParentID" => $filterField));
156
                    }
157
                    $singletonObj = singleton($class);
158
                    $sortField = $singletonObj->SortFieldForDataObjectSorter();
159
                    $objects = $objects->sort($sortField, "ASC");
160
                    $tobeExcludedArray = array();
161
                    if ($objects->count()) {
162
                        foreach ($objects as $obj) {
163
                            if ($obj->canEdit()) {
164
                                if ($titleField) {
165
                                    $method = "getSortTitle";
166
                                    if ($obj->hasMethod($method)) {
167
                                        $obj->SortTitle = $obj->$method();
168
                                    } else {
169
                                        $method = 'SortTitle';
170
                                        if ($obj->hasMethod($method)) {
171
                                            $obj->SortTitle = $obj->$method();
172
                                        } elseif ($obj->hasDatabaseField($titleField)) {
173
                                            $obj->SortTitle = $obj->$titleField;
174
                                        }
175
                                    }
176
                                } else {
177
                                    $obj->SortTitle = $obj->getTitle();
178
                                }
179
                            } else {
180
                                $tobeExcludedArray[$obj->ID] = $obj->ID;
181
                            }
182
                        }
183
                        $objects = $objects->exclude(array('ID' => $tobeExcludedArray));
184
                        $this->addRequirements($class);
185
                        self::$_children_cache_for_sorting = $objects;
186
                    } else {
187
                        return null;
188
                    }
189
                } else {
190
                    user_error("$class does not exist", E_USER_WARNING);
191
                }
192
            } else {
193
                user_error("Please make sure to provide a class to sort e.g. http://www.sunnysideup.co.nz/dataobjectsorter/MyLongList - where MyLongList is the DataObject you want to sort.", E_USER_WARNING);
194
            }
195
        } else {
196
        }
197
        return self::$_children_cache_for_sorting;
198
    }
199
200
    /**
201
     * adds functionality for actual sorting...
202
     *
203
     * @param string $className - name of the class being sorted
204
     */
205
    protected function addRequirements($className)
206
    {
207
        $url = Director::absoluteURL(
208
            Injector::inst()->get('DataObjectSorterController')->Link('dosort/'.$className)
209
        );
210
        Requirements::customScript(
211
            "var DataObjectSorterURL = '".$url."'",
212
            'DataObjectSorterURL'
213
        );
214
    }
215
}
216