From 89e6dd94b545084beb19d8358a3baeedd8d09407 Mon Sep 17 00:00:00 2001
From: retrodans <dan@retrobadger.net>
Date: Fri, 19 Jul 2013 12:05:20 +0100
Subject: [PATCH] Issue 1991806 to add capability for availability in views

---
 ...ion_handler_field_entity_capacity_available.inc | 35 ++++++++++++++++++++++
 modules/registration_views/registration_views.info |  1 +
 .../registration_views/registration_views.module   | 11 +++++++
 3 files changed, 47 insertions(+)
 create mode 100755 modules/registration_views/includes/registration_handler_field_entity_capacity_available.inc

diff --git a/modules/registration_views/includes/registration_handler_field_entity_capacity_available.inc b/modules/registration_views/includes/registration_handler_field_entity_capacity_available.inc
new file mode 100755
index 0000000..efdad9d
--- /dev/null
+++ b/modules/registration_views/includes/registration_handler_field_entity_capacity_available.inc
@@ -0,0 +1,35 @@
+<?php
+/**
+ * @file
+ * Display maximum slots for an entity.
+ */
+class registration_handler_field_entity_capacity_available extends views_handler_field_numeric {
+  function render($values) {
+  	$entity_id = $this->get_value($values); 	
+  	$table = $this->table;
+  	$registration_entity_id = $this->view->core;
+
+  	$settings = registration_entity_settings($table, $registration_entity_id);
+
+
+  	// If closed, then just say closed
+  	var_dump(registration_status($table, $registration_entity_id));
+  	if("registration_status($table, $registration_entity_id)"){
+  	  return 'closed';
+  	}
+
+  	$slots = $settings['capacity'];
+    
+    // Check if there are unlimited spaces before running logic
+    if (isset($slots)) {
+      if (!empty($slots)) {
+        $slots_used = (int) registration_event_count($table, $registration_entity_id);
+        return $slots - $slots_used;
+      }
+      else {
+        return t('Unlimited');
+      }
+    }
+
+  }
+}
diff --git a/modules/registration_views/registration_views.info b/modules/registration_views/registration_views.info
index 13f8076..be0efd3 100644
--- a/modules/registration_views/registration_views.info
+++ b/modules/registration_views/registration_views.info
@@ -13,6 +13,7 @@ files[] = includes/registration_handler_field_registration_link_delete.inc
 
 ; Views: Registration Host Entity
 files[] = includes/registration_handler_relationship.inc
+files[] = includes/registration_handler_field_entity_capacity_available.inc
 files[] = includes/registration_handler_field_entity_capacity_used.inc
 files[] = includes/registration_handler_field_entity_capacity_total.inc
 files[] = includes/registration_handler_field_entity_datetime.inc
diff --git a/modules/registration_views/registration_views.module b/modules/registration_views/registration_views.module
index e473382..a75c83e 100644
--- a/modules/registration_views/registration_views.module
+++ b/modules/registration_views/registration_views.module
@@ -198,6 +198,17 @@ function registration_views_views_data_alter(&$data) {
       ),
     );
 
+    // Host entity: used slots.
+    $data[$info['base table']]['capacity_available'] = array(
+      'real field' => $info['entity keys']['id'],
+      'group' => t('Registration Settings'),
+      'title' => t('Slots Available'),
+      'help' => t('Current number of slots available for this entity.'),
+      'field' => array(
+        'handler' => 'registration_handler_field_entity_capacity_available',
+      ),
+    );
+
     // Registration to entity.
     $data['registration']['registration_related_' . $entity_type] = array(
       'group' => t('Registration'),
-- 
1.8.1.2

