Documentation

Recycle bin for deleted entities

This module manages recycle bin for deleted entities (tasks and templates).

Before using this module, check if it is installed and connect it via the following structure:

\Bitrix\Main\Loader::includeModule('recyclebin');

Class Description Available from version
Recyclebin Class contains methods for handling recycle bin for deleted entities (tasks and templates).


Example

(specifying your handler for processing recycle bin for "Tasks")

First, create a manager class that will return necessary data when requested by recycle bin:

\Bitrix\Tasks\Integration\Recyclebin\Manager

class Manager
{
   const TASKS_RECYCLEBIN_ENTITY = 'tasks_task';
   const TASKS_TEMPLATE_RECYCLEBIN_ENTITY = 'tasks_template';
   const MODULE_ID = 'tasks';

   public static function OnModuleSurvey()
   {
      $data = [];

      $data[self::TASKS_RECYCLEBIN_ENTITY] = array(
         'NAME'    => Loc::getMessage('TASKS_RECYCLEBIN_ENTITY_NAME'), // Entity name
         'HANDLER' => \Bitrix\Tasks\Integration\Recyclebin\Task::class // Entity handler class
      );

      $data[self::TASKS_TEMPLATE_RECYCLEBIN_ENTITY] = array(
         'NAME'    => Loc::getMessage('TASKS_TEMPLATE_RECYCLEBIN_ENTITY_NAME'),
         'HANDLER' => \Bitrix\Tasks\Integration\Recyclebin\Template::class
      );

      return new EventResult(
         EventResult::SUCCESS, [
         'NAME' => Loc::getMessage('TASKS_RECYCLEBIN_MODULE_NAME'),
         'LIST' => $data
      ], self::MODULE_ID
      );
   }
}
 

Second, "register" this manager into Recycle bin events:

$eventManager = \Bitrix\Main\EventManager::getInstance();

//Recyclebin
$eventManager->registerEventHandler(
   'recyclebin',
   'OnModuleSurvey',
   'tasks',
   '\Bitrix\Tasks\Integration\Recyclebin\Manager',
   'OnModuleSurvey'
);
 

Now, when we open the recycle bin, it executes the method \Bitrix\Recyclebin\Recyclebin::getAvailableModules() that informs it about which entities it is ready to handle.

\Bitrix\Recyclebin\Internals\Contracts\Recyclebinable to have an option to handle the recycle bin.

interface Recyclebinable
{

   /**
    * Method for restoring from recycle bin
    * 
    * @param Entity $entity
    *
    * @return boolean
    */
   public static function moveFromRecyclebin(Entity $entity);

   /**
    * Method for deleting from recycle bin
    * 
    * @param Entity $entity
    *
    * @return boolean
    */
   public static function removeFromRecyclebin(Entity $entity);

   /**
    * Method for recycle bin preview
    * 
    * @param Entity $entity
    *
    * @return boolean
    */
   public static function previewFromRecyclebin(Entity $entity);


   /**
    * Method that provides localizations for action results. 
    * NOTIFY - Task deleted/restored 
    * CONFIRM - Are you sure you want to restore/delete task?
    * 
    * @return array
    *
    * [
    *     'NOTIFY'=> [
    *        'RESTORE' => Loc::getMessage(''),
    *        'REMOVE' => Loc::getMessage(''),
    *     ],
    *     'CONFIRM' => [
    *        'RESTORE' => Loc::getMessage(''),
    *        'REMOVE' => Loc::getMessage('')
    *     ]
    */
   public static function getNotifyMessages();
}
 

Such handlers must be described for each restored entity (with its localizations).

When task is deleted, method OnBeforeTaskDelete is called (this event must be registered separately).

use Bitrix\Recyclebin\Internals\Entity;

public static function OnBeforeTaskDelete($taskId, array $task = [])
{
   $recyclebin = new Entity($taskId, Manager::TASKS_RECYCLEBIN_ENTITY, Manager::MODULE_ID); // create a recycle bin object
   $recyclebin->setTitle($task['TITLE']); // set title

   $additionalData = self::collectTaskAdditionalData($taskId); //collect data to be saved in the recycle bin in the action-data format
// for example: [MEMBERS=>[1,2,3,4,5]], then use the key MEMBERS to know what to restore
   if ($additionalData)
   {
      foreach ($additionalData as $action => $data)
      {
         $recyclebin->add($action, $data);  // add to recycle bin
      }
   }

   $result = $recyclebin->save(); // save in recycle bin
   $resultData = $result->getData();

   return $resultData['ID']; // get ID of created recycle bin
}
 

Restoration:

use Bitrix\Recyclebin\Internals\Entity;

public static function moveFromRecyclebin(Entity $entity)
{
   $result = new Result();

   $connection = Application::getConnection();

      $connection->queryExecute(
         'UPDATE '.TaskTable::getTableName().' SET ZOMBIE=\'N\' WHERE ID='.$entity->getEntityId()
      ); // restored task (in this case, API was not used due to unavailability of such feature
      $arLogFields = array(
         "TASK_ID" => $entity->getEntityId(),
         "USER_ID" => User::getId(),
         "CREATED_DATE" => new DateTime(),
         "FIELD" => 'RENEW'
      );

      $log = new \CTaskLog();
      $log->Add($arLogFields); // add restoration information to task log


   $dataEntity = $entity->getData(); // get saved data from recycle bin


      if ($dataEntity)
      {
         foreach ($dataEntity as $value)
         {
            $data = unserialize($value['DATA']);
            $action = $value['ACTION'];

            self::restoreTaskAdditionalData($entity->getEntityId(), $action, $data); // restore data based on saved array
         }
      }

      $task = \CTaskItem::getInstance($entity->getEntityId(), 1);
      $task->update([], ['FORCE_RECOUNT_COUNTER'=>'Y']); // recalculate task counters


   return $result; // result 
}
 

© «Bitrix24», 2001-2024