|
@@ -109,10 +109,20 @@ class TripalEntityController extends EntityAPIController {
|
|
|
* The title to use. It can contain tokens the correspond to field values.
|
|
|
* Token should be be compatible with those returned by
|
|
|
* tripal_get_entity_tokens().
|
|
|
+ * @param $cache
|
|
|
+ * This array is used to store objects you want to cache for performance reasons,
|
|
|
+ * as well as, cache related options. The following are supported:
|
|
|
+ * - TripalBundle $bundle
|
|
|
+ * The bundle for the current entity.
|
|
|
*/
|
|
|
- public function setTitle($entity, $title = NULL) {
|
|
|
+ public function setTitle($entity, $title = NULL, $cache = array()) {
|
|
|
|
|
|
- $bundle = tripal_load_bundle_entity(array('name' => $entity->bundle));
|
|
|
+ if (isset($cache['bundle'])) {
|
|
|
+ $bundle = $cache['bundle'];
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ $bundle = tripal_load_bundle_entity(array('name' => $entity->bundle));
|
|
|
+ }
|
|
|
|
|
|
// If no title was supplied then we should try to generate one using the
|
|
|
// default format set by admins.
|
|
@@ -140,18 +150,30 @@ class TripalEntityController extends EntityAPIController {
|
|
|
* The alias to use. It can contain tokens the correspond to field values.
|
|
|
* Token should be be compatible with those returned by
|
|
|
* tripal_get_entity_tokens().
|
|
|
+ * @param $cache
|
|
|
+ * This array is used to store objects you want to cache for performance reasons,
|
|
|
+ * as well as, cache related options. The following are supported:
|
|
|
+ * - TripalBundle $bundle
|
|
|
+ * The bundle for the current entity.
|
|
|
+ * - TripalTerm $term
|
|
|
+ * The term for the current entity.
|
|
|
*/
|
|
|
- public function setAlias($entity, $alias = NULL) {
|
|
|
+ public function setAlias($entity, $alias = NULL, $cache = array()) {
|
|
|
$source_url = "bio_data/$entity->id";
|
|
|
|
|
|
// If no alias was supplied then we should try to generate one using the
|
|
|
// default format set by admins.
|
|
|
if (!$alias) {
|
|
|
|
|
|
- // Load the TripalBundle entity for this TripalEntity.
|
|
|
+ // Load the TripalBundle entity for this TripalEntity (if it's not cached).
|
|
|
// First get the format for the url alias based on the bundle of the entity.
|
|
|
// Then replace all the tokens with values from the entity fields.
|
|
|
- $bundle_entity = tripal_load_bundle_entity(array('name' => $entity->bundle));
|
|
|
+ if (isset($cache['bundle'])) {
|
|
|
+ $bundle_entity = $cache['bundle'];
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ $bundle_entity = tripal_load_bundle_entity(array('name' => $entity->bundle));
|
|
|
+ }
|
|
|
$alias = tripal_get_bundle_variable('url_format', $bundle_entity->id);
|
|
|
$alias = tripal_replace_entity_tokens($alias, $entity, $bundle_entity);
|
|
|
}
|
|
@@ -163,7 +185,7 @@ class TripalEntityController extends EntityAPIController {
|
|
|
// Load the term for this TripalEntity. Set a default based on the term
|
|
|
// name and entity id. Then replace all the tokens with values from
|
|
|
// the entity fields.
|
|
|
- $term = entity_load('TripalTerm', array('id' => $entity->term_id));
|
|
|
+ $term = (isset($cache['term'])) ? $cache['term'] : entity_load('TripalTerm', array('id' => $entity->term_id));
|
|
|
$term = reset($term);
|
|
|
$alias = str_replace(' ', '', $term->name) . '/[TripalEntity__entity_id]';
|
|
|
$alias = tripal_replace_entity_tokens($alias, $entity, $bundle_entity);
|
|
@@ -173,7 +195,12 @@ class TripalEntityController extends EntityAPIController {
|
|
|
// this TripalEntity. Then replace all the tokens with values from the
|
|
|
// entity fields.
|
|
|
if($alias && (preg_match_all("/\[[^\]]*\]/", $alias, $bundle_tokens))) {
|
|
|
- $bundle_entity = tripal_load_bundle_entity(array('name' => $entity->bundle));
|
|
|
+ if (isset($cache['bundle'])) {
|
|
|
+ $bundle_entity = $cache['bundle'];
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ $bundle_entity = tripal_load_bundle_entity(array('name' => $entity->bundle));
|
|
|
+ }
|
|
|
$alias = tripal_replace_entity_tokens($alias, $entity, $bundle_entity);
|
|
|
}
|
|
|
|
|
@@ -256,7 +283,12 @@ class TripalEntityController extends EntityAPIController {
|
|
|
// already assigned this alias to this entity in a previous save.
|
|
|
elseif ($num_aliases == 1) {
|
|
|
|
|
|
- $bundle_entity = tripal_load_bundle_entity(array('name' => $entity->bundle));
|
|
|
+ if (isset($cache['bundle'])) {
|
|
|
+ $bundle_entity = $cache['bundle'];
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ $bundle_entity = tripal_load_bundle_entity(array('name' => $entity->bundle));
|
|
|
+ }
|
|
|
|
|
|
// Check to see if the single alias is for the same entity and if not
|
|
|
// warn the admin that the alias is already used (ie: not unique?)
|
|
@@ -284,7 +316,12 @@ class TripalEntityController extends EntityAPIController {
|
|
|
// If there are more then one alias' matching what we generated then there's
|
|
|
// a real problem and we need to warn the administrator.
|
|
|
else {
|
|
|
- $bundle_entity = tripal_load_bundle_entity(array('name' => $entity->bundle));
|
|
|
+ if (isset($cache['bundle'])) {
|
|
|
+ $bundle_entity = $cache['bundle'];
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ $bundle_entity = tripal_load_bundle_entity(array('name' => $entity->bundle));
|
|
|
+ }
|
|
|
|
|
|
$aliases = db_query('SELECT source FROM {url_alias} WHERE alias=:alias',
|
|
|
array(':alias' => $alias))->fetchAll();
|
|
@@ -317,29 +354,33 @@ class TripalEntityController extends EntityAPIController {
|
|
|
*
|
|
|
* @param $entity
|
|
|
* A TripalEntity object to save.
|
|
|
- * @param $transaction
|
|
|
- * If you have a transaction open, you can pass it through to avoid the overhead of
|
|
|
- * creating nested transactions.
|
|
|
- * @param $clear_cached_fields
|
|
|
- * Clearing cached fields is NECESSARY. IF you choose to set this to false then YOU
|
|
|
- * must clear the cache yourself using cache_clear_all('field:TripalEntity:[entity_id]', 'cache_field', TRUE).
|
|
|
- * The only known reason to set this to FALSE is to clear the cache in bulk for perfomance reasons.
|
|
|
+ * @param $cache
|
|
|
+ * This array is used to store objects you want to cache for performance reasons,
|
|
|
+ * as well as, cache related options. The following are supported:
|
|
|
+ * - DatabaseTransaction $transaction
|
|
|
+ * Allows you to pass in your current transaction to save nested transaction overhead.
|
|
|
+ * - boolean $clear_cached_fields
|
|
|
+ * Clearing cached fields is NECESSARY. IF you choose to set this to false then YOU
|
|
|
+ * must clear the cache yourself using cache_clear_all('field:TripalEntity:[entity_id]', 'cache_field', TRUE).
|
|
|
+ * The only known reason to set this to FALSE is to clear the cache in bulk for perfomance reasons.
|
|
|
+ * - TripalBundle $bundle
|
|
|
+ * The bundle for the current entity.
|
|
|
+ * - TripalTerm $term
|
|
|
+ * The term for the current entity.
|
|
|
*
|
|
|
* @return
|
|
|
* The saved entity object with updated properties.
|
|
|
*/
|
|
|
- public function save($entity, DatabaseTransaction $transaction = NULL, $clear_cached_fields = TRUE) {
|
|
|
+ public function save($entity, $cache = array()) {
|
|
|
global $user;
|
|
|
$pkeys = array();
|
|
|
|
|
|
- // @performance remove after development
|
|
|
- // @performance $started_at = microtime(true);
|
|
|
+ if (!isset($cache['clear_cached_fields'])) $cache['clear_cached_fields'] = TRUE;
|
|
|
|
|
|
$changed_date = time();
|
|
|
$create_date = $changed_date;
|
|
|
if (property_exists($entity, 'created')) {
|
|
|
if (!is_numeric($entity->created)) {
|
|
|
- print "IM CREATING A DATE!\n";
|
|
|
$temp = new DateTime($entity->created);
|
|
|
$create_date = $temp->getTimestamp();
|
|
|
}
|
|
@@ -352,7 +393,7 @@ class TripalEntityController extends EntityAPIController {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- $transaction = isset($transaction) ? $transaction : db_transaction();
|
|
|
+ $transaction = isset($cache['transaction']) ? $cache['transaction'] : db_transaction();
|
|
|
try {
|
|
|
// If our entity has no id, then we need to give it a
|
|
|
// time of creation.
|
|
@@ -371,8 +412,6 @@ class TripalEntityController extends EntityAPIController {
|
|
|
// Invoke hook_entity_presave().
|
|
|
module_invoke_all('entity_presave', $entity, $entity->type);
|
|
|
|
|
|
- // @performance print ' - After entity_presave :' . number_format(microtime(true) - $started_at, 4) . "s.\n";
|
|
|
-
|
|
|
// Write out the entity record.
|
|
|
$record = array(
|
|
|
'term_id' => $entity->term_id,
|
|
@@ -395,8 +434,6 @@ class TripalEntityController extends EntityAPIController {
|
|
|
$entity->id = $record['id'];
|
|
|
}
|
|
|
|
|
|
- // @performance print ' - After drupal_write_record :' . number_format(microtime(true) - $started_at, 4) . "s.\n";
|
|
|
-
|
|
|
// Now we need to either insert or update the fields which are
|
|
|
// attached to this entity. We use the same primary_keys logic
|
|
|
// to determine whether to update or insert, and which hook we
|
|
@@ -404,7 +441,6 @@ class TripalEntityController extends EntityAPIController {
|
|
|
// This is because a field may have default values and if so, those fields
|
|
|
// will be attached and the storage backend may then try to insert
|
|
|
// fields which should not be inserted because they already exist.
|
|
|
- // @performance print " - Invocation: $invocation.\n";
|
|
|
if ($invocation == 'entity_insert') {
|
|
|
field_attach_insert('TripalEntity', $entity);
|
|
|
}
|
|
@@ -413,27 +449,18 @@ class TripalEntityController extends EntityAPIController {
|
|
|
}
|
|
|
|
|
|
// Set the title for this entity.
|
|
|
- $this->setTitle($entity);
|
|
|
-
|
|
|
- // @performance print ' - After setTitle :' . number_format(microtime(true) - $started_at, 4) . "s.\n";
|
|
|
+ $this->setTitle($entity, NULL, $cache);
|
|
|
|
|
|
// Set the path/url alias for this entity.
|
|
|
- $this->setAlias($entity);
|
|
|
-
|
|
|
- // @performance print ' - After setAlias :' . number_format(microtime(true) - $started_at, 4) . "s.\n";
|
|
|
+ $this->setAlias($entity, NULL, $cache);
|
|
|
|
|
|
// Invoke either hook_entity_update() or hook_entity_insert().
|
|
|
module_invoke_all('entity_postsave', $entity, $entity->type);
|
|
|
-
|
|
|
- // @performance print ' - After entity_postsave :' . number_format(microtime(true) - $started_at, 4) . "s.\n";
|
|
|
-
|
|
|
module_invoke_all($invocation, $entity, $entity->type);
|
|
|
|
|
|
- // @performance print " - After $invocation :" . number_format(microtime(true) - $started_at, 4) . "s.\n";
|
|
|
-
|
|
|
// Clear any cache entries for this entity so it can be reloaded using
|
|
|
// the values that were just saved.
|
|
|
- if ($clear_cached_fields) {
|
|
|
+ if ($cache['clear_cached_fields']) {
|
|
|
$cid = 'field:TripalEntity:' . $entity->id;
|
|
|
cache_clear_all($cid, 'cache_field', TRUE);
|
|
|
}
|
|
@@ -446,8 +473,6 @@ class TripalEntityController extends EntityAPIController {
|
|
|
drupal_set_message("Could not save the entity: " . $e->getMessage(), "error");
|
|
|
return FALSE;
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
}
|
|
|
|
|
|
/**
|