aboutsummaryrefslogtreecommitdiff
path: root/types/videoftp/videoftp.module
blob: 078d81cd6497a903099f3650f2f2bba0eb281f7f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
// $Id$

/**
 * @file
 * video ftp core hooks and menu callbacks.
 */

module_load_include('inc', 'videoftp', 'videoftp_widget');

/*
 * Implementation of hook_menu().
 */
function videoftp_menu() {
  $items = array();
  $items['videoftp/ahah/%/%/%'] = array(
    'page callback' => 'videoftp_js',
    'page arguments' => array(2, 3, 4),
    //'access callback' => 'videoftp_edit_access',
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );
  $items['videoftp/progress'] = array(
    'page callback' => 'videoftp_progress',
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );
  return $items;
}

/**
 * Implementation of hook_theme().
 */
function videoftp_theme() {
  $theme = array();
  $theme['videoftp_widget'] = array(
    'arguments' => array('element' => NULL),
    'file' => 'videoftp.theme.inc',
  );
  $theme['videoftp_widget_item'] = array(
    'arguments' => array('element' => NULL),
    'file' => 'videoftp.theme.inc',
  );
  $theme['videoftp_widget_file'] = array(
    'arguments' => array('element' => NULL),
    'file' => 'videoftp.theme.inc',
  );
  return $theme;
}

/**
 * Implementation of CCK's hook_widget_info().
 */
function videoftp_widget_info() {
  return array(
    'videoftp_widget' => array(
      'label' => t('Video FTP'),
      'field types' => array('filefield'),
      'multiple values' => CONTENT_HANDLE_CORE,
      'callbacks' => array('default value' => CONTENT_CALLBACK_CUSTOM),
      'description' => t('Widget allows you to select video files uploaded through FTP to be attached to the node.'),
    ),
  );
}

/**
 * Implementation of hook_elements().
 */
function videoftp_elements() {
  return array(
    'videoftp_widget' => array(
      '#input' => TRUE,
      '#columns' => array('fid', 'list', 'data', 'filepath'),
      '#process' => array('videoftp_widget_process'),
  	  '#value_callback' => 'videoftp_widget_value',
    ),
  );
}

/**
 * Implementation of CCK's hook_widget_settings().
 */
function videoftp_widget_settings($op, $widget) {
  //load up our include file for the widget
  module_load_include('inc', 'videoftp', 'videoftp_widget');
  switch ($op) {
    case 'form':
      return videoftp_widget_settings_form($widget);
    case 'validate':
      return videoftp_widget_settings_validate($widget);
    case 'save':
      return videoftp_widget_settings_save($widget);
  }
}

/**
 * Implementation of hook_widget().
 */
function videoftp_widget(&$form, &$form_state, $field, $items, $delta = NULL) {
  $item = array('fid' => 0, 'list' => $field['list_default'], 'data' => array('description' => '', 'video_thumb' => ''));
  if (isset($items[$delta])) {
    $item = array_merge($item, $items[$delta]);
  }
  $element = array(
    '#title' => $field['widget']['label'],
    '#type' => $field['widget']['type'],
    '#default_value' => $item,
  );
  return $element;
}

/**
 * Menu callback; Shared AHAH callback for ftp file attachment and deletions.
 *
 * This rebuilds the form element for a particular field item. As long as the
 * form processing is properly encapsulated in the widget element the form
 * should rebuild correctly using FAPI without the need for additional callbacks
 * or processing.
 */
function videoftp_js($type_name, $field_name, $delta) {
  module_load_include('inc', 'videoftp', 'videoftp_widget');
  $field = content_fields($field_name, $type_name);

  if (empty($field) || empty($_POST['form_build_id'])) {
    // Invalid request.
    drupal_set_message(t('An unrecoverable error occurred.'), 'error');
    print drupal_to_js(array('data' => theme('status_messages')));
    exit;
  }

  // Build the new form.
  $form_state = array('submitted' => FALSE);
  $form_build_id = $_POST['form_build_id'];
  $form = form_get_cache($form_build_id, $form_state);

  if (!$form) {
    // Invalid form_build_id.
    drupal_set_message(t('An unrecoverable error occurred. This form was missing from the server cache. Try reloading the page and submitting again.'), 'error');
    print drupal_to_js(array('data' => theme('status_messages')));
    exit;
  }

  // Build the form. This calls the file field's #value_callback function and
  // saves the uploaded file. Since this form is already marked as cached
  // (the #cache property is TRUE), the cache is updated automatically and we
  // don't need to call form_set_cache().
  $args = $form['#parameters'];
  $form_id = array_shift($args);
  $form['#post'] = $_POST;
  $form = form_builder($form_id, $form, $form_state);

  // Update the cached form with the new element at the right place in the form.
  if (module_exists('fieldgroup') && ($group_name = _fieldgroup_field_get_group($type_name, $field_name))) {
    if (isset($form['#multigroups']) && isset($form['#multigroups'][$group_name][$field_name])) {
      $form_element = $form[$group_name][$delta][$field_name];
    }
    else {
      $form_element = $form[$group_name][$field_name][$delta];
    }
  }
  else {
    $form_element = $form[$field_name][$delta];
  }

  if (isset($form_element['_weight'])) {
    unset($form_element['_weight']);
  }
  $output = drupal_render($form_element);
  // AHAH is not being nice to us and doesn't know the "other" button (that is,
  // either "Attach" or "Delete") yet. Which in turn causes it not to attach
  // AHAH behaviours after replacing the element. So we need to tell it first.

  // Loop through the JS settings and find the settings needed for our buttons.
  $javascript = drupal_add_js(NULL, NULL);
  $videoftp_ahah_settings = array();
  if (isset($javascript['setting'])) {
    foreach ($javascript['setting'] as $settings) {
      if (isset($settings['ahah'])) {
        foreach ($settings['ahah'] as $id => $ahah_settings) {
          if (strpos($id, 'videoftp-attach') || strpos($id, 'videoftp-remove')) {
            $videoftp_ahah_settings[$id] = $ahah_settings;
          }
        }
      }
    }
  }

  // Add the AHAH settings needed for our new buttons.
  if (!empty($videoftp_ahah_settings)) {
    $output .= '<script type="text/javascript">jQuery.extend(Drupal.settings.ahah, '. drupal_to_js($videoftp_ahah_settings) .');</script>';
  }

  $output = theme('status_messages') . $output;

  // For some reason, file uploads don't like drupal_json() with its manual
  // setting of the text/javascript HTTP header. So use this one instead.
  $GLOBALS['devel_shutdown'] = FALSE;
  print drupal_to_js(array('status' => TRUE, 'data' => $output));
  exit;
}

/**
 * filefield source support
 */
function videoftp_filefield_sources_widgets() {
  return array('videoftp_widget');
}