Your IP : 216.73.216.68


Current Path : /home/jeromecohp/www/plugins/actionlog/emailbeautifier/
Upload File :
Current File : /home/jeromecohp/www/plugins/actionlog/emailbeautifier/emailbeautifier.php

<?php
/**
 * @package     EmailBeautifier
 * @subpackage  Actionlog.emailbeautifier
 *
 * @author      Techjoomla <extensions@techjoomla.com>
 * @copyright   Copyright (C) 2009 - 2022 Techjoomla. All rights reserved.
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
 */

// No direct access.
defined('_JEXEC') or die('Restricted access');

use Joomla\CMS\Application\CMSApplication;
use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Factory;
use Joomla\CMS\MVC\Model\BaseDatabaseModel;
use Joomla\CMS\Plugin\CMSPlugin;
use Joomla\CMS\Table\Table;
use Joomla\Data\DataObject;

JLoader::register('ActionlogsHelper', JPATH_ADMINISTRATOR . '/components/com_actionlogs/helpers/actionlogs.php');

/**
 * JTicketing Actions Logging Plugin.
 *
 * @since  2.1.0
 */
class PlgActionlogEmailbeautifier extends CMSPlugin
{
	/**
	 * Application object.
	 *
	 * @var    CMSApplication
	 * @since  2.1.0
	 */
	protected $app;

	/**
	 * Database object.
	 *
	 * @var    JDatabaseDriver
	 * @since  2.1.0
	 */
	protected $db;

	/**
	 * Load plugin language file automatically so that it can be used inside component
	 *
	 * @var    boolean
	 * @since  2.1.0
	 */
	protected $autoloadLanguage = true;

	/**
	 * On saving of template
	 *
	 * Method is called after saving a template
	 *
	 * @param   array    $templateDetails  Template details
	 * @param   boolean  $isNew            Save or edit?
	 *
	 * @return  void
	 *
	 * @since   2.1.0
	 */
	public function ebOnAfterTemplateSave($templateDetails, $isNew)
	{
		if (!$this->params->get('logActionForTemplateSave', 1))
		{
			return;
		}

		$option   = $this->app->input->getCmd('option');
		$user     = Factory::getUser();
		$userId   = $user->id;
		$userName = $user->username;

		$action               = $isNew ? 'add' : 'edit';
		$messageLanguageKey   = $isNew ? 'PLG_ACTIONLOGS_EMAILBEAUTIFIER_TEMPLATE_ADDED' : 'PLG_ACTIONLOGS_EMAILBEAUTIFIER_TEMPLATE_EDITED';

		$message = array(
			'action'           => $action,
			'userAccountLink'  => 'index.php?option=com_users&task=user.edit&id=' . $userId,
			'userName'         => $userName,
			'editTemplateLink' => 'index.php?option=com_emailbeautifier&task=template.edit&id=' . $templateDetails['id'],
			'templateName'     => $templateDetails['type']
		);

		$this->addLog(array($message), $messageLanguageKey, $option, $userId);
	}

	/**
	 * On content change status logging method
	 * This method adds a record to #__action_logs contains (message, date, context, user)
	 * Method is called when the status of the article is changed
	 *
	 * @param   string   $context  The context of the content passed to the plugin
	 * @param   array    $pks      An array of primary key ids of the content that has changed state.
	 * @param   integer  $value    The value of the state that the content has been changed to.
	 *
	 * @return  void
	 *
	 * @since   2.1.0
	 */
	public function ebOnAfterTemplateChangeState($context, $pks, $value)
	{
		if (!$this->params->get('logActionForTemplateStatusChange', 1))
		{
			return;
		}

		$option   = $this->app->input->getCmd('option');
		$user     = Factory::getUser();
		$userId   = $user->id;
		$userName = $user->username;

		$action               = $value ? 'publish' : 'unpublish';
		$messageLanguageKey   = $value ? 'PLG_ACTIONLOGS_EMAILBEAUTIFIER_TEMPLATE_PUBLISHED' : 'PLG_ACTIONLOGS_EMAILBEAUTIFIER_TEMPLATE_UNPUBLISHED';

		$ebTableTemplate = Table::getInstance('Template', 'EmailBeautiferTable', array());

		foreach ($pks as $pk)
		{
			$ebTableTemplate->load(array('id' => $pk));

			$message = array(
				'action'           => $action,
				'userAccountLink'  => 'index.php?option=com_users&task=user.edit&id=' . $userId,
				'userName'         => $userName,
				'editTemplateLink' => 'index.php?option=com_emailbeautifier&task=template.edit&id=' . $pk,
				'templateName'     => $ebTableTemplate->type
			);

			$this->addLog(array($message), $messageLanguageKey, $option, $userId);
		}
	}

	/**
	 * On content change status logging method
	 * This method adds a record to #__action_logs contains (message, date, context, user)
	 * Method is called when the status of the article is changed
	 *
	 * @param   array    $pk       Template id
	 * @param   integer  $default  0 or 1
	 *
	 * @return  void
	 *
	 * @since   2.1.0
	 */
	public function ebOnAfterTemplateToggleDefault($pk, $default)
	{
		if (!$this->params->get('logActionForTemplateToggleDefault', 1))
		{
			return;
		}

		$option   = $this->app->input->getCmd('option');
		$user     = Factory::getUser();
		$userId   = $user->id;
		$userName = $user->username;

		$action               = $default ? 'setDefault' : 'unsetDefault';
		$messageLanguageKey   = $default ? 'PLG_ACTIONLOGS_EMAILBEAUTIFIER_TEMPLATE_SET_DEFAULT' : 'PLG_ACTIONLOGS_EMAILBEAUTIFIER_TEMPLATE_UNSET_DEFAULT';

		$ebTableTemplate = Table::getInstance('Template', 'EmailBeautiferTable', array());

		$ebTableTemplate->load(array('id' => $pk));

		$message = array(
			'action'           => $action,
			'userAccountLink'  => 'index.php?option=com_users&task=user.edit&id=' . $userId,
			'userName'         => $userName,
			'editTemplateLink' => 'index.php?option=com_emailbeautifier&task=template.edit&id=' . $pk,
			'templateName'     => $ebTableTemplate->type
		);

		$this->addLog(array($message), $messageLanguageKey, $option, $userId);
	}

	/**
	 * On after deleting template data logging method
	 *
	 * Method is called after teamplate data is deleted from  the database.
	 *
	 * @param   string  $context  com_emailbeautifier
	 * @param   Object  $table    Holds the template data
	 *
	 * @return  void
	 *
	 * @since   2.1.0
	 */
	public function ebOnAfterTemplateDelete($context, $table)
	{
		if (!$this->params->get('logActionForTemplateDelete', 1))
		{
			return;
		}

		$option   = $this->app->input->getCmd('option');
		$user     = Factory::getUser();
		$userId   = $user->id;
		$userName = $user->username;

		$action               = 'delete';
		$messageLanguageKey   = 'PLG_ACTIONLOGS_EMAILBEAUTIFIER_TEMPLATE_DELETED';

		$message = array(
			'action'           => $action,
			'userAccountLink'  => 'index.php?option=com_users&task=user.edit&id=' . $userId,
			'userName'         => $userName,
			'templateName'     => $table->type
		);

		$this->addLog(array($message), $messageLanguageKey, $option, $userId);
	}

	/**
	 * Proxy for ActionlogsModelUserlog addLog method
	 *
	 * This method adds a record to #__action_logs contains (message_language_key, message, date, context, user)
	 *
	 * @param   array   $messages            The contents of the messages to be logged
	 * @param   string  $messageLanguageKey  The language key of the message
	 * @param   string  $context             The context of the content passed to the plugin
	 * @param   int     $userId              ID of user perform the action, usually ID of current logged in user
	 *
	 * @return  void
	 *
	 * @since   2.1.0
	 */
	protected function addLog($messages, $messageLanguageKey, $context, $userId = null)
	{
		JLoader::register('ActionlogsModelActionlog', JPATH_ADMINISTRATOR . '/components/com_actionlogs/models/actionlog.php');

		/* @var ActionlogsModelActionlog $model */
		$model = BaseDatabaseModel::getInstance('Actionlog', 'ActionlogsModel');
		$model->addLog($messages, $messageLanguageKey, $context, $userId);
	}
}