changeset 7:f3040f91b65d

Add a manual search / import form
author Franck Deroche <franck@defr.org>
date Tue, 20 Jul 2010 11:57:23 +0200
parents f0e59759c605
children da69ba21d0c3
files scald_dailymotion.module scald_dailymotion.pages.inc
diffstat 2 files changed, 130 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/scald_dailymotion.module	Tue Jul 20 11:56:42 2010 +0200
+++ b/scald_dailymotion.module	Tue Jul 20 11:57:23 2010 +0200
@@ -51,6 +51,10 @@
     'scald_dailymotion_imports_table' => array(
       'arguments' => array('form' => NULL),
       'file' => 'scald_dailymotion.admin.inc'
+    ),
+    'scald_dailymotion_search_results_table' => array(
+      'arguments' => array('form' => NULL),
+      'file' => 'scald_dailymotion.pages.inc'
     )
   );
 }
@@ -59,7 +63,7 @@
  * Implements hook_perm.
  */
 function scald_dailymotion_perm() {
-  return array('administer dailymotion imports');
+  return array('administer dailymotion imports', 'import dailymotion videos');
 }
 
 /**
@@ -88,6 +92,14 @@
     'description' => 'Configure the videos that should be automatically imported from DailyMotion',
     'file' => 'scald_dailymotion.admin.inc'
   );
+  $items['dailymotion/search'] = array(
+    'title' => 'DailyMotion Video search',
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('scald_dailymotion_search_form', 2),
+    'access arguments' => array('import dailymotion videos'),
+    'description' => 'Search for new videos to import into this site',
+    'file' => 'scald_dailymotion.pages.inc'
+  );
   return $items;
 }
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/scald_dailymotion.pages.inc	Tue Jul 20 11:57:23 2010 +0200
@@ -0,0 +1,117 @@
+<?php
+// $Id$
+/**
+ * @file
+ *   Contains form handlers for the DailyMotion search form.
+ */
+
+/**
+ * Generates the search and search results form.
+ */
+function scald_dailymotion_search_form($form_state) {
+  $terms = $form_state['storage']['terms'];
+  $form = array();
+  $form['search'] = array(
+    '#type' => 'fieldset',
+    '#tree' => TRUE,
+    '#attributes' => array('class' => 'container-inline')
+  );
+  $form['search']['search_term'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Terms'),
+    '#default_value' => $terms
+  );
+  $form['search']['submit'] = array(
+    '#type' => 'submit',
+    '#value' => t('Search'),
+    '#submit' => array('scald_dailymotion_search_form_search_submit')
+  );
+  if (!empty($terms)) {
+    $form['results'] = array(
+      '#type' => 'fieldset',
+      '#title' => t('Search results'),
+      '#tree' => TRUE,
+      '#theme' => 'scald_dailymotion_search_results_table'
+    );
+    $items = scald_dailymotion_feed('search', $terms);
+    if (count($items)) {
+      foreach ($items as $video) {
+        $thumb = str_replace('large', 'small', $video->thumbnail['src']);
+        $form['results'][$video->id] = array(
+          'checkbox' => array(
+            '#type' => 'checkbox',
+          ),
+          'title' => array(
+            '#type' => 'item',
+            '#value' => $video->title
+          ),
+          'thumbnail' => array(
+            '#type' => 'markup',
+            '#value' => theme('image', $thumb, '', '', NULL, FALSE)
+          )
+        );
+      }
+      $form['import'] = array(
+        '#type' => 'submit',
+        '#value' => t('Import'),
+        '#submit' => array('scald_dailymotion_search_form_submit')
+      );
+    }
+    else {
+      $form['results']['empty'] = array(
+        '#type' => 'markup',
+        '#value' => t('No results')
+      );
+    }
+  }
+  return $form;
+}
+
+/**
+ * Handles search terms form submission.
+ */
+function scald_dailymotion_search_form_search_submit($form, &$form_state) {
+  if ($form_state['clicked_button']['#value'] == t('Search')) {
+    $form_state['storage']['terms'] = $form_state['values']['search']['search_term'];
+  }
+}
+
+/**
+ * Handlers import form submission.
+ */
+function scald_dailymotion_search_form_submit($form, &$form_state) {
+  $ids = array();
+  // Find all the elements that have been checked in the results table
+  foreach ($form_state['values']['results'] as $id => $element) {
+    if ($element['checkbox']) {
+      $ids[] = $id;
+    }
+  }
+  // And now create an atom for each of them
+  foreach ($ids as $id) {
+    $sid = scald_dailymotion_register($id);
+    $video = scald_fetch($sid);
+    drupal_set_message(t('Created video %title', array('%title' => $video->title)));
+  }
+  // End the multistep workflow
+  unset($form_state['storage']);
+  $form_state['rebuild'] = FALSE;
+}
+
+/**
+ * Themes the results table.
+ */
+function theme_scald_dailymotion_search_results_table($form) {
+  $headers = array(t('Import'), t('Title'), t('Thumbnail'));
+  $rows = array();
+  foreach (element_children($form) as $key) {
+    // Render our row
+    $row = array();
+    $row[] = drupal_render($form[$key]['checkbox']);
+    $row[] = drupal_render($form[$key]['title']);
+    $row[] = drupal_render($form[$key]['thumbnail']);
+    // And add it to the list of table rows.
+    $rows[] = $row;
+  }
+  return theme('table', $headers, $rows);
+}