0
Welcome Guest! Login
0 items Join Now

How to hook into save method of template backend?

  • How to hook into save method of template backend?

    Posted 6 years 7 months ago
    • I was wondering if there is an easy way to create overrides in the gantry backend similar to the features of the frontend?
      Actually I would like to hook into the save method without the need to write a whole extension plug-in.

      The motivation is to offer template users the Joomla update facility but for a restricted user group only.
      This can be achieved by filling the field "extra_query" in the database table "update_sites" for the given template.
      What I did so far:

      1.) added download ID in template backend (template-options.xml)
      <field name="downloadid" type="text" default=""
                     label="COMMON_DOWNLOADID_LABEL" description ="COMMON_DOWNLOADID_DESCRIPTION" class="text-long" />


      2.) created a proof of concept via a feature: <templatefolder>/features/downloadid.php
      /*
       * This code is called in frontend with every page load
       * which is consuming resources unnecessarily
       * Todo: find a way to make this code run on template backend on save only
       * possible solution: extention plug-in which hooks into gantry template save event
       */
      class GantryFeatureDownloadId extends GantryFeature 
      {
        var $_feature_name = 'downloadId';
        var $_extra_query = "";
      
        function isEnabled() {
      
          global $gantry;
      
      
          $dID = $gantry->get('downloadid');
          if (!empty($dID)){
            $this->_extra_query= "downloadid=$dID";
            return true;
          }
      
          return false;
        }
      
      
        function init() {
      
          global $gantry;
      
          parent::init();
      
          if (!empty($this->_extra_query)) {
            $this->SetUpdateSite($this->_extra_query,  $this->GetExtensionId());
          }
        }
      
      
        private function GetExtensionId()
        {
          global $gantry;
      
          $templateName= $gantry->_template->getName();
      
          $db    = JFactory::getDbo();
          $query = $db->getQuery(true)
              ->select('extension_id')
              ->from($db->qn('#__extensions'))
              ->where($db->qn('client_id') . ' = ' . $db->q(0))
              ->where($db->qn('type') . ' = ' . $db->q('template'))
              ->where($db->qn('name') . ' = ' . $db->q($templateName))
          ;
          $db->setQuery($query);
      
          return $db->loadResult();
        }
      
      
        private function SetUpdateSite($extra_query, $extensionId)
        {
      
          if(empty($extra_query) || empty($extensionId)) return; // do nothing
      
          $db    = JFactory::getDbo();
          $query = $db->getQuery(true)
              ->select('update_site_id')
              ->from($db->qn('#__update_sites_extensions'))
              ->where($db->qn('extension_id') . ' = ' . $db->q($extensionId));
          $db->setQuery($query);
          $updateSiteId = $db->loadResult();
      
          if ($updateSiteId) {
            // Update the update site record
            $query = $db->getQuery(true)
                ->update($db->qn('#__update_sites'))
                ->set(array(
                          $db->quoteName('extra_query') . ' = ' . $db->q($extra_query),
                          $db->quoteName('enabled') . ' = ' . 1,
                          $db->quoteName('last_check_timestamp') . ' = ' . 0,
                      ))
                ->where($db->qn('update_site_id') . ' = ' . $db->q($updateSiteId));
            $db->setQuery($query);
            $db->execute();
      
            // Delete any existing updates (essentially flushes the updates cache for this update site)
            $query = $db->getQuery(true)
                ->delete($db->qn('#__updates'))
                ->where($db->qn('update_site_id') . ' = ' . $db->q($updateSiteId));
            $db->setQuery($query);
            $db->execute();
          }
        }
      }
    • Last Edit: 6 years 7 months ago by Christoph Hagedorn.
    • Damir's Avatar
    • Damir
    • Preeminent Rocketeer
    • Posts: 22450
    • Thanks: 2679
    • Web Developer

    Re: How to hook into save method of template backend?

    Posted 6 years 7 months ago

Time to create page: 0.049 seconds