~ubuntu-branches/debian/sid/php-horde-turba/sid

« back to all changes in this revision

Viewing changes to turba-4.1.1/lib/View/List.php

  • Committer: Package Import Robot
  • Author(s): Mathieu Parent
  • Date: 2013-08-11 13:16:25 UTC
  • mfrom: (1.1.4)
  • Revision ID: package-import@ubuntu.com-20130811131625-z91stjvq51jr9onv
Tags: 4.1.1-1
New upstream version 4.1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * The Turba_View_List:: class provides an interface for objects that
 
4
 * visualize Turba_List objects.
 
5
 *
 
6
 * Copyright 2000-2013 Horde LLC (http://www.horde.org/)
 
7
 *
 
8
 * See the enclosed file LICENSE for license information (ASL).  If you did
 
9
 * did not receive this file, see http://www.horde.org/licenses/apache.
 
10
 *
 
11
 * @author   Chuck Hagenbuch <chuck@horde.org>
 
12
 * @author   Jon Parise <jon@csh.rit.edu>
 
13
 * @category Horde
 
14
 * @license  http://www.horde.org/licenses/apache ASL
 
15
 * @package  Turba
 
16
 */
 
17
class Turba_View_List implements Countable
 
18
{
 
19
    /**
 
20
     * The Turba_List object that we are visualizing.
 
21
     *
 
22
     * @var Turba_List
 
23
     */
 
24
    public $list;
 
25
 
 
26
    /**
 
27
     * Show/hide "mark" column in the display.
 
28
     *
 
29
     * @var boolean
 
30
     */
 
31
    public $showMark = false;
 
32
 
 
33
    /**
 
34
     * Show/hide "edit" column in the display.
 
35
     *
 
36
     * @var boolean
 
37
     */
 
38
    public $showEdit = false;
 
39
 
 
40
    /**
 
41
     * Show/hide "vcard" column in the display.
 
42
     *
 
43
     * @var boolean
 
44
     */
 
45
    public $showVcard = false;
 
46
 
 
47
    /**
 
48
     * Show/hide "group" column in the display.
 
49
     *
 
50
     * @var boolean
 
51
     */
 
52
    public $showGroup = false;
 
53
 
 
54
    /**
 
55
     * Show/hide "sort" column in the display.
 
56
     *
 
57
     * @var boolean
 
58
     */
 
59
    public $showSort = false;
 
60
 
 
61
    /**
 
62
     * Type of list.
 
63
     *
 
64
     * @var string
 
65
     */
 
66
    public $type;
 
67
 
 
68
    /**
 
69
     * The HTML renderer.
 
70
     *
 
71
     * @var Horde_Core_Ui_VarRenderer_Html
 
72
     */
 
73
    public $renderer;
 
74
 
 
75
    /**
 
76
     * A Horde_Variables object.
 
77
     *
 
78
     * @var Horde_Variables
 
79
     */
 
80
    public $vars;
 
81
 
 
82
    /**
 
83
     * A list of Horde_Form_Variable objects.
 
84
     *
 
85
     * @var array
 
86
     */
 
87
    public $variables = array();
 
88
 
 
89
    /**
 
90
     * A dummy form object.
 
91
     *
 
92
     * @var Horde_Form
 
93
     */
 
94
    public $form = null;
 
95
 
 
96
    /**
 
97
     * Which columns to render
 
98
     *
 
99
     * @var array
 
100
     */
 
101
    public $columns;
 
102
 
 
103
    /**
 
104
     * Constructs a new Turba_View_List object.
 
105
     *
 
106
     * @param Turba_List $list  List of contacts to display.
 
107
     * @param array $controls   Which icons to display
 
108
     * @param array $columns    The list of columns to display
 
109
     *
 
110
     * @return Turba_View_List
 
111
     */
 
112
    public function __construct($list, array $controls = null,
 
113
                                array $columns = null)
 
114
    {
 
115
        if (is_null($controls)) {
 
116
            $controls = array(
 
117
                'Mark' => true,
 
118
                'Edit' => true,
 
119
                'Vcard' => true,
 
120
                'Group' => true,
 
121
                'Sort' => true
 
122
            );
 
123
        }
 
124
        $this->columns = $columns;
 
125
        $this->list = $list;
 
126
        $this->setControls($controls);
 
127
        $this->renderer = Horde_Core_Ui_VarRenderer::factory('Html');
 
128
        $this->vars = new Horde_Variables();
 
129
    }
 
130
 
 
131
    /**
 
132
     * Set which controls are shown by the display templates.
 
133
     *
 
134
     * @param array $controls
 
135
     */
 
136
    public function setControls(array $controls)
 
137
    {
 
138
        foreach ($controls as $control => $show) {
 
139
            $key = 'show' . $control;
 
140
            $this->$key = (bool)$show;
 
141
        }
 
142
    }
 
143
 
 
144
    /**
 
145
     *
 
146
     * @param string $type
 
147
     */
 
148
    public function setType($type)
 
149
    {
 
150
        $this->type = $type;
 
151
    }
 
152
 
 
153
    /**
 
154
     *
 
155
     * @return string
 
156
     */
 
157
    public function getType()
 
158
    {
 
159
        return $this->type;
 
160
    }
 
161
 
 
162
    /**
 
163
     * @TODO: these should be injected when we refactor to Horde_View
 
164
     * @global $prefs
 
165
     * @global $session
 
166
     */
 
167
    public function display()
 
168
    {
 
169
        global $prefs, $session;
 
170
 
 
171
        $driver = $GLOBALS['injector']
 
172
            ->getInstance('Turba_Factory_Driver')
 
173
            ->create(Turba::$source);
 
174
        $hasDelete = $driver->hasPermission(Horde_Perms::DELETE);
 
175
        $hasEdit = $driver->hasPermission(Horde_Perms::EDIT);
 
176
        $hasExport = ($GLOBALS['conf']['menu']['import_export'] && !empty($GLOBALS['cfgSources'][Turba::$source]['export']));
 
177
        $vars = Horde_Variables::getDefaultVariables();
 
178
 
 
179
        list($addToList, $addToListSources) = $this->getAddSources();
 
180
 
 
181
        if ($this->type == 'search') {
 
182
            $page = $vars->get('page', 0);
 
183
            $numitem = count($this);
 
184
            $maxpage = $prefs->getValue('maxpage');
 
185
            $perpage = $prefs->getValue('perpage');
 
186
            $min = $page * $perpage;
 
187
            while ($min > $numitem) {
 
188
                $page--;
 
189
                $min = $page * $perpage;
 
190
            }
 
191
            $max = $min + $perpage;
 
192
            $start = ($page * $perpage) + 1;
 
193
            $end = min($numitem, $start + $perpage - 1);
 
194
            $listHtml = $this->getPage($numDisplayed, $min, $max, $vars->get('page'));
 
195
            $crit = array();
 
196
            if ($session->get('turba', 'search_mode') == 'advanced') {
 
197
                $map = $driver->getCriteria();
 
198
                foreach (array_keys($map) as $key) {
 
199
                    if (($key != '__key') && !empty($vars->$key)) {
 
200
                        $crit[$key] = $vars->$key;
 
201
                    }
 
202
                }
 
203
            }
 
204
            $params = array_merge($crit, array(
 
205
                'criteria' => $vars->criteria,
 
206
                'val' => $vars->val,
 
207
                'source' => $vars->get('source', Turba::$source)
 
208
            ));
 
209
            $viewurl = Horde::url('search.php')->add($params);
 
210
            $pager = new Horde_Core_Ui_Pager('page', $vars, array(
 
211
                'num' => $numitem,
 
212
                'url' => $viewurl,
 
213
                'page_limit' => $maxpage,
 
214
                'perpage' => $perpage
 
215
            ));
 
216
            $pagerHeader = 'numPager.inc';
 
217
        } else {
 
218
            if (count($this) > $prefs->getValue('perpage')) {
 
219
                $page = $vars->get('page', 'A');
 
220
                if (!preg_match('/^[A-Za-z*]$/', $page)) {
 
221
                    $page = 'A';
 
222
                }
 
223
            } else {
 
224
                $page = $vars->get('page', '*');
 
225
                if (!preg_match('/^[A-Za-z*]$/', $page)) {
 
226
                    $page = '*';
 
227
                }
 
228
            }
 
229
            $listHtml = $this->getAlpha($numDisplayed, $page);
 
230
            $pagerHeader = 'alphaPager.inc';
 
231
 
 
232
            $viewurl = Horde::url('browse.php')->add(array(
 
233
                'show' => $vars->get('show', 'all')
 
234
            ));
 
235
            if (isset($vars->key)) {
 
236
                $viewurl->add('key', $vars->key);
 
237
            }
 
238
        }
 
239
 
 
240
        if ($numDisplayed) {
 
241
            $copymove_source_options = '';
 
242
            foreach ($GLOBALS['copymoveSources'] as $key => $curSource) {
 
243
                if ($key != Turba::$source) {
 
244
                    $copymove_source_options .= '<option value="' . htmlspecialchars($key) . '">' . htmlspecialchars($curSource['title']) . '</option>';
 
245
                }
 
246
            }
 
247
 
 
248
            require TURBA_TEMPLATES . '/browse/actions.inc';
 
249
            require TURBA_TEMPLATES . '/list/' . $pagerHeader;
 
250
            echo $listHtml;
 
251
        } else {
 
252
            require TURBA_TEMPLATES . '/list/' . $pagerHeader;
 
253
            echo '<p><em>' . _("No matching contacts") . '</em></p>';
 
254
        }
 
255
    }
 
256
 
 
257
    /**
 
258
     * Renders the list contents into an HTML view.
 
259
     *
 
260
     * @param integer $numDisplayed  Ouptut parameter - the number of rows
 
261
     *                               rendered.
 
262
     * @param integer $min           Minimum number of rows to display.
 
263
     * @param integer $max           Maximum number of rows to display.
 
264
     * @param string $page           The currently displayed page.
 
265
     *
 
266
     * @return string  HTML to echo.
 
267
     */
 
268
    public function getPage(&$numDisplayed, $min = 0, $max = null, $page = 0)
 
269
    {
 
270
        if (is_null($max)) {
 
271
            $max = count($this);
 
272
        }
 
273
        return $this->_get($numDisplayed,
 
274
                           new Turba_View_List_PageFilter($min, $max),
 
275
                           $page);
 
276
    }
 
277
 
 
278
    /**
 
279
     * Renders the list contents that match $alpha into an HTML view.
 
280
     *
 
281
     * @param integer $numDisplayed  This will be set to the number of contacts
 
282
     *                               in the view.
 
283
     * @param string $alpha The letter to display.
 
284
     *
 
285
     * @return string HTML of the list.
 
286
     */
 
287
    public function getAlpha(&$numDisplayed, $alpha)
 
288
    {
 
289
        return $this->_get($numDisplayed,
 
290
                           new Turba_View_List_AlphaFilter($alpha),
 
291
                           $alpha);
 
292
    }
 
293
 
 
294
    /**
 
295
     * Retrieves a column's name
 
296
     *
 
297
     * @param integer $i  The zero-basd index of the column
 
298
     *
 
299
     * @return string
 
300
     */
 
301
    public function getColumnName($i)
 
302
    {
 
303
        return Turba::getColumnName($i, $this->columns);
 
304
    }
 
305
 
 
306
    /**
 
307
     * @param integer $i  The zero-based index of the column
 
308
     */
 
309
    public function getSortInfoForColumn($i)
 
310
    {
 
311
        $sortorder = Turba::getPreferredSortOrder();
 
312
        $column_name = $this->getColumnName($i);
 
313
        $i = 0;
 
314
        foreach ($sortorder as $sortfield) {
 
315
            if ($column_name == $sortfield['field']) {
 
316
                return array_merge($sortfield, array('rank' => $i));
 
317
            }
 
318
            $i++;
 
319
        }
 
320
        return null;
 
321
    }
 
322
 
 
323
    /**
 
324
     * @param integer $i
 
325
     * @param string $title
 
326
     *
 
327
     * @return string
 
328
     */
 
329
    public function getColumnSortImage($i, $title = null)
 
330
    {
 
331
        if (is_null($title)) {
 
332
            $title = _("Sort Direction");
 
333
        }
 
334
        $sortdir = $this->getColumnSortDirection($i);
 
335
        if ($this->isPrimarySortColumn($i)) {
 
336
            return Horde::img($sortdir ? 'za.png' : 'az.png', $title);
 
337
        } else {
 
338
            return Horde::img($sortdir ? 'za_secondary.png' : 'az_secondary.png', _("Sort Direction"));
 
339
        }
 
340
    }
 
341
 
 
342
    /**
 
343
     * Retrieves a natural language description of the sort order
 
344
     *
 
345
     * @return string
 
346
     */
 
347
    public function getSortOrderDescription()
 
348
    {
 
349
        $description = array();
 
350
        $sortorder = Turba::getPreferredSortOrder();
 
351
        foreach ($sortorder as $elt) {
 
352
            $field = $elt['field'];
 
353
            if (!strlen($field) || ($field == 'lastname')) {
 
354
                $field = 'name';
 
355
            }
 
356
            $description[] = $GLOBALS['attributes'][$field]['label'];
 
357
        }
 
358
 
 
359
        return join(', ', $description);
 
360
    }
 
361
 
 
362
    /**
 
363
     * @param integer $i  The zero-based index of the column
 
364
     */
 
365
    public function getColumnSortDirection($i)
 
366
    {
 
367
        $result = $this->getSortInfoForColumn($i);
 
368
        if (is_null($result)) {
 
369
            return null;
 
370
        }
 
371
 
 
372
        return $result['ascending'] ? 0 : 1;
 
373
    }
 
374
 
 
375
    /**
 
376
     * Determines whether we are sorting on the specified column
 
377
     *
 
378
     * @param integer $i  The zero-based column index
 
379
     *
 
380
     * @return boolean
 
381
     */
 
382
    public function isSortColumn($i)
 
383
    {
 
384
        return !is_null($this->getSortInfoForColumn($i));
 
385
    }
 
386
 
 
387
    /**
 
388
     * Determines whether this is the first column to sort by
 
389
     *
 
390
     * @param integer $i  The zero-based column index
 
391
     *
 
392
     * @return boolean
 
393
     */
 
394
    public function isPrimarySortColumn($i)
 
395
    {
 
396
        $result = $this->getSortInfoForColumn($i);
 
397
        if (is_null($result)) {
 
398
            return false;
 
399
        }
 
400
 
 
401
        return ($result['rank'] == 0);
 
402
    }
 
403
 
 
404
    /**
 
405
     * @param integer $numDisplayed  Set to the number of displayed contacts.
 
406
     * @param object $filter         A Turba_View_List filter object.
 
407
     * @param string $page           The currently displayed page.
 
408
     *
 
409
     * @return string
 
410
     */
 
411
    protected function _get(&$numDisplayed, $filter, $page)
 
412
    {
 
413
        ob_start();
 
414
        $width = floor(90 / (count($this->columns) + 1));
 
415
        $own = $GLOBALS['prefs']->getValue('own_contact');
 
416
        if (strpos($own, ';')) {
 
417
            list($own_source, $own_id) = explode(';', $own);
 
418
        } else {
 
419
            $own_source = $own_id = null;
 
420
        }
 
421
 
 
422
        $vars = Horde_Variables::getDefaultVariables();
 
423
 
 
424
        include TURBA_TEMPLATES . '/browse/column_headers.inc';
 
425
 
 
426
        $numDisplayed = 0;
 
427
        $this->list->reset();
 
428
        while ($ob = $this->list->next()) {
 
429
            if ($filter->skip($ob)) {
 
430
                continue;
 
431
            }
 
432
 
 
433
            include TURBA_TEMPLATES . '/browse/row.inc';
 
434
            $numDisplayed++;
 
435
        }
 
436
 
 
437
        include TURBA_TEMPLATES . '/browse/column_footers.inc';
 
438
        return ob_get_clean();
 
439
    }
 
440
 
 
441
    /**
 
442
     */
 
443
    public function getAddSources()
 
444
    {
 
445
        global $addSources;
 
446
 
 
447
        // Create list of lists for Add to.
 
448
        $addToList = array();
 
449
        $addToListSources = array();
 
450
        foreach ($addSources as $src => $srcConfig) {
 
451
            if (!empty($srcConfig['map']['__type'])) {
 
452
                $addToListSources[] = array('key' => '',
 
453
                                            'name' => '&nbsp;&nbsp;' . htmlspecialchars($srcConfig['title']),
 
454
                                            'source' => htmlspecialchars($src));
 
455
 
 
456
                $srcDriver = $GLOBALS['injector']->getInstance('Turba_Factory_Driver')->create($src);
 
457
                try {
 
458
                    $listList = $srcDriver->search(
 
459
                        array('__type' => 'Group'),
 
460
                        array(
 
461
                            array(
 
462
                                'field' => 'name',
 
463
                                'ascending' => true
 
464
                            )
 
465
                        ),
 
466
                        'AND',
 
467
                        array('name')
 
468
                    );
 
469
 
 
470
                    $listList->reset();
 
471
                    $currentList = Horde_Util::getFormData('key');
 
472
                    while ($listObject = $listList->next()) {
 
473
                        if ($listObject->getValue('__key') != $currentList) {
 
474
                            $addToList[] = array(
 
475
                                'name' => htmlspecialchars($listObject->getValue('name')),
 
476
                                'source' => htmlspecialchars($src),
 
477
                                'key' => htmlspecialchars($listObject->getValue('__key'))
 
478
                            );
 
479
                        }
 
480
                    }
 
481
                } catch (Turba_Exception $e) {
 
482
                    $GLOBALS['notification']->push($e, 'horde.error');
 
483
                }
 
484
            }
 
485
        }
 
486
        if ($addToListSources) {
 
487
            if ($addToList) {
 
488
                array_unshift($addToList, '- - - - - - - - -');
 
489
            }
 
490
            $addToList = array_merge(array(_("Create a new Contact List in:")), $addToListSources, $addToList);
 
491
            $addToListSources = null;
 
492
        }
 
493
 
 
494
        return array($addToList, $addToListSources);
 
495
    }
 
496
 
 
497
    /* Countable methods. */
 
498
 
 
499
    /**
 
500
     * Returns the number of Turba_Objects that are in the list. Use this to
 
501
     * hide internal implementation details from client objects.
 
502
     *
 
503
     * @return integer  The number of objects in the list.
 
504
     */
 
505
    public function count()
 
506
    {
 
507
        return $this->list->count();
 
508
    }
 
509
 
 
510
}