-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement object hook to get event object information from respective…
… sources
- Loading branch information
Showing
7 changed files
with
206 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
<?php | ||
|
||
/* Icinga Notifications Web | (c) 2024 Icinga GmbH | GPLv2 */ | ||
|
||
namespace Icinga\Module\Notifications\Hook; | ||
|
||
use Exception; | ||
use Icinga\Application\Hook; | ||
use Icinga\Application\Logger; | ||
use Icinga\Module\Notifications\Model\Objects; | ||
use Icinga\Module\Notifications\Model\Source; | ||
use ipl\Html\HtmlString; | ||
use ipl\Html\ValidHtml; | ||
use ipl\Orm\Query; | ||
|
||
abstract class EventsObjectsInfoHook | ||
{ | ||
/** @var array<string, ValidHtml> */ | ||
protected static $objectNameWidgets = []; | ||
|
||
/** | ||
* Get the object name widgets for the objects using the object ID tags | ||
* | ||
* @param array<string, array<string, string>> $objectIdTags | ||
* | ||
* @return array<string, ValidHtml> | ||
*/ | ||
abstract public function getObjectNameWidgets(array $objectIdTags): array; | ||
|
||
/** | ||
* Get the source of the objects | ||
* | ||
* @return string | ||
*/ | ||
abstract public function getSource(): string; | ||
|
||
/** | ||
* Get the object list item widget for the given object ID tag | ||
* | ||
* @param array<string, string> $objectIdTag | ||
* | ||
* @return ValidHtml | ||
*/ | ||
abstract public function getHtmlForObject(array $objectIdTag): ValidHtml; | ||
|
||
/** | ||
* Register object name widgets for the objects using the object ID tags | ||
* | ||
* @param array<string, array<string, array<string, string>>> $objectIdTags | ||
* | ||
* @return void | ||
*/ | ||
final public static function registerObjectNameWidgets(array $objectIdTags): void | ||
{ | ||
$objectDisplayNames = []; | ||
foreach (Hook::all('Notifications\\EventsObjectsInfo') as $hook) { | ||
/** @var self $hook */ | ||
try { | ||
$objectDisplayNames = array_merge( | ||
$objectDisplayNames, | ||
$hook->getObjectNameWidgets($objectIdTags[$hook->getSource()]) | ||
); | ||
} catch (Exception $e) { | ||
Logger::error('Failed to load hook %s:', get_class($hook), $e); | ||
} | ||
} | ||
|
||
self::$objectNameWidgets = $objectDisplayNames; | ||
} | ||
|
||
/** | ||
* Get the display name for the given object | ||
* | ||
* @param Objects $obj | ||
* | ||
* @return ValidHtml | ||
*/ | ||
final public static function getObjectNameWidget(Objects $obj): ValidHtml | ||
{ | ||
/** @var Query|Source $source */ | ||
$source = $obj->source; | ||
if ($source instanceof Query) { | ||
$source = $obj->source->first(); | ||
} | ||
|
||
$objectTags = []; | ||
|
||
foreach ($obj->id_tags as $tag => $value) { | ||
$objectTags[] = sprintf('%s=%s', $tag, $value); | ||
} | ||
|
||
return self::$objectNameWidgets[$source->type][$obj->id] ?? new HtmlString(implode(', ', $objectTags)); | ||
} | ||
|
||
/** | ||
* Get the object list item widget for the object using the object ID tag | ||
* | ||
* @param string $source | ||
* @param array<string, string> $objectIdTag | ||
* | ||
* @return ?ValidHtml | ||
*/ | ||
final public static function getObjectListItemWidget(string $source, array $objectIdTag): ?ValidHtml | ||
{ | ||
$objectListItemWidget = null; | ||
foreach (Hook::all('Notifications\\EventsObjectsInfo') as $hook) { | ||
/** @var self $hook */ | ||
try { | ||
if ($source === $hook->getSource()) { | ||
$objectListItemWidget = $hook->getHtmlForObject($objectIdTag); | ||
} | ||
} catch (Exception $e) { | ||
Logger::error('Failed to load hook %s:', get_class($hook), $e); | ||
} | ||
} | ||
|
||
return $objectListItemWidget; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.