Source for file languagecode.php

Documentation is available at languagecode.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Plugin
  4.  * @subpackage  System.languagecode
  5.  *
  6.  * @copyright   Copyright (C) 2005 - 2013 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. {
  20.     /**
  21.      * Plugin that change the language code used in the <html /> tag
  22.      *
  23.      * @since  2.5
  24.      */
  25.     public function onAfterRender()
  26.     {
  27.         $app JFactory::getApplication();
  28.         // Use this plugin only in site application
  29.         if ($app->isSite())
  30.         {
  31.             // Get the response body
  32.             $body $app->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[2as $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[2as $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.             $app->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.         // Ensure that data is an object
  94.         $data = (object) $data;
  95.  
  96.         // Check we have a form
  97.         if (!($form instanceof JForm))
  98.         {
  99.             $this->_subject->setError('JERROR_NOT_A_FORM');
  100.             return false;
  101.         }
  102.  
  103.         // Check we are manipulating a valid form.
  104.         $app JFactory::getApplication();
  105.         if ($form->getName(!= 'com_plugins.plugin'
  106.             || isset($data->name&& $data->name != 'plg_system_languagecode'
  107.             || empty($data&& !$app->getUserState('plg_system_language_code.edit')
  108.         )
  109.         {
  110.             return true;
  111.         }
  112.  
  113.         // Mark the plugin as being edited
  114.         $app->setUserState('plg_system_language_code.edit'$data->name == 'plg_system_languagecode');
  115.  
  116.         // Get site languages
  117.         if ($languages JLanguage::getKnownLanguages(JPATH_SITE))
  118.         {
  119.             // Inject fields into the form
  120.             foreach ($languages as $tag => $language)
  121.             {
  122.                 $form->load('
  123.                     <form>
  124.                         <fields name="params">
  125.                             <fieldset
  126.                                 name="languagecode"
  127.                                 label="PLG_SYSTEM_LANGUAGECODE_FIELDSET_LABEL"
  128.                                 description="PLG_SYSTEM_LANGUAGECODE_FIELDSET_DESC"
  129.                             >
  130.                                 <field
  131.                                     name="'.strtolower($tag).'"
  132.                                     type="text"
  133.                                     description="' htmlspecialchars(JText::sprintf('PLG_SYSTEM_LANGUAGECODE_FIELD_DESC'$language['name'])ENT_COMPAT'UTF-8''"
  134.                                     translate_description="false"
  135.                                     label="' $tag '"
  136.                                     translate_label="false"
  137.                                     size="7"
  138.                                     filter="cmd"
  139.                                 />
  140.                             </fieldset>
  141.                         </fields>
  142.                     </form>
  143.                 ');
  144.             }
  145.         }
  146.  
  147.         return true;
  148.     }
  149. }

Documentation generated on Tue, 19 Nov 2013 15:06:33 +0100 by phpDocumentor 1.4.3