You are here

function drupagram_account_save in Drupagram 7

Same name and namespace in other branches
  1. 6 drupagram.inc \drupagram_account_save()

Saves a InstagramUser object to {drupagram_account}

3 calls to drupagram_account_save()
drupagram_account_form_submit in ./drupagram.pages.inc
Implements hook_FORM_ID_submit().
drupagram_account_list_form_submit in ./drupagram.pages.inc
@todo Please document this function.
drupagram_oauth_callback_form_submit in ./drupagram.pages.inc
Handle a Instagram OAuth return request and store the account creds in the DB. Redirects to user/%/edit/drupagram

File

./drupagram.inc, line 29
Instagram API functions

Code

function drupagram_account_save($drupagram_user, $save_auth = FALSE, $account = NULL) {
  $values = (array) $drupagram_user;
  if (empty($drupagram_user)) {
    return FALSE;
  }
  $values['drupagram_id'] = $values['id'];
  if ($save_auth) {
    $values += $drupagram_user
      ->get_auth();
    if (empty($account)) {
      global $user;
      $account = $user;
    }
    $values['uid'] = $account->uid;
  }
  $schema = drupal_get_schema('drupagram_account');
  foreach ($values as $k => $v) {

    // Remove values that we don't have defined in our schema
    if (!isset($schema['fields'][$k])) {
      unset($values[$k]);
    }
    elseif (isset($schema['fields'][$k]['not null']) && $schema['fields'][$k]['not null'] && empty($values[$k])) {
      if ($schema['fields'][$k]['type'] == 'int') {
        $values[$k] = isset($schema['fields'][$k]['default']) ? $schema['fields'][$k]['default'] : 0;
      }
      else {
        $values[$k] = isset($schema['fields'][$k]['default']) ? drupagram_emoji($schema['fields'][$k]['default']) : '';
      }
    }
  }
  db_merge('drupagram_account')
    ->key(array(
    'drupagram_id' => $values['drupagram_id'],
  ))
    ->fields($values)
    ->execute();
  if (module_exists('entitycache')) {
    cache_clear_all($values['uid'], 'cache_entity_user');
  }
}