48 lines
1.3 KiB
PHP
48 lines
1.3 KiB
PHP
<?php
|
|
namespace Espo\Custom\Hooks\CPuls;
|
|
|
|
use Espo\ORM\Entity;
|
|
use Espo\ORM\Repository\Option\SaveOptions;
|
|
use Espo\Core\Hook\Hook\BeforeSave;
|
|
|
|
class UpdateTeamStats implements BeforeSave
|
|
{
|
|
public function __construct(
|
|
private \Espo\ORM\EntityManager $entityManager
|
|
) {}
|
|
|
|
public function beforeSave(Entity $entity, SaveOptions $options): void
|
|
{
|
|
// Zähle Dokumente
|
|
if ($entity->isNew() || $entity->isAttributeChanged('id')) {
|
|
$dokumenteCount = $this->entityManager
|
|
->getRDBRepository('CDokumente')
|
|
->where(['pulsId' => $entity->getId()])
|
|
->count();
|
|
|
|
$entity->set('anzahlDokumente', $dokumenteCount);
|
|
}
|
|
|
|
// Zähle Team-Zuordnungen
|
|
$zuordnungen = $this->entityManager
|
|
->getRDBRepository('CPulsTeamZuordnung')
|
|
->where(['pulsId' => $entity->getId()])
|
|
->find();
|
|
|
|
$aktiv = 0;
|
|
$abgeschlossen = 0;
|
|
|
|
foreach ($zuordnungen as $z) {
|
|
if ($z->get('aktiv')) {
|
|
$aktiv++;
|
|
if ($z->get('abgeschlossen')) {
|
|
$abgeschlossen++;
|
|
}
|
|
}
|
|
}
|
|
|
|
$entity->set('anzahlTeamsAktiv', $aktiv);
|
|
$entity->set('anzahlTeamsAbgeschlossen', $abgeschlossen);
|
|
}
|
|
}
|