[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Workflow/ -> WorkflowServiceTrait.php (source)

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2018 Open Source Matters, Inc. <https://www.joomla.org>
   7   * @license    GNU General Public License version 2 or later; see LICENSE
   8   */
   9  
  10  namespace Joomla\CMS\Workflow;
  11  
  12  use Joomla\CMS\Component\ComponentHelper;
  13  use Joomla\CMS\Event\AbstractEvent;
  14  use Joomla\CMS\Factory;
  15  use Joomla\CMS\MVC\Factory\MVCFactoryInterface;
  16  use Joomla\CMS\MVC\Model\WorkflowModelInterface;
  17  use Joomla\Event\DispatcherAwareInterface;
  18  
  19  // phpcs:disable PSR1.Files.SideEffects
  20  \defined('JPATH_PLATFORM') or die;
  21  // phpcs:enable PSR1.Files.SideEffects
  22  
  23  /**
  24   * Trait for component workflow service.
  25   *
  26   * @since  4.0.0
  27   */
  28  trait WorkflowServiceTrait
  29  {
  30      /**
  31       * Get a MVC factory
  32       *
  33       * @return  MVCFactoryInterface
  34       *
  35       * @since   4.0.0
  36       */
  37      abstract public function getMVCFactory(): MVCFactoryInterface;
  38  
  39      /**
  40       * Check if the functionality is supported by the component
  41       * The variable $supportFunctionality has the following structure
  42       * [
  43       *   'core.featured' => [
  44       *     'com_content.article',
  45       *   ],
  46       *   'core.state' => [
  47       *     'com_content.article',
  48       *   ],
  49       * ]
  50       *
  51       * @param   string  $functionality  The functionality
  52       * @param   string  $context        The context of the functionality
  53       *
  54       * @return boolean
  55       */
  56      public function supportFunctionality($functionality, $context): bool
  57      {
  58          if (empty($this->supportedFunctionality[$functionality])) {
  59              return false;
  60          }
  61  
  62          if (!is_array($this->supportedFunctionality[$functionality])) {
  63              return true;
  64          }
  65  
  66          return in_array($context, $this->supportedFunctionality[$functionality], true);
  67      }
  68  
  69      /**
  70       * Check if the functionality is used by a plugin
  71       *
  72       * @param   string  $functionality  The functionality
  73       * @param   string  $extension      The extension
  74       *
  75       * @return boolean
  76       * @throws \Exception
  77       *
  78       * @since   4.0.0
  79       */
  80      public function isFunctionalityUsed($functionality, $extension): bool
  81      {
  82          static $used = [];
  83  
  84          $cacheKey = $extension . '.' . $functionality;
  85  
  86          if (isset($used[$cacheKey])) {
  87              return $used[$cacheKey];
  88          }
  89  
  90          // The container to get the services from
  91          $app = Factory::getApplication();
  92  
  93          if (!($app instanceof DispatcherAwareInterface)) {
  94              return false;
  95          }
  96  
  97          $eventResult = $app->getDispatcher()->dispatch(
  98              'onWorkflowFunctionalityUsed',
  99              AbstractEvent::create(
 100                  'onWorkflowFunctionalityUsed',
 101                  [
 102                      'eventClass'    => 'Joomla\CMS\Event\Workflow\WorkflowFunctionalityUsedEvent',
 103                      'subject'       => $this,
 104                      'extension'     => $extension,
 105                      'functionality' => $functionality
 106                  ]
 107              )
 108          );
 109  
 110          $used[$cacheKey] = $eventResult->getArgument('used', false);
 111  
 112          return $used[$cacheKey];
 113      }
 114  
 115      /**
 116       * Returns the model name, based on the context
 117       *
 118       * @param   string  $context  The context of the workflow
 119       *
 120       * @return boolean
 121       *
 122       * @since   4.0.0
 123       */
 124      public function getModelName($context): string
 125      {
 126          $parts = explode('.', $context);
 127  
 128          if (count($parts) < 2) {
 129              return '';
 130          }
 131  
 132          array_shift($parts);
 133  
 134          return ucfirst(array_shift($parts));
 135      }
 136  
 137      /**
 138       * Returns an array of possible conditions for the component.
 139       *
 140       * @param   string  $extension  The component and section separated by ".".
 141       *
 142       * @return  array
 143       *
 144       * @since   4.0.0
 145       */
 146      public static function getConditions(string $extension): array
 147      {
 148          return \defined('self::CONDITION_NAMES') ? self::CONDITION_NAMES : Workflow::CONDITION_NAMES;
 149      }
 150  
 151      /**
 152       * Check if the workflow is active
 153       *
 154       * @param   string  $context  The context of the workflow
 155       *
 156       * @return boolean
 157       */
 158      public function isWorkflowActive($context): bool
 159      {
 160          $parts  = explode('.', $context);
 161          $config = ComponentHelper::getParams($parts[0]);
 162  
 163          if (!$config->get('workflow_enabled')) {
 164              return false;
 165          }
 166  
 167          $modelName = $this->getModelName($context);
 168  
 169          if (empty($modelName)) {
 170              return false;
 171          }
 172  
 173          $component = $this->getMVCFactory();
 174          $appName   = Factory::getApplication()->getName();
 175          $model     = $component->createModel($modelName, $appName, ['ignore_request' => true]);
 176  
 177          return $model instanceof WorkflowModelInterface;
 178      }
 179  }


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