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\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');
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user