You are here

class EntityType in Entity Construction Kit (ECK) 7.3

Same name and namespace in other branches
  1. 7.2 eck.classes.inc \EntityType

Hierarchy

Expanded class hierarchy of EntityType

1 string reference to 'EntityType'
eck_permissions_object_id in modules/eck_permissions/eck_permissions.module

File

./eck.classes.inc, line 221
Classes for all the different objects used in ECK.

View source
class EntityType extends DBObject {

  // If an entity type is new, we can create its table from the current data of
  // the object, but if this is a loaded object, we need to actually keep
  // track of the changes happening so we can modify the already existing table
  // appropiately.
  private $changes;

  /**
   * Constructor.
   */
  public function __construct() {
    parent::__construct('eck_entity_type');
    $this->properties = array();
    $this->changes = array();
  }

  /**
   * Add a new property to the entity type.
   *
   * @param string $name
   *   The name.
   * @param string $label
   *   A label.
   * @param string $type
   *   The type of the property.
   * @param string $behavior
   *   A behavior to attach to the property.
   */
  public function addProperty($name, $label, $type, $behavior = NULL, $schema = NULL) {
    if (!$this->is_new) {
      $this
        ->recordFieldChange('add', $name);
    }
    $p = $p2 = $this->properties;

    // @todo check that type is an actual type.
    $p[$name] = array(
      'label' => $label,
      'type' => $type,
      'behavior' => $behavior,
      'schema' => $schema,
    );
    $this->properties = $p;

    // If the property did not exist already, let the behaviors execute some
    // code.
    if (!array_key_exists($name, $p2)) {
      eck_property_behavior_invoke_plugin($this, 'property_add', array(
        'name' => $name,
        'property' => $p[$name],
        'entity_type' => $this,
      ));
    }
  }

  /**
   * Remove a property.
   *
   * @param string $name
   *   The name of the property.
   */
  public function removeProperty($name) {
    $p = $this->properties;
    if (array_key_exists($name, $p)) {

      // Let the behaviors execute some code.
      eck_property_behavior_invoke_plugin($this, 'property_remove', array(
        'name' => $name,
        'property' => $p[$name],
        'entity_type' => $this,
      ));
      unset($p[$name]);
      $this->properties = $p;
      if (!$this->is_new) {
        $this
          ->recordFieldChange('remove', $name);
      }
    }
  }

  /**
   * Change the behavior of a property.
   *
   * @param string $name
   *   The name of the property.
   *
   * @param string $behavior
   *   the name of the behavior.
   */
  public function changeBehavior($name, $behavior) {
    $p = $this->properties;

    // @todo check that type is an actual type.
    if (array_key_exists($name, $p)) {
      $p[$name]['behavior'] = $behavior;

      // @todo look at this more closelly, does the behavior change really
      // affect the property cache?
      entity_property_info_cache_clear();
    }
    else {

      // @todo add exception if the property does not exist.
    }
    $this->properties = $p;
  }

  /**
   * Remove behavior.
   *
   * @param string $name
   *   The name of the behavior.
   */
  public function removeBehavior($name) {
    $this
      ->changeBehavior($name, NULL);
  }

  /**
   * Record field changes.
   */
  private function recordFieldChange($op, $name) {

    // If it is not new we need to keep track of stuff.
    if (!$this->is_new) {
      $p = $this->properties;
      $c = $this->changes;
      switch ($op) {
        case 'add':

          // If the property does not exist already add keep track.
          if (!array_key_exists($name, $p)) {
            $c[$op][] = $name;
          }
          break;
        case 'remove':

          // If there is an add in the changes take it out, otherwise add a
          // remove.
          if (array_key_exists('add', $c)) {
            $key = array_search($name, $c['add']);
            if ($key != FALSE) {
              unset($c['add'][$key]);
            }
          }
          else {
            $c[$op][] = $name;
          }
          break;
      }
      $this->changes = $c;
    }
  }

  /**
   * Save.
   */
  public function save() {
    if ($this->is_new) {
      module_load_include('inc', 'eck', 'eck.entity_type');
      $schema = eck__entity_type__schema($this);
      db_create_table("eck_{$this->name}", $schema);

      // Allow other modules to respond to the creation of entity types.
      module_invoke_all('eck_entity_type_insert', $this);
    }
    else {

      // Modify the already existing table in accordance with the
      // recorded changes.
      if (array_key_exists('add', $this->changes)) {
        foreach ($this->changes['add'] as $name) {

          // First lets get the record.
          $properties = $this->properties;
          $property = $properties[$name];

          // Now we check to see whether it is a default or a custom property
          // it is not custom so lets get the schema and add the field.
          $schema = eck_get_property_type_schema($property['type']);
          db_add_field("eck_{$this->name}", $name, $schema);
        }
      }
      if (array_key_exists('remove', $this->changes)) {
        foreach ($this->changes['remove'] as $name) {
          db_drop_field("eck_{$this->name}", $name);
        }
      }

      // Allow other modules to respond to the change of entity types.
      module_invoke_all('eck_entity_type_update', $this);
    }
    parent::save();
    global $_eck_entity_types_cache;
    $cache_enabled = isset($_eck_entity_types_cache) ? TRUE : FALSE;
    if ($cache_enabled) {
      $_eck_entity_types_cache
        ->reset();
    }
    variable_set('eck_clear_caches', TRUE);
  }

  /**
   * Delete.
   */
  public function delete() {

    // Delete all the bundles from this entity type.
    $bundles = Bundle::loadByEntityType($this);
    foreach ($bundles as $bundle) {
      $bundle
        ->delete();
    }
    parent::delete();
    db_drop_table('eck_' . $this->name);

    // Allow other modules to respond to the deletion of entity types.
    module_invoke_all('eck_entity_type_delete', $this);
    global $_eck_entity_types_cache;
    $cache_enabled = isset($_eck_entity_types_cache) ? TRUE : FALSE;
    if ($cache_enabled) {
      $_eck_entity_types_cache
        ->reset();
    }
    variable_set('eck_clear_caches', TRUE);
  }

  /**
   * Load by name.
   *
   * @return mixed
   *   The entity type or Null.
   */
  public static function loadByName($name) {
    $entity_types = EntityType::loadAll();
    if (array_key_exists($name, $entity_types)) {
      return $entity_types[$name];
    }
    return NULL;
  }

  /**
   * Load all.
   *
   * @return array
   *   An array of entity types keyed by entity type name.
   */
  public static function loadAll($reset = FALSE) {
    $entity_types = array();
    global $_eck_entity_types_cache;
    $cache_enabled = isset($_eck_entity_types_cache) ? TRUE : FALSE;
    if ($cache_enabled) {
      if ($reset) {
        $_eck_entity_types_cache
          ->reset();
      }
      $entity_types = $_eck_entity_types_cache
        ->get();
    }
    if (empty($entity_types)) {
      $entity_types = array();
      $results = db_select('eck_entity_type', 't')
        ->fields('t', array(
        'name',
      ))
        ->execute();
      foreach ($results as $result) {
        $name = $result->name;
        $entity_type = new EntityType();
        $entity_type
          ->load('name', $name);
        $entity_types[$name] = $entity_type;
      }
      if ($cache_enabled) {
        $_eck_entity_types_cache
          ->set($entity_types);
      }
    }
    return $entity_types;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DBObject::$data private property
DBObject::$is_new public property
DBObject::$position private property
DBObject::$primaryKeys private property
DBObject::$serialize private property
DBObject::$table private property
DBObject::$vars private property
DBObject::current public function From Iterator Interface.
DBObject::key public function From Iterator Interface.
DBObject::load protected function Load.
DBObject::next public function From Iterator Interface.
DBObject::rewind public function From Iterator Interface.
DBObject::valid public function From Iterator Interface.
DBObject::__get public function Magic method.
DBObject::__isset public function Magic method.
DBObject::__set public function Magic method.
DBObject::__unset public function Magic method.
EntityType::$changes private property
EntityType::addProperty public function Add a new property to the entity type.
EntityType::changeBehavior public function Change the behavior of a property.
EntityType::delete public function Delete. Overrides DBObject::delete
EntityType::loadAll public static function Load all.
EntityType::loadByName public static function Load by name.
EntityType::recordFieldChange private function Record field changes.
EntityType::removeBehavior public function Remove behavior.
EntityType::removeProperty public function Remove a property.
EntityType::save public function Save. Overrides DBObject::save
EntityType::__construct public function Constructor. Overrides DBObject::__construct