| 1 |
<?php
|
| 2 |
// $Id$
|
| 3 |
|
| 4 |
//
|
| 5 |
// @file cdn2_field.inc configuration settings for the cck content type
|
| 6 |
//
|
| 7 |
|
| 8 |
/**
|
| 9 |
* Declare information about the cdn2 field type
|
| 10 |
*/
|
| 11 |
function cdn2_field_info() {
|
| 12 |
return array(
|
| 13 |
'cdn2' => array('label' => 'CDN2'),
|
| 14 |
);
|
| 15 |
}
|
| 16 |
|
| 17 |
|
| 18 |
function cdn2_widget_info() {
|
| 19 |
return array(
|
| 20 |
'cdn2' => array(
|
| 21 |
'label' => 'CDN2 Field',
|
| 22 |
'field types' => array('cdn2'),
|
| 23 |
),
|
| 24 |
);
|
| 25 |
}
|
| 26 |
|
| 27 |
/**
|
| 28 |
* Handle the parameters for the cck field. (used in the add/modify field form)
|
| 29 |
*/
|
| 30 |
function cdn2_field_settings($op, $field) {
|
| 31 |
switch ($op) {
|
| 32 |
// case 'form': (not needed.. no additional options for this field type)
|
| 33 |
// case 'validate':
|
| 34 |
// case 'save':
|
| 35 |
case 'database columns':
|
| 36 |
return array(
|
| 37 |
'value' => array(
|
| 38 |
'type' => 'varchar(36)',
|
| 39 |
'not null' => TRUE,
|
| 40 |
'default' => "''",
|
| 41 |
'sortable' => FALSE
|
| 42 |
)
|
| 43 |
);
|
| 44 |
case 'callbacks':
|
| 45 |
// use our own view callback when rendering the field.
|
| 46 |
return array(
|
| 47 |
'view' => CONTENT_CALLBACK_CUSTOM,
|
| 48 |
);
|
| 49 |
// case 'tables':
|
| 50 |
// case 'arguments':
|
| 51 |
// case 'filters':
|
| 52 |
}
|
| 53 |
}
|
| 54 |
|
| 55 |
/**
|
| 56 |
* Define the behavior of the cdn2 field type
|
| 57 |
*/
|
| 58 |
function cdn2_field($op, &$node, $field, &$node_field, $teaser, $page) {
|
| 59 |
switch ($op) {
|
| 60 |
// case 'view':
|
| 61 |
// case 'validate':
|
| 62 |
// case 'submit':
|
| 63 |
// case 'insert':
|
| 64 |
// case 'update':
|
| 65 |
// case 'delete':
|
| 66 |
case 'load':
|
| 67 |
$query = 'SELECT cv.video_token, cv.preset_name, cv.status, cv.asset_fetch_url, cv.file_size, cv.video_length FROM {cdn2_videos} cv WHERE cv.nid=%d';
|
| 68 |
$resultset = db_query($query, $node->nid);
|
| 69 |
$field_name = $field['field_name'];
|
| 70 |
$additions = array();
|
| 71 |
$additions[$field_name][0]['assets'] = array();
|
| 72 |
while ($row = db_fetch_array($resultset)) {
|
| 73 |
$additions[$field_name][0]['assets'][] = $row;
|
| 74 |
// TODO: see if this is handled by the node load directly, otherwise
|
| 75 |
// fetch it from the content table.
|
| 76 |
$additions[$field_name][0]['value']= $row['video_token'];
|
| 77 |
}
|
| 78 |
|
| 79 |
$query = "SELECT nid from {cdn2_video_node} cvn WHERE video_token='%s'";
|
| 80 |
$cvn_nid = db_result(db_query($query, $additions[$field_name][0]['value']));
|
| 81 |
|
| 82 |
if ($cvn_nid != 0 && !count($additions[$field_name][0]['assets'])) {
|
| 83 |
$additions[$field_name][0]['processing'] = TRUE;
|
| 84 |
}
|
| 85 |
return $additions;
|
| 86 |
case 'view':
|
| 87 |
// process the item through cdn2_video theme (which handles different formats and preset types
|
| 88 |
// through sub-element callbacks
|
| 89 |
$field_name = $field['field_name'];
|
| 90 |
if ($node->{$field_name} && $node->{$field_name}[0]['processing']) {
|
| 91 |
$output = t('Your video is still processing. Once it is complete it will be displayed here.');
|
| 92 |
}
|
| 93 |
else {
|
| 94 |
$output = theme('cdn2_video', $node, $field, $node_field, $teaser, $page);
|
| 95 |
}
|
| 96 |
$node_field[0]['view'] = $output;
|
| 97 |
|
| 98 |
$cdn2_default_preset = variable_get('cdn2_default_preset', $node_field[0]['assets'][0]['preset_name']);
|
| 99 |
$js = 'var cdn2_default_preset = \''. $cdn2_default_preset .'\';'."\n";
|
| 100 |
$js .='var aPresetList = new Array();'."\n";
|
| 101 |
for ($i = 0; $i < count($items[0]['assets']); $i++) {
|
| 102 |
$js .= sprintf('aPresetList[\'%s\'] = \'%s\';'."\n", $node_field[0]['assets'][$i]['preset_name'], $i);
|
| 103 |
}
|
| 104 |
drupal_add_js($js, 'inline');
|
| 105 |
drupal_add_css(drupal_get_path('module', 'cdn2') .'/cdn2.css');
|
| 106 |
drupal_add_js(drupal_get_path('module', 'cdn2') .'/js/ui.core.js');
|
| 107 |
drupal_add_js(drupal_get_path('module', 'cdn2') .'/js/ui.tabs.js');
|
| 108 |
drupal_add_js(drupal_get_path('module', 'cdn2') .'/js/cdn2.js', 'footer');
|
| 109 |
$node_field = theme('field', $node, $field, $node_field, $teaser, $page);
|
| 110 |
return $node_field;
|
| 111 |
case 'insert':
|
| 112 |
// reassociate the video back with the node
|
| 113 |
//
|
| 114 |
foreach ($node->{$field['field_name']} as $cdn2_video) {
|
| 115 |
$video_token = $cdn2_video['value'];
|
| 116 |
// update the db records to reassociate the node ids with any videos that have been processed.
|
| 117 |
// this will address a potential race condition vis a vis xmlrpc callback is invoked before the node
|
| 118 |
// has been saved.
|
| 119 |
//
|
| 120 |
|
| 121 |
$query = "UPDATE {cdn2_video_node} SET nid=%d WHERE video_token='%s'";
|
| 122 |
db_query($query, $node->nid, $video_token);
|
| 123 |
|
| 124 |
$query = "UPDATE {cdn2_videos} SET nid=%d where video_token='%s'";
|
| 125 |
db_query($query, $node->nid, $video_token);
|
| 126 |
}
|
| 127 |
|
| 128 |
drupal_set_message('Your video is being processed. It will appear here once it has finished processing. You may '. l('refresh this page', 'node/'. $node->nid) .' to see progress.', 'info');
|
| 129 |
break;
|
| 130 |
}
|
| 131 |
}
|
| 132 |
|
| 133 |
|
| 134 |
/**
|
| 135 |
* Implementation of hook_field_formatter().
|
| 136 |
*/
|
| 137 |
function cdn2_field_formatter($field, $item, $formatter, $node) {
|
| 138 |
if (!isset($item['value'])) {
|
| 139 |
return '';
|
| 140 |
}
|
| 141 |
|
| 142 |
if ($allowed_values = text_allowed_values($field)) {
|
| 143 |
return $allowed_values[$item['value']];
|
| 144 |
}
|
| 145 |
|
| 146 |
switch ($formatter) {
|
| 147 |
case 'plain':
|
| 148 |
$text = strip_tags($item['value']);
|
| 149 |
break;
|
| 150 |
|
| 151 |
case 'trimmed':
|
| 152 |
$text = node_teaser($item['value'], $field['text_processing'] ? $item['format'] : NULL);
|
| 153 |
break;
|
| 154 |
|
| 155 |
case 'default':
|
| 156 |
$text = $item['value'];
|
| 157 |
}
|
| 158 |
|
| 159 |
if ($field['text_processing']) {
|
| 160 |
return check_markup($text, $item['format'], is_null($node) || isset($node->in_preview));
|
| 161 |
}
|
| 162 |
else {
|
| 163 |
return check_plain($text);
|
| 164 |
}
|
| 165 |
}
|
| 166 |
|
| 167 |
|
| 168 |
/**
|
| 169 |
* Implementation of hook_widget().
|
| 170 |
* (shown when the edit/submit forms are called)
|
| 171 |
*/
|
| 172 |
function cdn2_widget($op, &$node, $field, &$node_field) {
|
| 173 |
switch ($op) {
|
| 174 |
case 'form':
|
| 175 |
drupal_add_css(drupal_get_path('module', 'cdn2') .'/cdn2.css');
|
| 176 |
$form = array();
|
| 177 |
$formats = array();
|
| 178 |
|
| 179 |
$enabled_presets = variable_get('cdn2_allowed_presets', array());
|
| 180 |
foreach ($enabled_presets as $preset) {
|
| 181 |
if ($preset && user_access('transcode cdn2 video to '. $preset)) {
|
| 182 |
$formats[] = $preset;
|
| 183 |
}
|
| 184 |
}
|
| 185 |
// refetch the video token for this node if it exists
|
| 186 |
if ($node->nid) {
|
| 187 |
$video_token = db_result(db_query("SELECT video_token from {cdn2_video_node} WHERE nid=%d", $node->nid));
|
| 188 |
}
|
| 189 |
else if (!count($_POST) && !isset($_POST[$field['field_name']])) {
|
| 190 |
$cdn2 = _cdn2_get_soap_client();
|
| 191 |
$video_token = $cdn2->getVideoToken($formats);
|
| 192 |
$node_field[0]['value'] = $video_token;
|
| 193 |
}
|
| 194 |
else {
|
| 195 |
$video_token = $_POST[$field['field_name']][0]['value'];
|
| 196 |
}
|
| 197 |
if (!$video_token && strpos('admin', $_REQUEST['q']) !== 0) {
|
| 198 |
drupal_set_message(t('Unable to contact CDN2 service. Video Uploads are currently disabled.'), 'error');
|
| 199 |
return $form;
|
| 200 |
}
|
| 201 |
|
| 202 |
$form[$field['field_name']] = array('#tree' => TRUE);
|
| 203 |
$form[$field['field_name']][0]['value'] = array(
|
| 204 |
'#type' => 'hidden',
|
| 205 |
'#title' => t($field['widget']['label']),
|
| 206 |
'#default_value' => $video_token,
|
| 207 |
'#required' => $field['required'],
|
| 208 |
'#description' => t($field['widget']['description']),
|
| 209 |
'#maxlength' => $field['max_length'] ? $field['max_length'] : NULL,
|
| 210 |
'#weight' => $field['widget']['weight'],
|
| 211 |
);
|
| 212 |
|
| 213 |
$query = "SELECT is_submitted FROM {cdn2_video_node} WHERE video_token='%s'";
|
| 214 |
$is_submitted = db_result(db_query($query, $video_token));
|
| 215 |
|
| 216 |
$form['cdn2_is_submitted'] = array(
|
| 217 |
'#type' => 'hidden',
|
| 218 |
'#value' => $is_submitted ? 'true' : 'false'
|
| 219 |
);
|
| 220 |
// TODO: Here we need to show the uploader form if there's no video associated with this node, or show the list of assets associated with it and their status otherwise
|
| 221 |
//
|
| 222 |
$show_form = TRUE;
|
| 223 |
if ($node->nid) {
|
| 224 |
$show_form = FALSE;
|
| 225 |
$assets = $node_field[0]['assets'];
|
| 226 |
$asset_output = '';
|
| 227 |
$form[$field['field_name'] .'_fieldset'] = array(
|
| 228 |
'#type' => 'fieldset',
|
| 229 |
'#title' => t('CDN2 Video Assets'),
|
| 230 |
);
|
| 231 |
|
| 232 |
if (count($assets)) {
|
| 233 |
$asset_items = array();
|
| 234 |
$presets = _cdn2_get_available_presets();
|
| 235 |
$source = array();
|
| 236 |
$source['asset_fetch_url'] = 'http://us.cloudfront.cdn2.net/'. $node->{$field['field_name']}[0]['value'];
|
| 237 |
$source_preset = new stdClass();
|
| 238 |
|
| 239 |
$source_preset->friendlyName = 'Original Video';
|
| 240 |
$asset_items[] = theme('cdn2_video_asset_generic', $node, $source, $source_preset);
|
| 241 |
foreach ($assets as $asset) {
|
| 242 |
$asset_items[] = theme('cdn2_video_asset_generic', $node, $asset, $presets[$asset['preset_name']]);
|
| 243 |
}
|
| 244 |
$output = theme('item_list', $asset_items);
|
| 245 |
}
|
| 246 |
else {
|
| 247 |
$output = t('Your video is either still transcoding or no formats were selected.');
|
| 248 |
}
|
| 249 |
$form[$field['field_name'] .'_fieldset'][$field['field_name'] .'_markup'] = array(
|
| 250 |
'#type' => 'markup',
|
| 251 |
'#value' => $output
|
| 252 |
);
|
| 253 |
|
| 254 |
}
|
| 255 |
if ($show_form) {
|
| 256 |
// TODO: handle resizing of the iframe to its child content's scroll height, e.g.:
|
| 257 |
// document.getElementById('cdn2_iframe').height = document.getElementById('cdn2_iframe').contentWindow.document.body.scrollHeight;
|
| 258 |
$form['uploader'] = array(
|
| 259 |
'#type' => 'markup',
|
| 260 |
'#value' => '<iframe id="cdn2_iframe" class="cdn2" height="350" width="100%" frameborder="0" src="'. url('cdn2/uploadform') .'/'. $video_token .'">CDN2 Video requires iframe support</iframe>',
|
| 261 |
);
|
| 262 |
}
|
| 263 |
return $form;
|
| 264 |
case 'validate':
|
| 265 |
if ($node->cdn2_is_submitted == 'false') {
|
| 266 |
form_set_error('uploader', t('You must upload a video to continue.'));
|
| 267 |
}
|
| 268 |
break;
|
| 269 |
case 'process form values':
|
| 270 |
//$node->cdn2 = array('value'=>$node->cdn2);
|
| 271 |
break;
|
| 272 |
}
|
| 273 |
}
|
| 274 |
|