You are here

function opigno_moxtra_app_user_update in Opigno Moxtra App 7

Implements hook_user_update()

File

./opigno_moxtra_app.module, line 89

Code

function opigno_moxtra_app_user_update(&$edit, $account, $category) {
  $organization = variable_get('opigno_moxtra_app_org_id', NULL);
  if (!empty($organization) && opigno_moxtra_app_organization_is_active()) {
    if ($account->status == 1) {
      $response = opigno_moxtra_app_api_opigno_update_user(array(
        'uid' => $account->uid,
        'name' => $account->name,
        'timezone' => $account->timezone,
      ));
      if (isset($response->opigno_error_message)) {
        drupal_set_message($response->opigno_error_message, 'error');
        drupal_set_message(t('The user will be blocked. Try again by unblocking this user.'), 'warning');

        // Blocking the user like this will not pass by the module_invoke (module_invoke pass by here again and make an infinite loop...)
        db_update('users')
          ->condition('uid', $account->uid, '=')
          ->fields(array(
          'status' => 0,
        ))
          ->execute();
        return;
      }
    }
    if ($edit['original']->status != $account->status) {
      if ($account->status != 1) {
        $response = opigno_moxtra_app_api_opigno_delete_user($account->uid);
        if (isset($response->opigno_error_message)) {
          drupal_set_message($response->opigno_error_message, 'error');
          db_update('users')
            ->condition('uid', $account->uid, '=')
            ->fields(array(
            'status' => 1,
          ))
            ->execute();
        }
      }
      else {
        $user = array(
          'uid' => $account->uid,
          'name' => $account->name,
          'timezone' => $account->timezone,
        );
        $response = opigno_moxtra_app_api_opigno_create_users(array(
          $user,
        ));
        if (isset($response->opigno_error_message)) {
          drupal_set_message($response->opigno_error_message, 'error');
        }
        $response = opigno_moxtra_app_api_opigno_enable_user($user);
        if (isset($response->opigno_error_message)) {
          drupal_set_message($response->opigno_error_message, 'error');
          drupal_set_message(t('The user will be blocked. Try again by unblocking this user.'), 'warning');

          // Blocking the user like this will not pass by the module_invoke (module_invoke pass by here again and make an infinite loop...)
          db_update('users')
            ->condition('uid', $account->uid, '=')
            ->fields(array(
            'status' => 0,
          ))
            ->execute();
          return;
        }
      }
    }
  }
}