update cli allegati & new db

This commit is contained in:
2025-01-07 17:43:04 +01:00
parent 1b81b34df9
commit 705a4bbc1b
2 changed files with 31 additions and 25 deletions

View File

@ -40,69 +40,75 @@ class UpdateContentCli extends CliApplication implements CMSApplicationInterface
$this->processAttachments($attachments);
} catch (Exception $e) {
$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()
{
$query = $this->db->getQuery(true)
->select(['parent_id', 'url', 'display_name'])
->select(['parent_id', 'url', 'display_name', 'filename'])
->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);
return $this->db->loadAssocList();
}
protected function processAttachments($attachments)
{
$groupedAttachments = [];
// Group attachments by parent_id
foreach ($attachments as $attachment) {
$parentId = $attachment['parent_id'];
$groupedAttachments[$parentId][] = $attachment;
}
foreach ($groupedAttachments as $parentId => $attachmentGroup) {
$fieldValue = $this->getFieldValue($parentId);
$existingValue = $fieldValue ? json_decode($fieldValue['value'], true) : [];
$originalValue = $existingValue; // Keep original for comparison later
$nextIndex = count($existingValue) + 1;
foreach ($attachmentGroup as $attachment) {
$isDuplicate = false;
foreach ($existingValue as $entry) {
if ($entry['title'] === $attachment['display_name'] && $entry['value'] === $attachment['url']) {
$isDuplicate = true;
$title = !empty($attachment['display_name']) ? $attachment['display_name'] : pathinfo($attachment['filename'], PATHINFO_FILENAME);
$url = $attachment['url'];
$foundKey = null;
// Check if the URL already exists in the value
foreach ($existingValue as $key => $entry) {
if ($entry['value'] === $url) {
$foundKey = $key;
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] = [
'title' => $attachment['display_name'],
'title' => $title,
'description' => '',
'value' => $attachment['url']
'value' => $url
];
$nextIndex++;
}
}
if ($existingValue !== $originalValue) {
$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 {
$this->out("No changes for parent_id: $parentId");
Log::add("No changes for parent_id: $parentId", Log::INFO, 'cli_update');
}
}
}
protected function getFieldValue($parentId)
{
$query = $this->db->getQuery(true)

Binary file not shown.