Page 1 of 1

How to use classes and function in external pages ?

PostPosted: Tue Feb 09, 2010 1:40 pm
by pyroboynroses
Hello,

I'm setting a paypal button payment system on my xataface website and I need help to insert the id of the logged in user.

The button file is named pack15.php and it contains this code :
Code: Select all
<HTML>
  <HEAD>
  <TITLE>Test</TITLE>
  </HEAD>
  <BODY>
  <H1>Pack de 15 SMS</H1>
  <form target="paypal" action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="business" value="myemail@fai.com">
<input type="hidden" name="lc" value="FR">

<input type="hidden" name="item_name" <?php
mysql_connect(localhost, 'root','') or die("Erreur de connexion au serveur");
mysql_select_db('lepensebete') or die("Erreur de connexion à la base de donnees");
$query = "SELECT * FROM t_pack WHERE nom_pack = 'Pack de 15 SMS'";
$result = mysql_query($query);
$row = mysql_fetch_row($result);
echo 'value="'.$row[1].'"';
mysql_close();
?>>

<input type="hidden" name="amount" <?php
mysql_connect(localhost, 'root','') or die("Erreur de connexion au serveur");
mysql_select_db('lepensebete') or die("Erreur de connexion à la base de donnees");
$query = "SELECT * FROM t_pack WHERE nom_pack = 'Pack de 15 SMS'";
$result = mysql_query($query);
$row = mysql_fetch_row($result);
echo 'value="'.$row[4].'"';
mysql_close();
?>>

<input type="hidden" name="currency_code" value="EUR">

<input type="hidden" name="custom" <?php
class test {
function test() {
$auth =& Dataface_AuthenticationTool::getInstance();
$user =& $auth->getLoggedInUser();
$id = $user->val('id');
echo 'value="'.$id.'"';
}
}
?>>

<input type="hidden" name="button_subtype" value="products">
<input type="hidden" name="tax_rate" value="5.50">
<input type="hidden" name="shipping" value="0.00">
<input type="hidden" name="add" value="1">
<input type="hidden" name="bn" value="PP-ShopCartBF:btn_cart_LG.gif:NonHostedGuest">
<input type="image" src="https://www.paypal.com/fr_FR/FR/i/btn/btn_cart_LG.gif" border="0" name="submit" alt="PayPal - la solution de paiement en ligne la plus simple et la plus sécurisée !">
<img alt="" border="0" src="https://www.paypal.com/fr_FR/i/scr/pixel.gif" width="1" height="1">
</form>
  </BODY>
  </HTML>


So I can in this file put database data in parameters of the form with a mysql query but I can't get the id of the logged in user with my class :
Code: Select all
<?php
class test {
function test() {
$auth =& Dataface_AuthenticationTool::getInstance();
$user =& $auth->getLoggedInUser();
$id = $user->val('id');
echo 'value="'.$id.'"';
}
}
?>>


I haven't got errors but the field is empty...

How can i do that ?

Thank you for the help

Re: How to use classes and function in external pages ?

PostPosted: Wed Feb 10, 2010 7:51 am
by pyroboynroses
So I try several things.

I inspired from the index.php file needed in a xataface website.

So I tried that in my external page :

Code: Select all
<?php
require_once '../xataface/dataface-public-api.php';
df_init(__FILE__, 'http://localhost/mysite/xataface');

$auth =& Dataface_AuthenticationTool::getInstance();
$user =& $auth->getLoggeInUser();
$id = $user->val('id');
?>


But this code doesn't work. I have this error :

Fatal error: Class 'Dataface_AuthenticationTool' not found in ...

So how can I fix that ???

Thanks for your help.

Re: How to use classes and function in external pages ?

PostPosted: Wed Feb 10, 2010 8:09 am
by pyroboynroses
Ok, I tried an echo command on the $user variable and it is empty !!!

Why ???

Re: How to use classes and function in external pages ?

PostPosted: Wed Feb 10, 2010 9:34 am
by shannah
Is there any reason why you want to do this as a separate page and not as a custom action?

Re: How to use classes and function in external pages ?

PostPosted: Wed Feb 10, 2010 9:39 am
by pyroboynroses
I would like to do that because it's he code of a paypal button called by a link in a xataface record.

But if there is a way to do that in a custom action, I would be happy to know how I can do that. :D

Thanks for the answer.

Re: How to use classes and function in external pages ?

PostPosted: Wed Feb 10, 2010 9:46 am
by shannah
Simple custom action:

file: actions/hello.php
Code: Select all
<?php
class actions_hello {
    function handle($params){
        echo "Hello world";
    }
}


Call your action with:
index.php?-action=hello

Custom Action using template:

file: actions/hello.php:
Code: Select all
<?php
class actions_hello {
    function handle($params){
        df_display(array('name'=>'Steve'), 'hello.html');
    }
}


file: templates/hello.html:
Code: Select all
<html><head><title>Hello {$name}</title></head>
<body>
<h1>Hello {$name}</h1>


Still access your action via index.php?-action=hello


Custom action using template that inherits from Xataface's Main Template:
Everything same as previous example except templates/hello.html now looks like:
Code: Select all
{use_macro file="Dataface_Main_Template.html"}
{fill_slot name="main_column"}
<h1>Hello {$name}</h1>
{/fill_slot}
{/use_macro}

Re: How to use classes and function in external pages ?

PostPosted: Wed Feb 10, 2010 11:40 pm
by pyroboynroses
Thanks !!

I'vz tried it and it works perfectly. :D

I will post the whole code for the paypal button soon and how it works with xataface.

Re: How to use classes and function in external pages ?

PostPosted: Sat Feb 13, 2010 6:11 am
by pyroboynroses
So this is the code in my record to have a button which point to the paypal.php action :
Code: Select all
<form method="post" action="http://127.0.0.1/lepensebete/xatagestion/index.php?-action=paypal" >
<input type="hidden" name="pack_name" value="Pack de 15 SMS">
<input type="submit" value="Acheter avec Paypal">
</form>


this button send with a post method the pack name to the paypal action (to find the correspondant record in the paypal action).

And this is the code for the paypal action (paypal.php in the actions directory) :
Code: Select all
<HTML>
  <HEAD>
  <TITLE>Test</TITLE>
  </HEAD>
  <BODY>
  <H1>Ajouter un pack de SMS au panier</H1>
  <?php
  class actions_paypal {
    function handle($params){

$auth =& Dataface_AuthenticationTool::getInstance();
$user =& $auth->getLoggedInUser();
$id = $user->val('id');
 
$pack_name = $_POST["pack_name"];

mysql_connect(localhost, 'root','') or die("Erreur de connexion au serveur");
mysql_select_db('lepensebete') or die("Erreur de connexion à la base de donnees");
$query = "SELECT * FROM t_pack WHERE nom_pack = '".$pack_name."'";
$result = mysql_query($query);

$row = mysql_fetch_row($result);
mysql_close();

$item_name = $row[1];
$nb_sms = $row[2];
$prix_unit = $row[3];
$amount = $row[4];

echo '
<center>
<TABLE BORDER="1">
   <CAPTION> <br>R&eacute;capitulatif du pack s&eacute;lectionn&eacute;</CAPTION>
   <TR>
      <TD>Nom du pack</TD>
      <TD>Nombre de SMS</TD>
      <TD>Prix TTC unit SMS</TD>
      <TD>Prix TTC du Pack</TD>
      <TD>id utilisateur</TD>
   </TR>
      
   <TR>
      <TD> <center>'.$item_name.'</center></TD>
      <TD> <center>'.$nb_sms.'</center></TD>
      <TD> <center>'.$prix_unit.' &#8364</center></TD>
      <TD> <center>'.$amount.' &#8364</center></TD>
      <TD> <center>'.$id.'</center></TD>
   </TR>
</TABLE>
<br>
En cliquant sur "Ajouter au panier", vous aller &ecirc;tre redirig&eacute; sur le site de paiement Paypal.
<br>
<br>
<form target="paypal" action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="business" value="adrien.grelet@gmail.com">
<input type="hidden" name="lc" value="FR">
<input type="hidden" name="item_name" value="'.$item_name.' (prix du SMS : '.$prix_unit.' EUR)">
<input type="hidden" name="amount" value="'.$amount.'">
<input type="hidden" name="currency_code" value="EUR">
<input type="hidden" name="custom" value="'.$id.'">
<input type="hidden" name="button_subtype" value="products">
<input type="hidden" name="tax_rate" value="0.00">
<input type="hidden" name="shipping" value="0.00">
<input type="hidden" name="add" value="1">
<input type="hidden" name="bn" value="PP-ShopCartBF:btn_cart_LG.gif:NonHostedGuest">
<input type="image" src="https://www.paypal.com/fr_FR/FR/i/btn/btn_cart_LG.gif" border="0" name="submit" alt="PayPal - la solution de paiement en ligne la plus simple et la plus sécurisée !">
<img alt="" border="0" src="https://www.paypal.com/fr_FR/i/scr/pixel.gif" width="1" height="1">
</form></center>';
}
}
?>
  </BODY>
  </HTML>


The paypal button is in fact an html form with hidden fields.

If you have any question, I could answer :wink:

Re: How to use classes and function in external pages ?

PostPosted: Tue Feb 16, 2010 1:13 pm
by shannah
Thanks for posting the code. Glad to hear it works. One suggestion as a matter of style and convention is that delegate classes shouldn't really be embedded in HTML like this.

Better to place the header and footer stuff either inside an echo statement inside your handle() method, or use a template (best solution).

-Steve