diff --git a/cli/dbqueryallegati.php b/cli/dbqueryallegati.php new file mode 100644 index 00000000..a1280137 --- /dev/null +++ b/cli/dbqueryallegati.php @@ -0,0 +1,162 @@ +db = Factory::getDbo(); // Use Joomla CMS Factory for database connection + //Log::addLogger(['text_file' => 'cli_update.log.php'], Log::ALL, ['cli_update']); + } + + public function doExecute() + { + try { + $attachments = $this->getAttachments(); + $this->processAttachments($attachments); + } catch (Exception $e) { + $this->out('Error: ' . $e->getMessage()); + Log::add('Error: ' . $e->getMessage(), Log::ERROR, 'cli_update'); + } + } + + protected function getAttachments() + { + $query = $this->db->getQuery(true) + ->select(['parent_id', 'url', 'display_name']) + ->from($this->db->quoteName('zfgey70wb_attachments')); + //->where($this->db->quoteName('parent_id') . ' = 4361'); // 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; + break; + } + } + + if (!$isDuplicate) { + $existingValue[$nextIndex] = [ + 'title' => $attachment['display_name'], + 'description' => '', + 'value' => $attachment['url'] + ]; + $nextIndex++; + } + } + + if ($existingValue !== $originalValue) { + $this->updateOrInsertFieldValue($parentId, $existingValue); + $this->out("Updated field_value for parent_id: $parentId"); + } else { + $this->out("No changes for parent_id: $parentId"); + } + } + } + + protected function getFieldValue($parentId) + { + $query = $this->db->getQuery(true) + ->select(['item_id', 'field_id', 'value']) + ->from($this->db->quoteName('zfgey70wb_fields_values')) + ->where($this->db->quoteName('field_id') . ' = 769') + ->where($this->db->quoteName('item_id') . ' = ' . $this->db->quote($parentId)); + + $this->db->setQuery($query); + + return $this->db->loadAssoc(); + } + + protected function updateOrInsertFieldValue($parentId, $value) + { + $query = $this->db->getQuery(true); + + // Check if the record exists + $existingRecord = $this->getFieldValue($parentId); + + if ($existingRecord) { + // Update existing record using item_id and field_id + $query + ->update($this->db->quoteName('zfgey70wb_fields_values')) + ->set($this->db->quoteName('value') . ' = ' . $this->db->quote(json_encode($value))) + ->where($this->db->quoteName('item_id') . ' = ' . $this->db->quote($parentId)) + ->where($this->db->quoteName('field_id') . ' = 769'); + } else { + // Insert new record + $columns = ['field_id', 'item_id', 'value']; + $values = [769, $parentId, $this->db->quote(json_encode($value))]; + + $query + ->insert($this->db->quoteName('zfgey70wb_fields_values')) + ->columns($this->db->quoteName($columns)) + ->values(implode(',', $values)); + } + + $this->db->setQuery($query); + $this->db->execute(); + } + + public function getName() + { + return 'UpdateContentCli'; + } +} + +// Execute the application +try { + // Create an instance of the application + $app = new UpdateContentCli(); + $app->execute(); +} catch (Exception $e) { + echo 'Error: ' . $e->getMessage() . "\n"; + echo 'Trace: ' . $e->getTraceAsString() . "\n"; +} diff --git a/db-latest.sql.zip b/db-latest.sql.zip deleted file mode 100644 index 5e176c80..00000000 Binary files a/db-latest.sql.zip and /dev/null differ diff --git a/db_latest.sql.zip b/db_latest.sql.zip new file mode 100644 index 00000000..4a686ea1 Binary files /dev/null and b/db_latest.sql.zip differ