Source for file languages.php

Documentation is available at languages.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Administrator
  4.  * @subpackage  com_installer
  5.  * @copyright   Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  6.  * @license     GNU General Public License version 2 or later; see LICENSE.txt
  7.  */
  8.  
  9. defined('_JEXEC'or die;
  10.  
  11. jimport('joomla.updater.update');
  12.  
  13. /**
  14.  * Languages Installer Model
  15.  *
  16.  * @package     Joomla.Administrator
  17.  * @subpackage  com_installer
  18.  * @since       2.5.7
  19.  */
  20. {
  21.     /**
  22.      * Constructor override, defines a white list of column filters.
  23.      *
  24.      * @param   array  $config  An optional associative array of configuration settings.
  25.      *
  26.      * @since   2.5.7
  27.      */
  28.     public function __construct($config array())
  29.     {
  30.         if (empty($config['filter_fields']))
  31.         {
  32.             $config['filter_fields'array(
  33.                 'update_id''update_id',
  34.                 'name''name',
  35.             );
  36.         }
  37.  
  38.         parent::__construct($config);
  39.     }
  40.  
  41.     /**
  42.      * Method to get the available languages database query.
  43.      *
  44.      * @return  JDatabaseQuery  The database query
  45.      *
  46.      * @since   2.5.7
  47.      */
  48.     protected function _getListQuery()
  49.     {
  50.         $db   JFactory::getDbo();
  51.         $query $db->getQuery(true);
  52.  
  53.         // Select the required fields from the updates table
  54.         $query->select('update_id, name, version, detailsurl, type')
  55.  
  56.             ->from('#__updates');
  57.  
  58.         // This Where clause will avoid to list languages already installed.
  59.         $query->where('extension_id = 0');
  60.  
  61.         // Filter by search in title
  62.         $search $this->getState('filter.search');
  63.         if (!empty($search))
  64.         {
  65.             $search $db->quote('%' $db->escape($searchtrue'%');
  66.             $query->where('(name LIKE ' $search ')');
  67.         }
  68.  
  69.         // Add the list ordering clause.
  70.         $listOrder $this->state->get('list.ordering');
  71.         $orderDirn $this->state->get('list.direction');
  72.         $query->order($db->escape($listOrder' ' $db->escape($orderDirn));
  73.  
  74.         return $query;
  75.     }
  76.  
  77.     /**
  78.      * Method to get a store id based on model configuration state.
  79.      *
  80.      * @param   string  $id  A prefix for the store id.
  81.      *
  82.      * @return  string  A store id.
  83.      *
  84.      * @since   2.5.7
  85.      */
  86.     protected function getStoreId($id '')
  87.     {
  88.         // Compile the store id.
  89.         $id    .= ':' $this->getState('filter.search');
  90.  
  91.         return parent::getStoreId($id);
  92.     }
  93.  
  94.     /**
  95.      * Method to auto-populate the model state.
  96.      *
  97.      * Note. Calling getState in this method will result in recursion.
  98.      *
  99.      * @param   string  $ordering   list order
  100.      * @param   string  $direction  direction in the list
  101.      *
  102.      * @return  void 
  103.      *
  104.      * @since   2.5.7
  105.      */
  106.     protected function populateState($ordering 'name'$direction 'asc')
  107.     {
  108.         $app JFactory::getApplication();
  109.  
  110.         $value $app->getUserStateFromRequest($this->context . '.filter.search''filter_search');
  111.         $this->setState('filter.search'$value);
  112.  
  113.         $this->setState('extension_message'$app->getUserState('com_installer.extension_message'));
  114.  
  115.         parent::populateState($ordering$direction);
  116.     }
  117.  
  118.     /**
  119.      * Method to find available languages in the Accredited Languages Update Site.
  120.      *
  121.      * @param   int  $cache_timeout  time before refreshing the cached updates
  122.      *
  123.      * @return  bool 
  124.      *
  125.      * @since   2.5.7
  126.      */
  127.     public function findLanguages($cache_timeout 0)
  128.     {
  129.         $updater JUpdater::getInstance();
  130.  
  131.         /*
  132.          * The following function uses extension_id 600, that is the english language extension id.
  133.          * In #__update_sites_extensions you should have 600 linked to the Accredited Translations Repo
  134.          */
  135.         $updater->findUpdates(array(600)$cache_timeout);
  136.  
  137.         return true;
  138.     }
  139.  
  140.     /**
  141.      * Install languages in the system.
  142.      *
  143.      * @param   array  $lids  array of language ids selected in the list
  144.      *
  145.      * @return  bool 
  146.      *
  147.      * @since   2.5.7
  148.      */
  149.     public function install($lids)
  150.     {
  151.         $app       JFactory::getApplication();
  152.         $installer JInstaller::getInstance();
  153.  
  154.         // Loop through every selected language
  155.         foreach ($lids as $id)
  156.         {
  157.             // Loads the update database object that represents the language
  158.             $language JTable::getInstance('update');
  159.             $language->load($id);
  160.  
  161.             // Get the url to the XML manifest file of the selected language
  162.             $remote_manifest $this->_getLanguageManifest($id);
  163.             if (!$remote_manifest)
  164.             {
  165.                 // Could not find the url, the information in the update server may be corrupt
  166.                 $message  JText::sprintf('COM_INSTALLER_MSG_LANGUAGES_CANT_FIND_REMOTE_MANIFEST'$language->name);
  167.                 $message .= ' ' JText::_('COM_INSTALLER_MSG_LANGUAGES_TRY_LATER');
  168.                 $app->enqueueMessage($message);
  169.                 continue;
  170.             }
  171.  
  172.             // Based on the language XML manifest get the url of the package to download
  173.             $package_url $this->_getPackageUrl($remote_manifest);
  174.             if (!$package_url)
  175.             {
  176.                 // Could not find the url , maybe the url is wrong in the update server, or there is not internet access
  177.                 $message  JText::sprintf('COM_INSTALLER_MSG_LANGUAGES_CANT_FIND_REMOTE_PACKAGE'$language->name);
  178.                 $message .= ' ' JText::_('COM_INSTALLER_MSG_LANGUAGES_TRY_LATER');
  179.                 $app->enqueueMessage($message);
  180.                 continue;
  181.             }
  182.  
  183.             // Download the package to the tmp folder
  184.             $package $this->_downloadPackage($package_url);
  185.  
  186.             // Install the package
  187.             if (!$installer->install($package['dir']))
  188.             {
  189.                 // There was an error installing the package
  190.                 $message  JText::sprintf('COM_INSTALLER_INSTALL_ERROR'$language->name);
  191.                 $message .= ' ' JText::_('COM_INSTALLER_MSG_LANGUAGES_TRY_LATER');
  192.                 $app->enqueueMessage($message);
  193.                 continue;
  194.             }
  195.  
  196.             // Package installed successfully
  197.             $app->enqueueMessage(JText::sprintf('COM_INSTALLER_INSTALL_SUCCESS'$language->name));
  198.  
  199.             // Cleanup the install files in tmp folder
  200.             if (!is_file($package['packagefile']))
  201.             {
  202.                 $config JFactory::getConfig();
  203.                 $package['packagefile'$config->get('tmp_path''/' $package['packagefile'];
  204.             }
  205.             JInstallerHelper::cleanupInstall($package['packagefile']$package['extractdir']);
  206.  
  207.             // Delete the installed language from the list
  208.             $language->delete($id);
  209.         }
  210.  
  211.     }
  212.  
  213.     /**
  214.      * Gets the manifest file of a selected language from a the language list in a update server.
  215.      *
  216.      * @param   int  $uid  the id of the language in the #__updates table
  217.      *
  218.      * @return  string 
  219.      *
  220.      * @since   2.5.7
  221.      */
  222.     protected function _getLanguageManifest($uid)
  223.     {
  224.         $instance JTable::getInstance('update');
  225.         $instance->load($uid);
  226.  
  227.         return $instance->detailsurl;
  228.     }
  229.  
  230.     /**
  231.      * Finds the url of the package to download.
  232.      *
  233.      * @param   string  $remote_manifest  url to the manifest XML file of the remote package
  234.      *
  235.      * @return  string|bool
  236.      *
  237.      * @since   2.5.7
  238.      */
  239.     protected function _getPackageUrl$remote_manifest )
  240.     {
  241.         $update new JUpdate;
  242.         $update->loadFromXML($remote_manifest);
  243.         $package_url trim($update->get('downloadurl'false)->_data);
  244.  
  245.         return $package_url;
  246.     }
  247.  
  248.     /**
  249.      * Download a language package from a URL and unpack it in the tmp folder.
  250.      *
  251.      * @param   string  $url  hola
  252.      *
  253.      * @return  array|bool Package details or false on failure
  254.      *
  255.      * @since   2.5.7
  256.      */
  257.     protected function _downloadPackage($url)
  258.     {
  259.         // Download the package from the given URL
  260.         $p_file JInstallerHelper::downloadPackage($url);
  261.  
  262.         // Was the package downloaded?
  263.         if (!$p_file)
  264.         {
  265.             JError::raiseWarning(''JText::_('COM_INSTALLER_MSG_INSTALL_INVALID_URL'));
  266.             return false;
  267.         }
  268.  
  269.         $config   JFactory::getConfig();
  270.         $tmp_dest $config->get('tmp_path');
  271.  
  272.         // Unpack the downloaded package file
  273.         $package JInstallerHelper::unpack($tmp_dest '/' $p_file);
  274.  
  275.         return $package;
  276.     }
  277. }

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