Verschiebe Such-Logik in die Include
Bernd Wurst

Bernd Wurst commited on 2013-01-22 08:21:02
Zeige 2 geänderte Dateien mit 56 Einfügungen und 45 Löschungen.

... ...
@@ -83,6 +83,60 @@ function find_users_for_customer($id)
83 83
 
84 84
 
85 85
 
86
+function build_results($term) {
87
+  global $ret;
88
+  $ret = array();
89
+  
90
+  $add = function($val, $id, $value) {
91
+    global $ret;
92
+    if (isset($ret[$val]) && is_array($ret[$val])) {
93
+      array_push($ret[$val], array("id" => $id, "value" => $value));
94
+    } else {
95
+      $ret[$val] = array( array("id" => $id, "value" => $value) );
96
+    }
97
+  };
98
+
99
+
100
+  $result = array_unique(find_customers($term));
101
+  sort($result);
102
+  foreach ($result as $val) {
103
+    $c = new Customer((int) $val);
104
+    if ($c->id == $term) {
105
+      $add(10, "c{$c->id}", "Kunde {$c->id}: {$c->fullname}");
106
+    } else {
107
+      $add(90, "c{$c->id}", "Kunde {$c->id}: {$c->fullname}");
108
+    }
109
+    $users = find_users_for_customer($c->id);
110
+    foreach ($users as $u) {
111
+      $realname = $c->fullname;
112
+      if ($u['name']) {
113
+        $realname = $u['name'];
114
+      }
115
+      if ($u['uid'] == $term || $u['username'] == $term) {
116
+        $add(15, "u{$u['uid']}", "{$u['username']} (UID {$u['uid']}, {$realname})");
117
+      } elseif (strstr($u['username'], $term)) {
118
+        $add(20, "u{$u['uid']}", "{$u['username']} (UID {$u['uid']}, {$realname})");
119
+      } elseif (stristr($u['name'], $term)) {
120
+        $add(25, "u{$u['uid']}", "{$u['username']} (UID {$u['uid']}, {$realname})");
121
+      } else {
122
+        $add(85, "u{$u['uid']}", "{$u['username']} (UID {$u['uid']}, {$realname})");
123
+      }
124
+    }
125
+  }
126
+
127
+  ksort($ret);
86 128
   
129
+  $allentries = array();
130
+  foreach ($ret as $group) {
131
+    usort($group, function ($a, $b) {
132
+      return strnatcmp($a['value'], $b['value']); 
133
+    });
134
+    foreach ($group as $entry) {
135
+      $allentries[] = $entry;
136
+    }
137
+  }
138
+  unset($ret);
139
+  return $allentries;
140
+}
87 141
 
88 142
 
... ...
@@ -28,56 +28,13 @@ require_role(ROLE_SYSADMIN);
28 28
 $_SESSION['su_ajax_timestamp'] = time();
29 29
 
30 30
 $term = $_GET['term'];
31
-$ret = array();
32
-
33
-function add($val, $id, $value) {
34
-  global $ret;
35
-  if (isset($ret[$val]) && is_array($ret[$val])) {
36
-    array_push($ret[$val], array("id" => $id, "value" => $value));
37
-  } else {
38
-    $ret[$val] = array( array("id" => $id, "value" => $value) );
39
-  }
40
-}
41
-
42
-
43
-$result = array_unique(find_customers($term));
44
-sort($result);
45
-foreach ($result as $val) {
46
-  $c = new Customer((int) $val);
47
-  if ($c->id == $term) {
48
-    add(10, "c{$c->id}", "Kunde {$c->id}: {$c->fullname}");
49
-  } else {
50
-    add(90, "c{$c->id}", "Kunde {$c->id}: {$c->fullname}");
51
-  }
52
-  $users = find_users_for_customer($c->id);
53
-  foreach ($users as $u) {
54
-    $realname = $c->fullname;
55
-    if ($u['name']) {
56
-      $realname = $u['name'];
57
-    }
58
-    if ($u['uid'] == $term || $u['username'] == $term) {
59
-      add(15, "u{$u['uid']}", "{$u['username']} (UID {$u['uid']}, {$realname})");
60
-    } elseif (strstr($u['username'], $term)) {
61
-      add(20, "u{$u['uid']}", "{$u['username']} (UID {$u['uid']}, {$realname})");
62
-    } elseif (stristr($u['name'], $term)) {
63
-      add(25, "u{$u['uid']}", "{$u['username']} (UID {$u['uid']}, {$realname})");
64
-    } else {
65
-      add(85, "u{$u['uid']}", "{$u['username']} (UID {$u['uid']}, {$realname})");
66
-    }
67
-  }
68
-}
69 31
 
70
-ksort($ret);
32
+$allentries = build_results($term);
71 33
 
72 34
 $lines = array();
73
-foreach ($ret as $group) {
74
-  usort($group, function ($a, $b) {
75
-    return strnatcmp($a['value'], $b['value']); 
76
-  });
77
-  foreach ($group as $entry) {
35
+foreach ($allentries as $entry) {
78 36
   $lines[] = "  { \"id\": \"".filter_input_general($entry['id'])."\", \"value\": \"".filter_input_general($entry['value'])."\" }";
79 37
 }
80
-}
81 38
 
82 39
 
83 40
 
84 41