You are here

public function LdapServerAdmin::save in Lightweight Directory Access Protocol (LDAP) 8.2

Same name and namespace in other branches
  1. 7.2 ldap_servers/LdapServerAdmin.class.php \LdapServerAdmin::save()
  2. 7 ldap_servers/LdapServerAdmin.class.php \LdapServerAdmin::save()

Parameters

string enum $op 'add', 'update':

1 call to LdapServerAdmin::save()
LdapServerAdmin::drupalFormSubmit in ldap_servers/LdapServerAdmin.class.php

File

ldap_servers/LdapServerAdmin.class.php, line 104

Class

LdapServerAdmin

Code

public function save($op) {
  $values = new stdClass();
  foreach ($this
    ->field_to_properties_map() as $field_name => $property_name) {
    $field_name_lcase = drupal_strtolower($field_name);
    $values->{$field_name_lcase} = $this->{$property_name};
  }
  if (isset($this->bindpw) && $this->bindpw) {
    $values->bindpw = ldap_servers_encrypt($this->bindpw);
  }
  if ($this->bindpw_new) {
    $values->bindpw = ldap_servers_encrypt($this->bindpw_new);
  }
  elseif ($this->bindpw_clear) {
    $values->bindpw = NULL;
  }
  $values->tls = (int) $this->tls;
  if (module_exists('ctools')) {
    ctools_include('export');

    // Populate our object with ctool's properties
    $object = ctools_export_crud_new('ldap_servers');
    foreach ($object as $property => $value) {
      $property_lcase = drupal_strtolower($property);
      if (!isset($values->{$property}) || !isset($values->{$property_lcase})) {
        $values->{$property_lcase} = $value;
      }
    }
    try {
      $values->export_type = NULL;
      $result = ctools_export_crud_save('ldap_servers', $values);
    } catch (Exception $e) {
      $values->export_type = EXPORT_IN_DATABASE;
      $result = ctools_export_crud_save('ldap_servers', $values);
    }
    ctools_export_load_object_reset('ldap_servers');

    // ctools_export_crud_save doesn't invalidate cache
  }
  else {

    // directly via db
    unset($values->numeric_sid);
    if ($op == 'add') {
      $result = drupal_write_record('ldap_servers', $values);
    }
    else {
      $result = drupal_write_record('ldap_servers', $values, 'sid');
    }
    ldap_servers_cache_clear();
  }

  //  debug("values sent to save op=$op, ctools=". (int)module_exists('ctools')); debug($values);
  if ($result) {
    $this->inDatabase = TRUE;
  }
  else {
    drupal_set_message(t('Failed to write LDAP Server to the database.'));
  }
}