private function _updateContact($user, $attributes) { $contact = $user->createContact(); if ($contact) { \GO::debug('LDAPAUTH: updating user contact'); $contact->setAttributes($attributes); $contact->cutAttributeLengths(); $contact->skip_user_update = true; if (!empty($attributes['company'])) { $company = \GO\Addressbook\Model\Company::model()->findSingleByAttributes(array('addressbook_id' => $contact->addressbook_id, 'name' => $attributes['company'])); if (!$company) { \GO::debug('LDAPAUTH: creating company for contact'); $company = new \GO\Addressbook\Model\Company(); $company->name = $attributes['company']; $company->addressbook_id = $contact->addressbook_id; $company->cutAttributeLengths(); $company->save(); } else { \GO::debug('LDAPAUTH: found existing company for contact'); } $contact->company_id = $company->id; } $contact->save(); } }
/** * Before importing a contact in the database first check if the company name of this contact * Is a company that excists in the database. If not create a company. After this set the id * of the create company to the contact we insert. * * If the email addres set to a contact does not validate. Remove it so import wont fail */ protected function beforeImport($params, &$model, &$attributes, $record) { $impBasParams = json_decode($params['importBaseParams'], true); $addressbookId = $impBasParams['addressbook_id']; if (!empty($attributes['Company'])) { $companyName = $attributes['Company']; } else { if (!empty($attributes['company'])) { $companyName = $attributes['company']; } else { if (!empty($attributes['company_name'])) { $companyName = $attributes['company_name']; } else { if (!empty($attributes['companyName'])) { $companyName = $attributes['companyName']; } else { if (!empty($attributes['name'])) { $companyName = $attributes['name']; } } } } } if (!empty($companyName)) { $companyModel = \GO\Addressbook\Model\Company::model()->find(\GO\Base\Db\FindParams::newInstance()->single()->criteria(\GO\Base\Db\FindCriteria::newInstance()->addCondition('name', $companyName)->addCondition('addressbook_id', $addressbookId))); if (empty($companyModel)) { $companyModel = new \GO\Addressbook\Model\Company(); $companyModel->setAttributes(array('name' => $companyName, 'addressbook_id' => $addressbookId)); $companyModel->save(); } $model->company_id = $companyModel->id; } if (isset($attributes['email']) && !\GO\Base\Util\String::validate_email($attributes['email'])) { unset($attributes['email']); } if (isset($attributes['email2']) && !\GO\Base\Util\String::validate_email($attributes['email2'])) { unset($attributes['email2']); } if (isset($attributes['email3']) && !\GO\Base\Util\String::validate_email($attributes['email3'])) { unset($attributes['email3']); } return parent::beforeImport($params, $model, $attributes, $record); }
/** * Import a contact (with or without company) from a VObject * * @param Sabre\VObject\Component $vobject * @param array $attributes Extra attributes to apply to the contact. Raw values should be past. No input formatting is applied. * @return Contact */ public function importVObject(Sabre\VObject\Component $vobject, $attributes = array(), $saveToDb = true, $ignoreInvalidProperties = true) { //$event = new \GO\Calendar\Model\Event(); $companyAttributes = array(); // if (!empty($attributes['addressbook_id'])) { // $companyAttributes['addressbook_id'] = $attributes['addressbook_id']; // } $uid = (string) $vobject->uid; if (!empty($uid) && empty($attributes['uuid'])) { $attributes['uuid'] = $uid; } $emails = array(); foreach ($vobject->children as $vobjProp) { switch ($vobjProp->name) { case 'PHOTO': if ($vobjProp->getValue()) { $photoFile = \GO\Base\Fs\File::tempFile('', 'jpg'); $photoFile->putContents($vobjProp->getValue()); } break; case 'N': $nameArr = explode(';', $vobjProp->getValue()); if (isset($nameArr[0])) { $attributes['last_name'] = $nameArr[0]; } if (isset($nameArr[1])) { $attributes['first_name'] = $nameArr[1]; } $attributes['middle_name'] = !empty($nameArr[2]) ? $nameArr[2] : ''; $attributes['suffix'] = !empty($nameArr[4]) ? $nameArr[4] : ''; $attributes['title'] = !empty($nameArr[3]) ? $nameArr[3] : ''; break; case 'ORG': $companyAttributes['name'] = null; if ($vobjProp->getValue()) { $compNameArr = explode(';', $vobjProp->getValue()); if (!empty($compNameArr[0])) { $companyAttributes['name'] = $compNameArr[0]; } if (!empty($compNameArr[1])) { $attributes['department'] = $compNameArr[1]; } if (!empty($compNameArr[2])) { $companyAttributes['name2'] = $compNameArr[2]; } } break; // case 'TITLE': // $attributes['title'] = $vobjProp->getValue() ? $vobjProp->getValue() : null; // break; // case 'TITLE': // $attributes['title'] = $vobjProp->getValue() ? $vobjProp->getValue() : null; // break; case 'TEL': if ($vobjProp->getValue()) { $types = array(); foreach ($vobjProp->parameters as $param) { if ($param->name == 'TYPE') { $types = explode(',', strtolower($param->getValue())); } } if (in_array('work', $types) && (in_array('voice', $types) || count($types) == 1)) { $attributes['work_phone'] = $vobjProp->getValue(); $companyAttributes['phone'] = $vobjProp->getValue(); } if (in_array('cell', $types) && (in_array('voice', $types) || count($types) == 1)) { if (empty($attributes['cellular'])) { $attributes['cellular'] = $vobjProp->getValue(); } elseif (empty($attributes['cellular2'])) { $attributes['cellular2'] = $vobjProp->getValue(); } } if (in_array('fax', $types) && in_array('home', $types)) { $attributes['fax'] = $vobjProp->getValue(); } if (in_array('fax', $types) && in_array('work', $types)) { $companyAttributes['fax'] = $vobjProp->getValue(); $attributes['work_fax'] = $vobjProp->getValue(); } if (in_array('home', $types) && (in_array('voice', $types) || count($types) == 1)) { $attributes['home_phone'] = $vobjProp->getValue(); } } // foreach ($vobjProp->parameters as $param) { // if ($param['name']=='TYPE') { // switch (susbstr($param['value'],0,4)) { // case 'work': // $attributes['work_phone'] = $vobjProp->getValue(); // break; // default: // $attributes['home_phone'] = $vobjProp->getValue(); // break; // } // } // } break; // case 'LABEL': // case 'LABEL': case 'ADR': $types = array(); foreach ($vobjProp->parameters as $param) { if ($param->name == 'TYPE') { $types = explode(',', strtolower($param->getValue())); } } if (in_array('work', $types)) { $addrArr = explode(';', $vobjProp->getValue()); if (isset($addrArr[2])) { $companyAttributes['address'] = $addrArr[2]; } if (isset($addrArr[3])) { $companyAttributes['city'] = $addrArr[3]; } if (isset($addrArr[4])) { $companyAttributes['state'] = $addrArr[4]; } if (isset($addrArr[5])) { $companyAttributes['zip'] = $addrArr[5]; } if (isset($addrArr[6])) { $companyAttributes['country'] = $addrArr[6]; } } if (in_array('home', $types)) { $addrArr = explode(';', $vobjProp->getValue()); if (isset($addrArr[2])) { $attributes['address'] = $addrArr[2]; } if (isset($addrArr[3])) { $attributes['city'] = $addrArr[3]; } if (isset($addrArr[4])) { $attributes['state'] = $addrArr[4]; } if (isset($addrArr[5])) { $attributes['zip'] = $addrArr[5]; } if (isset($addrArr[6])) { $attributes['country'] = $addrArr[6]; } } if (empty($types)) { $addrArr = explode(';', $vobjProp->getValue()); if (isset($addrArr[2])) { $companyAttributes['post_address'] = $addrArr[2]; } if (isset($addrArr[3])) { $companyAttributes['post_city'] = $addrArr[3]; } if (isset($addrArr[4])) { $companyAttributes['post_state'] = $addrArr[4]; } if (isset($addrArr[5])) { $companyAttributes['post_zip'] = $addrArr[5]; } if (isset($addrArr[6])) { $companyAttributes['post_country'] = $addrArr[6]; } } break; case 'EMAIL': // foreach ($vobjProp->parameters as $param) { // if ($param->name=='TYPE') // $types = explode(',',strtolower($param->getValue())); // else // $types = array(); // } // if(in_array('pref',$types)) { // $attributes['email'] = $vobjProp->getValue(); // } elseif(in_array('home',$types)) { // $attributes['email2'] = $vobjProp->getValue(); // } elseif(in_array('work',$types)) { // $attributes['email3'] = $vobjProp->getValue(); // } else { // $attributes['email'] = $vobjProp->getValue(); // } if ($vobjProp->getValue()) { $emails[] = $vobjProp->getValue(); } break; case 'TITLE': $attributes['function'] = $vobjProp->getValue(); break; case 'BDAY': if ($vobjProp->getValue()) { // is already formatted in GO\Base\VObject\Reader::convertVCard21ToVCard30 // $attributes['birthday'] = substr($vobjProp->getValue(),0,4).'-'.substr($vobjProp->getValue(),5,2).'-'.substr($vobjProp->getValue(),8,2); $attributes['birthday'] = $vobjProp->getValue(); } break; case 'NOTE': $attributes['comment'] = $vobjProp->getValue(); break; case 'VERSION': case 'LAST-MODIFIED': break; default: $paramsArr = array(); foreach ($vobjProp->parameters as $param) { $paramsArr[] = $param->serialize(); } // $remainingVcardProps[] = array('name' => $vobjProp->name, 'parameters'=>implode(';',$paramsArr), 'value'=>$vobjProp->getValue()); break; } } foreach ($emails as $email) { if (!isset($attributes['email'])) { $attributes['email'] = $email; } elseif (!isset($attributes['email2'])) { $attributes['email2'] = $email; } elseif (!isset($attributes['email3'])) { $attributes['email3'] = $email; } } //some attributes can be specified with multiple values like tel and email. //We don't know which value is going to map to which exact GO attribute because every client handles this differently. //Clear the values if they haven't been found at all. // // Not clearing them cause some client might not send it and this can cause data loss. // $attributesMultiple=array('home_phone','work_phone','fax', 'work_fax','cellular','email','email2','email3'); // foreach($attributesMultiple as $attributeName){ // if(!isset($attributes[$attributeName])) // $attributes[$attributeName]=""; // } $attributes = array_map('trim', $attributes); $attributes = $this->_splitAddress($attributes); if (empty($attributes['last_name']) && empty($attributes['first_name'])) { $attributes['first_name'] = 'unnamed'; } $this->setAttributes($attributes, false); if (isset($companyAttributes['name'])) { $company = Company::model()->findSingleByAttributes(array('name' => $companyAttributes['name'], 'addressbook_id' => $this->addressbook_id)); if (!$company) { $company = new Company(); $company->setAttributes($companyAttributes, false); $company->addressbook_id = $this->addressbook_id; } if (!empty($saveToDb)) { $company->save(); } $this->setAttribute('company_id', $company->id); } $this->cutAttributeLengths(); if ($ignoreInvalidProperties) { $this->ignoreInvalidProperties(); } if (!empty($saveToDb)) { $this->save(); } if (!empty($photoFile) && $saveToDb) { $this->setPhoto($photoFile); $this->save(); } // foreach ($remainingVcardProps as $prop) { // if (!empty($this->id) && substr($prop['name'],0,2)=='X-') { // // Process encounters a custom property name in the VCard. // $arr = explode('-',$prop['name']); // $currentPropName = 'X-'.$arr[1]; // if (!in_array($currentPropName,$deletedPropertiesPrefixes_nonGO)) { // // Process encounters a new custom property prefix in the VCard. // // Now deleting all properties with this contact that have this prefix. // // Because of $deletedPropertiesPrefixes_nonGO, this is only done once // // per sync per VCard. // $deletablePropertiesStmt = ContactVcardProperty::model()->find( // \GO\Base\Db\FindParams::newInstance()->criteria( // \GO\Base\Db\FindCriteria::newInstance() // ->addCondition('contact_id',$this->id) // ->addCondition('name',$currentPropName.'-%','LIKE') // ) // ); // // while ($delPropModel = $deletablePropertiesStmt->fetch()) // $delPropModel->delete(); // // $deletedPropertiesPrefixes_nonGO[] = $currentPropName; // Keep track of prefixes for which we have deleted the properties. // } // } // // $propModel = ContactVcardProperty::model()->find( // \GO\Base\Db\FindParams::newInstance() // ->single() // ->criteria( // \GO\Base\Db\FindCriteria::newInstance() // ->addCondition('contact_id',$this->id) // ->addCondition('name',$prop['name']) // ->addCondition('parameters',$prop['parameters']) // ) // ); // if (empty($propModel)) // $propModel = new ContactVcardProperty(); // $propModel->contact_id = $this->id; // $propModel->name = $prop['name']; // $propModel->parameters = $prop['parameters']; // $propModel->value = $prop['value']; // $propModel->cutAttributeLengths(); // $propModel->save(); // } return $this; }