[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/components/com_contact/src/Model/ -> FormModel.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Site
   5   * @subpackage  com_contact
   6   *
   7   * @copyright   (C) 2020 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\Site\Model;
  12  
  13  use Exception;
  14  use Joomla\CMS\Factory;
  15  use Joomla\CMS\Form\Form;
  16  use Joomla\CMS\Helper\TagsHelper;
  17  use Joomla\CMS\Language\Associations;
  18  use Joomla\CMS\Language\Multilanguage;
  19  use Joomla\CMS\Table\Table;
  20  use Joomla\Registry\Registry;
  21  use Joomla\Utilities\ArrayHelper;
  22  
  23  // phpcs:disable PSR1.Files.SideEffects
  24  \defined('_JEXEC') or die;
  25  // phpcs:enable PSR1.Files.SideEffects
  26  
  27  /**
  28   * Contact Component Contact Model
  29   *
  30   * @since  4.0.0
  31   */
  32  class FormModel extends \Joomla\Component\Contact\Administrator\Model\ContactModel
  33  {
  34      /**
  35       * Model typeAlias string. Used for version history.
  36       *
  37       * @var    string
  38       *
  39       * @since  4.0.0
  40       */
  41      public $typeAlias = 'com_contact.contact';
  42  
  43      /**
  44       * Name of the form
  45       *
  46       * @var    string
  47       *
  48       * @since  4.0.0
  49       */
  50      protected $formName = 'form';
  51  
  52      /**
  53       * Method to get the row form.
  54       *
  55       * @param   array    $data      Data for the form.
  56       * @param   boolean  $loadData  True if the form is to load its own data (default case), false if not.
  57       *
  58       * @return  Form|boolean  A Form object on success, false on failure
  59       *
  60       * @since   4.0.0
  61       */
  62      public function getForm($data = array(), $loadData = true)
  63      {
  64          $form = parent::getForm($data, $loadData);
  65  
  66          // Prevent messing with article language and category when editing existing contact with associations
  67          if ($id = $this->getState('contact.id') && Associations::isEnabled()) {
  68              $associations = Associations::getAssociations('com_contact', '#__contact_details', 'com_contact.item', $id);
  69  
  70              // Make fields read only
  71              if (!empty($associations)) {
  72                  $form->setFieldAttribute('language', 'readonly', 'true');
  73                  $form->setFieldAttribute('language', 'filter', 'unset');
  74              }
  75          }
  76  
  77          return $form;
  78      }
  79  
  80      /**
  81       * Method to get contact data.
  82       *
  83       * @param   integer  $itemId  The id of the contact.
  84       *
  85       * @return  mixed  Contact item data object on success, false on failure.
  86       *
  87       * @throws  Exception
  88       *
  89       * @since   4.0.0
  90       */
  91      public function getItem($itemId = null)
  92      {
  93          $itemId = (int) (!empty($itemId)) ? $itemId : $this->getState('contact.id');
  94  
  95          // Get a row instance.
  96          $table = $this->getTable();
  97  
  98          // Attempt to load the row.
  99          try {
 100              if (!$table->load($itemId)) {
 101                  return false;
 102              }
 103          } catch (Exception $e) {
 104              Factory::getApplication()->enqueueMessage($e->getMessage());
 105  
 106              return false;
 107          }
 108  
 109          $properties = $table->getProperties();
 110          $value      = ArrayHelper::toObject($properties, \Joomla\CMS\Object\CMSObject::class);
 111  
 112          // Convert field to Registry.
 113          $value->params = new Registry($value->params);
 114  
 115          // Convert the metadata field to an array.
 116          $registry        = new Registry($value->metadata);
 117          $value->metadata = $registry->toArray();
 118  
 119          if ($itemId) {
 120              $value->tags = new TagsHelper();
 121              $value->tags->getTagIds($value->id, 'com_contact.contact');
 122              $value->metadata['tags'] = $value->tags;
 123          }
 124  
 125          return $value;
 126      }
 127  
 128      /**
 129       * Get the return URL.
 130       *
 131       * @return  string  The return URL.
 132       *
 133       * @since   4.0.0
 134       */
 135      public function getReturnPage()
 136      {
 137          return base64_encode($this->getState('return_page', ''));
 138      }
 139  
 140      /**
 141       * Method to save the form data.
 142       *
 143       * @param   array  $data  The form data.
 144       *
 145       * @return  boolean  True on success.
 146       *
 147       * @since   4.0.0
 148       *
 149       * @throws  Exception
 150       */
 151      public function save($data)
 152      {
 153          // Associations are not edited in frontend ATM so we have to inherit them
 154          if (
 155              Associations::isEnabled() && !empty($data['id'])
 156              && $associations = Associations::getAssociations('com_contact', '#__contact_details', 'com_contact.item', $data['id'])
 157          ) {
 158              foreach ($associations as $tag => $associated) {
 159                  $associations[$tag] = (int) $associated->id;
 160              }
 161  
 162              $data['associations'] = $associations;
 163          }
 164  
 165          return parent::save($data);
 166      }
 167  
 168      /**
 169       * Method to auto-populate the model state.
 170       *
 171       * Note. Calling getState in this method will result in recursion.
 172       *
 173       * @return  void
 174       *
 175       * @since   4.0.0
 176       *
 177       * @throws  Exception
 178       */
 179      protected function populateState()
 180      {
 181          $app = Factory::getApplication();
 182  
 183          // Load state from the request.
 184          $pk = $app->input->getInt('id');
 185          $this->setState('contact.id', $pk);
 186  
 187          $this->setState('contact.catid', $app->input->getInt('catid'));
 188  
 189          $return = $app->input->get('return', '', 'base64');
 190          $this->setState('return_page', base64_decode($return));
 191  
 192          // Load the parameters.
 193          $params = $app->getParams();
 194          $this->setState('params', $params);
 195  
 196          $this->setState('layout', $app->input->getString('layout'));
 197      }
 198  
 199      /**
 200       * Allows preprocessing of the JForm object.
 201       *
 202       * @param   Form    $form   The form object
 203       * @param   array   $data   The data to be merged into the form object
 204       * @param   string  $group  The plugin group to be executed
 205       *
 206       * @return  void
 207       *
 208       * @since   4.0.0
 209       */
 210      protected function preprocessForm(Form $form, $data, $group = 'contact')
 211      {
 212          if (!Multilanguage::isEnabled()) {
 213              $form->setFieldAttribute('language', 'type', 'hidden');
 214              $form->setFieldAttribute('language', 'default', '*');
 215          }
 216  
 217          parent::preprocessForm($form, $data, $group);
 218      }
 219  
 220      /**
 221       * Method to get a table object, load it if necessary.
 222       *
 223       * @param   string  $name     The table name. Optional.
 224       * @param   string  $prefix   The class prefix. Optional.
 225       * @param   array   $options  Configuration array for model. Optional.
 226       *
 227       * @return  bool|Table  A Table object
 228       *
 229       * @since   4.0.0
 230  
 231       * @throws  Exception
 232       */
 233      public function getTable($name = 'Contact', $prefix = 'Administrator', $options = array())
 234      {
 235          return parent::getTable($name, $prefix, $options);
 236      }
 237  }


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