Joomla! PHP Cross Reference Web Portals

Source: /plugins/system/languagecode/languagecode.php - 144 lines - 3886 bytes - Summary - Text - Print

   1  <?php
   2  /**
   3   * @package     Joomla.Plugin
   4   * @subpackage  System.languagecode
   5   *
   6   * @copyright   Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
   7   * @license     GNU General Public License version 2 or later; see LICENSE.txt
   8   */
   9  
  10  defined('_JEXEC') or die;
  11  
  12  /**
  13   * Language Code plugin class.
  14   *
  15   * @package     Joomla.Plugin
  16   * @subpackage  Content.languagecode
  17   * @since       2.5
  18   */
  19  class plgSystemLanguagecode extends JPlugin
  20  {
  21      /**
  22       * Plugin that change the language code used in the <html /> tag
  23       *
  24       * @since  2.5
  25       */
  26  	public function onAfterRender()
  27      {
  28          // Use this plugin only in site application
  29          if (JFactory::getApplication()->isSite())
  30          {
  31              // Get the response body
  32              $body = JResponse::getBody();
  33  
  34              // Get the current language code
  35              $code = JFactory::getDocument()->getLanguage();
  36  
  37              // Get the new code
  38              $new_code  = $this->params->get($code);
  39  
  40              // Replace the old code by the new code in the <html /> tag
  41              if ($new_code)
  42              {
  43                  // Replace the new code in the HTML document
  44                  $patterns = array(
  45                      chr(1) . '(<html.*\s+xml:lang=")(' . $code . ')(".*>)' . chr(1) . 'i',
  46                      chr(1) . '(<html.*\s+lang=")(' . $code . ')(".*>)' . chr(1) . 'i',
  47                  );
  48                  $replace = array(
  49                      '$1}' . strtolower($new_code) . '$3}',
  50                      '$1}' . strtolower($new_code) . '$3}'
  51                  );
  52              }
  53              else
  54              {
  55                  $patterns = array();
  56                  $replace = array();
  57              }
  58  
  59              // Replace codes in <link hreflang="" /> attributes
  60              preg_match_all(chr(1) . '(<link.*\s+hreflang=")([0-9a-z\-]*)(".*\s+rel="alternate".*/>)' . chr(1) . 'i', $body, $matches);
  61              foreach ($matches[2] as $match)
  62              {
  63                  $new_code = $this->params->get(strtolower($match));
  64                  if ($new_code)
  65                  {
  66                      $patterns[] = chr(1) . '(<link.*\s+hreflang=")(' . $match . ')(".*\s+rel="alternate".*/>)' . chr(1) . 'i';
  67                      $replace[] = '$1}' . $new_code . '$3}';
  68                  }
  69              }
  70              preg_match_all(chr(1) . '(<link.*\s+rel="alternate".*\s+hreflang=")([0-9A-Za-z\-]*)(".*/>)' . chr(1) . 'i', $body, $matches);
  71              foreach ($matches[2] as $match)
  72              {
  73                  $new_code = $this->params->get(strtolower($match));
  74                  if ($new_code)
  75                  {
  76                      $patterns[] = chr(1) . '(<link.*\s+rel="alternate".*\s+hreflang=")(' . $match . ')(".*/>)' . chr(1) . 'i';
  77                      $replace[] = '$1}' . $new_code . '$3}';
  78                  }
  79              }
  80              JResponse::setBody(preg_replace($patterns, $replace, $body));
  81          }
  82      }
  83  
  84      /**
  85       * @param    JForm    $form    The form to be altered.
  86       * @param    array    $data    The associated data for the form.
  87       *
  88       * @return    boolean
  89       * @since    2.5
  90       */
  91  	public function onContentPrepareForm($form, $data)
  92      {
  93          // Check we have a form
  94          if (!($form instanceof JForm))
  95          {
  96              $this->_subject->setError('JERROR_NOT_A_FORM');
  97              return false;
  98          }
  99  
 100          // Check we are manipulating a valid form.
 101          $app = JFactory::getApplication();
 102          if ($form->getName() != 'com_plugins.plugin'
 103              || isset($data->name) && $data->name != 'plg_system_languagecode'
 104              || empty($data) && !$app->getUserState('plg_system_language_code.edit')
 105          )
 106          {
 107              return true;
 108          }
 109  
 110          // Mark the plugin as being edited
 111          $app->setUserState('plg_system_language_code.edit', $data->name == 'plg_system_languagecode');
 112  
 113          // Get site languages
 114          $languages = JLanguage::getKnownLanguages(JPATH_SITE);
 115  
 116          // Inject fields into the form
 117          foreach ($languages as $tag => $language)
 118          {
 119              $form->load('
 120  <form>
 121      <fields name="params">
 122          <fieldset
 123              name="languagecode"
 124              label="PLG_SYSTEM_LANGUAGECODE_FIELDSET_LABEL"
 125              description="PLG_SYSTEM_LANGUAGECODE_FIELDSET_DESC"
 126          >
 127              <field
 128                  name="'.strtolower($tag).'"
 129                  type="text"
 130                  description="' . htmlspecialchars(JText::sprintf('PLG_SYSTEM_LANGUAGECODE_FIELD_DESC', $language['name']), ENT_COMPAT, 'UTF-8') . '"
 131                  translate_description="false"
 132                  label="' . $tag . '"
 133                  translate_label="false"
 134                  size="7"
 135                  filter="cmd"
 136              />
 137          </fieldset>
 138      </fields>
 139  </form>
 140              ');
 141          }
 142          return true;
 143      }
 144  }

title

Description

title

Description

title

Description

title

title

Body