1 <?php
2 /**
3 * @package Joomla.Libraries
4 * @subpackage Helper
5 *
6 * @copyright Copyright (C) 2005 - 2017 Open Source Matters, Inc. All rights reserved.
7 * @license GNU General Public License version 2 or later; see LICENSE.txt
8 */
9
10 defined('JPATH_PLATFORM') or die;
11
12 /**
13 * Base Helper class.
14 *
15 * @since 3.2
16 */
17 class JHelper
18 {
19 /**
20 * Gets the current language
21 *
22 * @param boolean $detectBrowser Flag indicating whether to use the browser language as a fallback.
23 *
24 * @return string The language string
25 *
26 * @since 3.2
27 */
28 public function getCurrentLanguage($detectBrowser = true)
29 {
30 $app = JFactory::getApplication();
31 $langCode = $app->input->cookie->getString(JApplicationHelper::getHash('language'));
32
33 // No cookie - let's try to detect browser language or use site default
34 if (!$langCode)
35 {
36 if ($detectBrowser)
37 {
38 $langCode = JLanguageHelper::detectLanguage();
39 }
40 else
41 {
42 $langCode = JComponentHelper::getParams('com_languages')->get('site', 'en-GB');
43 }
44 }
45
46 return $langCode;
47 }
48
49 /**
50 * Gets the associated language ID
51 *
52 * @param string $langCode The language code to look up
53 *
54 * @return integer The language ID
55 *
56 * @since 3.2
57 */
58 public function getLanguageId($langCode)
59 {
60 $db = JFactory::getDbo();
61 $query = $db->getQuery(true)
62 ->select('lang_id')
63 ->from('#__languages')
64 ->where($db->quoteName('lang_code') . ' = ' . $db->quote($langCode));
65 $db->setQuery($query);
66
67 return $db->loadResult();
68 }
69
70 /**
71 * Gets a row of data from a table
72 *
73 * @param JTableInterface $table JTable instance for a row.
74 *
75 * @return array Associative array of all columns and values for a row in a table.
76 *
77 * @since 3.2
78 */
79 public function getRowData(JTableInterface $table)
80 {
81 $fields = $table->getFields();
82 $data = array();
83
84 foreach ($fields as &$field)
85 {
86 $columnName = $field->Field;
87 $value = $table->$columnName;
88 $data[$columnName] = $value;
89 }
90
91 return $data;
92 }
93
94 /**
95 * Method to get an object containing all of the table columns and values.
96 *
97 * @param JTableInterface $table JTable object.
98 *
99 * @return stdClass Contains all of the columns and values.
100 *
101 * @since 3.2
102 */
103 public function getDataObject(JTableInterface $table)
104 {
105 $fields = $table->getFields();
106 $dataObject = new stdClass;
107
108 foreach ($fields as $field)
109 {
110 $fieldName = $field->Field;
111 $dataObject->$fieldName = $table->get($fieldName);
112 }
113
114 return $dataObject;
115 }
116 }
117