Turba API search()
Sebastian Birnbach <[email protected]>
| Newsgroups | gmane.comp.horde.devel |
|---|---|
| Message-ID | <CACTNBFNoNT4=oce0_WD21RWHwaKCJ9oWkNYzk=LmYBz7qdNxaQ@mail.gmail.com> |
I am working on a Block that will output certain address book information.
I have trouble restricting the search to certain fields, namely the
"company" field.
[...]
$terms = array($this->_params['search']);
$options = array(
'count_only' => false,
'fields' => array('company')
);
$res = $registry->contacts->search($terms, $options);
[...]
This returns all persons with the search string in any searchable field, be
it "company" or "name".
I enclose the Block lib/Block/Contacts.php for better reproducability, call
it from any wiki page with this code:
[[block horde/Contacts search=string]]
Any clues anybody?
Kind regards
Sebastian
--
dev mailing list
Frequently Asked Questions: http://wiki.horde.org/FAQ
To unsubscribe, mail: [email protected]
Contacts.php
(application/x-httpd-php, 2.3 KB)
<?php
/**
* @package Abakus
*/
class Horde_Block_Contacts extends Horde_Core_Block
{
/**
*/
public function __construct($app, $params = array())
{
parent::__construct($app, $params);
$this->_name = _("Contacts");
}
/**
*/
protected function _params()
{
return array(
'search' => array(
'type' => 'text',
'name' => _("Search"),
'default' => 'noname'
),
);
}
/**
*/
protected function _title()
{
return _("Search");
}
/**
*/
protected function _content()
{
if (!isset($this->_params['search'])) {
return "<b>Error:</b> cannot query address book without parameter 'search'";
}
// the return variable
$html = "";
// Load the Horde Framework Core
require_once HORDE_BASE . '/lib/core.php';
// get access to the registry object
global $registry;
// prepare search parameters
$terms = array($this->_params['search']); // what to search for, copy from call parameter
// perform search
$options = array(
'count_only' => true,
//'sources' => array_keys($registry->call('contacts/sources')), // wenn hier Schmarrn steht, werden alle bekannten Quellen durchsucht
'fields' => array("company"), // Mögliche Felder siehe turba/config/attributes.php. Funktioniert noch nicht (Stand 2016-06-06)
);
$res = $registry->contacts->search($terms, $options);
// analyze returned entries as table
if (empty($res)) {
$html .= "Keine Einträge im Adressbuch";
} else {
$html .= "<table>\xA";
$html .= " <tr> <th>Name</th> <th>Funktion</th> </tr>\xA";
foreach ($res[$this->_params['search']] as $person) {
// skip Erfinder
if ($person[title] === 'Erfinder') {
continue;
}
$html .= " <tr>\xA";
$link = "/turba/contact.php?source=" . $person[source] . "&key=" . $person[__key] . "&view=Contact";
$html .= " <td> <a target= \"_blank\" href=\"" . $link . "\">" . $person[firstname] . " " . $person[lastname] . "</a> </td>\xA";
if (empty($person[title])) {
$html .= " <td> -?- </td>\xA";
} else {
$html .= " <td>" . $person[title] . "</td>\xA";
}
$html .= " </tr>\xA";
}
$html .= "</table>";
}
return $html;
}
}