SocialEngine Tutorial – Checking user authentication and administration privilages

Sometimes you’ll need to protect your controllers from non members. In order to do this you can use the code below in your action (controller class);

if(!$this->_helper->requireUser()->isValid()) return;

This will check that you have a valid user who is signed into the site. However if you want to check if the current user is an administrator you can use;

if(!$this->_helper->api()->user()->getViewer()->isAdmin()) return;

SocialEngine Tutorial Adding custom routes for your modules using Bootstrap.php

When you are developing your SocialEngine module or widgets you might want to be able to pass parameters to an action within your controller. For example;

http://yourwebsite.com/yourmodule/your-controller/your-action/your-variable

Now to put it into context, say we are building a profile viewer module (called profile) and we had a controller (user) that shows a user’s profile (using the displayUserDetailsAction) based on the ID you you provide to it. The current URL would look like this;

http://yourwebsite.com/profile/user/displayUserDetails/1 (unique user_id)

As you can see this is a rather long url for the user to type, a more clean solution would be to have a URL which looks like this;

http://yourwebsite.com/profile/1

In order to achieve this you can use the Bootstrap.php file located within the root of your module which handles all the custom routing for actions within your module. You will want to create the method “protected function _initRouter()”, fetch an instance of the router and then add the custom route to it.

class YOURMODULE_Bootstrap extends Engine_Application_Bootstrap_Abstract
{
    // You will need to add this method
    protected function _initRouter(){
        $fc = Zend_Controller_Front::getInstance();
        $router = $fc->getRouter();
        $router->addRoute('UNIQUE_ROUTE_NAME', new Zend_Controller_Router_Route('profile/:user_id', array('module' => 'profile',
'controller' => 'user',
'action' => 'displayUserDetails'
)));
 
        return $router;
    }
 
}

Now what this will do is route any request of http://yourwebsite.com/profile/anynumber to the module of profile using the user controller and call the displayUserDetails action passing in the variable of user_id. In order to get this id you have to use the code below within your action;

$user_id = $this->getRequest()->getParam("user_id");

Now as I found out you might want to have multiple routes within your module. If you want to do this then you have to have unique route names within your bootstrap.php file!! Took me a while to realise why they weren’t working but you just have to add;

class YOURMODULE_Bootstrap extends Engine_Application_Bootstrap_Abstract
{
    // You will need to add this method
    protected function _initRouter(){
        $fc = Zend_Controller_Front::getInstance();
        $router = $fc->getRouter();
        $router->addRoute('UNIQUE_ROUTE_NAME1', new Zend_Controller_Router_Route('profile/:user_id', array('module' => 'profile',
'controller' => 'user',
'action' => 'displayUserDetails'
)));
 
$router->addRoute('UNIQUE_ROUTE_NAME2', new Zend_Controller_Router_Route('profile/:user_id', array('module' => 'profile',
'controller' => 'user',
'action' => 'displayUserDetails'
)));
 
        return $router;
    }
 
}

Take note of unique_route_name1 and 2!!

Happy coding

Credit to this guy who had an initial post on routing
http://tjgamble.com/2011/04/adding-custom-routes-to-your-socialengine-4-modules/

SocialEngine Tutorial – Getting the current user’s id and data

When you load any view inside your website you should always have an object named $viewer().
Here are some useful things you can find out in your view using the viewer object;

Get the current viewier’s Id

$this->viewer()->getIdentity();

Get the current viewier’s name

$this->viewer()->getTitle();

Get the current viewier’s profile URL

$this->viewer()->getHref();

Check if current user is an admin (return is either 1 or 0, true or false)

$this->viewer()->isAdmin();

If you want to be able to do the same thing within your controller rather than your view you can also use

Get current viewer’s identity

Engine_Api::_()->user()->getViewer()->getIdentity();

On a side note if you ever need to know whats being loaded in your view just print_r out the view object. However I find it extremely useful to call get_class_methods() on the object so that you can see what you can do with it;

echo '<pre>';
print_r(get_class_methods($this->viewer()));
echo '

‘;

(get rid of the backslash, had to include it to stop my pre tags messing up!

Android – Setting screen brightness from code behind

I ran into a problem today with trying to override the screen brightness for the current activity. I found there are 2 methods of changing your screen brightness, 1 which changed the whole system brightness or the other which is to change your current activity’s brightness.

I went for the latter however I came up against some problems when I tried to use the code I found here;

http://stackoverflow.com/a/5090578/276220

 I realised my eclipse was complaining because I had the project set as an android 2.1 project rather than the 2.3.3 handset which I was debugging on.
It would seem that you used to be able to just set it with;
public void SetBright(float value) 
{
    Window mywindow = getWindow();
    WindowManager.LayoutParams lp = mywindow.getAttributes();
    lp.screenBrightness = value;
    mywindow.setAttributes(lp);
}
However when google updated android with the option to set automatic brightness that method no longer worked. Therefore you had to check if the auto brightness had been set so that you could tell android that you were going to manually override the brightness
public void setBrightness(float brightness){ 
    try{
	int brightnessMode = android.provider.Settings.System.getInt(getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE);
 
        if (brightnessMode == android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC) {
		android.provider.Settings.System.putInt(getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE, android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
	}
 
	WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
	layoutParams.screenBrightness = brightness;
	getWindow().setAttributes(layoutParams);
    } catch (Exception e){
        // do something useful
    }
}
I also found this which is a nice slider attached to the brightness, however their post looks pretty awful to read so I’d suggest check the stackoverflow post first before you try and understand this one 😀
http://android-er.blogspot.com/2011/02/change-system-screen-brightness-using.html

SocialEngine Tutorial – Adding dynamic href links from code behind

One thing you might want to do when you’re coding your own modules/widgets is to have a link to another page you might have created within your socialengine site.

You could use the following to add a link inside your index.tpl view file

<a href='http://yoursite.com/yourmodule/yourfunctionname'>Click me</a>

However if you use direct paths such as this you might run into troubles later on if you decide to move your site or share your modules/widgets with other people.

The best way you can generate html links is through using the helper class which is available inside socialengine which is loaded with your view and available from the $this object. The syntax for using this helper class is as follows;

<?php echo $this->htmlLink('your-module-name/the-action-you-want-to-execute','Click me!'); ?>

SocialEngine Tutorial – Creating your own custom theme and styles with css

Carrying on from the initial post about customisation I thought I’d add a quick overview of how on earth you go about customising your SocialEngine site with your own custom css.

One thing i would suggest about editing the SocialEngine platform is that if you can, do it through the admin interface as much as possible. The reason I say this is because as the engine is designed, quite rightly, to function in a multitude of different styles and formations it is intrinsically linked to a database backend which keeps a note of themes available and modules/widget positions etc.

So first off head to the theme editor;
Log into your network as an admin > Admin > Layout > Theme Editor

I would suggest that you hit ‘Clone’ and clone an existing theme which will bring you to the next screen;

Now you’ll be taken back to the theme editor, MAKE SURE YOU ACTIVATE YOUR THEME! Otherwise you’ll be editing the original theme. To activate just hit ‘Activate Theme’ which will then allow you to edit your theme.

Now themes are made up of two items; constant.css and theme.css.

Constants deals with all the constants which are generic for the site for example, site width will be specified in there and body background etc. This is where you’re most likely to edit items which will effect most of the site.

Inside the theme.css you’ll be able to edit other stuff like menu bars and headings etc but you will also be able to specify your own styles for your own modules or widgets that you create.

After you’ve finished editing your theme hit the save button and you’re ready to rock and roll to see what you’ve destroyed on the site 🙂

The easiest way to know what styles to edit is to use some browser debugging such as firebug (for firefox) or chrome developer tools (google chrome). Enjoy!

 

SocialEngine Tutorial – Creating your own custom homepage

One of the first things you might want to do with SocialEngine is to start customising the hell out of it. What better place to start then with your homepage? I did a bit of googling for the answer to this and in the end I came up with a relatively neat-ish hack for it which requires minimal set up.

First off you’ll need to head into the layout editor –
Log into your network > Admin > Layout > Layout Editor

This page allows you to add widgets from the menu on the right (you can add stuff like login boxes and member counters etc). However it doesn’t really allow you to scrap the whole thing and start with your own layout completely.

If you want to do this then hit ‘edit columns’ and select the option with only a header, content and footer;

Now drag the ‘HTML Block’ from the available blocks (on the right) into your main content area. You can then click ‘edit’ on the html block and paste in your HTML. I’d suggest just creating your markup in another file and once you’re happy with it just paste it in.

If you want to be able to add a login area then drag that in too and then head to the main page in your browser and copy and paste the html of the login area block. This will allow you then to add your own styles to the login form as well as completely customise your own front page.