Skip to content

Commit

Permalink
efforts to sort out deletion cascade
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickmj committed Feb 8, 2017
1 parent fe4ef63 commit 87c082a
Showing 1 changed file with 73 additions and 23 deletions.
96 changes: 73 additions & 23 deletions EditorialPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ class EditorialPlugin extends Omeka_Plugin_AbstractPlugin
'deactivate',
'after_save_exhibit_page_block',
'after_save_exhibit_page',
'before_save_exhibit_page',
'before_save_exhibit_page_block',
'after_delete_exhibit_page_block',
'before_delete_exhibit_page',
'admin_head',
'public_head',
'define_acl',
Expand Down Expand Up @@ -130,32 +131,13 @@ public function hookDefineAcl($args)
);
}

public function hookAfterDeleteExhibitPageBlock($args)
{
$block = $args['record'];
$options = $block->getOptions();
$responseTable = $this->_db->getTable('EditorialBlockResponse');
$responses = $responseTable->findResponsesForBlock($block);
foreach ($responses as $response) {
//sad voodoo for response somehow sometimes being null
if ($response) {
$response->delete();
}
}

$infoRecord = $this->_db->getTable('EditorialBlockInfo')->findByBlock($block);
if ($infoRecord) {
$infoRecord->delete();
}
}

/**
* Save responses to a block
*
* IDs are stored in the block's options
* @param unknown_type $args
*/

public function hookBeforeSaveExhibitPageBlock($args)
{
$block = $args['record'];
Expand All @@ -166,7 +148,6 @@ public function hookBeforeSaveExhibitPageBlock($args)

$responseTable = $this->_db->getTable('EditorialBlockResponse');
$options = $block->getOptions();
debug(print_r($options, true));
$responseIds = empty($options['response_ids']) ? array() : $options['response_ids'];
if (isset($options['responses'])) {
foreach ($options['responses'] as $responseData) {
Expand Down Expand Up @@ -211,12 +192,81 @@ public function hookBeforeSaveExhibitPageBlock($args)
$block->setOptions($options);
}


public function hookBeforeDeleteExhibitPage($args)
{
$page = $args['record'];
$blocks = $page->getPageBlocks();
$blockInfoTable = $this->_db->getTable('EditorialBlockInfo');
$accessesTable = $this->_db->getTable('EditorialExhibitAccess');
$responseTable = $this->_db->getTable('EditorialBlockResponse');
// responses are just stored in the options
foreach ($blocks as $block) {
$options = $block->getOptions();
$responses = $responseTable->findResponsesForBlock($block);
foreach ($responses as $response) {
//sad voodoo for response somehow sometimes being null
if ($response) {
$response->delete();
}
}

$info = $blockInfoTable->findByBlock($block);
$info->delete();

$accesses = $accessesTable->findBy(array('block_id', $block->id));
foreach ($accesses as $access) {
$access->delete();
}
}
}

public function hookBeforeSaveExhibitPage($args)
{
$page = $args['record'];
$post = $args['post'];
$postedBlocks = $post['blocks'];
$editorialBlockInfoTable = $this->_db->getTable('EditorialBlockInfo');
$editorialResponsesTable = $this->_db->getTable('EditorialBlockResponse');
$oldPostedIds = array();
foreach($postedBlocks as $postedBlock) {
if($postedBlock['layout'] == 'editorial-block') {
if(isset($postedBlock['options']['old_id'])) {
$oldPostedIds[] = $postedBlock['options']['old_id'];
}
}
}

$blocks = $page->getPageBlocks();
$editorialBlockInfoTable = $this->_db->getTable('EditorialBlockInfo');
foreach ($blocks as $block) {
$options = $block->getOptions();
if (isset($options['old_id'])) {
$oldId = $options['old_id'];
if (! in_array($oldId, $oldPostedIds)) {
// means block has been deleted
// look up editorial records based on the block, and delete them
$info = $editorialBlockInfoTable->findByBlock($block);
$info->delete();
$responseIds = $options['response_ids'];
$responsesSelect = $editorialResponsesTable->getSelect();
$responsesSelect->where('id IN (?)', $responseIds);
$responses = $editorialResponsesTable->fetchObjects($responsesSelect);
foreach($responses as $response) {
$response->delete();
}
}
}
}
}

public function hookAfterSaveExhibitPage($args)
{
$page = $args['record'];
$blocks = $page->getPageBlocks();
$editorialBlockInfoTable = $this->_db->getTable('EditorialBlockInfo');
$editorialAccessesTable = $this->_db->getTable('EditorialExhibitAccess');
// responses are just stored in the options
$oldBlockInfos = array();
$oldBlockAccesses = array();
$newBlockIdMap = array();
Expand Down Expand Up @@ -256,7 +306,7 @@ public function hookAfterSaveExhibitPage($args)
$this->adjustPermissions($block);
}
}

public function hookAfterSaveExhibitPageBlock($args)
{
$block = $args['record'];
Expand Down

0 comments on commit 87c082a

Please sign in to comment.