Overview

Packages

  • Horde
    • Data
  • None
  • Turba

Classes

  • Turba
  • Turba_Api
  • Turba_Driver
  • Turba_Driver_Facebook
  • Turba_Driver_Favourites
  • Turba_Driver_Group
  • Turba_Driver_Imsp
  • Turba_Driver_Kolab
  • Turba_Driver_Ldap
  • Turba_Driver_Prefs
  • Turba_Driver_Share
  • Turba_Driver_Sql
  • Turba_Driver_Vbook
  • Turba_Exception
  • Turba_Factory_Driver
  • Turba_Form_AddContact
  • Turba_Form_Contact
  • Turba_Form_ContactBase
  • Turba_Form_CreateAddressBook
  • Turba_Form_DeleteAddressBook
  • Turba_Form_EditAddressBook
  • Turba_Form_EditContact
  • Turba_Form_EditContactGroup
  • Turba_List
  • Turba_LoginTasks_SystemTask_Upgrade
  • Turba_Object
  • Turba_Object_Group
  • Turba_Test
  • Turba_View_Browse
  • Turba_View_Contact
  • Turba_View_DeleteContact
  • Turba_View_Duplicates
  • Turba_View_EditContact
  • Turba_View_List
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * The Turba_View_List:: class provides an interface for objects that
  4:  * visualize Turba_List objects.
  5:  *
  6:  * Copyright 2000-2012 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:      * @global $default_source
167:      * @global $copymove_source_options
168:      */
169:     public function display()
170:     {
171:         global $prefs, $session, $default_source, $copymove_source_options;
172: 
173:         $driver = $GLOBALS['injector']
174:             ->getInstance('Turba_Factory_Driver')
175:             ->create($default_source);
176:         $hasDelete = $driver->hasPermission(Horde_Perms::DELETE);
177:         $hasEdit = $driver->hasPermission(Horde_Perms::EDIT);
178:         $hasExport = ($GLOBALS['conf']['menu']['import_export'] && !empty($GLOBALS['cfgSources'][$default_source]['export']));
179:         $vars = Horde_Variables::getDefaultVariables();
180: 
181:         list($addToList, $addToListSources) = $this->getAddSources();
182: 
183:         if ($this->type == 'search') {
184:             $page = Horde_Util::getFormData('page', 0);
185:             $numitem = count($this);
186:             $maxpage = $prefs->getValue('maxpage');
187:             $perpage = $prefs->getValue('perpage');
188:             $min = $page * $perpage;
189:             while ($min > $numitem) {
190:                 $page--;
191:                 $min = $page * $perpage;
192:             }
193:             $max = $min + $perpage;
194:             $start = ($page * $perpage) + 1;
195:             $end = min($numitem, $start + $perpage - 1);
196:             $listHtml = $this->getPage($numDisplayed, $min, $max);
197:             $crit = array();
198:             if ($session->get('turba', 'search_mode') == 'advanced') {
199:                 $map = $driver->getCriteria();
200:                 foreach ($map as $key => $value) {
201:                     if ($key != '__key') {
202:                         $val = Horde_Util::getFormData($key);
203:                         if (!empty($val)) {
204:                             $crit[$key] = $val;
205:                         }
206:                     }
207:                 }
208:             }
209:             $params = array_merge($crit, array(
210:                 'criteria' => Horde_Util::getFormData('criteria'),
211:                 'val' => Horde_Util::getFormData('val'),
212:                 'source' => Horde_Util::getFormData('source', $default_source)
213:             ));
214:             $viewurl = Horde::url('search.php')->add($params);
215:             $pager = new Horde_Core_Ui_Pager('page', $vars,
216:                                         array('num' => $numitem,
217:                                               'url' => $viewurl,
218:                                               'page_limit' => $maxpage,
219:                                               'perpage' => $perpage));
220:             $pagerHeader = 'numPager.inc';
221:         } else {
222:             $page = Horde_Util::getFormData('page', '*');
223:             if (!preg_match('/^[A-Za-z*]$/', $page)) {
224:                 $page = '*';
225:             }
226:             if (count($this) > $prefs->getValue('perpage')) {
227:                 $page = Horde_Util::getFormData('page', 'A');
228:                 if (!preg_match('/^[A-Za-z*]$/', $page)) {
229:                     $page = 'A';
230:                 }
231:             }
232:             $listHtml = $this->getAlpha($numDisplayed, $page);
233:             $pagerHeader = 'alphaPager.inc';
234: 
235:             $viewurl = Horde::url('browse.php')->add(array(
236:                 'show' => $vars->get('show', 'all')
237:             ));
238:             if (isset($vars->key)) {
239:                 $viewurl->add('key', $vars->key);
240:             }
241:         }
242: 
243:         if ($numDisplayed) {
244:             require TURBA_TEMPLATES . '/browse/actions.inc';
245:             require TURBA_TEMPLATES . '/list/' . $pagerHeader;
246:             echo $listHtml;
247:         } else {
248:             require TURBA_TEMPLATES . '/list/' . $pagerHeader;
249:             echo '<p><em>' . _("No matching contacts") . '</em></p>';
250:         }
251:     }
252: 
253:     /**
254:      * Renders the list contents into an HTML view.
255:      *
256:      * @param integer $numDisplayed  Ouptut parameter - the number of rows
257:      *                               rendered.
258:      * @param integer $min           Minimum number of rows to display.
259:      * @param integer $max           Maximum number of rows to display.
260:      *
261:      * @return string  HTML to echo.
262:      */
263:     public function getPage(&$numDisplayed, $min = 0, $max = null)
264:     {
265:         if (is_null($max)) {
266:             $max = count($this);
267:         }
268:         return $this->_get($numDisplayed,
269:                            new Turba_View_List_PageFilter($min, $max));
270:     }
271: 
272:     /**
273:      * Renders the list contents that match $alpha into an HTML view.
274:      *
275:      * @param integer $numDisplayed  This will be set to the number of contacts
276:      *                               in the view.
277:      * @param string $alpha The letter to display.
278:      *
279:      * @return string HTML of the list.
280:      */
281:     public function getAlpha(&$numDisplayed, $alpha)
282:     {
283:         return $this->_get($numDisplayed,
284:                            new Turba_View_List_AlphaFilter($alpha));
285:     }
286: 
287:     /**
288:      * Retrieves a column's name
289:      *
290:      * @param integer $i  The zero-basd index of the column
291:      *
292:      * @return string
293:      */
294:     public function getColumnName($i)
295:     {
296:         return Turba::getColumnName($i, $this->columns);
297:     }
298: 
299:     /**
300:      * @param integer $i  The zero-based index of the column
301:      */
302:     public function getSortInfoForColumn($i)
303:     {
304:         $sortorder = Turba::getPreferredSortOrder();
305:         $column_name = $this->getColumnName($i);
306:         $i = 0;
307:         foreach ($sortorder as $sortfield) {
308:             if ($column_name == $sortfield['field']) {
309:                 return array_merge($sortfield, array('rank' => $i));
310:             }
311:             $i++;
312:         }
313:         return null;
314:     }
315: 
316:     /**
317:      * @param integer $i
318:      * @param string $title
319:      *
320:      * @return string
321:      */
322:     public function getColumnSortImage($i, $title = null)
323:     {
324:         if (is_null($title)) {
325:             $title = _("Sort Direction");
326:         }
327:         $sortdir = $this->getColumnSortDirection($i);
328:         if ($this->isPrimarySortColumn($i)) {
329:             return Horde::img($sortdir ? 'za.png' : 'az.png', $title);
330:         } else {
331:             return Horde::img($sortdir ? 'za_secondary.png' : 'az_secondary.png', _("Sort Direction"));
332:         }
333:     }
334: 
335:     /**
336:      * Retrieves a natural language description of the sort order
337:      *
338:      * @return string
339:      */
340:     public function getSortOrderDescription()
341:     {
342:         $description = array();
343:         $sortorder = Turba::getPreferredSortOrder();
344:         foreach ($sortorder as $elt) {
345:             $field = $elt['field'];
346:             if (!strlen($field) || ($field == 'lastname')) {
347:                 $field = 'name';
348:             }
349:             $description[] = $GLOBALS['attributes'][$field]['label'];
350:         }
351: 
352:         return join(', ', $description);
353:     }
354: 
355:     /**
356:      * @param integer $i  The zero-based index of the column
357:      */
358:     public function getColumnSortDirection($i)
359:     {
360:         $result = $this->getSortInfoForColumn($i);
361:         if (is_null($result)) {
362:             return null;
363:         }
364: 
365:         return $result['ascending'] ? 0 : 1;
366:     }
367: 
368:     /**
369:      * Determines whether we are sorting on the specified column
370:      *
371:      * @param integer $i  The zero-based column index
372:      *
373:      * @return boolean
374:      */
375:     public function isSortColumn($i)
376:     {
377:         return !is_null($this->getSortInfoForColumn($i));
378:     }
379: 
380:     /**
381:      * Determines whether this is the first column to sort by
382:      *
383:      * @param integer $i  The zero-based column index
384:      *
385:      * @return boolean
386:      */
387:     public function isPrimarySortColumn($i)
388:     {
389:         $result = $this->getSortInfoForColumn($i);
390:         if (is_null($result)) {
391:             return false;
392:         }
393: 
394:         return ($result['rank'] == 0);
395:     }
396: 
397:     /**
398:      * @param integer $numDisplayed
399:      * @param object $filter         A Turba_View_List filter object
400:      *
401:      * @return string
402:      */
403:     protected function _get(&$numDisplayed, $filter)
404:     {
405:         ob_start();
406:         $width = floor(90 / (count($this->columns) + 1));
407:         $own = $GLOBALS['prefs']->getValue('own_contact');
408:         if (strpos($own, ';')) {
409:             list($own_source, $own_id) = explode(';', $own);
410:         } else {
411:             $own_source = $own_id = null;
412:         }
413: 
414:         $vars = Horde_Variables::getDefaultVariables();
415:         $page = $vars->get('page', 'A');
416: 
417:         include TURBA_TEMPLATES . '/browse/column_headers.inc';
418: 
419:         $numDisplayed = 0;
420:         $this->list->reset();
421:         while ($ob = $this->list->next()) {
422:             if ($filter->skip($ob)) {
423:                 continue;
424:             }
425: 
426:             include TURBA_TEMPLATES . '/browse/row.inc';
427:             $numDisplayed++;
428:         }
429: 
430:         include TURBA_TEMPLATES . '/browse/column_footers.inc';
431:         return ob_get_clean();
432:     }
433: 
434:     /**
435:      */
436:     public function getAddSources()
437:     {
438:         global $addSources;
439: 
440:         // Create list of lists for Add to.
441:         $addToList = array();
442:         $addToListSources = array();
443:         foreach ($addSources as $src => $srcConfig) {
444:             if (!empty($srcConfig['map']['__type'])) {
445:                 $addToListSources[] = array('key' => '',
446:                                             'name' => '&nbsp;&nbsp;' . htmlspecialchars($srcConfig['title']),
447:                                             'source' => htmlspecialchars($src));
448: 
449:                 $srcDriver = $GLOBALS['injector']->getInstance('Turba_Factory_Driver')->create($src);
450:                 try {
451:                     $listList = $srcDriver->search(
452:                         array('__type' => 'Group'),
453:                         array(
454:                             array(
455:                                 'field' => 'name',
456:                                 'ascending' => true
457:                             )
458:                         ),
459:                         'AND',
460:                         array('name')
461:                     );
462: 
463:                     $listList->reset();
464:                     $currentList = Horde_Util::getFormData('key');
465:                     while ($listObject = $listList->next()) {
466:                         if ($listObject->getValue('__key') != $currentList) {
467:                             $addToList[] = array(
468:                                 'name' => htmlspecialchars($listObject->getValue('name')),
469:                                 'source' => htmlspecialchars($src),
470:                                 'key' => htmlspecialchars($listObject->getValue('__key'))
471:                             );
472:                         }
473:                     }
474:                 } catch (Turba_Exception $e) {
475:                     $GLOBALS['notification']->push($e, 'horde.error');
476:                 }
477:             }
478:         }
479:         if ($addToListSources) {
480:             if ($addToList) {
481:                 array_unshift($addToList, '- - - - - - - - -');
482:             }
483:             $addToList = array_merge(array(_("Create a new Contact List in:")), $addToListSources, $addToList);
484:             $addToListSources = null;
485:         }
486: 
487:         return array($addToList, $addToListSources);
488:     }
489: 
490:     /* Countable methods. */
491: 
492:     /**
493:      * Returns the number of Turba_Objects that are in the list. Use this to
494:      * hide internal implementation details from client objects.
495:      *
496:      * @return integer  The number of objects in the list.
497:      */
498:     public function count()
499:     {
500:         return $this->list->count();
501:     }
502: 
503: }
504: 
API documentation generated by ApiGen