comparison scald_dailymotion.pages.inc @ 7:f3040f91b65d

Add a manual search / import form
author Franck Deroche <franck@defr.org>
date Tue, 20 Jul 2010 11:57:23 +0200
parents
children
comparison
equal deleted inserted replaced
6:f0e59759c605 7:f3040f91b65d
1 <?php
2 // $Id$
3 /**
4 * @file
5 * Contains form handlers for the DailyMotion search form.
6 */
7
8 /**
9 * Generates the search and search results form.
10 */
11 function scald_dailymotion_search_form($form_state) {
12 $terms = $form_state['storage']['terms'];
13 $form = array();
14 $form['search'] = array(
15 '#type' => 'fieldset',
16 '#tree' => TRUE,
17 '#attributes' => array('class' => 'container-inline')
18 );
19 $form['search']['search_term'] = array(
20 '#type' => 'textfield',
21 '#title' => t('Terms'),
22 '#default_value' => $terms
23 );
24 $form['search']['submit'] = array(
25 '#type' => 'submit',
26 '#value' => t('Search'),
27 '#submit' => array('scald_dailymotion_search_form_search_submit')
28 );
29 if (!empty($terms)) {
30 $form['results'] = array(
31 '#type' => 'fieldset',
32 '#title' => t('Search results'),
33 '#tree' => TRUE,
34 '#theme' => 'scald_dailymotion_search_results_table'
35 );
36 $items = scald_dailymotion_feed('search', $terms);
37 if (count($items)) {
38 foreach ($items as $video) {
39 $thumb = str_replace('large', 'small', $video->thumbnail['src']);
40 $form['results'][$video->id] = array(
41 'checkbox' => array(
42 '#type' => 'checkbox',
43 ),
44 'title' => array(
45 '#type' => 'item',
46 '#value' => $video->title
47 ),
48 'thumbnail' => array(
49 '#type' => 'markup',
50 '#value' => theme('image', $thumb, '', '', NULL, FALSE)
51 )
52 );
53 }
54 $form['import'] = array(
55 '#type' => 'submit',
56 '#value' => t('Import'),
57 '#submit' => array('scald_dailymotion_search_form_submit')
58 );
59 }
60 else {
61 $form['results']['empty'] = array(
62 '#type' => 'markup',
63 '#value' => t('No results')
64 );
65 }
66 }
67 return $form;
68 }
69
70 /**
71 * Handles search terms form submission.
72 */
73 function scald_dailymotion_search_form_search_submit($form, &$form_state) {
74 if ($form_state['clicked_button']['#value'] == t('Search')) {
75 $form_state['storage']['terms'] = $form_state['values']['search']['search_term'];
76 }
77 }
78
79 /**
80 * Handlers import form submission.
81 */
82 function scald_dailymotion_search_form_submit($form, &$form_state) {
83 $ids = array();
84 // Find all the elements that have been checked in the results table
85 foreach ($form_state['values']['results'] as $id => $element) {
86 if ($element['checkbox']) {
87 $ids[] = $id;
88 }
89 }
90 // And now create an atom for each of them
91 foreach ($ids as $id) {
92 $sid = scald_dailymotion_register($id);
93 $video = scald_fetch($sid);
94 drupal_set_message(t('Created video %title', array('%title' => $video->title)));
95 }
96 // End the multistep workflow
97 unset($form_state['storage']);
98 $form_state['rebuild'] = FALSE;
99 }
100
101 /**
102 * Themes the results table.
103 */
104 function theme_scald_dailymotion_search_results_table($form) {
105 $headers = array(t('Import'), t('Title'), t('Thumbnail'));
106 $rows = array();
107 foreach (element_children($form) as $key) {
108 // Render our row
109 $row = array();
110 $row[] = drupal_render($form[$key]['checkbox']);
111 $row[] = drupal_render($form[$key]['title']);
112 $row[] = drupal_render($form[$key]['thumbnail']);
113 // And add it to the list of table rows.
114 $rows[] = $row;
115 }
116 return theme('table', $headers, $rows);
117 }