* 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;
}
* 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)) {
$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;
}
}
}
+
+/**
+ * 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";
+ }
+}
/**
* Implements hook_node_view()
+ *
+ * @todo
*/
function muamba_node_view($node, $view_mode, $langcode) {
global $user;
}
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,
),
);
+ // 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;
}