From 461a0d5737e8aec766758c9887904d53eb177e8f Mon Sep 17 00:00:00 2001 From: Silvio Rhatto Date: Mon, 26 Sep 2011 21:11:41 -0300 Subject: Adding more transaction logic --- muamba.business.inc | 60 ++++++++++++++++++++++++++++++++++++++++ muamba.db.inc | 9 ++++++ muamba.misc.inc | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++++ muamba.module | 1 + muamba.theme.inc | 16 ++++++++--- 5 files changed, 162 insertions(+), 4 deletions(-) create mode 100644 muamba.misc.inc diff --git a/muamba.business.inc b/muamba.business.inc index 5efb469..1b40d1b 100644 --- a/muamba.business.inc +++ b/muamba.business.inc @@ -9,6 +9,66 @@ include_once('muamba.db.inc'); include_once('muamba.misc.inc'); +/** + * Determine possible actions for a transaction. + * + * @param $type + * Transaction type (sent or received). + * + * @param $status + * Current transaction status. + * + * @return + * Array of available action codes. + */ +function muamba_actions_available($type = 'sent', $status) { + if ($type == 'sent') { + switch ($status) { + case MUAMBA_REQUESTED: + $actions = array( + MUAMBA_ACCEPTED, + MUAMBA_REJECTED, + ); + break; + + case MUAMBA_ACCEPTED: + $actions = array( + MUAMBA_RELEASED, + ); + break; + + case MUAMBA_RETURNED: + $actions = array( + MUAMBA_RELEASED, + ); + break; + + default: + $actions = array(); + } + } + else { + switch ($status) { + case MUAMBA_REQUESTED: + $actions = array( + MUAMBA_CANCELLED, + ); + break; + + case MUAMBA_ACCEPTED: + $actions = array( + MUAMBA_RETURNED, + ); + break; + + default: + $actions = array(); + } + } + + return $actions; +} + /** * Transaction management page. */ diff --git a/muamba.db.inc b/muamba.db.inc index 93eb5bc..bd2322f 100644 --- a/muamba.db.inc +++ b/muamba.db.inc @@ -42,6 +42,15 @@ function muamba_check_user_request($nid, $uid) { * * @param $uid * Requester user uid. + * + * @param $type + * Transaction type (sent or received). + * + * @param $status + * Current transaction status. + * + * @return + * Array of existing transactions. */ function muamba_get_transactions($uid, $type = 'sent', $status = MUAMBA_REQUESTED) { $uid = (int) $uid; diff --git a/muamba.misc.inc b/muamba.misc.inc new file mode 100644 index 0000000..4b2d2e6 --- /dev/null +++ b/muamba.misc.inc @@ -0,0 +1,80 @@ + 'requested', + MUAMBA_ACCEPTED => 'accepted', + MUAMBA_REJECTED => 'rejected', + MUAMBA_RELEASED => 'released', + MUAMBA_RETURNED => 'returned', + ); + + if ($code == NULL) { + return $status; + } + + return $status[$code]; +} + +/** + * Get status callback name. + * + * @param $code + * Optional status code. + * + * @return + * Array of callback names or callback + * name if $code is supplied. + */ +function muamba_actions($code = NULL) { + $status = array( + MUAMBA_REQUESTED => 'request', + MUAMBA_ACCEPTED => 'accept', + MUAMBA_REJECTED => 'reject', + MUAMBA_RELEASED => 'releas', + MUAMBA_RETURNED => 'return', + ); + + if ($code == NULL) { + return $status; + } + + return $status[$code]; +} diff --git a/muamba.module b/muamba.module index 9588dac..6476093 100644 --- a/muamba.module +++ b/muamba.module @@ -14,6 +14,7 @@ define('MUAMBA_ACCEPTED', 1); define('MUAMBA_REJECTED', 2); define('MUAMBA_RELEASED', 3); define('MUAMBA_RETURNED', 4); +define('MUAMBA_CANCELLED', 4); /** * Implements hook_permission() diff --git a/muamba.theme.inc b/muamba.theme.inc index 771c80b..1bec871 100644 --- a/muamba.theme.inc +++ b/muamba.theme.inc @@ -5,12 +5,15 @@ * Misc theme functions. */ +/** + * Theme callback. + */ function theme_muamba_transactions($variables) { $rows = array(); $type = $variables['type']; $transactions = $variables['transactions']; - $output = '
'; + $output = '
'; if ($type == 'sent') { $title = t('Items I requested from people'); @@ -19,7 +22,7 @@ function theme_muamba_transactions($variables) { $title = t('My items that people requested'); } - $output .= '

'. $title .'

'; + $output .= '

'. $title .'

'; if (empty($transactions)) { $output .= t('There are no transactions.'); @@ -28,11 +31,16 @@ function theme_muamba_transactions($variables) { } foreach($transactions as $transaction) { + foreach (muamba_actions_available($type, $transaction['status']) as $action) { + $callback = muamba_actions($action); + $available_actions[] = l(t($callback), $callback); + } + $rows[] = array( 'asset' => $transaction['node']->title, 'user' => $transaction['user']->name, - 'status' => muamba_statuses($transaction['status']), - 'actions' => 'accept', // TODO + 'status' => t(muamba_statuses($transaction['status'])), + 'available_actions' => implode($available_actions, '|'), ); } -- cgit v1.2.3