Skip to content

Commit

Permalink
[privacy] Move common code to PrivacyPlugin class (#22402)
Browse files Browse the repository at this point in the history
* Move duplicated code to the PrivacyPlugin

* Code style
  • Loading branch information
laoneo authored and Michael Babker committed Oct 6, 2018
1 parent a0fab61 commit 0ff9318
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 281 deletions.
60 changes: 60 additions & 0 deletions administrator/components/com_privacy/helpers/plugin.php
Expand Up @@ -12,6 +12,7 @@
JLoader::register('PrivacyExportDomain', __DIR__ . '/export/domain.php');
JLoader::register('PrivacyExportField', __DIR__ . '/export/field.php');
JLoader::register('PrivacyExportItem', __DIR__ . '/export/item.php');
JLoader::register('FieldsHelper', JPATH_ADMINISTRATOR . '/components/com_fields/helpers/fields.php');

/**
* Base class for privacy plugins
Expand All @@ -20,6 +21,22 @@
*/
abstract class PrivacyPlugin extends JPlugin
{
/**
* Database object
*
* @var JDatabaseDriver
* @since 3.9.0
*/
protected $db;

/**
* Affects constructor behaviour. If true, language files will be loaded automatically.
*
* @var boolean
* @since 3.9.0
*/
protected $autoloadLanguage = true;

/**
* Create a new domain object
*
Expand Down Expand Up @@ -96,4 +113,47 @@ protected function createItemForTable($table)

return $this->createItemFromArray($data, $table->{$table->getKeyName(false)});
}

/**
* Helper function to create the domain for the items custom fields.
*
* @param string $context The context
* @param stdClass $item The items
*
* @return PrivacyExportDomain
*
* @since 3.9.0
*/
protected function createCustomFieldsDomain($context, $item)
{
$parts = FieldsHelper::extract($context);

if (!$parts)
{
return array();
}

$type = str_replace('com_', '', $parts[0]);

$domain = $this->createDomain($type . '_custom_fields', 'joomla_' . $type . '_custom_fields_data');

// Get item's fields, also preparing their value property for manual display
$fields = FieldsHelper::getFields($parts[0] . '.' . $parts[1], $item);

foreach ($fields as $field)
{
$fieldValue = is_array($field->value) ? implode(', ', $field->value) : $field->value;

$data = array(
$type . '_id' => $item->id,
'field_name' => $field->name,
'field_title' => $field->title,
'field_value' => $fieldValue,
);

$domain->addItem($this->createItemFromArray($data));
}

return $domain;
}
}
16 changes: 0 additions & 16 deletions plugins/privacy/actionlogs/actionlogs.php
Expand Up @@ -19,22 +19,6 @@
*/
class PlgPrivacyActionlogs extends PrivacyPlugin
{
/**
* Database object
*
* @var JDatabaseDriver
* @since 3.9.0
*/
protected $db;

/**
* Affects constructor behavior. If true, language files will be loaded automatically.
*
* @var boolean
* @since 3.9.0
*/
protected $autoloadLanguage = true;

/**
* Processes an export request for Joomla core actionlog data
*
Expand Down
109 changes: 13 additions & 96 deletions plugins/privacy/contact/contact.php
Expand Up @@ -9,7 +9,6 @@

defined('_JEXEC') or die;

JLoader::register('FieldsHelper', JPATH_ADMINISTRATOR . '/components/com_fields/helpers/fields.php');
JLoader::register('PrivacyPlugin', JPATH_ADMINISTRATOR . '/components/com_privacy/helpers/plugin.php');

/**
Expand All @@ -19,30 +18,6 @@
*/
class PlgPrivacyContact extends PrivacyPlugin
{
/**
* Database object
*
* @var JDatabaseDriver
* @since 3.9.0
*/
protected $db;

/**
* Affects constructor behaviour. If true, language files will be loaded automatically.
*
* @var boolean
* @since 3.9.0
*/
protected $autoloadLanguage = true;

/**
* Contacts array
*
* @var array
* @since 3.9.0
*/
protected $contacts = array();

/**
* Processes an export request for Joomla core user contact data
*
Expand All @@ -59,95 +34,37 @@ class PlgPrivacyContact extends PrivacyPlugin
*/
public function onPrivacyExportRequest(PrivacyTableRequest $request, JUser $user = null)
{
if ((!$user) && (!$request->email))
if (!$user && !$request->email)
{
return array();
}

$domains = array();
$domains[] = $this->createContactDomain($request, $user);

// An user may have more than 1 contact linked to them
foreach ($this->contacts as $contact)
{
$domains[] = $this->createContactCustomFieldsDomain($contact);
}
$domain = $this->createDomain('user_contact', 'joomla_user_contact_data');
$domains[] = $domain;

return $domains;
}

/**
* Create the domain for the user contact data
*
* @param PrivacyTableRequest $request The request record being processed
* @param JUser $user The user account associated with this request if available
*
* @return PrivacyExportDomain
*
* @since 3.9.0
*/
private function createContactDomain(PrivacyTableRequest $request, JUser $user = null)
{
$domain = $this->createDomain('user_contact', 'joomla_user_contact_data');
$query = $this->db->getQuery(true)
->select('*')
->from($this->db->quoteName('#__contact_details'))
->order($this->db->quoteName('ordering') . ' ASC');

if ($user)
{
$query = $this->db->getQuery(true)
->select('*')
->from($this->db->quoteName('#__contact_details'))
->where($this->db->quoteName('user_id') . ' = ' . (int) $user->id)
->order($this->db->quoteName('ordering') . ' ASC');
$query->where($this->db->quoteName('user_id') . ' = ' . (int) $user->id);
}
else
{
$query = $this->db->getQuery(true)
->select('*')
->from($this->db->quoteName('#__contact_details'))
->where($this->db->quoteName('email_to') . ' = ' . $this->db->quote($request->email))
->order($this->db->quoteName('ordering') . ' ASC');
$query->where($this->db->quoteName('email_to') . ' = ' . $this->db->quote($request->email));
}

$items = $this->db->setQuery($query)->loadAssocList();
$items = $this->db->setQuery($query)->loadObjectList();

foreach ($items as $item)
{
$domain->addItem($this->createItemFromArray($item));
$this->contacts[] = (object) $item;
}

return $domain;
}

/**
* Create the domain for the contact custom fields
*
* @param Object $contact The contact to process
*
* @return PrivacyExportDomain
*
* @since 3.9.0
*/
private function createContactCustomFieldsDomain($contact)
{
$domain = $this->createDomain('contact_custom_fields', 'joomla_contact_custom_fields_data');

// Get item's fields, also preparing their value property for manual display
$fields = FieldsHelper::getFields('com_contact.contact', $contact);

foreach ($fields as $field)
{
$fieldValue = is_array($field->value) ? implode(', ', $field->value) : $field->value;

$data = array(
'contact_id' => $contact->id,
'field_name' => $field->name,
'field_title' => $field->title,
'field_value' => $fieldValue,
);

$domain->addItem($this->createItemFromArray($data));
$domain->addItem($this->createItemFromArray((array) $item));
$domains[] = $this->createCustomFieldsDomain('com_contact.contact', $item);
}

return $domain;
return $domains;
}
}
90 changes: 6 additions & 84 deletions plugins/privacy/content/content.php
Expand Up @@ -9,7 +9,6 @@

defined('_JEXEC') or die;

JLoader::register('FieldsHelper', JPATH_ADMINISTRATOR . '/components/com_fields/helpers/fields.php');
JLoader::register('PrivacyPlugin', JPATH_ADMINISTRATOR . '/components/com_privacy/helpers/plugin.php');

/**
Expand All @@ -19,30 +18,6 @@
*/
class PlgPrivacyContent extends PrivacyPlugin
{
/**
* Database object
*
* @var JDatabaseDriver
* @since 3.9.0
*/
protected $db;

/**
* Affects constructor behaviour. If true, language files will be loaded automatically.
*
* @var boolean
* @since 3.9.0
*/
protected $autoloadLanguage = true;

/**
* Contents array
*
* @var array
* @since 3.9.0
*/
protected $contents = array();

/**
* Processes an export request for Joomla core user content data
*
Expand All @@ -65,76 +40,23 @@ public function onPrivacyExportRequest(PrivacyTableRequest $request, JUser $user
}

$domains = array();
$domains[] = $this->createContentDomain($user);

foreach ($this->contents as $content)
{
$domains[] = $this->createContentCustomFieldsDomain($content);
}

return $domains;
}

/**
* Create the domain for the user content data
*
* @param JUser $user The user account associated with this request
*
* @return PrivacyExportDomain
*
* @since 3.9.0
*/
private function createContentDomain(JUser $user)
{
$domain = $this->createDomain('user_content', 'joomla_user_content_data');
$domain = $this->createDomain('user_content', 'joomla_user_content_data');
$domains[] = $domain;

$query = $this->db->getQuery(true)
->select('*')
->from($this->db->quoteName('#__content'))
->where($this->db->quoteName('created_by') . ' = ' . (int) $user->id)
->order($this->db->quoteName('ordering') . ' ASC');

$items = $this->db->setQuery($query)->loadAssocList();
$items = $this->db->setQuery($query)->loadObjectList();

foreach ($items as $item)
{
$domain->addItem($this->createItemFromArray($item));
$this->contents[] = (object) $item;
$domain->addItem($this->createItemFromArray((array) $item));
$domains[] = $this->createCustomFieldsDomain('com_content.article', $item);
}

return $domain;
}

/**
* Create the domain for the content custom fields
*
* @param Object $content The content to process
*
* @return PrivacyExportDomain
*
* @since 3.9.0
*/
private function createContentCustomFieldsDomain($content)
{
$domain = $this->createDomain('content_custom_fields', 'joomla_content_custom_fields_data');

// Get item's fields, also preparing their value property for manual display
$fields = FieldsHelper::getFields('com_content.article', $content);

foreach ($fields as $field)
{
$fieldValue = is_array($field->value) ? implode(', ', $field->value) : $field->value;

$data = array(
'content_id' => $content->id,
'field_name' => $field->name,
'field_title' => $field->title,
'field_value' => $fieldValue,
);

$domain->addItem($this->createItemFromArray($data));
}

return $domain;
return $domains;
}
}

0 comments on commit 0ff9318

Please sign in to comment.