Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.1][com_actionlog] synch extension to log with email notification #38588

Merged
merged 24 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

/**
* @package Joomla.Administrator
* @subpackage com_actionlogs
*
* @copyright (C) 2022 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

namespace Joomla\Component\Actionlogs\Administrator\Field;

use Joomla\CMS\Application\ApplicationHelper;
use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Factory;
use Joomla\CMS\Form\Field\ListField;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Language\Text;
use Joomla\Component\Actionlogs\Administrator\Helper\ActionlogsHelper;
use Joomla\Database\ParameterType;

/**
* Field to load a list of all users that have logged actions
*
* @since __DEPLOY_VERSION__
*/
class UserlogtypeField extends ListField
{
/**
* The form field type.
*
* @var string
* @since __DEPLOY_VERSION__
*/
protected $type = 'UserLogType';

/**
* Method to get the field options.
*
* @return array The field option objects.
*
* @since __DEPLOY_VERSION__
*/
public function getOptions()
{
$db = $this->getDatabase();
$user = Factory::getApplication()->getIdentity();
$query = $db->getQuery(true)
->select($db->quoteName('extensions'))
->from($db->quoteName('#__action_logs_users'))
->where($db->quoteName('user_id') . ' = :userid')
->bind(':userid', $user->id, ParameterType::INTEGER);

$extensions = $db->setQuery($query)->loadColumn();
$userExt = [];
$params = ComponentHelper::getParams('com_actionlogs');
$globalExt = $params->get('loggable_extensions', []);

if (!empty($extensions)) {
$userExt = substr($extensions[0], 2);
$userExt = substr($userExt, 0, -2);
$userExt = explode('","', $userExt);
}

$common = array_merge($globalExt, array_intersect($globalExt, $userExt));
$options = [];

foreach ($common as $extension) {
ActionlogsHelper::loadTranslationFiles($extension);
$extensionName = Text::_($extension);
$options[ApplicationHelper::stringURLSafe($extensionName) . '_' . $extension] = HTMLHelper::_('select.option', $extension, $extensionName);
}

ksort($options);

return array_merge(parent::getOptions(), array_values($options));
}
}
2 changes: 1 addition & 1 deletion plugins/system/actionlogs/forms/actionlogs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
</field>
<field
name="actionlogsExtensions"
type="logtype"
type="userlogtype"
label="PLG_SYSTEM_ACTIONLOGS_EXTENSIONS_NOTIFICATIONS"
multiple="true"
validate="options"
Expand Down
57 changes: 57 additions & 0 deletions plugins/system/actionlogs/src/Extension/ActionLogs.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace Joomla\Plugin\System\ActionLogs\Extension;

use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Form\Form;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Language\Text;
Expand Down Expand Up @@ -347,4 +348,60 @@ public static function renderActionlogsExtensions($extensions)

return implode(', ', $extensions);
}

/**
* On Saving extensions logging method
* Method is called when an extension is being saved
*
* @param string $context The extension
* @param Table $table DataBase Table object
* @param boolean $isNew If the extension is new or not
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
public function onExtensionAfterSave($context, $table, $isNew): void
{
if ($context !== 'com_config.component' || $table->name !== 'com_actionlogs') {
return;
}

$params = ComponentHelper::getParams('com_actionlogs');
$globalExt = (array) $params->get('loggable_extensions', []);

$db = $this->getDatabase();

$query = $db->getQuery(true)
->select($db->quoteName(['user_id', 'notify', 'extensions']))
->from($db->quoteName('#__action_logs_users'));

try {
$values = $db->setQuery($query)->loadObjectList();
} catch (ExecutionFailureException $e) {
return;
}

foreach ($values as $item) {
$userExt = substr($item->extensions, 2);
$userExt = substr($userExt, 0, -2);
$user = explode('","', $userExt);
$common = array_intersect($globalExt, $user);

$extension = json_encode(array_values($common));

$query->clear()
->update($db->quoteName('#__action_logs_users'))
->set($db->quoteName('extensions') . ' = :extension')
->where($db->quoteName('user_id') . ' = :userid')
->bind(':userid', $item->user_id, ParameterType::INTEGER)
->bind(':extension', $extension);

try {
$db->setQuery($query)->execute();
} catch (ExecutionFailureException $e) {
// Do nothing.
}
}
}
}