Category Archives: Social Engine(SE)

Profile preview in social engine like facebook

If any one need to preview a user profile then he/she might look here.

At first I make a controller in user module. It is similar to profile view. I simply want to load this page where the viewer will be null. So I make some adjustment.
#step 1:
Create a controller just for keep it simple

class User_PreviewProfileController extends Core_Controller_Action_Standard

    public function init()
        // @todo this may not work with some of the content stuff in here, double-check
        $subject = null;
        if (!Engine_Api::_()->core()->hasSubject())
            $id = $this->_getParam('id');

            // use viewer ID if not specified
            //if( is_null($id) )
            //  $id = Engine_Api::_()->user()->getViewer()->getIdentity();

            if (null !== $id)
                $subject = Engine_Api::_()->user()->getUser($id);
                if ($subject->getIdentity())

                $subject, Engine_Api::_()->user()->getViewer(), 'view'

    public function indexAction()
        $subject = Engine_Api::_()->core()->getSubject();
        $viewer = Engine_Api::_()->user()->getViewer();
        // check if the viewer of this page is the same user
        if ($subject->isSelf($viewer))
            $viewer = Engine_Api::_()->user()->getViewer($viewer->getIdentity());

        // check public settings
        $require_check = Engine_Api::_()->getApi('settings', 'core')->core_general_profile;
        if (!$require_check && !$this->_helper->requireUser()->isValid())

        // Check enabled
        if (!$subject->enabled && !$viewer->isAdmin())
            return $this->_forward('requireauth', 'error', 'core');

        // Check block
        if ($viewer->isBlockedBy($subject) && !$viewer->isAdmin())
            return $this->_forward('requireauth', 'error', 'core');

        // Increment view count
        if (!$subject->isSelf($viewer))

        // Check to see if profile styles is allowed
        $style_perm = Engine_Api::_()->getDbtable('permissions', 'authorization')->getAllowed('user', $subject->level_id, 'style');
        if ($style_perm)
            // Get styles
            $table = Engine_Api::_()->getDbtable('styles', 'core');
            $select = $table->select()
                    ->where('type = ?', $subject->getType())
                    ->where('id = ?', $subject->getIdentity())

            $row = $table->fetchRow($select);
            if (null !== $row && !empty($row->style))

        // Render


#Step 2
router for the link

// User - Specific Profile Preview
        'user_preview_profile' => array(
            'route' => 'preview/profile/:id/*',
            'defaults' => array(
                'module' => 'user',
                'controller' => 'preview-profile',
                'action' => 'index'

#step 3
create page for the link

INSERT INTO `engine4_core_pages` (`name`, `displayname`, `url`, `title`, `description`, `keywords`, `custom`, `fragment`, `layout`, `levels`, `provides`, `view_count`) VALUES
('user_preview-profile_index', 'Preview Member Profile', NULL, 'Preview Member Profile', 'This is a member''s preview profile.', '', 0, 0, '', NULL, 'subject=user', 0);

#step 4
Now create content of the page

INSERT INTO `engine4_core_content` (`content_id`, `page_id`, `type`, `name`, `parent_content_id`, `order`, `params`, `attribs`) VALUES
(609, 18, 'container', 'main', NULL, 2, '[""]', NULL),
(610, 18, 'container', 'left', 609, 4, '[""]', NULL),
(611, 18, 'container', 'middle', 609, 6, '[""]', NULL),

Note: Here content_id for main remain empty when you create main
for other put main content_id as parent_content_id

And then do same as member profile does.

#step 5
Slight change to your getViewer model function

// Viewer

     * Gets the current viewer instance using the authentication storage
     * @return User_Model_User
    public function getViewer($id = null)
        if (null === $this->_viewer)
            $identity = $this->getAuth()->getIdentity();
            $this->_viewer = $this->_getUser($identity);
        elseif($this->getAuth()->getIdentity() == $id)
            $this->_viewer = $this->_getUser(null);

        return $this->_viewer;

#step 6
Now create a new menu in user home menu

INSERT INTO `engine4_core_menuitems` (`name`, `module`, `label`, `plugin`, `params`, `menu`, `submenu`, `enabled`, `custom`, `order`) VALUES
('user_home_preview', 'user', 'Preview My Profile', 'User_Plugin_Menus', '{"route":"user_preview_profile","icon":"application/modules/User/externals/images/links/profile.png"}', 'user_home', '', 1, 0, 22);

#step 7
create a initialization function for the menu in user core

public function onMenuInitialize_UserHomePreview($row)
    $viewer = Engine_Api::_()->user()->getViewer();
    if( $viewer->getIdentity() ) {
      return array(
        'label' => $row->label,
        'icon' => $row->params['icon'],
        'route' => 'user_preview_profile',
        'params' => array(
          'id' => $viewer->getIdentity()
    return false;

That’s it. Have fun!


exception ‘Engine_Api_Exception’ with message ‘Unknown item type

First you should create a api core class in API folder.


class [modulename]_Api_Core extends Core_Api_Abstract

But you have to do another thing in db row class, extends Core_Model_Item_Abstract instate of Core_Model_Item_Collection

this will remove the error when you delete using the item

Model In Social Engine

In SE there are two kinds of Model

1.DB Model
2.Row Model

Folder stacture

–>Model //all Row model
–>Model/DbTable //all DbTable Model

1. DB Model: This model is used for query in the whole table. In SE the table naming convention is table_name(s). So it is good for us to use plural name for table.

For example. Our table name = engine4_core_rooms[this table is for keeping information of a room in a university]
so, we will use room_id as primary key for SE database table naming convention

So our DB Model will

class ModuleName_Model_DbTable_Rooms extends Engine_Db_Table {

    protected $_rowClass = 'ModuleName_Model_Room'; //This is our Row Model class


Note:: Here we must keep in mind that we use Engine_Db_Table calss to extends our new DbTable Class

2.Row Model: This is a very usefull stuff in SE. Every row of a table is itself work as a object here. So we can minimise our join query here if we can use it in a perfact way.

Every DbTable Model has a Row model. A DbTable’s row model is define in DbTabe, as

protected $_rowClass = 'ModuleName_Model_Room';

//This is our Row Model class

That means we have a row model for our Rooms DbTable and which name is Room row model. Here we must keep in mind that we use the same name as we use it in DbTable model. We will
keep it a singular to follow as SE nemin convention.

So our Row model will be

 class ModuleName_Model_Building extends Core_Model_Item_Abstract
	//all function goes there..

Note:: We must extends Core_Model_Item_Abstract

Calling convention


 $dbTable = Engine_Api::_()->getDbtable('tablename', 'modeulename');
 $dbTable = Engine_Api::_()->getItemTable('itemname');
 $somereturn = dbTable->someFunction();


 rowObject = Engine_Api::_()->getItem('itemname', $primaryKey);
 $someReturn = $rowObject->someFunction();

Add A New Menu Item

Now we will discuss about How to manupulate menu in SE.

Actually SE does not provide any Iterface for Menu in admin section. We have to edit Database for creating a new menu

Module Menu

INSERT IGNORE INTO `engine4_core_menuitems` (`name`, `module`, `label`, `plugin`, `params`, `menu`, `submenu`, `order`) VALUES
('core_admin_main_plugins_[modulename]', '[modulename]', '[Module Level]', '', '{"route":"admin_default","module":"[modulename]","controller":"[controllername]","action":"[actionname]"}', 'core_admin_main_plugins', '', [order]);

core_admin_main_plugins = Plugins Manin Menu[if we use it then our module will display under Plugin Menu]
core_admin_main = Main Menu [if we use it, it will show in main nave bar]


('core_admin_main_plugins_sport', 'sport', 'Sport', '', '{"route":"admin_default","module":"sport","controller":"manage","action":"browse"}', 'core_admin_main_plugins', '', 999);

Sub menu Under it.

('[modulename]_admin_main_[uniquename]', '[modulename]', '[Menu Level]', '', '{"route":"admin_default","module":"[modulename]","controller":"[controllername]","action":"[actionname]"}', '[modulename]_admin_main', '', [order]);


('sport_admin_main_manage', 'sport', 'Manage Sports', '', '{"route":"admin_default","module":"sport","controller":"manage","action":"browse"}', 'sport_admin_main', '', 1),


In Controller

$this->view->VariableThatUseInView = Engine_Api::_()->getApi('menus', 'core')
                ->getNavigation('[main_menu_name]', array(), '[active_sub_menu]');

VariableThatUseInView = navigation
main_menu_name = sport_admin_main
active_sub_menu = sport_admin_main_manage

$this->view->navigation = $navigation = Engine_Api::_()->getApi('menus', 'core')
                ->getNavigation('sport_admin_main', array(), 'sport_admin_main_manage');

In View

<?php if (count($this->navigation)): ?>
    <div class='tabs'>
        echo $this->navigation()->menu()->setContainer($this->navigation)->render()
<?php endif; ?>

Item In Social Engine(SE)

Item an important feather in SE. We can make our life easy by using it.

For decliring Item in SE. We have to do it in Module/setting/manifest.php
Here we use this

// Items ---------------------------------------------------------------------
    'items' => array(

Every item will represent a particular table.
If we follow the naming convention of SE in database table.
Then our Table name will be plural and row class will be singular

So, If we have a item named “item” then it’s primary key will be “item_id” and table will be “items”

Calling Item

By using item we can easily use any table and get it’s a particular row easily.

Table info using Item

$tableInfo = Engine_Api::_()->getItemTable('[itemname]');

Table’s a perticular row using Item

$rowInfo = Engine_Api::_()->getItem('[itemname]',[primary_key_value]);

We have also two very important items.

1. Viewer
2. Subject

##Viewer: This item gives us the information of a user who is current viewing on the site

$viewer = Engine_Api::_()->user()->getViewer();
$id = $viewer->getIdentity(); 

[This function will return id of this table, it is a magic function of item in SE]

##Subject: This item gives us the information of a subject which is being viewing by a user

$subject = Engine_Api::_()->core()->getSubject();
$id = $subject->getIdentity(); 

[This function will return id of this table, it is a magic function of item in SE]

Before we using subject we first have to set he subject
Setting a subject


How to set

if (0 !== ($id = (int) $this->_getParam('id')) &&
                null !== ($subjectToSet = Engine_Api::_()->getItem('item', $id)))

We have to set it in it’s controller. and it is good if we set it in ini()

public function init()
%d bloggers like this: