Skip to content

Instantly share code, notes, and snippets.

@arturo-c
Created February 20, 2014 17:45
Show Gist options
  • Save arturo-c/9119329 to your computer and use it in GitHub Desktop.
Save arturo-c/9119329 to your computer and use it in GitHub Desktop.
diff --git a/www/sites/all/modules/apci_features/apci_public_api_services/AllPlayers/Resources/UserResource.php b/www/sites/all/modules/apci_features/apci_public_api_services/AllPlayers/Resources/UserResource.php
index 394832a..73976ec 100644
--- a/www/sites/all/modules/apci_features/apci_public_api_services/AllPlayers/Resources/UserResource.php
+++ b/www/sites/all/modules/apci_features/apci_public_api_services/AllPlayers/Resources/UserResource.php
@@ -86,7 +86,18 @@ class UserResource {
if (!empty($address)) {
$webform = webform_profile_load_profile_webform();
$profile = webform_profile_load_profile($created_user->uid);
- UserResource::setAddress($profile, $address);
+ $keys_not_found = '';
+ foreach ($address as $key => $value) {
+ if ($value) {
+ $exists = apci_webform_set_profile_value($profile, 'profile__field_' . $key . '__profile', $value);
+ if (!$exists) {
+ $keys_not_found .= $key . ', ';
+ }
+ }
+ }
+ if ($keys_not_found != '') {
+ return services_error(t('The following keys were not found: !keys', array('keys' => $keys_not_found)));
+ }
module_load_include('inc', 'webform', 'includes/webform.submissions');
webform_submission_update($webform, $profile);
@@ -120,8 +131,9 @@ class UserResource {
*
* @Access(callback='UserResource::access', args={'update'}, appendArgs=TRUE)
*/
- public static function update($uuid, $last_modified, $firstname = NULL, $lastname = NULL, $email = NULL, $gender = NULL, $birthday = NULL, $password = NULL, $external_id = NULL, $address = NULL) {
+ public static function update($uuid, $last_modified, $update_fields = array(), $firstname = NULL, $lastname = NULL, $email = NULL, $gender = NULL, $birthday = NULL, $password = NULL, $external_id = NULL, $address = NULL) {
$account = user_get_by_uuid($uuid);
+ $keys_not_found = '';
if ($account->changed > $last_modified) {
return services_error(t('The user has been modified by an earlier request'), 409);
}
@@ -133,23 +145,20 @@ class UserResource {
if ($profile->submitted > $last_modified) {
return services_error(t('The user has been modified by an earlier request'), 409);
}
- if ($firstname) {
- apci_webform_set_profile_value($profile, 'profile__field_firstname__profile', $firstname);
- }
- if ($lastname) {
- apci_webform_set_profile_value($profile, 'profile__field_lastname__profile', $lastname);
+ $updates = array('firstname' => $firstname, 'lastname' => $lastname, 'user_gender' => $gender, 'api_external_id' => $external_id, 'birth_date' => $birthday);
+ foreach ($address as $key => $value) {
+ $updates[$key] = $value;
}
- if ($gender) {
- apci_webform_set_profile_value($profile, 'profile__field_user_gender__profile', $gender);
- }
- if ($external_id) {
- apci_webform_set_profile_value($profile, 'profile__field_api_external_id__profile', $external_id);
- }
- if ($birthday) {
- apci_webform_set_profile_value($profile, 'profile__field_birth_date__profile', $birthday);
+ foreach ($updates as $key => $value) {
+ if ($value) {
+ $exists = apci_webform_set_profile_value($profile, 'profile__field_' . $key . '__profile', $value);
+ if (!$exists) {
+ $keys_not_found .= $key . ', ';
+ }
+ }
}
- if ($address) {
- UserResource::setAddress($profile, $address);
+ if ($keys_not_found != '') {
+ return services_error(t('The following keys were not found: !keys', array('keys' => $keys_not_found)));
}
module_load_include('inc', 'webform', 'includes/webform.submissions');
@@ -1230,33 +1239,4 @@ class UserResource {
return UserResource::retrieve($uuid, '*');
}
}
-
- /**
- * Helper function to set the address on a user webform profile.
- *
- * @param stdClass $profile
- * The user profile object.
- * @param array $address
- * Array of address fields.
- */
- private static function setAddress(&$profile, $address) {
- if (isset($address['street'])) {
- apci_webform_set_profile_value($profile, 'profile__field_address_street__profile', $address['street']);
- }
- if (isset($address['additional'])) {
- apci_webform_set_profile_value($profile, 'profile__field_address_additional__profile', $address['additional']);
- }
- if (isset($address['city'])) {
- apci_webform_set_profile_value($profile, 'profile__field_address_city__profile', $address['city']);
- }
- if (isset($address['province'])) {
- apci_webform_set_profile_value($profile, 'profile__field_address_province__profile', $address['province']);
- }
- if (isset($address['postal_code'])) {
- apci_webform_set_profile_value($profile, 'profile__field_address_postal_code__profile', $address['postal_code']);
- }
- if (isset($address['country'])) {
- apci_webform_set_profile_value($profile, 'profile__field_address_country__profile', $address['country']);
- }
- }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment