From 9fe2bfe328145ac630d18929c0197c98402381f1 Mon Sep 17 00:00:00 2001 From: Silvio Rhatto Date: Sun, 6 Nov 2011 14:21:34 -0200 Subject: Refactoring muamba_check_availability() and adding new views handlers --- muamba.db.inc | 20 +++++++++++++++---- muamba.handlers.inc | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++ muamba.module | 5 ++++- muamba.views.inc | 20 +++++++++++++++++++ 4 files changed, 96 insertions(+), 5 deletions(-) diff --git a/muamba.db.inc b/muamba.db.inc index 0500dd9..21e2d8c 100644 --- a/muamba.db.inc +++ b/muamba.db.inc @@ -113,9 +113,13 @@ function muamba_get_transaction($mid) { * Item nid or node object. * * @return - * Transaction nid there's an ongoing transaction. + * Data of the ongoing transaction. + * + * @todo + * There might be more than one active transaction for + * a given item if they all are in the "requested" state. */ -function muamba_current_transaction($data) { +function muamba_current_transaction($data, $owner = NULL) { if (is_object($data)) { $nid = (int) $data->nid; } @@ -149,7 +153,7 @@ function muamba_current_transaction($data) { * Item nid or node object. * * @return - * TRUE if user already requested an item, FALSE otherwise. + * TRUE if item is borrowed, FALSE otherwise. */ function muamba_check_availability($data) { if (is_object($data)) { @@ -159,7 +163,15 @@ function muamba_check_availability($data) { $nid = (int) $data; } - $current = muamba_current_transaction($nid); + $query = db_select('muamba', 'm'); + $query->fields('m', array('mid', 'nid', 'uid', 'owner', 'status', 'thread_id')); + + $query + ->condition('m.nid', $nid, '=') + ->condition('m.active', '1', '=') + ->condition('m.status', MUAMBA_REQUESTED, '!='); + + $current = $query->execute()->fetchAll(); if (empty($current)) { return TRUE; diff --git a/muamba.handlers.inc b/muamba.handlers.inc index e67ed80..d634d6c 100644 --- a/muamba.handlers.inc +++ b/muamba.handlers.inc @@ -30,3 +30,59 @@ class views_handler_filter_muamba_status extends views_handler_filter_in_operato } } } + +/** + * Field handler for muamba transaction status. + */ +class views_handler_field_muamba_available extends views_handler_field { + /** + * Implements views_handler_field#query(). + * + * @see views_php_views_pre_execute() + */ + function query() { + // Provide an field alias but don't actually alter the query. + $this->field_alias = 'views_muamba_available_' . $this->position; + } + + /** + * Renders the field. + * + * @todo + * Return also "Yes, you requested this item" + */ + function render($values) { + if (muamba_check_availability($values->nid)) { + return t('Yes'); + } + + return t('No'); + } +} + +/** + * Field handler for muamba transaction status. + * + * @todo + */ +class views_handler_field_muamba_actions extends views_handler_field { + /** + * Implements views_handler_field#query(). + * + * @see views_php_views_pre_execute() + */ + function query() { + // Provide an field alias but don't actually alter the query. + $this->field_alias = 'views_muamba_actions_' . $this->position; + } + + /** + * Renders the field. + * + * @todo + */ + function render($values) { + dpm($values); + return "TODO"; + } +} diff --git a/muamba.module b/muamba.module index 3be6d6b..dfe7b21 100644 --- a/muamba.module +++ b/muamba.module @@ -107,6 +107,8 @@ function muamba_menu() { /** * Implements hook_node_view() + * + * @todo */ function muamba_node_view($node, $view_mode, $langcode) { global $user; @@ -117,7 +119,8 @@ function muamba_node_view($node, $view_mode, $langcode) { } if ($view_mode == 'full') { - $transaction = muamba_current_transaction($node); + // @todo: there might be more than one transaction + $transaction = muamba_current_transactions($node); $node->content['muamba'] = array( '#markup' => theme('muamba_widget', array('node' => $node, 'transaction' => $transaction)), '#weight' => 100, diff --git a/muamba.views.inc b/muamba.views.inc index 6e2ca1a..a7c58c1 100644 --- a/muamba.views.inc +++ b/muamba.views.inc @@ -154,5 +154,25 @@ function muamba_views_data() { ), ); + // Available items. + $data['muamba']['available'] = array( + 'title' => t('Availability'), + 'help' => t('Available muamba items.'), + 'field' => array( + 'help' => t('Display muamba node availability information.'), + 'handler' => 'views_handler_field_muamba_available', + ), + ); + + // Available actions. + $data['muamba']['actions'] = array( + 'title' => t('Actions'), + 'help' => t('Available actions for an item.'), + 'field' => array( + 'help' => t('Display actions over an item.'), + 'handler' => 'views_handler_field_muamba_actions', + ), + ); + return $data; } -- cgit v1.2.3