3
* The Turba_View_List:: class provides an interface for objects that
4
* visualize Turba_List objects.
6
* Copyright 2000-2013 Horde LLC (http://www.horde.org/)
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.
11
* @author Chuck Hagenbuch <chuck@horde.org>
12
* @author Jon Parise <jon@csh.rit.edu>
14
* @license http://www.horde.org/licenses/apache ASL
17
class Turba_View_List implements Countable
20
* The Turba_List object that we are visualizing.
27
* Show/hide "mark" column in the display.
31
public $showMark = false;
34
* Show/hide "edit" column in the display.
38
public $showEdit = false;
41
* Show/hide "vcard" column in the display.
45
public $showVcard = false;
48
* Show/hide "group" column in the display.
52
public $showGroup = false;
55
* Show/hide "sort" column in the display.
59
public $showSort = false;
71
* @var Horde_Core_Ui_VarRenderer_Html
76
* A Horde_Variables object.
78
* @var Horde_Variables
83
* A list of Horde_Form_Variable objects.
87
public $variables = array();
90
* A dummy form object.
97
* Which columns to render
104
* Constructs a new Turba_View_List object.
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
110
* @return Turba_View_List
112
public function __construct($list, array $controls = null,
113
array $columns = null)
115
if (is_null($controls)) {
124
$this->columns = $columns;
126
$this->setControls($controls);
127
$this->renderer = Horde_Core_Ui_VarRenderer::factory('Html');
128
$this->vars = new Horde_Variables();
132
* Set which controls are shown by the display templates.
134
* @param array $controls
136
public function setControls(array $controls)
138
foreach ($controls as $control => $show) {
139
$key = 'show' . $control;
140
$this->$key = (bool)$show;
146
* @param string $type
148
public function setType($type)
157
public function getType()
163
* @TODO: these should be injected when we refactor to Horde_View
167
public function display()
169
global $prefs, $session;
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();
179
list($addToList, $addToListSources) = $this->getAddSources();
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) {
189
$min = $page * $perpage;
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'));
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;
204
$params = array_merge($crit, array(
205
'criteria' => $vars->criteria,
207
'source' => $vars->get('source', Turba::$source)
209
$viewurl = Horde::url('search.php')->add($params);
210
$pager = new Horde_Core_Ui_Pager('page', $vars, array(
213
'page_limit' => $maxpage,
214
'perpage' => $perpage
216
$pagerHeader = 'numPager.inc';
218
if (count($this) > $prefs->getValue('perpage')) {
219
$page = $vars->get('page', 'A');
220
if (!preg_match('/^[A-Za-z*]$/', $page)) {
224
$page = $vars->get('page', '*');
225
if (!preg_match('/^[A-Za-z*]$/', $page)) {
229
$listHtml = $this->getAlpha($numDisplayed, $page);
230
$pagerHeader = 'alphaPager.inc';
232
$viewurl = Horde::url('browse.php')->add(array(
233
'show' => $vars->get('show', 'all')
235
if (isset($vars->key)) {
236
$viewurl->add('key', $vars->key);
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>';
248
require TURBA_TEMPLATES . '/browse/actions.inc';
249
require TURBA_TEMPLATES . '/list/' . $pagerHeader;
252
require TURBA_TEMPLATES . '/list/' . $pagerHeader;
253
echo '<p><em>' . _("No matching contacts") . '</em></p>';
258
* Renders the list contents into an HTML view.
260
* @param integer $numDisplayed Ouptut parameter - the number of rows
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.
266
* @return string HTML to echo.
268
public function getPage(&$numDisplayed, $min = 0, $max = null, $page = 0)
273
return $this->_get($numDisplayed,
274
new Turba_View_List_PageFilter($min, $max),
279
* Renders the list contents that match $alpha into an HTML view.
281
* @param integer $numDisplayed This will be set to the number of contacts
283
* @param string $alpha The letter to display.
285
* @return string HTML of the list.
287
public function getAlpha(&$numDisplayed, $alpha)
289
return $this->_get($numDisplayed,
290
new Turba_View_List_AlphaFilter($alpha),
295
* Retrieves a column's name
297
* @param integer $i The zero-basd index of the column
301
public function getColumnName($i)
303
return Turba::getColumnName($i, $this->columns);
307
* @param integer $i The zero-based index of the column
309
public function getSortInfoForColumn($i)
311
$sortorder = Turba::getPreferredSortOrder();
312
$column_name = $this->getColumnName($i);
314
foreach ($sortorder as $sortfield) {
315
if ($column_name == $sortfield['field']) {
316
return array_merge($sortfield, array('rank' => $i));
325
* @param string $title
329
public function getColumnSortImage($i, $title = null)
331
if (is_null($title)) {
332
$title = _("Sort Direction");
334
$sortdir = $this->getColumnSortDirection($i);
335
if ($this->isPrimarySortColumn($i)) {
336
return Horde::img($sortdir ? 'za.png' : 'az.png', $title);
338
return Horde::img($sortdir ? 'za_secondary.png' : 'az_secondary.png', _("Sort Direction"));
343
* Retrieves a natural language description of the sort order
347
public function getSortOrderDescription()
349
$description = array();
350
$sortorder = Turba::getPreferredSortOrder();
351
foreach ($sortorder as $elt) {
352
$field = $elt['field'];
353
if (!strlen($field) || ($field == 'lastname')) {
356
$description[] = $GLOBALS['attributes'][$field]['label'];
359
return join(', ', $description);
363
* @param integer $i The zero-based index of the column
365
public function getColumnSortDirection($i)
367
$result = $this->getSortInfoForColumn($i);
368
if (is_null($result)) {
372
return $result['ascending'] ? 0 : 1;
376
* Determines whether we are sorting on the specified column
378
* @param integer $i The zero-based column index
382
public function isSortColumn($i)
384
return !is_null($this->getSortInfoForColumn($i));
388
* Determines whether this is the first column to sort by
390
* @param integer $i The zero-based column index
394
public function isPrimarySortColumn($i)
396
$result = $this->getSortInfoForColumn($i);
397
if (is_null($result)) {
401
return ($result['rank'] == 0);
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.
411
protected function _get(&$numDisplayed, $filter, $page)
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);
419
$own_source = $own_id = null;
422
$vars = Horde_Variables::getDefaultVariables();
424
include TURBA_TEMPLATES . '/browse/column_headers.inc';
427
$this->list->reset();
428
while ($ob = $this->list->next()) {
429
if ($filter->skip($ob)) {
433
include TURBA_TEMPLATES . '/browse/row.inc';
437
include TURBA_TEMPLATES . '/browse/column_footers.inc';
438
return ob_get_clean();
443
public function getAddSources()
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' => ' ' . htmlspecialchars($srcConfig['title']),
454
'source' => htmlspecialchars($src));
456
$srcDriver = $GLOBALS['injector']->getInstance('Turba_Factory_Driver')->create($src);
458
$listList = $srcDriver->search(
459
array('__type' => 'Group'),
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'))
481
} catch (Turba_Exception $e) {
482
$GLOBALS['notification']->push($e, 'horde.error');
486
if ($addToListSources) {
488
array_unshift($addToList, '- - - - - - - - -');
490
$addToList = array_merge(array(_("Create a new Contact List in:")), $addToListSources, $addToList);
491
$addToListSources = null;
494
return array($addToList, $addToListSources);
497
/* Countable methods. */
500
* Returns the number of Turba_Objects that are in the list. Use this to
501
* hide internal implementation details from client objects.
503
* @return integer The number of objects in the list.
505
public function count()
507
return $this->list->count();