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

eZ debug

Timing: Jan 21 2025 06:01:52
Script start
Timing: Jan 21 2025 06:01:52
Module start 'content'
Timing: Jan 21 2025 06:01:54
Module end 'content'
Timing: Jan 21 2025 06:01:54
Script end

Main resources:

Total runtime1.8804 sec
Peak memory usage4,096.0000 KB
Database Queries186

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0076 589.5391180.8516
Module start 'content' 0.00761.5711 770.3906449.9141
Module end 'content' 1.57870.3013 1,220.3047355.0938
Script end 1.8800  1,575.3984 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00430.2282210.0002
Check MTime0.00150.0781210.0001
Mysql Total
Database connection0.00130.067910.0013
Mysqli_queries1.781994.76591860.0096
Looping result0.00230.12111840.0000
Template Total1.839197.820.9195
Template load0.00250.134820.0013
Template processing1.836597.668220.9183
Template load and register function0.00020.010710.0002
states
state_id_array0.00180.093410.0018
state_identifier_array0.00290.152420.0014
Override
Cache load0.00200.1061140.0001
Sytem overhead
Fetch class attribute can translate value0.00140.076320.0007
Fetch class attribute name0.00050.029210.0005
XML
Image XML parsing0.00050.023920.0002
class_abstraction
Instantiating content class attribute0.00000.000210.0000
General
dbfile0.02511.3325210.0012
String conversion0.00000.000330.0000
Note: percentages do not add up to 100% because some accumulators overlap

CSS/JS files loaded with "ezjscPacker" during request:

CacheTypePacklevelSourceFiles
CSS0extension/community/design/community/stylesheets/ext/jquery.autocomplete.css
extension/community_design/design/suncana/stylesheets/scrollbars.css
extension/community_design/design/suncana/stylesheets/tabs.css
extension/community_design/design/suncana/stylesheets/roadmap.css
extension/community_design/design/suncana/stylesheets/content.css
extension/community_design/design/suncana/stylesheets/star-rating.css
extension/community_design/design/suncana/stylesheets/syntax_and_custom_tags.css
extension/community_design/design/suncana/stylesheets/buttons.css
extension/community_design/design/suncana/stylesheets/tweetbox.css
extension/community_design/design/suncana/stylesheets/jquery.fancybox-1.3.4.css
extension/bcsmoothgallery/design/standard/stylesheets/magnific-popup.css
extension/sevenx/design/simple/stylesheets/star_rating.css
extension/sevenx/design/simple/stylesheets/libs/fontawesome/css/all.min.css
extension/sevenx/design/simple/stylesheets/main.v02.css
extension/sevenx/design/simple/stylesheets/main.v02.res.css
JS0extension/ezjscore/design/standard/lib/yui/3.17.2/build/yui/yui-min.js
extension/ezjscore/design/standard/javascript/jquery-3.7.0.min.js
extension/community_design/design/suncana/javascript/jquery.ui.core.min.js
extension/community_design/design/suncana/javascript/jquery.ui.widget.min.js
extension/community_design/design/suncana/javascript/jquery.easing.1.3.js
extension/community_design/design/suncana/javascript/jquery.ui.tabs.js
extension/community_design/design/suncana/javascript/jquery.hoverIntent.min.js
extension/community_design/design/suncana/javascript/jquery.popmenu.js
extension/community_design/design/suncana/javascript/jScrollPane.js
extension/community_design/design/suncana/javascript/jquery.mousewheel.js
extension/community_design/design/suncana/javascript/jquery.cycle.all.js
extension/sevenx/design/simple/javascript/jquery.scrollTo.js
extension/community_design/design/suncana/javascript/jquery.cookie.js
extension/community_design/design/suncana/javascript/ezstarrating_jquery.js
extension/community_design/design/suncana/javascript/jquery.initboxes.js
extension/community_design/design/suncana/javascript/app.js
extension/community_design/design/suncana/javascript/twitterwidget.js
extension/community_design/design/suncana/javascript/community.js
extension/community_design/design/suncana/javascript/roadmap.js
extension/community_design/design/suncana/javascript/ez.js
extension/community_design/design/suncana/javascript/ezshareevents.js
extension/sevenx/design/simple/javascript/main.js

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1node/view/full.tplfull/forum_topic.tplextension/sevenx/design/simple/override/templates/full/forum_topic.tplEdit templateOverride template
1content/datatype/view/ezxmltext.tpl<No override>extension/community_design/design/suncana/templates/content/datatype/view/ezxmltext.tplEdit templateOverride template
5content/datatype/view/ezxmltags/paragraph.tpl<No override>extension/ezwebin/design/ezwebin/templates/content/datatype/view/ezxmltags/paragraph.tplEdit templateOverride template
2content/datatype/view/ezxmltags/line.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/line.tplEdit templateOverride template
2content/datatype/view/ezxmltags/literal.tpl<No override>extension/community/design/standard/templates/content/datatype/view/ezxmltags/literal.tplEdit templateOverride template
1pagelayout.tpl<No override>extension/sevenx/design/simple/templates/pagelayout.tplEdit templateOverride template
 Number of times templates used: 12
 Number of unique templates used: 6

Time used to render debug report: 0.0002 secs