[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/administrator/components/com_installer/src/Model/ -> UpdatesiteModel.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Administrator
   5   * @subpackage  com_installer
   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\Installer\Administrator\Model;
  12  
  13  use Exception;
  14  use Joomla\CMS\Form\Form;
  15  use Joomla\CMS\MVC\Model\AdminModel;
  16  use Joomla\CMS\Object\CMSObject;
  17  use Joomla\Component\Installer\Administrator\Helper\InstallerHelper;
  18  use Joomla\Database\ParameterType;
  19  
  20  // phpcs:disable PSR1.Files.SideEffects
  21  \defined('_JEXEC') or die;
  22  // phpcs:enable PSR1.Files.SideEffects
  23  
  24  /**
  25   * Item Model for an update site.
  26   *
  27   * @since  4.0.0
  28   */
  29  class UpdatesiteModel extends AdminModel
  30  {
  31      /**
  32       * The type alias for this content type.
  33       *
  34       * @var    string
  35       * @since  4.0.0
  36       */
  37      public $typeAlias = 'com_installer.updatesite';
  38  
  39      /**
  40       * Method to get the row form.
  41       *
  42       * @param   array    $data      Data for the form.
  43       * @param   boolean  $loadData  True if the form is to load its own data (default case), false if not.
  44       *
  45       * @return  Form|boolean  A Form object on success, false on failure
  46       *
  47       * @throws  Exception
  48       *
  49       * @since   4.0.0
  50       */
  51      public function getForm($data = [], $loadData = true)
  52      {
  53          // Get the form.
  54          $form = $this->loadForm('com_installer.updatesite', 'updatesite', ['control' => 'jform', 'load_data' => $loadData]);
  55  
  56          if (empty($form)) {
  57              return false;
  58          }
  59  
  60          return $form;
  61      }
  62      /**
  63       * Method to get the data that should be injected in the form.
  64       *
  65       * @return  mixed  The data for the form.
  66       *
  67       * @since   4.0.0
  68       */
  69      protected function loadFormData()
  70      {
  71          $data = $this->getItem();
  72          $this->preprocessData('com_installer.updatesite', $data);
  73  
  74          return $data;
  75      }
  76  
  77      /**
  78       * Method to get a single record.
  79       *
  80       * @param   integer  $pk  The id of the primary key.
  81       *
  82       * @return  CMSObject|boolean  Object on success, false on failure.
  83       *
  84       * @since   4.0.0
  85       */
  86      public function getItem($pk = null)
  87      {
  88          $item = parent::getItem($pk);
  89  
  90          $db           = $this->getDatabase();
  91          $updateSiteId = (int) $item->get('update_site_id');
  92          $query        = $db->getQuery(true)
  93              ->select(
  94                  $db->quoteName(
  95                      [
  96                          'update_sites.extra_query',
  97                          'extensions.type',
  98                          'extensions.element',
  99                          'extensions.folder',
 100                          'extensions.client_id',
 101                          'extensions.checked_out'
 102                      ]
 103                  )
 104              )
 105              ->from($db->quoteName('#__update_sites', 'update_sites'))
 106              ->join(
 107                  'INNER',
 108                  $db->quoteName('#__update_sites_extensions', 'update_sites_extensions'),
 109                  $db->quoteName('update_sites_extensions.update_site_id') . ' = ' . $db->quoteName('update_sites.update_site_id')
 110              )
 111              ->join(
 112                  'INNER',
 113                  $db->quoteName('#__extensions', 'extensions'),
 114                  $db->quoteName('extensions.extension_id') . ' = ' . $db->quoteName('update_sites_extensions.extension_id')
 115              )
 116              ->where($db->quoteName('update_sites.update_site_id') . ' = :updatesiteid')
 117              ->bind(':updatesiteid', $updateSiteId, ParameterType::INTEGER);
 118  
 119          $db->setQuery($query);
 120          $extension = new CMSObject($db->loadAssoc());
 121  
 122          $downloadKey = InstallerHelper::getDownloadKey($extension);
 123  
 124          $item->set('extra_query', $downloadKey['value'] ?? '');
 125          $item->set('downloadIdPrefix', $downloadKey['prefix'] ?? '');
 126          $item->set('downloadIdSuffix', $downloadKey['suffix'] ?? '');
 127  
 128          return $item;
 129      }
 130  
 131      /**
 132       * Method to save the form data.
 133       *
 134       * @param   array  $data  The form data.
 135       *
 136       * @return  boolean  True on success, False on error.
 137       *
 138       * @since   4.0.0
 139       */
 140      public function save($data): bool
 141      {
 142          // Apply the extra_query. Always empty when saving a free extension's update site.
 143          if (isset($data['extra_query'])) {
 144              $data['extra_query'] = $data['downloadIdPrefix'] . $data['extra_query'] . $data['downloadIdSuffix'];
 145          }
 146  
 147          // Force Joomla to recheck for updates
 148          $data['last_check_timestamp'] = 0;
 149  
 150          $result = parent::save($data);
 151  
 152          if (!$result) {
 153              return $result;
 154          }
 155  
 156          // Delete update records forcing Joomla to fetch them again, applying the new extra_query.
 157          $db = $this->getDatabase();
 158          $query = $db->getQuery(true)
 159              ->delete($db->quoteName('#__updates'))
 160              ->where($db->quoteName('update_site_id') . ' = :updateSiteId');
 161          $query->bind(':updateSiteId', $data['update_site_id'], ParameterType::INTEGER);
 162  
 163          try {
 164              $db->setQuery($query)->execute();
 165          } catch (Exception $e) {
 166              // No problem if this fails for any reason.
 167          }
 168  
 169          return true;
 170      }
 171  }


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