aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSilvio Rhatto <rhatto@riseup.net>2011-11-09 13:50:38 -0200
committerSilvio Rhatto <rhatto@riseup.net>2011-11-09 13:50:38 -0200
commit024bd20549c58317e8565e7772eec5630295d33c (patch)
tree710a454814efe6e6d64f23c173125aa0bd82a487
parent3bae0572ed01c0dfc80a4d52e3eb05b766a73649 (diff)
downloadmuamba-024bd20549c58317e8565e7772eec5630295d33c.tar.gz
muamba-024bd20549c58317e8565e7772eec5630295d33c.tar.bz2
Adding views_handler_field_muamba_total()
-rw-r--r--muamba.db.inc38
-rw-r--r--muamba.handlers.inc50
-rw-r--r--muamba.views.inc10
3 files changed, 98 insertions, 0 deletions
diff --git a/muamba.db.inc b/muamba.db.inc
index 4275dde..ff682f5 100644
--- a/muamba.db.inc
+++ b/muamba.db.inc
@@ -228,3 +228,41 @@ function muamba_finish($mid) {
->condition('mid', $mid, '=')
->execute();
}
+
+/**
+ * Return total transactions.
+ *
+ * @param $nid
+ * Muamba node id.
+ *
+ * @param $type
+ * Type of transactions.
+ *
+ * @return
+ * Number of transactions.
+ */
+function muamba_total($nid, $type = 'transactions') {
+ $nid = (int) $nid;
+ $query = db_select('muamba', 'm');
+ $query->condition('m.nid', $nid, '=');
+
+ if ($type != 'transactions') {
+ if ($type == 'requested') {
+ $query->condition('m.status', MUAMBA_REQUESTED, '=');
+ }
+ elseif ($type == 'rejected') {
+ $query->condition('m.status', MUAMBA_REJECTED, '=');
+ }
+ elseif ($type == 'borrowed') {
+ $query->condition(
+ db_or()
+ ->condition('m.status', MUAMBA_ACCEPTED, '=')
+ ->condition('m.status', MUAMBA_RETURNED, '=')
+ ->condition('m.status', MUAMBA_RECOVERED, '=')
+ ->condition('m.status', MUAMBA_LOST, '=')
+ );
+ }
+ }
+
+ return $query->countQuery()->execute()->fetchField();
+}
diff --git a/muamba.handlers.inc b/muamba.handlers.inc
index 87286f3..33c312a 100644
--- a/muamba.handlers.inc
+++ b/muamba.handlers.inc
@@ -98,3 +98,53 @@ class views_handler_field_muamba_actions extends views_handler_field {
}
}
}
+
+/**
+ * Field handler for muamba total transactions.
+ */
+class views_handler_field_muamba_total extends views_handler_field {
+ /**
+ * Implements views_handler_field#query().
+ */
+ function query() {
+ // Provide an field alias but don't actually alter the query.
+ $this->field_alias = 'views_muamba_total_' . $this->position;
+ }
+
+ /**
+ * Option definition.
+ */
+ function option_definition() {
+ $options = parent::option_definition();
+ $options['value'] = array('default' => 'transactions');
+ return $options;
+ }
+
+ /**
+ * Provide link to node option.
+ */
+ function options_form(&$form, &$form_state) {
+ parent::options_form($form, $form_state);
+
+ $form['value'] = array(
+ '#type' => 'select',
+ '#title' => t('Which value'),
+ '#options' => array(
+ 'transactions' => t('Number of transactions'),
+ 'requested' => t('Number of current requests'),
+ 'rejected' => t('Number of rejections'),
+ 'borrowed' => t('Number of times it was borrowed'),
+ ),
+ '#default_value' => isset($this->options['value']) ? $this->options['value'] : 'transactions',
+ );
+ }
+
+ /**
+ * Renders the field.
+ */
+ function render($values) {
+ if (isset($values->nid)) {
+ return muamba_total($values->nid, $this->options['value']);
+ }
+ }
+}
diff --git a/muamba.views.inc b/muamba.views.inc
index a7c58c1..e8e433d 100644
--- a/muamba.views.inc
+++ b/muamba.views.inc
@@ -174,5 +174,15 @@ function muamba_views_data() {
),
);
+ // Total transactions.
+ $data['muamba']['total'] = array(
+ 'title' => t('Total'),
+ 'help' => t('Total item transactions.'),
+ 'field' => array(
+ 'help' => t('Display total transactions involving an item.'),
+ 'handler' => 'views_handler_field_muamba_total',
+ ),
+ );
+
return $data;
}