aboutsummaryrefslogtreecommitdiff
path: root/muamba.db.inc
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 /muamba.db.inc
parent79c10bc8fd6277ee349872551e314a38605dc66b (diff)
downloadmuamba-5e044ecbd0e1914d93ebde1514b4567e8a17b1de.tar.gz
muamba-5e044ecbd0e1914d93ebde1514b4567e8a17b1de.tar.bz2
Use muamba_current_transaction() at muamba_node_view()
Diffstat (limited to 'muamba.db.inc')
-rw-r--r--muamba.db.inc99
1 files changed, 47 insertions, 52 deletions
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;
}