1
Thulani
please help with Xoops classes using xoopshandlers
  • 2012/4/23 10:39

  • Thulani

  • Just popping in

  • Posts: 3

  • Since: 2012/4/23


*My module folder name is "tutorial" ,and my class name is "software" ,and my php admin database name is "software_myform"..I am trying to insert data into a form.

<?php
//Capture Softwares
require('../../mainfile.php');
require(
XOOPS_ROOT_PATH.'/header.php');


global 
$xoopsTpl$xoopsOption$xoopsUser;


if(!(
is_object($xoopsUser))){

      echo(
'you need to be logged in to Capture Softwares.');
   }

$op ='';


if(isset(
$_POST['op'])) {
    
$op $_POST['op'];
}



switch(
$op) {
    
        case 
'post':
        
       
$SoftwaresSoftwareHandler =& xoops_getModuleHandler('software','tutorial');
        
        
$software_id $_POST['software_id'];

        if (
$Software_id == 0) {
            
$Softwareobj =  $SoftwaresSoftwareHandler->create();
        }
   

        else {
            
$Softwareobj $SoftwaresSoftwareHandler->get($software_id);
        }   

        
$Softwareobj->setVar("software_id"$_POST['software_id']);
        
$Softwareobj->setVar("software_title"$_POST['software_title']);
        
$Softwareobj->setVar("publisher"$_POST['publisher']);
        
$Softwareobj->setVar("license_key"$_POST['license_key']);
        
$Softwareobj->setVar("total_no_licenses"$_POST['total_no_licenses']);
        
$Softwareobj->setVar("S_Date"time()); 
        
$Softwareobj->setVar("userid"$GLOBALS['xoopsUser']->getVar("uid"));
       } 
         
     if(!(
$SoftwaresSoftwareHandler->insert($Softwareobj))){
         echo 
"Error";
  }     else {
          echo 
"successful";
   }  


    if(
$SoftwareObj->isNew()) {  //update User posts
                
if(is_object($xoopsUser)) {
                    
$uid $xoopsUser->GetVar('uid');
                    if (
$uid 0) {
                        
$member_handler =& xoops_gethandler('member');
                        
$poster =& $member_handler->getUser($uid);
                        if (
is_object($poster)) {
                            
$member_handler->updateUserByField($poster'posts'$poster->getVar('posts') + 1);
                        }
                    }
                }

            }  

 return 
true;


require(
XOOPS_ROOT_PATH.'/footer.php');
?>

//here is my class:
<?php

if (!defined("XOOPS_ROOT_PATH")) { die('root path not defined');
}
 
    
class 
tutorialsoftware extends XoopsObject {
     
     var 
$_groups_read null;
  
      function 
tutorialsoftware()
    {
        
$this->__construct();
    }
   
     function 
__construct()
    {
       
// $this->XoopsObject();
        
$this->initVar('software_id',          XOBJ_DTYPE_INT);
        
$this->initVar('software_title',       XOBJ_DTYPE_TXTBOX,null,255);
        
$this->initVar('publisher',            XOBJ_DTYPE_TXTBOX,null,255);
        
$this->initVar('license_key',          XOBJ_DTYPE_TXTBOX,null,255);
        
$this->initVar('total_no_licenses',    XOBJ_DTYPE_INT);
        
$this->initVar('S_Date',               XOBJ_DTYPE_INT);
        
$this->initVar('userid',               XOBJ_DTYPE_INT);
 
    }

 }   
  

  class 
tutorialsoftwareHandler extends XoopsPersistableObjectHandler
{

   function 
SoftwaresSoftwareHandler(&$db)
    {
        
$this->__construct($db);
    } 
  
    function 
__construct(&$db
    {
        
parent::__construct($db,'software_myform','tutorialsoftware''software_id');
    }
}
?>


//here is my form

<html>
<
head>
<
body>
<
form name="form" method="post" action="capture.php">
  <
table width="400" border="0">
    <
tr>
      <
td align="right">Software Title</td>
      <
td><input type="text" name="software_title"></td>
    </
tr><tr>
      <
td align="right">Purchase</td>
      <
td><input type="text" name="publisher"></td>
    </
tr><tr>
      <
td align="right">License key</td>
      <
td><input type="text" name="license_key"></td>
    </
tr><tr>
      <
td align="right">Total no licenses</td>
      <
td><input type="text" name="total_no_licenses"></td>
    </
tr><tr>
      
     
      <
td>
     <
input type="submit" name="submit" value="submit"></td>
       
 </
table>
</
head>
</
body>
</
form>





2
zyspec
Re: please help with Xoops classes using xoopshandlers
  • 2012/4/23 16:52

  • zyspec

  • Module Developer

  • Posts: 1095

  • Since: 2004/9/21


Here's a few recommendations:
* You need to make sure to sanitize any data before you attempt to put it into the database.
* Pay close attention to camelcase when creating/using class names.

NOTE: The code below has not been tested so you might find some syntax errors, etc. but I think it should work pretty much 'as is'.

in ./modules/tutorial/capture.php
<?php
//Capture Softwares
require('../../mainfile.php');
require(
XOOPS_ROOT_PATH.'/header.php');

global 
$xoopsTpl$xoopsOption$xoopsUser;

$myts =& MyTextSanitizer::getInstance();

if(!(
is_object($xoopsUser))){
    echo(
'you need to be logged in to Capture Softwares.');
}

$op ='';

if(isset(
$_POST['op'])) {
 
$op $_POST['op'];
}

switch(
$op) {
    case 
'post':
        
$SoftwaresSoftwareHandler =& xoops_getModuleHandler('software','tutorial');

        
$software_id       = (!empty($_POST['software_id'])) ? (int) $_POST['software_id'] : ;
        
$software_title    = (isset($_POST['software_title'])) ? $myts->addSlashes($_POST['software_title'] : '';
        
$publisher         = (isset($_POST['publisher'])) ? $myts->addSlashes($_POST['publisher'] : '';
        
$license_key       = (isset($_POST['license_key'])) ? $myts->addSlashes($_POST['license_key'] : '';
        
$total_no_licenses = (isset($_POST['total_no_licenses']) && ((int)$_POST['total_no_licenses'] > 0)) ? (int)($_POST['total_no_licenses']) : 0;
        
$Softwareobj->setVar('software_title') = $software_title;
        
$Softwareobj->setVar('publisher') = $publisher;
        
$Softwareobj->setVar('total_no_licenses') = $total_no_licenses;
        
$Softwareobj->setVar("S_Date"time()); 
        
$Softwareobj->setVar("userid"$GLOBALS['xoopsUser']->getVar("uid"));

        if (
>= $software_id) {
            
$Softwareobj $SoftwaresSoftwareHandler->create();
        } else {
            
$Softwareobj $SoftwaresSoftwareHandler->get($software_id);
        } 


if(!(
$SoftwaresSoftwareHandler->insert($Softwareobj))){
    echo 
"Error";
} else {
    echo 
"successful";
}

if(
$SoftwareObj->isNew() && (is_object($xoopsUser))) { //update User posts
    
$uid $xoopsUser->getVar('uid');
    if (
$uid 0) {
        
$member_handler =& xoops_gethandler('member');
        
$poster =& $member_handler->getUser($uid);
        if (
is_object($poster)) {
            
$member_handler->updateUserByField($poster'posts'$poster->getVar('posts') + 1);
        }
    }


//return true;


require(XOOPS_ROOT_PATH.'/footer.php');
?>


in ./modules/tutorial/class/software.php
<?php

if (!defined("XOOPS_ROOT_PATH")) { die('root path not defined');
}
 

class 
tutorialSoftware extends XoopsObject {

    var 
$_groups_read null;

    function 
tutorialSoftware()
    {
        
$this->__construct();
    }

    function 
__construct()
    {
        
// $this->XoopsObject();
        
$this->initVar('software_id'XOBJ_DTYPE_INT);
        
$this->initVar('software_title'XOBJ_DTYPE_TXTBOX,null,255);
        
$this->initVar('publisher'XOBJ_DTYPE_TXTBOX,null,255);
        
$this->initVar('license_key'XOBJ_DTYPE_TXTBOX,null,255);
        
$this->initVar('total_no_licenses'XOBJ_DTYPE_INT);
        
$this->initVar('S_Date'XOBJ_DTYPE_INT);
        
$this->initVar('userid'XOBJ_DTYPE_INT);

    }



class 
tutorialSoftwareHandler extends XoopsPersistableObjectHandler
{
    function 
SoftwaresSoftwareHandler(&$db)
    {
        
$this->__construct($db);
    } 

    function 
__construct(&$db
    {
        
parent::__construct($db,'software_myform','tutorialSoftware''software_id');
    }
}
?>


Your form file should be okay. Although the way it's implemented will only let you add new objects to the database, you will need to modify the form to allow you to edit existing objects.

Hope this helps you get started...

3
Mamba
Re: please help with Xoops classes using xoopshandlers
  • 2012/4/23 17:13

  • Mamba

  • Moderator

  • Posts: 11366

  • Since: 2004/4/23


Since current XOOPS requires PHP 5.2+, we don't need this anymore:

function tutorialSoftware() 
    { 
        
$this->__construct(); 
    }

or this:

function SoftwaresSoftwareHandler(&$db
    { 
        
$this->__construct($db); 
    }


as the constructor will be called automatically, correct?
Support XOOPS => DONATE
Use 2.5.10 | Docs | Modules | Bugs

4
redheadedrod
Re: please help with Xoops classes using xoopshandlers

Quote:

Mamba wrote:
Since current XOOPS requires PHP 5.2+, we don't need this anymore:

function tutorialSoftware() 
    { 
        
$this->__construct(); 
    }

or this:

function SoftwaresSoftwareHandler(&$db
    { 
        
$this->__construct($db); 
    }


as the constructor will be called automatically, correct?


You are correct Mamba.
function tutorialSoftware()


Is the old constructor under php4

function __construct()


Is the php5 constructor.

You only need to use that if you need to support php4 and since xoops will not run in php4 there is no need to use the older construct.

Not sure why they changed them since all other languages I have dealt with use the php4 style constructor.
Attending College working towards Bachelors in Software Engineering and Network Security.

5
Thulani
Re: please help with Xoops classes using xoopshandlers
  • 2012/4/24 14:24

  • Thulani

  • Just popping in

  • Posts: 3

  • Since: 2012/4/23


Thank somuch it worked pretty well fine :)

6
Thulani
Re: please help with Xoops classes using xoopshandlers
  • 2012/4/24 14:26

  • Thulani

  • Just popping in

  • Posts: 3

  • Since: 2012/4/23


I'm happy thanks it works.

Login

Who's Online

216 user(s) are online (118 user(s) are browsing Support Forums)


Members: 0


Guests: 216


more...

Donat-O-Meter

Stats
Goal: $100.00
Due Date: Mar 31
Gross Amount: $0.00
Net Balance: $0.00
Left to go: $100.00
Make donations with PayPal!

Latest GitHub Commits