[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/api/components/com_languages/src/Controller/ -> StringsController.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.API
   5   * @subpackage  com_languages
   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\Languages\Api\Controller;
  12  
  13  use Joomla\CMS\Factory;
  14  use Joomla\CMS\Language\Text;
  15  use Joomla\CMS\MVC\Controller\ApiController;
  16  use Tobscure\JsonApi\Exception\InvalidParameterException;
  17  
  18  // phpcs:disable PSR1.Files.SideEffects
  19  \defined('_JEXEC') or die;
  20  // phpcs:enable PSR1.Files.SideEffects
  21  
  22  /**
  23   * The strings controller
  24   *
  25   * @since  4.0.0
  26   */
  27  class StringsController extends ApiController
  28  {
  29      /**
  30       * The content type of the item.
  31       *
  32       * @var    string
  33       * @since  4.0.0
  34       */
  35      protected $contentType = 'strings';
  36  
  37      /**
  38       * The default view for the display method.
  39       *
  40       * @var    string
  41       * @since  3.0
  42       */
  43      protected $default_view = 'strings';
  44  
  45      /**
  46       * Search by languages constants
  47       *
  48       * @return  static  A \JControllerLegacy object to support chaining.
  49       *
  50       * @throws  InvalidParameterException
  51       * @since   4.0.0
  52       */
  53      public function search()
  54      {
  55          $data = $this->input->get('data', json_decode($this->input->json->getRaw(), true), 'array');
  56  
  57          if (!isset($data['searchstring']) || !\is_string($data['searchstring'])) {
  58              throw new InvalidParameterException("Invalid param 'searchstring'");
  59          }
  60  
  61          if (!isset($data['searchtype']) || !\in_array($data['searchtype'], ['constant', 'value'])) {
  62              throw new InvalidParameterException("Invalid param 'searchtype'");
  63          }
  64  
  65          $app = Factory::getApplication();
  66          $app->input->set('searchstring', $data['searchstring']);
  67          $app->input->set('searchtype', $data['searchtype']);
  68          $app->input->set('more', 0);
  69  
  70          $viewType   = $this->app->getDocument()->getType();
  71          $viewName   = $this->input->get('view', $this->default_view);
  72          $viewLayout = $this->input->get('layout', 'default', 'string');
  73  
  74          try {
  75              /** @var \Joomla\Component\Languages\Api\View\Strings\JsonapiView $view */
  76              $view = $this->getView(
  77                  $viewName,
  78                  $viewType,
  79                  '',
  80                  ['base_path' => $this->basePath, 'layout' => $viewLayout, 'contentType' => $this->contentType]
  81              );
  82          } catch (\Exception $e) {
  83              throw new \RuntimeException($e->getMessage());
  84          }
  85  
  86          /** @var \Joomla\Component\Languages\Administrator\Model\StringsModel $model */
  87          $model = $this->getModel($this->contentType, '', ['ignore_request' => true]);
  88  
  89          if (!$model) {
  90              throw new \RuntimeException(Text::_('JLIB_APPLICATION_ERROR_MODEL_CREATE'));
  91          }
  92  
  93          // Push the model into the view (as default)
  94          $view->setModel($model, true);
  95  
  96          $view->document = $this->app->getDocument();
  97          $view->displayList();
  98  
  99          return $this;
 100      }
 101  
 102      /**
 103       * Refresh cache
 104       *
 105       * @return  static  A \JControllerLegacy object to support chaining.
 106       *
 107       * @throws \Exception
 108       * @since   4.0.0
 109       */
 110      public function refresh()
 111      {
 112          /** @var \Joomla\Component\Languages\Administrator\Model\StringsModel $model */
 113          $model = $this->getModel($this->contentType, '', ['ignore_request' => true]);
 114  
 115          if (!$model) {
 116              throw new \RuntimeException(Text::_('JLIB_APPLICATION_ERROR_MODEL_CREATE'));
 117          }
 118  
 119          $result = $model->refresh();
 120  
 121          if ($result instanceof \Exception) {
 122              throw $result;
 123          }
 124  
 125          return $this;
 126      }
 127  }


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