Please upgrade here. These earlier versions are no longer being updated and have security issues.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.
Options

I Need Help To Avoid Modifying Core Files*

AdrianAdrian Wandering SpiritMontreal MVP
edited April 2013 in Vanilla 2.0 - 2.8

****I am trying to create an ADD on to help user go back to mobile site in Vanilla 2.0.1.8, but I am stuck,

Here is the code I have

class GoBackMobile extends Gdn_Plugin { 

// works if added to  class ProfileController extends Gdn_Controller in core code
public function Mobile() {
      $Expiration = time() - 172800;
      $Expire = 0;
      $UserID = ((Gdn::Session()->IsValid()) ? Gdn::Session()->UserID : 0);
      $KeyData = $UserID."-{$Expiration}";
      Gdn_CookieIdentity::SetCookie('VanillaNoMobile', $KeyData, array($UserID, $Expiration, 'force'), $Expire);
      Redirect("/", 302);
   }

public function Base_Render_Before($Sender) {
$ForceNoMobile = Gdn_CookieIdentity::GetCookiePayload('VanillaNoMobile');
if ($ForceNoMobile !== FALSE && is_array($ForceNoMobile)&& in_array('force', $ForceNoMobile))
$Sender->AddAsset('Foot', Wrap(Anchor(T('Back to Mobile Site'), '/profile/mobile/1'),'div'),'Mobile Link'); 
}
}

The problem is this only works if I hack class.profilecontroller.php. I really don't want to hack core code. Do you have any thoughts?

Once I figure it out I will release as addon for everyone to use

thanks!

Comments

  • Options
    lifeisfoolifeisfoo Zombie plugins finder ✭✭✭

    If you paste here your profilecontroller hack, an help will arrive sooner (much clear problem/solution).

    There was an error rendering this rich post.

  • Options
    hgtonighthgtonight ∞ · New MVP

    @lifeisfoo The hack is in the code block he posted.

    @adriansonline What is calling the $ProfileController->Mobile()?

    Search first

    Check out the Documentation! We are always looking for new content and pull requests.

    Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.

  • Options
    lifeisfoolifeisfoo Zombie plugins finder ✭✭✭
    public function Base_Render_Before($Sender) {
        if( IsMobile() ){
             echo "Is Mobile!";
        }
    }
    

    Just replace the string with a link

    Ref http://vanillaforums.org/discussion/23498

    There was an error rendering this rich post.

  • Options
    AdrianAdrian Wandering Spirit Montreal MVP

    @lifeisfoo I want users who went to the full site to go back to mobile site. A cookie is set that needs to be rewritten setting the time in the past. Right now that is what is needed.

    @hgtonight not sure what you mean. If I place the mobile() function anove in class.profilecontroller.php at line 368 right under function nomobile() it works like a charm. But I want to future proof this addon and want to avoid hacking core files.

    Is that clearer ;) thanks for the help all

  • Options
    hgtonighthgtonight ∞ · New MVP
    edited April 2013

    So you want to show a link if the VanillaNoMobile cookie is set that allows them to destroy the cookie (and thus go back to the full site)?

    Use your plugin to extend the profile controller with your Mobile function.

    class GoBackMobile extends Gdn_Plugin {
    
        public function ProfileController_Mobile_Create($Sender) {
            $Expiration = time() - 172800;
            $Expire = 0;
            $UserID = ((Gdn::Session()->IsValid()) ? Gdn::Session()->UserID : 0);
            $KeyData = $UserID."-{$Expiration}";
            Gdn_CookieIdentity::SetCookie('VanillaNoMobile', $KeyData, array($UserID, $Expiration, 'force'), $Expire);
            Redirect("/", 302);
        }
    
        public function Base_Render_Before($Sender) {
            $ForceNoMobile = Gdn_CookieIdentity::GetCookiePayload('VanillaNoMobile');
            if ($ForceNoMobile !== FALSE && is_array($ForceNoMobile)&& in_array('force', $ForceNoMobile))
            $Sender->AddAsset('Foot', Wrap(Anchor(T('Back to Mobile Site'), '/profile/mobile/'),'div'),'Mobile Link');
        }
    }
    

    Search first

    Check out the Documentation! We are always looking for new content and pull requests.

    Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.

  • Options
    businessdadbusinessdad Stealth contributor MVP

    @hgtonight said:
    So you want to show a link if the VanillaNoMobile cookie is set that allows them to destroy the cookie (and thus go back to the full site?

    Use your plugin to extend the profile controller with your Mobile function.

    +1, I was thinking exactly the same.

  • Options
    AdrianAdrian Wandering Spirit Montreal MVP

    That's what I was missing

    public function ProfileController_Mobile_Create($Sender) {

    Thanks @hgtonight, you rock.

    I'll finish up some stuff and test before I release add on. I'll make sure to give you full props!

  • Options
    hgtonighthgtonight ∞ · New MVP

    Thanks! Magic methods are one of the reasons I fell in love with this framework.

    Search first

    Check out the Documentation! We are always looking for new content and pull requests.

    Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.

  • Options
    vrijvlindervrijvlinder Papillon-Sauvage MVP

    This is needed for sure I hacked the core with this code several months ago in 6 installations. But to allow going back an forth between full and mobile on the mobile.

    How can I make a link only open if it is mobile ? In other words to force mobile view only?

  • Options
    AdrianAdrian Wandering Spirit Montreal MVP
    edited April 2013

    You want the user to only see mobile version of your site? I would "force" the mobile cookie when they arrive. In essence reverse of how it works now... I can try to work out the code if you want

  • Options
    vrijvlindervrijvlinder Papillon-Sauvage MVP
    edited April 2013

    yea well I made this plugin it is a home page/landing page intended for only mobile view. Where you can put links and info or anything really . sort of a mobile style intro page.

    I have a link to profile/mobile which when viewing on a mobile or mobile user agent one gets the mobile view.But I don't want the MobileHome page to be accessed by a normal view. I guess what I need is a way to force the user agent.

    If not mobile=>force user agent x

    What I ended up doing is putting a title on the link to say when hovered that this link only works on a mobile device. but that is a lie lol

  • Options

    this is how I handle it

    public function ProfileController_Mobile_Create($Sender){
        Gdn_CookieIdentity::DeleteCookie('VanillaNoMobile');
        Redirect("/", 302);
    }
    
    public function Base_AfterRenderAsset_Handler($Sender) {
        $AssetName = GetValueR('EventArguments.AssetName', $Sender);
        if($AssetName!='Content') return;
        $ForceNoMobile = Gdn_CookieIdentity::GetCookiePayload('VanillaNoMobile');
        if ($ForceNoMobile !== FALSE && is_array($ForceNoMobile) && in_array('force', $ForceNoMobile)){
            echo Wrap(Anchor('Mobile Site','/profile/mobile', array('class'=>'Button MobileSiteBtn')),'div');
        }else if(IsMobile()){
            echo Wrap(Anchor('Full Site','/profile/nomobile', array('class'=>'Button NoMobileSiteBtn')),'div');
        }   
    }
    

    grep is your friend.

  • Options
    vrijvlindervrijvlinder Papillon-Sauvage MVP

    Thanks for that, Like this you mean? instead of the typical sender add link ?

    
    class MobileHomePlugin extends Gdn_Plugin {
    
        public function Base_Render_Before($Sender) {
            $Session = Gdn::Session();
           if ($Sender->Menu) {
            
    public function ProfileController_Mobile_Create($Sender){
        Gdn_CookieIdentity::DeleteCookie('VanillaNoMobile');
        Redirect("/", 302);
    }
    public function Base_AfterRenderAsset_Handler($Sender) {
        $AssetName = GetValueR('EventArguments.AssetName', $Sender);
        if($AssetName!='Content') return;
        $ForceNoMobile = Gdn_CookieIdentity::GetCookiePayload('VanillaNoMobile');
        if ($ForceNoMobile !== FALSE && is_array($ForceNoMobile) && in_array('force', $ForceNoMobile)){
            echo Wrap(Anchor('Mobile Site','/profile/mobile', array('class'=>'Button MobileSiteBtn')),'div');
        }else if(IsMobile()){
            echo Wrap(Anchor('Full Site','/profile/nomobile', array('class'=>'Button NoMobileSiteBtn')),'div');
        }   
    }
              
    
        public function PluginController_MobileHome_Create($Sender) {
       
            $Session = Gdn::Session();
    
            if ($Sender->Menu)  {
                $Sender->ClearCssFiles();
                $Sender->AddCssFile('mobile.css');
                $Sender->AddCssFile('style.css');
                $Sender->AddCssFile('moho.css', 'plugins/MobileHome');
                $Sender->MasterView = 'default';
    
                $Sender->Render('MobileHome', '', 'plugins/MobileHome');
            }
        
       
        }
      public function OnDisable() {
                $matchroute = '^mobilehome(/.*)?$';
                 
                 Gdn::Router()-> DeleteRoute($matchroute); 
      
      }
     
        public function Setup() {
      
                 $matchroute = '^mobilehome(/.*)?$';
                 $target = 'plugin/MobileHome$1';
            
                 if(!Gdn::Router()->MatchRoute($matchroute))
                      Gdn::Router()->SetRoute($matchroute,$target,'Internal'); 
              
        }
    
    }
    
  • Options
    AdrianAdrian Wandering Spirit Montreal MVP

    @x00 you are a star--wish you were here earlier. I guess there are many ways to skin a cat :) I just finished the add-on for my needs and I just uploaded it. I gave you and @hgtonight props:
    http://vanillaforums.org/addon/gobackmobile-plugin-1.0

  • Options

    yep true, and there is usually a native function too. :)

    grep is your friend.

Sign In or Register to comment.