/[drupal]/contributions/modules/youtube_cck/youtube_cck.module
ViewVC logotype

Contents of /contributions/modules/youtube_cck/youtube_cck.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.7 - (show annotations) (download) (as text)
Thu Dec 13 20:58:24 2007 UTC (23 months, 1 week ago) by aaron
Branch: MAIN
CVS Tags: HEAD
Changes since 1.6: +212 -2 lines
File MIME type: text/x-php
convert youtube_cck types to emfield
1 <?php
2 // $Id: youtube_cck.module,v 1.6 2007/10/17 18:12:53 aaron Exp $
3
4 define('YOUTUBE_CCK_REST_ENDPOINT', 'http://www.youtube.com/api2_rest');
5
6 function youtube_cck_menu($may_cache) {
7 $items = array();
8 if ($may_cache) {
9 $items[] = array(
10 'path' => 'admin/settings/youtube_cck',
11 'title' => t('YouTube CCK configuration'),
12 'description' => t('Configure YouTube CCK API key. Necessary if you wish to display YouTube thumbnails with the videos.'),
13 'callback' => 'drupal_get_form',
14 'callback arguments' => 'youtube_cck_settings',
15 'access' => user_access('administer site configuration'));
16 $items[] = array(
17 'path' => 'admin/settings/youtube_cck/configure',
18 'title' => t('Configure'),
19 'description' => t('Configure YouTube CCK API key. Necessary if you wish to display YouTube thumbnails with the videos.'),
20 'callback' => 'drupal_get_form',
21 'callback arguments' => 'youtube_cck_settings',
22 'access' => user_access('administer site configuration'),
23 'type' => MENU_DEFAULT_LOCAL_TASK,
24 );
25 $items[] = array(
26 'path' => 'admin/settings/youtube_cck/convert',
27 'title' => t('Convert'),
28 'callback' => 'drupal_get_form',
29 'callback arguments' => 'youtube_cck_convert_settings',
30 'access' => user_access('administer site configuration'),
31 'type' => MENU_LOCAL_TASK,
32 );
33 }
34 return $items;
35 }
36
37 function youtube_cck_settings() {
38 drupal_set_message(t('The @youtube module is deprecated. It is highly recommended that you install the !emfield module and its included @emvideo module, and convert any @youtube types from the !conversion.',
39 array(
40 '@youtube' => 'YouTube CCK',
41 '!emfield' => '<a href="http://drupal.org/project/emfield">Embedded Media Field</a>',
42 '@emvideo' => 'Embedded Video Field',
43 '!conversion' => l(t('conversion page'), 'admin/settings/youtube_cck/convert'),
44 )), 'error');
45
46 // link to http://www.youtube.com/my_profile_dev for api code
47 $form['api'] = array(
48 '#type' => 'fieldset',
49 '#title' => t('YouTube API'),
50 '#description' => t('If you wish to be able to display YouTube thumbnails automatically, you will first need to apply for an API Developer Key from the !youtube. Note that you do not need this key to display YouTube videos themselves.', array('!youtube' => l('YouTube Developer Profile page', 'http://www.youtube.com/my_profile_dev', array('target' => '_blank')))),
51 '#collapsible' => true,
52 '#collapsed' => false,
53 );
54 $form['api']['youtube_cck_api_key'] = array(
55 '#type' => 'textfield',
56 '#title' => t('YouTube API Key'),
57 '#default_value' => variable_get('youtube_cck_api_key', ''),
58 '#description' => t('Please enter your YouTube Developer Key here.'),
59 );
60 $form['api']['youtube_cck_api_secret'] = array(
61 '#type' => 'textfield',
62 '#title' => t('YouTube API Secret'),
63 '#default_value' => variable_get('youtube_cck_api_secret', ''),
64 '#description' => t('If you have a secret for the YouTube API, enter it here.'),
65 );
66 return system_settings_form($form);
67 }
68
69 /**
70 * Submit a request to YouTube. Borrowed & modified from the flickr.module
71 * call like: youtube_cck_request('youtube.videos.get_details', array('video_id' => '0AFjIUhGvmI'));
72 *
73 * @param $method
74 * string method name
75 * @param $args
76 * associative array of arguments names and values
77 * @param $cacheable
78 * boolean indicating if it's safe cache the results of this request
79 *
80 * @return
81 * the result of the request
82 */
83 function youtube_cck_request($method, $args = array(), $cacheable = TRUE) {
84 $args['dev_id'] = trim(variable_get('youtube_cck_api_key', ''));
85 $args['method'] = $method;
86 ksort($args);
87
88 // build an argument hash that we'll use for the cache id and api signing
89 $arghash = '';
90 foreach($args as $k => $v){
91 $arghash .= $k . $v;
92 }
93 // if we've got a secret sign the arguments
94 // TODO: doesn't seem to matter
95 // if ($secret = trim(variable_get('youtube_cck_api_secret', ''))) {
96 // $args['api_sig'] = md5($secret . $arghash);
97 // }
98
99 // build the url
100 foreach ($args as $k => $v){
101 $encoded_params[] = urlencode($k).'='.urlencode($v);
102 }
103 $url = YOUTUBE_CCK_REST_ENDPOINT .'?'. implode('&', $encoded_params);
104
105 // if it's a cachable request, try to load a cached value
106 if ($cacheable) {
107 if ($cache = cache_get($arghash, 'cache')) {
108 return unserialize($cache->data);
109 }
110 }
111
112 // connect and fetch a value
113 $result = drupal_http_request($url);
114
115 if ($result->code == 200) {
116 $parser = drupal_xml_parser_create($result->data);
117 $vals = array();
118 $index = array();
119 xml_parse_into_struct($parser, $result->data, $vals, $index);
120 xml_parser_free($parser);
121
122 $params = array();
123 $level = array();
124 $start_level = 1;
125 foreach ($vals as $xml_elem) {
126 if ($xml_elem['type'] == 'open') {
127 if (array_key_exists('attributes',$xml_elem)) {
128 list($level[$xml_elem['level']],$extra) = array_values($xml_elem['attributes']);
129 } else {
130 $level[$xml_elem['level']] = $xml_elem['tag'];
131 }
132 }
133 if ($xml_elem['type'] == 'complete') {
134 $php_stmt = '$params';
135 while($start_level < $xml_elem['level']) {
136 $php_stmt .= '[$level['.$start_level.']]';
137 $start_level++;
138 }
139 $php_stmt .= '[$xml_elem[\'tag\']][] = $xml_elem[\'value\'];';
140 eval($php_stmt);
141 $start_level--;
142 }
143 }
144
145 // save a cacheable result for future use
146 if ($cacheable) {
147 cache_set($arghash, 'cache', serialize($params), time() + 3600);
148 }
149 return $params;
150 }
151 return array();
152 }
153
154 /**Implementation of hook_field_info **/
155
156 function youtube_cck_field_info() {
157 $fields = array(
158 'youtube_cck_youtube' => array('label' => t('YouTube Video')),
159 );
160 return $fields;
161 }
162
163 /** Implementation of hook_field_settings **/
164
165 function youtube_cck_field_settings($op, $field) {
166 switch ($op) {
167
168 case 'database columns':
169 $columns = array(
170 'embed' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => "''", 'sortable' => TRUE),
171 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => "''", 'sortable' => TRUE),
172 );
173 switch ($field['type']) {
174 case 'youtube_cck_youtube':
175 break;
176 }
177 return $columns;
178 }
179 }
180
181 /** Implementation of hook_field **/
182
183 function youtube_cck_field($op, &$node, $field, &$items, $teaser, $page) {
184 switch ($op) {
185 case 'view':
186 foreach ($items as $delta => $item) {
187 $items[$delta]['view'] = content_format($field, $item, 'default', $node);
188 }
189 return theme('field', $node, $field, $items, $teaser, $page);
190
191 case 'validate':
192 if ($field['multiple']) {
193 foreach ($items as $delta => $item) {
194 $error_field = $field['field_name'].']['.$delta.'][embed';
195
196 _youtube_cck_field_validate_id($field, $item, $error_field);
197 }
198 }
199 else {
200 $error_field = $field['field_name'];
201
202 _youtube_cck_field_validate_id($field, $items[0], $error_field);
203 }
204 break;
205
206 case 'submit':
207 if ($field['multiple']) {
208 foreach ($items as $delta => $item) {
209 $error_field = $field['field_name'].']['.$delta.'][embed';
210 $items[$delta]['value'] = _youtube_cck_field_submit_id($item);
211 }
212 }
213 else {
214 $error_field = $field['field_name'];
215 $items[0]['value'] = _youtube_cck_field_submit_id($items[0]);
216 }
217 break;
218 }
219 }
220
221 /**
222 * extract the youtube id from embedded code or video url
223 */
224 function _youtube_cck_field_validate_id($field, $item, $error_field) {
225 return _youtube_cck_field_submit_id($item);
226 }
227
228 /**
229 * replace youtube embedded code with the extracted id. this goes in the field 'value'
230 * also allows you to grab directly from the URL to play the video
231 */
232 function _youtube_cck_field_submit_id($item) {
233 $embed = $item['embed'];
234
235 // src="http://www.youtube.com/v/nvbQQnvxXDk"
236 if ($embed && preg_match('@youtube\.com/v/([^"\&]+)@i', $embed, $matches)) {
237 $item['value'] = $matches[1];
238 }
239 else if ($embed && preg_match('@youtube\.com/watch\?v=([^"\&]+)@i', $embed, $matches)) {
240 $item['value'] = $matches[1];
241 }
242 else {
243 $item['value'] = $embed;
244 }
245 return $item['value'];
246 }
247
248 /** Implementation of hook_field_formatter_info **/
249 function youtube_cck_field_formatter_info() {
250 $types = array('youtube_cck_youtube',);
251 $formats = array(
252 'default' => array(
253 'label' => t('Default'),
254 'field types' => $types,
255 ),
256 'youtube_video' => array(
257 'label' => t('YouTube Full Size Video'),
258 'field types' => $types,
259 ),
260 'youtube_preview' => array(
261 'label' => t('YouTube Preview Video'),
262 'field types' => $types,
263 ),
264 'youtube_thumbnail' => array(
265 'label' => t('YouTube Thumbnail'),
266 'field types' => $types,
267 ),
268 );
269 return $formats;
270 }
271
272 /** Implementation of hook_field_formatter **/
273
274 function youtube_cck_field_formatter($field, $item, $formatter, $node) {
275 if (!isset($item['value'])) {
276 return '';
277 }
278
279 // unfortunately, when we come from a view, we don't get all the widget fields
280 if (!$node->type) {
281 $type = content_types($field['type_name']);
282 $field['widget'] = $type['fields'][$field['field_name']]['widget'];
283 }
284
285 switch ($formatter) {
286 case 'youtube_thumbnail':
287 $output .= theme('youtube_cck_thumbnail', $field, $item);
288 break;
289 case 'youtube_preview':
290 $output .= theme('youtube_cck_preview', $field, $item);
291 break;
292 case 'youtube_video':
293 case 'default':
294 default:
295 $output .= theme('youtube_cck_video', $field, $item);
296 break;
297 }
298 return $output;
299 }
300
301 /** Widgets **/
302
303 /** Implementation of hook_widget_info **/
304 function youtube_cck_widget_info() {
305 return array(
306 'youtube_cck_textfields' => array(
307 'label' => t('Text Fields'),
308 'field types' => array('youtube_cck_youtube',),
309 ),
310 );
311 }
312
313 function youtube_cck_widget_settings($op, $widget) {
314 switch ($op) {
315 case 'form':
316 $form = array();
317 if ($widget['type'] == 'youtube_cck_textfields') {
318 $form['video'] = array(
319 '#type' => 'fieldset',
320 '#title' => t('YouTube Video Settings'),
321 '#description' => t('These settings control how this video is displayed in its full size, which defaults to 350x425.'),
322 '#collapsible' => true,
323 '#collapsed' => false,
324 );
325 $form['video']['video_height'] = array(
326 '#type' => 'textfield',
327 '#title' => t('YouTube video height'),
328 '#default_value' => $widget['video_height'] ? $widget['video_height'] : 350,
329 '#required' => true,
330 '#description' => t('The height of the YouTube video. It defaults to 350.'),
331 );
332 $form['video']['video_width'] = array(
333 '#type' => 'textfield',
334 '#title' => t('YouTube video width'),
335 '#default_value' => $widget['video_width'] ? $widget['video_width'] : 425,
336 '#required' => true,
337 '#description' => t('The width of the YouTube video. It defaults to 425.'),
338 );
339
340 $form['preview'] = array(
341 '#type' => 'fieldset',
342 '#title' => t('YouTube Video Settings'),
343 '#description' => t('These settings control how this video is displayed in its full size, which defaults to 350x425.'),
344 '#collapsible' => true,
345 '#collapsed' => false,
346 );
347 $form['preview']['preview_height'] = array(
348 '#type' => 'textfield',
349 '#title' => t('YouTube preview height'),
350 '#default_value' => $widget['preview_height'] ? $widget['preview_height'] : 350,
351 '#required' => true,
352 '#description' => t('The height of the YouTube preview video. It defaults to 350.'),
353 );
354 $form['preview']['preview_width'] = array(
355 '#type' => 'textfield',
356 '#title' => t('YouTube preview width'),
357 '#default_value' => $widget['preview_width'] ? $widget['preview_width'] : 425,
358 '#required' => true,
359 '#description' => t('The width of the YouTube preview video. It defaults to 425.'),
360 );
361
362 $form['tn'] = array(
363 '#type' => 'fieldset',
364 '#title' => t('Thumbnail'),
365 '#description' => t('These settings control what thumbnail to display from YouTube.'),
366 '#collapsible' => true,
367 '#collapsed' => false,
368 );
369 $form['tn']['tn_display'] = array(
370 '#type' => 'select',
371 '#title' => t('What YouTube Thumbnail to Display'),
372 '#default_value' => $widget['tn_display'],
373 '#options' => array(
374 0 => t('Random Thumbnail'),
375 1 => t('First Thumbnail'),
376 2 => t('Second Thumbnail'),
377 3 => t('Third Thumbnail'),
378 'all' => t('All Thumbnails'),
379 ),
380 );
381 }
382 return $form;
383
384 case 'validate':
385 if ($widget['type'] == 'youtube_cck_textfields') {
386 if (!is_numeric($widget['video_width']) || intval($widget['video_width']) != $widget['video_width'] || $widget['video_width'] < 1) {
387 form_set_error('video_width', t('"Video Width" must be a positive integer.'));
388 }
389 if (!is_numeric($widget['video_height']) || intval($widget['video_height']) != $widget['video_height'] || $widget['video_height'] < 1) {
390 form_set_error('video_height', t('"Video Height" must be a positive integer.'));
391 }
392 if (!is_numeric($widget['preview_width']) || intval($widget['preview_width']) != $widget['preview_width'] || $widget['preview_width'] < 1) {
393 form_set_error('preview_width', t('"Preview Width" must be a positive integer.'));
394 }
395 if (!is_numeric($widget['preview_height']) || intval($widget['preview_height']) != $widget['preview_height'] || $widget['preview_height'] < 1) {
396 form_set_error('preview_height', t('"Preview Height" must be a positive integer.'));
397 }
398 }
399 break;
400
401 case 'save':
402 if ($widget['widget']['type'] == 'youtube_cck_textfields') {
403 return array('video_width', 'video_height', 'preview_width', 'preview_height', 'tn_display', );
404 }
405 break;
406 }
407 }
408
409 /** Implementation of hook_widget **/
410
411 function youtube_cck_widget($op, &$node, $field, &$node_field) {
412 switch ($op) {
413 case 'form':
414 $form = array();
415
416 $form['youtube_cck_fieldset'] = array(
417 '#type' => 'fieldset',
418 '#title' => t($field['widget']['label']),
419 '#description' => $field['widget']['description'],
420 '#collapsible' => true,
421 '#collapsed' => false,
422 );
423
424 $form['youtube_cck_fieldset'][$field['field_name']] = array('#tree' => TRUE);
425 $textfield = 'embed';
426 $field['required'] = FALSE;
427 $textfield_title = t('YouTube Embed Code, Video URL, or Video ID');
428 $textfield_description = t('Enter the Embed Code, Video URL, or Video ID here. For instance, you might enter the URL from the address bar, from the Embed bar provided by YouTube, or simply cut and paste the YouTube video code.');
429
430 if ($field['multiple']) {
431 $form['youtube_cck_fieldset'][$field['field_name']]['#type'] = 'fieldset';
432 $form['youtube_cck_fieldset'][$field['field_name']]['#title'] = t($field['widget']['label']);
433 $delta = 0;
434 foreach ($node_field as $data) {
435 if (isset($data[$textfield])) {
436 $form['youtube_cck_fieldset'][$field['field_name']][$delta][$textfield] = array(
437 '#type' => 'textfield',
438 '#title' => $textfield_title,
439 '#description' => $textfield_description,
440 '#default_value' => $data[$textfield],
441 '#required' => ($delta == 0) ? $field['required'] : FALSE,
442 '#maxlength' => 255,
443 );
444 $form['youtube_cck_fieldset'][$field['field_name']][$delta]['value'] = array(
445 '#type' => 'value',
446 '#value' => $data['value'],
447 );
448 if ($data['value']) {
449 $form['youtube_cck_fieldset'][$field['field_name']][$delta]['markup_value'] = array(
450 '#type' => 'markup',
451 '#value' => t('(YouTube Video ID: @value)', array('@value' => $data['value'])),
452 );
453 }
454 $delta++;
455 }
456 }
457 foreach (range($delta, $delta + 2) as $delta) {
458 $form['youtube_cck_fieldset'][$field['field_name']][$delta][$textfield] = array(
459 '#type' => 'textfield',
460 '#title' => $textfield_title,
461 '#description' => $textfield_description,
462 '#default_value' => '',
463 '#required' => ($delta == 0) ? $field['required'] : FALSE,
464 '#maxlength' => 255,
465 );
466 $form['youtube_cck_fieldset'][$field['field_name']][$delta]['value'] = array(
467 '#type' => 'value',
468 '#title' => '',
469 );
470 }
471 }
472 else {
473 $form['youtube_cck_fieldset'][$field['field_name']][0][$textfield] = array(
474 '#type' => 'textfield',
475 '#title' => $textfield_title, //t($field['widget']['label']),
476 '#description' => $textfield_description,
477 '#default_value' => isset($node_field[0][$textfield]) ? $node_field[0][$textfield] : '',
478 '#required' => $field['required'],
479 '#maxlength' => 255,
480 );
481 if ($textfield == 'embed') {
482 $value = isset($node_field[0]['value']) ? $node_field[0]['value'] : '';
483 $form['youtube_cck_fieldset'][$field['field_name']][0]['value'] = array(
484 '#type' => 'value',
485 '#value' => $value,
486 );
487 if ($value) {
488 $form['youtube_cck_fieldset'][$field['field_name']][0]['value_markup'] = array(
489 '#type' => 'markup',
490 '#value' => t('(YouTube Video ID: @value)', array('@value' => $value)),
491 );
492 }
493 }
494 }
495 return $form;
496 default:
497 break;
498 }
499 }
500
501 function youtube_cck_thumbnail($youtube_id) {
502 $request = youtube_cck_request('youtube.videos.get_details', array('video_id' => $youtube_id));
503 return $request['THUMBNAIL_URL'][0];
504 }
505
506 function theme_youtube_cck_thumbnail ($field, $item) {
507 if ($item['value']) {
508 $tn = youtube_cck_thumbnail($item['value']);
509 if ($tn) {
510 return '<img src="' . $tn . '" />';
511 }
512 }
513 }
514
515 function theme_youtube_cck_video($field, $item) {
516 $width = $field['widget']['video_width'];
517 $height = $field['widget']['video_height'];
518 $embed = $item['value'];
519 $output = theme('youtube_cck_youtube_flash', $embed, $width, $height);
520 return $output;
521 }
522
523 function theme_youtube_cck_preview($field, $item) {
524 $width = $field['widget']['preview_width'];
525 $height = $field['widget']['preview_height'];
526 $embed = $item['value'];
527 $output = theme('youtube_cck_youtube_flash', $embed, $width, $height);
528 return $output;
529 }
530
531 function theme_youtube_cck_youtube_flash($embed, $width, $height) {
532 if ($embed) {
533 $output .= "<object height=\"$height\" width=\"$width\"><param name=\"movie\" value=\"http://www.youtube.com/v/$embed\"><param name=\"wmode\" value=\"transparent\"><embed src=\"http://www.youtube.com/v/$embed\" type=\"application/x-shockwave-flash\" wmode=\"transparent\" height=\"$height\" width=\"$width\"></object>";
534 }
535 return $output;
536 }
537
538 /**
539 * This is the form that will convert all your youtube_cck types to emfield video types.
540 */
541 function youtube_cck_convert_settings() {
542 $remove_msg = t('You currently have no types using %youtube that need to be converted. You may safely disable and remove the %youtube module now. If you experienced any difficulties with this process, please !report.', array('%youtube' => 'YouTube CCK', '!report' => l(t('report your problems'), 'http://drupal.org/project/issues/youtube_cck')));
543 $form = array();
544 $form['convert'] = array(
545 '#type' => 'fieldset',
546 '#title' => t('Convert types'),
547 '#description' => t('The following types are registered as belonging to the %youtubecck module. Pressing the Convert button will convert them to work with %emfield instead. After doing so, or if you don\'t see any types on this page, you can safely disable the %youtubecck module. Note that the fields themselves will be changed, but will keep any data already stored.', array('%youtubecck' => 'YouTube CCK', '%emfield' => 'Embedded Media Field')),
548 );
549 $system_types = _content_type_info();
550 $content_types = $system_types['content types'];
551 $field_types = $system_types['field types'];
552 $types = array();
553 foreach ($content_types as $content_type => $type) {
554 // determine which content types implement this module
555 foreach ($type['fields'] as $field_type => $field) {
556 if ($field_types[$field['type']]['module'] == 'youtube_cck') {
557 $types[$content_type][$field_type] = $field;
558 }
559 }
560 }
561
562 // if we have any video_cck types, then list them here
563 if (!empty($types)) {
564 drupal_set_message(t('The @youtube module is deprecated. It is highly recommended that you install the !emfield module and its included @emvideo module, and convert any @youtube types from this screen. PLEASE make sure to create a backup of your database in case anything goes wrong. If you experience any difficulties with this process, please !report.', array('@youtube' => 'YouTube CCK', '!emfield' => '<a href="http://drupal.org/project/emfield">Embedded Media Field</a>', '@emvideo' => 'Embedded Video Field', '!report' => l(t('report your problems'), 'http://drupal.org/project/issues/youtube_cck'))), 'error');
565 foreach ($types as $type_name => $type) {
566 $fields = array();
567 // add a link for each field that uses youtube videos
568 foreach ($type as $field_name => $field) {
569 $fields[$field_name] = l($field['field_name'], 'admin/content/types/' . $type_name . '/fields/' . $field['field_name']);
570 }
571 // add a link to the content type edit page
572 $options[$type_name] = l($content_types[$type_name]['name'], 'admin/content/types/' . $type_name) . ' (' . implode(', ', $fields) . ')';
573 }
574
575 //list all the types to be converted
576 $form['convert']['types'] = array(
577 '#type' => 'markup',
578 '#value' => t('The following types will be converted: !types', array('!types' => theme('item_list', $options))),
579 );
580
581 // if we don't have emfield installed, then show a message here, and don't include a submit button
582 if (!module_exists('emfield') || !module_exists('video_cck')) {
583 drupal_set_message('You must have the !emfield module and its included @emvideo module active before converting types.', array('!emfield' => '<a href="http://drupal.org/project/emfield">Embedded Media Field</a>', '@emvideo' => 'Embedded Video Field'), 'error');
584 }
585 else {
586 $form['convert']['submit'] = array(
587 '#type' => 'submit',
588 '#value' => t('Convert types'),
589 );
590 }
591 }
592 else {
593 // we don't have any types. safe to disable this module now
594 drupal_set_message($remove_msg);
595 $form['convert']['na'] = array(
596 '#type' => 'markup',
597 '#value' => $remove_msg,
598 );
599 }
600
601 // display a warning to change any relevant code
602 drupal_set_message(t('Note that if you are using any custom code in template files to display videos in nodes, you may need to rewrite some code. Specifically, although the field names themselves will be untouched, the field formatters have changed. The formatter for youtube_video has become video_video, youtube_preview is now video_preview, and youtube_thumbnail is video_thumbnail.'), 'error');
603 return $form;
604 }
605
606 /**
607 * convert all existing types that contain youtube_cck fields to emfield
608 */
609 function youtube_cck_convert_settings_submit($form_id, $form_values) {
610 if (!module_exists('emfield') || !module_exists('video_cck')) {
611 drupal_set_message('You must have the !emfield module and its included @emvideo module active before converting types.', array('!emfield' => '<a href="http://drupal.org/project/emfield">Embedded Media Field</a>', '@emvideo' => 'Embedded Video Field'), 'error');
612 return;
613 }
614 $system_types = _content_type_info();
615 $content_types = $system_types['content types'];
616 $field_types = $system_types['field types'];
617 $types = array();
618 $field_tables = array();
619
620 // a list of display/view formatters that need to be converted
621 $formatters = array(
622 'youtube_video' => 'video_video',
623 'youtube_preview' => 'video_preview',
624 'youtube_thumbnail' => 'video_thumbnail',
625 );
626
627 // make a list of types to be converted
628 foreach ($content_types as $content_type => $type) {
629 // determine which content types implement this module
630 foreach ($type['fields'] as $field_type => $field) {
631
632 // if the type has a youtube_cck field, then mark it to be converted
633 if ($field_types[$field['type']]['module'] == 'youtube_cck') {
634 $types[$content_type][$field_type] = $field;
635 if (!empty($field_tables[$field_type]) || $field['multiple']) {
636 $field_tables[$field_type] = 'content_' . $field_type;
637 }
638 else {
639 $field_tables[$field_type] = 'content_type_' . $content_type;
640 }
641 }
642 }
643 }
644
645 // stop if no types for youtube_cck
646 if (empty($types)) {
647 drupal_set_message('You have no types using YouTube CCK that need to be converted.', 'error');
648 return;
649 }
650
651 // set display settings and widget types
652 foreach ($types as $type_name => $fields) {
653 foreach ($fields as $field_name => $field) {
654 $display_settings = unserialize(db_result(db_query_range("SELECT display_settings FROM {node_field_instance} WHERE type_name = '%s' AND field_name='%s'", $type_name, $field_name, 0, 1)));
655 if (is_array($display_settings)) {
656 foreach ($display_settings as $display => $formatter) {
657 foreach ($formatters as $old => $new) {
658 if ($formatter['format'] == $old) {
659 $display_settings[$display]['format'] = $new;
660 }
661 }
662 }
663 db_query("UPDATE {node_field_instance} SET widget_type = 'video_cck_textfields', display_settings='%s' WHERE type_name = '%s' AND field_name = '%s'", serialize($display_settings), $type_name, $field_name);
664 }
665 else {
666 db_query("UPDATE {node_field_instance} SET widget_type = 'video_cck_textfields' WHERE type_name='%s' AND field_name = '%s'", $type_name, $field_name);
667 }
668 }
669 }
670
671 // change node content tables
672 foreach ($field_tables as $field_name => $table) {
673 db_query("UPDATE {node_field} SET type='video_cck' WHERE field_name = '%s'", $field_name);
674 $field_name_embed = $field_name . '_embed';
675 $field_name_provider = $field_name . '_provider';
676 $field_name_data = $field_name . '_data';
677 switch ($GLOBALS['db_type']) {
678 case 'pgsql':
679 $longtext = "text NOT NULL default ''";
680 break;
681 case 'mysql':
682 case 'mysqli':
683 $longtext = 'longtext NOT NULL';
684 break;
685 }
686 db_query("ALTER TABLE {" . $table . "} CHANGE COLUMN $field_name_embed $field_name_embed $longtext");
687 db_query("ALTER TABLE {" . $table . "} ADD COLUMN $field_name_provider varchar(255) NOT NULL default ''");
688 db_query("ALTER TABLE {" . $table . "} ADD COLUMN $field_name_data $longtext");
689 }
690
691 // add youtube as provider to existing nodes with embed code/values
692 foreach ($field_tables as $field_name => $table) {
693 $field_name_provider = $field_name . '_provider';
694 $field_name_value = $field_name . '_value';
695 $field_name_data = $field_name . '_data';
696 db_query("UPDATE {" . $table . "} SET $field_name_provider='youtube', $field_name_data='%s'", serialize(array()));
697 }
698
699 // fix views tables
700 foreach ($formatters as $old => $new) {
701 db_query("UPDATE {view_tablefield} SET options='%s' WHERE options='%s'", $new, $old);
702 }
703
704 // set youtube api if we have it (for display of thumbnails)
705 if (variable_get('video_cck_youtube_api_key', '') == '') {
706 variable_set('video_cck_youtube_api_key', variable_get('youtube_cck_api_key', ''));
707 }
708 if (variable_get('video_cck_youtube_api_secret', '') == '') {
709 variable_set('video_cck_youtube_api_secret', variable_get('youtube_cck_api_secret', ''));
710 }
711
712 // clear our content
713 db_query("DELETE FROM {cache}");
714 if (module_exists('views')) {
715 db_query("DELETE FROM {cache_views}");
716 }
717
718 // rebuild our content types
719 content_enable();
720 }
721

  ViewVC Help
Powered by ViewVC 1.1.2