-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaction.sortlike.php
More file actions
187 lines (181 loc) · 5.57 KB
/
Copy pathaction.sortlike.php
File metadata and controls
187 lines (181 loc) · 5.57 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php
#----------------------------------------------------------------------
# Module: Booker - a resource booking module
# Action: sortlike - ajax processor to generate replacement table-body
#----------------------------------------------------------------------
# See file Booker.module.php for full details of copyright, licence, etc.
#----------------------------------------------------------------------
if (!function_exists('array_like')) {
/*
simple_diff:
Paul's simple diff algorithm, similar to Ratcliff/Obershelp, v 0.1
(C) Paul Butler 2007 <http://www.paulbutler.org/>
@a: array
@b: array
Returns: array of the changes between @a and @b
*/
function simple_diff($a, $b)
{
$matrix = array();
$maxlen = 0;
foreach ($a as $oindex=>$ovalue) {
$nkeys = array_keys($b,$ovalue);
foreach ($nkeys as $nindex) {
$matrix[$oindex][$nindex] = isset($matrix[$oindex - 1][$nindex - 1]) ?
$matrix[$oindex - 1][$nindex - 1] + 1 : 1;
if ($matrix[$oindex][$nindex] > $maxlen) {
$maxlen = $matrix[$oindex][$nindex];
$omax = $oindex + 1 - $maxlen;
$nmax = $nindex + 1 - $maxlen;
}
}
}
if ($maxlen == 0) { //arrays are completely different
if ($a || $b)
return array(array('d'=>$a,'i'=>$b));
return array();
}
return array_merge(
simple_diff(array_slice($a,0,$omax),array_slice($b,0,$nmax)),
array_slice($b,$nmax,$maxlen),
simple_diff(array_slice($a,$omax + $maxlen), array_slice($b,$nmax + $maxlen)));
}
/*
array_like:
For comparing keywords or split strings with re-ordered chars
@a: array
@b: array
Returns: float 0.0 .. 1.0, higher = @a,@b more similar
*/
function array_like($a, $b)
{
if (!$a || !$b)
return 0.0;
$m = 0;
$ins = array();
$outs = array();
$parts = simple_diff($a,$b);
foreach ($parts as $i=>$one) {
if (is_array($one)) { //difference
if ($one['d']) { //something missing
$f = array_search($one['d'],$ins);
if ($f === FALSE)
$outs[$i] = $one['d'];
else {
unset($ins[$f]);
$m += 0.75 / abs($f-$i);
}
}
if ($one['i']) { //something extra
$f = array_search($one['i'],$outs);
if ($f === FALSE) {
$ins[$i] = $one['i'];
} else {
unset($outs[$f]);
$m += 0.75 / abs($f-$i);
}
}
} else { //matching part
$m++;
}
}
$mc = max(count($a),count($b));
return $m / $mc;
}
function cmp_like($a, $b)
{
$d = $a['score'] - $b['score'];
if ($d > 0) return -1;
if ($d < 0) return 1;
return strnatcasecmp($a['name'],$b['name']);
}
}
/*supplied $params = array(
'item_id' => number
'sort' => 'groups' or 'members'
)
*/
$utils = new Booker\Utils();
$item_id = (int)$params['item_id'];
$havegroups = array($item_id);
$type = $params['sort'];
if ($type == 'members')
$members = $utils->GetGroupItems($this,$item_id);
else
$members = $utils->GetItemGroups($this,$item_id);
if ($members) {
if (count($members) > 1) {
$data = array();
foreach ($members as $i) {
$data[$i] = $utils->GetItemProperties($this,$i,array('name','description','keywords'));
if (empty($data[$i]['name']))
$data[$i]['name'] = $utils->GetItemNameForID($this,$i);
if ($i >= Booker::MINGRPID)
$havegroups[] = $i;
}
$first = $item_id; //$params['first_id'];
$fname = !empty($data[$first]['name']) ? str_split($data[$first]['name']) : FALSE;
$fdesc = !empty($data[$first]['description']) ? str_split($data[$first]['description']) : FALSE;
$fkeys = !empty($data[$first]['keywords']) ? explode(',',$data[$first]['keywords']) : FALSE;
$cmps = array();
foreach ($data as $i=>$one) {
if (!($i == $first || $i == $item_id)) {
$cmps[$i] = array('score'=>0.0,'name'=>'');
if ($fname && !empty($data[$i]['name'])) {
$ref = str_split($data[$i]['name']);
$cmps[$i]['score'] += 1.0 - array_like($fname,$ref);
$cmps[$i]['name'] = $data[$i]['name'];
}
if ($fdesc && !empty($data[$i]['description'])) {
$ref = str_split($data[$i]['description']);
$cmps[$i]['score'] += 1.0 - array_like($fdesc,$ref);
}
if ($fkeys && !empty($data[$i]['keywords'])) {
$ref = explode(',',$data[$i]['description']);
$cmps[$i]['score'] += 1.0 - array_like($fkeys,$ref);
}
}
}
uasort($cmps,'cmp_like');
} else {
$i = key($members);
if ($i != $first) {
$cmps = array($i=>array('score'=>30.0,'name'=>''));
if ($i >= Booker::MINGRPID)
$havegroups[] = $i;
}
}
$all = array($first=>array('score'=>50.0,'name'=>'')) + $cmps; //CHECKME why prepend?
$chkname = ($type == 'members') ? $type : 'ingroups'; //kludge to conform
$sorted = array();
//NOTE must conform with action.openitem.php table-data creation, except -
//cuz' js & ajax are available, the up/down links will be hidden/irrelevant
foreach ($all as $i=>$one) {
if (isset($data[$i])) {
$oneset = new stdClass();
$oneset->name = $data[$i]['name'];
$oneset->uplink = '';
$oneset->dnlink = '';
$oneset->check = $this->CreateInputCheckbox($id,$chkname.'[]',$i,$i);
$sorted[] = $oneset;
}
}
//append extra groups, unselected
$moregroups = $db->GetAssoc('SELECT item_id,name FROM '.$this->ItemTable.
' WHERE item_id >= '.Booker::MINGRPID.' AND item_id NOT IN ('.
implode(',',$havegroups).') ORDER BY name');
foreach ($moregroups as $i=>$one) {
$oneset = new stdClass();
$oneset->name = $one; //too bad if name empty!
$oneset->uplink = '';
$oneset->dnlink = '';
$oneset->check = $this->CreateInputCheckbox($id,$chkname.'[]',$i,-1);
$sorted[] = $oneset;
}
$tplvars = array(
'entries' => $sorted,
'cellclass' => $type
);
echo Booker\Utils::ProcessTemplate($this,'membersbody.tpl',$tplvars);
} else
echo 0;