class EntityType in Entity Construction Kit (ECK) 7.2
Same name and namespace in other branches
- 7.3 eck.classes.inc \EntityType
An entity type database object.
Hierarchy
- class \DBObject implements \Iterator
- class \EntityType
Expanded class hierarchy of EntityType
File
- ./
eck.classes.inc, line 211 - 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();
}
/**
* @param string[] $names
* @return EntityType[]
*/
public static function loadMultipleByName(array $names) {
$entityTypes = array();
foreach ($names as $name) {
$entityType = self::loadByName($name);
if (!empty($entityType)) {
$entityTypes[$name] = $entityType;
}
}
return $entityTypes;
}
/**
* 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) {
if (!$this->isNew) {
$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,
);
$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->isNew) {
$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);
}
/**
* Keep track of changes happening to the entity type.
*
* This is useful to later replica the changes on the DB.
*/
private function recordFieldChange($op, $name) {
// If it is not new we need to keep track of stuff.
if (!$this->isNew) {
$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->isNew) {
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_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();
}
eck_clean_up_request();
}
/**
* 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();
}
eck_clean_up_request();
}
/**
* Load a specific entity type.
*
* @param string $name
* The name of the entity type.
*
* @return EntityType|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 entity types.
*
* @param bool $reset
* Use data from the cache, or not.
*
* @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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DBObject:: |
private | property | ||
DBObject:: |
public | property | ||
DBObject:: |
private | property | ||
DBObject:: |
private | property | ||
DBObject:: |
private | property | ||
DBObject:: |
private | property | ||
DBObject:: |
private | property | ||
DBObject:: |
public | function | From Iterator Interface. | |
DBObject:: |
public | function | From Iterator Interface. | |
DBObject:: |
protected | function | Load. | |
DBObject:: |
public | function | From Iterator Interface. | |
DBObject:: |
public | function | From Iterator Interface. | |
DBObject:: |
public | function | From Iterator Interface. | |
DBObject:: |
public | function | Magic method. | |
DBObject:: |
public | function | Magic method. | |
DBObject:: |
public | function | Magic method. | |
DBObject:: |
public | function | Magic method. | |
EntityType:: |
private | property | ||
EntityType:: |
public | function | Add a new property to the entity type. | |
EntityType:: |
public | function | Change the behavior of a property. | |
EntityType:: |
public | function |
Delete. Overrides DBObject:: |
|
EntityType:: |
public static | function | Load all entity types. | |
EntityType:: |
public static | function | Load a specific entity type. | |
EntityType:: |
public static | function | ||
EntityType:: |
private | function | Keep track of changes happening to the entity type. | |
EntityType:: |
public | function | Remove behavior. | |
EntityType:: |
public | function | Remove a property. | |
EntityType:: |
public | function |
Save. Overrides DBObject:: |
|
EntityType:: |
public | function |
Constructor. Overrides DBObject:: |