/
view_0_mixed_search.class.php
137 lines (123 loc) · 4.81 KB
/
view_0_mixed_search.class.php
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
<?php
class View__Mixed_Search extends View
{
var $_family_data = Array();
var $_person_data = Array();
var $_group_data = Array();
function processView()
{
$GLOBALS['system']->includeDBClass('person');
$this->_search_params = Array();
$search = trim(array_get($_REQUEST, 'search', array_get($_REQUEST, 'tel', '')));
$tel = preg_replace('/[^0-9]/', '', $search);
if ($search == '') return;
if (!empty($tel)) {
// Look for phone number matches
$this->_family_data = $GLOBALS['system']->getDBObjectData('family', Array('home_tel' => $tel));
$this->_person_data = $GLOBALS['system']->getDBObjectData('person', Array('mobile_tel' => $tel, 'work_tel' => $tel));
}
if (empty($tel) || (empty($this->_family_data) && empty($this->_person_data))) {
// Look for family name, person name, group name or person email
$this->_family_data = $GLOBALS['system']->getDBObjectData('family', Array('family_name' => $search.'%'));
$this->_person_data = Person::getPersonsByName($search);
$this->_group_data = $GLOBALS['system']->getDBObjectData('person_group', Array('name' => $search.'%'), 'OR', 'name');
if (FALSE !== strpos($search, '@')) {
// Add email search
$this->_person_data += $GLOBALS['system']->getDBObjectData('person', Array('email' => $search));
}
if (empty($this->_group_data)) {
$this->_group_data = $GLOBALS['system']->getDBObjectData('person_group', Array('name' => '%'.$search.'%'), 'OR', 'name');
}
}
$numResults = count($this->_family_data) + count($this->_group_data) + count($this->_person_data);
if ($numResults == 1) {
// For a single result, just redirect to its detail view, don't show a list
if (!empty($this->_person_data)) {
add_message("One matching person found");
redirect('persons', Array('search' => NULL, 'personid' => key($this->_person_data)));
} else if (!empty($this->_family_data)) {
add_message("One matching family found");
redirect('families', Array('search' => NULL, 'familyid' => key($this->_family_data)));
} else if (!empty($this->_group_data)) {
add_message("One matching group found");
redirect('groups', Array('search' => NULL, 'groupid' => key($this->_group_data)));
}
}
// Put all archived results at the end of the list
foreach (Array('_person_data', '_family_data', '_group_data') as $var) {
$archiveds = Array();
$ref = &$this->$var;
foreach ($ref as $k => $v) {
if ((array_get($v, 'status') == 'archived') || array_get($v, 'is_archived')) {
$archiveds[$k] = $v;
unset($ref[$k]);
}
}
foreach ($archiveds as $k => $v) {
$ref[$k] = $v;
}
}
}
function getTitle()
{
return 'Search results';
}
function printView()
{
if (empty($this->_person_data) && empty($this->_family_data) && empty($this->_group_data)) {
echo '<p><i>No matching persons or families were found. Try searching again.</i></p>';
echo '<form class="form form-horizontal"><input type="hidden" name="view" value="_mixed_search">';
echo '<input type="text" name="search" placeholder="Name, Phone or Email" />';
echo '<button type="submit" class="btn">Search</button></form>';
return;
}
?>
<table class="table table-hover table-striped table-min-width clickable-rows">
<?php
if (!empty($this->_group_data)) {
foreach ($this->_group_data as $id => $values) {
$class = ($values['is_archived']) ? 'class="archived"' : '';
?>
<tr <?php echo $class; ?>>
<td><?php echo ents($values['name']); ?></td>
<td class="narrow">
<a href="?view=groups&groupid=<?php echo $id; ?>"><i class="icon-list"></i>View</a>
<a href="?view=_edit_group&groupid=<?php echo $id; ?>"><i class="icon-wrench"></i>Edit</a>
</td>
</tr>
<?php
}
}
if (!empty($this->_person_data)) {
foreach ($this->_person_data as $id => $values) {
$class = ($values['status'] == 'archived') ? 'class="archived"' : '';
?>
<tr <?php echo $class; ?>>
<td><?php echo ents($values['first_name']).' '.ents($values['last_name']); ?></td>
<td class="narrow">
<a href="?view=persons&personid=<?php echo $id; ?>"><i class="icon-user"></i>View</a>
<a href="?view=_edit_person&personid=<?php echo $id; ?>"><i class="icon-wrench"></i>Edit</a>
</td>
</tr>
<?php
}
}
if (!empty($this->_family_data)) {
foreach ($this->_family_data as $id => $values) {
$class = ($values['status'] == 'archived') ? 'class="archived"' : '';
?>
<tr <?php echo $class; ?>>
<td><?php echo ents($values['family_name']); ?> Family</td>
<td class="narrow">
<a href="?view=families&familyid=<?php echo $id; ?>"><i class="icon-home"></i>View</a>
<a href="?view=_edit_family&familyid=<?php echo $id; ?>"><i class="icon-wrench"></i>Edit</a>
</td>
</tr>
<?php
}
}
?>
</table>
<?php
}
}