Source for file languages.php

Documentation is available at languages.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Installation
  4.  * @subpackage  Model
  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. jimport('joomla.updater.update');
  13. jimport('legacy.component.helper');
  14.  
  15. /**
  16.  * Language Installer model for the Joomla Core Installer.
  17.  *
  18.  * @package     Joomla.Installation
  19.  * @subpackage  Model
  20.  * @since       3.1
  21.  */
  22. {
  23.     /**
  24.      * @var    object  Client object
  25.      * @since  3.1
  26.      */
  27.     protected $client = null;
  28.  
  29.     /**
  30.      * @var    array  Languages description
  31.      * @since  3.1
  32.      */
  33.     protected $data = null;
  34.  
  35.     /**
  36.      * @var    string  Language path
  37.      * @since  3.1
  38.      */
  39.     protected $path = null;
  40.  
  41.     /**
  42.      * @var    integer  Total number of languages installed
  43.      * @since  3.1
  44.      */
  45.     protected $langlist = null;
  46.  
  47.     /**
  48.      * @var    Admin Id, author of all generated content
  49.      * @since  3.1
  50.      */
  51.     protected $adminId = null;
  52.  
  53.     /**
  54.      * Constructor: Deletes the default installation config file and recreates it with the good config file.
  55.      *
  56.      * @since  3.1
  57.      */
  58.     public function __construct()
  59.     {
  60.         // Overrides application config and set the configuration.php file so tokens and database works
  61.         JFactory::$config null;
  62.         JFactory::getConfig(JPATH_SITE '/configuration.php');
  63.         JFactory::$session null;
  64.  
  65.         parent::__construct();
  66.     }
  67.  
  68.     /**
  69.      * Generate a list of language choices to install in the Joomla CMS
  70.      *
  71.      * @return  boolean  True if successful
  72.      *
  73.      * @since   3.1
  74.      */
  75.     public function getItems()
  76.     {
  77.         $updater JUpdater::getInstance();
  78.  
  79.         /*
  80.          * The following function uses extension_id 600, that is the English language extension id.
  81.          * In #__update_sites_extensions you should have 600 linked to the Accredited Translations Repo
  82.          */
  83.         $updater->findUpdates(array(600)0);
  84.  
  85.         $db    JFactory::getDbo();
  86.         $query $db->getQuery(true);
  87.  
  88.         // Select the required fields from the updates table
  89.         $query->select('update_id, name, version')
  90.             ->from('#__updates')
  91.             ->order('name');
  92.  
  93.         $db->setQuery($query);
  94.         $list $db->loadObjectList();
  95.  
  96.         if (!$list || $list instanceof Exception)
  97.         {
  98.             $list array();
  99.         }
  100.  
  101.         return $list;
  102.     }
  103.  
  104.     /**
  105.      * Method that installs in Joomla! the selected languages in the Languages View of the installer.
  106.      *
  107.      * @param   array  $lids  List of the update_id value of the languages to install
  108.      *
  109.      * @return  boolean True if successful
  110.      */
  111.     public function install($lids)
  112.     {
  113.         /* @var InstallationApplicationWeb $app */
  114.         $app       JFactory::getApplication();
  115.         $installer JInstaller::getInstance();
  116.  
  117.         // Loop through every selected language
  118.         foreach ($lids as $id)
  119.         {
  120.             // Loads the update database object that represents the language
  121.             $language JTable::getInstance('update');
  122.             $language->load($id);
  123.  
  124.             // Get the url to the XML manifest file of the selected language
  125.             $remote_manifest $this->getLanguageManifest($id);
  126.  
  127.             if (!$remote_manifest)
  128.             {
  129.                 // Could not find the url, the information in the update server may be corrupt
  130.                 $message JText::sprintf('INSTL_DEFAULTLANGUAGE_COULD_NOT_INSTALL_LANGUAGE'$language->name);
  131.                 $message .= ' ' JText::_('INSTL_DEFAULTLANGUAGE_TRY_LATER');
  132.                 $app->enqueueMessage($message);
  133.                 continue;
  134.             }
  135.  
  136.             // Based on the language XML manifest get the url of the package to download
  137.             $package_url $this->getPackageUrl($remote_manifest);
  138.  
  139.             if (!$package_url)
  140.             {
  141.                 // Could not find the url , maybe the url is wrong in the update server, or there is not internet access
  142.                 $message JText::sprintf('INSTL_DEFAULTLANGUAGE_COULD_NOT_INSTALL_LANGUAGE'$language->name);
  143.                 $message .= ' ' JText::_('INSTL_DEFAULTLANGUAGE_TRY_LATER');
  144.                 $app->enqueueMessage($message);
  145.                 continue;
  146.             }
  147.  
  148.             // Download the package to the tmp folder
  149.             $package $this->downloadPackage($package_url);
  150.  
  151.             // Install the package
  152.             if (!$installer->install($package['dir']))
  153.             {
  154.                 // There was an error installing the package
  155.                 $message JText::sprintf('INSTL_DEFAULTLANGUAGE_COULD_NOT_INSTALL_LANGUAGE'$language->name);
  156.                 $message .= ' ' JText::_('INSTL_DEFAULTLANGUAGE_TRY_LATER');
  157.                 $app->enqueueMessage($message);
  158.                 continue;
  159.             }
  160.  
  161.             // Cleanup the install files in tmp folder
  162.             if (!is_file($package['packagefile']))
  163.             {
  164.                 $config                 JFactory::getConfig();
  165.                 $package['packagefile'$config->get('tmp_path''/' $package['packagefile'];
  166.             }
  167.  
  168.             JInstallerHelper::cleanupInstall($package['packagefile']$package['extractdir']);
  169.  
  170.             // Delete the installed language from the list
  171.             $language->delete($id);
  172.         }
  173.  
  174.         return true;
  175.     }
  176.  
  177.     /**
  178.      * Gets the manifest file of a selected language from a the language list in a update server.
  179.      *
  180.      * @param   integer  $uid  The id of the language in the #__updates table
  181.      *
  182.      * @return  string 
  183.      *
  184.      * @since   3.1
  185.      */
  186.     protected function getLanguageManifest($uid)
  187.     {
  188.         $instance JTable::getInstance('update');
  189.         $instance->load($uid);
  190.         $detailurl trim($instance->detailsurl);
  191.  
  192.         return $detailurl;
  193.     }
  194.  
  195.     /**
  196.      * Finds the url of the package to download.
  197.      *
  198.      * @param   string  $remote_manifest  Url to the manifest XML file of the remote package
  199.      *
  200.      * @return  string|bool
  201.      *
  202.      * @since   3.1
  203.      */
  204.     protected function getPackageUrl($remote_manifest)
  205.     {
  206.         $update new JUpdate;
  207.         $update->loadFromXML($remote_manifest);
  208.         $package_url trim($update->get('downloadurl'false)->_data);
  209.  
  210.         return $package_url;
  211.     }
  212.  
  213.     /**
  214.      * Download a language package from a URL and unpack it in the tmp folder.
  215.      *
  216.      * @param   string  $url  Url of the package
  217.      *
  218.      * @return  array|boolPackage details or false on failure
  219.      *
  220.      * @since   3.1
  221.      */
  222.     protected function downloadPackage($url)
  223.     {
  224.         // Download the package from the given URL
  225.         $p_file JInstallerHelper::downloadPackage($url);
  226.  
  227.         // Was the package downloaded?
  228.         if (!$p_file)
  229.         {
  230.             JFactory::getApplication()->enqueueMessage(JText::_('COM_INSTALLER_MSG_INSTALL_INVALID_URL')'warning');
  231.  
  232.             return false;
  233.         }
  234.  
  235.         $config   JFactory::getConfig();
  236.         $tmp_dest $config->get('tmp_path');
  237.  
  238.         // Unpack the downloaded package file
  239.         $package JInstallerHelper::unpack($tmp_dest '/' $p_file);
  240.  
  241.         return $package;
  242.     }
  243.  
  244.     /**
  245.      * Get Languages item data for the Administrator
  246.      *
  247.      * @return  array 
  248.      *
  249.      * @since   3.1
  250.      */
  251.     public function getInstalledlangsAdministrator()
  252.     {
  253.         return $this->getInstalledlangs('administrator');
  254.     }
  255.  
  256.     /**
  257.      * Get Languages item data for the Frontend
  258.      *
  259.      * @return  array  List of installed languages in the frontend application
  260.      *
  261.      * @since   3.1
  262.      */
  263.     public function getInstalledlangsFrontend()
  264.     {
  265.         return $this->getInstalledlangs('site');
  266.     }
  267.  
  268.     /**
  269.      * Get Languages item data
  270.      *
  271.      * @param   string  $cms_client  Name of the cms client
  272.      *
  273.      * @return  array 
  274.      *
  275.      * @since   3.1
  276.      */
  277.     protected function getInstalledlangs($cms_client 'administrator')
  278.     {
  279.         // Get information
  280.         $path     $this->getPath();
  281.         $client   $this->getClient($cms_client);
  282.         $langlist $this->getLanguageList($client->id);
  283.  
  284.         // Compute all the languages
  285.         $data array();
  286.  
  287.         foreach ($langlist as $lang)
  288.         {
  289.             $file          $path '/' $lang '/' $lang '.xml';
  290.             $info          JInstaller::parseXMLInstallFile($file);
  291.             $row           new stdClass;
  292.             $row->language $lang;
  293.  
  294.             if (!is_array($info))
  295.             {
  296.                 continue;
  297.             }
  298.  
  299.             foreach ($info as $key => $value)
  300.             {
  301.                 $row->$key $value;
  302.             }
  303.  
  304.             // If current then set published
  305.             $params JComponentHelper::getParams('com_languages');
  306.  
  307.             if ($params->get($client->name'en-GB'== $row->language)
  308.             {
  309.                 $row->published 1;
  310.             }
  311.             else
  312.             {
  313.                 $row->published 0;
  314.             }
  315.  
  316.             $row->checked_out 0;
  317.             $data[]           $row;
  318.         }
  319.  
  320.         usort($dataarray($this'compareLanguages'));
  321.  
  322.         return $data;
  323.     }
  324.  
  325.     /**
  326.      * Get installed languages data.
  327.      *
  328.      * @param   integer  $client_id  The client ID to retrieve data for
  329.      *
  330.      * @return  object  The language data
  331.      *
  332.      * @since   3.1
  333.      */
  334.     protected function getLanguageList($client_id 1)
  335.     {
  336.         // Create a new db object.
  337.         $db    JFactory::getDbo();
  338.         $query $db->getQuery(true);
  339.  
  340.         // Select field element from the extensions table.
  341.         $query->select('a.element, a.name')
  342.             ->from('#__extensions AS a')
  343.             ->where('a.type = ' $db->quote('language'))
  344.             ->where('state = 0')
  345.             ->where('enabled = 1')
  346.             ->where('client_id=' . (int) $client_id);
  347.  
  348.         $db->setQuery($query);
  349.  
  350.         $this->langlist = $db->loadColumn();
  351.  
  352.         return $this->langlist;
  353.     }
  354.  
  355.     /**
  356.      * Compare two languages in order to sort them.
  357.      *
  358.      * @param   object  $lang1  The first language
  359.      * @param   object  $lang2  The second language
  360.      *
  361.      * @return  integer 
  362.      *
  363.      * @since   3.1
  364.      */
  365.     protected function compareLanguages($lang1$lang2)
  366.     {
  367.         return strcmp($lang1->name$lang2->name);
  368.     }
  369.  
  370.     /**
  371.      * Get the languages folder path.
  372.      *
  373.      * @return  string  The path to the languages folders
  374.      *
  375.      * @since   3.1
  376.      */
  377.     protected function getPath()
  378.     {
  379.         if (is_null($this->path))
  380.         {
  381.             $client     $this->getClient();
  382.             $this->path = JLanguage::getLanguagePath($client->path);
  383.         }
  384.  
  385.         return $this->path;
  386.     }
  387.  
  388.     /**
  389.      * Get the client object of Administrator or Frontend.
  390.      *
  391.      * @param   string  $client  Name of the client object
  392.      *
  393.      * @return  object 
  394.      *
  395.      * @since   3.1
  396.      */
  397.     protected function getClient($client 'administrator')
  398.     {
  399.         $this->client = JApplicationHelper::getClientInfo($clienttrue);
  400.  
  401.         return $this->client;
  402.     }
  403.  
  404.     /**
  405.      * Set the default language.
  406.      *
  407.      * @param   string  $language    The language to be set as default
  408.      * @param   string  $cms_client  The name of the CMS client
  409.      *
  410.      * @return  boolean 
  411.      *
  412.      * @since   3.1
  413.      */
  414.     public function setDefault($language$cms_client 'administrator')
  415.     {
  416.         // Get the application
  417.         /* @var InstallationApplicationWeb $app */
  418.         $app JFactory::getApplication();
  419.  
  420.         $client $this->getClient($cms_client);
  421.  
  422.         $params JComponentHelper::getParams('com_languages');
  423.         $params->set($client->name$language);
  424.  
  425.         $table JTable::getInstance('extension');
  426.         $id    $table->find(array('element' => 'com_languages'));
  427.  
  428.         // Load
  429.         if (!$table->load($id))
  430.         {
  431.             $app->enqueueMessage($table->getError()'warning');
  432.  
  433.             return false;
  434.         }
  435.  
  436.         $table->params = (string) $params;
  437.  
  438.         // Pre-save checks
  439.         if (!$table->check())
  440.         {
  441.             $app->enqueueMessage($table->getError()'warning');
  442.  
  443.             return false;
  444.         }
  445.  
  446.         // Save the changes
  447.         if (!$table->store())
  448.         {
  449.             $app->enqueueMessage($table->getError()'warning');
  450.  
  451.             return false;
  452.         }
  453.  
  454.         return true;
  455.     }
  456.  
  457.     /**
  458.      * Get the current setup options from the session.
  459.      *
  460.      * @return  array 
  461.      *
  462.      * @since   3.1
  463.      */
  464.     public function getOptions()
  465.     {
  466.         $session JFactory::getSession();
  467.         $options $session->get('setup.options'array());
  468.  
  469.         return $options;
  470.     }
  471.  
  472.     /**
  473.      * Get the model form.
  474.      *
  475.      * @param   string  $view  The view being processed
  476.      *
  477.      * @return  mixed  JForm object on success, false on failure.
  478.      *
  479.      * @since   3.1
  480.      */
  481.     public function getForm($view null)
  482.     {
  483.         /* @var InstallationApplicationWeb $app */
  484.         $app JFactory::getApplication();
  485.  
  486.         if (!$view)
  487.         {
  488.             $view $app->input->getWord('view''defaultlanguage');
  489.         }
  490.  
  491.         // Get the form.
  492.         JForm::addFormPath(JPATH_COMPONENT '/model/forms');
  493.         JForm::addFieldPath(JPATH_COMPONENT '/model/fields');
  494.         JForm::addRulePath(JPATH_COMPONENT '/model/rules');
  495.  
  496.         try
  497.         {
  498.             $form JForm::getInstance('jform'$viewarray('control' => 'jform'));
  499.         }
  500.         catch (Exception $e)
  501.         {
  502.             $app->enqueueMessage($e->getMessage()'error');
  503.  
  504.             return false;
  505.         }
  506.  
  507.         // Check the session for previously entered form data.
  508.         $data = (array) $this->getOptions();
  509.  
  510.         // Bind the form data if present.
  511.         if (!empty($data))
  512.         {
  513.             $form->bind($data);
  514.         }
  515.  
  516.         return $form;
  517.     }
  518.  
  519.     /**
  520.      * Enable a Joomla plugin
  521.      *
  522.      * @param   string  $pluginName  The name of plugin
  523.      *
  524.      * @return  boolean 
  525.      *
  526.      * @since   3.2
  527.      */
  528.     public function enablePlugin($pluginName)
  529.     {
  530.         // Create a new db object.
  531.         $db    JFactory::getDbo();
  532.         $query $db->getQuery(true);
  533.  
  534.         $query
  535.             ->clear()
  536.             ->update('#__extensions')
  537.             ->set('enabled = 1')
  538.             ->where('name = ' $db->quote($pluginName))
  539.             ->where('type = ' $db->quote('plugin'));
  540.  
  541.         $db->setQuery($query);
  542.  
  543.         if (!$db->execute())
  544.         {
  545.             return false;
  546.         }
  547.  
  548.         // Store language filter plugin parameters
  549.         if ($pluginName == 'plg_system_languagefilter')
  550.         {
  551.             $params '{"detect_browser":"0","automatic_change":"1","item_associations":"1","remove_default_prefix":"0","lang_cookie":"0","alternate_meta":"1"}';
  552.             $query
  553.                 ->clear()
  554.                 ->update('#__extensions')
  555.                 ->set('params = ' $db->quote($params))
  556.                 ->where('name = ' $db->quote('plg_system_languagefilter'))
  557.                 ->where('type = ' $db->quote('plugin'));
  558.  
  559.             $db->setQuery($query);
  560.  
  561.             if (!$db->execute())
  562.             {
  563.                 return false;
  564.             }
  565.         }
  566.  
  567.         return true;
  568.     }
  569.  
  570.     /**
  571.      * Enable the Language Switcher Module.
  572.      *
  573.      * @return  boolean 
  574.      *
  575.      * @since   3.2
  576.      */
  577.     public function addModuleLanguageSwitcher()
  578.     {
  579.         JTable::addIncludePath(JPATH_LIBRARIES '/legacy/table/');
  580.         $tableModule JTable::getInstance('Module''JTable');
  581.         $moduleData  array(
  582.             'id'        => 0,
  583.             'title'     => 'Language Switcher',
  584.             'note'      => '',
  585.             'content'   => '',
  586.             'position'  => 'position-0',
  587.             'module'    => 'mod_languages',
  588.             'access'    => 1,
  589.             'showtitle' => 0,
  590.             'params'    =>
  591.                 '{"header_text":"","footer_text":"","dropdown":"0","image":"1","inline":"1","show_active":"1",'
  592.                 . '"full_name":"1","layout":"_:default","moduleclass_sfx":"","cache":"0","cache_time":"900","cachemode":"itemid",'
  593.                 . '"module_tag":"div","bootstrap_size":"0","header_tag":"h3","header_class":"","style":"0"}',
  594.             'client_id' => 0,
  595.             'language'  => '*',
  596.             'published' => 1
  597.         );
  598.  
  599.         // Bind the data.
  600.         if (!$tableModule->bind($moduleData))
  601.         {
  602.             return false;
  603.         }
  604.  
  605.         // Check the data.
  606.         if (!$tableModule->check())
  607.         {
  608.             return false;
  609.         }
  610.  
  611.         // Store the data.
  612.         if (!$tableModule->store())
  613.         {
  614.             return false;
  615.         }
  616.  
  617.         return $this->addModuleInModuleMenu((int) $tableModule->id);
  618.     }
  619.  
  620.     /**
  621.      * Add a Module in Module menus
  622.      *
  623.      * @param   integer  $moduleId  The Id of module
  624.      *
  625.      * @return  boolean 
  626.      *
  627.      * @since   3.2
  628.      */
  629.     public function addModuleInModuleMenu($moduleId)
  630.     {
  631.         // Create a new db object.
  632.         $db    JFactory::getDbo();
  633.         $query $db->getQuery(true);
  634.  
  635.         // Add Module in Module menus
  636.         $query->clear()
  637.             ->insert('#__modules_menu')
  638.             ->columns(array($db->quoteName('moduleid')$db->quoteName('menuid')))
  639.             ->values($moduleId ', 0');
  640.         $db->setQuery($query);
  641.  
  642.         try
  643.         {
  644.             $db->execute();
  645.         }
  646.         catch (RuntimeException $e)
  647.         {
  648.             return false;
  649.         }
  650.  
  651.         return true;
  652.     }
  653.  
  654.     /**
  655.      * Gets a unique language SEF string
  656.      *
  657.      * This function checks other existing language with the same code, if they exist provides a unique SEF name.
  658.      * For instance: en-GB, en-US and en-AU will share the same SEF code by default: www.mywebsite.com/en/
  659.      * To avoid this conflict, this function creates an specific SEF in case of existing conflict:
  660.      * For example: www.mywebsite.com/en-au/
  661.      *
  662.      * @param   stdClass    $itemLanguage   Language Object
  663.      * @param   stdClass[]  $siteLanguages  All Language Objects
  664.      *
  665.      * @return  string 
  666.      *
  667.      * @since   3.2
  668.      */
  669.     public function getSefString($itemLanguage$siteLanguages)
  670.     {
  671.         $langs explode('-'$itemLanguage->language);
  672.         $prefixToFind $langs[0];
  673.  
  674.         $numberPrefixesFound 0;
  675.         foreach ($siteLanguages as $siteLang)
  676.         {
  677.             $langs explode('-'$siteLang->language);
  678.             $lang  $langs[0];
  679.  
  680.             if ($lang == $prefixToFind)
  681.             {
  682.                 ++$numberPrefixesFound;
  683.             }
  684.         }
  685.  
  686.         if ($numberPrefixesFound == 1)
  687.         {
  688.             return $prefixToFind;
  689.         }
  690.         return strtolower($itemLanguage->language);
  691.     }
  692.  
  693.     /**
  694.      * Add a Content Language
  695.      *
  696.      * @param   stdClass  $itemLanguage   Language Object
  697.      * @param   string    $sefLangString  String to use for SEF so it doesn't conflict
  698.      *
  699.      * @return  boolean 
  700.      *
  701.      * @since   3.2
  702.      */
  703.     public function addLanguage($itemLanguage$sefLangString)
  704.     {
  705.         $tableLanguage JTable::getInstance('Language');
  706.  
  707.         $flag strtolower(str_replace('-''_',  $itemLanguage->language));
  708.  
  709.         // Load the native language name
  710.         $installationLocalisedIni new JLanguage($itemLanguage->languagefalse);
  711.         $nativeLanguageName       $installationLocalisedIni->_('INSTL_DEFAULTLANGUAGE_NATIVE_LANGUAGE_NAME');
  712.  
  713.         // If the local name do not exist in the translation file we use the international standard name
  714.         if ($nativeLanguageName == 'INSTL_DEFAULTLANGUAGE_NATIVE_LANGUAGE_NAME')
  715.         {
  716.             $nativeLanguageName $itemLanguage->name;
  717.         }
  718.  
  719.         $langData array(
  720.             'lang_id'      => 0,
  721.             'lang_code'    => $itemLanguage->language,
  722.             'title'        => $itemLanguage->name,
  723.             'title_native' => $nativeLanguageName,
  724.             'sef'          => $sefLangString,
  725.             'image'        => $flag,
  726.             'published'    => 1
  727.         );
  728.  
  729.         // Bind the data.
  730.         if (!$tableLanguage->bind($langData))
  731.         {
  732.             return false;
  733.         }
  734.  
  735.         // Check the data.
  736.         if (!$tableLanguage->check())
  737.         {
  738.             return false;
  739.         }
  740.  
  741.         // Store the data.
  742.         if (!$tableLanguage->store())
  743.         {
  744.             return false;
  745.         }
  746.  
  747.         return true;
  748.     }
  749.  
  750.     /**
  751.      * Add Menu Group
  752.      *
  753.      * @param   stdClass  $itemLanguage  Language Object
  754.      *
  755.      * @return  boolean 
  756.      *
  757.      * @since   3.2
  758.      */
  759.     public function addMenuGroup($itemLanguage)
  760.     {
  761.         // Add menus
  762.         JLoader::registerPrefix('J'JPATH_PLATFORM '/legacy');
  763.         JTable::addIncludePath(JPATH_ADMINISTRATOR '/components/com_menus/tables/');
  764.  
  765.         // Add Menu Group
  766.         $tableMenu JTable::getInstance('Type''JTableMenu');
  767.  
  768.         $menuData array(
  769.             'id'          => 0,
  770.             'menutype'    => 'mainmenu-' strtolower($itemLanguage->language),
  771.             'title'       => 'Main Menu (' $itemLanguage->language ')',
  772.             'description' => 'The main menu for the site in language ' $itemLanguage->name
  773.         );
  774.  
  775.         // Bind the data.
  776.         if (!$tableMenu->bind($menuData))
  777.         {
  778.             return false;
  779.         }
  780.  
  781.         // Check the data.
  782.         if (!$tableMenu->check())
  783.         {
  784.             return false;
  785.         }
  786.  
  787.         // Store the data.
  788.         if (!$tableMenu->store())
  789.         {
  790.             return false;
  791.         }
  792.  
  793.         return true;
  794.     }
  795.  
  796.     /**
  797.      * Add Menu Item.
  798.      *
  799.      * @param   stdClass  $itemLanguage  Language Object
  800.      *
  801.      * @return  boolean 
  802.      *
  803.      * @since   3.2
  804.      */
  805.     public function addMenuItem($itemLanguage)
  806.     {
  807.         // Add Menu Item
  808.         $tableItem JTable::getInstance('Menu''MenusTable');
  809.  
  810.         $newlanguage new JLanguage($itemLanguage->languagefalse);
  811.         $newlanguage->load('com_languages'JPATH_ADMINISTRATOR$itemLanguage->languagetrue);
  812.         $title $newlanguage->_('COM_LANGUAGES_HOMEPAGE');
  813.         $alias 'home_' $itemLanguage->language;
  814.  
  815.         $menuItem array(
  816.             'id'           => 0,
  817.             'title'        => $title,
  818.             'alias'        => $alias,
  819.             'menutype'     => 'mainmenu-' strtolower($itemLanguage->language),
  820.             'type'         => 'component',
  821.             'link'         => 'index.php?option=com_content&view=featured',
  822.             'component_id' => 22,
  823.             'published'    => 1,
  824.             'parent_id'    => 1,
  825.             'level'        => 1,
  826.             'home'         => 1,
  827.             'params'       => '{"featured_categories":[""],"layout_type":"blog","num_leading_articles":"1",'
  828.                 . '"num_intro_articles":"3","num_columns":"3","num_links":"0","orderby_pri":"","orderby_sec":"front",'
  829.                 . '"order_date":"","multi_column_order":"1","show_pagination":"2","show_pagination_results":"1","show_noauth":"",'
  830.                 . '"article-allow_ratings":"","article-allow_comments":"","show_feed_link":"1","feed_summary":"",'
  831.                 . '"show_title":"","link_titles":"","show_intro":"","show_category":"","link_category":"",'
  832.                 . '"show_parent_category":"","link_parent_category":"","show_author":"","show_create_date":"",'
  833.                 . '"show_modify_date":"","show_publish_date":"","show_item_navigation":"","show_readmore":"",'
  834.                 . '"show_icons":"","show_print_icon":"","show_email_icon":"","show_hits":"","menu-anchor_title":"",'
  835.                 . '"menu-anchor_css":"","menu_image":"","show_page_heading":1,"page_title":"","page_heading":"",'
  836.                 . '"pageclass_sfx":"","menu-meta_description":"","menu-meta_keywords":"","robots":"","secure":0}',
  837.             'language'     => $itemLanguage->language
  838.         );
  839.  
  840.         // Bind the data.
  841.         if (!$tableItem->bind($menuItem))
  842.         {
  843.             return false;
  844.         }
  845.  
  846.         $tableItem->setLocation($menuItem['parent_id']'last-child');
  847.  
  848.         // Check the data.
  849.         if (!$tableItem->check())
  850.         {
  851.             return false;
  852.         }
  853.  
  854.         // Store the data.
  855.         if (!$tableItem->store())
  856.         {
  857.             return false;
  858.         }
  859.  
  860.         // Rebuild the tree path.
  861.         if (!$tableItem->rebuildPath($tableItem->id))
  862.         {
  863.             return false;
  864.         }
  865.  
  866.         return true;
  867.     }
  868.  
  869.     /**
  870.      * Add Module Menu
  871.      *
  872.      * @param   stdClass  $itemLanguage  Language Object
  873.      *
  874.      * @return  boolean 
  875.      *
  876.      * @since   3.2
  877.      */
  878.     public function addModuleMenu($itemLanguage)
  879.     {
  880.         $tableModule JTable::getInstance('Module''JTable');
  881.         $title 'Main menu ' $itemLanguage->language;
  882.  
  883.         $moduleData array(
  884.             'id'        => 0,
  885.             'title'     => $title,
  886.             'note'      => '',
  887.             'content'   => '',
  888.             'position'  => 'position-7',
  889.             'module'    => 'mod_menu',
  890.             'access'    => 1,
  891.             'showtitle' => 1,
  892.             'params'    => '{"menutype":"mainmenu-' strtolower($itemLanguage->language)
  893.                 . '","startLevel":"0","endLevel":"0","showAllChildren":"0","tag_id":"","class_sfx":"","window_open":"",'
  894.                 . '"layout":"","moduleclass_sfx":"_menu","cache":"1","cache_time":"900","cachemode":"itemid"}',
  895.             'client_id' => 0,
  896.             'language'  => $itemLanguage->language,
  897.             'published' => 1
  898.         );
  899.  
  900.         // Bind the data.
  901.         if (!$tableModule->bind($moduleData))
  902.         {
  903.             return false;
  904.         }
  905.  
  906.         // Check the data.
  907.         if (!$tableModule->check())
  908.         {
  909.             return false;
  910.         }
  911.  
  912.         // Store the data.
  913.         if (!$tableModule->store())
  914.         {
  915.             return false;
  916.         }
  917.  
  918.         return $this->addModuleInModuleMenu((int) $tableModule->id);
  919.     }
  920.  
  921.     /**
  922.      * Disable Default Main Menu Module
  923.      *
  924.      * @return  boolean 
  925.      *
  926.      * @since   3.2
  927.      */
  928.     public function disableModuleMainMenu()
  929.     {
  930.         // Create a new db object.
  931.         $db    JFactory::getDbo();
  932.         $query $db->getQuery(true);
  933.  
  934.         // Add Module in Module menus
  935.         $query
  936.             ->clear()
  937.             ->update($db->quoteName('#__modules'))
  938.             ->set($db->quoteName('published'' = 0')
  939.             ->where($db->quoteName('module'' = ' $db->quote('mod_menu'))
  940.             ->where($db->quoteName('language'' = ' $db->quote('*'))
  941.             ->where($db->quoteName('client_id'' = ' $db->quote('0'))
  942.             ->where($db->quoteName('position'' = ' $db->quote('position-7'));
  943.         $db->setQuery($query);
  944.  
  945.         if (!$db->execute())
  946.         {
  947.             return false;
  948.         }
  949.  
  950.         return true;
  951.     }
  952.  
  953.     /**
  954.      * Enable a module
  955.      *
  956.      * @param   string  $moduleName  The Name of the module to activate
  957.      *
  958.      * @return  boolean 
  959.      *
  960.      * @since   3.2
  961.      */
  962.     public function enableModule($moduleName)
  963.     {
  964.         // Create a new db object.
  965.         $db    JFactory::getDbo();
  966.         $query $db->getQuery(true);
  967.  
  968.         $query
  969.             ->clear()
  970.             ->update($db->quoteName('#__modules'))
  971.             ->set($db->quoteName('published'' = 1')
  972.             ->where($db->quoteName('module'' = ' $db->quote($moduleName));
  973.         $db->setQuery($query);
  974.  
  975.         if (!$db->execute())
  976.         {
  977.             return false;
  978.         }
  979.  
  980.         return true;
  981.     }
  982.  
  983.     /**
  984.      * Method create a category for a specific language
  985.      *
  986.      * @param   stdClass  $itemLanguage  Language Object
  987.      *
  988.      * @return  JTable Category Object
  989.      *
  990.      * @since   3.2
  991.      */
  992.     public function addCategory($itemLanguage)
  993.     {
  994.         $newlanguage new JLanguage($itemLanguage->languagefalse);
  995.         $newlanguage->load('joomla'JPATH_ADMINISTRATOR$itemLanguage->languagetrue);
  996.         $title $newlanguage->_('JCATEGORY');
  997.  
  998.         // Initialize a new category
  999.         $category                  JTable::getInstance('Category');
  1000.         $category->extension       'com_content';
  1001.         $category->title           $title ' (' strtolower($itemLanguage->language')';
  1002.         $category->description     '';
  1003.         $category->published       1;
  1004.         $category->access          1;
  1005.         $category->params          '{"target":"","image":""}';
  1006.         $category->metadata        '{"page_title":"","author":"","robots":""}';
  1007.         $category->created_time    JFactory::getDate()->toSql();
  1008.         $category->language        $itemLanguage->language;
  1009.  
  1010.         // Set the location in the tree
  1011.         $category->setLocation(1'last-child');
  1012.  
  1013.         // Check to make sure our data is valid
  1014.         if (!$category->check())
  1015.         {
  1016.             return false;
  1017.         }
  1018.  
  1019.         // Store the category
  1020.         if (!$category->store(true))
  1021.         {
  1022.             return false;
  1023.         }
  1024.  
  1025.         // Build the path for our category
  1026.         $category->rebuildPath($category->id);
  1027.  
  1028.         return $category;
  1029.     }
  1030.  
  1031.     /**
  1032.      * Create an article in a specific language
  1033.      *
  1034.      * @param   stdClass  $itemLanguage  Language Object
  1035.      * @param   int       $categoryId    The id of the category where we want to add the article
  1036.      *
  1037.      * @return  JTable Category Object
  1038.      *
  1039.      * @since   3.2
  1040.      */
  1041.     public function addArticle($itemLanguage$categoryId)
  1042.     {
  1043.         $db JFactory::getDbo();
  1044.  
  1045.         $newlanguage new JLanguage($itemLanguage->languagefalse);
  1046.         $newlanguage->load('plg_editors-xtd_article'JPATH_ADMINISTRATOR$itemLanguage->languagetrue);
  1047.         $title $newlanguage->_('PLG_ARTICLE_BUTTON_ARTICLE');
  1048.  
  1049.         $article                   JTable::getInstance('Content');
  1050.         $article->title            $title ' (' strtolower($itemLanguage->language')';
  1051.         $article->introtext        '<p>Lorem ipsum ad his scripta blandit partiendo, eum fastidii accumsan euripidis'
  1052.                                         . ' in, eum liber hendrerit an. Qui ut wisi vocibus suscipiantur, quo dicit'
  1053.                                         . ' ridens inciderint id. Quo mundi lobortis reformidans eu, legimus senserit'
  1054.                                         . 'definiebas an eos. Eu sit tincidunt incorrupte definitionem, vis mutat'
  1055.                                         . ' affert percipit cu, eirmod consectetuer signiferumque eu per. In usu latine'
  1056.                                         . 'equidem dolores. Quo no falli viris intellegam, ut fugit veritus placerat'
  1057.                                         . 'per. Ius id vidit volumus mandamus, vide veritus democritum te nec, ei eos'
  1058.                                         . 'debet libris consulatu.</p>';
  1059.         $article->state            1;
  1060.         $article->created          JFactory::getDate()->toSql();
  1061.         $article->created_by       $this->getAdminId();
  1062.         $article->created_by_alias 'Joomla';
  1063.         $article->publish_up       JFactory::getDate()->toSql();
  1064.         $article->publish_down     $db->getNullDate();
  1065.         $article->version          1;
  1066.         $article->catid            $categoryId;
  1067.         $article->metadata         '{"robots":"","author":"","rights":"","xreference":"","tags":null}';
  1068.         $article->language         $itemLanguage->language;
  1069.         $article->featured         1;
  1070.  
  1071.         // Check to make sure our data is valid
  1072.         if (!$article->check())
  1073.         {
  1074.             return false;
  1075.         }
  1076.  
  1077.         // Now store the category
  1078.         if (!$article->store(true))
  1079.         {
  1080.             return false;
  1081.         }
  1082.  
  1083.         // Get the new item ID
  1084.         $newId $article->get('id');
  1085.  
  1086.         $query $db->getQuery(true)
  1087.             ->insert($db->quoteName('#__content_frontpage'))
  1088.             ->values($newId ', 0');
  1089.  
  1090.         $db->setQuery($query);
  1091.  
  1092.         if (!$db->execute())
  1093.         {
  1094.             return false;
  1095.         }
  1096.  
  1097.         return true;
  1098.     }
  1099.  
  1100.     /**
  1101.      * Retrieve the admin user id.
  1102.      *
  1103.      * @return  int|boolOne Administrator ID
  1104.      *
  1105.      * @since   3.2
  1106.      */
  1107.     private function getAdminId()
  1108.     {
  1109.         if ($this->adminId)
  1110.         {
  1111.             // Return local cached admin ID
  1112.             return $this->adminId;
  1113.         }
  1114.  
  1115.         $db    JFactory::getDbo();
  1116.         $query $db->getQuery(true);
  1117.  
  1118.         // Select the required fields from the updates table
  1119.         $query
  1120.             ->clear()
  1121.             ->select('u.id')
  1122.             ->from('#__users as u')
  1123.             ->join('LEFT''#__user_usergroup_map AS map ON map.user_id = u.id')
  1124.             ->join('LEFT''#__usergroups AS g ON map.group_id = g.id')
  1125.             ->where('g.title = ' $db->q('Super Users'));
  1126.  
  1127.         $db->setQuery($query);
  1128.         $id $db->loadResult();
  1129.  
  1130.         if (!$id || $id instanceof Exception)
  1131.         {
  1132.             return false;
  1133.         }
  1134.  
  1135.         return $id;
  1136.     }
  1137. }

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