| Current Path : /var/www/consult-e-syn/public_html/plugins/actionlog/ats/ |
| Current File : /var/www/consult-e-syn/public_html/plugins/actionlog/ats/ats.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\Helper\Permissions;
use Akeeba\TicketSystem\Site\Controller\ManagerNote;
use Akeeba\TicketSystem\Site\Controller\NewTicket;
use Akeeba\TicketSystem\Site\Controller\Post;
use Akeeba\TicketSystem\Site\Controller\Ticket;
use Akeeba\TicketSystem\Site\Model\ManagerNotes;
use Akeeba\TicketSystem\Site\Model\Posts;
use Akeeba\TicketSystem\Site\Model\Tickets;
use FOF40\Container\Container;
use FOF40\Model\DataModel\Exception\RecordNotLoaded;
use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Plugin\CMSPlugin;
class plgActionlogAts extends CMSPlugin
{
/** @var Container */
private $container;
/**
* Constructor
*
* @param object $subject The object to observe
* @param array $config An array that holds the plugin configuration
*
* @since 2.5
*/
public function __construct(&$subject, $config)
{
// Make sure ATS is installed
if (!file_exists(JPATH_ADMINISTRATOR . '/components/com_ats'))
{
return;
}
// Make sure ATS is enabled
if (!ComponentHelper::isEnabled('com_ats'))
{
return;
}
// Load FOF
if (!defined('FOF40_INCLUDED') && !@include_once(JPATH_LIBRARIES . '/fof40/include.php'))
{
return;
}
$this->container = Container::getInstance('com_ats');
// No point in logging guest actions
if (Permissions::getUser()->guest)
{
return;
}
// If any of the above statement returned, our plugin is not attached to the subject, so it's basically disabled
parent::__construct($subject, $config);
}
/**
* @param $controller NewTicket
*/
public function onComAtsControllerNewTicketAfterSave($controller)
{
/** @var Tickets $model */
$model = $controller->getModel();
$info = $this->getInfoFromTicket($model);
$key = 'COM_ATS_LOGS_TICKET_CREATED_OWN';
if ($model->created_by != $model->modified_by)
{
$user = Permissions::getUser($model->created_by);
$key = 'COM_ATS_LOGS_TICKET_CREATED_BEHALF';
$info['behalf'] = $user->username;
}
$this->container->platform->logUserAction($info, $key, 'com_ats');
}
/**
* @param $controller Post
*/
public function onComAtsControllerPostAfterSave($controller)
{
/** @var Posts $model */
$model = $controller->getModel();
$info = $this->getInfoFromTicket($model->ticket);
$post_id = $model->ats_post_id;
$info['postid'] = $post_id;
$user = Permissions::getUser();
// Do we have a modify date? It means that the user modified an existing post
if (intval($model->modified_on))
{
// Ok, but are we modifying OUR own reply, or a reply from someone else?
if ($model->modified_by != $model->created_by)
{
$key = 'COM_ATS_LOGS_POST_EDIT_OTHER';
}
else
{
$key = 'COM_ATS_LOGS_POST_EDIT_OWN';
}
}
// Nope, we're working on a new post
else
{
$key = 'COM_ATS_LOGS_POST_CREATED';
}
// Finally, this ticket belonged to us or someones else?
if ($user->id == $model->ticket->created_by)
{
$key .= '_OWN_TICKET';
}
else
{
$key .= '_OTHER_TICKET';
}
$this->container->platform->logUserAction($info, $key, 'com_ats');
}
/**
* @param $controller Ticket
*/
public function onComAtsControllerTicketAfterUnpublish($controller)
{
// This method could be invoked from frontend with direct link, from backend with direct and from backend with form submit
$ids = $this->getIDsFromRequest();
/** @var Tickets $model */
$model = $controller->getModel()->getClone();
foreach ($ids as $id)
{
try
{
$model->findOrFail($id);
}
catch (RecordNotLoaded $e)
{
// Ignore any missing record
continue;
}
$info = $this->getInfoFromTicket($model);
$this->container->platform->logUserAction($info, 'COM_ATS_LOGS_TICKET_UNPUBLISH', 'com_ats');
}
}
/**
* @param $controller Ticket
*/
public function onComAtsControllerTicketAfterClose($controller)
{
/** @var Tickets $model */
$model = $controller->getModel();
$info = $this->getInfoFromTicket($model);
$key = 'COM_ATS_LOGS_TICKET_CLOSED_OWN';
if ($model->created_by != $model->modified_by)
{
$key = 'COM_ATS_LOGS_TICKET_CLOSED_NOT_OWN';
}
$this->container->platform->logUserAction($info, $key, 'com_ats');
}
/**
* @param $controller Ticket
*/
public function onComAtsControllerTicketAfterPublic_publish($controller)
{
/** @var Tickets $model */
$model = $controller->getModel();
$info = $this->getInfoFromTicket($model);
$this->container->platform->logUserAction($info, 'COM_ATS_LOGS_TICKET_SET_PUBLIC', 'com_ats');
}
/**
* @param $controller Ticket
*/
public function onComAtsControllerTicketAfterPublic_unpublish($controller)
{
/** @var Tickets $model */
$model = $controller->getModel();
$info = $this->getInfoFromTicket($model);
$this->container->platform->logUserAction($info, 'COM_ATS_LOGS_TICKET_SET_PRIVATE', 'com_ats');
}
/**
* We have to run BEFORE the task because we're going to close the application to output a JSON response
*
* @param $controller Ticket
*/
public function onComAtsControllerTicketBeforeAssign($controller)
{
/** @var Tickets $model */
$model = $controller->getModel()->getClone();
$model->load($this->container->input->getInt('id'));
$assigned_to = Text::_('COM_ATS_TICKETS_UNASSIGNED');
$assigned_id = $this->container->input->getInt('assigned_to', 0);
if ($assigned_id)
{
$assigned_to = Permissions::getUser($assigned_id)->name;
}
$info = $this->getInfoFromTicket($model);
$info['assigned'] = $assigned_to;
$this->container->platform->logUserAction($info, 'COM_ATS_LOGS_TICKET_ASSIGNED_TO', 'com_ats');
}
/**
* P.A. This method can only be invoked from backend
*
* @param \Akeeba\TicketSystem\Admin\Controller\Ticket $controller
*/
public function onComAtsControllerTicketBeforeRemove($controller)
{
$ids = $this->getIDsFromRequest();
/** @var \Akeeba\TicketSystem\Admin\Model\Tickets $model */
$model = $controller->getModel()->getClone();
foreach ($ids as $id)
{
try
{
$model->findOrFail($id);
}
catch (RecordNotLoaded $e)
{
// Ignore any missing record
continue;
}
$info = $this->getInfoFromTicket($model);
$this->container->platform->logUserAction($info, 'COM_ATS_LOGS_TICKET_DELETED', 'com_ats');
}
}
/**
* @param ManagerNote $controller
*/
public function onComAtsControllerManagerNoteAfterSave($controller)
{
/** @var ManagerNotes $model */
$model = $controller->getModel();
$info = $this->getInfoFromTicket($model->ticket);
$info['noteid'] = $model->ats_managernote_id;
$key = 'COM_ATS_LOGS_MANAGERNOTE_CREATED';
// Do we have a modify date? It means that the user modified an existing post
if (intval($model->modified_on))
{
$key = 'COM_ATS_LOGS_MANAGERNOTE_EDIT';
}
$this->container->platform->logUserAction($info, $key, 'com_ats');
}
/**
* @param ManagerNote $controller
*/
public function onComAtsControllerManagerNoteAfterUnpublish($controller)
{
/** @var ManagerNotes $model */
$model = $controller->getModel();
$info = $this->getInfoFromTicket($model->ticket);
$info['noteid'] = $model->ats_managernote_id;
$key = 'COM_ATS_LOGS_MANAGERNOTE_UNPUBLISH';
$this->container->platform->logUserAction($info, $key, 'com_ats');
}
/**
* @param ManagerNote $controller
*/
public function onComAtsControllerManagerNoteAfterPublish($controller)
{
/** @var ManagerNotes $model */
$model = $controller->getModel();
$info = $this->getInfoFromTicket($model->ticket);
$info['noteid'] = $model->ats_managernote_id;
$key = 'COM_ATS_LOGS_MANAGERNOTE_PUBLISH';
$this->container->platform->logUserAction($info, $key, 'com_ats');
}
/**
* @param ManagerNote $controller
*/
public function onComAtsControllerManagerNoteBeforeRemove($controller)
{
/** @var ManagerNotes $model */
$model = $controller->getModel();
$info = $this->getInfoFromTicket($model->ticket);
$info['noteid'] = $model->ats_managernote_id;
$key = 'COM_ATS_LOGS_MANAGERNOTE_DELETED';
$this->container->platform->logUserAction($info, $key, 'com_ats');
}
/**
* @param \FOF40\Controller\DataController $controller
*/
public function onComAtsControllerCannedRepliesAfterApplySave($controller)
{
/** @var \Akeeba\TicketSystem\Admin\Model\CannedReplies $model */
$model = $controller->getModel();
$info = [
'id' => $model->ats_cannedreply_id,
'title' => $model->title,
];
$key = 'COM_ATS_LOGS_CANNEDREPLY_CREATED';
// Do we have a modify date? It means that the user modified an existing post
if (intval($model->modified_on))
{
$key = 'COM_ATS_LOGS_CANNEDREPLY_EDIT';
}
$this->container->platform->logUserAction($info, $key, 'com_ats');
}
/**
* @param \FOF40\Controller\DataController $controller
*/
public function onComAtsControllerCannedRepliesAfterPublish($controller)
{
$ids = $this->getIDsFromRequest();
/** @var \Akeeba\TicketSystem\Admin\Model\CannedReplies $model */
$model = $controller->getModel()->getClone();
foreach ($ids as $id)
{
try
{
$model->findOrFail($id);
}
catch (RecordNotLoaded $e)
{
// Ignore any missing record
continue;
}
$info = [
'id' => $model->ats_cannedreply_id,
'title' => $model->title,
];
$this->container->platform->logUserAction($info, 'COM_ATS_LOGS_CANNEDREPLY_PUBLISHED', 'com_ats');
}
}
/**
* @param \FOF40\Controller\DataController $controller
*/
public function onComAtsControllerCannedRepliesAfterUnpublish($controller)
{
$ids = $this->getIDsFromRequest();
/** @var \Akeeba\TicketSystem\Admin\Model\CannedReplies $model */
$model = $controller->getModel()->getClone();
foreach ($ids as $id)
{
try
{
$model->findOrFail($id);
}
catch (RecordNotLoaded $e)
{
// Ignore any missing record
continue;
}
$info = [
'id' => $model->ats_cannedreply_id,
'title' => $model->title,
];
$this->container->platform->logUserAction($info, 'COM_ATS_LOGS_CANNEDREPLY_UNPUBLISHED', 'com_ats');
}
}
/**
* @param \FOF40\Controller\DataController $controller
*/
public function onComAtsControllerCannedRepliesBeforeRemove($controller)
{
$ids = $this->getIDsFromRequest();
/** @var \Akeeba\TicketSystem\Admin\Model\CannedReplies $model */
$model = $controller->getModel()->getClone();
foreach ($ids as $id)
{
try
{
$model->findOrFail($id);
}
catch (RecordNotLoaded $e)
{
// Ignore any missing record
continue;
}
$info = [
'id' => $model->ats_cannedreply_id,
'title' => $model->title,
];
$this->container->platform->logUserAction($info, 'COM_ATS_LOGS_CANNEDREPLY_DELETED', 'com_ats');
}
}
/**
* @param \FOF40\Controller\DataController $controller
*/
public function onComAtsControllerBucketAfterApplySave($controller)
{
/** @var \Akeeba\TicketSystem\Admin\Model\Buckets $model */
$model = $controller->getModel();
$info = [
'id' => $model->ats_bucket_id,
'title' => $model->title,
];
$key = 'COM_ATS_LOGS_BUCKETS_CREATED';
// Do we have a modify date? It means that the user modified an existing post
if (intval($model->modified_on))
{
$key = 'COM_ATS_LOGS_BUCKETS_EDIT';
}
$this->container->platform->logUserAction($info, $key, 'com_ats');
}
/**
* @param \FOF40\Controller\DataController $controller
*/
public function onComAtsControllerBucketAfterPublish($controller)
{
$ids = $this->getIDsFromRequest();
/** @var \Akeeba\TicketSystem\Admin\Model\Buckets $model */
$model = $controller->getModel()->getClone();
foreach ($ids as $id)
{
try
{
$model->findOrFail($id);
}
catch (RecordNotLoaded $e)
{
// Ignore any missing record
continue;
}
$info = [
'id' => $model->ats_bucket_id,
'title' => $model->title,
];
$this->container->platform->logUserAction($info, 'COM_ATS_LOGS_BUCKETS_PUBLISHED', 'com_ats');
}
}
/**
* @param \FOF40\Controller\DataController $controller
*/
public function onComAtsControllerBucketAfterUnpublish($controller)
{
$ids = $this->getIDsFromRequest();
/** @var \Akeeba\TicketSystem\Admin\Model\Buckets $model */
$model = $controller->getModel()->getClone();
foreach ($ids as $id)
{
try
{
$model->findOrFail($id);
}
catch (RecordNotLoaded $e)
{
// Ignore any missing record
continue;
}
$info = [
'id' => $model->ats_bucket_id,
'title' => $model->title,
];
$this->container->platform->logUserAction($info, 'COM_ATS_LOGS_BUCKETS_UNPUBLISHED', 'com_ats');
}
}
/**
* @param \FOF40\Controller\DataController $controller
*/
public function onComAtsControllerBucketBeforeRemove($controller)
{
$ids = $this->getIDsFromRequest();
/** @var \Akeeba\TicketSystem\Admin\Model\Buckets $model */
$model = $controller->getModel()->getClone();
foreach ($ids as $id)
{
try
{
$model->findOrFail($id);
}
catch (RecordNotLoaded $e)
{
// Ignore any missing record
continue;
}
$info = [
'id' => $model->ats_bucket_id,
'title' => $model->title,
];
$this->container->platform->logUserAction($info, 'COM_ATS_LOGS_BUCKETS_DELETED', 'com_ats');
}
}
/**
* It needs to run _before_ the controller task since we're going to close application to echo JSON data
*
* @param \Akeeba\TicketSystem\Admin\Controller\Bucket $controller
*/
public function onComAtsControllerBucketBeforeAddtickets($controller)
{
$tickets = (array) $this->container->input->getInt('ats_ticket_id');
$bucket = (array) $this->container->input->getInt('cid');
// Missing info
if (!$tickets || !$bucket)
{
return;
}
$bucket = array_pop($bucket);
/** @var \Akeeba\TicketSystem\Admin\Model\Buckets $model */
$model = $controller->getModel();
try
{
$model->findOrFail($bucket);
}
catch (RecordNotLoaded $e)
{
// Ignore any missing record
return;
}
foreach ($tickets as $ticket)
{
$info = [
'ticketid' => $ticket,
'id' => $model->ats_bucket_id,
'title' => $model->title,
];
$this->container->platform->logUserAction($info, 'COM_ATS_LOGS_BUCKETS_ADDTICKETS', 'com_ats');
}
}
/**
* @param \Akeeba\TicketSystem\Admin\Controller\Attachment $controller
*/
public function onComAtsControllerAttachmentAfterPublish($controller)
{
/** @var \Akeeba\TicketSystem\Admin\Model\Attachments $model */
$model = $controller->getModel();
$ticket = $model->post->ticket;
$info = $this->getInfoFromTicket($ticket);
$info['file'] = $model->original_filename;
$this->container->platform->logUserAction($info, 'COM_ATS_LOGS_ATTACHMENTS_PUBLISH', 'com_ats');
}
/**
* @param \Akeeba\TicketSystem\Admin\Controller\Attachment $controller
*/
public function onComAtsControllerAttachmentAfterUnpublish($controller)
{
/** @var \Akeeba\TicketSystem\Admin\Model\Attachments $model */
$model = $controller->getModel();
$ticket = $model->post->ticket;
$info = $this->getInfoFromTicket($ticket);
$info['file'] = $model->original_filename;
$this->container->platform->logUserAction($info, 'COM_ATS_LOGS_ATTACHMENTS_UNPUBLISH', 'com_ats');
}
/**
* @param \Akeeba\TicketSystem\Admin\Controller\Attachment $controller
*/
public function onComAtsControllerAttachmentBeforeRemove($controller)
{
/** @var \Akeeba\TicketSystem\Admin\Model\Attachments $model */
$model = $controller->getModel();
if (!$model->getId())
{
$ids = $this->getIDsFromRequest();
if (!$ids)
{
return;
}
try
{
$model->findOrFail($ids[0]);
}
catch (RecordNotLoaded $e)
{
return;
}
}
$ticket = $model->post->ticket;
$info = $this->getInfoFromTicket($ticket);
$info['file'] = $model->original_filename;
$this->container->platform->logUserAction($info, 'COM_ATS_LOGS_ATTACHMENTS_DELETE', 'com_ats');
}
/**
* In this event we cheat a little. Attachments aren't saved in the usual way, so our component will send a custom
* event and we monitor that
*
* @param array $attachments Array of attachment being updated with the post id
* @param int $post_id ID of the new post that contains the attachments
*/
public function onAttachmentPostUpdate($attachments, $post_id)
{
/** @var Posts $post */
$post = $this->container->factory->model('Posts')->tmpInstance();
try
{
$post->findOrFail($post_id);
}
catch (RecordNotLoaded $e)
{
return;
}
$ticket = $post->ticket;
$info = $this->getInfoFromTicket($ticket);
/** @var \Akeeba\TicketSystem\Admin\Model\Attachments $attach_model */
$attach_model = $this->container->factory->model('Attachments')->tmpInstance();
foreach ($attachments as $attachment)
{
try
{
$attach_model->findOrFail($attachment);
}
catch (RecordNotLoaded $e)
{
continue;
}
$info['file'] = $attach_model->original_filename;
$this->container->platform->logUserAction($info, 'COM_ATS_LOGS_ATTACHMENTS_CREATED', 'com_ats');
}
}
/**
* @param Ticket $controller
*/
public function onComAtsControllerTicketBeforeRead($controller)
{
/** @var Tickets $model */
$model = $controller->getModel();
// No point continuing if we do not have a loaded model
if (!$model->getId())
{
return;
}
// Not a manager? No need to continue
if (!Permissions::isManager($model->catid))
{
return;
}
// I will log access to private tickets (always) and to public ones with manager notes. This means that if the ticket
// is public AND we do not have any manager notes, let's bail out
if ($model->public && !count($model->manager_notes))
{
return;
}
$key = '';
if (!$model->public)
{
$key = 'COM_ATS_LOGS_TICKET_READ_PRIVATE';
}
elseif ($model->public && count($model->manager_notes))
{
$key = 'COM_ATS_LOGS_TICKET_READ_MANAGERNOTES';
}
$info = $this->getInfoFromTicket($model);
$this->container->platform->logUserAction($info, $key, 'com_ats');
}
/**
* Creates User Log title starting from a ticket entry
*
* @param \Akeeba\TicketSystem\Admin\Model\Tickets $model
*
* @return array
*/
private function getInfoFromTicket(\Akeeba\TicketSystem\Admin\Model\Tickets $model)
{
$id = $model->ats_ticket_id;
$title = $model->title;
$category = $model->joomla_category->title;
$keys = [
'ticketid' => $id,
'title' => $title,
'category' => $category,
];
return $keys;
}
/**
* Gets the list of IDs from the request data
*
* @return array
*/
private function getIDsFromRequest()
{
// Get the ID or list of IDs from the request or the configuration
$cid = $this->container->input->get('cid', [], 'array');
$id = $this->container->input->getInt('id', 0);
$ids = [];
if (is_array($cid) && !empty($cid))
{
$ids = $cid;
}
elseif (!empty($id))
{
$ids = [$id];
}
return $ids;
}
}