[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/administrator/components/com_privacy/src/Plugin/ -> PrivacyPlugin.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Administrator
   5   * @subpackage  com_privacy
   6   *
   7   * @copyright   (C) 2018 Open Source Matters, Inc. <https://www.joomla.org>
   8   * @license     GNU General Public License version 2 or later; see LICENSE.txt
   9   */
  10  
  11  namespace Joomla\Component\Privacy\Administrator\Plugin;
  12  
  13  use Joomla\CMS\Plugin\CMSPlugin;
  14  use Joomla\CMS\Table\Table;
  15  use Joomla\Component\Fields\Administrator\Helper\FieldsHelper;
  16  use Joomla\Component\Privacy\Administrator\Export\Domain;
  17  use Joomla\Component\Privacy\Administrator\Export\Field;
  18  use Joomla\Component\Privacy\Administrator\Export\Item;
  19  
  20  // phpcs:disable PSR1.Files.SideEffects
  21  \defined('_JEXEC') or die;
  22  // phpcs:enable PSR1.Files.SideEffects
  23  
  24  /**
  25   * Base class for privacy plugins
  26   *
  27   * @since  3.9.0
  28   */
  29  abstract class PrivacyPlugin extends CMSPlugin
  30  {
  31      /**
  32       * Database object
  33       *
  34       * @var    \Joomla\Database\DatabaseDriver
  35       * @since  3.9.0
  36       */
  37      protected $db;
  38  
  39      /**
  40       * Affects constructor behaviour. If true, language files will be loaded automatically.
  41       *
  42       * @var    boolean
  43       * @since  3.9.0
  44       */
  45      protected $autoloadLanguage = true;
  46  
  47      /**
  48       * Create a new domain object
  49       *
  50       * @param   string  $name         The domain's name
  51       * @param   string  $description  The domain's description
  52       *
  53       * @return  Domain
  54       *
  55       * @since   3.9.0
  56       */
  57      protected function createDomain($name, $description = '')
  58      {
  59          $domain              = new Domain();
  60          $domain->name        = $name;
  61          $domain->description = $description;
  62  
  63          return $domain;
  64      }
  65  
  66      /**
  67       * Create an item object for an array
  68       *
  69       * @param   array         $data    The array data to convert
  70       * @param   integer|null  $itemId  The ID of this item
  71       *
  72       * @return  Item
  73       *
  74       * @since   3.9.0
  75       */
  76      protected function createItemFromArray(array $data, $itemId = null)
  77      {
  78          $item = new Item();
  79          $item->id = $itemId;
  80  
  81          foreach ($data as $key => $value) {
  82              if (is_object($value)) {
  83                  $value = (array) $value;
  84              }
  85  
  86              if (is_array($value)) {
  87                  $value = print_r($value, true);
  88              }
  89  
  90              $field        = new Field();
  91              $field->name  = $key;
  92              $field->value = $value;
  93  
  94              $item->addField($field);
  95          }
  96  
  97          return $item;
  98      }
  99  
 100      /**
 101       * Create an item object for a Table object
 102       *
 103       * @param   Table  $table  The Table object to convert
 104       *
 105       * @return  Item
 106       *
 107       * @since   3.9.0
 108       */
 109      protected function createItemForTable($table)
 110      {
 111          $data = [];
 112  
 113          foreach (array_keys($table->getFields()) as $fieldName) {
 114              $data[$fieldName] = $table->$fieldName;
 115          }
 116  
 117          return $this->createItemFromArray($data, $table->{$table->getKeyName(false)});
 118      }
 119  
 120      /**
 121       * Helper function to create the domain for the items custom fields.
 122       *
 123       * @param   string  $context  The context
 124       * @param   array   $items    The items
 125       *
 126       * @return  Domain
 127       *
 128       * @since   3.9.0
 129       */
 130      protected function createCustomFieldsDomain($context, $items = array())
 131      {
 132          if (!is_array($items)) {
 133              $items = [$items];
 134          }
 135  
 136          $parts = FieldsHelper::extract($context);
 137  
 138          if (!$parts) {
 139              return [];
 140          }
 141  
 142          $type = str_replace('com_', '', $parts[0]);
 143  
 144          $domain = $this->createDomain($type . '_' . $parts[1] . '_custom_fields', 'joomla_' . $type . '_' . $parts[1] . '_custom_fields_data');
 145  
 146          foreach ($items as $item) {
 147              // Get item's fields, also preparing their value property for manual display
 148              $fields = FieldsHelper::getFields($parts[0] . '.' . $parts[1], $item);
 149  
 150              foreach ($fields as $field) {
 151                  $fieldValue = is_array($field->value) ? implode(', ', $field->value) : $field->value;
 152  
 153                  $data = [
 154                      $type . '_id' => $item->id,
 155                      'field_name'  => $field->name,
 156                      'field_title' => $field->title,
 157                      'field_value' => $fieldValue,
 158                  ];
 159  
 160                  $domain->addItem($this->createItemFromArray($data));
 161              }
 162          }
 163  
 164          return $domain;
 165      }
 166  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer