From 5e044ecbd0e1914d93ebde1514b4567e8a17b1de Mon Sep 17 00:00:00 2001 From: Silvio Rhatto Date: Thu, 6 Oct 2011 16:50:35 -0300 Subject: Use muamba_current_transaction() at muamba_node_view() --- muamba-widget.tpl.php | 2 +- muamba.business.inc | 12 +++++++ muamba.db.inc | 99 ++++++++++++++++++++++++--------------------------- muamba.module | 6 ++-- 4 files changed, 62 insertions(+), 57 deletions(-) diff --git a/muamba-widget.tpl.php b/muamba-widget.tpl.php index 1b2a598..3caf614 100644 --- a/muamba-widget.tpl.php +++ b/muamba-widget.tpl.php @@ -4,7 +4,7 @@ * @file * Template for displaying the muamba widget. * - * @todo + * @todo: use $transaction * Show item status: can it be requested? * Is this item already requested? */ diff --git a/muamba.business.inc b/muamba.business.inc index 195409f..cd9dc5d 100644 --- a/muamba.business.inc +++ b/muamba.business.inc @@ -5,6 +5,18 @@ * Business logic for Muamba. */ +/** + * Define which status belongs to an ongoing transaction. + */ +function muamba_ongoing() { + return array( + MUAMBA_REQUESTED, + MUAMBA_ACCEPTED, + MUAMBA_RETURNED, + MUAMBA_LOST, + ); +} + /** * Determine possible actions for a transaction. * diff --git a/muamba.db.inc b/muamba.db.inc index 4414a1a..6c2f74d 100644 --- a/muamba.db.inc +++ b/muamba.db.inc @@ -82,25 +82,16 @@ function muamba_get_transactions($uid, $type = 'sent', $status = NULL) { /** * Get a single transaction. * - * @param $data - * Transaction id or node object. + * @param $mid + * Transaction id. * * @return * Transaction data. - * - * @todo - * When a node is provided, duplicate rows might occur. */ -function muamba_get_transaction($data) { +function muamba_get_transaction($mid) { $query = db_select('muamba', 'm'); $query->fields('m', array('mid', 'nid', 'uid', 'owner', 'status', 'thread_id')); - - if (is_object($data)) { - $query->condition('m.nid', $data->nid, '='); - } - else { - $query->condition('m.mid', (int) $data, '='); - } + $query->condition('m.mid', (int) $mid, '='); $rows = array(); $results = $query->execute()->fetchAll(); @@ -115,60 +106,64 @@ function muamba_get_transaction($data) { } } -/** - * Check item availability. - * - * @param $nid - * Item nid. - * - * @param $uid - * Requester user nid. - * - * @return - * TRUE if user already requested an item, FALSE otherwise. - * - * @todo - */ -function muamba_check_availability($nid, $uid = NULL) { -} - /** * Get current transaction for an item. * - * @param $nid - * Item nid. + * @param $data + * Item nid or node object. * * @return * Transaction nid there's an ongoing transaction. - * - * @todo */ -function muamba_current_transaction($nid) { +function muamba_current_transaction($data) { + if (is_object($data)) { + $nid = (int) $data->nid; + } + else { + $nid = (int) $data; + } + $query = db_select('muamba', 'm'); $query->fields('m', array('mid', 'nid', 'uid', 'owner', 'status', 'thread_id')); - $query->condition('m.nid', $nid, '='); - $query->condition( - db_or() - ->condition('m.status', MUAMBA_REQUESTED, '=') - ->condition('m.status', MUAMBA_ACCEPTED, '=') - ->condition('m.status', MUAMBA_RETURNED, '=') - ->condition('m.status', MUAMBA_LOST, '=') - ); + $query + ->condition('m.nid', $nid, '=') + ->condition('m.status', array(muamba_ongoing()), 'IN'); $results = $query->execute()->fetchAll(); - return $results; + // Sanitize the data before handing it off to the theme layer. + foreach ($results as $entry) { + $rows[] = array_map('check_plain', (array) $entry); + } + + if (isset($rows[0])) { + return $rows[0]; + } } /** - * Define which status belongs to an ongoing transaction. + * Check item availability. + * + * @param $data + * Item nid or node object. + * + * @return + * TRUE if user already requested an item, FALSE otherwise. */ -function muamba_ongoing() { - return array( - MUAMBA_REQUESTED, - MUAMBA_ACCEPTED, - MUAMBA_RETURNED, - MUAMBA_LOST, - ); +function muamba_check_availability($data) { + if (is_object($data)) { + $nid = (int) $data->nid; + } + else { + $nid = (int) $data; + } + + $current = muamba_current_transaction($nid); + + if (empty($current)) { + return TRUE; + } + + return FALSE; } diff --git a/muamba.module b/muamba.module index 2835dd9..5a92f35 100644 --- a/muamba.module +++ b/muamba.module @@ -18,6 +18,7 @@ define('MUAMBA_RECOVERED', 5); define('MUAMBA_LOST', 6); // Load requirements. +include_once('muamba.business.inc'); include_once('muamba.misc.inc'); include_once('muamba.db.inc'); @@ -106,9 +107,6 @@ function muamba_menu() { /** * Implements hook_node_view() - * - * @todo - * Use muamba_check_availability() before muamba_get_transaction() */ function muamba_node_view($node, $view_mode, $langcode) { global $user; @@ -119,7 +117,7 @@ function muamba_node_view($node, $view_mode, $langcode) { } if ($view_mode == 'full') { - $transaction = muamba_get_transaction($node); + $transaction = muamba_current_transaction($node); $node->content['muamba'] = array( '#markup' => theme('muamba_widget', array('nid' => $node->nid, 'transaction' => $transaction)), '#weight' => 100, -- cgit v1.2.3