[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/api/components/com_contact/src/View/Contacts/ -> JsonapiView.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.API
   5   * @subpackage  com_contact
   6   *
   7   * @copyright   (C) 2019 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\Contact\Api\View\Contacts;
  12  
  13  use Joomla\CMS\Language\Multilanguage;
  14  use Joomla\CMS\MVC\View\JsonApiView as BaseApiView;
  15  use Joomla\Component\Contact\Api\Serializer\ContactSerializer;
  16  use Joomla\Component\Content\Api\Helper\ContentHelper;
  17  use Joomla\Component\Fields\Administrator\Helper\FieldsHelper;
  18  
  19  // phpcs:disable PSR1.Files.SideEffects
  20  \defined('_JEXEC') or die;
  21  // phpcs:enable PSR1.Files.SideEffects
  22  
  23  /**
  24   * The contacts view
  25   *
  26   * @since  4.0.0
  27   */
  28  class JsonapiView extends BaseApiView
  29  {
  30      /**
  31       * The fields to render item in the documents
  32       *
  33       * @var  array
  34       * @since  4.0.0
  35       */
  36      protected $fieldsToRenderItem = [
  37          'id',
  38          'alias',
  39          'name',
  40          'category',
  41          'created',
  42          'created_by',
  43          'created_by_alias',
  44          'modified',
  45          'modified_by',
  46          'image',
  47          'tags',
  48          'featured',
  49          'publish_up',
  50          'publish_down',
  51          'version',
  52          'hits',
  53          'metakey',
  54          'metadesc',
  55          'metadata',
  56          'con_position',
  57          'address',
  58          'suburb',
  59          'state',
  60          'country',
  61          'postcode',
  62          'telephone',
  63          'fax',
  64          'misc',
  65          'email_to',
  66          'default_con',
  67          'user_id',
  68          'access',
  69          'mobile',
  70          'webpage',
  71          'sortname1',
  72          'sortname2',
  73          'sortname3',
  74      ];
  75  
  76      /**
  77       * The fields to render items in the documents
  78       *
  79       * @var  array
  80       * @since  4.0.0
  81       */
  82      protected $fieldsToRenderList = [
  83          'id',
  84          'alias',
  85          'name',
  86          'category',
  87          'created',
  88          'created_by',
  89          'created_by_alias',
  90          'modified',
  91          'modified_by',
  92          'image',
  93          'tags',
  94          'user_id',
  95      ];
  96  
  97      /**
  98       * The relationships the item has
  99       *
 100       * @var    array
 101       * @since  4.0.0
 102       */
 103      protected $relationship = [
 104          'category',
 105          'created_by',
 106          'modified_by',
 107          'user_id',
 108          'tags',
 109      ];
 110  
 111      /**
 112       * Constructor.
 113       *
 114       * @param   array  $config  A named configuration array for object construction.
 115       *                          contentType: the name (optional) of the content type to use for the serialization
 116       *
 117       * @since   4.0.0
 118       */
 119      public function __construct($config = [])
 120      {
 121          if (\array_key_exists('contentType', $config)) {
 122              $this->serializer = new ContactSerializer($config['contentType']);
 123          }
 124  
 125          parent::__construct($config);
 126      }
 127  
 128      /**
 129       * Execute and display a template script.
 130       *
 131       * @param   array|null  $items  Array of items
 132       *
 133       * @return  string
 134       *
 135       * @since   4.0.0
 136       */
 137      public function displayList(array $items = null)
 138      {
 139          foreach (FieldsHelper::getFields('com_contact.contact') as $field) {
 140              $this->fieldsToRenderList[] = $field->name;
 141          }
 142  
 143          return parent::displayList();
 144      }
 145  
 146      /**
 147       * Execute and display a template script.
 148       *
 149       * @param   object  $item  Item
 150       *
 151       * @return  string
 152       *
 153       * @since   4.0.0
 154       */
 155      public function displayItem($item = null)
 156      {
 157          foreach (FieldsHelper::getFields('com_contact.contact') as $field) {
 158              $this->fieldsToRenderItem[] = $field->name;
 159          }
 160  
 161          if (Multilanguage::isEnabled()) {
 162              $this->fieldsToRenderItem[] = 'languageAssociations';
 163              $this->relationship[]       = 'languageAssociations';
 164          }
 165  
 166          return parent::displayItem();
 167      }
 168  
 169      /**
 170       * Prepare item before render.
 171       *
 172       * @param   object  $item  The model item
 173       *
 174       * @return  object
 175       *
 176       * @since   4.0.0
 177       */
 178      protected function prepareItem($item)
 179      {
 180          foreach (FieldsHelper::getFields('com_contact.contact', $item, true) as $field) {
 181              $item->{$field->name} = $field->apivalue ?? $field->rawvalue;
 182          }
 183  
 184          if (Multilanguage::isEnabled() && !empty($item->associations)) {
 185              $associations = [];
 186  
 187              foreach ($item->associations as $language => $association) {
 188                  $itemId = explode(':', $association)[0];
 189  
 190                  $associations[] = (object) [
 191                      'id'       => $itemId,
 192                      'language' => $language,
 193                  ];
 194              }
 195  
 196              $item->associations = $associations;
 197          }
 198  
 199          if (!empty($item->tags->tags)) {
 200              $tagsIds   = explode(',', $item->tags->tags);
 201              $tagsNames = $item->tagsHelper->getTagNames($tagsIds);
 202  
 203              $item->tags = array_combine($tagsIds, $tagsNames);
 204          } else {
 205              $item->tags = [];
 206          }
 207  
 208          if (isset($item->image)) {
 209              $item->image = ContentHelper::resolve($item->image);
 210          }
 211  
 212          return parent::prepareItem($item);
 213      }
 214  }


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