1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16:
17: class Turba_View_List implements Countable
18: {
19: 20: 21: 22: 23:
24: public $list;
25:
26: 27: 28: 29: 30:
31: public $showMark = false;
32:
33: 34: 35: 36: 37:
38: public $showEdit = false;
39:
40: 41: 42: 43: 44:
45: public $showVcard = false;
46:
47: 48: 49: 50: 51:
52: public $showGroup = false;
53:
54: 55: 56: 57: 58:
59: public $showSort = false;
60:
61: 62: 63: 64: 65:
66: public $type;
67:
68: 69: 70: 71: 72:
73: public $renderer;
74:
75: 76: 77: 78: 79:
80: public $vars;
81:
82: 83: 84: 85: 86:
87: public $variables = array();
88:
89: 90: 91: 92: 93:
94: public $form = null;
95:
96: 97: 98: 99: 100:
101: public $columns;
102:
103: 104: 105: 106: 107: 108: 109: 110: 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: 133: 134: 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: 147:
148: public function setType($type)
149: {
150: $this->type = $type;
151: }
152:
153: 154: 155: 156:
157: public function getType()
158: {
159: return $this->type;
160: }
161:
162: 163: 164: 165: 166: 167: 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: 255: 256: 257: 258: 259: 260: 261: 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: 274: 275: 276: 277: 278: 279: 280:
281: public function getAlpha(&$numDisplayed, $alpha)
282: {
283: return $this->_get($numDisplayed,
284: new Turba_View_List_AlphaFilter($alpha));
285: }
286:
287: 288: 289: 290: 291: 292: 293:
294: public function getColumnName($i)
295: {
296: return Turba::getColumnName($i, $this->columns);
297: }
298:
299: 300: 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: 318: 319: 320: 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: 337: 338: 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: 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: 370: 371: 372: 373: 374:
375: public function isSortColumn($i)
376: {
377: return !is_null($this->getSortInfoForColumn($i));
378: }
379:
380: 381: 382: 383: 384: 385: 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: 399: 400: 401: 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:
441: $addToList = array();
442: $addToListSources = array();
443: foreach ($addSources as $src => $srcConfig) {
444: if (!empty($srcConfig['map']['__type'])) {
445: $addToListSources[] = array('key' => '',
446: 'name' => ' ' . 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:
491:
492: 493: 494: 495: 496: 497:
498: public function count()
499: {
500: return $this->list->count();
501: }
502:
503: }
504: