From c24d9f9e01b34ab8be1769d583bac416c7333d1c Mon Sep 17 00:00:00 2001 From: Jesper Kristensen Date: Tue, 20 May 2014 14:01:33 +0200 Subject: [PATCH] Issue #339 cableman: Added SMS and phone number to alma --- alma.features.field_base.inc | 82 +++++++++++++++++++++++++++++++ alma.features.field_instance.inc | 84 ++++++++++++++++++++++++++++---- alma.info | 7 ++- alma.module | 80 +++++++++++++++++++++++++++--- 4 files changed, 236 insertions(+), 17 deletions(-) diff --git a/alma.features.field_base.inc b/alma.features.field_base.inc index fd8dc6f..f8a527f 100644 --- a/alma.features.field_base.inc +++ b/alma.features.field_base.inc @@ -385,6 +385,88 @@ function alma_field_default_field_bases() { ), ); + // Exported field_base: 'field_alma_sms' + $field_bases['field_alma_sms'] = array( + 'active' => 1, + 'cardinality' => 1, + 'deleted' => 0, + 'entity_types' => array(), + 'field_name' => 'field_alma_sms', + 'foreign keys' => array( + 'format' => array( + 'columns' => array( + 'format' => 'format', + ), + 'table' => 'filter_format', + ), + ), + 'indexes' => array( + 'format' => array( + 0 => 'format', + ), + ), + 'locked' => 0, + 'module' => 'text', + 'settings' => array( + 'max_length' => 255, + 'profile2_private' => 0, + ), + 'storage' => array( + 'active' => 1, + 'module' => 'virtual_field', + 'settings' => array(), + 'type' => 'virtual_field', + ), + 'translatable' => 0, + 'type' => 'text', + 'virtual_field' => array( + 'entity_types' => array( + 0 => 'profile2', + ), + ), + ); + + // Exported field_base: 'field_alma_sms_id' + $field_bases['field_alma_sms_id'] = array( + 'active' => 1, + 'cardinality' => 1, + 'deleted' => 0, + 'entity_types' => array(), + 'field_name' => 'field_alma_sms_id', + 'foreign keys' => array( + 'format' => array( + 'columns' => array( + 'format' => 'format', + ), + 'table' => 'filter_format', + ), + ), + 'indexes' => array( + 'format' => array( + 0 => 'format', + ), + ), + 'locked' => 0, + 'module' => 'text', + 'settings' => array( + 'max_length' => 255, + 'profile2_private' => 0, + ), + 'storage' => array( + 'active' => 1, + 'module' => 'virtual_field', + 'settings' => array(), + 'type' => 'virtual_field', + ), + 'translatable' => 0, + 'type' => 'text', + 'virtual_field' => array( + 'entity_types' => array( + 0 => 'profile2', + ), + ), + ); + // Exported field_base: 'field_alma_street_name' $field_bases['field_alma_street_name'] = array( 'active' => 1, diff --git a/alma.features.field_instance.inc b/alma.features.field_instance.inc index 149a19f..6d03c53 100644 --- a/alma.features.field_instance.inc +++ b/alma.features.field_instance.inc @@ -21,7 +21,7 @@ function alma_field_default_field_instances() { 'label' => 'hidden', 'settings' => array(), 'type' => 'hidden', - 'weight' => 9, + 'weight' => 10, ), ), 'entity_type' => 'profile2', @@ -36,7 +36,7 @@ function alma_field_default_field_instances() { 'module' => 'virtual_field', 'settings' => array(), 'type' => 'hidden', - 'weight' => 9, + 'weight' => 10, ), ); @@ -118,7 +118,7 @@ function alma_field_default_field_instances() { 'module' => 'list', 'settings' => array(), 'type' => 'list_default', - 'weight' => 8, + 'weight' => 9, ), ), 'entity_type' => 'profile2', @@ -182,7 +182,7 @@ function alma_field_default_field_instances() { 'module' => 'text', 'settings' => array(), 'type' => 'text_default', - 'weight' => 5, + 'weight' => 6, ), ), 'entity_type' => 'profile2', @@ -200,7 +200,7 @@ function alma_field_default_field_instances() { 'size' => 12, ), 'type' => 'text_textfield', - 'weight' => 8, + 'weight' => 9, ), ); @@ -215,7 +215,7 @@ function alma_field_default_field_instances() { 'label' => 'hidden', 'settings' => array(), 'type' => 'hidden', - 'weight' => 10, + 'weight' => 11, ), ), 'entity_type' => 'profile2', @@ -230,7 +230,7 @@ function alma_field_default_field_instances() { 'module' => 'virtual_field', 'settings' => array(), 'type' => 'hidden', - 'weight' => 10, + 'weight' => 12, ), ); @@ -277,7 +277,7 @@ function alma_field_default_field_instances() { 'module' => 'list', 'settings' => array(), 'type' => 'list_default', - 'weight' => 6, + 'weight' => 7, ), ), 'entity_type' => 'profile2', @@ -313,7 +313,7 @@ function alma_field_default_field_instances() { 'multiple_to' => '', ), 'type' => 'date_default', - 'weight' => 7, + 'weight' => 8, ), ), 'entity_type' => 'profile2', @@ -345,6 +345,70 @@ function alma_field_default_field_instances() { ), ); + // Exported field_instance: 'profile2-provider_alma-field_alma_sms' + $field_instances['profile2-provider_alma-field_alma_sms'] = array( + 'bundle' => 'provider_alma', + 'default_value' => NULL, + 'deleted' => 0, + 'description' => '', + 'display' => array( + 'default' => array( + 'label' => 'inline', + 'module' => 'text', + 'settings' => array(), + 'type' => 'text_default', + 'weight' => 5, + ), + ), + 'entity_type' => 'profile2', + 'field_name' => 'field_alma_sms', + 'label' => 'SMS', + 'required' => 0, + 'settings' => array( + 'text_processing' => 0, + 'user_register_form' => FALSE, + ), + 'widget' => array( + 'active' => 1, + 'module' => 'text', + 'settings' => array( + 'size' => 60, + ), + 'type' => 'text_textfield', + 'weight' => 8, + ), + ); + + // Exported field_instance: 'profile2-provider_alma-field_alma_sms_id' + $field_instances['profile2-provider_alma-field_alma_sms_id'] = array( + 'bundle' => 'provider_alma', + 'default_value' => NULL, + 'deleted' => 0, + 'description' => '', + 'display' => array( + 'default' => array( + 'label' => 'hidden', + 'settings' => array(), + 'type' => 'hidden', + 'weight' => 12, + ), + ), + 'entity_type' => 'profile2', + 'field_name' => 'field_alma_sms_id', + 'label' => 'SMS id', + 'required' => 0, + 'settings' => array( + 'text_processing' => 0, + 'user_register_form' => FALSE, + ), + 'widget' => array( + 'module' => 'virtual_field', + 'settings' => array(), + 'type' => 'hidden', + 'weight' => 11, + ), + ); + // Exported field_instance: 'profile2-provider_alma-field_alma_street_name' $field_instances['profile2-provider_alma-field_alma_street_name'] = array( 'bundle' => 'provider_alma', @@ -388,6 +452,8 @@ function alma_field_default_field_instances() { t('Postal code'); t('Preferred branch'); t('Reservation pause'); + t('SMS'); + t('SMS id'); t('Set your reservations on pause.'); t('Street name'); t('The e-mail address is not made public and will only be used if you wish to receive certain news or notifications by e-mail.'); diff --git a/alma.info b/alma.info index 4c69ca5..cc822cd 100644 --- a/alma.info +++ b/alma.info @@ -2,7 +2,6 @@ name = Alma description = Implementation of Axiell’s Alma API for DDELibra. core = 7.x package = Providers -php = 5.2.4 project = alma dependencies[] = date dependencies[] = ding_provider @@ -16,7 +15,7 @@ dependencies[] = options dependencies[] = profile2 dependencies[] = text dependencies[] = virtual_field -features[features_api][] = api:1 +features[features_api][] = api:2 features[field_base][] = field_alma_absent_id features[field_base][] = field_alma_city features[field_base][] = field_alma_full_name @@ -27,6 +26,8 @@ features[field_base][] = field_alma_phone_id features[field_base][] = field_alma_postal_code features[field_base][] = field_alma_preferred_branch features[field_base][] = field_alma_reservation_pause +features[field_base][] = field_alma_sms +features[field_base][] = field_alma_sms_id features[field_base][] = field_alma_street_name features[field_instance][] = profile2-provider_alma-field_alma_absent_id features[field_instance][] = profile2-provider_alma-field_alma_city @@ -38,6 +39,8 @@ features[field_instance][] = profile2-provider_alma-field_alma_phone_id features[field_instance][] = profile2-provider_alma-field_alma_postal_code features[field_instance][] = profile2-provider_alma-field_alma_preferred_branch features[field_instance][] = profile2-provider_alma-field_alma_reservation_pause +features[field_instance][] = profile2-provider_alma-field_alma_sms +features[field_instance][] = profile2-provider_alma-field_alma_sms_id features[field_instance][] = profile2-provider_alma-field_alma_street_name features[profile2_type][] = provider_alma files[] = alma.module diff --git a/alma.module b/alma.module index b0a791d..e81d2a6 100644 --- a/alma.module +++ b/alma.module @@ -312,6 +312,22 @@ function alma_profile2_presave($entity) { } } + // SMS number. + $value = $wrapper->field_alma_sms->value(); + if (!is_null($wrapper_original->field_alma_sms->value())) { + // Update sms phone number. + $value = is_null($value) ? 'DELETE' : $value; + if ($value != $wrapper_original->field_alma_sms->value()) { + $changes['sms'] = $value; + + // Get sms id. + $changes['sms_id'] = $wrapper->field_alma_sms_id->value(); + } + } + elseif (!is_null($value) && is_null($wrapper_original->field_alma_sms->value())) { + $changes['sms'] = $value; + } + // Mobile phone. $value = $wrapper->field_alma_mobile_phone->value(); if (!is_null($wrapper_original->field_alma_mobile_phone->value())) { @@ -400,7 +416,7 @@ function alma_update_provider($changes, $entity) { else { // Update mobile. try { - $res = alma_client_invoke('change_phone_number', $creds['name'], $creds['pass'], $changes['phone_id'], $changes['mobile']); + $res = alma_client_invoke('change_phone_number', $creds['name'], $creds['pass'], $changes['phone_id'], $changes['mobile'], FALSE); } catch (Exception $exception) { watchdog_exception('Alma provider', $exception); @@ -415,7 +431,7 @@ function alma_update_provider($changes, $entity) { elseif (isset($changes['mobile'])) { // Add mobile. try { - $res = alma_client_invoke('add_phone_number', $creds['name'], $creds['pass'], $changes['mobile']); + $res = alma_client_invoke('add_phone_number', $creds['name'], $creds['pass'], $changes['mobile'], FALSE); } catch (Exception $exception) { watchdog_exception('Alma provider', $exception); @@ -426,6 +442,51 @@ function alma_update_provider($changes, $entity) { } } + // Mobile phone; add, change, delete. + if (isset($changes['sms_id'])) { + if ($changes['sms'] == 'DELETE') { + // Delete mobile. + try { + $res = alma_client_invoke('remove_phone_number', $creds['name'], $creds['pass'], $changes['sms_id']); + } + catch (Exception $exception) { + watchdog_exception('Alma provider', $exception); + } + + if (empty($res)) { + $error_message .= t('sms not deleted'); + drupal_set_message($error_message, 'warning'); + } + } + else { + // Update mobile. + try { + $res = alma_client_invoke('change_phone_number', $creds['name'], $creds['pass'], $changes['sms_id'], $changes['sms']); + } + catch (Exception $exception) { + watchdog_exception('Alma provider', $exception); + } + + if (!$res) { + $error_message .= t('sms not updated'); + drupal_set_message($error_message, 'warning'); + } + } + } + elseif (isset($changes['sms'])) { + // Add mobile. + try { + $res = alma_client_invoke('add_phone_number', $creds['name'], $creds['pass'], $changes['sms']); + } + catch (Exception $exception) { + watchdog_exception('Alma provider', $exception); + } + if (!$res) { + $error_message .= t('sms not added'); + drupal_set_message($error_message, 'warning'); + } + } + // Update reservation pause (absentPeriod). if (!empty($changes['absent_id']) && !empty($changes['reservation_pause_start']) && @@ -625,10 +686,17 @@ function alma_profile2_load($entities) { // Preferred_branch. $wrapper->field_alma_preferred_branch->set($patron->branch); - // Mobile; also here alma supports multiple phones - again we pick the - // first. - $wrapper->field_alma_mobile_phone->set(isset($patron->mobiles[0]['phone']) ? $patron->mobiles[0]['phone'] : ''); - $wrapper->field_alma_phone_id->set(isset($patron->mobiles[0]['id']) ? $patron->mobiles[0]['id'] : NULL); + // Mobile (SMS and telephone). + foreach ($patron->mobiles as $mobile) { + if ($mobile['sms']) { + $wrapper->field_alma_sms->set(isset($mobile['phone']) ? $mobile['phone'] : ''); + $wrapper->field_alma_sms_id->set(isset($mobile['id']) ? $mobile['id'] : NULL); + } + else { + $wrapper->field_alma_mobile_phone->set(isset($mobile['phone']) ? $mobile['phone'] : ''); + $wrapper->field_alma_phone_id->set(isset($mobile['id']) ? $mobile['id'] : NULL); + } + } // Mail address. $wrapper->field_alma_mail->set($patron->email);