/[drupal]/contributions/modules/video_upload/video_upload.admin.inc
ViewVC logotype

Contents of /contributions/modules/video_upload/video_upload.admin.inc

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


Revision 1.13 - (show annotations) (download) (as text)
Sun Oct 11 22:06:52 2009 UTC (6 weeks, 4 days ago) by jhedstrom
Branch: MAIN
CVS Tags: HEAD
Branch point for: DRUPAL-7--1
Changes since 1.12: +1 -1 lines
File MIME type: text/x-php
Initital 7.x port.
1 <?php
2
3 // $Id: video_upload.admin.inc,v 1.5.2.6 2009/09/04 20:51:16 jhedstrom Exp $
4
5 /**
6 * @file video_upload.admin.inc
7 */
8
9 /**
10 * Obtain a list of all fields using the video upload module.
11 */
12 function _video_upload_relevant_fields() {
13 $fields = content_fields();
14 $video_upload_fields = array();
15 foreach ($fields as $field => $config) {
16 if (($config['type'] == 'video_upload') && ($config['widget']['type'] == 'video_upload_widget')) {
17 $video_upload_fields[$field] = $config;
18 $video_upload_fields[$field]['database_information'] = content_database_info($config);
19 }
20 }
21
22 return $video_upload_fields;
23 }
24
25 /**
26 * Callback function for admin/settings/video-upload
27 * @return
28 * Returns an array defining the admin settings form
29 */
30 function _video_upload_admin_settings_form() {
31 // @todo: Abstract out YouTube.
32 $form['youtube'] = array(
33 '#type' => 'fieldset',
34 '#title' => t('YouTube API Settings'),
35 );
36 $form['youtube']['video_upload_youtube_developer_key'] = array(
37 '#type' => 'textfield',
38 '#title' => t('Developer Key'),
39 '#description' => t('A YouTube <a href="!url">Developer Key</a> is required', array('!url' => url('http://code.google.com/apis/youtube/dashboard/'))),
40 '#default_value' => variable_get('video_upload_youtube_developer_key', FALSE),
41 '#required' => TRUE,
42 );
43 $form['youtube']['video_upload_youtube_username'] = array(
44 '#type' => 'textfield',
45 '#title' => t('YouTube Username'),
46 '#default_value' => variable_get('video_upload_youtube_username', FALSE),
47 '#required' => TRUE,
48 );
49 $form['youtube']['video_upload_youtube_password'] = array(
50 '#type' => 'textfield',
51 '#title' => t('YouTube Account Password'),
52 '#default_value' => variable_get('video_upload_youtube_password', FALSE),
53 '#required' => TRUE,
54 );
55 return system_settings_form($form);
56 }
57
58 /**
59 * Menu callback: administer video
60 */
61 function video_upload_admin_video() {
62 if ($_POST['operation'] == 'delete' && $_POST['videos']) {
63 return drupal_get_form('video_upload_admin_video_delete_confirm');
64 }
65 $output = drupal_get_form('video_upload_admin_video_form');
66 return $output;
67 }
68
69 /**
70 * Video deletion confirmation
71 */
72 function video_upload_admin_video_delete_confirm() {
73 $edit = $_POST;
74
75 $form['videos'] = array(
76 '#prefix' => '<ul>',
77 '#suffix' => '</ul>',
78 '#tree' => TRUE
79 );
80
81 // array_filter returns only elements with TRUE values
82 foreach (array_filter($edit['videos']) as $vid => $value) {
83 $form['videos'][$vid] = array(
84 '#type' => 'hidden',
85 '#value' => 'delete',
86 '#prefix' => '<li>',
87 '#suffix' => check_plain($edit['vtitle-' . $vid]) . '</li>' . "\n",
88 );
89 }
90 $form['operation'] = array('#type' => 'hidden', '#value' => 'delete');
91
92 return confirm_form(
93 $form,
94 t('Are you sure you want to delete these videos?'),
95 'admin/content/video-upload', t('This action cannot be undone.'),
96 t('Delete all'), t('Cancel')
97 );
98 }
99
100 /**
101 * Delete videos after confirmation.
102 */
103 function video_upload_admin_video_delete_confirm_submit($form_id, $form_values) {
104 if ($form_values['confirm']) {
105 // @todo: Abstract out YouTube.
106 $connection = video_upload_connect(TRUE);
107 foreach ($form_values['videos'] as $vid => $value) {
108 $video = _video_upload_get_video_object_by_id($vid, $connection);
109 if ($video) {
110 _video_upload_delete_video_remote($connection, $video);
111 drupal_set_message(t('Video %id has been removed from YouTube', array('%id' => $vid)));
112 }
113 }
114 drupal_set_message(t('The videos have been deleted'));
115 }
116
117 return 'admin/content/video-upload';
118 }
119
120 /**
121 * Process video admin form
122 */
123 function video_upload_admin_video_form_submit($form_id, $form_values) {
124 }
125
126 /**
127 * Video administration form
128
129 * @todo Replace these queries with calls to the cck api, or similar, since
130 * they currently break once a field has been reused in another type.
131 */
132 function video_upload_admin_video_form() {
133 // @todo: Abstract out YouTube.
134 // Get YouTube connection.
135 $connection = video_upload_connect(TRUE);
136
137 // Get feed of all videos.
138 $feed = _video_upload_gdata_get_feed(VIDEO_UPLOAD_YOUTUBE_DEFAULT_USER_FEED, $connection);
139
140 // Get list of all videos.
141 $all_videos = array();
142 $fields = _video_upload_relevant_fields();
143 foreach ($fields as $field => $config) {
144 $db_info = $config['database_information'];
145 $where = array();
146 $video_id_field = $db_info['columns']['video_id']['column'];
147 $result = db_query("SELECT %s AS id, nid FROM {" . $db_info['table'] . "}", $video_id_field);
148 while ($id = db_fetch_object($result)) {
149 // add field_name
150 $id->field_name = $field;
151 $id->id_field_name = $video_id_field;
152 $all_videos[$id->id] = $id;
153 }
154 }
155
156 $video_ids = array_keys($all_videos);
157
158 // the big feed list will be a combination of gdata objects and node objects
159 // (in the event of orphaned nodes)
160 $big_feed_list = $video_ids_youtube = array();
161
162 // now, with a list of ids, loop through the feed looking for
163 // isolated/stranded videos that have a developer tag set by one of our
164 // fields
165 foreach ($feed as $video) {
166 $id = $video->getVideoId();
167
168 // add id to the youtube ids (this is to find orphaned nodes later on)
169 $video_ids_youtube[] = $id;
170
171 $big_feed_list[] = $video;
172 }
173
174 // now, loop through drupal videos to find video ids not on youtube
175 foreach ($all_videos as $id => $video) {
176 if (!in_array($id, $video_ids_youtube)) {
177 $node = node_load(array('nid' => $video->nid));
178 $video->title = $node->title;
179 $video->link = l($node->title, 'node/' . $node->nid);
180 $big_feed_list[] = $video;
181 }
182 }
183
184 // loop through combined list of feed video and orphaned video
185 foreach ($big_feed_list as $video) {
186 // @todo abstract out youtube
187 if (!_video_upload_is_zend_object($video)) {
188 // Due to the strict OO nature of the Zend client library, we must use
189 // this function instead of a simple boolean check
190 $id = $video->ytid;
191 }
192 else {
193 $id = $video->getVideoId();
194
195 // set video title
196 $video->title = check_plain($video->getVideoTitle());
197 // set link
198 $video->link = l($id, video_upload_link_youtube($id));
199 }
200
201 $form['id'][$id] = array(
202 '#value' => $video->link,
203 );
204
205 $form['title'][$id] = array(
206 '#value' => $video->title,
207 );
208
209 $form['vtitle'][$id] = array(
210 '#name' => 'vtitle-' . $id,
211 '#type' => 'hidden',
212 '#value' => $video->title,
213 );
214
215 $node = FALSE;
216 if (in_array($id, $video_ids)) {
217 // this video is associated with a node, so we can fetch the node to
218 // determine the status
219 $nid = $all_videos[$id]->nid;
220
221 $node = node_load(array('nid' => $nid));
222
223 $form['node'][$id] = array('#value' => l($nid, 'node/' . $nid));
224
225 $field_name = $all_videos[$id]->field_name;
226 $form['field_name'][$id] = array('#value' => l($field_name, 'admin/content/node-type/' . $node->type . '/fields'));
227 }
228 else {
229 // Orphaned video, so we can operate on it. Videos associated with nodes
230 // must be operated on through the node system.
231 $videos[$id] = '';
232
233 $form['node'][$key] = FALSE;
234 $form['field_name'][$key] = FALSE;
235 }
236
237 // if the node has the status, display that
238 $status = VIDEO_UPLOAD_STATUS_UNKNOWN;
239 if ($node) {
240 $status = $id ? $node->{$all_videos[$id]->field_name}[0]['status'] : VIDEO_UPLOAD_STATUS_UPLOAD_PENDING;
241 }
242
243 if(_video_upload_is_zend_object($video) && $status < VIDEO_UPLOAD_STATUS_OK) {
244 // find status from YouTube since it either hasn't been checked,
245 // or this is an orphaned node
246 $status = _video_upload_youtube_get_status_by_id($id, $connection);
247 }
248 elseif ($status != VIDEO_UPLOAD_STATUS_UPLOAD_PENDING && !_video_upload_is_zend_object($video)) {
249 $status = VIDEO_UPLOAD_STATUS_ORPHANED;
250 }
251
252
253 $status_text = theme('video_upload_status_text', $status);
254 $form['status'][$id] = array('#value' => $status_text);
255 }
256
257 // this constructs the checkboxes, and in fact, the only interactivity
258 // currently available. If there aren't any orphaned videos, there's nothing
259 // to do.
260 if (!empty($videos)) {
261 $form['videos'] = array(
262 '#type' => 'checkboxes',
263 '#options' => $videos,
264 );
265
266 $form['options'] = array(
267 '#type' => 'fieldset',
268 '#title' => t('Video management options'),
269 '#prefix' => '<div class="container-inline">',
270 '#suffix' => '</div>',
271 );
272 $options = array('delete' => t('Delete selected videos'));
273 $form['options']['operation'] = array(
274 '#type' => 'select',
275 '#options' => $options,
276 '#default_value' => 'approve');
277 $form['options']['submit'] = array(
278 '#type' => 'submit',
279 '#value' => t('Submit'));
280 }
281
282 $form['pager'] = array('#value' => theme('pager', NULL, 50, 0));
283
284 return $form;
285 }
286
287 /**
288 * @defgroup "Video Upload video processing functions."
289 * @{
290 */
291
292 /**
293 * Upload all queued videos to the provider.
294 *
295 * @param array $fields
296 * CCK field definitions.
297 *
298 * @return integer
299 * Number of videos uploaded.
300 */
301 function _video_upload_upload_all($fields) {
302 $uploaded = 0;
303 foreach ($fields as $field => $config) {
304 $result = _video_upload_query_videos($config, VIDEO_UPLOAD_STATUS_UPLOAD_PENDING);
305 while ($video = db_fetch_object($result)) {
306 $video->field = $config;
307 if (video_upload_upload($config, $video)) {
308 $uploaded ++;
309 }
310 }
311 }
312 return $uploaded;
313 }
314
315 /**
316 * Cycle through all records in a table with status of 0 and attempt
317 * to verify them on YouTube. Once verified, also attempt to update
318 * title/tags on YouTube.
319 *
320 * @param $fields
321 * CCK configuration for video upload fields.
322 */
323 function _video_upload_verify_all($fields) {
324 // @TODO Abstract out youtube.
325 $video_ids = $videos = array();
326 foreach ($fields as $field => $config) {
327 $result = _video_upload_query_videos($config, VIDEO_UPLOAD_STATUS_UNKNOWN);
328 while ($video = db_fetch_object($result)) {
329 $video->field = $config;
330 $video_ids[] = $video->video_id;
331 $videos[$video->video_id] = $video;
332 }
333 }
334
335 // Establish provider connection.
336 $connection = video_upload_connect(TRUE);
337
338 // Get a feed of all the user's videos (!)
339 // @fixme
340 // Unfortunately, this is currently the only way to check on
341 // videos that are not yet published. A much more efficient and
342 // sane method would be to only look up the videos on which we
343 // don't have a status.
344 $feed = _video_upload_gdata_get_feed(VIDEO_UPLOAD_YOUTUBE_DEFAULT_USER_FEED, $connection);
345
346 if ($feed) {
347 foreach ($feed as $video) {
348 if (!in_array($video->getVideoId(), $video_ids)) {
349 // This isn't in the list of videos we currently care to check.
350 continue;
351 }
352
353 $status = _video_upload_gdata_get_video_status($video);
354
355 if ($status->status === FALSE) {
356 // The video is bad, the node should be deleted, or unpublished, or
357 // perhaps the user should be notified Setting status to -1 queues
358 // the video for deletion.
359 $local_video = $videos[$video->getVideoId()];
360 $local_video->video_status = VIDEO_UPLOAD_STATUS_BAD;
361 _video_upload_update_video($local_video);
362 }
363 elseif ($status->status === TRUE) {
364 // The video is good, update the table.
365 $local_video = $videos[$video->getVideoId()];
366 $node = node_load($local_video->nid);
367 // Update YouTube if applicable.
368 _video_upload_update_video_remote($connection, $video, $local_video, $node);
369
370 // The video is good, update the table.
371 _video_upload_update_video($local_video);
372
373 // @TODO Notify the author, or expose this to trigger/action functionality.
374 }
375 else {
376 // Nothing found on the video, wait until next time.
377 }
378 }
379 }
380 }
381
382 /**
383 * Cycle through all records in a table with status of 1 and update
384 * info on YouTube.
385 * @param $fields
386 * CCK configuration for video upload fields.
387 */
388 function _video_upload_update_all_videos($fields) {
389 // @todo Abstract out YouTube.
390 $video_ids = $videos = array();
391 foreach ($fields as $field => $config) {
392 $result = _video_upload_query_videos($config, VIDEO_UPLOAD_STATUS_OK);
393
394 while ($video = db_fetch_object($result)) {
395 $video->field = $config;
396 $video_ids[] = $video->video_id;
397 $videos[$video->video_id] = $video;
398 }
399 }
400
401 // Establish provider connection.
402 $connection = _video_upload_youtube(TRUE);
403
404 // Get a feed of all the user's videos (!).
405 // @fixme unfortunately, this is currently the only way to check on videos
406 // that are not yet published. A much more efficient and sane
407 // method would be to only look up the videos on which we don't
408 // have a status
409 $feed = _video_upload_gdata_get_feed(VIDEO_UPLOAD_YOUTUBE_DEFAULT_USER_FEED, $connection);
410
411 foreach ($feed as $video) {
412 if (!in_array($video->getVideoId(), $video_ids)) {
413 continue;
414 }
415 // Load the node in question.
416 $local_video = $videos[$video->getVideoId()];
417 $node = node_load(array('nid' => $local_video->nid));
418 _video_upload_update_video_remote($connection, $video, $local_video, $node);
419
420 // The video may have been updated, thus the status may have changed.
421 _video_upload_node_set_video_status($local_video);
422 }
423
424 }
425
426 /**
427 * Cycle through all records in a table with status of -1 and delete
428 * them if the field is configured to do so.
429 *
430 * @param array $fields
431 * CCK configuration for video upload fields.
432 */
433 function _video_upload_delete_rejected_videos($fields) {
434 // @todo Abstract out YouTube.
435 $video_ids = $videos = array();
436 foreach ($fields as $field => $config) {
437 if (!$config['widget']['auto_delete_rejected_videos'] && !$config['widget']['remove_deleted_videos']) {
438 continue;
439 }
440 $result = _video_upload_query_videos($config, VIDEO_UPLOAD_STATUS_DELETE);
441 while ($video = db_fetch_object($result)) {
442 $video_ids[] = $video->video_id;
443 $videos[$video->video_id] = $video;
444 }
445 }
446
447 // Establish provider connection.
448 $connection = video_upload_connect(TRUE);
449
450 // Get a feed of all the user's videos (!).
451 // @FIXME
452 // Unfortunately, this is currently the only way to check on videos that
453 // are not yet published. A much more efficient and sane method would be
454 // to only look up the videos on which we don't have a status.
455 $feed = _video_upload_gdata_get_feed(VIDEO_UPLOAD_YOUTUBE_DEFAULT_USER_FEED, $connection);
456
457 foreach ($feed as $video) {
458 if (!in_array($video->getVideoId(), $video_ids)) {
459 continue;
460 }
461
462 // Delete the video from the provider.
463 _video_upload_delete_video_remote($connection, $video);
464
465 // Remove record from local table.
466 $local_video = $videos[$video->getVideoId()];
467 video_upload_delete_local($local_video);
468 }
469 }
470
471 /**
472 * @} End defgroup "Video Upload video processing functions."
473 */
474
475 /**
476 * Query to find all videos for a given field.
477 *
478 * @param array $config
479 * CCK field definition array.
480 *
481 * @param string $video_status
482 * Video status
483 */
484 function _video_upload_query_videos($config, $status) {
485 $db_info = $config['database_information'];
486 $params[':video_id'] = $db_info['columns']['video_id']['column'];
487 $params[':filefield'] = $db_info['columns']['fid']['column'];
488 $params[':video_status_field'] = $db_info['columns']['video_status']['column'];
489 $params[':video_status_field_2'] = $db_info['columns']['video_status']['column'];
490 $params[':video_status'] = $status;
491 $params[':filefield_2'] = $db_info['columns']['fid']['column'];
492 $multiple = $config['multiple'] ? ', delta' : '';
493 return db_query("SELECT %s AS video_id, nid, %s AS fid, %s AS video_status" . $multiple . " FROM {" . $db_info['table'] . "} t WHERE %s = '%s' AND %s IS NOT NULL", $params);
494 }
495

  ViewVC Help
Powered by ViewVC 1.1.2