Forums / Developer / Shop Donation

Shop Donation

Author Message

Claudia Kosny

Thursday 15 February 2007 12:34:28 am

Hi Bruce

I did the extension more as a proof of concept, it is not something that is fully tested and there is a lot of error checking missing. Also it does not deal with multiple currencies or languages. So I'll just post the main code here, maybe someone else can use it.
To decrease the number of new objects it might be good idea to offer a list of common values and below that the input form to specify a custom value. I think quite few people rather choose a set value then bother to type their own.

General setup:
The module has the name 'donation'.
You have a special class which has only two attributes, first the name, second the price. (Otherwise you need to adapt the the code where the contentattributes are filled with values).

The form to donate something looks like this:

<form action={'donation/add'|ezurl()} method="post">
 <input type="hidden" name="amount" value="5" />
 <button type="submit" name="addDonationButton">{'Donate 5 Euros'|i18n('my')}</button>
</form>
<form action={'donation/add'|ezurl()} method="post">
 <label for="custom_amount">{'Custom amount'|i18n('my')}</label>
 <input type="text" id="custom_amount" name="amount" />
 <button type="submit" name="addDonationButton">{'Donate'|i18n('my')}</button>
</form>

This will post the amount to the view add in the module donation, which calls the following script:

<?php
include_once('lib/ezutils/classes/ezmodule.php');
include_once('kernel/common/template.php');
include_once('kernel/classes/ezcontentobjecttreenode.php');
include_once('kernel/classes/ezcontentobject.php');
include_once('lib/ezutils/classes/ezexecution.php');
include_once( "lib/ezutils/classes/ezhttptool.php" );
include_once( "kernel/classes/ezbasket.php" );

$http =& eZHTTPTool::instance();
$Module =& $Params['Module'];
$tpl =& templateInit();

//the submit button of the buy form must have the name 'addDonationButton'
if(isset($_POST['addDonationButton']))
{
  //fetch amount of donated money
  $amount = isset($_POST['amount']) ? (float)str_replace(',', '.', $_POST['amount']) : 0.00;
  
  if(!$amount)
  {
    eZDebug::writeDebug('No valid amount for donation', 'donation/add_donation.php');
    //@TODO: redirect back to previous page with message that the price must be more than 0.00
  }
  
  //class id of donation class
  $donationClassId = 35;  
  
  //id of parent node for donation objects, should be some folder that does not appear in menus
  $donationParentNodeId = 2; 
   
  //all donation objects have remote id that indicates the amount
  $remoteId = createRemoteId($amount);
 eZDebug::addTimingPoint('foo'); 
  //check whether there is already an object id with this amount (by their remote id), so we can reuse it
  $objectId = fetchObjectIdByRemoteId($remoteId);
  //create a new donation object if necessary
  if(!$objectId)
  {
    $class =& eZContentClass::fetch($donationClassId);
    //14 is the id of the admin user
    $donationObject =& $class->instantiate(14);
    $donationObject->setAttribute('remote_id', $remoteId);
    $donationObject->store();
    
    $objectId = $donationObject->attribute('id');
    
    $nodeAssignment =& eZNodeAssignment::create(array('contentobject_id' => $objectId,
                                                      'contentobject_version' => $donationObject->attribute('current_version'),
                                                      'parent_node' => $donationParentNodeId,
                                                      'is_main' => 1));
    $nodeAssignment->store(); 
    
    $version =& $donationObject->version(1);
    $contentObjectAttributes =& $version->contentObjectAttributes();
    //first attribute is the name
    //@TODO Create this in the appropriate language
    $contentObjectAttributes[0]->setAttribute('data_text', 'Donation' . ' - ' . $amount);
    $contentObjectAttributes[0]->store();
    //second attribute is the price
    $content =& $contentObjectAttributes[1]->attribute('content');
    $content->setPrice($amount);
    $contentObjectAttributes[1]->setAttribute('data_float', $amount);
    $contentObjectAttributes[1]->store();
    
    $donationObject->store();
    
    //now publish the object
    include_once('lib/ezutils/classes/ezoperationhandler.php');                                            
    $operationResult = eZOperationHandler::execute('content', 'publish', array('object_id' => $objectId,
                                                                               'version' => $donationObject->attribute('current_version')));
  }
  eZDebug::addTimingPoint('bar'); 
 
  //fake the post variables that are usually sent when adding something to the basket
  $http->setPostVariable('ContentObjectID', $objectId);
  $http->setPostVariable('ActionAddToBasket', 'ActionAddToBasket');
  
  $basket =& eZBasket::currentBasket();
  $basket->updatePrices(); 
  $http->setSessionVariable( "FromPage", $_SERVER['HTTP_REFERER'] );
  $http->setSessionVariable( "AddToBasket_OptionList_" . $objectId, array() );
    
  $Module->redirectTo( "/shop/add/" . $objectId );
}

//@TODO redirect to some appropriate page if this is called without valid posted value



//*************************************** helper function ******************************//

function createRemoteId($amount)
{
  return md5('donationObject' . $amount);
}

//this code is stolen from ezcontentobject::fetchByRemoteID(), which fetches the object itself
//we need just the id, therefore this function
function fetchObjectIdByRemoteId($remoteId)
{
  $db =& eZDB::instance();
  $cleanRemoteId = $db->escapeString($remoteId);
  $resultArray   = $db->arrayQuery( 'SELECT id FROM ezcontentobject WHERE remote_id=\'' . $cleanRemoteId . '\'' );
  if (count( $resultArray ) != 1)
  {
    return false;
  }
  else
  {
    return $resultArray[0]['id'];
  }
}

?>

Claudia