Beispiel #1
0
 /**
  * Factory method to create a Contact object from an array
  * @param array $props - Associative array of initial properties to set
  * @return Contact
  */
 public static function create(array $props)
 {
     $contact = new Contact();
     $contact->id = parent::getValue($props, "id");
     $contact->status = parent::getValue($props, "status");
     $contact->first_name = parent::getValue($props, "first_name");
     $contact->middle_name = parent::getValue($props, "middle_name");
     $contact->last_name = parent::getValue($props, "last_name");
     $contact->confirmed = parent::getValue($props, "confirmed");
     $contact->source = parent::getValue($props, "source");
     if (isset($props['email_addresses'])) {
         foreach ($props['email_addresses'] as $email_address) {
             $contact->email_addresses[] = EmailAddress::create($email_address);
         }
     }
     $contact->prefix_name = parent::getValue($props, "prefix_name");
     $contact->job_title = parent::getValue($props, "job_title");
     if (isset($props['addresses'])) {
         foreach ($props['addresses'] as $address) {
             $contact->addresses[] = Address::create($address);
         }
     }
     if (isset($props['notes'])) {
         foreach ($props['notes'] as $note) {
             $contact->notes[] = Note::create($note);
         }
     }
     $contact->company_name = parent::getValue($props, "company_name");
     $contact->home_phone = parent::getValue($props, "home_phone");
     $contact->work_phone = parent::getValue($props, "work_phone");
     $contact->cell_phone = parent::getValue($props, "cell_phone");
     $contact->fax = parent::getValue($props, "fax");
     if (isset($props['custom_fields'])) {
         foreach ($props['custom_fields'] as $custom_field) {
             $contact->custom_fields[] = CustomField::create($custom_field);
         }
     }
     if (isset($props['lists'])) {
         foreach ($props['lists'] as $contact_list) {
             $contact->lists[] = ContactList::create($contact_list);
         }
     }
     $contact->created_date = parent::getValue($props, "created_date");
     $contact->modified_date = parent::getValue($props, "modified_date");
     $contact->source_details = parent::getValue($props, "source_details");
     return $contact;
 }
 /**
  * Clean and parse an array of contact details to be in CTCT format
  *
  * The method filters out any data not allowed by Constant Contact, so you don't
  * need to worry about having just the right details.
  *
  * It converts `email`, `email_address` and `user_email` into the
  * proper `email_addresses` array so you don't have to.
  *
  * @param  array $contact_array Contact information.
  * @param  boolean $add [description]
  *
  * @return [type]                 [description]
  */
 private function prepare(array $contact_array, $add = false)
 {
     $defaults = array('id' => NULL, 'status' => NULL, 'name' => NULL, 'first_name' => NULL, 'middle_name' => NULL, 'last_name' => NULL, 'source' => NULL, 'email_addresses' => array(), 'email' => NULL, 'email_address' => NULL, 'user_email' => NULL, 'prefix_name' => NULL, 'job_title' => NULL, 'addresses' => array(), 'address' => NULL, 'address_line1' => NULL, 'address_line2' => NULL, 'address_line3' => NULL, 'address_city' => NULL, 'address_state_code' => NULL, 'address_state_name' => NULL, 'address_country_code' => NULL, 'address_postal_code' => NULL, 'address_sub_postal_code' => NULL, 'notes' => array(), 'company_name' => NULL, 'home_phone' => NULL, 'work_phone' => NULL, 'cell_phone' => NULL, 'fax' => NULL, 'custom_fields' => array(), 'lists' => array(), 'source_details' => CTCT_APIKEY);
     $Contact = wp_parse_args($contact_array, $defaults);
     # r($contact_array);
     # r($Contact);
     foreach ($Contact as $k => $v) {
         /** Only allow permitted data in the Contact array */
         if (!array_key_exists($k, $defaults)) {
             // @todo: Add logging notice.
             unset($Contact[$k]);
         }
         switch ($k) {
             case 'user_email':
                 if (!empty($v)) {
                     $Contact['email'] = $Contact['user_email'];
                 }
                 unset($Contact['user_email']);
                 break;
             case 'name':
                 $Contact = $this->prepareName($Contact);
                 unset($Contact['name']);
                 break;
             case 'CustomField1':
             case 'CustomField2':
             case 'CustomField3':
             case 'CustomField4':
             case 'CustomField5':
             case 'CustomField6':
             case 'CustomField7':
             case 'CustomField8':
             case 'CustomField9':
             case 'CustomField10':
             case 'CustomField11':
             case 'CustomField12':
             case 'CustomField13':
             case 'CustomField14':
             case 'CustomField15':
                 $Contact['custom_fields'][] = array('name' => $k, 'value' => $v);
                 unset($Contact[$k]);
                 break;
             case 'address_line1':
             case 'address_line2':
             case 'address_line3':
             case 'address_city':
             case 'address_state_code':
             case 'address_state_name':
             case 'address_country_code':
             case 'address_postal_code':
             case 'address_sub_postal_code':
                 $Contact['address'][str_replace('address_', '', $k)] = $v;
                 unset($Contact[$k]);
                 break;
         }
     }
     if (!empty($Contact['email_address']) && empty($Contact['email_addresses'])) {
         $Contact['email_addresses'] = array($Contact['email_address']);
     }
     unset($Contact['email_address']);
     if (!empty($Contact['email']) && empty($Contact['email_addresses'])) {
         $Contact['email_addresses'] = array($Contact['email']);
     }
     unset($Contact['email']);
     $Contact['email_addresses'] = (array) $Contact['email_addresses'];
     if (empty($Contact['email_addresses'])) {
         return new WP_Error('no_email_address', 'There was no email defined.', $Contact);
     }
     foreach ($Contact['email_addresses'] as &$email_address) {
         $email_address = EmailAddress::create(array('email_address' => $email_address));
     }
     if (!empty($Contact['address']) && empty($Contact['addresses'])) {
         $Contact['address'] = $this->prepareAddress($Contact['address']);
         $Contact['addresses'] = array($Contact['address']);
     }
     unset($Contact['address']);
     if (!empty($Contact['addresses'])) {
         $Contact['addresses'] = (array) $Contact['addresses'];
         foreach ($Contact['addresses'] as $key => &$address) {
             $check_address = $address;
             foreach ($check_address as $k => $v) {
                 if (empty($v)) {
                     unset($check_address[$k]);
                 }
             }
             if (empty($check_address)) {
                 unset($address, $Contact['addresses'][$key]);
                 continue;
             }
             $address['address_type'] = strtoupper($address['address_type']);
             if (!in_array($address['address_type'], array('PERSONAL', 'BUSINESS', 'UNKNOWN'))) {
                 $address['address_type'] = 'PERSONAL';
             }
             $address = Address::create($this->prepareAddress((array) $address));
         }
         if (empty($Contact['addresses'])) {
             unset($Contact['addresses']);
         }
     }
     if (isset($Contact['notes'])) {
         unset($Contact['notes']);
         /*// CTCT got rid of updating notes for now.
         		$Contact['notes'] = array( $Contact['notes'] );
         		foreach( $Contact['notes'] as &$note) {
         			$note = is_array($note) ? $note : array('note' => $note);
         			$note = Note::create($note);
         		}*/
     }
     if (isset($Contact['custom_fields'])) {
         $Contact['custom_fields'] = (array) $Contact['custom_fields'];
         foreach ($Contact['custom_fields'] as &$custom_field) {
             $custom_field = CustomField::create($custom_field);
         }
     }
     if (isset($Contact['lists'])) {
         $Contact['lists'] = (array) $Contact['lists'];
         foreach ($Contact['lists'] as &$contact_list) {
             if (is_numeric($contact_list)) {
                 $contact_list = array('id' => (string) $contact_list);
             }
             $contact_list = ContactList::create($contact_list);
         }
     }
     if ($add || !in_array($Contact['status'], array('ACTIVE', 'UNCONFIRMED', 'OPTOUT', 'REMOVED', 'NON_SUBSCRIBER', 'VISITOR'))) {
         unset($Contact['status']);
     }
     if ($add) {
         unset($Contact['id'], $Contact['confirmed']);
     }
     return $Contact;
 }