1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13:
14: class Turba_View_Browse
15: {
16: 17: 18:
19: protected $_params;
20:
21: 22: 23: 24: 25:
26: public function __construct(array $params)
27: {
28: $this->_params = $params;
29: }
30:
31: public function updateSortOrderFromVars()
32: {
33: extract($this->_params, EXTR_REFS);
34: Turba::setPreferredSortOrder($vars, $source);
35: }
36:
37: public function run()
38: {
39: extract($this->_params, EXTR_REFS);
40:
41: $this->updateSortOrderFromVars();
42: $title = _("Address Book Listing");
43: if (!$browse_source_count && $vars->get('key') != '**search') {
44: $notification->push(_("There are no browseable address books."), 'horde.warning');
45: } else {
46: try {
47: $driver = $GLOBALS['injector']
48: ->getInstance('Turba_Factory_Driver')
49: ->create($source);
50: } catch (Turba_Exception $e) {
51: $notification->push($e, 'horde.error');
52: unset($driver);
53: }
54: }
55:
56: if (isset($driver)) {
57: $actionID = $vars->get('actionID');
58: switch ($actionID) {
59: case 'delete':
60: $keys = $vars->get('objectkeys');
61: if (!is_array($keys)) {
62: break;
63: }
64:
65: $key = false;
66: if ($vars->exists('key')) {
67: $key = $vars->get('key');
68: }
69: if ($key && $key != '**search') {
70:
71: $errorCount = 0;
72: $list = $driver->getObject($key);
73: foreach ($keys as $sourceKey) {
74: list($objectSource, $objectKey) = explode(':', $sourceKey, 2);
75: if (!$list->removeMember($objectKey, $objectSource)) {
76: $errorCount++;
77: }
78: }
79: if (!$errorCount) {
80: $notification->push(
81: sprintf(_("Successfully removed %d contact(s) from list."),
82: count($keys)),
83: 'horde.success');
84: } elseif (count($keys) == $errorCount) {
85: $notification->push(
86: sprintf(_("Error removing %d contact(s) from list."),
87: count($keys)),
88: 'horde.error');
89: } else {
90: $notification->push(
91: sprintf(_("Error removing %d of %d requested contact(s) from list."),
92: $errorCount,
93: count($keys)),
94: 'horde.error');
95: }
96: $list->store();
97: } else {
98:
99: $errorCount = 0;
100: foreach ($keys as $sourceKey) {
101: list($objectSource, $objectKey) = explode(':', $sourceKey, 2);
102: try {
103: $driver->delete($objectKey);
104: } catch (Turba_Exception $e) {
105: ++$errorCount;
106: }
107: }
108: if (!$errorCount) {
109: $notification->push(
110: sprintf(ngettext("Successfully deleted %d contact.", "Successfully deleted %d contacts.", count($keys)),
111: count($keys)),
112: 'horde.success');
113: } elseif (count($keys) == $errorCount) {
114: $notification->push(
115: sprintf(ngettext("Error deleting %d contact.", "Error deleting %d contacts.", count($keys)),
116: count($keys)),
117: 'horde.error');
118: } else {
119: $notification->push(
120: sprintf(ngettext("Error deleting %d of %d requested contact.", "Error deleting %d of %d requested contacts.", count($keys)),
121: $errorCount,
122: count($keys)),
123: 'horde.error');
124: }
125: }
126: break;
127:
128: case 'move':
129: case 'copy':
130: $keys = $vars->get('objectkeys');
131: if (!(is_array($keys) && $keys)) {
132: break;
133: }
134:
135:
136: $targetSource = $vars->get('targetAddressbook');
137:
138: try {
139: $targetDriver = $GLOBALS['injector']
140: ->getInstance('Turba_Factory_Driver')
141: ->create($targetSource);
142: } catch (Turba_Exception $e) {
143: $notification->push($e, 'horde.error');
144: break;
145: }
146:
147: $max_contacts = Turba::getExtendedPermission($targetDriver, 'max_contacts');
148: if ($max_contacts !== true &&
149: $max_contacts <= count($targetDriver)) {
150: Horde::permissionDeniedError(
151: 'turba',
152: 'max_contacts',
153: sprintf(_("You are not allowed to create more than %d contacts in \"%s\"."), $max_contacts, $cfgSources[$targetSource]['title'])
154: );
155: break;
156: }
157:
158: foreach ($keys as $sourceKey) {
159:
160: list($objectSource, $objectKey) = explode(':', $sourceKey, 2);
161:
162:
163:
164: if ($objectSource == $targetDriver->getName()) {
165: continue;
166: }
167:
168:
169: try {
170: $sourceDriver = $GLOBALS['injector']
171: ->getInstance('Turba_Factory_Driver')
172: ->create($objectSource);
173: } catch (Turba_Exception $e) {
174: $notification->push($e, 'horde.error');
175: continue;
176: }
177:
178: try {
179: $object = $sourceDriver->getObject($objectKey);
180: } catch (Turba_Exception $e) {
181: $notification->push(
182: sprintf(_("Failed to find object to be added: %s"),
183: $e->getMessage()),
184: 'horde.error');
185: continue;
186: }
187:
188: if ($object->isGroup()) {
189: if ($actionID == 'move') {
190: $notification->push(
191: sprintf(_("\"%s\" was not moved because it is a list."),
192: $object->getValue('name')),
193: 'horde.warning');
194: } else {
195: $notification->push(
196: sprintf(_("\"%s\" was not copied because it is a list."),
197: $object->getValue('name')),
198: 'horde.warning');
199: }
200: continue;
201: }
202:
203:
204: $objAttributes = array();
205:
206:
207: foreach ($targetDriver->getCriteria() as $info_key => $info_val) {
208: if (!is_array($targetDriver->map[$info_key]) ||
209: isset($targetDriver->map[$info_key]['attribute'])) {
210: $objectValue = $object->getValue($info_key);
211:
212:
213:
214: $objAttributes[$info_key] =
215: isset($GLOBALS['attributes'][$info_key]) &&
216: $GLOBALS['attributes'][$info_key]['type'] == 'image' ?
217: $objectValue['load']['data'] : $objectValue;
218: }
219: }
220: unset($objAttributes['__owner']);
221:
222: try {
223: $targetDriver->add($objAttributes);
224: } catch (Turba_Exception $e) {
225: $notification->push(
226: sprintf(_("Failed to add %s to %s: %s"),
227: $object->getValue('name'),
228: $targetDriver->title,
229: $e),
230: 'horde.error');
231: break;
232: }
233:
234: $notification->push(
235: sprintf(_("Successfully added %s to %s"),
236: $object->getValue('name'),
237: $targetDriver->title),
238: 'horde.success');
239:
240:
241:
242: if ($actionID == 'move') {
243: try {
244: $sourceDriver->delete($objectKey);
245: } catch (Turba_Exception $e) {
246: $notification->push(
247: sprintf(_("There was an error deleting \"%s\" from the source address book."),
248: $object->getValue('name')),
249: 'horde.error');
250: }
251:
252: 253: 254:
255: try {
256: $GLOBALS['injector']->getInstance('Horde_History')
257: ->log('turba:' . $targetDriver->getName() . ':' . $objAttributes['__uid'],
258: array('action' => 'add'),
259: true);
260: } catch (Exception $e) {
261: Horde::logMessage($e, 'ERR');
262: }
263: }
264: }
265: break;
266:
267: case 'add':
268:
269: $keys = $vars->get('objectkeys');
270: $targetKey = $vars->get('targetList');
271: if (empty($targetKey)) {
272: break;
273: }
274:
275: if (!$vars->exists('targetNew') || $vars->get('targetNew') == '') {
276: list($targetSource, $targetKey) = explode(':', $targetKey, 2);
277: if (!isset($cfgSources[$targetSource])) {
278: break;
279: }
280:
281: try {
282: $targetDriver = $GLOBALS['injector']
283: ->getInstance('Turba_Factory_Driver')
284: ->create($targetSource);
285: } catch (Turba_Exception $e) {
286: $notification->push($e, 'horde.error');
287: break;
288: }
289:
290: try {
291: $target = $targetDriver->getObject($targetKey);
292: } catch (Turba_Exception $e) {
293: $notification->push($e, 'horde.error');
294: break;
295: }
296: } else {
297: $targetSource = $vars->get('targetAddressbook');
298: try {
299: $targetDriver = $GLOBALS['injector']
300: ->getInstance('Turba_Factory_Driver')
301: ->create($targetSource);
302: } catch (Turba_Exception $e) {
303: $notification->push($e, 'horde.error');
304: break;
305: }
306: }
307: if (!empty($target) && $target->isGroup()) {
308:
309: if (is_array($keys)) {
310: $errorCount = 0;
311: foreach ($keys as $sourceKey) {
312: list($objectSource, $objectKey) = explode(':', $sourceKey, 2);
313: try {
314: $target->addMember($objectKey, $objectSource);
315: } catch (Turba_Exception $e) {
316: $notification->push($e, 'horde.error');
317: $errorCount++;
318: }
319: }
320: if (!$errorCount) {
321: $notification->push(
322: sprintf(_("Successfully added %d contact(s) to list."),
323: count($keys)),
324: 'horde.success');
325: } elseif ($errorCount == count($keys)) {
326: $notification->push(
327: sprintf(_("Error adding %d contact(s) to list."),
328: count($keys)),
329: 'horde.error');
330: } else {
331: $notification->push(
332: sprintf(_("Error adding %d of %d requested contact(s) to list."),
333: $errorCount,
334: count($keys)),
335: 'horde.error');
336: }
337: $target->store();
338: }
339: } else {
340:
341: $max_contacts = Turba::getExtendedPermission($driver, 'max_contacts');
342: if ($max_contacts !== true &&
343: $max_contacts <= count($driver)) {
344: Horde::permissionDeniedError(
345: 'turba',
346: 'max_contacts',
347: sprintf(_("You are not allowed to create more than %d contacts in \"%s\"."),
348: $max_contacts,
349: $cfgSources[$source]['title'])
350: );
351: break;
352: }
353:
354:
355: $newList = array(
356: '__owner' => $targetDriver->getContactOwner(),
357: '__type' => 'Group',
358: 'name' => $targetKey
359: );
360:
361: try {
362: $targetKey = $targetDriver->add($newList);
363: } catch (Turba_Exception $e) {
364: $notification->push(_("There was an error creating a new list."), 'horde.error');
365: $targetKey = null;
366: }
367:
368: if ($targetKey) {
369: try {
370: $target = $targetDriver->getObject($targetKey);
371: if ($target->isGroup()) {
372: $notification->push(
373: sprintf(_("Successfully created the contact list \"%s\"."),
374: $newList['name']),
375: 'horde.success');
376: if (is_array($keys)) {
377: $errorCount = 0;
378: foreach ($keys as $sourceKey) {
379: list($objectSource, $objectKey) = explode(':', $sourceKey, 2);
380: try {
381: $target->addMember($objectKey, $objectSource);
382: } catch (Turba_Exception $e) {
383: $notification->push($e, 'horde.error');
384: ++$errorCount;
385: }
386: }
387: if (!$errorCount) {
388: $notification->push(
389: sprintf(_("Successfully added %d contact(s) to list."),
390: count($keys)),
391: 'horde.success');
392: } elseif ($errorCount == count($keys)) {
393: $notification->push(
394: sprintf(_("Error adding %d contact(s) to list."),
395: count($keys)),
396: 'horde.error');
397: } else {
398: $notification->push(
399: sprintf(_("Error adding %d of %d requested contact(s) to list."),
400: $errorCount,
401: count($keys)),
402: 'horde.error');
403: }
404: $target->store();
405: }
406: }
407: } catch (Turba_Exception $e) {}
408: }
409: }
410: break;
411: }
412:
413:
414:
415: if ($actionID && empty($cfgSources[$source]['browse'])) {
416: Horde::url($prefs->getValue('initial_page'), true)
417: ->redirect();
418: }
419: }
420:
421: $templates = array();
422: if (isset($driver)) {
423: Turba::addBrowseJs();
424:
425:
426: $sources = Turba::getColumns();
427: $columns = isset($sources[$source]) ? $sources[$source] : array();
428: $sortorder = Turba::getPreferredSortOrder();
429:
430: if ($vars->get('key')) {
431:
432: try {
433: $list = $driver->getObject($vars->get('key'));
434: } catch (Turba_Exception $e) {
435: $notification->push(_("There was an error displaying the list"), 'horde.error');
436: $list = null;
437: }
438:
439: if ($list && $list->isGroup()) {
440: $title = sprintf(_("Contacts in list: %s"),
441: $list->getValue('name'));
442: $templates[] = '/browse/header.inc';
443:
444:
445: try {
446: $results = $list->listMembers($sortorder);
447: if (count($results) != $list->count()) {
448: $count = $list->count() - count($results);
449: $notification->push(
450: sprintf(ngettext("There is %d contact in this list that is not viewable to you",
451: "There are %d contacts in this list that are not viewable to you", $count),
452: $count),
453: 'horde.message');
454: }
455: $view = new Turba_View_List($results, null, $columns);
456: $view->setType('list');
457: } catch (Turba_Exception $e) {
458: $notification->push(_("Failed to browse list"), 'horde.error');
459: }
460: }
461: } else {
462:
463: $title = $cfgSources[$source]['title'];
464: $templates[] = '/browse/header.inc';
465: if (empty($cfgSources[$source]['browse'])) {
466: $notification->push(_("Your default address book is not browseable."), 'horde.warning');
467: } else {
468: $type_filter = array();
469: switch ($vars->get('show')) {
470: case 'contacts':
471: $type_filter = array('__type' => 'Object');
472: break;
473:
474: case 'lists':
475: $type_filter = array('__type' => 'Group');
476: break;
477: }
478:
479: try {
480: $results = $driver->search($type_filter, $sortorder, 'AND', $columns ? $columns : array('name'));
481: $view = new Turba_View_List($results, null, $columns);
482: $view->setType('directory');
483: } catch (Turba_Exception $e) {
484: $notification->push($e, 'horde.error');
485: }
486: }
487: }
488: } else {
489: $templates[] = '/browse/header.inc';
490: }
491:
492: Horde::addScriptFile('quickfinder.js', 'horde');
493: Horde::addScriptFile('effects.js', 'horde');
494: Horde::addScriptFile('redbox.js', 'horde');
495: require $registry->get('templates', 'horde') . '/common-header.inc';
496: require TURBA_TEMPLATES . '/menu.inc';
497: foreach ($templates as $template) {
498: require TURBA_TEMPLATES . $template;
499: }
500:
501: if (isset($view) && is_object($view)) {
502: $view->display();
503: }
504:
505: require $registry->get('templates', 'horde') . '/common-footer.inc';
506: }
507:
508: }
509: