Your IP : 216.73.217.142


Current Path : /var/www/consult-e-syn/public_html/plugins/ats/deletenotes/
Upload File :
Current File : /var/www/consult-e-syn/public_html/plugins/ats/deletenotes/deletenotes.php

<?php
/**
 * @package   ats
 * @copyright Copyright (c)2011-2022 Nicholas K. Dionysopoulos / Akeeba Ltd
 * @license   GNU General Public License version 3, or later
 */

defined('_JEXEC') or die();

use Akeeba\TicketSystem\Admin\Model\ManagerNotes;
use Akeeba\TicketSystem\Admin\Model\Tickets;
use FOF40\Container\Container;
use FOF40\Timer\Timer;
use Joomla\CMS\Log\Log;
use Joomla\CMS\Plugin\CMSPlugin;

/**
 * Deletes the Manager Notes of tickets when they are closed or unpublished.
 *
 * @since 3.2.0
 */
class plgAtsDeletenotes extends CMSPlugin
{
	const atsCommandName = 'deletenotes';

	/**
	 * @inheritDoc
	 */
	public function __construct(&$subject, $config = [])
	{
		parent::__construct($subject, $config);

		if (!defined('FOF40_INCLUDED') && !@include_once(JPATH_LIBRARIES . '/fof40/include.php'))
		{
			throw new RuntimeException('This extension requires FOF 4.', 500);
		}

		$this->loadLanguage();
	}

	/**
	 * Delete the Manager Notes after a ticket is unpublished
	 *
	 * Hook into the onAfterUpdate event of ATS' Tickets model. This event is fired automatically by FOF whenever we
	 * successfully save a ticket.
	 *
	 * @param   Tickets  $ticket  The ticket which just got saved
	 *
	 * @return  void
	 * @since   3.2.0
	 */
	public function onComAtsModelTicketsAfterUpdate($ticket)
	{
		if ($this->params->get('event', 0) != 1)
		{
			return;
		}

		// Only trigger when the ticket is unpublised (not enabled) or closed (status = C)
		$mustTrigger = !$ticket->enabled || ($ticket->status == 'C');

		if (!$mustTrigger)
		{
			return;
		}

		$notes = $ticket->manager_notes;

		if (is_null($notes))
		{
			/** @var ManagerNotes $mnModel */
			$mnModel = $ticket->getContainer()->factory->model('ManagerNotes')->tmpInstance();
			$mnModel->ats_ticket_id($ticket->getId());
			$notes = $mnModel->get(true);
		}

		if ($notes->count() < 1)
		{
			return;
		}

		/** @var ManagerNotes $note */
		foreach ($notes as $note)
		{
			try
			{
				$note->delete();
			}
			catch (Exception $e)
			{
				// No sweat, we'll do a traditional delete afterwards
			}
		}

		$db    = $ticket->getDbo();
		$query = $db->getQuery(true)
			->delete($db->qn('#__ats_managernotes'))
			->where($db->qn('ats_ticket_id') . ' = ' . $ticket->getId());

		try
		{
			$db->setQuery($query)->execute();
		}
		catch (Exception $e)
		{
			// Well, not much I can do if the DB has gone tits up, yes?
		}
	}

	/**
	 * Returns information about the CRON task provided by this plugin
	 *
	 * @return  array
	 *
	 * @since   3.2.0
	 */
	public function onAtsCronTaskInfo()
	{
		$container = Container::getInstance('com_ats');

		return [
			'command'     => self::atsCommandName,
			'label'       => 'PLG_ATS_' . $this->_name . '_TASK_LABEL',
			'description' => 'PLG_ATS_' . $this->_name . '_TASK_DESC',
		];
	}

	/**
	 * Handles the ATS CRON tasks.
	 *
	 * @param   string  $command  The command name ATS CRON was asked to execute.
	 * @param   array   $options  Any options passed to this CRON job
	 *
	 * @return  bool|null  True on success, null if it's not the expected command name.
	 * @since   3.2.0
	 */
	public function onAtsCronTask($command, array $options = [])
	{
		if ($this->params->get('cron', 1) != 1)
		{
			return null;
		}

		// Make sure we are calling the correct command
		if ($command != self::atsCommandName)
		{
			return null;
		}

		$timeLimit = array_key_exists('time_limit', $options) ? $options['time_limit'] : 86400;
		$timer     = new Timer($timeLimit);
		$container = Container::getInstance('com_ats');
		$db        = $container->db;

		// Loop while we have tickets and we have not hit the time limit
		while ($timer->getTimeLeft())
		{
			// Get 100 closed / unpublished tickets with manager notes
			Log::add('Getting up to 100 closed / unpublished tickets with manager notes', Log::DEBUG, 'ats.cron');

			$subQuery = $db->getQuery(true)
				->select($db->qn('ats_ticket_id'))
				->from($db->qn('#__ats_tickets'))
				->where($db->qn('enabled') . ' = 0', 'OR')
				->where($db->qn('status') . ' = ' . $db->q('C'), 'OR');

			$query = $db->getQuery(true)
				->select($db->qn('ats_ticket_id'))
				->from($db->qn('#__ats_managernotes'))
				->where($db->qn('ats_ticket_id') . ' IN (' . $subQuery . ')')
				->group($db->qn('ats_ticket_id'))
				->order($db->qn('ats_ticket_id') . ' ASC');

			$ticketIDs = $db->setQuery($query)->loadColumn();
			$count     = count($ticketIDs);

			Log::add(sprintf('Found %u ticket(s)', $count), Log::DEBUG, 'ats.cron');

			// No more tickets? Quit.
			if (!$count)
			{
				Log::add('No more tickets to process. Returning successfully.', Log::DEBUG, 'ats.cron');

				return true;
			}

			// Delete the manager notes of these tickets
			Log::add(sprintf('Deleting manager notes from %u ticket(s)', $count), Log::DEBUG, 'ats.cron');
			$ticketIDs = array_map(function ($x) {
				return (int) $x;
			}, $ticketIDs);

			$query = $db->getQuery(true)
				->delete($db->qn('#__ats_managernotes'))
				->where($db->qn('ats_ticket_id') . ' IN (' . implode(', ', $ticketIDs) . ')');
			$db->setQuery($query)->execute();
		}

		Log::add(sprintf('I have reached the time limit of %u second(s). Stopping for now.', $timeLimit), Log::DEBUG, 'ats.cron');

		return true;
	}
}