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 header("Content-Length: ".strlen($content));
159 print $locale->translateCharset($content, 'UTF-8', $locale->getExportCharset());
162 function importVCard($filename, $module='Contacts'){
163 global $current_user;
164 $lines = file($filename);
166 $contact = loadBean($module);
168 $contact->title = 'imported';
169 $contact->assigned_user_id = $current_user->id;
173 for($index = 0; $index < sizeof($lines); $index++){
174 $line = $lines[$index];
176 // check the encoding and change it if needed
177 $locale = new Localization();
178 $encoding = $locale->detectCharset($line);
179 if ( $encoding != $GLOBALS['sugar_config']['default_charset'] ) {
180 $line = $locale->translateCharset($line,$encoding);
185 if(substr_count(strtoupper($line), 'END:VCARD')){
186 if(!isset($contact->last_name)){
187 $contact->last_name = $fullname;
191 $keyvalue = explode(':',$line);
192 if(sizeof($keyvalue)==2){
193 $value = $keyvalue[1];
194 for($newindex= $index + 1; $newindex < sizeof($lines), substr_count($lines[$newindex], ':') == 0; $newindex++){
195 $value .= $lines[$newindex];
198 $values = explode(';',$value );
199 $key = strtoupper($keyvalue[0]);
200 $key = strtr($key, '=', '');
201 $key = strtr($key, ',',';');
202 $keys = explode(';' ,$key);
204 if($keys[0] == 'TEL'){
205 if(substr_count($key, 'WORK') > 0){
206 if(substr_count($key, 'FAX') > 0){
207 if(!isset($contact->phone_fax)){
208 $contact->phone_fax = $value;
211 if(!isset($contact->phone_work)){
212 $contact->phone_work = $value;
216 if(substr_count($key, 'HOME') > 0){
217 if(substr_count($key, 'FAX') > 0){
218 if(!isset($contact->phone_fax)){
219 $contact->phone_fax = $value;
222 if(!isset($contact->phone_home)){
223 $contact->phone_home = $value;
227 if(substr_count($key, 'CELL') > 0){
228 if(!isset($contact->phone_mobile)){
229 $contact->phone_mobile = $value;
233 if(substr_count($key, 'FAX') > 0){
234 if(!isset($contact->phone_fax)){
235 $contact->phone_fax = $value;
242 if(sizeof($values) > 0)
243 $contact->last_name = $values[0];
244 if(sizeof($values) > 1)
245 $contact->first_name = $values[1];
246 if(sizeof($values) > 2)
247 $contact->salutation = $values[2];
252 if($keys[0] == 'FN'){
259 if($keys[0] == 'ADR'){
260 if(substr_count($key, 'WORK') > 0 && (substr_count($key, 'POSTAL') > 0|| substr_count($key, 'PARCEL') == 0)){
262 if(!isset($contact->primary_address_street) && sizeof($values) > 2){
263 $textBreaks = array("\n", "\r");
264 $vcardBreaks = array("=0A", "=0D");
265 $contact->primary_address_street = str_replace($vcardBreaks, $textBreaks, $values[2]);
267 if(!isset($contact->primary_address_city) && sizeof($values) > 3){
268 $contact->primary_address_city = $values[3];
270 if(!isset($contact->primary_address_state) && sizeof($values) > 4){
271 $contact->primary_address_state = $values[4];
273 if(!isset($contact->primary_address_postalcode) && sizeof($values) > 5){
274 $contact->primary_address_postalcode = $values[5];
276 if(!isset($contact->primary_address_country) && sizeof($values) > 6){
277 $contact->primary_address_country = $values[6];
282 if($keys[0] == 'TITLE'){
283 $contact->title = $value;
286 if($keys[0] == 'EMAIL'){
287 $field = 'email' . $email_suffix;
288 if(!isset($contact->$field)) {
289 $contact->$field = $value;
292 if($email_suffix == 1) {
293 $_REQUEST['email1'] = $value;
299 if($keys[0] == 'ORG'){
300 $GLOBALS['log']->debug('I found a company name');
302 $GLOBALS['log']->debug('I found a company name (fer real)');
303 if ( is_a($contact,"Contact") || is_a($contact,"Lead") ) {
304 $GLOBALS['log']->debug('And Im dealing with a person!');
305 $accountBean = loadBean('Accounts');
306 // It's a contact, we better try and match up an account
307 $full_company_name = trim($values[0]);
308 // Do we have a full company name match?
309 $result = $accountBean->retrieve_by_string_fields(array('name' => $full_company_name, 'deleted' => 0));
310 if ( ! isset($result->id) ) {
311 // Try to trim the full company name down, see if we get some other matches
312 $vCardTrimStrings = array('/ltd\.*/i'=>'',
318 // Allow users to override the trimming strings
319 if ( file_exists('custom/include/vCardTrimStrings.php') ) {
320 require_once('custom/include/vCardTrimStrings.php');
322 $short_company_name = trim(preg_replace(array_keys($vCardTrimStrings),$vCardTrimStrings,$full_company_name)," ,.");
324 $GLOBALS['log']->debug('Trying an extended search for: '.$short_company_name);
325 $result = $accountBean->retrieve_by_string_fields(array('name' => $short_company_name, 'deleted' => 0));
328 if ( is_a($contact,"Lead") || ! isset($result->id) ) {
329 // We could not find a parent account, or this is a lead so only copy the name, no linking
330 $GLOBALS['log']->debug("Did not find a matching company ($full_company_name)");
331 $contact->account_id = '';
332 $contact->account_name = $full_company_name;
334 $GLOBALS['log']->debug("Found a matching company: ".$result->name);
335 $contact->account_id = $result->id;
336 $contact->account_name = $result->name;
338 $contact->department = $values[1];
340 $contact->department = $value;
351 //FOUND THE BEGINING OF THE VCARD
352 if(!$start && substr_count(strtoupper($line), 'BEGIN:VCARD')){
358 if ( is_a($contact, "Contact") && empty($contact->account_id) && !empty($contact->account_name) ) {
359 $GLOBALS['log']->debug("Look ma! I'm creating a new account: ".$contact->account_name);
360 // We need to create a new account
361 $accountBean = loadBean('Accounts');
362 // Populate the newly created account with all of the contact information
363 foreach ( $contact->field_defs as $field_name => $field_def ) {
364 if ( !empty($contact->$field_name) ) {
365 $accountBean->$field_name = $contact->$field_name;
368 $accountBean->name = $contact->account_name;
369 $accountBean->save();
370 $contact->account_id = $accountBean->id;
373 $contactId = $contact->save();