summaryrefslogtreecommitdiffstats
path: root/lib/kolab_client_task.php
diff options
context:
space:
mode:
authorAleksander Machniak <alec@alec.pl>2013-08-09 12:29:51 +0200
committerAleksander Machniak <alec@alec.pl>2013-08-09 12:29:51 +0200
commitfbb7f37f8506e5e9c991ad9e9d05bafb71f800b1 (patch)
tree4e8e61b5d55aaef6b3d357f6b34928503a0c1fd1 /lib/kolab_client_task.php
parenta985f6095fbb8625cb82bebaf07734a4d6116c68 (diff)
downloadwebadmin-fbb7f37f8506e5e9c991ad9e9d05bafb71f800b1.tar.gz
Generalized list/search handling. Fixed searching users by email/name (Bug #2057)
Diffstat (limited to 'lib/kolab_client_task.php')
-rw-r--r--lib/kolab_client_task.php242
1 files changed, 237 insertions, 5 deletions
diff --git a/lib/kolab_client_task.php b/lib/kolab_client_task.php
index 3f50b9d..3936dac 100644
--- a/lib/kolab_client_task.php
+++ b/lib/kolab_client_task.php
@@ -41,12 +41,21 @@ class kolab_client_task
*/
protected $config;
- protected $ajax_only = false;
- protected $page_title = 'Kolab Web Admin Panel';
- protected $menu = array();
- protected $cache = array();
- protected $devel_mode = false;
+ protected $ajax_only = false;
+ protected $page_title = 'Kolab Web Admin Panel';
+ protected $menu = array();
+ protected $cache = array();
+ protected $devel_mode = false;
protected $object_types = array('user', 'group', 'role', 'resource', 'sharedfolder', 'domain');
+ protected $page_size = 20;
+ protected $list_attribs = array();
+ protected $list_module;
+
+ protected $search_attribs = array(
+ 'name' => array('cn', 'displayname'),
+ 'email' => array('mail', 'alias', 'mailalternateaddress'),
+ 'uid' => array('uid'),
+ );
protected static $translation = array();
@@ -1345,4 +1354,227 @@ class kolab_client_task
return $form;
}
+ /**
+ * Search form.
+ *
+ * @return string HTML output of the form
+ */
+ public function search_form()
+ {
+ $attribs = $this->get_search_attribs();
+
+ if (empty($attribs)) {
+ return '';
+ }
+
+ $options = array();
+ foreach (array_keys($attribs) as $key) {
+ $options[$key] = kolab_html::escape($this->translate('search.' . $key));
+ }
+
+ $form = new kolab_form(array('id' => 'search-form'));
+
+ $form->add_section('criteria', kolab_html::escape($this->translate('search.criteria')));
+ $form->add_element(array(
+ 'section' => 'criteria',
+ 'label' => $this->translate('search.field'),
+ 'name' => 'field',
+ 'type' => kolab_form::INPUT_SELECT,
+ 'options' => $options,
+ ));
+ $form->add_element(array(
+ 'section' => 'criteria',
+ 'label' => $this->translate('search.method'),
+ 'name' => 'method',
+ 'type' => kolab_form::INPUT_SELECT,
+ 'options' => array(
+ 'both' => kolab_html::escape($this->translate('search.contains')),
+ 'exact' => kolab_html::escape($this->translate('search.is')),
+ 'prefix' => kolab_html::escape($this->translate('search.prefix')),
+ ),
+ ));
+
+ return $form->output();
+ }
+
+ /**
+ * Returns list of attributes assigned to search field(s)
+ *
+ * @param string $name Optional search field name
+ *
+ * @return array List of attributes
+ */
+ protected function get_search_attribs($name = null)
+ {
+ $task = $this->get_task();
+ $types = $this->object_types($task);
+ $attribs = array();
+
+ foreach ($this->search_attribs as $key => $value) {
+ foreach ((array)$value as $idx => $attr) {
+ $found = false;
+ foreach ($types as $type) {
+ if (array_key_exists($attr, (array)$type['attributes']['auto_form_fields'])
+ || array_key_exists($attr, (array)$type['attributes']['form_fields'])
+ || array_key_exists($attr, (array)$type['attributes']['fields'])
+ ) {
+ $found = true;
+ break;
+ }
+ }
+
+ if (!$found) {
+ unset($value[$idx]);
+ }
+ }
+
+ if (!empty($value)) {
+ $attribs[$key] = $value;
+ }
+ }
+
+ return $name ? $attribs[$name] : $attribs;
+ }
+
+ /**
+ * Object list (and search handler)
+ */
+ public function action_list()
+ {
+ if (empty($this->list_attribs)) {
+ return;
+ }
+
+ $task = $this->get_task();
+ $page = (int) self::get_input('page', 'POST');
+
+ if (!$page || $page < 1) {
+ $page = 1;
+ }
+
+ // request parameters
+ $post = array(
+ 'attributes' => $this->list_attribs,
+ 'sort_by' => $this->list_attribs,
+// 'sort_order' => 'ASC',
+ 'page_size' => $this->page_size,
+ 'page' => $page,
+ );
+
+ // search parameters
+ if (!empty($_POST['search'])) {
+ $search = self::get_input('search', 'POST', true);
+ $field = self::get_input('field', 'POST');
+ $method = self::get_input('method', 'POST');
+ $attrs = $this->get_search_attribs($field);
+ $search_request = array();
+
+ foreach ($attrs as $attr) {
+ $search_request[$attr] = array(
+ 'value' => $search,
+ 'type' => $method,
+ );
+ }
+ }
+ else if (!empty($_POST['search_request'])) {
+ $search_request = self::get_input('search_request', 'POST');
+ $search_request = @unserialize(base64_decode($search_request));
+ }
+
+ if (!empty($search_request)) {
+ $post['search'] = $search_request;
+ $post['search_operator'] = 'OR';
+ }
+
+ // get users list
+ $module = $this->list_module ? $this->list_module : $task . 's';
+ $result = $this->api_post($module . '.list', null, $post);
+ $count = $result->get('count');
+ $result = (array) $result->get('list');
+
+ // calculate records
+ if ($count) {
+ $start = 1 + max(0, $page - 1) * $page_size;
+ $end = min($start + $page_size - 1, $count);
+ }
+
+ $rows = $head = $foot = array();
+ $cols = array('name');
+ $i = 0;
+
+ // table header
+ $head[0]['cells'][] = array('class' => 'name', 'body' => $this->translate($task . '.list'));
+
+ // table footer (navigation)
+ if ($count) {
+ $pages = ceil($count / $this->page_size);
+ $prev = max(0, $page - 1);
+ $next = $page < $pages ? $page + 1 : 0;
+
+ $count_str = kolab_html::span(array(
+ 'content' => $this->translate('list.records', $start, $end, $count)), true);
+ $prev = kolab_html::a(array(
+ 'class' => 'prev' . ($prev ? '' : ' disabled'),
+ 'href' => '#',
+ 'onclick' => $prev ? "kadm.command('$task.list', {page: $prev})" : "return false",
+ ));
+ $next = kolab_html::a(array(
+ 'class' => 'next' . ($next ? '' : ' disabled'),
+ 'href' => '#',
+ 'onclick' => $next ? "kadm.command('$task.list', {page: $next})" : "return false",
+ ));
+
+ $foot_body = kolab_html::span(array('content' => $prev . $count_str . $next));
+ }
+ $foot[0]['cells'][] = array('class' => 'listnav', 'body' => $foot_body);
+
+ // table body
+ if (!empty($result)) {
+ foreach ($result as $idx => $item) {
+ if (!is_array($item)) {
+ continue;
+ }
+
+ if (method_exists($this, 'list_item_handler')) {
+ $item = $this->list_item_handler($item);
+ }
+ else {
+ $item = array_shift($item);
+ }
+
+ if (empty($item)) {
+ continue;
+ }
+
+ $i++;
+ $cells = array();
+ $cells[] = array('class' => 'name', 'body' => kolab_html::escape($item),
+ 'onclick' => "kadm.command('$task.info', '$idx')");
+ $rows[] = array('id' => $i, 'class' => 'selectable', 'cells' => $cells);
+ }
+ }
+ else {
+ $rows[] = array('cells' => array(
+ 0 => array('class' => 'empty-body', 'body' => $this->translate($task . '.norecords')
+ )));
+ }
+
+ $table = kolab_html::table(array(
+ 'id' => $task . 'list',
+ 'class' => 'list',
+ 'head' => $head,
+ 'body' => $rows,
+ 'foot' => $foot,
+ ));
+
+ if ($this->action == 'list') {
+ $this->output->command('set_watermark', 'taskcontent');
+ }
+
+ $this->output->set_env('search_request', $search_request ? base64_encode(serialize($search_request)) : null);
+ $this->output->set_env('list_page', $page);
+ $this->output->set_env('list_count', $count);
+ $this->output->set_env('list_size', count($result));
+ $this->output->set_object($task . 'list', $table);
+ }
}