diff --git a/election-default.controller.inc b/election-default.controller.inc index e0c1da13197ff165aa90b32fc1cae33a92a7b2b5..217eaa99f020325ad49aad1cb0be4c64729b9fe9 100644 --- a/election-default.controller.inc +++ b/election-default.controller.inc @@ -21,7 +21,7 @@ class ElectionDefaultController extends EntityAPIControllerExportable { // Convert timestamp fields. foreach ($entity as $property => $value) { - if (substr($property, -5) == '_time' && !empty($value)){ + if (substr($property, -5) == '_time' && !empty($value)) { $entity->$property = strtotime($value); } } diff --git a/election-post.admin.inc b/election-post.admin.inc index f71c86395d486a00a41de471d7d55080daf1b803..cfe5d749db9b12c85a1265a91c378a9660409ff1 100644 --- a/election-post.admin.inc +++ b/election-post.admin.inc @@ -610,7 +610,12 @@ function election_post_page_add($election) { return MENU_NOT_FOUND; } - $post = entity_get_controller('election_post')->create($election); + $values = array(); + $values['election_id'] = $election->election_id; + $type = _election_type_load($election->type); + $values['type'] = $type['post key']; + + $post = entity_get_controller('election_post')->create($values); $post_name = 'post'; $type = _election_type_load($election->type); diff --git a/election-post.controller.inc b/election-post.controller.inc index 75983473d3ade1d51277c98f62c5933d3dfffe67..31fd511c2ef4a5fdbf207d0a8397f9e6683a6ee8 100644 --- a/election-post.controller.inc +++ b/election-post.controller.inc @@ -8,18 +8,14 @@ * @extends * DrupalDefaultEntityController */ -class ElectionPostController extends DrupalDefaultEntityController { +class ElectionPostController extends ElectionDefaultController { - public function create($election) { - - $type = _election_type_load($election->type); - $post_type = $type['post key']; - - return (object) array( + public function create(array $values = array()) { + $default_values = array( 'post_id' => '', - 'election_id' => $election->election_id, + 'election_id' => '', 'title' => '', - 'type' => $post_type, + 'type' => '', 'vstatus_inheritance' => ELECTION_POST_STATUS_INHERIT, 'nstatus_inheritance' => ELECTION_POST_STATUS_INHERIT, 'require_manifesto' => 1, @@ -30,103 +26,9 @@ class ElectionPostController extends DrupalDefaultEntityController { 'use_ron' => 1, 'electorates' => array(), ); - - } - - public function save($post) { - - $transaction = db_transaction(); - - try { - - global $user; - - // Determine whether the post is new. - $post->is_new = empty($post->post_id); - - // Set timestamp fields. - if (empty($post->created)) { - $post->created = REQUEST_TIME; - } - - $post->changed = REQUEST_TIME; - - field_attach_presave('election_post', $post); - - if ($post->is_new) { - drupal_write_record('election_post', $post); - $op = 'insert'; - } - else { - drupal_write_record('election_post', $post, 'post_id'); - $op = 'update'; - } - - $function = 'field_attach_' . $op; - $function('election_post', $post); - - module_invoke_all('entity_' . $op, $post, 'election_post'); - - db_delete('election_post_electorate') - ->condition('post_id', $post->post_id) - ->execute(); - foreach ($post->electorates as $electorate_id) { - db_insert('election_post_electorate')->fields(array( - 'post_id' => $post->post_id, - 'electorate_id' => $electorate_id, - ))->execute(); - } - _election_uninstall_code_electorates(); - - unset($post->is_new); - - // Skip the slave server for this user temporarily. - db_ignore_slave(); - - return $post; - - } - catch (Exception $e) { - - $transaction->rollback(); - watchdog_exception('election_post', $e, NULL, WATCHDOG_ERROR); - return FALSE; - - } - - } - - public function delete($ids) { - - if (!empty($ids)) { - $posts = $this->load($ids, array()); - $transaction = db_transaction(); - - try { - db_delete('election_post') - ->condition('post_id', $ids, 'IN') - ->execute(); - db_delete('election_post_electorate') - ->condition('post_id', $ids, 'IN') - ->execute(); - foreach ($posts as $post_id => $post) { - field_attach_delete('election_post', $post); - } - db_ignore_slave(); - } - catch (Exception $e) { - $transaction->rollback(); - watchdog_exception('election', $e, NULL, WATCHDOG_ERROR); - return FALSE; - } - - cache_clear_all(); - $this->resetCache(); - - } - - return TRUE; - + // Overwrite default values with supplied values. + $values = array_merge($default_values, $values); + return parent::create($values); } } diff --git a/election.module b/election.module index 51b4ae072ca2c2cc1a2f909ddb760a88f5946035..7ac9f450c2a10d23969d09df4f49e4d7f32466d7 100644 --- a/election.module +++ b/election.module @@ -104,6 +104,9 @@ function election_entity_info() { 'id' => 'candidate_id', 'bundle' => 'type', ), + 'bundle keys' => array( + 'bundle' => 'type', + ), 'static cache' => TRUE, 'fieldable' => TRUE, 'module' => 'election', @@ -870,29 +873,62 @@ function election_save($election) { } /** - * Implements hook_entity_insert(). + * Implements hook_entity_update(). */ -function election_election_insert($election) { - if (function_exists('pathauto_election_update_alias')) { - pathauto_election_update_alias($election, 'insert'); +function election_entity_update($entity, $type) { + switch ($type) { + case 'election': + if (function_exists('pathauto_election_update_alias')) { + pathauto_election_update_alias($entity, 'update'); + } + break; } } /** - * Implements hook_entity_update(). + * Implements hook_entity_delete(). */ -function election_election_update($election) { - if (function_exists('pathauto_election_update_alias')) { - pathauto_election_update_alias($election, 'update'); +function election_entity_delete($entity, $type) { + switch ($type) { + case 'election': + if (function_exists('pathauto_entity_path_delete_all')) { + pathauto_entity_path_delete_all('election', $entity); + } + break; } } /** - * Implements hook_entity_delete(). + * Implements hook_entity_insert(). */ -function election_election_delete($election) { - if (function_exists('pathauto_entity_path_delete_all')) { - pathauto_entity_path_delete_all('election', $election); +function election_entity_insert($entity, $type) { + switch ($type) { + case 'election': + if (function_exists('pathauto_election_update_alias')) { + pathauto_election_update_alias($entity, 'insert'); + } + break; + case 'election_post': + $transaction = db_transaction(); + try { + db_delete('election_post_electorate') + ->condition('post_id', $entity->post_id) + ->execute(); + foreach ($entity->electorates as $electorate_id) { + db_insert('election_post_electorate')->fields(array( + 'post_id' => $entity->post_id, + 'electorate_id' => $electorate_id, + ))->execute(); + } + _election_uninstall_code_electorates(); + } + catch (Exception $e) { + $transaction->rollback(); + watchdog_exception('election', $e, NULL, WATCHDOG_ERROR); + return FALSE; + } + return TRUE; + break; } }