Initial import of animal_management module
This commit is contained in:
182
Events.php
Normal file
182
Events.php
Normal file
@@ -0,0 +1,182 @@
|
||||
<?php
|
||||
|
||||
namespace humhub\modules\animal_management;
|
||||
|
||||
use humhub\modules\animal_management\models\Animal;
|
||||
use humhub\modules\animal_management\models\AnimalGalleryItem;
|
||||
use humhub\modules\animal_management\services\GalleryIntegrationService;
|
||||
use humhub\modules\file\models\File;
|
||||
use humhub\modules\post\models\Post;
|
||||
use humhub\modules\space\models\Space;
|
||||
use Yii;
|
||||
|
||||
class Events
|
||||
{
|
||||
public static function onSpaceMenuInit($event): void
|
||||
{
|
||||
$space = $event->sender->space ?? null;
|
||||
if ($space === null || !$space->moduleManager->isEnabled('animal_management')) {
|
||||
return;
|
||||
}
|
||||
|
||||
$event->sender->addItem([
|
||||
'label' => Yii::t('AnimalManagementModule.base', 'Animals'),
|
||||
'group' => 'modules',
|
||||
'url' => $space->createUrl('/animal_management/animals/index'),
|
||||
'icon' => '<i class="fa fa-paw"></i>',
|
||||
'sortOrder' => 110,
|
||||
'isActive' => (
|
||||
Yii::$app->controller
|
||||
&& Yii::$app->controller->module
|
||||
&& Yii::$app->controller->module->id === 'animal_management'
|
||||
&& Yii::$app->controller->id === 'animals'
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
public static function onRescueSettingsMenuInit($event): void
|
||||
{
|
||||
$space = $event->sender->space ?? null;
|
||||
if ($space === null || !$space->moduleManager->isEnabled('animal_management')) {
|
||||
return;
|
||||
}
|
||||
|
||||
$event->sender->addItem([
|
||||
'label' => Yii::t('AnimalManagementModule.base', 'Animal Management'),
|
||||
'url' => $space->createUrl('/animal_management/settings'),
|
||||
'sortOrder' => 300,
|
||||
'isActive' => (
|
||||
Yii::$app->controller
|
||||
&& Yii::$app->controller->module
|
||||
&& Yii::$app->controller->module->id === 'animal_management'
|
||||
&& Yii::$app->controller->id === 'settings'
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
public static function onSpaceAdminMenuInitFallback($event): void
|
||||
{
|
||||
$space = $event->sender->space ?? null;
|
||||
if ($space === null || !$space->moduleManager->isEnabled('animal_management')) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($space->moduleManager->isEnabled('rescue_foundation') || !$space->isAdmin()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$event->sender->addItem([
|
||||
'label' => Yii::t('AnimalManagementModule.base', 'Animal Management'),
|
||||
'group' => 'admin',
|
||||
'url' => $space->createUrl('/animal_management/settings'),
|
||||
'icon' => '<i class="fa fa-paw"></i>',
|
||||
'sortOrder' => 600,
|
||||
'isActive' => (
|
||||
Yii::$app->controller
|
||||
&& Yii::$app->controller->module
|
||||
&& Yii::$app->controller->module->id === 'animal_management'
|
||||
&& Yii::$app->controller->id === 'settings'
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
public static function onPostAfterSave($event): void
|
||||
{
|
||||
$post = $event->sender ?? null;
|
||||
if (!$post instanceof Post) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (Yii::$app->db->schema->getTableSchema('rescue_animal_gallery_item', true) === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
$message = trim((string)$post->message);
|
||||
if ($message === '' || stripos($message, '#gallery') === false) {
|
||||
return;
|
||||
}
|
||||
|
||||
$content = $post->content;
|
||||
$container = $content->container ?? null;
|
||||
if (!$container instanceof Space) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!$container->moduleManager->isEnabled('animal_management')) {
|
||||
return;
|
||||
}
|
||||
|
||||
$attachedFiles = File::findByRecord($post);
|
||||
if (empty($attachedFiles)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$imageFiles = [];
|
||||
foreach ($attachedFiles as $file) {
|
||||
if (!$file instanceof File) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (strpos((string)$file->mime_type, 'image/') !== 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$imageFiles[] = $file;
|
||||
}
|
||||
|
||||
if (empty($imageFiles)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$animals = Animal::find()
|
||||
->where(['contentcontainer_id' => $container->contentcontainer_id])
|
||||
->all();
|
||||
|
||||
if (empty($animals)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$matchedAnimals = [];
|
||||
$searchableMessage = strtolower(strip_tags($message));
|
||||
foreach ($animals as $animal) {
|
||||
$uid = strtolower(trim((string)$animal->animal_uid));
|
||||
if ($uid === '') {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (strpos($searchableMessage, $uid) !== false) {
|
||||
$matchedAnimals[] = $animal;
|
||||
}
|
||||
}
|
||||
|
||||
if (empty($matchedAnimals)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$createdBy = Yii::$app->user->isGuest ? null : (int)Yii::$app->user->id;
|
||||
foreach ($matchedAnimals as $animal) {
|
||||
foreach ($imageFiles as $file) {
|
||||
$exists = AnimalGalleryItem::findOne([
|
||||
'animal_id' => (int)$animal->id,
|
||||
'file_id' => (int)$file->id,
|
||||
]);
|
||||
|
||||
if ($exists instanceof AnimalGalleryItem) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$item = new AnimalGalleryItem();
|
||||
$item->animal_id = (int)$animal->id;
|
||||
$item->file_id = (int)$file->id;
|
||||
$item->source_post_id = (int)$post->id;
|
||||
$item->source_type = 'post';
|
||||
$item->created_by = $createdBy;
|
||||
$item->save();
|
||||
}
|
||||
|
||||
if ($container instanceof Space) {
|
||||
GalleryIntegrationService::ensureAnimalGallery($animal, $container);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user