]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - Zend/Gdata/Contacts/ListEntry.php
Release 6.5.0
[Github/sugarcrm.git] / Zend / Gdata / Contacts / ListEntry.php
1 <?php
2 /*********************************************************************************
3  * SugarCRM Community Edition is a customer relationship management program developed by
4  * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
5  * 
6  * This program is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License version 3 as published by the
8  * Free Software Foundation with the addition of the following permission added
9  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
11  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12  * 
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
16  * details.
17  * 
18  * You should have received a copy of the GNU Affero General Public License along with
19  * this program; if not, see http://www.gnu.org/licenses or write to the Free
20  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  * 02110-1301 USA.
22  * 
23  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
24  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
25  * 
26  * The interactive user interfaces in modified source and object code versions
27  * of this program must display Appropriate Legal Notices, as required under
28  * Section 5 of the GNU Affero General Public License version 3.
29  * 
30  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31  * these Appropriate Legal Notices must retain the display of the "Powered by
32  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
33  * technical reasons, the Appropriate Legal Notices must display the words
34  * "Powered by SugarCRM".
35  ********************************************************************************/
36
37
38
39 /**
40  * @see Zend_Gdata_Entry
41  */
42 require_once 'Zend/Gdata/Entry.php';
43
44 /**
45  * @see Zend_Gdata_Contacts_Extension_Name
46  */
47 require_once 'Zend/Gdata/Contacts/Extension/Name.php';
48
49 /**
50  * @see Zend_Gdata_Contacts_Extension_Birthday
51  */
52 require_once 'Zend/Gdata/Contacts/Extension/Birthday.php';
53
54 /**
55  * @see Zend_Gdata_Contacts_Extension_PhoneNumber
56  */
57 require_once 'Zend/Gdata/Contacts/Extension/PhoneNumber.php';
58
59 /**
60  * @see Zend_Gdata_Contacts_Extension_Email
61  */
62 require_once 'Zend/Gdata/Contacts/Extension/Email.php';
63
64 /**
65  * @see Zend_Gdata_Contacts_Extension_Address
66  */
67 require_once 'Zend/Gdata/Contacts/Extension/Address.php';
68
69 /**
70  * @see Zend_Gdata_Contacts_Extension_Address
71  */
72 require_once 'Zend/Gdata/Contacts/Extension/Organization.php';
73
74 /**
75  * @see Zend_Extension_Where
76  */
77 require_once 'Zend/Gdata/Extension/Where.php';
78
79 /**
80  * Represents a Contact entry in the Contact data API meta feed
81  *
82  */
83 class Zend_Gdata_Contacts_ListEntry extends Zend_Gdata_Entry
84 {
85
86     protected $_names = null;
87     protected $_birthday = null;
88     protected $_phones = array();
89     protected $_emails = array();
90     protected $_addresses = array();
91     protected $_organization = null;
92
93
94     public function __construct($element = null)
95     {
96         $this->registerAllNamespaces(Zend_Gdata_Contacts::$namespaces);
97         parent::__construct($element);
98     }
99
100     public function getDOM($doc = null, $majorVersion = 1, $minorVersion = null)
101     {
102         $element = parent::getDOM($doc, $majorVersion, $minorVersion);
103         return $element;
104     }
105
106     protected function takeChildFromDOM($child)
107     {
108         $absoluteNodeName = $child->namespaceURI . ':' . $child->localName;
109         switch ($absoluteNodeName) {
110
111             case $this->lookupNamespace('gd') . ':' . 'name';
112                 $item = new Zend_Gdata_Contacts_Extension_Name();
113                 $item->transferFromDOM($child);
114                 $this->_names = $item;
115             break;
116
117             case $this->lookupNamespace('gContact') . ':' . 'birthday';
118                 $item = new Zend_Gdata_Contacts_Extension_Birthday();
119                 $item->transferFromDOM($child);
120                 $this->_birthday = $item;
121             break;
122
123             case $this->lookupNamespace('gd') . ':' . 'phoneNumber';
124                 $item = new Zend_Gdata_Contacts_Extension_PhoneNumber();
125                 $item->transferFromDOM($child);
126                 $this->_phones[] = $item;
127             break;
128
129             case $this->lookupNamespace('gd') . ':' . 'email';
130                 $item = new Zend_Gdata_Contacts_Extension_Email();
131                 $item->transferFromDOM($child);
132                 $this->_emails[] = $item;
133             break;
134
135             case $this->lookupNamespace('gd') . ':' . 'structuredPostalAddress';
136                 $item = new Zend_Gdata_Contacts_Extension_Address();
137                 $item->transferFromDOM($child);
138                 $this->_addresses[] = $item;
139             break;
140
141             case $this->lookupNamespace('gd') . ':' . 'organization';
142                 $item = new Zend_Gdata_Contacts_Extension_Organization();
143                 $item->transferFromDOM($child);
144                 $this->_organization = $item;
145             break;
146
147             default:
148                 parent::takeChildFromDOM($child);
149             break;
150         }
151     }
152
153     public function toArray()
154     {
155         $entry = array( 'first_name' => '', 'last_name' => '', 'full_name' => '', 'id' => '', 'birthday' => '','email1' => '','email2' => '',
156                         'title' => '', 'account_name' => '', 'notes' => '', 'phone_main' => '','phone_mobile' => '',
157                         'alt_address_street' => '','alt_address_postcode' => '','alt_address_city' => '','alt_address_state' => '','alt_address_country' => '',
158                         'primary_address_street' => '','primary_address_postcode' => '','primary_address_city' => '','primary_address_state' => '','primary_address_country' => '',
159                         'team_name' => '', 'assigned_user_name' => ''
160                         );
161
162         if($this->_names != null)
163             $entry = array_merge($entry, $this->_names->toArray() );
164
165         //Get the self link so we can query for the contact details at a later date
166         foreach($this->_link as $linkEntry)
167         {
168             $linkRel = $linkEntry->getRel();
169             if( $linkRel != null && $linkRel == "self" )
170             {
171                 continue;
172             }
173         }
174
175         //Get addresses
176         foreach($this->_addresses as $address)
177         {
178             $entry = array_merge($entry, $address->toArray() );
179         }
180         //Process phones
181         foreach($this->_phones as $phoneEntry)
182         {
183             $key = "phone_" . $phoneEntry->getPhoneType();
184             $entry[$key] = $phoneEntry->getNumber();
185         }
186
187         //Process emails
188          $entry = array_merge($entry, $this->getEmailAddresses() );
189
190         //Get Notes
191         if($this->_content != null)
192             $entry['notes'] = $this->getContent()->getText();
193
194         //ID
195         $entry['id'] = $this->getId()->getText();
196
197         //Birthday
198         if($this->_birthday != null)
199             $entry['birthday'] = $this->_birthday->getBirthday();
200
201         //Organization name and title
202         if($this->_organization != null)
203         {
204             $entry['account_name'] = $this->_organization->getOrganizationName();
205             $entry['title'] = $this->_organization->getOrganizationTitle();
206         }
207
208         return $entry;
209     }
210
211     protected function getEmailAddresses()
212     {
213         $results = array();
214         $primaryEmail = $this->getPrimaryEmail();
215         if($primaryEmail !== FALSE)
216             $results['email1'] =  $primaryEmail;
217         else
218         {
219             $nonPrimaryEmail = $this->getNextNonPrimaryEmail();
220             if($nonPrimaryEmail !== FALSE)
221                 $results['email1'] = $nonPrimaryEmail;
222             else
223                 return array();
224         }
225
226         $secondaryEmail = $this->getNextNonPrimaryEmail();
227         if($secondaryEmail !== FALSE)
228             $results['email2'] = $secondaryEmail;
229         
230         return $results;
231
232     }
233     protected function getPrimaryEmail()
234     {
235         $results = FALSE;
236         foreach($this->_emails as $emailEntry)
237         {
238             if( $emailEntry->isPrimary() )
239                 return $emailEntry->getEmail();
240         }
241         return $results;
242     }
243
244     protected function getNextNonPrimaryEmail()
245     {
246         $results = FALSE;
247         foreach($this->_emails as $k => $emailEntry)
248         {
249             if( !$emailEntry->isPrimary() )
250             {
251                 $results = $emailEntry->getEmail();
252                 unset($this->_emails[$k]);
253                 return $results;
254             }
255         }
256         return $results;
257     }
258
259 }