Skip to content

Instantly share code, notes, and snippets.

@litzinger
Created Aug 4, 2021
Embed
What would you like to do?
<?php
private function process_bloqs_fields(ChannelEntry $newEntry, int $originalEntryId)
{
$fields = $this->get_fields($newEntry, 'bloqs');
// Process each bloqs field in the entry
foreach ($fields as $fieldId) {
/** @var CI_DB_result $bloqsQuery */
$bloqsQuery = ee()->db->select('*')
->from('blocks_block')
->where([
'entry_id' => $originalEntryId,
'field_id' => $fieldId,
'publisher_lang_id' => $this->requestService->getCurrentLanguage()->getId(),
'publisher_status' => 'open',
])
->get();
$bloqIdChanges = [];
foreach ($bloqsQuery->result() as $bloqsRow) {
$oldBloqId = $bloqsRow->id;
unset($bloqsRow->id);
$bloqsRow->entry_id = $newEntry->entry_id;
ee()->db->insert('blocks_block', $bloqsRow);
$newBloqId = intval(ee()->db->insert_id());
// Keep track of the old ids and new ids, we'll need to update the parent_id column later.
$bloqIdChanges[$oldBloqId] = $newBloqId;
// Go through atoms for each bloq
/** @var CI_DB_result $atomsQuery */
$atomsQuery = ee()->db->select('*')
->from('blocks_atom')
->where('block_id', $oldBloqId)
->get();
foreach ($atomsQuery->result() as $atomsRow) {
unset($atomsRow->id);
$atomsRow->block_id = $newBloqId;
ee()->db->insert('blocks_atom', $atomsRow);
// Check to see if it's a relationship field and update relationships as needed
/** @var CI_DB_result $atomDefinitionQuery */
$atomDefinitionQuery = ee()->db->select('type')
->from('blocks_atomdefinition')
->where('id', $atomsRow->atomdefinition_id)
->get();
if ($atomDefinitionQuery->row()->type === 'relationship' && $this->isEnabled('relationships')) {
/** @var CI_DB_result $relationshipsQuery */
$relationshipsQuery = ee()->db
->from('relationships')
->where([
'parent_id' => $originalEntryId,
'grid_row_id' => $oldBloqId,
'grid_col_id' => $atomsRow->atomdefinition_id,
])
->get();
foreach ($relationshipsQuery->result() as $relationshipRow) {
$relationshipRow->parent_id = $newEntry->entry_id;
$relationshipRow->grid_row_id = $newBloqId;
unset($relationshipRow->relationship_id);
if ($this->requestService->isDefaultLanguage()) {
ee()->db->insert('relationships', $relationshipRow);
}
$relationshipRow->publisher_lang_id = $this->requestService->getCurrentLanguage()->getId();
$relationshipRow->publisher_status = 'open';
ee()->db->insert('publisher_relationships', $relationshipRow);
}
}
if ($atomDefinitionQuery->row()->type === 'assets' && $this->isEnabled('assets')) {
/** @var CI_DB_result $assetsQuery */
$assetsQuery = ee()->db
->from('assets_selections')
->where([
'entry_id' => $originalEntryId,
'row_id' => $oldBloqId,
])
->get();
foreach ($assetsQuery->result() as $assetRow) {
$assetRow->entry_id = $newEntry->entry_id;
$assetRow->row_id = $newBloqId;
unset($assetRow->selection_id);
$assetRow->publisher_lang_id = $this->requestService->getCurrentLanguage()->getId();
$assetRow->publisher_status = 'open';
ee()->db->insert('assets_selections', $assetRow);
}
}
}
}
foreach ($bloqIdChanges as $oldId => $newId) {
ee()->db
->where('parent_id', $oldId)
->where('entry_id', $newEntry->entry_id)
->update('blocks_block', [
'parent_id' => $newId
]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment