changeset 4:fd5fb845d0bc

Add an auto-import admin screen
author Franck Deroche <franck@defr.org>
date Mon, 19 Jul 2010 14:46:20 +0200
parents 2a63a6e15166
children bb63856c865a
files scald_dailymotion.admin.inc scald_dailymotion.module
diffstat 2 files changed, 158 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/scald_dailymotion.admin.inc	Mon Jul 19 14:46:20 2010 +0200
@@ -0,0 +1,113 @@
+<?php
+// $Id$
+/**
+ * @file
+ *   Provides admin form for DailyMotion's Scald Provider.
+ */
+
+/**
+ * Defines the import settings form.
+ */
+function scald_dailymotion_imports_form() {
+  $form = array();
+  $imports = variable_get('scald_dailymotion_imports', array());
+  $types = array('user' => t('User'), 'tag' => t('Tag'));
+  if (count($imports)) {
+    $form['imports'] = array(
+       '#type' => 'fieldset',
+       '#title' => t('Current imports'),
+       '#tree' => TRUE,
+       '#theme' => 'scald_dailymotion_imports_table'
+     );
+    foreach ($imports as $key => $import) {
+      $form['imports'][$key] = array(
+        'type' => array(
+          '#type' => 'select',
+          '#title' => t('Type'),
+          '#options' => array('delete' => t('<Delete>')) + $types,
+          '#default_value' => $import['type']
+        ),
+        'value' => array(
+          '#type' => 'textfield',
+          '#title' => t('Identifier'),
+          '#default_value' => $import['value']
+        ),
+      );
+    }
+  }
+  $form['add'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Add import'),
+    '#collapsible' => TRUE,
+    '#collapsed' => count($imports)
+  );
+  $form['add']['type'] = array(
+    '#type' => 'select',
+    '#title' => t('Type'),
+    '#options' => $types,
+  );
+  $form['add']['value'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Identifier'),
+    '#description' => t('This field value meaning depends on the Type
+    field defined above. For a <em>User</em> import, put the username
+    whose videos you\'d loke to import here, for a tag import, use the
+    tag name.')
+  );
+  $form['add']['submit'] = array(
+    '#type' => 'submit',
+    '#value' => t('Add this import'),
+    '#submit' => array('scald_dailymotion_imports_form_add')
+  );
+
+  $form['submit'] = array(
+    '#type' => 'submit',
+    '#value' => t('Save configuration')
+  );
+  return $form;
+}
+
+/**
+ * Handles the submission of the form that adds a new import.
+ */
+function scald_dailymotion_imports_form_add($form, &$form_state) {
+  $imports = variable_get('scald_dailymotion_imports', array());
+  $values = $form_state['values'];
+  $key = $values['type'] . '-' . $values['value'];
+  $imports[$key] = array('type' => $values['type'], 'value' => $values['value']);
+  variable_set('scald_dailymotion_imports', $imports);
+  drupal_set_message(t('Import added'));
+}
+
+/**
+ * Handles the submission of the whole form.
+ */
+function scald_dailymotion_imports_form_submit($form, &$form_state) {
+  drupal_set_message(t('The configuration options have been saved.'));
+  $imports = array();
+  foreach ($form_state['values']['imports'] as $key => $import) {
+    if ($import['type'] != 'delete') {
+      $imports[$key] = $import;
+    }
+  }
+  variable_set('scald_dailymotion_imports', $imports);
+}
+
+/**
+ * Themes the current imports form.
+ */
+function theme_scald_dailymotion_imports_table($form) {
+  $headers = array(t('Type'), t('Identifier'));
+  $rows = array();
+  foreach (element_children($form) as $key) {
+    // Unset per widget titles, they're already in the column title
+    $form[$key]['type']['#title'] = $form[$key]['value']['#title'] = '';
+    // Render our row
+    $row = array();
+    $row[] = drupal_render($form[$key]['type']);
+    $row[] = drupal_render($form[$key]['value']);
+    // And add it to the list of table rows.
+    $rows[] = $row;
+  }
+  return theme('table', $headers, $rows);
+}
--- a/scald_dailymotion.module	Mon Jul 19 14:45:25 2010 +0200
+++ b/scald_dailymotion.module	Mon Jul 19 14:46:20 2010 +0200
@@ -1,4 +1,9 @@
 <?php
+// $Id$
+/**
+ * @file
+ *   Defines a DailyMotion provider for Scald.
+ */
 define('DAILYMOTION_RSS', 'http://www.dailymotion.com/rss/');
 define('NS_MEDIA',              'http://search.yahoo.com/mrss');
 define('NS_DM',                 'http://www.dailymotion.com/dmrss');
@@ -42,11 +47,51 @@
     'scald_dailymotion_player' => array(
       'arguments' => array('video' => NULL, 'thumbnail' => NULL),
       'template' => 'scald_dailymotion_player'
+    ),
+    'scald_dailymotion_imports_table' => array(
+      'arguments' => array('form' => NULL),
+      'file' => 'scald_dailymotion.admin.inc'
     )
   );
 }
 
 /**
+ * Implements hook_perm.
+ */
+function scald_dailymotion_perm() {
+  return array('administer dailymotion imports');
+}
+
+/**
+ * Implements hook_cron.
+ */
+function scald_dailymotion_cron() {
+  $imports = variable_get('scald_dailymotion_imports', array());
+  foreach ($imports as $import) {
+    $items = scald_dailymotion_feed($import['type'], $import['value']);
+    foreach ($items as $item) {
+      scald_dailymotion_register($item);
+    }
+  }
+}
+
+/**
+ * Implements hook_menu.
+ */
+function scald_dailymotion_menu() {
+  $items = array();
+  $items['admin/settings/scald_dailymotion'] = array(
+    'title' => 'DailyMotion imports',
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('scald_dailymotion_imports_form'),
+    'access arguments' => array('administer dailymotion imports'),
+    'description' => 'Configure the videos that should be automatically imported from DailyMotion',
+    'file' => 'scald_dailymotion.admin.inc'
+  );
+  return $items;
+}
+
+/**
  * Creates an atom based on a DailyMotion video id or an object
  * containing the video informations..
  * @param $video