New features to this module #1

Closed
wants to merge 6 commits into
from
View
@@ -2,4 +2,5 @@
define('MOBILE_DIR', 'mobile');
Object::add_extension('SiteConfig', 'MobileSiteConfigExtension');
-Object::add_extension('ContentController', 'MobileSiteControllerExtension');
+Object::add_extension('ContentController', 'MobileSiteControllerExtension');
+Object::add_extension('SiteTree', 'MobileSiteAccessModifier');
@@ -19,8 +19,12 @@ public static function is_android() {
return (stripos($_SERVER['HTTP_USER_AGENT'], 'android') !== false) ? true : false;
}
- public static function is_iphone() {
+ public static function is_iphone() {
return (preg_match('/(ipod|iphone)/i', $_SERVER['HTTP_USER_AGENT'])) ? true : false;
+ }
+
+ public static function is_ipad() {
+ return (stripos($_SERVER['HTTP_USER_AGENT'], 'ipad') !== false) ? true : false;
}
public static function is_opera_mini() {
@@ -45,7 +49,7 @@ public static function is_windows() {
*
* @return bool
*/
- public static function is_mobile() {
+ public static function is_mobile() {
$isMobile = false;
$agent = $_SERVER['HTTP_USER_AGENT'];
$accept = isset($_SERVER['HTTP_ACCEPT']) ? $_SERVER['HTTP_ACCEPT'] : '';
@@ -54,6 +58,9 @@ public static function is_mobile() {
case(self::is_iphone()):
$isMobile = true;
break;
+ case(self::is_ipad()):
+ $isMobile = true;
+ break;
case(self::is_android()):
$isMobile = true;
break;
@@ -0,0 +1,46 @@
+<?php
+
+class MobileDeviceTheme extends DataObject {
+
+ static $db = array(
+ 'Device' => 'Varchar',
+ 'Theme' => 'Varchar'
+ );
+
+ static $has_one = array(
+ 'Configuration' => 'SiteConfig'
+ );
+
+
+ /**
+ *
+ * @return FieldSet
+ */
+ function getPopupFields(){
+
+ $siteConfig = SiteConfig::current_site_config();
+
+ return new FieldSet(
+ new DropdownField('Device', _t('MobileSiteConfig.MOBILETHEME', 'Mobile device'), array(
+ 'android' => 'android',
+ 'iphone' => 'iphone',
+ 'ipad' => 'ipad',
+ 'blackberry' => 'blackberry',
+ 'palm' => 'palm'
+ )),
+ new DropdownField('Theme', _t('MobileSiteConfig.MOBILETHEME', 'Mobile theme'), $siteConfig->getAvailableThemes())
+ );
+ }
+
+ /**
+ *
+ * @return void
+ */
+ function onBeforeWrite(){
+ parent::onBeforeWrite();
+ $siteConfig = SiteConfig::current_site_config();
+ $this->ConfigurationID = $siteConfig->ID;
+ }
+
+
+}
@@ -0,0 +1,72 @@
+<?php
+
+class MobileSiteAccessModifier extends DataObjectDecorator{
+
+ /**
+ * modify the CanViewType field to hold additional viewer options
+ * @return Array
+ */
+ function extraStatics(){
+ return array(
+ "db" => array(
+ "CanViewType" => "Enum('Anyone, LoggedInUsers, OnlyTheseUsers, Inherit,iPhoneUsers,iPadUsers,MobileUsers,AndriodUsers,DesktopUsers', 'Inherit')"
+ )
+ );
+ }
+
+
+
+ function updateCMSFields($fields){
+ $fields->removeFieldFromTab("Root.Access", "CanViewType");
+ $viewersOptionsField = new OptionsetField(
+ "CanViewType",
+ ""
+ );
+ $viewersOptionsSource = array();
+ $viewersOptionsSource["Inherit"] = _t('SiteTree.INHERIT', "Inherit from parent page");
+ $viewersOptionsSource["Anyone"] = _t('SiteTree.ACCESSANYONE', "Anyone");
+ $viewersOptionsSource["LoggedInUsers"] = _t('SiteTree.ACCESSLOGGEDIN', "Logged-in users");
+
+ $viewersOptionsSource["DesktopUsers"] = _t('SiteTree.ACCESSONLYCOMPUTERS', "Users viewing the site using computers");
+ $viewersOptionsSource["MobileUsers"] = _t('SiteTree.ACCESSONLYMOBILES', "Users viewing the site using mobile devices");
+ $viewersOptionsSource["iPhoneUsers"] = _t('SiteTree.ACCESSONLYIPHONES', "Users viewing the site using iPhones");
+ $viewersOptionsSource["iPadUsers"] = _t('SiteTree.ACCESSONLYIPADS', "Users viewing the site using iPads");
+ $viewersOptionsSource["AndriodUsers"] = _t('SiteTree.ACCESSONLYANDRIOD', "Users viewing the site using Android devices");
+
+
+ $viewersOptionsSource["OnlyTheseUsers"] = _t('SiteTree.ACCESSONLYTHESE', "Only these people (choose from list)");
+
+
+
+ $viewersOptionsField->setSource($viewersOptionsSource);
+ $fields->addFieldToTab("Root.Access", $viewersOptionsField, "WhoCanEditHeader");
+ }
+
+
+ /**
+ * check whether the user can view the page with respect to the
+ * device he is using to browse
+ * @return boolean
+ */
+ function canView($member = null){
+ $canView = null;
+ $siteTree = $this->owner;
+ if(0 == strcmp($siteTree->CanViewType, 'DesktopUsers') && !MobileBrowserDetector::is_mobile()){
+ $canView = true;
+ }
+ if(0 == strcmp($siteTree->CanViewType, 'iPhoneUsers') && MobileBrowserDetector::is_iphone()){
+ $canView = true;
+ }
+ if(0 == strcmp($siteTree->CanViewType, 'iPadUsers') && MobileBrowserDetector::is_ipad()){
+ $canView = true;
+ }
+ if(0 == strcmp($siteTree->CanViewType, 'AndriodUsers') && MobileBrowserDetector::is_andriod()){
+ $canView = true;
+ }
+ if(0 == strcmp($siteTree->CanViewType, 'MobileUsers') && MobileBrowserDetector::is_mobile()){
+ $canView = true;
+ }
+ return $canView;
+ }
+
+}
@@ -44,6 +44,9 @@ public function extraStatics() {
'FullSiteDomain' => 'http://' . $_SERVER['HTTP_HOST'],
'MobileTheme' => 'blackcandymobile',
'MobileSiteType' => 'Disabled'
+ ),
+ 'has_many' => array(
+ 'Themes' => 'MobileDeviceTheme'
)
);
}
@@ -155,7 +158,14 @@ public function updateCMSFields(FieldSet $fields) {
new LiteralField('MobileDomainHelpText', '<label class="helpText">' . _t('MobileSiteConfig.MOBILEDOMAINHELP', '(e.g. "mobile.mysite.com"). Please make sure you point the given domain to this site') . '</label>'),
new TextField('FullSiteDomain', _t('MobileSiteConfig.FULLSITEDOMAIN', 'Full site domain')),
new LiteralField('FullSiteDomainHelpText', '<label class="helpText">' . _t('MobileSiteConfig.FULLSITEDOMAINHELP', '(e.g. "mysite.com"). This usually doesn\'t need to be changed') . '</label>'),
- new DropdownField('MobileTheme', _t('MobileSiteConfig.MOBILETHEME', 'Mobile theme'), $this->owner->getAvailableThemes(), '', null, _t('SiteConfig.DEFAULTTHEME', '(Use default theme)'))
+ new DropdownField('MobileTheme', _t('MobileSiteConfig.MOBILETHEME', 'Mobile theme'), $this->owner->getAvailableThemes(), '', null, _t('SiteConfig.DEFAULTTHEME', '(Use default theme)')),
+
+ new LiteralField('MultipleThemes', '<label class="helpText">' . _t('MobileSiteConfig.MULTIPLETHEMES', 'If you want to add themes specifically for different mobile devices, please use the table below. Just leave it if you are using only one mobile theme for every device.') . '</label>'),
+
+ new ComplexTableField($this, 'Themes', 'MobileDeviceTheme', array(
+ 'Device' => 'Device',
+ 'Theme' => 'Theme'
+ ), 'getPopupFields')
)
);
}
@@ -33,21 +33,48 @@ public function onAfterInit() {
} elseif(!empty($_COOKIE['fullSite'])) {
return; // nothing more to be done
}
-
+
// If the user requested the mobile domain, set the right theme
- if($this->onMobileDomain()) {
- SSViewer::set_theme($config->MobileTheme);
+ if($this->onMobileDomain()) {
+ SSViewer::set_theme($this->getSuitableTheme());
}
// User just wants to see a theme, but no redirect occurs
- if(MobileBrowserDetector::is_mobile() && $config->MobileSiteType == 'MobileThemeOnly') {
- SSViewer::set_theme($config->MobileTheme);
+ if(MobileBrowserDetector::is_mobile() && $config->MobileSiteType == 'MobileThemeOnly') {
+ SSViewer::set_theme($this->getSuitableTheme());
}
// If on a mobile device, but not on the mobile domain and has been setup for redirection
if(!$this->onMobileDomain() && MobileBrowserDetector::is_mobile() && $config->MobileSiteType == 'RedirectToDomain') {
return $this->owner->redirect($config->MobileDomain);
}
+ }
+
+ /**
+ * Return the correct theme according to the device
+ * $return string
+ */
+ function getSuitableTheme(){
+ $config = SiteConfig::current_site_config();
+ $device = '';
+ if(MobileBrowserDetector::is_iphone()){
+ $device = 'iphone';
+ }elseif(MobileBrowserDetector::is_ipad()){
+ $device = 'ipad';
+ }elseif(MobileBrowserDetector::is_android()){
+ $device = 'android';
+ }elseif(MobileBrowserDetector::is_blackberry()){
+ $device = 'blackberry';
+ }
+ elseif(MobileBrowserDetector::is_palm()){
+ $device = 'palm';
+ }
+
+ if(0 != strcmp($device, '') && $theme = DataObject::get_one('MobileDeviceTheme', "Device = '{$device}' AND ConfigurationID = {$config->ID}")){
+ return $theme->Theme;
+ }else{
+ return $config->MobileTheme;
+ }
}
/**