[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/api/components/com_media/src/Model/ -> MediaModel.php (source)

   1  <?php
   2  /**
   3   * @package     Joomla.API
   4   * @subpackage  com_media
   5   *
   6   * @copyright   (C) 2021 Open Source Matters, Inc. <https://www.joomla.org>
   7   * @license     GNU General Public License version 2 or later; see LICENSE.txt
   8   */
   9  
  10  namespace Joomla\Component\Media\Api\Model;
  11  
  12  \defined('_JEXEC') or die;
  13  
  14  use Joomla\CMS\Language\Text;
  15  use Joomla\CMS\MVC\Controller\Exception\ResourceNotFound;
  16  use Joomla\CMS\MVC\Model\BaseModel;
  17  use Joomla\CMS\MVC\Model\ListModelInterface;
  18  use Joomla\CMS\Pagination\Pagination;
  19  use Joomla\Component\Media\Administrator\Exception\FileNotFoundException;
  20  use Joomla\Component\Media\Administrator\Model\ApiModel;
  21  use Joomla\Component\Media\Administrator\Provider\ProviderManagerHelperTrait;
  22  
  23  /**
  24   * Media web service model supporting lists of media items.
  25   *
  26   * @since  4.1.0
  27   */
  28  class MediaModel extends BaseModel implements ListModelInterface
  29  {
  30      use ProviderManagerHelperTrait;
  31  
  32      /**
  33       * Instance of com_media's ApiModel
  34       *
  35       * @var ApiModel
  36       * @since  4.1.0
  37       */
  38      private $mediaApiModel;
  39  
  40      /**
  41       * A hacky way to enable the standard jsonapiView::displayList() to create a Pagination object,
  42       * since com_media's ApiModel does not support pagination as we know from regular ListModel derived models.
  43       *
  44       * @var int
  45       * @since  4.1.0
  46       */
  47      private $total = 0;
  48  
  49  	public function __construct($config = [])
  50      {
  51          parent::__construct($config);
  52  
  53          $this->mediaApiModel = new ApiModel();
  54      }
  55  
  56      /**
  57       * Method to get a list of files and/or folders.
  58       *
  59       * @return  array  An array of data items.
  60       *
  61       * @since   4.1.0
  62       */
  63  	public function getItems(): array
  64      {
  65          // Map web service model state to com_media options.
  66          $options = [
  67              'url'       => $this->getState('url', false),
  68              'temp'      => $this->getState('temp', false),
  69              'search'    => $this->getState('search', ''),
  70              'recursive' => $this->getState('search_recursive', false),
  71              'content'   => $this->getState('content', false),
  72          ];
  73  
  74          ['adapter' => $adapterName, 'path' => $path] = $this->resolveAdapterAndPath($this->getState('path', ''));
  75          try
  76          {
  77              $files = $this->mediaApiModel->getFiles($adapterName, $path, $options);
  78          }
  79          catch (FileNotFoundException $e)
  80          {
  81              throw new ResourceNotFound(
  82                  Text::sprintf('WEBSERVICE_COM_MEDIA_FILE_NOT_FOUND', $path),
  83                  404
  84              );
  85          }
  86  
  87          /**
  88           * A hacky way to enable the standard jsonapiView::displayList() to create a Pagination object.
  89           * Because com_media's ApiModel does not support pagination as we know from regular ListModel
  90           * derived models, we always return all retrieved items.
  91           */
  92          $this->total = \count($files);
  93  
  94          return $files;
  95      }
  96  
  97      /**
  98       * Method to get a \JPagination object for the data set.
  99       *
 100       * @return  Pagination  A Pagination object for the data set.
 101       *
 102       * @since   4.1.0
 103       */
 104  	public function getPagination(): Pagination
 105      {
 106          return new Pagination($this->getTotal(), $this->getStart(), 0);
 107      }
 108  
 109      /**
 110       * Method to get the starting number of items for the data set. Because com_media's ApiModel
 111       * does not support pagination as we know from regular ListModel derived models,
 112       * we always start at the top.
 113       *
 114       * @return  int  The starting number of items available in the data set.
 115       *
 116       * @since   4.1.0
 117       */
 118  	public function getStart(): int
 119      {
 120          return 0;
 121      }
 122  
 123      /**
 124       * Method to get the total number of items for the data set.
 125       *
 126       * @return  int  The total number of items available in the data set.
 127       *
 128       * @since   4.1.0
 129       */
 130  	public function getTotal(): int
 131      {
 132          return $this->total;
 133      }
 134  }


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