private static function get_client($form, $entry, $settings, $name_fields) { $client = new FreshBooks_Client(); $email = strtolower($entry[$settings["meta"]["email"]]); $is_new = true; if ($settings["meta"]["update_client"]) { //is there an existing client with the same email? If so, use it, if not, create one $client->listing($all_clients, $result_info); foreach ($all_clients as $current_client) { if (strtolower($current_client->email) == $email) { $client = $current_client; $is_new = false; break; } } } $client->email = esc_html(self::get_entry_value($settings["meta"]["email"], $entry, $name_fields)); $client->firstName = esc_html(self::get_entry_value($settings["meta"]["first_name"], $entry, $name_fields)); $client->lastName = esc_html(self::get_entry_value($settings["meta"]["last_name"], $entry, $name_fields)); $client->organization = esc_html(self::get_entry_value($settings["meta"]["organization"], $entry, $name_fields)); $address_field = $settings["meta"]["address"]; if (!empty($address_field)) { $client->pStreet1 = esc_html($entry[$address_field . ".1"]); $client->pStreet2 = esc_html($entry[$address_field . ".2"]); $client->pCity = esc_html($entry[$address_field . ".3"]); $client->pState = esc_html($entry[$address_field . ".4"]); $client->pCode = esc_html($entry[$address_field . ".5"]); $client->pCountry = esc_html($entry[$address_field . ".6"]); } if (!empty($settings["meta"]["phone"])) { $client->workPhone = esc_html(self::get_entry_value($settings["meta"]["phone"], $entry, $name_fields)); } if (!empty($settings["meta"]["fax"])) { $client->fax = esc_html(self::get_entry_value($settings["meta"]["fax"], $entry, $name_fields)); } $client->notes = esc_html($settings["meta"]["notes"]); if ($is_new) { self::log_debug("Client not found; creating new client with email address {$email}."); $client->create(); $lastError = $client->lastError; if (empty($lastError)) { self::log_debug("New client created."); } else { self::log_error("The following error occurred when trying to create a new client: {$lastError}"); } } else { self::log_debug("Existing client found with email address {$email}, not creating new one."); $id = $client->clientId; $client->update(); $client->clientId = $id; } return $client; }
private function get_client($form, $entry, $settings, $name_fields) { $client = new FreshBooks_Client(); $email = strtolower($entry[$settings['meta']['email']]); $is_new = true; if ($settings['meta']['updateClient']) { $existing_clients = ''; $result_info = ''; // is there an existing client with the same email? If so, use it, if not, create one $email = $entry[$settings['meta']['email']]; $client->listing($existing_clients, $result_info, 1, 1, array('email' => $email, 'username' => '')); if (!empty($existing_clients)) { $client = $existing_clients[0]; $is_new = false; } } $client->email = esc_html($this->get_entry_value($settings['meta']['email'], $entry, $name_fields)); $client->firstName = esc_html($this->get_entry_value($settings['meta']['firstName'], $entry, $name_fields)); $client->lastName = esc_html($this->get_entry_value($settings['meta']['lastName'], $entry, $name_fields)); $client->organization = esc_html($this->get_entry_value($settings['meta']['organization'], $entry, $name_fields)); $address_field = $settings['meta']['address']; if (!empty($address_field)) { $client->pStreet1 = esc_html($entry[$address_field . '.1']); $client->pStreet2 = esc_html($entry[$address_field . '.2']); $client->pCity = esc_html($entry[$address_field . '.3']); $client->pState = esc_html($entry[$address_field . '.4']); $client->pCode = esc_html($entry[$address_field . '.5']); $client->pCountry = esc_html($entry[$address_field . '.6']); } if (!empty($settings['meta']['phone'])) { $client->workPhone = esc_html(self::get_entry_value($settings['meta']['phone'], $entry, $name_fields)); } if (!empty($settings['meta']['fax'])) { $client->fax = esc_html(self::get_entry_value($settings['meta']['fax'], $entry, $name_fields)); } $client->notes = esc_html(GFCommon::replace_variables($settings['meta']['notes'], $form, $entry, false, false, false, 'text')); if ($is_new) { $this->log_debug(__METHOD__ . "(): Client not found; creating new client with email address {$email}."); $client->create(); $lastError = $client->lastError; if (empty($lastError)) { $this->log_debug(__METHOD__ . '(): New client created.'); } else { $this->log_error(__METHOD__ . "(): The following error occurred when trying to create a new client: {$lastError}"); } } else { $this->log_debug(__METHOD__ . "(): Existing client found with email address {$email}, not creating new one."); $id = $client->clientId; $client->update(); $client->clientId = $id; } return $client; }