cli & new db
This commit is contained in:
162
cli/dbqueryallegati.php
Normal file
162
cli/dbqueryallegati.php
Normal file
@ -0,0 +1,162 @@
|
|||||||
|
<?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());
|
||||||
|
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";
|
||||||
|
}
|
||||||
Binary file not shown.
BIN
db_latest.sql.zip
Normal file
BIN
db_latest.sql.zip
Normal file
Binary file not shown.
Reference in New Issue
Block a user