We build. You grow.

Get best community software here

Start a social network, a fan-site, an education project with oxwall - free opensource community software

How to create custom page available for loged in user only. [Solution Available] | Forum

Alia Team
Alia Jun 2 '14
A lot of you guys on forum have been asking how can you create a custom page that will prompt users to sign in to see it's content. And you have already tried using Pages&menus functionality, which didn't work because it limits accessability of menu item only and not the page itself: both guest and registered users can access your custom page via direct URL.

In this forum topic I will explain how you can create a very basic plugin that in turn will create a custom page accessable for loged in users only. Please note that I will rely heavily on our dev crash course, so make sure to read it as well: http://docs.oxwall.org/dev:crash-course

Keep in mind that this is just a basic plugin, and you can add much more functionality into it if you want to and if you know how to =)
--------------------------------

What you will have as an end result:

new main menu item visible to all visitors (members+guests). On click, if user is not loged in, sign in form will open up. If user is loged in, conent of the page will open.  Site admin will be adding page content directly within the corresponding .html file.

---------------------------------

1. on  your computer create new folder with the name of your future plugin. Example: "custompage".

2. within this folded create  plugin.xml  file. More information on what you need to add within this file can be found here: http://docs.oxwall.org/dev:crash-course
Example:

<?xml version="1.0" encoding="UTF-8"?>
<plugin>
    <name>Custom Page</name>
    <key>custompage</key>
    <description>custom page description</description>
    <author>Alia</author>
    <authorEmail>youremail@gmail.com</authorEmail>
    <authorUrl>http://oxwall.org</authorUrl>;
    <developerKey>your_key_here</developerKey>
    <build>1</build>
    <copyright>(C)All rights reserved.</copyright>
    <license>The BSD License</license>
    <licenseUrl>http://www.opensource.org/...hp</licenseUrl>;
</plugin>

3.Within your folder create following files: install.php, uninstall.php, init.php, activate.php and deactivate.php.

Leave all files except install.php empty for now.
Add following piece of code into install.php:

<?php

BOL_LanguageService::getInstance()->addPrefix('custompage', 'My Custom Page');

4. Compress your folder into .zip archive
5. Go to your admin panel>>manage plugins>>add new plugin and install your custom plugin, just like any other plugin  you install.
6. At this stage, plugin is insatlled and all needed details are automatically added into the database, but plugin is not doing anything yet - is is just there.
Now it is time to add some functionality:
7.Open your plugin's folder directly on the server using FTP or file manager.
Within your plugins' folder create two folders: controllers and views.Create one more controllers folder in the views folder.



8. Let's foget about  "Views" folder for now. Open "controllers" folder and create our first contoller there. Let's name it "custom.php".

This file's content should be as follows: class PLUGINKEY_CTRL_Nameofcontroller extends OW_ActionController

{

}


Example:


<?php

class CUSTOMPAGE_CTRL_custom extends OW_ActionController
{

}

You can see that we are using our pluginkey from plugin.xml file in uppercase letters here. Also, note that name of our file "custom.php" and name of the controller (CUSTOMPAGE_CTRL_custom) should be the same.


More information on naming rules can be found here: http://docs.oxwall.org/dev:crash-course ( under "Creating a page" section).


9. Let's create the first action inside of the class:


<?php

class CUSTOMPAGE_CTRL_custom extends OW_ActionController

{

public function index()
{
$this->setPageTitle("My New Custom Page");
$this->setPageHeading("Custom Page");
}

}


10. For our page to display correctly we should assign a view for the action. For that we need to create an empty file custom_index.html in the views/controllers/ folder. As you can see, the view name contains of the controller file name ( "custom") and the action name ("index
) separated by an underscore.


11. Finally, we can take a look at our page. The URL of the page looks like this: <domain>/custompage/custom/index

Basically you will see an empty page with the page title. It is up to you to add your custom content within custom_index.html file.


12. The URL looks rather lengthy. No worries though - Oxwall supports nice urls. For our page to be available from a shorter URL (from example, <domain>/custom) we should add the following line to the previously created init.php file:


<?php

OW::getRouter()->addRoute(new OW_Route('custompage.index', 'custompage', "CUSTOMPAGE_CTRL_Custom", 'index'));  



Parameters:

  1. route name, custompage.index
  2. path
  3. controller class name
  4. name of the action the route points to.

Now our page open under <domain>/custompage URL.


13. To make the page accessible, let's add a link to the page within the main menu. For that we should first create main_menu_custom key in Languages with the prefix of our plug-in, and 'My Cool Title' as the value. To add new key open your admin panel using dev-tools : www.sitename.com/admin/dev-tools/languages >>click on "Add New Text">> section: select your plugin's name within the drop down>> key:main_menu_custom >>text: any text.


14. Once key is added, add the following code to the activate.php file of your plugin:


<?php

OW::getNavigation()->addMenuItem(OW_Navigation::MAIN, 'custompage.index', 'custompage', 'main_menu_custom', OW_Navigation::VISIBLE_FOR_ALL);

 

Also add following code into deactivate.php:

<?php
OW::getNavigation()->deleteMenuItem('custompage', 'main_menu_custom');


15. From admin panel>>manage plugin>> deactivate and activate your plugin. Now open your site's homepage. You will see that new menu item for your custom page was added.


16. And we are almost done. At this stage any user can open your new page and see it. Now we need to add the code that checks whether user is loged in or not before showing our custom page to him.

Within the controllers/custom.php file you have created add following piece code:


if ( !OW::getUser()->isAuthenticated() )
{
throw new AuthenticateException();
}


Overall you should have:


<?php

class CUSTOMPAGE_CTRL_custom extends OW_ActionController
{
public function index()
{
$this->setPageTitle("My New Custom Page");
$this->setPageHeading("Custom Page");

if ( !OW::getUser()->isAuthenticated() )
{
throw new AuthenticateException();
}

}

}


17. And we are done. Now when guest user tried to open your custom page, he will get a log in form. Already loged in users will be able to see the content of your page right away.


Well, 17 steps!!! This sounds a bit complicated, but once you do this at least one time - things will be easy. And make sure to check http://docs.oxwall.org/dev:crash-course while doing every step from this forum topic, since this doc explains things in more details, while I am giving just a brief description.


Source code of the plugin described in this topic is attached for your reference.
Once more it is only up to you to add your custom content within custom_index.html file. 
Alia Team
Alia Jun 30 '15
AJ,

Could you please give an example of where and how you are trying to use an image in your plugin?