]> gitweb.fluxo.info Git - drupal/muamba.git/commitdiff
Adding views_handler_field_muamba_total()
authorSilvio Rhatto <rhatto@riseup.net>
Wed, 9 Nov 2011 15:50:38 +0000 (13:50 -0200)
committerSilvio Rhatto <rhatto@riseup.net>
Wed, 9 Nov 2011 15:50:38 +0000 (13:50 -0200)
muamba.db.inc
muamba.handlers.inc
muamba.views.inc

index 4275ddea57be5619dfa88487a82b38a0a2cb44a6..ff682f5024ee6da918ed95aea1393bc2f35ea35a 100644 (file)
@@ -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();
+}
index 87286f3ba564bc0f48f015bf9f45ade316be5e4a..33c312aef1554d0dda784aa59605e03e7c8d090a 100644 (file)
@@ -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']);
+    }
+  }
+}
index a7c58c18d1df03461b546260c5ded81f97b92f52..e8e433d0d2ec4a3006877568408effd5aa8f468a 100644 (file)
@@ -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;
 }