DataObjectSorterDOD::dodataobjectsort()   B
last analyzed

Complexity

Conditions 9
Paths 7

Size

Total Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 39
rs 7.7404
c 0
b 0
f 0
cc 9
nc 7
nop 1
1
<?php
2
3
/**
4
 * @author nicolaas [at] sunnysideup.co.nz
5
 * @description: adds dataobject sorting functionality
6
 *
7
 * @package: dataobjectsorter
8
 **/
9
10
class DataObjectSorterDOD extends DataExtension
11
{
12
13
14
    /**
15
     *
16
     * @var string
17
     */
18
    private static $sort_field = "Sort";
19
20
    /**
21
     * standard SS variable
22
     *
23
     */
24
    private static $db = array(
25
        'Sort' => 'Int'
26
    );
27
28
    /**
29
     * standard SS variable
30
     *
31
     */
32
    private static $casting = array(
33
        'SortTitle' => 'Varchar'
34
    );
35
36
    /**
37
     * action sort
38
     * @param array $data
39
     * @return string
40
     */
41
    public function dodataobjectsort($data)
42
    {
43
        $i = 0;
0 ignored issues
show
Unused Code introduced by
$i 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...
44
        $extraSet = '';
0 ignored issues
show
Unused Code introduced by
$extraSet 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...
45
        $extraWhere = '';
0 ignored issues
show
Unused Code introduced by
$extraWhere 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...
46
        $sortField = $this->SortFieldForDataObjectSorter();
47
        $baseDataClass = ClassInfo::baseDataClass($this->owner->ClassName);
0 ignored issues
show
Bug introduced by
The property ClassName does not seem to exist in SS_Object.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
48
        if ($baseDataClass) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $baseDataClass of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
49
            if (is_array($data) && count($data)) {
50
                foreach ($data as $position => $id) {
51
                    $id = intval($id);
52
                    $object = $baseDataClass::get()->byID($id);
53
                    //we add one because position 0 is not good.
54
                    $position = intval($position)+1;
55
                    if ($object && $object->canEdit()) {
56
                        if ($object->$sortField != $position) {
57
                            $object->$sortField = $position;
58
                            //hack for site tree
59
                            if ($object instanceof SiteTree) {
60
                                $object->writeToStage('Stage');
61
                                $object->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...
62
                            } else {
63
                                $object->write();
64
                            }
65
                        } else {
66
                            //do nothing
67
                        }
68
                    } else {
69
                        return _t("DataObjectSorter.NOACCESS", "You do not have access rights to make these changes.");
70
                    }
71
                }
72
            } else {
73
                return _t("DataObjectSorter.ERROR2", "Error 2");
74
            }
75
        } else {
76
            return _t("DataObjectSorter.ERROR1", "Error 1");
77
        }
78
        return _t("DataObjectSorter.UPDATEDRECORDS", "Updated record(s)");
79
    }
80
81
    /**
82
     *
83
     * standard SS method
84
     * @param FieldList $fields
85
     * @return FieldList
86
     */
87
    public function updateCMSFields(FieldList $fields)
88
    {
89
        $fields->removeFieldFromTab("Root.Main", $this->SortFieldForDataObjectSorter());
90
        if (! $this->owner instanceof SiteTree) {
91
            $link = $this->dataObjectSorterPopupLink();
92
            $fields->addFieldToTab("Root.Sort", new LiteralField("DataObjectSorterPopupLink", "<h2 class='dataObjectSorterDODLink'>".$link."</h2>"));
93
        }
94
        return $fields;
95
    }
96
97
    /**
98
     * simplified method
99
     *
100
     * @param string $filterField
101
     * @param string $filterValue
102
     * @param string $alternativeTitle
103
     *
104
     * @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...
105
     **/
106
    public function dataObjectSorterPopupLink($filterField = '', $filterValue = '', $alternativeTitle = '')
107
    {
108
        if ($alternativeTitle) {
109
            $linkText = $alternativeTitle;
110
        } else {
111
            $linkText = "Sort ".$this->owner->plural_name();
112
        }
113
        
114
        return DataObjectSorterController::popup_link($this->owner->ClassName, $filterField, $filterValue, $linkText);
0 ignored issues
show
Bug introduced by
The property ClassName does not seem to exist in SS_Object.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
115
    }
116
117
    /**
118
     * returns field name for sorting
119
     *
120
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be array|integer|double|string|boolean?

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...
121
     **/
122
    public function SortFieldForDataObjectSorter()
123
    {
124
        $sortField = Config::inst()->get("DataObjectSorterDOD", "sort_field");
125
        $field = "Sort";
126
        if ($sortField && $this->owner->hasDatabaseField($sortField)) {
127
            $field = $sortField;
128
        } elseif ($this->owner->hasDatabaseField("AlternativeSortNumber")) {
129
            $field = "AlternativeSortNumber";
130
        } elseif ($this->owner->hasDatabaseField("Sort")) {
131
            $field = "Sort";
132
        } elseif ($this->owner->hasDatabaseField("SortNumber")) {
133
            $field = "SortNumber";
134
        } else {
135
            user_error("No field Sort or AlternativeSortNumber (or $sortField) was found on data object: ".$class, E_USER_WARNING);
0 ignored issues
show
Bug introduced by
The variable $class does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
136
        }
137
        return $field;
138
    }
139
}
140