From 23843e21523a7b2a73c54eff2a71e6a481c56cae Mon Sep 17 00:00:00 2001 From: Silvio Rhatto Date: Mon, 7 Nov 2011 20:57:32 -0200 Subject: Adding muamba_widget() for widget handling --- muamba-widget.tpl.php | 11 ++--------- muamba.business.inc | 33 +++++++++++++++++++++++++++++++++ muamba.handlers.inc | 36 ++++++++++++------------------------ muamba.module | 34 +++++++++++++--------------------- muamba.theme.inc | 30 ++++++++++++++++++++++++++++++ 5 files changed, 90 insertions(+), 54 deletions(-) diff --git a/muamba-widget.tpl.php b/muamba-widget.tpl.php index 09b43bc..bd856b8 100644 --- a/muamba-widget.tpl.php +++ b/muamba-widget.tpl.php @@ -31,9 +31,7 @@ if ($node->uid != $user->uid) { ); } elseif ($transactions['uid'] == $user->uid) { - foreach (muamba_actions_available('sent', $transactions['status']) as $action) { - $content .= theme('muamba_widget_icon', array('status' => $action, 'id' => $transactions['mid'])); - } + $content .= theme('muamba_widget_requester', array('transaction' => $transactions)); } else { $content .= theme('muamba_widget_icon', array('status' => MUAMBA_REQUESTED, 'id' => $node->nid)); @@ -41,12 +39,7 @@ if ($node->uid != $user->uid) { } else { foreach ($transactions as $transaction) { - $requester = user_load($transaction['uid']); - $content .= t('Request from @name.', array('@name' => $requester->name)); - $content .= '
'; - foreach (muamba_actions_available('received', $transaction['status']) as $action) { - $content .= theme('muamba_widget_icon', array('status' => $action, 'id' => $transaction['mid'])); - } + $content .= theme('muamba_widget_owner', array('transaction' => $transaction)); } } diff --git a/muamba.business.inc b/muamba.business.inc index 7156363..4b033e7 100644 --- a/muamba.business.inc +++ b/muamba.business.inc @@ -365,3 +365,36 @@ function muamba_lost($mid) { return t('Item lost.'); } + +/** + * Display a muamba widget. + * + * @param $node + * Muamba node id. + * + * @return + * HTML formatted muamba widget. + */ +function muamba_widget($node) { + global $user; + + // Check if widget can be shown + if ($node->type != MUAMBA_NODE_TYPE || !muamba_has_request_access($node)) { + return; + } + + $available = muamba_check_availability($node->nid); + + if ($node->uid != $user->uid) { + $transactions = muamba_current_transactions($node, $user->uid); + } + else { + $transactions = muamba_current_transactions($node); + } + + return theme('muamba_widget', array( + 'node' => $node, + 'transactions' => $transactions, + 'available' => $available, + )); +} diff --git a/muamba.handlers.inc b/muamba.handlers.inc index f6c0982..4f75d94 100644 --- a/muamba.handlers.inc +++ b/muamba.handlers.inc @@ -49,6 +49,10 @@ class views_handler_field_muamba_available extends views_handler_field { * Renders the field. */ function render($values) { + if (!isset($values->nid)) { + return; + } + global $user; $available = muamba_check_availability($values->nid); @@ -72,9 +76,6 @@ class views_handler_field_muamba_available extends views_handler_field { /** * Field handler for muamba transaction status. - * - * @todo - * Show widget also in a transaction view. */ class views_handler_field_muamba_actions extends views_handler_field { /** @@ -95,30 +96,17 @@ class views_handler_field_muamba_actions extends views_handler_field { if (isset($values->mid)) { $transaction = muamba_get_transaction($values->mid); - // @todo - return 'TODO'; - } - elseif (isset($values->nid)) { - $available = muamba_check_availability($values->nid); - $node = node_load($values->nid); - - // Check if widget can be shown - if ($node->type != MUAMBA_NODE_TYPE || !muamba_has_request_access($node)) { - return; - } - if ($node->uid != $user->uid) { - $transactions = muamba_current_transactions($values->nid, $user->uid); + if ($transaction['owner'] == $user->uid) { + return theme('muamba_widget_owner', array('transaction' => $transaction)); } - else { - $transactions = muamba_current_transactions($values->nid); + elseif ($transaction['uid'] == $user->uid) { + return theme('muamba_widget_requester', array('transaction' => $transactions)); } - - return theme('muamba_widget', array( - 'node' => $node, - 'transactions' => $transactions, - 'available' => $available, - )); + } + elseif (isset($values->nid)) { + $node = node_load($values->nid); + return muamba_widget($node); } } } diff --git a/muamba.module b/muamba.module index 2b76df9..e9d6bf1 100644 --- a/muamba.module +++ b/muamba.module @@ -109,29 +109,9 @@ function muamba_menu() { * Implements hook_node_view() */ function muamba_node_view($node, $view_mode, $langcode) { - global $user; - - // Check if widget can be shown - if ($node->type != MUAMBA_NODE_TYPE || !muamba_has_request_access($node)) { - return; - } - if ($view_mode == 'full') { - $available = muamba_check_availability($node->nid); - - if ($node->uid != $user->uid) { - $transactions = muamba_current_transactions($node, $user->uid); - } - else { - $transactions = muamba_current_transactions($node); - } - $node->content['muamba'] = array( - '#markup' => theme('muamba_widget', array( - 'node' => $node, - 'transactions' => $transactions, - 'available' => $available, - )), + '#markup' => muamba_widget($node), '#weight' => 100, ); @@ -152,6 +132,18 @@ function muamba_theme($existing, $type, $theme, $path) { 'available' => NULL, ), ), + 'muamba_widget_owner' => array( + 'variables' => array( + 'transaction' => NULL, + ), + 'file' => 'muamba.theme.inc', + ), + 'muamba_widget_requester' => array( + 'variables' => array( + 'transaction' => NULL, + ), + 'file' => 'muamba.theme.inc', + ), 'muamba_powered' => array( 'template' => 'muamba-powered', ), diff --git a/muamba.theme.inc b/muamba.theme.inc index a51f6f6..a4ec6a0 100644 --- a/muamba.theme.inc +++ b/muamba.theme.inc @@ -158,3 +158,33 @@ function theme_muamba_widget_icon($variables) { return $output; } + +/** + * Theme callback. + */ +function theme_muamba_widget_owner($variables) { + $content = ''; + $transaction = $variables['transaction']; + $requester = user_load($transaction['uid']); + $content .= t('Request from @name.', array('@name' => $requester->name)); + $content .= '
'; + foreach (muamba_actions_available('received', $transaction['status']) as $action) { + $content .= theme('muamba_widget_icon', array('status' => $action, 'id' => $transaction['mid'])); + } + + return $content; +} + +/** + * Theme callback. + */ +function theme_muamba_widget_requester($variables) { + $content = ''; + $transaction = $variables['transaction']; + + foreach (muamba_actions_available('sent', $transaction['status']) as $action) { + $content .= theme('muamba_widget_icon', array('status' => $action, 'id' => $transaction['mid'])); + } + + return $content; +} -- cgit v1.2.3