The Rule form: Advanced PHP

(1 vote, average 5.00 out of 5)

8-advanced-php

The PHP box

Here’s where you can code your own PHP rules.

THE BASICS:

PHP rules can return the following values:

  • return true; (makes the rule SUCCEED)
  • return false; (makes the rule FAIL)
  • Joomla 1.5: return "templatename"; (makes the rule SUCCEED, sets the given template as the default, and stops rule processing immediately)
  • Joomla 1.5: return "templatename:continue"; (makes the rule SUCCEED, sets the given template as the default, and allows processing to continue to the next rule)
  • Joomla 1.7: return "style_id"; (use a numeric style id. Makes the rule SUCCEED, sets the given template style as the default, and stops rule processing immediately)
  • Joomla 1.7: return "style_id:continue"; (use a numeric style id. Makes the rule SUCCEED, sets the given template style as the default, and allows processing to continue to the next rule)
  • return null; (or no return value) (uses the Default PHP return value to return true or false, making the rule SUCCEED or FAIL)
If you "comment out" all the PHP in the box, watch for the default return value. If it’s set to false, then your entire rule will fail because there’s no return value, and the default is set to false. In that case, set the default return value to true so that the rest of the rule continues to work.

Resources and PHP hints

  • JomGenius support is built-in, and covers a myriad of common (and not so common) scenarios in Joomla. $core_genius, $menu_genius and $content_genius are already defined and waiting for you to query them...
  • For getting hold of URL parameters, take advantage of the extra safety of the JRequest object rather than directly referencing $_GET and $_POST.
  • Most of the MetaMod Recipes on this site also work in Chameleon (+Lite) with little modification.
    • ... instead of returning a module ID at the end of the PHP, return a true or false or a template name (Joomla 1.5) or template style id (Joomla 1.7)

VARIABLES: These variables pre-defined and ready for use.

  • $db is the Joomla Database object. Learn how to use it here.
  • $fromCountryId - the upper-case 2-letter ISO country code (e.g. GB, US, FR, DE) 
  • $fromCountryName - the official ISO country name
  • $geoip - if you have enabled GeoLiteCity or GeoIPCity, a record containing the following items:
    • $geoip->country_name - full country name, as above
    • $geoip->country_code - 2-letter ISO country code, as above
    • $geoip->country_code3 - 3-letter ISO country code (e.g. GBR, USA, FRA, DEU)
    • $geoip->region - 2-letter code. For US/Canada, ISO-3166-2 code for the state/province name, e.g. "GA" (Georgia, USA). Outside of the US and Canada, FIPS 10-4 code, e.g. "M9" (Staffordshire, UK)
    • $geoip->city - full city name
    • $geoip->postal_code - For US, Zipcodes, for Canada, postal codes. Available for about 56% of US GeoIP Records. More info.
    • $geoip->latitude
    • $geoip->longitude
    • $geoip->dma_code - 3-digit DMA/Metro code (US only)
    • $geoip->area_code - 3-digit telephone prefix (US Only)
  • $Itemid - the Itemid of the main component on the page
  • $option - the option of the main component on the page (e.g. com content)
  • $view - the view of the main component on the page (e.g. article)
  • $id - the id of the item in the main component on the page (e.g. 24:content-layouts)
  • $language - a lower-case language code controlled by settings in the Details panel. By default this returns the default language of the web visitor’s browser, but can alternatively return the language code of the Joomla front-end, or intelligently find the best match between a user’s browser languages and a list of languages that you provide. Typical language strings returned include: en, en-gb, en-us, fr, de and many others.
  • $language_code - the 2-letter language code without region (lower case) e.g. en
  • $language_region - if it exists, the 2-letter region code (lower case). e.g. if $language == "en-us", then $language_code == "en" and $language_region == "us". Having them in separate variables like this makes it easier to put into Chameleon rules.
  • $user - information about the user, if they are logged in...
    • $user->id - If 0, the user is not logged in
    • $user->name
    • $user->username
    • $user->email
    • $user->usertype - e.g. "" or "Public Frontend" means not logged in (test for both), otherwise "Registered", "Author", "Editor", "Publisher", "Manager", "Administrator" or "Super Administrator"
    • $user->registerDate - e.g. "2007-05-17 01:25:52"
    • $user->lastvisitDate - e.g. "2007-11-02 18:51:29"

CONSTANTS: These constants can be used in your rules just like variables, except they don’t need a "$" in front of them.

  • Time and Date related. These constants are calculated according to the time zone set in the time zone drop-down in Chameleon Parameters. If you choose your nearest region/city with the time zone drop-down, then the times and dates also respect daylight savings time.
    • MM_DAY_OF_WEEK – 0=Sunday, 6=Saturday
    • MM_DAY_OF_MONTH – 1 to 31
    • MM_MONTH – 1-12
    • MM_YEAR – YYYY format
    • MM_HOUR – 0-23
    • MM_MINUTE – 0-59
    • MM_SECOND – 0-59
    • MM_TIME – this is presented as a 24-hour clock number, including seconds, but without “:” characters. e.g. 10:11:59 AM = “101159”, 15:29 (3:29 PM) = “152900”. This makes it easy to use a template between two times of day, e.g. if (MM_TIME >= 101159 and MM_TIME <= 152900) return 101;
    • MM_DATE – this is presented as a YYYYMMDD number e.g. 25th Dec 2008 = 20081225. This makes it easy to use a template between two dates, e.g. between 1st and 24th May 2009 (inclusive): if (MM_DATE >= 20090501 and MM_DATE <= 20090524) return "ja_purity";
  • User/group related. These make it as easy as possible to make rules based on what group a logged-in user is a member of.
    • MM_LOGGED_IN – true if the user is logged in, no matter what group thay are part of
    • MM_NOT_LOGGED_IN – true if the user is not logged in
    • MM_USER_REGISTERED / MM_USER_NOT_REGISTERED – true if the user is logged in and a member (or NOT) of the “Registered” group
    • MM_USER_AUTHOR / MM_USER_NOT_AUTHOR – true if the user is logged in and a member (or NOT) of the “Author” group
    • MM_USER_EDITOR / MM_USER_NOT_EDITOR – true if the user is logged in and a member (or NOT) of the “Editor” group
    • MM_USER_PUBLISHER / MM_USER_NOT_PUBLISHER – true if the user is logged in and a member (or NOT) of the “Publisher” group
    • MM_USER_MANAGER / MM_USER_NOT_MANAGER – true if the user is logged in and a member (or NOT) of the “Manager” group
    • MM_USER_ADMINISTRATOR / MM_USER_NOT_ADMINISTRATOR – true if the user is logged in and a member (or NOT) of the “Administrator” group
    • MM_USER_SUPER_ADMINISTRATOR / MM_USER_NOT_SUPER_ADMINISTRATOR – true if the user is logged in and a member (or NOT) of the “Super Administrator” group
Last Updated on Tuesday, 09 August 2011 00:45