2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4 * SugarCRM 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 var $properties = array();
46 var $name = 'no_name';
49 $this->properties = array();
52 function loadContact($contactid, $module='Contacts') {
53 global $app_list_strings;
55 require_once($GLOBALS['beanFiles'][$GLOBALS['beanList'][$module]]);
56 $contact = new $GLOBALS['beanList'][$module]();
57 $contact->retrieve($contactid);
59 // cn: bug 8504 - CF/LB break Outlook's vCard import
60 $bad = array("\n", "\r");
61 $good = array("=0A", "=0D");
63 if(strpos($contact->primary_address_street, "\n") || strpos($contact->primary_address_street, "\r")) {
64 $contact->primary_address_street = str_replace($bad, $good, $contact->primary_address_street);
65 $encoding = 'QUOTED-PRINTABLE';
68 $this->setName(from_html($contact->first_name), from_html($contact->last_name), $app_list_strings['salutation_dom'][from_html($contact->salutation)]);
69 if ( isset($contact->birthdate) )
70 $this->setBirthDate(from_html($contact->birthdate));
71 $this->setPhoneNumber(from_html($contact->phone_fax), 'FAX');
72 $this->setPhoneNumber(from_html($contact->phone_home), 'HOME');
73 $this->setPhoneNumber(from_html($contact->phone_mobile), 'CELL');
74 $this->setPhoneNumber(from_html($contact->phone_work), 'WORK');
75 $this->setEmail(from_html($contact->email1));
76 $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);
77 if ( isset($contact->account_name) )
78 $this->setORG(from_html($contact->account_name), from_html($contact->department));
80 $this->setORG('', from_html($contact->department));
81 $this->setTitle($contact->title);
84 function setTitle($title){
85 $this->setProperty("TITLE",$title );
87 function setORG($org, $dep){
88 $this->setProperty("ORG","$org;$dep" );
90 function setAddress($address, $city, $state,$postal, $country, $type, $encoding=''){
91 if(!empty($encoding)) {
92 $encoding = ";ENCODING={$encoding}";
94 $this->setProperty("ADR;$type$encoding",";;$address;$city;$state;$postal;$country" );
97 function setName($first_name, $last_name, $prefix){
98 $this->name = strtr($first_name.'_'.$last_name, ' ' , '_');
99 $this->setProperty('N',$last_name.';'.$first_name.';'.$prefix );
100 $this->setProperty('FN',"$prefix $first_name $last_name");
103 function setEmail($address){
104 $this->setProperty('EMAIL;INTERNET', $address);
107 function setPhoneNumber( $number, $type){
108 $this->setProperty("TEL;$type", $number);
110 function setBirthDate($date){
111 $this->setProperty('BDAY',$date);
113 function getProperty($name){
114 if(isset($this->properties[$name]))
115 return $this->properties[$name];
119 function setProperty($name, $value){
120 $this->properties[$name] = $value;
124 $temp = "BEGIN:VCARD\n";
125 foreach($this->properties as $key=>$value){
126 $temp .= $key. ':'.$value."\n";
128 $temp.= "END:VCARD\n";
134 function saveVCard(){
137 $content = $this->toString();
138 header("Content-Disposition: attachment; filename={$this->name}.vcf");
139 header("Content-Type: text/x-vcard; charset=".$locale->getExportCharset());
140 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
141 header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT" );
142 header("Cache-Control: max-age=0");
143 header("Pragma: public");
144 header("Content-Length: ".strlen($content));
146 print $locale->translateCharset($content, 'UTF-8', $locale->getExportCharset());
149 function importVCard($filename, $module='Contacts'){
150 global $current_user;
151 $lines = file($filename);
153 $contact = loadBean($module);
155 $contact->title = 'imported';
156 $contact->assigned_user_id = $current_user->id;
160 for($index = 0; $index < sizeof($lines); $index++){
161 $line = $lines[$index];
163 // check the encoding and change it if needed
164 $locale = new Localization();
165 $encoding = $locale->detectCharset($line);
166 if ( $encoding != $GLOBALS['sugar_config']['default_charset'] ) {
167 $line = $locale->translateCharset($line,$encoding);
172 if(substr_count(strtoupper($line), 'END:VCARD')){
173 if(!isset($contact->last_name)){
174 $contact->last_name = $fullname;
178 $keyvalue = explode(':',$line);
179 if(sizeof($keyvalue)==2){
180 $value = $keyvalue[1];
181 for($newindex= $index + 1; $newindex < sizeof($lines), substr_count($lines[$newindex], ':') == 0; $newindex++){
182 $value .= $lines[$newindex];
185 $values = explode(';',$value );
186 $key = strtoupper($keyvalue[0]);
187 $key = strtr($key, '=', '');
188 $key = strtr($key, ',',';');
189 $keys = explode(';' ,$key);
191 if($keys[0] == 'TEL'){
192 if(substr_count($key, 'WORK') > 0){
193 if(substr_count($key, 'FAX') > 0){
194 if(!isset($contact->phone_fax)){
195 $contact->phone_fax = $value;
198 if(!isset($contact->phone_work)){
199 $contact->phone_work = $value;
203 if(substr_count($key, 'HOME') > 0){
204 if(substr_count($key, 'FAX') > 0){
205 if(!isset($contact->phone_fax)){
206 $contact->phone_fax = $value;
209 if(!isset($contact->phone_home)){
210 $contact->phone_home = $value;
214 if(substr_count($key, 'CELL') > 0){
215 if(!isset($contact->phone_mobile)){
216 $contact->phone_mobile = $value;
220 if(substr_count($key, 'FAX') > 0){
221 if(!isset($contact->phone_fax)){
222 $contact->phone_fax = $value;
229 if(sizeof($values) > 0)
230 $contact->last_name = $values[0];
231 if(sizeof($values) > 1)
232 $contact->first_name = $values[1];
233 if(sizeof($values) > 2)
234 $contact->salutation = $values[2];
239 if($keys[0] == 'FN'){
246 if($keys[0] == 'ADR'){
247 if(substr_count($key, 'WORK') > 0 && (substr_count($key, 'POSTAL') > 0|| substr_count($key, 'PARCEL') == 0)){
249 if(!isset($contact->primary_address_street) && sizeof($values) > 2){
250 $textBreaks = array("\n", "\r");
251 $vcardBreaks = array("=0A", "=0D");
252 $contact->primary_address_street = str_replace($vcardBreaks, $textBreaks, $values[2]);
254 if(!isset($contact->primary_address_city) && sizeof($values) > 3){
255 $contact->primary_address_city = $values[3];
257 if(!isset($contact->primary_address_state) && sizeof($values) > 4){
258 $contact->primary_address_state = $values[4];
260 if(!isset($contact->primary_address_postalcode) && sizeof($values) > 5){
261 $contact->primary_address_postalcode = $values[5];
263 if(!isset($contact->primary_address_country) && sizeof($values) > 6){
264 $contact->primary_address_country = $values[6];
269 if($keys[0] == 'TITLE'){
270 $contact->title = $value;
273 if($keys[0] == 'EMAIL'){
274 $field = 'email' . $email_suffix;
275 if(!isset($contact->$field)) {
276 $contact->$field = $value;
279 if($email_suffix == 1) {
280 $_REQUEST['email1'] = $value;
286 if($keys[0] == 'ORG'){
287 $GLOBALS['log']->debug('I found a company name');
289 $GLOBALS['log']->debug('I found a company name (fer real)');
290 if ( is_a($contact,"Contact") || is_a($contact,"Lead") ) {
291 $GLOBALS['log']->debug('And Im dealing with a person!');
292 $accountBean = loadBean('Accounts');
293 // It's a contact, we better try and match up an account
294 $full_company_name = trim($values[0]);
295 // Do we have a full company name match?
296 $result = $accountBean->retrieve_by_string_fields(array('name' => $full_company_name, 'deleted' => 0));
297 if ( ! isset($result->id) ) {
298 // Try to trim the full company name down, see if we get some other matches
299 $vCardTrimStrings = array('/ltd\.*/i'=>'',
305 // Allow users to override the trimming strings
306 if ( file_exists('custom/include/vCardTrimStrings.php') ) {
307 require_once('custom/include/vCardTrimStrings.php');
309 $short_company_name = trim(preg_replace(array_keys($vCardTrimStrings),$vCardTrimStrings,$full_company_name)," ,.");
311 $GLOBALS['log']->debug('Trying an extended search for: '.$short_company_name);
312 $result = $accountBean->retrieve_by_string_fields(array('name' => $short_company_name, 'deleted' => 0));
315 if ( is_a($contact,"Lead") || ! isset($result->id) ) {
316 // We could not find a parent account, or this is a lead so only copy the name, no linking
317 $GLOBALS['log']->debug("Did not find a matching company ($full_company_name)");
318 $contact->account_id = '';
319 $contact->account_name = $full_company_name;
321 $GLOBALS['log']->debug("Found a matching company: ".$result->name);
322 $contact->account_id = $result->id;
323 $contact->account_name = $result->name;
325 $contact->department = $values[1];
327 $contact->department = $value;
338 //FOUND THE BEGINING OF THE VCARD
339 if(!$start && substr_count(strtoupper($line), 'BEGIN:VCARD')){
345 if ( is_a($contact, "Contact") && empty($contact->account_id) && !empty($contact->account_name) ) {
346 $GLOBALS['log']->debug("Look ma! I'm creating a new account: ".$contact->account_name);
347 // We need to create a new account
348 $accountBean = loadBean('Accounts');
349 // Populate the newly created account with all of the contact information
350 foreach ( $contact->field_defs as $field_name => $field_def ) {
351 if ( !empty($contact->$field_name) ) {
352 $accountBean->$field_name = $contact->$field_name;
355 $accountBean->name = $contact->account_name;
356 $accountBean->save();
357 $contact->account_id = $accountBean->id;
360 $contactId = $contact->save();