Instead of using $post = JRequest::get('post')
to get all $_POST, you should use $post = JFactory::getApplication()->input->getArray(array())
in Joomla 3.X.
Category / PHP
Get Super users in Joomla
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
/** * Returns the Super Users email information. If you provide a comma separated $email list * we will check that these emails do belong to Super Users and that they have not blocked * system emails. * * @param null|string $email A list of Super Users to email * * @return array The list of Super User emails * * @since 3.5 */ private function getSuperUsers($email = null) { // Get a reference to the database object $db = JFactory::getDBO(); // Convert the email list to an array if (!empty($email)) { $temp = explode(',', $email); $emails = array(); foreach ($temp as $entry) { $entry = trim($entry); $emails[] = $db->q($entry); } $emails = array_unique($emails); } else { $emails = array(); } // Get a list of groups which have Super User privileges $ret = array(); try { $query = $db->getQuery(true) ->select($db->qn('rules')) ->from($db->qn('#__assets')) ->where($db->qn('parent_id') . ' = ' . $db->q(0)); $db->setQuery($query, 0, 1); $rulesJSON = $db->loadResult(); $rules = json_decode($rulesJSON, true); $rawGroups = $rules['core.admin']; $groups = array(); if (empty($rawGroups)) { return $ret; } foreach ($rawGroups as $g => $enabled) { if ($enabled) { $groups[] = $db->q($g); } } if (empty($groups)) { return $ret; } } catch (Exception $exc) { return $ret; } // Get the user IDs of users belonging to the SA groups try { $query = $db->getQuery(true) ->select($db->qn('user_id')) ->from($db->qn('#__user_usergroup_map')) ->where($db->qn('group_id') . ' IN(' . implode(',', $groups) . ')'); $db->setQuery($query); $rawUserIDs = $db->loadColumn(0); if (empty($rawUserIDs)) { return $ret; } $userIDs = array(); foreach ($rawUserIDs as $id) { $userIDs[] = $db->q($id); } } catch (Exception $exc) { return $ret; } // Get the user information for the Super Administrator users try { $query = $db->getQuery(true) ->select( array( $db->qn('id'), $db->qn('username'), $db->qn('email'), ) )->from($db->qn('#__users')) ->where($db->qn('id') . ' IN(' . implode(',', $userIDs) . ')') ->where($db->qn('sendEmail') . ' = ' . $db->q('1')); if (!empty($emails)) { $query->where($db->qn('email') . 'IN(' . implode(',', $emails) . ')'); } $db->setQuery($query); $ret = $db->loadObjectList(); } catch (Exception $exc) { return $ret; } return $ret; } |
Joomla Get the full current URI
1 2 3 4 5 6 |
// Get the full current URI. $uri = JUri::getInstance(); $current = rawurldecode($uri->toString(array('scheme', 'host', 'port', 'path', 'query', 'fragment'))); // the server-relative URL $currRel = rawurldecode($uri->toString(array('path', 'query', 'fragment'))); |
Joomla Cookie API
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
// Get input cookie object $inputCookie = JFactory::getApplication()->input->cookie; // Get cookie data $value = $inputCookie->get($name = 'myCookie', $defaultValue = null); // Check that cookie exists $cookieExists = ($value === null); // Set cookie data $inputCookie->set($name = 'myCookie', $value = '123', $expire = 0); // Remove cookie $inputCookie->set('myCookie', null, time() - 1); |
Some rules about $expire value
its a Unix tinestamp in seconds, like return value of time().
$expire == 0: cookie lifetime is of browser session.
$expire < time(): cookie is being deleted (expire set to past). You could remove cookie by setting it's value to null, but apparently IE fails to do so.
Notes
Keep in mind that cookies should be set before headers are sent (usually before output is echoed).
Cookie key and value should be properly escaped
Non-string values
When serializing the value on set (like json_encode($dataNode)), remember to use proper filter to retrieve it later on. Default is cmd, which filters out pretty much anything but a-Z, 0-9 and cracks JSON structure.
1 2 3 4 5 6 7 8 |
// Get cookie data $encodedString = $inputCookie->get('myCookie', null, $filter = 'string'); // Decode $values = json_decode($encodedString); // Encode and Set $inputCookie->set('myCookie', json_encode($values)); |
Rererences
Joomla CMS github repository: JInputCookie::set (very well documented)
php docs: php.net/setcookie (developer experiences)
Wikipedia: HTTP Cookies (theory)
Joomla Date/Timezone
JFactory::getDate()
will always stay in UTC. This is good for storing dates into the database and other calculations.
For displaying a date to a user, it is recommended to use JHtml::date()
. This will automatically use your timezone setting.
JHtml::date('now', 'Y-m-d H:i:s');
For more information and to see the code behind this function: http://docs.joomla.org/API16:JHtml/date
Add Additional Parameters in Pagination
We have to initialize JPagination object first
1 2 |
$pagination = new JPagination; $pagination->setAdditionalUrlParam('name','ronniee'); |
Date Difference for PHP 5.2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?php date_default_timezone_set('UTC'); function custom_date_diff($date1, $date2) { $current = $date1; $datetime2 = date_create($date2); $count = 0; while(date_create($current) < $datetime2){ $current = gmdate("Y-m-d", strtotime("+1 day", strtotime($current))); $count++; } return $count; } echo (custom_date_diff('2014-05-10 00:00:00', '2014-05-15 00:00:00')." days left"); ?> |
Add New Parameter in URL
1 2 3 |
$url =& JURI::getInstance(); $url->setVar( 'test', 1); print $url; |
Load Language file in Joomla
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
<?php $lang = JFactory::getLanguage(); $filename = 'plg_content_vote'; $lang->load($filename, JPATH_ADMINISTRATOR, 'en-GB', true); $lang->load($filename, JPATH_ADMINISTRATOR, $lang->getDefault(), true); Reference: libraries\joomla\language\language.php /** * Loads a single language file and appends the results to the existing strings * * @param string $extension The extension for which a language file should be loaded. * @param string $basePath The basepath to use. * @param string $lang The language to load, default null for the current language. * @param boolean $reload Flag that will force a language to be reloaded if set to true. * @param boolean $default Flag that force the default language to be loaded if the current does not exist. * * @return boolean True if the file has successfully loaded. * * @since 11.1 */ public function load($extension = 'joomla', $basePath = JPATH_BASE, $lang = null, $reload = false, $default = true) { if (!$lang) { $lang = $this->lang; } $path = self::getLanguagePath($basePath, $lang); $internal = $extension == 'joomla' || $extension == ''; $filename = $internal ? $lang : $lang . '.' . $extension; $filename = "$path/$filename.ini"; $result = false; if (isset($this->paths[$extension][$filename]) && !$reload) { // This file has already been tested for loading. $result = $this->paths[$extension][$filename]; } else { // Load the language file $result = $this->loadLanguage($filename, $extension); // Check whether there was a problem with loading the file if ($result === false && $default) { // No strings, so either file doesn't exist or the file is invalid $oldFilename = $filename; // Check the standard file name $path = self::getLanguagePath($basePath, $this->default); $filename = $internal ? $this->default : $this->default . '.' . $extension; $filename = "$path/$filename.ini"; // If the one we tried is different than the new name, try again if ($oldFilename != $filename) { $result = $this->loadLanguage($filename, $extension, false); } } } return $result; } ?> |
Read XLS/CSV file by PHPExcel
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
<?php /** Include PHPExcel_IOFactory */ require_once (JPATH_COMPONENT_ADMINISTRATOR.DS.'helpers/PHPExcel/Classes/PHPExcel/IOFactory.php'); // Read XLS file $objPHPExcel = PHPExcel_IOFactory::load($filepath); $rows = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true); // Read CSV file $inputFileType = 'CSV'; $inputFileName = $filepath; $objReader = PHPExcel_IOFactory::createReader($inputFileType); $objPHPExcel = $objReader->load($inputFileName); $worksheet = $objPHPExcel->getActiveSheet(); foreach ($worksheet->getRowIterator() as $row) { echo 'Row number: ' . $row->getRowIndex() . "\r\n"; $cellIterator = $row->getCellIterator(); $cellIterator->setIterateOnlyExistingCells(false); // Loop all cells, even if it is not set foreach ($cellIterator as $cell) { if (!is_null($cell)) { echo 'Cell: ' . $cell->getCoordinate() . ' - ' . $cell->getValue() . "\r\n"; } } } OR $objWorksheet = $objPHPExcel->getActiveSheet(); $highestRow = $objWorksheet->getHighestRow(); $highestColumn = $objWorksheet->getHighestColumn(); $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn); //read from file $rows = array(); for ($row = 1; $row <= $highestRow; ++$row) { $file_data = array(); for ($col = 0; $col < $highestColumnIndex; ++$col) { $value=$objWorksheet->getCellByColumnAndRow($col, $row)->getValue(); $file_data[$col]=trim($value); } $rows[] = $file_data; } ?> |