0

I am trying to build a custom module that uses a theme page to display a list of items withing a table. Each item has a unique ID. and I would like to have links within this table so that my users can preform actions using the uid. The link works and takes you to the form but it doesn't carry over the uid.

Here is the code from my theme file.

<?php

/**
 * @file
 * definition of uc-bitcoin-account-outgoing-escrows.tpl.php.
 */
$user = user_load($variables['uid']);

global $base_url;

$transactions = db_query("SELECT * FROM {uc_btc_escrow_payment} WHERE from_account='$user->uid'");

$header = array(t('Escrow ID'), t('Amount'), t('Payment'), t('Allocated to'), t('Release'), t('Dispiute'), t('Date'));

$rows = array();

foreach ($transactions as $transaction) {
  $rows[] = array(
      $transaction->escrow_id, $transaction->amount, $transaction->status , $transaction->to_user, '<a href="' . $base_url . '/user/orders/release-escrow/' . $transaction->escrow_id . '">Release Payment</a>', '<a href="' . $base_url . '/user/funds/dispute-escrow/' . $transaction->escrow_id . '">Dispute Payment</a>', date('d/m/Y   g:i:s A', $transaction->created)
  );
}

print t('<h2>Outgoing Escrows</h2>');

print theme('table', array('header' => $header, 'rows' => $rows));
?>

When the user clicks on the Release Payment link it will take them to the form I created but it has a few errors. And Doesn't work. Here is the code for that function.

function uc_escrow_release_payment($form, &$form_state, $escrow_id) {

  $form['#$escrow_id'] = $escrow_id;

  $transaction = db_query("SELECT * FROM {uc_btc_escrow_payment} WHERE escrow_id= :escrow_id", array(':escrow_id' => $escrow_id))->fetchAssoc();

  $user = user_load($transaction['reference']);
  $to_user = $transaction->to_user;
  return confirm_form($form, check_plain('Release Escrow Payment?'), 'user/orders', check_plain('Are you sure you want to release this payment and send the funds to ' . $to_user . '?'), check_plain('Yes'));
}

The errors I am getting are because the information isn't getting passed from one page to the other.

Notice: Undefined index: reference in uc_escrow_release_payment()
Warning: array_flip(): Can only flip STRING and INTEGER values! in DrupalDefaultEntityController->load()
Notice: Trying to get property of non-object in uc_escrow_release_payment() 

I think I am missing something small. Thank you for the help.

  • In uc_escrow_release_payment, have you confirmed $escrow_idis what you expect? Undefined index just means your array doesn't have a key 'reference', so either your result is empty, or you're expecting something different. – Chris Rockwell Jun 22 '13 at 3:49
  • Sorry, on my phone so its hard to see whole picture - you're trying to access $transaction as both am array, and an object - it's one or the other. – Chris Rockwell Jun 22 '13 at 3:57
  • 1
    Just a note, but be careful using $user = user_load(...) if global $user is in scope, you will clobber the logged in user with the new account. Better to always use $account = user_load(...) when loading user accounts. – David Thomas Jun 22 '13 at 5:22
  • Thank you for the help but please excuse my ignorance. I know in the first section of coding I am doing a database query using the users ID to filter out all other transactions not belonging to them. They I am using an array to display a table with the information about their transactions. – Jacob Jun 22 '13 at 5:41
  • Then on the next section of code,I am trying to use the escrow_id from the first section of code to filter my database query down to one order. I know I am getting the error "Notice: Trying to get property of non-object in uc_escrow_release_payment()" because the database query isn't happening properly. – Jacob Jun 22 '13 at 5:52
0

I thought it easier if I just mark up the code a bit, I've tried to notate where I made changes. Can you run this and tell us what it does, if it doesn't solve your problem?

<?php

  /**
   * @file
   * definition of uc-bitcoin-account-outgoing-escrows.tpl.php.
   */

  /** clr use $account instead of $user in case global $user is already in scope **/
  $account = user_load($variables['uid']);
  /* also, loading the user seems extraneous if you're not checking
   * anything else (is user valid, is user in a certain role, etc.)
   * if so, you should include an additional check.  If not, just
   *  do your query using $variables['uid']
   */
  global $base_url;

  /** clr drupalized the query **/
  $transactions = db_query("SELECT * FROM {uc_btc_escrow_payment} WHERE from_account=:user_id", array(':user_id' => $account->uid));

  $header = array(t('Escrow ID'), t('Amount'), t('Payment'), t('Allocated to'), t('Release'), t('Dispiute'), t('Date'));

  $rows = array();

  /** clr adding a check here **/
  if (!$transactions) {
    die('No Transactions were found');
  }
  /** clr clean up the anchor tags **/
  foreach ($transactions as $transaction) {
    $release_href = $base_url . '/user/orders/release-escrow/' . $transaction->escrow_id;
    $dispute_href = $base_url . '/user/funds/dispute-escrow/' . $transaction->escrow_id;
  $rows[] = array(
    $transaction->escrow_id, 
    $transaction->amount, $transaction->status , $transaction->to_user, 
    l('Release Payment', $release_href),
    l('Dispute Payment', $dispute_href),
    date('d/m/Y   g:i:s A', $transaction->created)
  );
}

print t('<h2>Outgoing Escrows</h2>');

print theme('table', array('header' => $header, 'rows' => $rows));
?>

And then your function:

function uc_escrow_release_payment($form, &$form_state, $escrow_id) {

/** check $escrow_id **/
if (!$escrow_id || $escrow_id == '') {
  die('Do something here, because you don\'t have everything you need');
}

/** clr not sure why '$' is used in $form key? */
$form['#$escrow_id'] = $escrow_id;

/** clr changed to fetch object **/
$transaction = db_query("SELECT * FROM {uc_btc_escrow_payment} WHERE escrow_id=:escrow_id", array(':escrow_id' => $escrow_id))->fetchObject();
/** check transaction **/
if (!$transaction) {
  die('No transaction was found; In ' . __FUNCTION__);
}
/** clr don't use $user for same reason above **/
$account = user_load($transaction->reference);
$to_user = $transaction->to_user;
/** check_plain() is only necessary when you're dealing with user supplied text. 
 *  that being said, I guess it never hurt to be over cautios */
return confirm_form($form, check_plain('Release Escrow Payment?'), 'user/orders', check_plain('Are you sure you want to release this payment and send the funds to ' . $to_user . '?'), check_plain('Yes'));
}
| improve this answer | |
  • That looks like it's got it. I was getting a message for $account = user_load($transaction->reference); So I removed that line. It now loads the vendors name in the form. So I know it's loading the proper database. For security I also added global $user; and change the db_query to check and make sure it was the buyer logged in to release the escrow payment. $transaction = db_query("SELECT * FROM {uc_btc_escrow_payment} WHERE from_account=$user->uid AND escrow_id=:escrow_id", array(':escrow_id' => $escrow_id))->fetchObject(); – Jacob Jun 23 '13 at 0:28
  • Glad I could help. Please remove those die () conditionals before you move to production. Its better to replace with watchdog () and, if very important, an action that emails someone if it fails. – Chris Rockwell Jun 23 '13 at 3:21
0

Thank you Chris Rockwell for that valuable information. Unfortunately when I tried to process the form the escrow_id didn't carry over. So I decided to keep the code you gave me and use session variables to make it the rest of the way.

function uc_escrow_release_payment($form, &$form_state, $escrow_id) {
global $user;
if (!$escrow_id || $escrow_id == '') {
  die('Do something here, because you don\'t have everything you need');
}
$form['#$escrow_id'] = $escrow_id;
$transaction = db_query("SELECT * FROM {uc_btc_escrow_payment} WHERE from_account=$user->uid AND escrow_id=:escrow_id", array(':escrow_id' => $escrow_id))->fetchObject();
if (!$transaction) {
  die('We did not find that transaction or you are not the buyer from that transaction!');
}
  $_SESSION['escrow']['#$escrow_id'] = $escrow_id;
  $form = array();
  $form['escrow-message'] = array(
  '#markup' => t('<p><strong>Are You Sure?</strong></p><p>This will release ' . $transaction->amount . ' of bitcoins to ' . $transaction->to_user . '?<p></p>'),
  );
    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Yes'),
        '#submit' => array('uc_bitcoin_escrow_release_payment_submit'),
    );

  $form['cancel'] = array(
    '#type' => 'submit',
    '#value' => t('No'),
    '#submit' => array('ubercart_escrow_get_user_escrow_payments'),
  );   
  return $form;

This allowed me to call escrow_id in the form submit section.

function uc_bitcoin_escrow_release_payment_submit($form, &$form_state) {
global $user;
$escrow_id = $_SESSION['escrow']['#$escrow_id'];
if (!$escrow_id || $escrow_id == '') {
  die('Do something here, because you don\'t have everything you need');
}

/** clr not sure why '$' is used in $form key? */
$form['#$escrow_id'] = $escrow_id;

/** clr changed to fetch object **/
$transaction = db_query("SELECT * FROM {uc_btc_escrow_payment} WHERE from_account=$user->uid AND escrow_id=:escrow_id", array(':escrow_id' => $escrow_id))->fetchObject();
/** check transaction **/
if (!$transaction) {
  die('We did not find that transaction or you are not the buyer from that transaction!');
}

Thank You once again for your help

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.