forked from RRZE-Webteam/fau-cris
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass_Formatter.php
More file actions
121 lines (107 loc) · 4.16 KB
/
class_Formatter.php
File metadata and controls
121 lines (107 loc) · 4.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
class CRIS_formatter {
/*
* This class provides grouping and sorting methods for any CRIS data.
* It will return reformatted data.
*/
private $sortkey;
private $sortvalues;
public function __construct(
$group_attribute, $group_order=SORT_DESC, $sort_attribute=null,
$sort_order=SORT_ASC) {
/*
* The method takes up to four arguments. First two group all datasets
* into a sorted array. Last two arguments define the order inside
* every group.
* E.g. one can group all publications by type and sort them inside by
* year.
*/
if ($group_attribute != null)
$this->group = strtolower($group_attribute);
else
$this->group = null;
# make all lookup values lower case
if (is_array($group_order))
$this->group_order = array_map('strtolower', $group_order);
else
$this->group_order = $group_order;
$this->sort = strtolower($sort_attribute);
if (is_array($sort_order))
$this->sort_order = array_map('strtolower', $sort_order);
else
$this->sort_order = $sort_order;
}
public function execute($data) {
/*
* Perform formatting on $data. If $limit is set, return $limit entries
* at max.
*/
$final = array();
foreach ($data as $single_dataset) {
if ($this->group != null) {
if (!array_key_exists($this->group, $single_dataset->attributes))
throw new Exception('attribute not found: '. $this->group);
$group_key = $this->group;
} else {
# no grouping requested, we assume that sort is set in this case
# also the case if a maximum limit is set
$group_key = $this->sort;
}
if ($this->group === null)
$value = $group_key;
else
$value = $single_dataset->attributes[$group_key];
if (!array_key_exists($value, $final))
$final[$value] = array();
if (!empty($value)) {
$final[$value][] = $single_dataset;
} else {
$final[__('O.A.','fau-cris')][] = $single_dataset;
}
}
unset($final[0]);
# first sort main groups
if (is_array($this->group_order)) {
# user-defined array for sorting
$this->sortkey = $group_key;
$this->sortvalues = $this->group_order;
uksort($final, "self::compare_group");
} elseif ($this->group_order === SORT_ASC)
ksort($final);
elseif ($this->group_order === SORT_DESC)
krsort($final);
elseif ($this->group_order !== NULL)
throw new Exception('unknown sorting');
# sort data inside groups
foreach ($final as $_k => $group) {
if ($_k == "Other" || $_k == "O.A." || $_k == "o.a.") {
/* } elseif (!is_array($this->sort)){*/
$final[$_k] = $group;
} else {
$this->sortkey = $this->sort;
uasort($group, "self::compare_attributes");
if ($this->sort_order === SORT_DESC)
$final[$_k] = array_reverse ($group, true);
else
$final[$_k] = $group;
}
if (empty($group)) {
unset($final[$_k]);
}
}
return $final;
}
private function compare_group($a, $b) {
# look-up index
# returns false if not found (in case that sort array is incomplete)
$_a = array_search(strtolower($a), $this->sortvalues);
$_b = array_search(strtolower($b), $this->sortvalues);
if ($_a == $b) return 0;
if ($_a === false || $_a > $_b) return 1;
if ($_b === false || $_a < $_b) return -1;
}
private function compare_attributes($a, $b) {
# Compare data based on attribute specified in self::sortkey
return strcmp($a->attributes[$this->sortkey], $b->attributes[$this->sortkey]);
}
}