1: <?php
2: /**
3: * Copyright 2014 Horde LLC (http://www.horde.org/)
4: *
5: * See the enclosed file COPYING for license information (GPL). If you
6: * did not receive this file, see http://www.horde.org/licenses/gpl.
7: *
8: * @category Horde
9: * @copyright 2014 Horde LLC
10: * @license http://www.horde.org/licenses/gpl GPL
11: * @package IMP
12: */
13:
14: /**
15: * Outputs an address list in the form used by the browser-side javascript
16: * code.
17: *
18: * @author Michael Slusarz <slusarz@horde.org>
19: * @category Horde
20: * @copyright 2014 Horde LLC
21: * @license http://www.horde.org/licenses/gpl GPL
22: * @package IMP
23: */
24: class IMP_Ajax_Addresses
25: {
26: /**
27: * Address object.
28: *
29: * @var Horde_Mail_Rfc822_Object
30: */
31: private $_addr;
32:
33: /**
34: * Address counter for toArray().
35: *
36: * @var integer
37: */
38: private $_count;
39:
40: /**
41: * Constructor.
42: *
43: * @param Horde_Mail_Rfc822_Object $addr Address object.
44: */
45: public function __construct(Horde_Mail_Rfc822_Object $addr)
46: {
47: $this->_addr = $addr;
48: }
49:
50: /**
51: * Output the address list.
52: *
53: * @param integer $limit Limit display to this many addresses. If null,
54: * shows all addresses.
55: *
56: * @return object Object with the following properties:
57: * - addr: (array) Array of objects with 2 possible formats:
58: * - Address keys: 'b' (bare address); 'p' (personal part)
59: * - Group keys: 'a' (list of addresses); 'g' (group name)
60: * - limit: (boolean) True if limit was reached.
61: * - total: (integer) Total address count.
62: */
63: public function toArray($limit = null)
64: {
65: $out = new stdClass;
66: $out->addr = array();
67: $out->limit = false;
68: $out->total = count($this->_addr);
69:
70: $this->_count = 0;
71:
72: foreach ($this->_addr->base_addresses as $ob) {
73: if ($limit && ($this->_count > $limit)) {
74: $out->limit = true;
75: break;
76: }
77:
78: if ($ob instanceof Horde_Mail_Rfc822_Group) {
79: $ob->addresses->unique();
80:
81: $tmp = new stdClass;
82: $tmp->a = array();
83: $tmp->g = $ob->groupname;
84:
85: foreach ($ob->addresses as $val) {
86: $tmp->a[] = $this->_addAddress($val);
87: if ($limit && ($this->_count > $limit)) {
88: break;
89: }
90: }
91: } else {
92: $tmp = $this->_addAddress($ob);
93: }
94:
95: $out->addr[] = $tmp;
96: }
97:
98: return $out;
99: }
100:
101: /**
102: * Create a bare address entry.
103: *
104: * @param Horde_Mail_Rfc822_Address $addr Address object.
105: *
106: * @return object Address object for use in JS code.
107: */
108: private function _addAddress(Horde_Mail_Rfc822_Address $addr)
109: {
110: $tmp = new stdClass;
111: if (strlen($b = $addr->bare_address)) {
112: $tmp->b = $b;
113: }
114: if (strlen($p = $addr->personal)) {
115: $tmp->p = $p;
116: }
117:
118: ++$this->_count;
119:
120: return $tmp;
121: }
122:
123: }
124: