Completed
Push — master ( 27e209...a08afa )
by Julito
186:04 queued 150:53
created

TableSort::is_image_column()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 5
nop 2
dl 0
loc 15
rs 9.2
c 0
b 0
f 0
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
/**
5
 * This is a library with some functions to sort tabular data
6
 *
7
 * @package chamilo.library
8
 */
9
10
define('SORT_DATE', 3);
11
define('SORT_IMAGE', 4);
12
13
/**
14
 * Class TableSort
15
 */
16
class TableSort
17
{
18
    /**
19
    * Sorts 2-dimensional table.
20
    * @param array $data The data to be sorted.
21
    * @param int $column The column on which the data should be sorted (default = 0)
22
    * @param int $direction The direction to sort (SORT_ASC (default) or SORT_DESC)
23
    * @param int $type How should data be sorted (SORT_REGULAR, SORT_NUMERIC,
24
    * SORT_STRING,SORT_DATE,SORT_IMAGE)
25
    * @return array The sorted dataset
26
    * @author [email protected]
27
    */
28
    public static function sort_table(
29
        $data,
30
        $column = 0,
31
        $direction = SORT_ASC,
32
        $type = SORT_REGULAR
33
    ) {
34
        if (!is_array($data) || empty($data)) {
35
            return [];
36
        }
37
        if ($column != strval(intval($column))) {
38
            // Probably an attack
39
            return $data;
40
        }
41
        if (!in_array($direction, [SORT_ASC, SORT_DESC])) {
42
            // Probably an attack
43
            return $data;
44
        }
45
46
        if ($type == SORT_REGULAR) {
47
            if (self::is_image_column($data, $column)) {
48
                $type = SORT_IMAGE;
49
            } elseif (self::is_date_column($data, $column)) {
50
                $type = SORT_DATE;
51
            } elseif (self::is_numeric_column($data, $column)) {
52
                $type = SORT_NUMERIC;
53
            } else {
54
                $type = SORT_STRING;
55
            }
56
        }
57
58
        $compare_operator = $direction == SORT_ASC ? '>' : '<=';
59
        switch ($type) {
60
            case SORT_NUMERIC:
61
                $compare_function = 'return strip_tags($a['.$column.']) '.$compare_operator.' strip_tags($b['.$column.']);';
62
                break;
63
            case SORT_IMAGE:
64
                $compare_function = 'return api_strnatcmp(api_strtolower(strip_tags($a['.$column.'], "<img>")), api_strtolower(strip_tags($b['.$column.'], "<img>"))) '.$compare_operator.' 0;';
65
                break;
66
            case SORT_DATE:
67
                $compare_function = 'return strtotime(strip_tags($a['.$column.'])) '.$compare_operator.' strtotime(strip_tags($b['.$column.']));';
68
                break;
69
            case SORT_STRING:
70
            default:
71
                $compare_function = 'return api_strnatcmp(api_strtolower(strip_tags($a['.$column.'])), api_strtolower(strip_tags($b['.$column.']))) '.$compare_operator.' 0;';
72
                break;
73
        }
74
75
        // Sort the content
76
        usort($data, create_function('$a, $b', $compare_function));
77
78
        return $data;
79
    }
80
81
    /**
82
     * Sorts 2-dimensional table. It is possile changing the columns that will be shown and the way that the columns are to be sorted.
83
     * @param array $data The data to be sorted.
84
     * @param int $column The column on which the data should be sorted (default = 0)
85
     * @param string $direction The direction to sort (SORT_ASC (default) orSORT_DESC)
86
     * @param array $column_show The columns that we will show in the table i.e: $column_show = array('1','0','1') we will show the 1st and the 3th column.
87
     * @param array $column_order Changes how the columns will be sorted ie. $column_order = array('0','3','2','3') The column [1] will be sorted like the column [3]
88
     * @param constant $type How should data be sorted (SORT_REGULAR, SORT_NUMERIC, SORT_STRING, SORT_DATE, SORT_IMAGE)
89
     * @return array The sorted dataset
90
     * @author [email protected]
91
     */
92
    public static function sort_table_config(
93
        $data,
94
        $column = 0,
95
        $direction = SORT_ASC,
96
        $column_show = null,
97
        $column_order = null,
98
        $type = SORT_REGULAR,
99
        $doc_filter = false
100
    ) {
101
        if (!is_array($data) || empty($data)) {
102
            return [];
103
        }
104
105
        if ($column != strval(intval($column))) {
106
            // Probably an attack
107
            return $data;
108
        }
109
110
        if (!in_array($direction, [SORT_ASC, SORT_DESC])) {
111
            // Probably an attack
112
            return $data;
113
        }
114
115
        // Change columns sort
116
        // Here we say that the real way of how the columns are going to be order is manage by the $column_order array
117
        if (is_array($column_order)) {
118
            $column = isset($column_order[$column]) ? $column_order[$column] : $column;
119
        }
120
121
        if ($type == SORT_REGULAR) {
122
            if (self::is_image_column($data, $column)) {
123
                $type = SORT_IMAGE;
124
            } elseif (self::is_date_column($data, $column)) {
125
                $type = SORT_DATE;
126
            } elseif (self::is_numeric_column($data, $column)) {
127
                $type = SORT_NUMERIC;
128
            } else {
129
                $type = SORT_STRING;
130
            }
131
        }
132
133
        //This fixes only works in the document tool when ordering by name
134
        if ($doc_filter && in_array($type, [SORT_STRING])) {
135
            $folder_to_sort = [];
136
            $new_data = [];
137
            if (!empty($data)) {
138
                foreach ($data as $document) {
139
                    if ($document['type'] == 'folder') {
140
                        $docs_to_sort[$document['id']] = api_strtolower($document['name']);
141
                    } else {
142
                        $folder_to_sort[$document['id']] = api_strtolower($document['name']);
143
                    }
144
                    $new_data[$document['id']] = $document;
145
                }
146
147
                if ($direction == SORT_ASC) {
148
                    if (!empty($docs_to_sort)) {
149
                        api_natsort($docs_to_sort);
150
                    }
151
                    if (!empty($folder_to_sort)) {
152
                        api_natsort($folder_to_sort);
153
                    }
154
                } else {
155
                    if (!empty($docs_to_sort)) {
156
                        api_natrsort($docs_to_sort);
157
                    }
158
                    if (!empty($folder_to_sort)) {
159
                        api_natrsort($folder_to_sort);
160
                    }
161
                }
162
163
                $new_data_order = [];
164
                if (!empty($docs_to_sort)) {
165
                    foreach ($docs_to_sort as $id => $document) {
166
                        if (isset($new_data[$id])) {
167
                            $new_data_order[] = $new_data[$id];
168
                        }
169
                    }
170
                }
171
172
                if (!empty($folder_to_sort)) {
173
                    foreach ($folder_to_sort as $id => $document) {
174
                        if (isset($new_data[$id])) {
175
                            $new_data_order[] = $new_data[$id];
176
                        }
177
                    }
178
                }
179
                $data = $new_data_order;
180
            }
181
        } else {
182
            $compare_operator = $direction == SORT_ASC ? '>' : '<=';
183
            switch ($type) {
184
                case SORT_NUMERIC:
185
                    $compare_function = 'return strip_tags($a['.$column.']) '.$compare_operator.' strip_tags($b['.$column.']);';
186
                    break;
187
                case SORT_IMAGE:
188
                    $compare_function = 'return api_strnatcmp(api_strtolower(strip_tags($a['.$column.'], "<img>")), api_strtolower(strip_tags($b['.$column.'], "<img>"))) '.$compare_operator.' 0;';
189
                    break;
190
                case SORT_DATE:
191
                    $compare_function = 'return strtotime(strip_tags($a['.$column.'])) '.$compare_operator.' strtotime(strip_tags($b['.$column.']));';
192
                    break;
193
                case SORT_STRING:
194
                default:
195
                    $compare_function = 'return api_strnatcmp(api_strtolower(strip_tags($a['.$column.'])), api_strtolower(strip_tags($b['.$column.']))) '.$compare_operator.' 0;';
196
                    break;
197
            }
198
199
            // Sort the content
200
            usort($data, create_function('$a, $b', $compare_function));
201
        }
202
203
        if (is_array($column_show) && !empty($column_show)) {
204
            // We show only the columns data that were set up on the $column_show array
205
            $new_order_data = [];
206
            $count_data = count($data);
207
            $count_column_show = count($column_show);
208
            for ($j = 0; $j < $count_data; $j++) {
209
                $k = 0;
210
                for ($i = 0; $i < $count_column_show; $i++) {
211
                    if ($column_show[$i]) {
212
                        $new_order_data[$j][$k] = $data[$j][$i];
213
                    }
214
                    $k++;
215
                }
216
            }
217
            // Replace the multi-arrays
218
            $data = $new_order_data;
219
        }
220
221
        return $data;
222
    }
223
224
    /**
225
     * Checks whether a column of a 2D-array contains only numeric values
226
     * @param array $data The data-array
227
     * @param int $column The index of the column to check
228
     * @return bool TRUE if column contains only dates, FALSE otherwise
229
     * @todo Take locale into account (eg decimal point or comma ?)
230
     * @author [email protected]
231
     */
232
    private static function is_numeric_column(& $data, $column)
233
    {
234
        $is_numeric = true;
235
        foreach ($data as $index => & $row) {
236
            $is_numeric &= is_numeric(strip_tags($row[$column]));
237
            if (!$is_numeric) {
238
                break;
239
            }
240
        }
241
        return $is_numeric;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $is_numeric also could return the type integer which is incompatible with the documented return type boolean.
Loading history...
242
    }
243
244
    /**
245
     * Checks whether a column of a 2D-array contains only dates (GNU date syntax)
246
     * @param array $data The data-array
247
     * @param int $column The index of the column to check
248
     * @return bool TRUE if column contains only dates, FALSE otherwise
249
     * @author [email protected]
250
     */
251
    private static function is_date_column(& $data, $column)
252
    {
253
        $is_date = true;
254
        foreach ($data as $index => & $row) {
255
            if (strlen(strip_tags($row[$column])) != 0) {
256
                $check_date = strtotime(strip_tags($row[$column]));
257
                // strtotime Returns a timestamp on success, FALSE otherwise.
258
                // Previous to PHP 5.1.0, this function would return -1 on failure.
259
                $is_date &= ($check_date != -1 && $check_date);
260
            } else {
261
                $is_date &= false;
262
            }
263
            if (!$is_date) {
264
                break;
265
            }
266
        }
267
        return $is_date;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $is_date also could return the type integer which is incompatible with the documented return type boolean.
Loading history...
268
    }
269
270
    /**
271
     * Checks whether a column of a 2D-array contains only images (<img src="path/file.ext" alt=".."/>)
272
     * @param array $data The data-array
273
     * @param int $column The index of the column to check
274
     * @return bool TRUE if column contains only images, FALSE otherwise
275
     * @author [email protected]
276
     */
277
    private static function is_image_column(& $data, $column)
278
    {
279
        $is_image = true;
280
        foreach ($data as $index => & $row) {
281
            if (isset($row[$column])) {
282
                // at least one img-tag
283
                $is_image &= strlen(trim(strip_tags($row[$column], '<img>'))) > 0;
284
                // and no text outside attribute-values
285
                $is_image &= strlen(trim(strip_tags($row[$column]))) == 0;
286
            }
287
            if (!$is_image) {
288
                break;
289
            }
290
        }
291
        return $is_image;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $is_image also could return the type integer which is incompatible with the documented return type boolean.
Loading history...
292
    }
293
}
294