update cli allegati & new db
This commit is contained in:
@ -40,69 +40,75 @@ class UpdateContentCli extends CliApplication implements CMSApplicationInterface
|
|||||||
$this->processAttachments($attachments);
|
$this->processAttachments($attachments);
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
$this->out('Error: ' . $e->getMessage());
|
$this->out('Error: ' . $e->getMessage());
|
||||||
Log::add('Error: ' . $e->getMessage(), Log::ERROR, 'cli_update');
|
$this->out('Error: ' . $e->getMessage(), Log::ERROR, 'cli_update');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function getAttachments()
|
protected function getAttachments()
|
||||||
{
|
{
|
||||||
$query = $this->db->getQuery(true)
|
$query = $this->db->getQuery(true)
|
||||||
->select(['parent_id', 'url', 'display_name'])
|
->select(['parent_id', 'url', 'display_name', 'filename'])
|
||||||
->from($this->db->quoteName('zfgey70wb_attachments'));
|
->from($this->db->quoteName('zfgey70wb_attachments'));
|
||||||
//->where($this->db->quoteName('parent_id') . ' = 4361'); // Filter for parent_id 4361
|
//->where($this->db->quoteName('parent_id') . ' = 71') // Filter for parent_id 4361
|
||||||
|
|
||||||
$this->db->setQuery($query);
|
$this->db->setQuery($query);
|
||||||
|
|
||||||
return $this->db->loadAssocList();
|
return $this->db->loadAssocList();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function processAttachments($attachments)
|
protected function processAttachments($attachments)
|
||||||
{
|
{
|
||||||
$groupedAttachments = [];
|
$groupedAttachments = [];
|
||||||
|
|
||||||
// Group attachments by parent_id
|
// Group attachments by parent_id
|
||||||
foreach ($attachments as $attachment) {
|
foreach ($attachments as $attachment) {
|
||||||
$parentId = $attachment['parent_id'];
|
$parentId = $attachment['parent_id'];
|
||||||
$groupedAttachments[$parentId][] = $attachment;
|
$groupedAttachments[$parentId][] = $attachment;
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($groupedAttachments as $parentId => $attachmentGroup) {
|
foreach ($groupedAttachments as $parentId => $attachmentGroup) {
|
||||||
$fieldValue = $this->getFieldValue($parentId);
|
$fieldValue = $this->getFieldValue($parentId);
|
||||||
|
|
||||||
$existingValue = $fieldValue ? json_decode($fieldValue['value'], true) : [];
|
$existingValue = $fieldValue ? json_decode($fieldValue['value'], true) : [];
|
||||||
$originalValue = $existingValue; // Keep original for comparison later
|
$originalValue = $existingValue; // Keep original for comparison later
|
||||||
|
|
||||||
$nextIndex = count($existingValue) + 1;
|
|
||||||
|
|
||||||
foreach ($attachmentGroup as $attachment) {
|
foreach ($attachmentGroup as $attachment) {
|
||||||
$isDuplicate = false;
|
$title = !empty($attachment['display_name']) ? $attachment['display_name'] : pathinfo($attachment['filename'], PATHINFO_FILENAME);
|
||||||
|
$url = $attachment['url'];
|
||||||
foreach ($existingValue as $entry) {
|
$foundKey = null;
|
||||||
if ($entry['title'] === $attachment['display_name'] && $entry['value'] === $attachment['url']) {
|
|
||||||
$isDuplicate = true;
|
// Check if the URL already exists in the value
|
||||||
|
foreach ($existingValue as $key => $entry) {
|
||||||
|
if ($entry['value'] === $url) {
|
||||||
|
$foundKey = $key;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!$isDuplicate) {
|
if ($foundKey !== null) {
|
||||||
|
// If the URL exists, update the title if it's different
|
||||||
|
if ($existingValue[$foundKey]['title'] !== $title) {
|
||||||
|
$existingValue[$foundKey]['title'] = $title;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// If the URL does not exist, add a new entry
|
||||||
|
$nextIndex = count($existingValue) + 1;
|
||||||
$existingValue[$nextIndex] = [
|
$existingValue[$nextIndex] = [
|
||||||
'title' => $attachment['display_name'],
|
'title' => $title,
|
||||||
'description' => '',
|
'description' => '',
|
||||||
'value' => $attachment['url']
|
'value' => $url
|
||||||
];
|
];
|
||||||
$nextIndex++;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($existingValue !== $originalValue) {
|
if ($existingValue !== $originalValue) {
|
||||||
$this->updateOrInsertFieldValue($parentId, $existingValue);
|
$this->updateOrInsertFieldValue($parentId, $existingValue);
|
||||||
$this->out("Updated field_value for parent_id: $parentId");
|
Log::add("Updated field_value for parent_id: $parentId", Log::INFO, 'cli_update');
|
||||||
} else {
|
} else {
|
||||||
$this->out("No changes for parent_id: $parentId");
|
Log::add("No changes for parent_id: $parentId", Log::INFO, 'cli_update');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function getFieldValue($parentId)
|
protected function getFieldValue($parentId)
|
||||||
{
|
{
|
||||||
$query = $this->db->getQuery(true)
|
$query = $this->db->getQuery(true)
|
||||||
|
|||||||
Binary file not shown.
Reference in New Issue
Block a user