File: search.php

package info (click to toggle)
turba2 2.2.1-2
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 7,332 kB
  • ctags: 2,927
  • sloc: php: 11,046; xml: 1,690; sql: 507; makefile: 62; perl: 17; sh: 1
file content (219 lines) | stat: -rw-r--r-- 8,076 bytes parent folder | download
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<?php
/**
 * $Horde: turba/search.php,v 1.94.4.18 2008/04/14 16:59:01 mrubinsk Exp $
 *
 * Copyright 2000-2008 The Horde Project (http://www.horde.org/)
 *
 * See the enclosed file LICENSE for license information (ASL).  If you
 * did not receive this file, see http://www.horde.org/licenses/asl.php.
 *
 * @author Chuck Hagenbuch <chuck@horde.org>
 */

/**
 * Add a virtual address book for the current user.
 *
 * @param array  The parameters for this virtual address book.
 *
 * @return mixed  The virtual address book ID | PEAR_Error
 */
function _createVBook($params)
{
    $id = md5(microtime());
    $vbook = array(
        'type' => 'vbook',
        'source' => $params['source'],
        'criteria' => $params['criteria'],
    );

    /* Generate the new share. */
    $share = &$GLOBALS['turba_shares']->newShare($id);
    if (is_a($share, 'PEAR_Error')) {
        return $share;
    }

    $share->set('name', $params['name']);
    $share->set('params', serialize($vbook));

    $result = $GLOBALS['turba_shares']->addShare($share);
    if (is_a($result, 'PEAR_Error')) {
        return $result;
    }

    /* Add the new addressbook to the user's list of visible address
     * books. */
    $GLOBALS['prefs']->setValue('addressbooks', $GLOBALS['prefs']->getValue('addressbooks') . "\n" . $id);

    return $id;
}

/**
 * Check for requested changes in sort order and apply to prefs.
 *
 */
function updateSortOrderFromVars()
{
    require_once 'Horde/Variables.php';
    $vars = Variables::getDefaultVariables();
    $source = Util::getFormData('source');

    if (($sortby = $vars->get('sortby')) !== null && $sortby != '') {
        $sources = Turba::getColumns();
        $columns = isset($sources[$source]) ? $sources[$source] : array();
        $column_name = Turba::getColumnName($sortby, $columns);

        $append = true;
        $ascending = ($vars->get('sortdir') == 0);
        if ($vars->get('sortadd')) {
            $sortorder = Turba::getPreferredSortOrder($columns);
            foreach ($sortorder as $i => $elt) {
                if ($elt['field'] == $column_name) {
                    $sortorder[$i]['ascending'] = $ascending;
                    $append = false;
                }
            }
        } else {
            $sortorder = array();
        }
        if ($append) {
            $sortorder[] = array('field' => $column_name,
                                 'ascending' => $ascending);
        }
        $GLOBALS['prefs']->setValue('sortorder', serialize($sortorder));
    }
}

require_once dirname(__FILE__) . '/lib/base.php';
require_once TURBA_BASE . '/lib/List.php';
require_once TURBA_BASE . '/lib/ListView.php';

/* Verify if the search mode variable is passed in form or is
 * registered in the session. Always use basic search by default. */
if (Util::getFormData('search_mode')) {
    $_SESSION['turba']['search_mode'] = Util::getFormData('search_mode');
}
if (!isset($_SESSION['turba']['search_mode'])) {
    $_SESSION['turba']['search_mode'] = 'basic';
}

/* Get the current source. */
$source = Util::getFormData('source', $default_source);
if (!isset($cfgSources[$source])) {
    reset($cfgSources);
    $source = key($cfgSources);

    /* If there are absolutely no valid sources, abort. */
    if (!isset($cfgSources[$source])) {
        $notification->push(_("No Address Books are currently available. Searching is disabled."), 'horde.error');
        require TURBA_TEMPLATES . '/common-header.inc';
        require TURBA_TEMPLATES . '/menu.inc';
        require $registry->get('templates', 'horde') . '/common-footer.inc';
        exit;
    }
}

/* Grab the form data. */
$criteria = Util::getFormData('criteria');
$val = Util::getFormData('val');
$action = Util::getFormData('actionID');
$driver = &Turba_Driver::singleton($source);
if (is_a($driver, 'PEAR_Error')) {
    $notification->push(sprintf(_("Failed to access the address book: %s"), $driver->getMessage()), 'horde.error');
    $map = array();
} else {
    $map = $driver->getCriteria();
    if ($_SESSION['turba']['search_mode'] == 'advanced') {
        $criteria = array();
        foreach ($map as $key => $value) {
            if ($key != '__key') {
                $val = Util::getFormData($key);
                if (strlen($val)) {
                    $criteria[$key] = $val;
                }
            }
        }
    }

    /* Check for updated sort criteria */
    updateSortOrderFromVars();

    /* Only try to perform a search if we actually have search criteria. */
    if ((is_array($criteria) && count($criteria)) || !empty($val)) {
        if (Util::getFormData('save_vbook')) {
            /* We create the vbook and redirect before we try to search
             * since we are not displaying the search results on this page
             * anyway. */
            $vname = Util::getFormData('vbook_name');
            if (empty($vname)) {
                $notification->push(_("You must provide a name for virtual address books."), 'horde.error');
                header('Location: ' . Horde::applicationUrl('search.php', true));
                exit;
            }

            /* Create the vbook. */
            $params = array(
                'name' => $vname,
                'source' => $source,
                'criteria' => $_SESSION['turba']['search_mode'] == 'basic' ? array($criteria => $val) : $criteria,
            );
            $vid = _createVBook($params);
            if (is_a($vid, 'PEAR_Error')) {
                $notification->push(sprintf(_("There was a problem creating the virtual address book: %s"), $vid->getMessage()), 'horde.error');
                header('Location: ' . Horde::applicationUrl('search.php', true));
                exit;
            }
            $notification->push(sprintf(_("Successfully created virtual address book \"%s\""), $vname), 'horde.success');

            $url = Horde::applicationURL('browse.php', true);
            $url = Util::addParameter($url, array('source' => $vid, null, false));
            header('Location: ' . $url);
            exit;
        }

        /* Perform a search. */
        if (($_SESSION['turba']['search_mode'] == 'basic' &&
             is_object($results = $driver->search(array($criteria => $val)))) ||
            ($_SESSION['turba']['search_mode'] == 'advanced' &&
             is_object($results = $driver->search($criteria)))) {
            if (is_a($results, 'PEAR_Error')) {
                $notification->push($results, 'horde.error');
            } else {
                /* Read the columns to display from the preferences. */
                $sources = Turba::getColumns();
                $columns = isset($sources[$source]) ? $sources[$source] : array();
                $results->sort(Turba::getPreferredSortOrder($columns));

                $view = &new Turba_ListView($results, null, $columns);
                $view->setType('search');
            }
        } else {
            $notification->push(_("Failed to search the address book"), 'horde.error');
        }
    }
}

if ($_SESSION['turba']['search_mode'] == 'basic') {
    $title = _("Basic Search");
    $notification->push('document.directory_search.val.focus();', 'javascript');
} else {
    $title = _("Advanced Search");
    $notification->push('document.directory_search.name.focus();', 'javascript');
}

Horde::addScriptFile('prototype.js', 'turba', true);
Horde::addScriptFile('QuickFinder.js', 'turba', true);
Horde::addScriptFile('effects.js', 'turba', true);
Horde::addScriptFile('redbox.js', 'turba', true);
require TURBA_TEMPLATES . '/common-header.inc';
require TURBA_TEMPLATES . '/menu.inc';
require TURBA_TEMPLATES . '/browse/search.inc';
if ($_SESSION['turba']['search_mode'] == 'advanced') {
    require TURBA_TEMPLATES . '/browse/search_criteria.inc';
}
require TURBA_TEMPLATES . '/browse/search_vbook.inc';
if (isset($view) && is_object($view)) {
    require TURBA_TEMPLATES . '/browse/javascript.inc';
    require TURBA_TEMPLATES . '/browse/header.inc';
    $view->display();
}
require $registry->get('templates', 'horde') . '/common-footer.inc';