| Current Path : /var/www/consult-e-syn/public_html/plugins/user/ats/ |
| Current File : /var/www/consult-e-syn/public_html/plugins/user/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\AddonEmails;
use FOF40\Container\Container;
use FOF40\Utils\ArrayHelper;
use Joomla\CMS\Factory;
use Joomla\CMS\Form\Form;
use Joomla\CMS\Plugin\CMSPlugin;
// Include FOF's loader if required
if (!defined('FOF40_INCLUDED') && !@include_once(JPATH_LIBRARIES . '/fof40/include.php'))
{
return;
}
class plgUserAts extends CMSPlugin
{
/**
* Constructor
*
* @access protected
*
* @param object $subject The object to observe
* @param array $config An array that holds the plugin configuration
*
* @since 1.5
*/
public function __construct(&$subject, $config)
{
parent::__construct($subject, $config);
$this->loadLanguage();
}
/**
* @param string $context The context for the data
* @param int $data The user id
* @param object
*
* @return boolean
*/
public function onContentPrepareData($context, $data)
{
// Check we are manipulating a valid form.
if (!in_array($context, ['com_users.profile', 'com_users.user', 'com_users.registration', 'com_admin.profile']))
{
return true;
}
// The $data must be an object
if (!is_object($data))
{
return true;
}
// We expect the numeric user ID in $data->id
if (!isset($data->id))
{
return true;
}
// Get the user ID
$userId = isset($data->id) ? (int) $data->id : 0;
// Make sure we have a positive integer user ID
if ($userId <= 0)
{
return true;
}
// Oh, cool, Joomla has already loaded the profile data for us.
if (isset($data->profile))
{
return true;
}
// Load the profile data from the database.
try
{
$db = Factory::getDbo();
$query = $db->getQuery(true)
->select([$db->qn('profile_key'), $db->qn('profile_value')])
->from($db->qn('#__user_profiles'))
->where($db->qn('user_id') . ' = ' . $db->q($userId))
->where($db->qn('profile_key') . ' LIKE ' . $db->q('ats.%', false))
->order($db->qn('ordering'));
$results = $db->setQuery($query)->loadRowList();// Check for a database error.
// Merge the profile data.
$data->ats = [];
foreach ($results as $v)
{
$k = str_replace('ats.', '', $v[0]);
$data->ats[$k] = $v[1];
}
}
catch (Exception $e)
{
// We suppress any database error. It means we have no data set.
}
return true;
}
/**
* @param JForm $form The form to be altered.
* @param array $data The associated data for the form.
*
* @return bool
*
* @throws Exception
*/
public function onContentPrepareForm($form, $data)
{
if (!($form instanceof Form))
{
throw new Exception('JERROR_NOT_A_FORM');
}
// Check we are manipulating a valid form.
if (!in_array($form->getName(), [
'com_admin.profile', 'com_users.user', 'com_users.registration', 'com_users.profile',
]))
{
return true;
}
// Add the registration fields to the form.
Form::addFormPath(dirname(__FILE__) . '/ats');
$form->loadFile('ats', false);
if (!$this->params->get('show_signature', 1))
{
$form->removeField('signature', 'ats');
}
if (!$this->params->get('show_multiple_email', 0))
{
$form->removeField('notification_emails', 'ats');
}
return true;
}
public function onUserAfterSave($data, $isNew, $result, $error)
{
// DO NOT REMOVE THIS LINE! It initializes the ATS autoloader, required to use the AddonEmails helper class.
$container = Container::getInstance('com_ats');
$userId = ArrayHelper::getValue($data, 'id', 0, 'int');
if ($userId && $result && isset($data['ats']) && (count($data['ats'])))
{
$db = Factory::getDbo();
$query = $db->getQuery(true)
->delete($db->qn('#__user_profiles'))
->where($db->qn('user_id') . ' = ' . $db->q($userId))
->where($db->qn('profile_key') . ' LIKE ' . $db->q('ats.%', false));
$db->setQuery($query)->execute();
$order = 1;
$query = $db->getQuery(true)
->insert($db->qn('#__user_profiles'))
->columns([$db->qn('user_id'), $db->qn('profile_key'), $db->qn('profile_value'), $db->qn('ordering')]);
foreach ($data['ats'] as $k => $v)
{
if ($k == 'notification_emails')
{
$emailAssoc = is_array($v) ? $v : AddonEmails::decodeAddonEmailJSON($v);
$emailAssoc = AddonEmails::filterAddonEmails($emailAssoc, $userId);
$v = AddonEmails::encodeAddonEmailJSON($emailAssoc);
}
$query->values($userId . ', ' . $db->quote('ats.' . $k) . ', ' . $db->quote($v) . ', ' . $order++);
}
$db->setQuery($query)->execute();
}
return true;
}
/**
* Remove all user profile information for the given user ID
*
* Method is called after user data is deleted from the database
*
* @param array $user Holds the user data
* @param boolean $success True if user was succesfully stored in the database
* @param string $msg Message
*
* @return bool
*
* @throws Exception
*/
public function onUserAfterDelete($user, $success, $msg)
{
if (!$success)
{
return false;
}
$userId = ArrayHelper::getValue($user, 'id', 0, 'int');
if ($userId)
{
$db = Factory::getDbo();
$query = $db->getQuery(true)
->delete($db->qn('#__user_profiles'))
->where($db->qn('user_id') . ' = ' . $db->q($userId))
->where($db->qn('profile_key') . ' LIKE ' . $db->q('ats.%', false));
$db->setQuery($query)->execute();
}
return true;
}
}