/libraries/joomla/language/help.php

https://bitbucket.org/kraymitchell/fcd · PHP · 189 lines · 115 code · 24 blank · 50 comment · 14 complexity · d8ff50219fb82fde16351c914627f457 MD5 · raw file

  1. <?php
  2. /**
  3. * @package Joomla.Platform
  4. * @subpackage Language
  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
  8. */
  9. defined('JPATH_PLATFORM') or die;
  10. /**
  11. * Help system class
  12. *
  13. * @package Joomla.Platform
  14. * @subpackage Language
  15. * @since 11.1
  16. */
  17. class JHelp
  18. {
  19. /**
  20. * Create a URL for a given help key reference
  21. *
  22. * @param string $ref The name of the help screen (its key reference)
  23. * @param boolean $useComponent Use the help file in the component directory
  24. * @param string $override Use this URL instead of any other
  25. * @param string $component Name of component (or null for current component)
  26. *
  27. * @return string
  28. *
  29. * @since 11.1
  30. */
  31. public static function createURL($ref, $useComponent = false, $override = null, $component = null)
  32. {
  33. $local = false;
  34. $app = JFactory::getApplication();
  35. if (is_null($component))
  36. {
  37. $component = JApplicationHelper::getComponentName();
  38. }
  39. // Determine the location of the help file. At this stage the URL
  40. // can contain substitution codes that will be replaced later.
  41. if ($override)
  42. {
  43. $url = $override;
  44. }
  45. else
  46. {
  47. // Get the user help URL.
  48. $user = JFactory::getUser();
  49. $url = $user->getParam('helpsite');
  50. // If user hasn't specified a help URL, then get the global one.
  51. if ($url == '')
  52. {
  53. $url = $app->getCfg('helpurl');
  54. }
  55. // Component help URL overrides user and global.
  56. if ($useComponent)
  57. {
  58. // Look for help URL in component parameters.
  59. $params = JComponentHelper::getParams($component);
  60. $url = $params->get('helpURL');
  61. if ($url == '')
  62. {
  63. $local = true;
  64. $url = 'components/{component}/help/{language}/{keyref}';
  65. }
  66. }
  67. // Set up a local help URL.
  68. if (!$url)
  69. {
  70. $local = true;
  71. $url = 'help/{language}/{keyref}';
  72. }
  73. }
  74. // If the URL is local then make sure we have a valid file extension on the URL.
  75. if ($local)
  76. {
  77. if (!preg_match('#\.html$|\.xml$#i', $ref))
  78. {
  79. $url .= '.html';
  80. }
  81. }
  82. /*
  83. * Replace substitution codes in the URL.
  84. */
  85. $lang = JFactory::getLanguage();
  86. $version = new JVersion;
  87. $jver = explode('.', $version->getShortVersion());
  88. $jlang = explode('-', $lang->getTag());
  89. $debug = $lang->setDebug(false);
  90. $keyref = JText::_($ref);
  91. $lang->setDebug($debug);
  92. // Replace substitution codes in help URL.
  93. $search = array('{app}', // Application name (eg. 'Administrator')
  94. '{component}', // Component name (eg. 'com_content')
  95. '{keyref}', // Help screen key reference
  96. '{language}', // Full language code (eg. 'en-GB')
  97. '{langcode}', // Short language code (eg. 'en')
  98. '{langregion}', // Region code (eg. 'GB')
  99. '{major}', // Joomla major version number
  100. '{minor}', // Joomla minor version number
  101. '{maintenance}'// Joomla maintenance version number
  102. );
  103. $replace = array($app->getName(), // {app}
  104. $component, // {component}
  105. $keyref, // {keyref}
  106. $lang->getTag(), // {language}
  107. $jlang[0], // {langcode}
  108. $jlang[1], // {langregion}
  109. $jver[0], // {major}
  110. $jver[1], // {minor}
  111. $jver[2]// {maintenance}
  112. );
  113. // If the help file is local then check it exists.
  114. // If it doesn't then fallback to English.
  115. if ($local)
  116. {
  117. $try = str_replace($search, $replace, $url);
  118. jimport('joomla.filesystem.file');
  119. if (!JFile::exists(JPATH_BASE . '/' . $try))
  120. {
  121. $replace[3] = 'en-GB';
  122. $replace[4] = 'en';
  123. $replace[5] = 'GB';
  124. }
  125. }
  126. $url = str_replace($search, $replace, $url);
  127. return $url;
  128. }
  129. /**
  130. * Builds a list of the help sites which can be used in a select option.
  131. *
  132. * @param string $pathToXml Path to an XML file.
  133. * @param string $selected Language tag to select (if exists).
  134. *
  135. * @return array An array of arrays (text, value, selected).
  136. *
  137. * @since 11.1
  138. */
  139. public static function createSiteList($pathToXml, $selected = null)
  140. {
  141. $list = array();
  142. $xml = false;
  143. if (!empty($pathToXml))
  144. {
  145. $xml = JFactory::getXML($pathToXml);
  146. }
  147. if (!$xml)
  148. {
  149. $option['text'] = 'English (GB) help.joomla.org';
  150. $option['value'] = 'http://help.joomla.org';
  151. $list[] = $option;
  152. }
  153. else
  154. {
  155. $option = array();
  156. foreach ($xml->sites->site as $site)
  157. {
  158. $option['text'] = (string) $site;
  159. $option['value'] = (string) $site->attributes()->url;
  160. $list[] = $option;
  161. }
  162. }
  163. return $list;
  164. }
  165. }