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()); $this->out('Error: ' . $e->getMessage(), Log::ERROR, 'cli_update'); } } protected function getAttachments() { $query = $this->db->getQuery(true) ->select(['parent_id', 'url', 'display_name', 'filename']) ->from($this->db->quoteName('zfgey70wb_attachments')); //->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 foreach ($attachmentGroup as $attachment) { $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 ($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' => $title, 'description' => '', 'value' => $url ]; } } if ($existingValue !== $originalValue) { $this->updateOrInsertFieldValue($parentId, $existingValue); Log::add("Updated field_value for parent_id: $parentId", Log::INFO, 'cli_update'); } else { Log::add("No changes for parent_id: $parentId", Log::INFO, 'cli_update'); } } } 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"; }