Thursday, September 13, 2012

simple example add , delete ,update ,show records in cakephp

stp-1
CREATE TABLE IF NOT EXISTS `movies` (
  `id` char(36) NOT NULL,
  `title` varchar(255) DEFAULT NULL,
  `genre` varchar(45) DEFAULT NULL,
  `rating` varchar(45) DEFAULT NULL,
  `format` varchar(45) DEFAULT NULL,
  `length` varchar(45) DEFAULT NULL,
  `created` datetime DEFAULT NULL,
  `modified` datetime DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
----------------------------------------------------------------------------------------------------------
stp- 2->create controller page under(moviescontroller.php)-> D:\xampp\htdocs\cakephp_lt\app\Controller\moviescontroller.php and paste below code
----------------------------------------------------------------------------------------------------------
<?php
class MoviesController extends AppController {
    public $components = array('Session');
   
   
        public function index()
        {
            $movies = $this->Movie->find('all');
            $this->set('movies', $movies);
         }
   
            public function add()
             {
                    if (!empty($this->data)) {
                    $this->Movie->create($this->data);
                    if ($this->Movie->save()) {
                    $this->Session->setFlash('The movie has been saved');
                    $this->redirect(array('action' => 'add'));
                    } else {
                    $this->Session->setFlash
                    ('The movie could not be saved. Please, try again.');
                    }
                    }
            }
               
           
            public function delete($id = null)
             {
                if (!$id) {
                $this->Session->setFlash('Invalid id for movie');
                $this->redirect(array('action' => 'index'));
                }
                if ($this->Movie->delete($id)) {
                $this->Session->setFlash('Movie deleted');
                } else {
                $this->Session->setFlash(__('Movie was not deleted',
                true));
                }
                $this->redirect(array('action' => 'index'));
             }



            function edit($id = null) {
                    if (!$id && empty($this->data)) {
                        $this->Session->setFlash('Invalid movie');
                        $this->redirect(array('action' => 'index'));
                    }
                    if (!empty($this->data)) {
                        if ($this->Movie->save($this->data)) {
                            $this->Session->setFlash('The movie has been saved');
                            $this->redirect(array('action' => 'index'));
                        } else {
                            $this->Session->setFlash('The movie could not be saved. Please, try again.');
                        }
                    }
                    if (empty($this->data)) {
                        $this->data = $this->Movie->read(null, $id);
                    }
                }
               
    function view($id = null) {
        if (!$id) {
            $this->Session->setFlash('Invalid movie');
            $this->redirect(array('action' => 'index'));
        }
        $this->set('movie', $this->Movie->findById($id));
    }
}
?>
-------------------------------------------------------------------------------------------------------------
create view file for show ,add,edit,delete under D:\xampp\htdocs\cakephp_lt\app\View\movies\(add.ctpedit.ctp,index.ctp,view.ctp)
------------------------------------------------------------------------------------------------------------

add.ctp
------------------------------------
<div class="movies form">
<?php
echo $this->Form->create('Movie');
echo $this->Form->inputs(array('title', 'genre', 'rating', 'format', 'length'));
echo $this->Form->end('Add');
?>
</div>

<div class="actions">
    <h3>Actions</h3>
    <ul>
        <li><?php echo $this->Html->link('List of bikash store', array('action' => 'index'));?></li>
    </ul>
</div>
----------------------------------
edit.ctp
--------------------------------
<div class="movies form">
<?php
echo $this->Form->create('Movie');
echo $this->Form->inputs(array('id', 'title', 'genre', 'rating', 'format', 'length'));
echo $this->Form->end('Edit');
?>
</div>
<div class="actions">
    <h3>Actions</h3>
    <ul>
        <li><?php echo $this->Html->link('List Movies',
array('action' => 'index'));?></li>
    </ul>
</div>
----------------------------------
index.ctp
--------------------------------
<div class="movies index">
    <h2>Movies</h2>
    <table cellpadding="0" cellspacing="0">
        <tr>
            <th>Title</th>
            <th>Genre</th>
            <th>Rating</th>
            <th>Format</th>
            <th>Length</th>
            <th class="actions">Actions</th>
        </tr>
    <?php foreach ($movies as $movie): ?>
    <tr>
        <td><?php echo $movie['Movie']['title']; ?> </td>
        <td><?php echo $movie['Movie']['genre']; ?> </td>
        <td><?php echo $movie['Movie']['rating']; ?> </td>
        <td><?php echo $movie['Movie']['format']; ?> </td>
        <td><?php echo $movie['Movie']['length']; ?> </td>
        <td class="actions">
            <?php echo $this->Html->link('View',
array('action' => 'view', $movie['Movie']['id'])); ?>
            <?php echo $this->Html->link('Edit',
array('action' => 'edit', $movie['Movie']['id'])); ?>
            <?php echo $this->Html->link('Delete',
array('action' => 'delete', $movie['Movie']['id']),
null, sprintf('Are you sure you want to delete %s?', $movie['Movie']['title'])); ?>
        </td>
    </tr>
    <?php endforeach; ?>
    </table>
</div>
<div class="actions">
    <h3>Actions</h3>
    <ul>
        <li><?php echo $this->Html->link('New Movie', array('action' => 'add')); ?></li>
    </ul>
</div>

----------------------------------
view.ctp
--------------------------------
<div class="movies view">
<h2>Movie</h2>
    <dl>
        <dt>Title</dt>
        <dd><?php echo $movie['Movie']['title']; ?></dd>
        <dt>Genre</dt>
        <dd><?php echo $movie['Movie']['genre']; ?></dd>
        <dt>Rating</dt>
        <dd><?php echo $movie['Movie']['rating']; ?></dd>
        <dt>Format</dt>
        <dd><?php echo $movie['Movie']['format']; ?></dd>
        <dt>Length</dt>
        <dd><?php echo $movie['Movie']['length']; ?></dd>
        <dt>Created</dt>
        <dd><?php echo $movie['Movie']['created']; ?></dd>
        <dt>Modified</dt>
        <dd><?php echo $movie['Movie']['modified']; ?></dd>
    </dl>
</div>


<div class="actions">
    <h3>Actions</h3>
    <ul>
        <li><?php echo $this->Html->link
('Edit Movie', array('action' => 'edit', $movie['Movie']['id'])); ?> </li>
        <li><?php echo $this->Html->link
('Delete Movie', array('action' => 'delete', $movie['Movie']['id']),
null, sprintf('Are you sure you want to delete # %s?', $movie['Movie']['id'])); ?> </li>
        <li><?php echo $this->Html->link
('List Movies', array('action' => 'index')); ?> </li>
        <li><?php echo $this->Html->link
('New Movie', array('action' => 'add')); ?> </li>
    </ul>
</div>
----------------------------------------------------------------------
then you can also insert, updata and delete can do but if you want to validation your form fields then you have to create e a model to validatae
-------------------------------------------------------------------------
create model uder->D:\xampp\htdocs\cakephp_lt\app\Model\(movie.php)
------------------------------------------------------------------------------
you can validate for your fields using model paste below these code
----------------------------------------------
<?php
class Movie extends AppModel {

   var $name = 'movie';  
    var $validate =array(
    'title' =>array(
               'alphaNumeric' =>array(
               'rule' => array('minLength',2),
               'required' => true,
               'message' => 'Enter should be minimum 2 only')              
               ),
    'genre' =>array(
               'alphaNumeric' =>array(
               'rule' => array('minLength',4),
               'required' => true,
               'message' => 'Enter should be minimum 4 only')              
               ),
   
           
    'rating' =>array(
               'alphaNumeric' =>array(
               'rule' => array('minLength',2),
               'required' => true,
               'message' => 'Enter should be minimum 4 only')              
               )
   
            );
  
}
?>

i hope every one can easily understand please:)

Tuesday, September 11, 2012

facebook login using php and get user emai id and orther data from face book





stp-1->include_once(COMMONINCLUDES.'classes/facebook.php');
--------------------------------------------------------------------------
stp-2
------------------------------------------------------------------------
class facebookapi {
   
   
    function facebookapi($postArr)
    {       
        global $g_criteria1;
       
        parent :: CommonClass();
        //parent :: Facebook();

       
               
        $this->CheckGetMagicQuotes($postArr);
       
        switch($g_criteria1)
        {
           
            case "nu":
                $this->News_Update();
            break;
           
            case "tm":
                $this->Testimonial();
                               
           
            case "fcall":
                $this->Facebook_Site_Callback();
           
           
            case "flogin":
                $this->Facebook_Login();
               
            case "fus":
                $this->Facebook_Save_User();
               
               
            default:
                $this->Facebook_Login();
        }
    }
   
   
    /*==============================================
        Author     :     bikash rajan nayak
        Modified By :  bikash rajan nayak
        Date    :    Nov 03, 2011
        Function to display home page
    ===================================================*/   
   
   
   
    function News_Update()
    {
   
        $smarty = new Smarty;
        $smarty->compile_check = true;
        $smarty->debugging = false;   
       
        $trail = new Trail();
        $trail->addStep('News & Update');
        $smarty->assign('TPL_PAGE_TITLE', 'News & Update');
       
        $plan_list_Order = $this->GetHomePlanList_Order_Two();
        $smarty->assign('TPL_PLAN_LIST_ORDERBY', $plan_list_Order);
       
        $news_list = $this->GetNEWS_List('I');
       
        $smarty->assign('TPL_NEWS_LIST', $news_list);
               
        $smarty->assign_by_ref('trail', $trail->path);
        $smarty->display('news-and-update.tpl');
    }
   
    function Facebook_Site_Callback()
    {
   
        $smarty = new Smarty;
        $smarty->compile_check = true;
        $smarty->debugging = false;   
               
        $hidArr = array();
        $hidArr = $this->GetHiddenVarVal($this);       
                                   
                   
                   
                   
                                $facebook = new Facebook(array('appId'  => '17378553343966053114','secret' => '63bb9447a4cf7df7d4be985bs55a6df3b648a9d',));
                                // Get User ID
                                $user = $facebook->getUser();
                               
                                if ($user)
                                {
                                try
                                {
                               
                                // Proceed knowing you have a logged in user who's authenticated.
                                    $user_profile = $facebook->api('/me');
                                   
                                   
                                        $Fuid = $user_profile['id'];
                                        $smarty->assign('FACE_UID',$Fuid);
                                       
                                        $FemailID = $user_profile['email'];
                                        $smarty->assign('FACE_EMAILID',$FemailID);
                                       
                                        $FfirstName = $user_profile['first_name'];
                                        $smarty->assign('FACE_FirstName',$FfirstName);
                                       
                                        $FlastName = $user_profile['last_name'];
                                        $smarty->assign('FACE_LastName',$FlastName);
                                       
                                        if(isset($user_profile['gender']))
                                        {
                                            $Fgender = $user_profile['gender'];
                                            $smarty->assign('FACE_gender',$Fgender);
                                        }
                                       
                                    //echo '<pre>'; print_r($user_profile); exit;
                                                                       
                                    //session_destroy();
                                                                       
                                        $u_selquery = "SELECT
                                                                    fld_id
                                                            FROM
                                                                            tbl_user
                                                            WHERE 
                                                                    fld_email = '$FemailID' AND flogin ='$Fuid' ";            
                                           
                                   
                                    $u_queryexe = mysql_query($u_selquery);
                                    if(mysql_affected_rows() > 0)
                                    {
                                       
                                                                               
                                        $whrCls1 = " WHERE (fld_email='$FemailID') AND flogin ='$Fuid' ";
                                        $table_fields1 = array('fld_id', 'fld_userName',  'fld_firstName', 'fld_lastName', 'fld_status', 'flogin');
                                        $sql_sel1 = $this->buildSelectSQL('tbl_user', $table_fields1, $whrCls1);
                                        $rs_valid1 = @mysql_query($sql_sel1);
                                       
                                            if($rs_valid1 && @mysql_num_rows($rs_valid1) > 0)
                                                {
                                               
                                                    $row = @mysql_fetch_assoc($rs_valid1);
                                                    $user_name = $row['fld_userName'];
                                                    $id = $row['fld_id'];
                                                    $fullName = $row['fld_firstName'];
                                                    $email = $FemailID;
                                                   
                                                    $facebookId = $row['flogin'];
                                                   
                                                    $objSessions = new Sessions($user_name, $id, 'customer', $fullName, $facebookId , '', $email);
                                                    $objSessions->set_sessions();
                                                   
                                                    //$_SESSION['facebookId'] = $facebookId;
                                                   
                                                   
                                                    //echo "<pre>";print_r($_SESSION);
                                                    //exit;

                                                 
//$logoutUrl = $facebook->getLogoutUrl();
//header("Location: $logoutUrl");
                                                    $this->redirectPage('myaccount.php?event=myaccount', $hidArr);   
                                                    exit;
                                                               
                                                               
                                                }
                                                else
                                                {
                                                    $hidArr = 'user name alrady exist, please choose another user name';
                                                    $this->redirectPage('myaccount.php?event=myaccount', $hidArr);   
                                                    exit;
                                               
                                                }
                   
                                   
                                    }
                                    else
                                    {
                                   
                                   
                                    //$logoutUrl = $facebook->getLogoutUrl();
                                    //header("Location: $logoutUrl");

                                    //$smarty->display('facebook-login.tpl');
                                    //exit;
                                    //07-March-Work............................................................................
                                   
                                   
                                        $Fuid = $user_profile['id'];
                                        $smarty->assign('FACE_UID',$Fuid);
                                       
                                        $FemailID = $user_profile['email'];
                                        $smarty->assign('FACE_EMAILID',$FemailID);
                                       
                                        $FfirstName = $user_profile['first_name'];
                                        $smarty->assign('FACE_FirstName',$FfirstName);
                                       
                                        $FlastName = $user_profile['last_name'];
                                        $smarty->assign('FACE_LastName',$FlastName);
                                       
                                        if(isset($user_profile['gender']))
                                        {
                                            $Fgender = $user_profile['gender'];
                                            $smarty->assign('FACE_gender',$Fgender);
                                        }
                                   
//echo "hello1";
//exit;   

                                    if($Fuid != '' && $FemailID != '' && $FfirstName != '' && $FlastName != '' )
                                    {
                                   
                                            $defaultImage = '2011_1.gif';
                                           
                                            $tbl_fld = array(
                                            'fld_userName', 'fld_email', 'fld_firstName', 'fld_lastName',
                                            'fld_sex', 'fld_image', 'fld_profileView','fld_wishView',
                                            'fld_status' ,'fld_creationDate','flogin' 
                                            );
                                            $tbl_fld_val = array(   
                                            $FemailID, $FemailID,  $FfirstName, $FlastName,
                                            $Fgender, $defaultImage, '1' , '1',
                                            '1', date('Y-m-d H:i:s'), $Fuid    
                                            );
                                           
                                            $sql_ins = $this->buildManipSQL('tbl_user', $tbl_fld, $tbl_fld_val, 'IN');
                                            $rs = mysql_query($sql_ins);
                                            $fld_id = mysql_insert_id();
                                           
                                           
                                            if($rs &&  $fld_id!='')
                                            {   
                                                    $whrCls1 = " WHERE (fld_userName ='".$FemailID."' OR fld_email='".$FemailID."') AND flogin='$Fuid'";
                                                    $table_fields1 = array('fld_id', 'fld_firstName', 'fld_lastName', 'fld_status');
                                                    $sql_sel1 = $this->buildSelectSQL('tbl_user', $table_fields1, $whrCls1);
                                                    $rs_valid1 = @mysql_query($sql_sel1);
                                                   
                                                    if($rs_valid1 && @mysql_num_rows($rs_valid1) > 0)
                                                        {
                                                           
                                                            $row = @mysql_fetch_assoc($rs_valid1);
                                                            $user_id = $row['fld_id'];
                                                           
                                                            if($row['fld_status'] == 0)
                                                            {                   
                                                                $hidArr['ec'] = NOT_ACT;                   
                                                                $hidArr['am'] = "YES";
                                                                $this->redirectPage('login.php', $hidArr);
                                                                exit;
                                                            }
                                                            else
                                                            {
                                                           
                                                                $user_name = $FemailID;
                                                                $id = $row['fld_id'];
                                                                $fullName = $row['fld_firstName'];
                                                                $email = $FemailID;
                                                                $objSessions = new Sessions($user_name, $id, 'customer', $fullName, '', '', $email);
                                                                $objSessions->set_sessions();
                                                                //echo "<pre>";print_r($_SESSION);
                                                                $this->redirectPage('myaccount.php?event=myaccount', $hidArr);   
                                                                exit;                   
                                                            }               
                                                        }
                                                   
                                           
                                            }
                               
                                   
                                    }
                                   
                                   
                                   
                                   
                                   
                                   
                                   
                                   
                                   
                                   
                                   
                                   
                                   
                                   
                                   
                                   
                                   
                                   
                                    }
                               
                                }
                                catch (FacebookApiException $e)
                                {
                                error_log($e);
                                $user = null;
                                }
                                }
                               
                               
                               
                                // Login or logout url will be needed depending on current user state.
                                if ($user) {
                               
                               
                                $logoutUrl = $facebook->getLogoutUrl();
                               
                                } else
                                {
                                    $loginUrl = $facebook->getLoginUrl(array('scope' => 'email,read_stream'));
                                    //echo '<script>self.location = '.$loginUrl.';
                                    header("Location: $loginUrl");
                                        //$hidArr['ec'] = " ";
                                        //$this->redirectPage($loginUrl, $hidArr);
                                }
                               
       
               
       
       
    }
   
   
   
   
   
   
   
   
   

    function Facebook_Login()
    {
        $smarty = new Smarty;
        $smarty->compile_check = true;
        $smarty->debugging = false;   
       
       
       
       
       
       
       
        $facebook = new Facebook(array('appId'  => '1737324242385966053114','secret' => '63bb9447a4cf7df7d4be985b3s6s53a2db648a9d',));

        // Get User ID
        echo "hjjdfgljf";
        $user = $facebook->getUser();
        if ($user) {
          try {
            // Proceed knowing you have a logged in user who's authenticated.
            $user_profile = $facebook->api('/me');
          } catch (FacebookApiException $e) {
            error_log($e);
            $user = null;
          }
        }
    //echo '<pre>'; print_r($user_profile); exit;
       
        $Fuid = $user_profile['id'];
        $smarty->assign('FACE_UID',$Fuid);
       
        $FemailID = $user_profile['email'];
        $smarty->assign('FACE_EMAILID',$FemailID);
       
        $FfirstName = $user_profile['first_name'];
        $smarty->assign('FACE_FirstName',$FfirstName);
       
        $FlastName = $user_profile['last_name'];
        $smarty->assign('FACE_LastName',$FlastName);
       
        $Fgender = $user_profile['gender'];
        $smarty->assign('FACE_gender',$Fgender);
       
        //echo '<pre>'; print_r( $user_profile); exit;
        //echo "<pre>"; print_r($_SESSION); exit;   
        //echo $facebook->getLogoutUrl();
       
       
        $cmsPageContent = $this->GetHomeCMSPage(1);
        $pageUrl = $this->getPageURLString();
        $smarty->assign('MENU_SEL',$pageUrl);
       
        //echo $cmsPageTitle = $cmsPageContent[0]['id'];
        $cmsPageTitle = $cmsPageContent[0]['title'];
        $cmsPageContent = $cmsPageContent[0]['content'];
       
        $smarty->assign('TPL_CMS_PAGE_TITLE', $cmsPageTitle);
        $smarty->assign('TPL_CMS_PAGE_CONTENT', $cmsPageContent);
               
       
       
       
        //echo "<pre>"; print_r($cmsPageContent); exit;
        //$smarty->assign('TPL_PLAN_LIST_ORDERBY', $plan_list_Order);
       
       
        $eventSlider = $this->GetUserEventSlider();
        $smarty->assign('event_list', $eventSlider);   
        //echo "<pre>";    print_r($eventSlider);exit;
               
        $smarty->display('facebook-login.tpl');
       
       
       
    }
   
   
   
    function Facebook_Save_User()
    {
   
        $smarty = new Smarty;
        $smarty->compile_check = true;
        $smarty->debugging = false;
       
        $hidArr = array();
        //$hidArr = $this->GetHiddenVarVal($this);
       
        $status = '0';       
        $ipaddress=$_SERVER['REMOTE_ADDR'];   
       
               
        //echo "<br />";
         date('Y-m-d H:i:s');
        //exit;
        $defaultImage = '2011_1.gif';
               
         if($this->hidface_gender == 'male')
         {
             $sex = '1';
         }
         else
         {
             $sex = '0';
         }
       
       
         if($this->txtemailID != '')
        {
            $u_selquery = "SELECT
                                fld_id
                         FROM
                                 tbl_user
                        WHERE 
                                fld_userName = '$this->txtUserName' OR  fld_email = '$this->txtemailID' ";            
       
         $u_queryexe = mysql_query($u_selquery);
         if(mysql_affected_rows() > 0)
             {
                $hidArr['ec'] = 'User Name or Email ID already exit Plz choose another name';
                $this->redirectPage('sign-up.php?event=ur', $hidArr);
            }
         }   
       
               
       
       
         if($this->hidface_emailid != '' && $this->hidface_fname != '' && $this->hidface_lname != '' && $this->hidface_gender != '' && $this->txtUserName != ''
            && $this->txtPassword != ''
            )
        {
         
       
            $defaultImage = '2011_1.gif';
            $tbl_fld = array(
                                'fld_userName', 'fld_email', 'fld_password', 'fld_firstName', 'fld_lastName',
                                'fld_sex', 'fld_image', 'fld_profileView','fld_wishView','fld_ipAddr',
                                'fld_status' ,'fld_creationDate','flogin' 
                            );
            $tbl_fld_val = array(   
                                    $this->txtUserName, $this->hidface_emailid,  md5($this->txtPassword), $this->hidface_fname, $this->hidface_lname,
                                    $sex, $defaultImage, '1' , '1',  $ipaddress,
                                    '1', date('Y-m-d H:i:s'), $this->hidface_id    
                                );
           
            $sql_ins = $this->buildManipSQL('tbl_user', $tbl_fld, $tbl_fld_val, 'IN');
            $rs = mysql_query($sql_ins);
            $fld_id = mysql_insert_id();
                               
            if($rs)
            {   
           
                 $txt_pass = md5($this->txtPassword);
            if($this->txtUserName != '' && $fld_id != '')
            {
           
                       
               $whrCls1 = " WHERE (fld_userName ='".$this->txtUserName."' OR fld_email='".$this->hidface_emailid."') AND fld_password='$txt_pass'";
               $table_fields1 = array('fld_id', 'fld_firstName', 'fld_lastName', 'fld_status');
                 $sql_sel1 = $this->buildSelectSQL('tbl_user', $table_fields1, $whrCls1);
       
           
            $rs_valid1 = @mysql_query($sql_sel1);
            if($rs_valid1 && @mysql_num_rows($rs_valid1) > 0)
            {
               
                $row = @mysql_fetch_assoc($rs_valid1);
                $user_id = $row['fld_id'];
               
                if($row['fld_status'] == 0)
                {                   
                    $hidArr['ec'] = NOT_ACT;                   
                    $hidArr['am'] = "YES";
                    $this->redirectPage('login.php', $hidArr);
                    exit;
                }
                else
                {
               
                    $user_name = $this->txtUserName;
                    $id = $row['fld_id'];
                    $fullName = $row['fld_firstName'];
                    $email = $this->txtUserName;
                    $objSessions = new Sessions($user_name, $id, 'customer', $fullName, '', '', $email);
                    $objSessions->set_sessions();
                    echo "<pre>";print_r($_SESSION);
                    $this->redirectPage('myaccount.php?event=myaccount', $hidArr);   
                    exit;                   
                }               
            }
           
        }   
            }
            else
            {
                $hidArr['ec'] = USER_ADD_FAIL;
                $this->redirectPage('sign-up.php?event=ur', $hidArr);
            }
             }
        else
        {

            $hidArr['ec'] = MEND_FIELDS;
            $this->redirectPage('sign-up.php?event=ur', $hidArr);
            exit;
        }
       
    }

 }
---------------------end face book class--------------------------------------------

last step pass user request for login
include_once('classes/facebookapi.class.php');

$obj_Facebookapi = new facebookapi($_REQUEST);