We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 50574
    • 70 Posts
    I'm in the process of adding a <tablet> tag into the mobiledetection plugin. As Zuriel mentions in this post http://forums.modx.com/thread/?thread=39267&page=1 "in fact I see a new tier "detect_tier_tablet" so maybe i’ll make a new tag like <tablet> and you can put tablet code in there."

    My code is:
    <?php
    /*------------------ OTHER FUNCTIONS------------------------------------------*/
    function store_user_prefs($x, $usecookie) {
    	if ($usecookie == 'yes') {
    		setcookie('browser',$x,time()+604800, "/", "", 0);
    	}
    }
    //-------------------------------------
    function get_user_prefs() {
    	return $_COOKIE['browser'];
    }
    //-------------------------------------
    function clear_user_prefs() {
    	if ($usecookie == 'yes') {
    		setcookie('browser','',time()-3600, "/", "", 0);
    	}
    }
    //-------------------------------------
    function delete_template_nodes($delete_node,$preserve_node) {
    
    	global $modx; // Without this, the $modx var is out of scope.
    	$delete_node = preg_quote($delete_node);
    	$pattern = '/\<'.$delete_node.'\>(.*)\<\/'.$delete_node.'\>/Usi';
    	$modx->resource->_output = preg_replace($pattern,'',$modx->resource->_output);
    	$preserve_node = preg_quote($preserve_node);
    	$pattern = '/\<'.$preserve_node.'\>/Usi';
    	$modx->resource->_output = preg_replace($pattern,'',$modx->resource->_output);
    	$pattern = '/\<\/'.$preserve_node.'\>/Usi';
    	$modx->resource->_output = preg_replace($pattern,'',$modx->resource->_output);
    }
    
    /*---------------------- ERROR CHECKING --------------------------------------*/
    // http://wiki.modxcms.com/index.php/API:logEvent
    if ($modx->event->name != 'OnWebPagePrerender') { 
    	$msg = 'Mobile Template Switcher: Plugin must be triggered by the OnWebPagePrerender event.  Instead it was triggered by '.$modx->event->name;
    	$modx->logEvent(3, 3, $msg, 'Mobile Detection');
    	return; 
    };
    
    $msg = '';
    // warn if any of those are unset
    if ( !isset($mobile_node) || $mobile_node == '') {
    	$msg .= 'Configuration error: Mobile node name not defined. Default value used. ';
    	$mobile_node = 'mobile';
    }
    if ( !isset($tablet_node) || $tablet_node == '') {
    	$msg .= 'Configuration error: Tablet node name not defined. Default value used. ';
    	$tablet_node = 'tablet';
    }
    if ( !isset($standard_node) || $standard_node == '' ) {
    	$msg .= 'Configuration error: Standard node name not defined. Default value used. ';
    	$standard_node = 'standard';
    }
    if ( !isset($force_browser_detect) || $force_browser_detect == '') {
    	$msg .= 'Configuration error: Force browser detect URL parameter not defined. Default value used. ';
    	$force_browser_detect = 'detect';
    }
    if ($msg != '') {
    	$modx->logEvent(3, 2, $msg,'Mobile Detection'); 
    }
    
    // $mobile_node, $standard_node, $force_browser_detect MUST be distinct
    // this is a cheap trick... using PHP hash to "count" distinct values.
    $hash[$mobile_node] = 1;
    $hash[$tablet_node] = 1;
    $hash[$standard_node] = 1;
    $hash[$force_browser_detect] = 1;
    if (count($hash) < 4) {
    	$msg = 'Configuration error: $mobile_node, $tablet_node, $standard_node, $force_browser_detect MUST be distinct values.';
    	$modx->logEvent(3, 3, $msg,'Mobile Detection'); 
    	return;
    }
    
    
    /*----------- Here begins the logical block ----------------------------------*/
    
    
    /* include the mobileESP detection script
    The latest copy can always be found at:  "http://mobileesp.googlecode.com/svn/PHP/mdetect.php"
    */
    include("assets/components/mobiledetection/mdetect.php");
    
    //Instantiate the object to do our testing with.
    $uagent_obj = new uagent_info();
    $var = $_GET[$force_browser_variable];
    if($var == $force_browser_tablet){
    	delete_template_nodes($standard_node, $tablet_node, $mobile_node);
    	store_user_prefs($tablet_node, $usecookie); 
    }else if($var == $force_browser_mobile){
    	delete_template_nodes($standard_node, $tablet_node, $mobile_node);
    	store_user_prefs($mobile_node, $usecookie); 
    }else if ($var == $force_browser_standard){
    	store_user_prefs($standard_node, $usecookie);
    	delete_template_nodes($mobile_node, $tablet_node, $standard_node);
    }else if ($var == $force_browser_detect){
    		clear_user_prefs();
    		//Detect iPhone
    		if (($uagent_obj->DetectTierIphone() == $uagent_obj->true) && ($iphone == 1)){
    		delete_template_nodes($standard_node, $tablet_node, $mobile_node);
    		store_user_prefs($mobile_node, $usecookie);
    		//Detect Tablet
    		}else if (($uagent_obj->DetectTierTablet() == $uagent_obj->true) && ($tablet == 1)){
    		delete_template_nodes($standard_node, $tablet_node, $mobile_node);
    		store_user_prefs($tablet_node, $usecookie);
    		//Detect iPad
    		} else if (($uagent_obj->DetectIpad() == $uagent_obj->true) && ($ipad == 1)){
    		delete_template_nodes($standard_node, $tablet_node, $mobile_node);
    		store_user_prefs($tablet_node, $usecookie);
    		//Detect Tier Rich Css
    		} else if (($uagent_obj->DetectTierRichCss() == $uagent_obj->true) && ($rich_css == 1)){
    		delete_template_nodes($standard_node, $tablet_node, $mobile_node);
    		store_user_prefs($mobile_node, $usecookie);
    		//Detect All Other Mobile Devices
    		} else if (($uagent_obj->DetectTierOtherPhones() == $uagent_obj->true) && ($all_other_mobile == 1)){
    		delete_template_nodes($standard_node, $tablet_node, $mobile_node);
    		store_user_prefs($mobile_node, $usecookie);
    		//Else it's a regular PC browser -- send to regular desktop site
    		} else { store_user_prefs($standard_node, $usecookie);
    		delete_template_nodes($mobile_node, $tablet_node, $standard_node);
    		}
    }else if ( get_user_prefs() == $standard_node ) { 
    delete_template_nodes($mobile_node, $tablet_node, $standard_node);
    store_user_prefs($standard_node, $usecookie); 
    }else if ( get_user_prefs() == $tablet_node ) { 
    delete_template_nodes($mobile_node, $tablet_node, $standard_node);
    store_user_prefs($tablet_node, $usecookie); 
    }else if ( get_user_prefs() == $mobile_node ) { 
    delete_template_nodes($standard_node, $tablet_node, $mobile_node);
    store_user_prefs($mobile_node, $usecookie); 
    //Detect iPhone
    } else if (($uagent_obj->DetectTierIphone() == $uagent_obj->true) && ($iphone == 1)){
    delete_template_nodes($standard_node, $tablet_node, $mobile_node);
    store_user_prefs($mobile_node, $usecookie);
    //Detect Tablet
    } else if (($uagent_obj->DetectTierTablet() == $uagent_obj->true) && ($tablet == 1)){
    delete_template_nodes($standard_node, $tablet_node, $mobile_node);
    store_user_prefs($tablet_node, $usecookie);
    //Detect iPad
    } else if (($uagent_obj->DetectIpad() == $uagent_obj->true) && ($ipad == 1)){
    delete_template_nodes($standard_node, $tablet_node, $mobile_node);
    store_user_prefs($tablet_node, $usecookie);
    //Detect Tier Rich Css
    } else if (($uagent_obj->DetectTierRichCss() == $uagent_obj->true) && ($rich_css == 1)){
    delete_template_nodes($standard_node, $tablet_node, $mobile_node);
    store_user_prefs($mobile_node, $usecookie);
    //Detect All Other Mobile Devices
    } else if (($uagent_obj->DetectTierOtherPhones() == $uagent_obj->true) && ($all_other_mobile == 1)){
    delete_template_nodes($standard_node, $tablet_node, $mobile_node);
    store_user_prefs($mobile_node, $usecookie);
    //Else it's a regular PC browser -- send to regular desktop site
    } else
    { store_user_prefs($standard_node, $usecookie);
    delete_template_nodes($mobile_node, $tablet_node, $standard_node);
    }

    I've basically just added the $tablet_node alongside the mobile and standard ones throughout. I've also added force_browser_tablet and tablet_node properties to the plugin. I've left the iPad detection in for now for continuity but hope to be able to remove it eventually.

    On my test page I've inserted the three tags
    <standard>Standard</standard>
    <tablet>Tablet</tablet>
    <mobile>Mobile</mobile>
    to see how it's working.
    Currently the following code is showing on each device:
    Desktop – leaves standard tag untouched, recognises tablet tag and removes, recognises mobile tag and strips all content
    <standard>Standard</standard>
    Tablet

    Tablet – leaves standard tag untouched, recognises tablet tag and removes, recognises mobile tag and strips all content
    <standard>Standard</standard>
    Tablet

    Mobile – recognises standard tag and strips all content, recognises tablet tag and removes, leaves mobile tag untouched
    Tablet
    <mobile>Mobile</mobile>


    Any ideas or advice on what's not quite working with it yet would be greatly appreciated.

    Thanks in advance