Tuesday, September 28, 2010

Zend Framework: Multiple Check Box Handling

Here I will try to describe how to handle multiple check box with Zend Form

Below is the Form from/user.php
<?php
class Form_User extends Zend_Form
{
    public function __construct($options = null)
    {
        parent::__construct($options);
      
        $this->setName('User_Form');
       
        $user_id = new Zend_Form_Element_Text('user_id');
        $user_id->setLabel('User ID')
                  ->setRequired(true)
                  ->addValidator('NotEmpty');

      
        ..................................
        ....................................
        ....................................

        $area = array(

                            'index' => 'Home Page',
                            'admin' => 'Admin Home',
                            'agent' => 'Agent Contorl',
                            'user' => 'User Contorl',
                            'cdist' => 'Card Distribution',
                            'cardsell' => 'Card Sell',
                            'manualmt' => 'Manual Money Transfer',
                            'moneydelivery' => 'Money Delivery',
                            'transactionmonitor'    => 'Transaction Moniitor',
                            'adminreport'   =>  'Admin Report',
                            'agentreport'   =>  'Agent Report',
                            'customercare'   =>  'Customer Care',

);

$privileges = new Zend_Form_Element_MultiCheckbox('privileges');

$privileges->setLabel("Select Previliges");
            foreach($area as $prev => $p)
            {
                $privileges->addMultiOption($prev,$p);
            }

        $user_photo = new Zend_Form_Element_File('user_photo');
        $user_photo->setLabel('Photo');
        $submit = new Zend_Form_Element_Submit('submit');
        $submit->setLabel('Save Data')
                 ->setAttrib('class', 'button')
        ;
       
         $this->addElements(array(
                $user_type,
                $agent_id,
                $user_id,
                .........
                $privileges,
                $submit));

    }
}


Now The Controller UserController.php


class UserController extends Zend_Controller_Action
{

    public function init()
    {
     /    }

       public function addAction()
    {
        // action body
       
        $this->view->headTitle('Add New User', 'PREPEND');
        $form = new Form_User();
        $this->view->form = $form;
      
        if ($this->getRequest()->isPost()) {
       
        $formData = $this->getRequest()->getPost();
        if ($form->isValid($formData)) {
        //$sp = implode(",",$privileges);
        $user_photo = ($form->getValue('user_photo')) ? $form->getValue('user_photo') : "";

        $data = array(
            'user_type' => $form->getValue('user_type'),
            'agent_id' => $form->getValue('agent_id'),
            'user_id' =>strtolower($form->getValue('user_id')),
           ..............................
.................................
            'privileges' => serialize($form->getValue('privileges')),
            'user_status' => $form->getValue('user_status'),
            'user_photo' => $user_photo,
        );

      
        $user = new Model_DbTable_Userinfo();

        $user-> addUser($data);
        $this->_redirect('/user/');

        } else {
            print_r($formData);
            $form->populate($formData);
        }
        }
    }



The Model to Do the database action

class Model_DbTable_Userinfo extends Zend_Db_Table_Abstract
{
    protected $_name = 'user_info';

       public function addUser($data)
    {
       
        $this->insert($data);
    }
}









To edit the Multiple check box value do as follows in Edit action

public function editAction()
    {
        // action body
        $sess = new Zend_Session_Namespace('uri');

        $this->view->title = "Edit User";
        $this->view->headTitle($this->view->title, 'PREPEND');
        $form = new Form_User();
        //$form->user_id->setAttrib('readonly','true');
        $form->removeElement('user_id');
        $user_id = new Zend_Form_Element_Hidden('user_id');
        $form->addElement($user_id);
        $this->view->form = $form;

        if ($this->getRequest()->isPost()) {
        $formData = $this->getRequest()->getPost();
        if ($form->isValid($formData)) {
        $user_id = $form->getValue('user_id');
        $agent_id = $form->getValue('agent_id');
        $user_photo =  $form->user_photo->getFileName('user_photo',false);

        if(!empty($user_photo))
            {
        $data = array(
            'user_type' => $form->getValue('user_type'),
            'agent_id' => $form->getValue('agent_id'),
            'user_pass' => $form->getValue('user_pass'),
            'user_name' => $form->getValue('user_name'),
            'user_address' => $form->getValue('user_address'),
            'user_contact' => $form->getValue('user_contact'),
            'user_email' => $form->getValue('user_email'),
            'privileges' => serialize($form->getValue('privileges')),
            'user_status' => $form->getValue('user_status'),
            'user_photo' => $form->getValue('user_photo'),
        );
            }
    else
        {
        $data = array(
            'user_type' => $form->getValue('user_type'),
            'agent_id' => $form->getValue('agent_id'),
            'user_pass' => $form->getValue('user_pass'),
            'user_name' => $form->getValue('user_name'),
            'user_address' => $form->getValue('user_address'),
            'user_contact' => $form->getValue('user_contact'),
            'user_email' => $form->getValue('user_email'),
            'privileges' => serialize($form->getValue('privileges')),
            'user_status' => $form->getValue('user_status'),
        );
        }
        $user = new Model_DbTable_Userinfo();
        $user->updateUser($data,$user_id);
        $this->_redirect($sess->req_uri);
        } else {
        $form->populate($formData);
        }
        }
        else {
        $user_id = $this->_getParam('user_id', 0);
        $user = new Model_DbTable_Userinfo();
        $user_data = $user->getUser($user_id);
        $form->populate($user_data);
        $prev = unserialize($user_data['privileges']);
        $form->privileges->setValue($prev);
        }
    }





Hope Thats help you.

Tuesday, September 7, 2010

How to detect if the user is logout in php?

You cannot detect when a user closes their browser or navigates off your site with PHP, and the JavaScript techniques of doing so are so far from guaranteed as to be useless.
Instead, your best bet is most likely to store each user's last activity time.
  • Create a column in your user table along the lines of 'last_activity'.
  • Whenever a user loads a page, update their last_activity to the current time.
  • To get a list of who's online, just query the database for users with last_activity values more recent than 10/20/whatever minutes ago.

Source: http://stackoverflow.com/questions/887919/how-to-detect-if-the-user-is-logout-in-php