169 lines
5.8 KiB
PHP
169 lines
5.8 KiB
PHP
<?php
|
|
// Define the entry point check
|
|
define('_JEXEC', 1);
|
|
|
|
// Include Joomla framework
|
|
if (file_exists(__DIR__ . '/../defines.php')) {
|
|
include_once __DIR__ . '/../defines.php';
|
|
}
|
|
|
|
if (!defined('_JDEFINES')) {
|
|
define('JPATH_BASE', __DIR__ . '/..');
|
|
require_once JPATH_BASE . '/includes/defines.php';
|
|
}
|
|
|
|
require_once JPATH_BASE . '/includes/framework.php';
|
|
|
|
// Bootstrap the CMS libraries using the new bootstrap file
|
|
require_once JPATH_LIBRARIES . '/bootstrap.php';
|
|
|
|
use Joomla\CMS\Application\CliApplication;
|
|
use Joomla\CMS\Factory;
|
|
use Joomla\CMS\Application\CMSApplicationInterface;
|
|
use Joomla\CMS\Log\Log;
|
|
|
|
class UpdateContentCli extends CliApplication implements CMSApplicationInterface
|
|
{
|
|
protected $db;
|
|
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
$this->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";
|
|
}
|