Monthly Archives: May 2012

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

<?php
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())
                {
                    Engine_Api::_()->core()->setSubject($subject);
                }
            }
        }

        $this->_helper->requireSubject('user');
        $this->_helper->requireAuth()->setNoForward()->setAuthParams(
                $subject, Engine_Api::_()->user()->getViewer(), 'view'
        );
    }

    public function indexAction()
    {
        Engine_Api::_()->user()->setViewer();
        $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())
        {
            return;
        }

        // 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))
        {
            $subject->view_count++;
            $subject->save();
        }


        // 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())
                    ->limit();

            $row = $table->fetchRow($select);
            if (null !== $row && !empty($row->style))
            {
                $this->view->headStyle()->appendStyle($row->style);
            }
        }

        // Render
        $this->_helper->content
                ->setNoRender()
                ->setEnabled()
        ;
    }

}

#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!

Advertisements

exception ‘Engine_Api_Exception’ with message ‘Unknown item type

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

Like

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

Water mark on a image in php

By Image ::


$watermark = imagecreatefrompng('select_sport.png');
$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);
$image = imagecreatetruecolor($watermark_width, $watermark_height);
$image = imagecreatefromjpeg('photo/Chrysanthemum.jpg');
$size = getimagesize('photo/Chrysanthemum.jpg');
$dest_x = $size[0] - $watermark_width - 50;
$dest_y = $size[1] - $watermark_height - 50;
imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, 100);
imagejpeg($image, "photo/" . 'ok.jpg');  
imagedestroy($image);
imagedestroy($watermark);

By Text ::

$font_path = "ARIAL.TTF"; // Font file
$font_size = 20; // in pixcels
$water_mark_text_2 = 'http://www.sample.com'; // Watermark Text

$oldimage_name = 'photo/Chrysanthemum.jpg';
list($owidth,$oheight) = getimagesize($oldimage_name);
$width = $owidth; $height = $oheight; 
$image = imagecreatetruecolor($width, $height);
$image_src = imagecreatefromjpeg($oldimage_name);
imagecopyresampled($image, $image_src, 0, 0, 0, 0, $width, $height, $owidth, $oheight);
$blank = imagecolorallocate($image, 255, 255, 255);
$size = getimagesize($oldimage_name);
$dest_x = $size[0] - 350;
$dest_y = $size[1] - 50;
imagettftext($image, $font_size, 45, $dest_x, $dest_y, $blank, $font_path, $water_mark_text_2);
imagejpeg($image, "photo/" . 'ok.jpg'); 
imagedestroy($image);

Model In Social Engine

In SE there are two kinds of Model

1.DB Model
2.Row Model

Folder stacture

Module
–>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:

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

RowTable

 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]);

Here
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]

ex.

('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]);

ex.

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

Calling

In Controller

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

ex.
Here
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'>
        <?php
        echo $this->navigation()->menu()->setContainer($this->navigation)->render()
        ?>
    </div>
<?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(
        'item1',
        'item2',
        'item3'
    ),

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

Engine_Api::_()->core()->setSubject($subjectToSet);

How to set

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

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: