From c8bcf7fad0b1faca0f22a0d74e2d638a766014b2 Mon Sep 17 00:00:00 2001
From: Andrew Berry <deviantintegral@gmail.com>
Date: Tue, 17 Apr 2012 16:42:33 -0400
Subject: [PATCH] Issue #1493470: Add the ability to filter videos based on
 their imported status.

---
 includes/ooyala.import.inc |   53 +++++++++++++++++++++++++++++++++++++++-----
 ooyala.css                 |    5 +++++
 2 files changed, 52 insertions(+), 6 deletions(-)

diff --git a/includes/ooyala.import.inc b/includes/ooyala.import.inc
index ff4e3f1..f51600f 100644
--- a/includes/ooyala.import.inc
+++ b/includes/ooyala.import.inc
@@ -31,10 +31,36 @@ function ooyala_import_multiple(&$form_state) {
   $videos = ooyala_api_video_query();
   $type = variable_get('ooyala_primary_content_type', FALSE);
   $form = array();
+
+  $form['status_filter'] = array(
+    '#type' => 'select',
+    '#title' => t('Video status'),
+    '#options' => array(
+      'all' => t('All'),
+      'imported' => t('Imported'),
+      'unimported' => t('Unimported'),
+    ),
+    '#default' => 'all',
+  );
+
+  $form['status_filter_button'] = array(
+    '#type' => 'button',
+    '#value' => t('Filter'),
+  );
+
   if (!empty($videos)) {
     // Fetch the statuses for all videos.
     $statuses = ooyala_get_status($type, array_keys($videos));
 
+    if (isset($form_state['post']['status_filter'])) {
+      if ($form_state['post']['status_filter'] == 'imported') {
+        $videos = array_intersect_key($videos, $statuses);
+      }
+      elseif ($form_state['post']['status_filter'] == 'unimported') {
+        $videos = array_diff_key($videos, $statuses);
+      }
+    }
+
     foreach ($videos as $embedCode => $data) {
       // Add the video to the video info table.
       $form['video_info'][$embedCode]['title'] = array('#value' => check_plain($data['title']));
@@ -69,11 +95,22 @@ function ooyala_import_multiple(&$form_state) {
       '#type' => 'value',
       '#value' => $videos,
     );
-    $form['buttons']['submit'] = array(
-      '#type' => 'submit', 
-      '#value' => t('Import selected'),
-      '#access' => $type,
-    );
+
+    if (!empty($videos)) {
+      $form['buttons']['submit'] = array(
+        '#type' => 'submit',
+        '#value' => t('Import selected'),
+        '#access' => $type,
+      );
+    }
+    else {
+      $form['no_videos'] = array(
+        '#type' => 'markup',
+        '#prefix' => '<p>',
+        '#value' => t('There are no @status videos to view.', array('@status' => $form_state['post']['status_filter'])),
+        '#suffix' => '</p>',
+      );
+    }
   }
   return $form;
 }
@@ -113,6 +150,8 @@ function ooyala_import_multiple_submit($form, &$form_state) {
  * Theme the ooyala_import_multiple() form.
  */
 function theme_ooyala_import_multiple($form) {
+  drupal_add_css(drupal_get_path('module', 'ooyala') . '/ooyala.css');
+
   // Build up a list of all the videos in a table.
   $rows = array();
   foreach (element_children($form['video_info']) as $key) {
@@ -161,12 +200,14 @@ function theme_ooyala_import_multiple($form) {
   }
 
   $output = '';
+  $output .= drupal_render($form['status_filter']);
+  $output .= drupal_render($form['status_filter_button']);
+
   if ($rows) {
     $select_header = theme('table_select_header_cell');
     $select_header['class'] .= ' checkbox';
     $header = array($select_header, t('Video information'), t('Status'), t('Operations'));
   
-    drupal_add_css(drupal_get_path('module', 'ooyala') . '/ooyala.css');
     $output .= theme('table', $header, $rows, array('id' => 'ooyala-import'));
   }
   elseif (isset($form['video_info'])) {
diff --git a/ooyala.css b/ooyala.css
index 035023a..2dad79b 100644
--- a/ooyala.css
+++ b/ooyala.css
@@ -85,3 +85,8 @@ input.ooyala-upload-thumbnails {
   width: 2em;
   padding: inherit;
 }
+
+#edit-status-filter-wrapper, #edit-status-filter-wrapper label {
+  display: inline;
+}
+
-- 
1.7.10

