condition('m.nid', $nid, '=') ->condition('m.uid', $uid, '=') ->condition('m.status', MUAMBA_REQUESTED, '='); $result = $query->countQuery()->execute()->fetchField(); if ($result > 0) { return TRUE; } return FALSE; } /** * Get the requests sent or received. * * @param $uid * Requester user uid. * * @param $type * Transaction type (sent or received). * * @param $status * Current transaction status. * * @return * Array of existing transactions. */ function muamba_get_transactions($uid, $type = 'sent', $status = NULL) { $uid = (int) $uid; $query = db_select('muamba', 'm'); $query->fields('m', array('mid', 'nid', 'uid', 'owner', 'status', 'thread_id')); if ($type == 'sent') { $query->condition('m.uid', $uid, '='); } else { $query->condition('m.owner', $uid, '='); } if ($status != NULL) { $query->condition('m.status', $status, '='); } $rows = array(); $results = $query->execute()->fetchAll(); // Sanitize the data before handing it off to the theme layer. foreach ($results as $entry) { $rows[] = array_map('check_plain', (array) $entry); } return $rows; } /** * Get a single transaction. * * @param $data * Transaction id or node object. * * @return * Transaction data. * * @todo * When a node is provided, duplicate rows might occur. */ function muamba_get_transaction($data) { $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, '='); } $rows = array(); $results = $query->execute()->fetchAll(); // 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]; } } /** * 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. * * @return * Transaction nid there's an ongoing transaction. * * @todo */ function 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, '='); $query->condition( db_or() ->condition('m.status', MUAMBA_REQUESTED, '=') ->condition('m.status', MUAMBA_ACCEPTED, '=') ->condition('m.status', MUAMBA_RETURNED, '=') ->condition('m.status', MUAMBA_LOST, '=') ); $results = $query->execute()->fetchAll(); return $results; } /** * Define which status belongs to an ongoing transaction. */ function muamba_ongoing() { return array( MUAMBA_REQUESTED, MUAMBA_ACCEPTED, MUAMBA_RETURNED, MUAMBA_LOST, ); }