diff --git a/cli/dbqueryallegati.php b/cli/dbqueryallegati.php index a1280137..e089ca69 100644 --- a/cli/dbqueryallegati.php +++ b/cli/dbqueryallegati.php @@ -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) diff --git a/db_latest.sql.zip b/db_latest.sql.zip index 9c498b11..58800b8c 100644 Binary files a/db_latest.sql.zip and b/db_latest.sql.zip differ