de.propeace.mailinglistsync/Civi/Mailinglistsync/Utils.php
2025-03-28 19:07:48 +01:00

60 lines
1.6 KiB
PHP

<?php
declare(strict_types=1);
namespace Civi\Mailinglistsync;
use Civi\Api4\LocationType;
use Civi\Mailinglistsync\Exceptions\MailinglistException;
use CRM_Mailinglistsync_ExtensionUtil as E;
/**
* Gets the location type IDs for mailing list email-addresses either from
* the database or from the cache.
*
* @return array
* @throws \Civi\Mailinglistsync\Exceptions\MailinglistException
*/
function getLocationTypes(): array {
// Try to get location types from cache
$locationTypes = \Civi::cache()->get(E::SHORT_NAME . '_location_types');
// Get location types from database if not in cache
if ($locationTypes === NULL) {
$locationTypeNames = [
GroupMailingList::getLocationTypeName(),
ADGroupMailingList::getLocationTypeName(),
EventMailingList::getLocationTypeName(),
];
try {
$result = LocationType::get()
->addSelect('id', 'name')
->addWhere('name', 'IN', $locationTypeNames)
->execute()
->getArrayCopy();
$locationTypes = [];
foreach ($result as $locationType) {
$locationTypes[$locationType['id']] = $locationType['name'];
}
}
catch (\Exception $e) {
throw new MailinglistException(
"Could not get location types: {$e->getMessage()}",
MailinglistException::ERROR_CODE_GET_LOCATION_TYPES_FAILED,
);
}
// Cache location types
\Civi::cache()->set(E::SHORT_NAME . '_location_types', $locationTypes);
}
return $locationTypes;
}
/**
* Get a user ID of the current session.
*
* @return int
*/
function getSessionUser(): int {
$session = \CRM_Core_Session::singleton();
return (int) $session->get('userID');
}