Add sync status and last sync fields to CAIKnowledge and CAdvowareAkten entities, update layouts and metadata. Implement hooks for global sync status management.
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
namespace Espo\Custom\Hooks\CAIKnowledge;
|
||||
|
||||
use Espo\ORM\Entity;
|
||||
use Espo\ORM\Repository\Option\SaveOptions;
|
||||
use Espo\Core\Hook\Hook\BeforeSave;
|
||||
|
||||
/**
|
||||
* Hook: Prüft Junction-Table und aktualisiert globalen syncStatus
|
||||
* basierend auf den syncstatus-Werten der verknüpften Dokumente
|
||||
*/
|
||||
class CheckGlobalSyncStatus implements BeforeSave
|
||||
{
|
||||
public function __construct(
|
||||
private \Espo\ORM\EntityManager $entityManager
|
||||
) {}
|
||||
|
||||
public function beforeSave(Entity $entity, SaveOptions $options): void
|
||||
{
|
||||
// Überspringe, wenn skipHooks gesetzt ist (verhindert Loops)
|
||||
if ($options->get('skipHooks')) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Nur wenn Entity bereits existiert (nicht bei Create)
|
||||
if ($entity->isNew()) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
// Hole alle verknüpften Dokumente mit ihren syncstatus-Werten aus der Junction-Tabelle
|
||||
$query = $this->entityManager->getQueryBuilder()
|
||||
->select(['syncstatus'])
|
||||
->from('CAIKnowledgeDokumente')
|
||||
->where([
|
||||
'cAIKnowledgeId' => $entity->getId(),
|
||||
'deleted' => false
|
||||
])
|
||||
->build();
|
||||
|
||||
$pdoStatement = $this->entityManager->getQueryExecutor()->execute($query);
|
||||
$rows = $pdoStatement->fetchAll(\PDO::FETCH_ASSOC);
|
||||
|
||||
// Wenn keine Dokumente verknüpft, setze auf "unclean"
|
||||
if (empty($rows)) {
|
||||
$entity->set('syncStatus', 'unclean');
|
||||
return;
|
||||
}
|
||||
|
||||
// Prüfe, ob irgendein Dokument "new" oder "unclean" ist
|
||||
$hasUnsynced = false;
|
||||
foreach ($rows as $row) {
|
||||
$status = $row['syncstatus'] ?? null;
|
||||
if ($status === 'new' || $status === 'unclean' || $status === null || $status === '') {
|
||||
$hasUnsynced = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Setze globalen Status
|
||||
if ($hasUnsynced) {
|
||||
$entity->set('syncStatus', 'unclean');
|
||||
} else {
|
||||
// Alle Dokumente sind "synced"
|
||||
$entity->set('syncStatus', 'synced');
|
||||
$entity->set('lastSync', date('Y-m-d H:i:s'));
|
||||
}
|
||||
|
||||
} catch (\Exception $e) {
|
||||
// Bei Fehler loggen und Status auf "unclean" setzen
|
||||
$GLOBALS['log']->error('CAIKnowledge CheckGlobalSyncStatus Hook Error: ' . $e->getMessage());
|
||||
$entity->set('syncStatus', 'unclean');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
namespace Espo\Custom\Hooks\CAIKnowledge;
|
||||
|
||||
use Espo\ORM\Entity;
|
||||
use Espo\Core\Hook\Hook\AfterRelate;
|
||||
|
||||
/**
|
||||
* Hook: Setzt Dokument-Sync-Status auf "new" beim Verknüpfen und
|
||||
* globalen syncStatus auf "unclean"
|
||||
*/
|
||||
class DokumenteSyncStatus implements AfterRelate
|
||||
{
|
||||
public function __construct(
|
||||
private \Espo\ORM\EntityManager $entityManager
|
||||
) {}
|
||||
|
||||
public function afterRelate(
|
||||
Entity $entity,
|
||||
string $relationName,
|
||||
Entity $foreignEntity,
|
||||
array $columnData,
|
||||
\Espo\ORM\Repository\Option\RelateOptions $options
|
||||
): void {
|
||||
// Nur für dokumentes-Beziehung
|
||||
if ($relationName !== 'dokumentes') {
|
||||
return;
|
||||
}
|
||||
|
||||
// Setze Sync-Status des Dokuments in der Junction-Tabelle auf "new"
|
||||
$repository = $this->entityManager->getRDBRepository('CAIKnowledge');
|
||||
|
||||
try {
|
||||
$repository->getRelation($entity, 'dokumentes')->updateColumns(
|
||||
$foreignEntity,
|
||||
['syncstatus' => 'new']
|
||||
);
|
||||
|
||||
// Setze globalen syncStatus auf "unclean"
|
||||
$entity->set('syncStatus', 'unclean');
|
||||
$this->entityManager->saveEntity($entity, ['silent' => true, 'skipHooks' => true]);
|
||||
|
||||
} catch (\Exception $e) {
|
||||
// Fehler loggen, aber nicht werfen (um Verknüpfung nicht zu blockieren)
|
||||
$GLOBALS['log']->error('CAIKnowledge DokumenteSyncStatus Hook Error: ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
namespace Espo\Custom\Hooks\CAdvowareAkten;
|
||||
|
||||
use Espo\ORM\Entity;
|
||||
use Espo\ORM\Repository\Option\SaveOptions;
|
||||
use Espo\Core\Hook\Hook\BeforeSave;
|
||||
|
||||
/**
|
||||
* Hook: Prüft Junction-Table und aktualisiert globalen syncStatus
|
||||
* basierend auf den syncstatus-Werten der verknüpften Dokumente
|
||||
*/
|
||||
class CheckGlobalSyncStatus implements BeforeSave
|
||||
{
|
||||
public function __construct(
|
||||
private \Espo\ORM\EntityManager $entityManager
|
||||
) {}
|
||||
|
||||
public function beforeSave(Entity $entity, SaveOptions $options): void
|
||||
{
|
||||
// Überspringe, wenn skipHooks gesetzt ist (verhindert Loops)
|
||||
if ($options->get('skipHooks')) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Nur wenn Entity bereits existiert (nicht bei Create)
|
||||
if ($entity->isNew()) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
// Hole alle verknüpften Dokumente mit ihren syncstatus-Werten aus der Junction-Tabelle
|
||||
$query = $this->entityManager->getQueryBuilder()
|
||||
->select(['syncstatus'])
|
||||
->from('CAdvowareAktenDokumente')
|
||||
->where([
|
||||
'cAdvowareAktenId' => $entity->getId(),
|
||||
'deleted' => false
|
||||
])
|
||||
->build();
|
||||
|
||||
$pdoStatement = $this->entityManager->getQueryExecutor()->execute($query);
|
||||
$rows = $pdoStatement->fetchAll(\PDO::FETCH_ASSOC);
|
||||
|
||||
// Wenn keine Dokumente verknüpft, setze auf "unclean"
|
||||
if (empty($rows)) {
|
||||
$entity->set('syncStatus', 'unclean');
|
||||
return;
|
||||
}
|
||||
|
||||
// Prüfe, ob irgendein Dokument "new" oder "unclean" ist
|
||||
$hasUnsynced = false;
|
||||
foreach ($rows as $row) {
|
||||
$status = $row['syncstatus'] ?? null;
|
||||
if ($status === 'new' || $status === 'unclean' || $status === null || $status === '') {
|
||||
$hasUnsynced = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Setze globalen Status
|
||||
if ($hasUnsynced) {
|
||||
$entity->set('syncStatus', 'unclean');
|
||||
} else {
|
||||
// Alle Dokumente sind "synced"
|
||||
$entity->set('syncStatus', 'synced');
|
||||
$entity->set('lastSync', date('Y-m-d H:i:s'));
|
||||
}
|
||||
|
||||
} catch (\Exception $e) {
|
||||
// Bei Fehler loggen und Status auf "unclean" setzen
|
||||
$GLOBALS['log']->error('CAdvowareAkten CheckGlobalSyncStatus Hook Error: ' . $e->getMessage());
|
||||
$entity->set('syncStatus', 'unclean');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
namespace Espo\Custom\Hooks\CAdvowareAkten;
|
||||
|
||||
use Espo\ORM\Entity;
|
||||
use Espo\Core\Hook\Hook\AfterRelate;
|
||||
|
||||
/**
|
||||
* Hook: Setzt Dokument-Sync-Status auf "new" beim Verknüpfen und
|
||||
* globalen syncStatus auf "unclean"
|
||||
*/
|
||||
class DokumenteSyncStatus implements AfterRelate
|
||||
{
|
||||
public function __construct(
|
||||
private \Espo\ORM\EntityManager $entityManager
|
||||
) {}
|
||||
|
||||
public function afterRelate(
|
||||
Entity $entity,
|
||||
string $relationName,
|
||||
Entity $foreignEntity,
|
||||
array $columnData,
|
||||
\Espo\ORM\Repository\Option\RelateOptions $options
|
||||
): void {
|
||||
// Nur für dokumentes-Beziehung
|
||||
if ($relationName !== 'dokumentes') {
|
||||
return;
|
||||
}
|
||||
|
||||
// Setze Sync-Status des Dokuments in der Junction-Tabelle auf "new"
|
||||
$repository = $this->entityManager->getRDBRepository('CAdvowareAkten');
|
||||
|
||||
try {
|
||||
$repository->getRelation($entity, 'dokumentes')->updateColumns(
|
||||
$foreignEntity,
|
||||
['syncstatus' => 'new']
|
||||
);
|
||||
|
||||
// Setze globalen syncStatus auf "unclean"
|
||||
$entity->set('syncStatus', 'unclean');
|
||||
$this->entityManager->saveEntity($entity, ['silent' => true, 'skipHooks' => true]);
|
||||
|
||||
} catch (\Exception $e) {
|
||||
// Fehler loggen, aber nicht werfen (um Verknüpfung nicht zu blockieren)
|
||||
$GLOBALS['log']->error('CAdvowareAkten DokumenteSyncStatus Hook Error: ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user