You are here

function eck__entity_type__form_submit in Entity Construction Kit (ECK) 7

Same name and namespace in other branches
  1. 7.3 eck.entity_type.inc \eck__entity_type__form_submit()
  2. 7.2 eck.entity_type.inc \eck__entity_type__form_submit()

Submit handler for adding and entity type.

Parameters

$form: Form array provided by the Form API

$state: array provided by the Form API

File

./eck.entity_type.inc, line 304
ENTITY TYPE

Code

function eck__entity_type__form_submit($form, &$state) {

  // This are required so I don't have to do any checks.
  $entity_type = $state['values']['entity_type'];
  $entity_type_name = $state['values']['entity_type_name'];
  $entity_type->name = $entity_type_name;
  $entity_type_label = $state['values']['entity_type_label'];
  $entity_type->label = $entity_type_label;
  $properties = array_filter($state['values']['properties']);
  if (array_key_exists('custom_properties', $state['values'])) {
    $custom_properties = array_filter($state['values']['custom_properties']);
  }
  else {
    $custom_properties = array();
  }
  $entity_type->new_properties = $properties;
  $entity_type->new_custom_properties = $custom_properties;
  if ($entity_type->is_new) {

    //if the entity_type is new, we need to set up the properties

    //in the entity object, this are used to create then entity_type

    //table which looks at the properties in the entity_type

    //to know what the table should look like
    $entity_type->properties = $properties;
  }

  // If the table does not exist, then this is a valid entity name, and we can save it.
  if (!db_table_exists("eck_{$entity_type_name}")) {

    // Let's add the type to the table.
    if (!empty($state['values']['bundle_name'])) {
      $bundle_name = $state['values']['bundle_name'];
      if (!empty($state['values']['bundle_label'])) {
        $bundle_label = $state['values']['bundle_label'];
      }
      else {
        $bundle_label = ucfirst($bundle_name);
      }
    }
    else {
      $bundle_name = $entity_type_name;
      $bundle_label = $entity_type_label;
    }
    db_insert('eck_bundle')
      ->fields(array(
      'entity_type' => $entity_type_name,
      'name' => $bundle_name,
      'label' => $bundle_label,
    ))
      ->execute();
    db_insert('eck_entity_type')
      ->fields(array(
      'name' => $entity_type_name,
      'label' => $entity_type_label,
      'properties' => serialize($properties),
      'custom_properties' => serialize($custom_properties),
    ))
      ->execute();
    db_create_table("eck_{$entity_type_name}", eck__entity_type__schema($entity_type));

    // Clear info caches in order to pick up newly created entities.
    drupal_get_schema(NULL, TRUE);
    entity_info_cache_clear();

    // Rebuild the menu to pick up entity related paths.
    menu_rebuild();
    drupal_set_message(t('Entity type %entity_type has been created.', array(
      '%entity_type' => $entity_type_label,
    )));
    drupal_goto("admin/structure/eck/{$entity_type->name}");
  }
  else {

    // @todo Move into validation handler.

    //drupal_set_message(t('Database table %name already exists', array('%name' => $name)), 'error');

    // Process properties.
    $removed_properties = array_diff_key($entity_type->properties, $properties);
    foreach ($removed_properties as $property => $value) {
      db_drop_field("eck_{$entity_type->name}", $property);
    }
    $new_properties = array_diff_key($properties, $entity_type->properties);
    foreach ($new_properties as $property => $value) {
      $schema = array();

      // Add property to database schema.
      eck_add_property_schema($schema, $entity_type, $property);
      foreach ($schema['fields'] as $field => $spec) {
        db_add_field("eck_{$entity_type->name}", $field, $spec);
      }
      foreach ($schema['indexes'] as $index => $fields) {
        db_add_index("eck_{$entity_type->name}", $index, $fields);
      }
    }
    db_merge('eck_entity_type')
      ->key(array(
      'name' => $entity_type->name,
    ))
      ->fields(array(
      'properties' => serialize($properties),
    ))
      ->execute();

    // Process custom properties.
    $removed_properties = array_diff_key($entity_type->custom_properties, $custom_properties);
    foreach ($removed_properties as $property => $value) {
      db_drop_field("eck_{$entity_type_name}", $property);
      unset($entity_type->custom_properties[$property]);
    }
    if (!empty($state['values']['add']['type']) && !empty($state['values']['add']['name'])) {

      // Add database column.
      $spec = eck_get_custom_property_schema($state['values']['add']);
      db_add_field("eck_{$entity_type_name}", $state['values']['add']['name'], $spec);
      $entity_type->custom_properties += array(
        $state['values']['add']['name'] => array(
          'type' => $state['values']['add']['type'],
          'label' => $state['values']['add']['label'],
        ),
      );
    }
    db_merge('eck_entity_type')
      ->key(array(
      'name' => $entity_type_name,
    ))
      ->fields(array(
      'custom_properties' => serialize($entity_type->custom_properties),
    ))
      ->execute();

    // Clear info caches in order to pick up newly created entities.
    drupal_get_schema(NULL, TRUE);
    entity_info_cache_clear();
  }
}