Skip to content

Commit

Permalink
Internal: Add entity listener to track deleted resource link
Browse files Browse the repository at this point in the history
  • Loading branch information
AngelFQC committed Nov 28, 2024
1 parent 0abb3e1 commit 48175c8
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 0 deletions.
6 changes: 6 additions & 0 deletions public/main/inc/lib/api.lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,12 @@
define('LOG_MESSAGE_DATA', 'message_data');
define('LOG_MESSAGE_DELETE', 'msg_deleted');

const LOG_RESOURCE_LINK_DELETE = 'resource_link_deleted';
const LOG_RESOURCE_LINK_SOFT_DELETE = 'resource_link_soft_deleted';
const LOG_RESOURCE_NODE = 'resource_node_id';
const LOG_RESOURCE_LINK = 'resource_link_id';
const LOG_RESOURCE_NODE_AND_RESOURCE_LINK = 'resource_node_id_and_resource_link_id';

define('LOG_USER_DELETE', 'user_deleted');
define('LOG_USER_PREDELETE', 'user_predeleted');
define('LOG_USER_CREATE', 'user_created');
Expand Down
29 changes: 29 additions & 0 deletions src/CoreBundle/Entity/Listener/ResourceLinkListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/* For licensing terms, see /license.txt */

declare(strict_types=1);

namespace Chamilo\CoreBundle\Entity\Listener;

use Chamilo\CoreBundle\Entity\ResourceLink;
use Doctrine\ORM\Event\PostRemoveEventArgs;
use Doctrine\ORM\Exception\ORMException;
use Event;

class ResourceLinkListener
{
/**
* @throws ORMException
*/
public function postRemove(ResourceLink $resourceLink, PostRemoveEventArgs $args): void
{
$resourceNode = $resourceLink->getResourceNode();

Event::addEvent(
LOG_RESOURCE_LINK_DELETE,
LOG_RESOURCE_NODE,
$resourceNode->getId(),
);
}
}
2 changes: 2 additions & 0 deletions src/CoreBundle/Entity/ResourceLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
namespace Chamilo\CoreBundle\Entity;

use ApiPlatform\Metadata\ApiResource;
use Chamilo\CoreBundle\Entity\Listener\ResourceLinkListener;
use Chamilo\CoreBundle\Repository\ResourceLinkRepository;
use Chamilo\CoreBundle\Traits\TimestampableTypedEntity;
use Chamilo\CourseBundle\Entity\CGroup;
Expand All @@ -27,6 +28,7 @@
name: 'idx_resource_link_sortable_groups'
)]
#[ORM\Entity(repositoryClass: ResourceLinkRepository::class)]
#[ORM\EntityListeners([ResourceLinkListener::class])]
#[Gedmo\SoftDeleteable(fieldName: 'deletedAt', timeAware: false, hardDelete: true)]
class ResourceLink implements Stringable
{
Expand Down
36 changes: 36 additions & 0 deletions src/CoreBundle/EventListener/ResourceLinkListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/* For licensing terms, see /license.txt */

declare(strict_types=1);

namespace Chamilo\CoreBundle\EventListener;

use Chamilo\CoreBundle\Entity\ResourceLink;
use Doctrine\Bundle\DoctrineBundle\Attribute\AsDoctrineListener;
use Doctrine\ORM\Exception\ORMException;
use Event;
use Gedmo\SoftDeleteable\Event\PostSoftDeleteEventArgs;
use Gedmo\SoftDeleteable\SoftDeleteableListener;

#[AsDoctrineListener(event: SoftDeleteableListener::POST_SOFT_DELETE, connection: 'default')]
class ResourceLinkListener
{
/**
* @throws ORMException
*/
public function postSoftDelete(PostSoftDeleteEventArgs $args): void
{
$object = $args->getObject();

if (!$object instanceof ResourceLink) {
return;
}

Event::addEvent(
LOG_RESOURCE_LINK_SOFT_DELETE,
LOG_RESOURCE_LINK,
$object->getId(),
);
}
}
2 changes: 2 additions & 0 deletions src/CoreBundle/Resources/config/listeners.yml
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,5 @@ services:
- {name: doctrine.orm.entity_listener, entity_manager: default, lazy: true}

Chamilo\CoreBundle\EventListener\MessageStatusListener: ~

Chamilo\CoreBundle\EventListener\ResourceLinkListener: ~

0 comments on commit 48175c8

Please sign in to comment.