SocialEngine Tutorial – Creating widgets for user profiles, getting the subject rather than the viewer information

I’ve been working on creating a widget for a user’s profile however I’ve needed to find out who I’ve been viewing in order to select the correct data.

I made a post about getting user details here however it only told you how to get the current viewer’s data. In this post we’ll be fetching the data for the current person being viewed by the user.

Lets say we have 2 users – alice and bob. We have a widget that fetches a list of websites that the user has viewed recently.

When Alice looks on Bob’s profile we need to run a query which uses Bob’s user_id so we can go and fetch the data about him and display the results to Alice.

What we have here is a viewer -> subject relationship whereby a viewer (Alice) is looking at the subject (Bob) and we need his ID rather than Alice’s (the viewer’s) id.

In order to get the current subject’s user details we need to do 2 things;

First things first, we need to check that we have a subject otherwise our page will error;

Engine_Api::_()->core()->hasSubject();

If this evaluates to true then we can do the following;

Engine_Api::_()->core()->getSubject();

Now the complete code to get the current subject’s user_id is as follows;

if(Engine_Api::_()->core()->hasSubject()){
    $user_id = Engine_Api::_()->core()->getSubject()->getIdentity());
} else {
    echo 'no subject on this page';
}

If you want to know what methods are available for the subject object you can call the ‘get_class_methods()’ function on the subject;

print_r(get_class_methods(Engine_Api::_()->core()->getSubject()));

Socialengine Tutorial – Including files within your modules, widgets and plugins within externals

When you develop modules or widgets you may wish to also include files such as images or javascript files etc. However rather then making the user drag and drop files into various locations you can actually place the files you wish to include within your module inside the ‘externals’ folder.

This folder then needs a .htaccess file which specifies allow all access to the folder. So take your ‘helloworld.png’ and place it inside here;

application/modules/YOUR MODULE NAME/externals/images/

Not forgetting to include create a file with the name .htaccess and placing it into;

application/modules/YOUR MODULE/externals/

.htaccess file content;

allow from all

SocialEngine Tutorial – Automatically post a status update / post to users wall on behalf of user using code behind

Sometimes you might need to post a status update on behalf of the user, say for example they have just completed an action or they’ve levelled up!

If you wish to post an update on behalf of the user you can use;

$action = Engine_Api::_()->getDbtable('actions', 'activity')->addActivity(Engine_Api::_()->user()->getViewer(), Engine_Api::_()->user()->getViewer(), 'status', 'YOUR STATUS TEXT HERE');

This creates a new user action stored under engine4_activity_actions which will then be posted on the current user’s wall.

*note this code is for use in a controller action only!

Socialengine Tutorial – Disable default css from being loaded

I’ve been working on creating a custom homepage which uses twitter bootstrap however I’ve found the default css being generated by the socialengine is interferring with the bootstrap css.

In order to stop socialengine from adding in the default theme css files head to

Application > Modules > Core > Layouts > Scripts > default-simple.tpl

This default simple is what is loaded on every page and then injected with the page’s content. However I didn’t want any of that to load on my landing page so I’ve done the following;

(In this file look for line 75 where the ‘linking’ of css files begins and I’ve wrapped it in an if statement which says if the current url string == the default home page (e.g. /) then don’t bother loading the rest of the php)

<?php // LINK/STYLES ?>
  <?php
  $uri = Zend_Controller_Front::getInstance()->getRequest()->getRequestUri();
  if(trim($uri, '/') !=  trim($this->baseUrl(), '/')):
  ?>
  <?php
    $this->headLink(array(
      'rel' => 'favicon',
      'href' => ( isset($this->layout()->favicon)
        ? $this->baseUrl() . $this->layout()->favicon
        : '/favicon.ico' ),
      'type' => 'image/x-icon'),
      'PREPEND');
    $themes = array();
    if( !empty($this->layout()->themes) ) {
      $themes = $this->layout()->themes;
    } else {
      $themes = array('default');
    }
    foreach( $themes as $theme ) {
      $this->headLink()
        ->prependStylesheet($this->baseUrl().'/application/css.php?request=application/themes/'.$theme.'/theme.css');
    }
    // Process
    foreach( $this->headLink()->getContainer() as $dat ) {
      if( !empty($dat->href) ) {
        if( false === strpos($dat->href, '?') ) {
          $dat->href .= '?c=' . $counter;
        } else {
          $dat->href .= '&c=' . $counter;
        }
      }
    }
  ?>
 
  <?php endif; ?>

Hope this helps 🙂

SocialEngine – Making Mootools and jQuery play nice

I’ve been working on a few custom modules for a socialengine site and quickly realised that when I was including jQuery in my views all other javascript functionality within socialengine stopped working. This is because of a namespacing issue between mootools and jQuery which means that the $(‘#bla’).val(); type commands in mootools using the dollar sign were being overwritten by the jQuery namespace meaning that all the previous functions used in socialengine stop working.

Usually you would use the following (which breaks mootools);

<script src="<?php echo $baseURL; ?>js/jquery.js"></script>
<script type="text/javascript">
 
$(document).ready(function() {
  alert();
});
 
</script>

So how do you get around this issue? You need to use jQuery noConflict which defines a namespace to use and thus resolves the issue by using a custom variable rather than the dollar $.

You’ll need to change all $(). to $j().

<script src="<?php echo $baseURL; ?>js/jquery.js"></script>
<script type="text/javascript">
 
var $j = jQuery.noConflict();
 
$j(document).ready(function() {
  alert();
});
 
</script>

So every time you use jQuery you’ll have to use your custom variable just after the dollar sign

Enjoy

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!

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!'); ?>