aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSilvio Rhatto <rhatto@riseup.net>2011-10-06 16:50:35 -0300
committerSilvio Rhatto <rhatto@riseup.net>2011-10-06 16:50:35 -0300
commit5e044ecbd0e1914d93ebde1514b4567e8a17b1de (patch)
tree7f04c5f8c86302a744963c402530bdb6f7e79958
parent79c10bc8fd6277ee349872551e314a38605dc66b (diff)
downloadmuamba-5e044ecbd0e1914d93ebde1514b4567e8a17b1de.tar.gz
muamba-5e044ecbd0e1914d93ebde1514b4567e8a17b1de.tar.bz2
Use muamba_current_transaction() at muamba_node_view()
-rw-r--r--muamba-widget.tpl.php2
-rw-r--r--muamba.business.inc12
-rw-r--r--muamba.db.inc99
-rw-r--r--muamba.module6
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
@@ -6,6 +6,18 @@
*/
/**
+ * 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.
*
* @param $type
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();
@@ -116,59 +107,63 @@ 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,