Projet

Général

Profil

Paste
Télécharger (4,99 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / feeds / tests / feeds_tests.module @ 2c8c2b87

1
<?php
2

    
3
/**
4
 * @file
5
 * Helper module for Feeds tests.
6
 */
7

    
8
/**
9
 * Implements hook_menu().
10
 */
11
function feeds_tests_menu() {
12
  $items['testing/feeds/flickr.xml'] = array(
13
    'page callback' => 'feeds_tests_flickr',
14
    'access arguments' => array('access content'),
15
    'type' => MENU_CALLBACK,
16
  );
17
  $items['testing/feeds/files.csv'] = array(
18
    'page callback' => 'feeds_tests_files',
19
    'access arguments' => array('access content'),
20
    'type' => MENU_CALLBACK,
21
  );
22
  return $items;
23
}
24

    
25
/**
26
 * Implements hook_theme().
27
 */
28
function feeds_tests_theme() {
29
  return array(
30
    'feeds_tests_flickr' => array(
31
      'variables' => array('image_urls' => array()),
32
      'path' => drupal_get_path('module', 'feeds_tests') . '/feeds',
33
      'template' => 'feeds-tests-flickr',
34
    ),
35
    'feeds_tests_files' => array(
36
      'variables' => array('files' => array()),
37
      'path' => drupal_get_path('module', 'feeds_tests') . '/feeds',
38
      'template' => 'feeds-tests-files',
39
    ),
40
  );
41
}
42

    
43
/**
44
 * Outputs flickr test feed.
45
 */
46
function feeds_tests_flickr() {
47
  $images = array(
48
    0 => "tubing.jpeg",
49
    1 => "foosball.jpeg",
50
    2 => "attersee.jpeg",
51
    3 => "hstreet.jpeg",
52
    4 => "la fayette.jpeg",
53
  );
54
  $path = drupal_get_path('module', 'feeds_tests') . '/feeds/assets';
55
  foreach ($images as &$image) {
56
    $image = url("$path/$image", array('absolute' => TRUE));
57
  }
58
  drupal_add_http_header('Content-Type', 'application/rss+xml; charset=utf-8');
59
  print theme('feeds_tests_flickr', array('image_urls' => $images));
60
}
61

    
62
/**
63
 * Outputs a CSV file pointing to files.
64
 */
65
function feeds_tests_files() {
66
  $images = array(
67
    0 => "tubing.jpeg",
68
    1 => "foosball.jpeg",
69
    2 => "attersee.jpeg",
70
    3 => "hstreet.jpeg",
71
    4 => "la fayette.jpeg",
72
  );
73
  foreach ($images as &$image) {
74
    $image = "public://images/$image";
75
  }
76
  drupal_add_http_header('Content-Type', 'text/plain; charset=utf-8');
77
  print theme('feeds_tests_files', array('files' => $images));
78
}
79

    
80
/**
81
 * Implements hook_feeds_processor_targets_alter()
82
 */
83
function feeds_tests_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_name) {
84
  $targets['test_target'] = array(
85
    'name' => t('Test Target'),
86
    'description' => t('This is a test target.'),
87
    'callback' => 'feeds_tests_mapper_set_target',
88
    'summary_callback' => 'feeds_tests_mapper_summary',
89
    'form_callback' => 'feeds_tests_mapper_form',
90
  );
91
}
92

    
93
/**
94
 * Set target value on entity.
95
 *
96
 * @see my_module_set_target()
97
 */
98
function feeds_tests_mapper_set_target($source, $entity, $target, $value, $mapping) {
99
  $entity->body['und'][0]['value'] = serialize($mapping);
100
}
101

    
102
/**
103
 * Provides setting summary for the mapper.
104
 *
105
 * @see my_module_summary_callback()
106
 */
107
function feeds_tests_mapper_summary($mapping, $target, $form, $form_state) {
108
  $options = array(
109
    'option1' => t('Option 1'),
110
    'option2' => t('Another Option'),
111
    'option3' => t('Option for select'),
112
    'option4' => t('Another One')
113
  );
114

    
115
  $items = array();
116
  if (!empty($mapping['checkbox']) && $mapping['checkbox']) {
117
    $items[] = t('Checkbox active.');
118
  }
119
  else {
120
    $items[] = t('Checkbox inactive.');
121
  }
122
  if (!empty($mapping['textfield'])) {
123
    $items[] = t('<strong>Textfield value</strong>: %textfield', array('%textfield' => $mapping['textfield']));
124
  }
125
  if (!empty($mapping['textarea'])) {
126
    $items[] = t('<strong>Textarea value</strong>: %textarea', array('%textarea' => $mapping['textarea']));
127
  }
128
  if (!empty($mapping['radios'])) {
129
    $items[] = t('<strong>Radios value</strong>: %radios', array('%radios' => $options[$mapping['radios']]));
130
  }
131
  if (!empty($mapping['select'])) {
132
    $items[] = t('<strong>Select value</strong>: %select', array('%select' => $options[$mapping['select']]));
133
  }
134
  $list = array(
135
    '#type' => 'ul',
136
    '#theme' => 'item_list',
137
    '#items' => $items,
138
  );
139
  return drupal_render($list);
140
}
141

    
142
/*
143
 * Provides the form with mapper settings.
144
 */
145
function feeds_tests_mapper_form($mapping, $target, $form, $form_state) {
146
  $mapping += array(
147
    'checkbox' => FALSE,
148
    'textfield' => '',
149
    'textarea' => '',
150
    'radios' => NULL,
151
    'select' => NULL,
152
  );
153
  return array(
154
    'checkbox' => array(
155
      '#type' => 'checkbox',
156
      '#title' => t('A checkbox'),
157
      '#default_value' => !empty($mapping['checkbox']),
158
    ),
159
    'textfield' => array(
160
      '#type' => 'textfield',
161
      '#title' => t('A text field'),
162
      '#default_value' => $mapping['textfield'],
163
      '#required' => TRUE,
164
    ),
165
    'textarea' => array(
166
      '#type' => 'textarea',
167
      '#title' => t('A textarea'),
168
      '#default_value' => $mapping['textarea'],
169
    ),
170
    'radios' => array(
171
      '#type' => 'radios',
172
      '#title' => t('Some radios'),
173
      '#options' => array('option1' => t('Option 1'), 'option2' => t('Another Option')),
174
      '#default_value' => $mapping['radios'],
175
    ),
176
    'select' => array(
177
      '#type' => 'select',
178
      '#title' => t('A select list'),
179
      '#options' => array('option3' => t('Option for select'), 'option4' => t('Another One')),
180
      '#default_value' => $mapping['select'],
181
    ),
182
  );
183
}
184