[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/MVC/View/ -> FormView.php (source)

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2016 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\CMS\MVC\View;
  11  
  12  use Joomla\CMS\Component\ComponentHelper;
  13  use Joomla\CMS\Factory;
  14  use Joomla\CMS\Language\Text;
  15  use Joomla\CMS\Object\CMSObject;
  16  use Joomla\CMS\Toolbar\ToolbarHelper;
  17  
  18  // phpcs:disable PSR1.Files.SideEffects
  19  \defined('JPATH_PLATFORM') or die;
  20  // phpcs:enable PSR1.Files.SideEffects
  21  
  22  /**
  23   * Base class for a Joomla Form View
  24   *
  25   * Class holding methods for displaying presentation data.
  26   *
  27   * @since  2.5.5
  28   */
  29  class FormView extends HtmlView
  30  {
  31      /**
  32       * The \JForm object
  33       *
  34       * @var  \Joomla\CMS\Form\Form
  35       */
  36      protected $form;
  37  
  38      /**
  39       * The active item
  40       *
  41       * @var  object
  42       */
  43      protected $item;
  44  
  45      /**
  46       * The model state
  47       *
  48       * @var  object
  49       */
  50      protected $state;
  51  
  52      /**
  53       * The actions the user is authorised to perform
  54       *
  55       * @var  CMSObject
  56       */
  57      protected $canDo;
  58  
  59      /**
  60       * The toolbar title
  61       *
  62       * @var string
  63       */
  64      protected $toolbarTitle;
  65  
  66      /**
  67       * The toolbar icon
  68       *
  69       * @var string
  70       */
  71      protected $toolbarIcon;
  72  
  73      /**
  74       * The preview link
  75       *
  76       * @var string
  77       */
  78      protected $previewLink;
  79  
  80      /**
  81       * The help link
  82       *
  83       * @var string
  84       */
  85      protected $helpLink;
  86  
  87      /**
  88       * Constructor
  89       *
  90       * @param   array  $config  An optional associative array of configuration settings.
  91       */
  92      public function __construct(array $config)
  93      {
  94          parent::__construct($config);
  95  
  96          if (isset($config['help_link'])) {
  97              $this->helpLink = $config['help_link'];
  98          }
  99  
 100          if (isset($config['toolbar_icon'])) {
 101              $this->toolbarIcon = $config['toolbar_icon'];
 102          } else {
 103              $this->toolbarIcon = 'pencil-2 ' . $this->getName() . '-add';
 104          }
 105  
 106          // Set default value for $canDo to avoid fatal error if child class doesn't set value for this property
 107          $this->canDo = new CMSObject();
 108      }
 109  
 110      /**
 111       * Execute and display a template script.
 112       *
 113       * @param   string  $tpl  The name of the template file to parse; automatically searches through the template paths.
 114       *
 115       * @return  void
 116       *
 117       * @throws  \Exception
 118       */
 119      public function display($tpl = null)
 120      {
 121          // Prepare view data
 122          $this->initializeView();
 123  
 124          // Check for errors.
 125          if (\count($errors = $this->get('Errors'))) {
 126              throw new GenericDataException(implode("\n", $errors), 500);
 127          }
 128  
 129          // Build toolbar
 130          $this->addToolbar();
 131  
 132          parent::display($tpl);
 133      }
 134  
 135      /**
 136       * Prepare view data
 137       *
 138       * @return  void
 139       */
 140      protected function initializeView()
 141      {
 142          $this->form  = $this->get('Form');
 143          $this->item  = $this->get('Item');
 144          $this->state = $this->get('State');
 145  
 146          // Set default toolbar title
 147          if ($this->item->id) {
 148              $this->toolbarTitle = Text::_(strtoupper($this->option . '_MANAGER_' . $this->getName() . '_EDIT'));
 149          } else {
 150              $this->toolbarTitle = Text::_(strtoupper($this->option . '_MANAGER_' . $this->getName() . '_NEW'));
 151          }
 152      }
 153  
 154      /**
 155       * Add the page title and toolbar.
 156       *
 157       * @return  void
 158       *
 159       * @since   1.6
 160       */
 161      protected function addToolbar()
 162      {
 163          Factory::getApplication()->input->set('hidemainmenu', true);
 164  
 165          $user       = Factory::getUser();
 166          $userId     = $user->id;
 167          $isNew      = ($this->item->id == 0);
 168          $viewName   = $this->getName();
 169          $checkedOut = $this->getModel()->isCheckedOut($this->item);
 170          $canDo      = $this->canDo;
 171  
 172          ToolbarHelper::title(
 173              $this->toolbarTitle,
 174              $this->toolbarIcon
 175          );
 176  
 177          // For new records, check the create permission.
 178          if ($isNew && $canDo->get('core.create')) {
 179              ToolbarHelper::saveGroup(
 180                  [
 181                      ['apply', $viewName . '.apply'],
 182                      ['save', $viewName . '.save'],
 183                      ['save2new', $viewName . '.save2new']
 184                  ],
 185                  'btn-success'
 186              );
 187  
 188              ToolbarHelper::cancel($viewName . '.cancel');
 189          } else {
 190              // Since it's an existing record, check the edit permission, or fall back to edit own if the owner.
 191              if (property_exists($this->item, 'created_by')) {
 192                  $itemEditable = $canDo->get('core.edit') || ($canDo->get('core.edit.own') && $this->item->created_by == $userId);
 193              } else {
 194                  $itemEditable = $canDo->get('core.edit');
 195              }
 196  
 197              $toolbarButtons = [];
 198  
 199              // Can't save the record if it's checked out and editable
 200              if (!$checkedOut && $itemEditable) {
 201                  $toolbarButtons[] = ['apply', $viewName . '.apply'];
 202                  $toolbarButtons[] = ['save', $viewName . '.save'];
 203  
 204                  // We can save this record, but check the create permission to see if we can return to make a new one.
 205                  if ($canDo->get('core.create')) {
 206                      $toolbarButtons[] = ['save2new', $viewName . '.save2new'];
 207                  }
 208              }
 209  
 210              // If checked out, we can still save
 211              if ($canDo->get('core.create')) {
 212                  $toolbarButtons[] = ['save2copy', $viewName . '.save2copy'];
 213              }
 214  
 215              ToolbarHelper::saveGroup(
 216                  $toolbarButtons,
 217                  'btn-success'
 218              );
 219  
 220              if (ComponentHelper::isEnabled('com_contenthistory') && $this->state->params->get('save_history', 0) && $itemEditable) {
 221                  ToolbarHelper::versions($this->option . '.' . $viewName, $this->item->id);
 222              }
 223  
 224              if (!$isNew && $this->previewLink) {
 225                  ToolbarHelper::preview($this->previewLink, Text::_('JGLOBAL_PREVIEW'), 'eye', 80, 90);
 226              }
 227  
 228              ToolbarHelper::cancel($viewName . '.cancel', 'JTOOLBAR_CLOSE');
 229          }
 230  
 231          ToolbarHelper::divider();
 232  
 233          if ($this->helpLink) {
 234              ToolbarHelper::help($this->helpLink);
 235          }
 236      }
 237  }


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