2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4 * SugarCRM Community Edition is a customer relationship management program developed by
5 * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
7 * This program is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU Affero General Public License version 3 as published by the
9 * Free Software Foundation with the addition of the following permission added
10 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
11 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
12 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
19 * You should have received a copy of the GNU Affero General Public License along with
20 * this program; if not, see http://www.gnu.org/licenses or write to the Free
21 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
25 * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
27 * The interactive user interfaces in modified source and object code versions
28 * of this program must display Appropriate Legal Notices, as required under
29 * Section 5 of the GNU Affero General Public License version 3.
31 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
32 * these Appropriate Legal Notices must retain the display of the "Powered by
33 * SugarCRM" logo. If the display of the logo is not reasonably feasible for
34 * technical reasons, the Appropriate Legal Notices must display the words
35 * "Powered by SugarCRM".
36 ********************************************************************************/
38 /*********************************************************************************
41 ********************************************************************************/
45 protected $properties = array();
47 protected $name = 'no_name';
49 public function clear()
51 $this->properties = array();
54 function loadContact($contactid, $module='Contacts') {
55 global $app_list_strings;
57 require_once($GLOBALS['beanFiles'][$GLOBALS['beanList'][$module]]);
58 $contact = new $GLOBALS['beanList'][$module]();
59 $contact->retrieve($contactid);
60 // cn: bug 8504 - CF/LB break Outlook's vCard import
61 $bad = array("\n", "\r");
62 $good = array("=0A", "=0D");
64 if(strpos($contact->primary_address_street, "\n") || strpos($contact->primary_address_street, "\r")) {
65 $contact->primary_address_street = str_replace($bad, $good, $contact->primary_address_street);
66 $encoding = 'QUOTED-PRINTABLE';
69 $this->setName(from_html($contact->first_name), from_html($contact->last_name), $app_list_strings['salutation_dom'][from_html($contact->salutation)]);
70 if ( isset($contact->birthdate) )
71 $this->setBirthDate(from_html($contact->birthdate));
72 $this->setPhoneNumber(from_html($contact->phone_fax), 'FAX');
73 $this->setPhoneNumber(from_html($contact->phone_home), 'HOME');
74 $this->setPhoneNumber(from_html($contact->phone_mobile), 'CELL');
75 $this->setPhoneNumber(from_html($contact->phone_work), 'WORK');
76 $this->setEmail(from_html($contact->email1));
77 $this->setAddress(from_html($contact->primary_address_street), from_html($contact->primary_address_city), from_html($contact->primary_address_state), from_html($contact->primary_address_postalcode), from_html($contact->primary_address_country), 'WORK', $encoding);
78 if ( isset($contact->account_name) )
79 $this->setORG(from_html($contact->account_name), from_html($contact->department));
81 $this->setORG('', from_html($contact->department));
82 $this->setTitle($contact->title);
85 function setTitle($title){
86 $this->setProperty("TITLE",$title );
88 function setORG($org, $dep){
89 $this->setProperty("ORG","$org;$dep" );
91 function setAddress($address, $city, $state,$postal, $country, $type, $encoding=''){
92 if(!empty($encoding)) {
93 $encoding = ";ENCODING={$encoding}";
95 $this->setProperty("ADR;$type$encoding",";;$address;$city;$state;$postal;$country" );
98 function setName($first_name, $last_name, $prefix){
99 $this->name = strtr($first_name.'_'.$last_name, ' ' , '_');
100 $this->setProperty('N',$last_name.';'.$first_name.';;'.$prefix );
101 $this->setProperty('FN',"$prefix $first_name $last_name");
104 function setEmail($address){
105 $this->setProperty('EMAIL;INTERNET', $address);
108 function setPhoneNumber( $number, $type)
111 $this->setProperty("TEL;$type", $number);
114 $this->setProperty("TEL;WORK;$type", $number);
117 function setBirthDate($date){
118 $this->setProperty('BDAY',$date);
120 function getProperty($name){
121 if(isset($this->properties[$name]))
122 return $this->properties[$name];
126 function setProperty($name, $value){
127 $this->properties[$name] = $value;
132 $temp = "BEGIN:VCARD\n";
133 foreach($this->properties as $key=>$value){
135 $temp .= $key. ';CHARSET='.strtolower($locale->getExportCharset()).':'.$value."\n";
137 $temp .= $key. ':'.$value."\n";
140 $temp.= "END:VCARD\n";
146 function saveVCard(){
148 $content = $this->toString();
149 if ( !defined('SUGAR_PHPUNIT_RUNNER') ) {
150 header("Content-Disposition: attachment; filename={$this->name}.vcf");
151 header("Content-Type: text/x-vcard; charset=".$locale->getExportCharset());
152 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
153 header("Last-Modified: " . TimeDate::httpTime() );
154 header("Cache-Control: max-age=0");
155 header("Pragma: public");
156 //bug45856 IIS Doesn't like this to be set and it causes the vCard to not get saved
157 if (preg_match('iis', $_SERVER['SERVER_SOFTWARE']))
159 header("Content-Length: ".strlen($content));
163 print $locale->translateCharset($content, 'UTF-8', $locale->getExportCharset());
166 function importVCard($filename, $module='Contacts'){
167 global $current_user;
168 $lines = file($filename);
170 $contact = loadBean($module);
172 $contact->title = 'imported';
173 $contact->assigned_user_id = $current_user->id;
177 for($index = 0; $index < sizeof($lines); $index++){
178 $line = $lines[$index];
180 // check the encoding and change it if needed
181 $locale = new Localization();
182 $encoding = $locale->detectCharset($line);
183 if ( $encoding != $GLOBALS['sugar_config']['default_charset'] ) {
184 $line = $locale->translateCharset($line,$encoding);
189 if(substr_count(strtoupper($line), 'END:VCARD')){
190 if(!isset($contact->last_name)){
191 $contact->last_name = $fullname;
195 $keyvalue = explode(':',$line);
196 if(sizeof($keyvalue)==2){
197 $value = $keyvalue[1];
198 for($newindex= $index + 1; $newindex < sizeof($lines), substr_count($lines[$newindex], ':') == 0; $newindex++){
199 $value .= $lines[$newindex];
202 $values = explode(';',$value );
203 $key = strtoupper($keyvalue[0]);
204 $key = strtr($key, '=', '');
205 $key = strtr($key, ',',';');
206 $keys = explode(';' ,$key);
208 if($keys[0] == 'TEL'){
209 if(substr_count($key, 'WORK') > 0){
210 if(substr_count($key, 'FAX') > 0){
211 if(!isset($contact->phone_fax)){
212 $contact->phone_fax = $value;
215 if(!isset($contact->phone_work)){
216 $contact->phone_work = $value;
220 if(substr_count($key, 'HOME') > 0){
221 if(substr_count($key, 'FAX') > 0){
222 if(!isset($contact->phone_fax)){
223 $contact->phone_fax = $value;
226 if(!isset($contact->phone_home)){
227 $contact->phone_home = $value;
231 if(substr_count($key, 'CELL') > 0){
232 if(!isset($contact->phone_mobile)){
233 $contact->phone_mobile = $value;
237 if(substr_count($key, 'FAX') > 0){
238 if(!isset($contact->phone_fax)){
239 $contact->phone_fax = $value;
246 if(sizeof($values) > 0)
247 $contact->last_name = $values[0];
248 if(sizeof($values) > 1)
249 $contact->first_name = $values[1];
250 if(sizeof($values) > 2)
251 $contact->salutation = $values[2];
256 if($keys[0] == 'FN'){
263 if($keys[0] == 'ADR'){
264 if(substr_count($key, 'WORK') > 0 && (substr_count($key, 'POSTAL') > 0|| substr_count($key, 'PARCEL') == 0)){
266 if(!isset($contact->primary_address_street) && sizeof($values) > 2){
267 $textBreaks = array("\n", "\r");
268 $vcardBreaks = array("=0A", "=0D");
269 $contact->primary_address_street = str_replace($vcardBreaks, $textBreaks, $values[2]);
271 if(!isset($contact->primary_address_city) && sizeof($values) > 3){
272 $contact->primary_address_city = $values[3];
274 if(!isset($contact->primary_address_state) && sizeof($values) > 4){
275 $contact->primary_address_state = $values[4];
277 if(!isset($contact->primary_address_postalcode) && sizeof($values) > 5){
278 $contact->primary_address_postalcode = $values[5];
280 if(!isset($contact->primary_address_country) && sizeof($values) > 6){
281 $contact->primary_address_country = $values[6];
286 if($keys[0] == 'TITLE'){
287 $contact->title = $value;
290 if($keys[0] == 'EMAIL'){
291 $field = 'email' . $email_suffix;
292 if(!isset($contact->$field)) {
293 $contact->$field = $value;
296 if($email_suffix == 1) {
297 $_REQUEST['email1'] = $value;
303 if($keys[0] == 'ORG'){
304 $GLOBALS['log']->debug('I found a company name');
306 $GLOBALS['log']->debug('I found a company name (fer real)');
307 if ( is_a($contact,"Contact") || is_a($contact,"Lead") ) {
308 $GLOBALS['log']->debug('And Im dealing with a person!');
309 $accountBean = loadBean('Accounts');
310 // It's a contact, we better try and match up an account
311 $full_company_name = trim($values[0]);
312 // Do we have a full company name match?
313 $result = $accountBean->retrieve_by_string_fields(array('name' => $full_company_name, 'deleted' => 0));
314 if ( ! isset($result->id) ) {
315 // Try to trim the full company name down, see if we get some other matches
316 $vCardTrimStrings = array('/ltd\.*/i'=>'',
322 // Allow users to override the trimming strings
323 if ( file_exists('custom/include/vCardTrimStrings.php') ) {
324 require_once('custom/include/vCardTrimStrings.php');
326 $short_company_name = trim(preg_replace(array_keys($vCardTrimStrings),$vCardTrimStrings,$full_company_name)," ,.");
328 $GLOBALS['log']->debug('Trying an extended search for: '.$short_company_name);
329 $result = $accountBean->retrieve_by_string_fields(array('name' => $short_company_name, 'deleted' => 0));
332 if ( is_a($contact,"Lead") || ! isset($result->id) ) {
333 // We could not find a parent account, or this is a lead so only copy the name, no linking
334 $GLOBALS['log']->debug("Did not find a matching company ($full_company_name)");
335 $contact->account_id = '';
336 $contact->account_name = $full_company_name;
338 $GLOBALS['log']->debug("Found a matching company: ".$result->name);
339 $contact->account_id = $result->id;
340 $contact->account_name = $result->name;
342 $contact->department = $values[1];
344 $contact->department = $value;
355 //FOUND THE BEGINING OF THE VCARD
356 if(!$start && substr_count(strtoupper($line), 'BEGIN:VCARD')){
362 if ( is_a($contact, "Contact") && empty($contact->account_id) && !empty($contact->account_name) ) {
363 $GLOBALS['log']->debug("Look ma! I'm creating a new account: ".$contact->account_name);
364 // We need to create a new account
365 $accountBean = loadBean('Accounts');
366 // Populate the newly created account with all of the contact information
367 foreach ( $contact->field_defs as $field_name => $field_def ) {
368 if ( !empty($contact->$field_name) ) {
369 $accountBean->$field_name = $contact->$field_name;
372 $accountBean->name = $contact->account_name;
373 $accountBean->save();
374 $contact->account_id = $accountBean->id;
377 $contactId = $contact->save();