| 1 |
<?php // $Id: movino_node.module,v 1.14 2008/05/04 16:42:16 tomsun Exp $
|
| 2 |
/*
|
| 3 |
Movino Web Frontend
|
| 4 |
Copyright 2006, 2007 Tom Sundström
|
| 5 |
|
| 6 |
This program is free software; you can redistribute it and/or modify
|
| 7 |
it under the terms of the GNU General Public License version 2 as
|
| 8 |
published by the Free Software Foundation.
|
| 9 |
|
| 10 |
This program is distributed in the hope that it will be useful,
|
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
| 13 |
GNU General Public License for more details.
|
| 14 |
|
| 15 |
You should have received a copy of the GNU General Public
|
| 16 |
License along with this program; if not, write to the Free Software
|
| 17 |
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
| 18 |
*/
|
| 19 |
|
| 20 |
|
| 21 |
/**
|
| 22 |
* Implementation of hook_help().
|
| 23 |
*/
|
| 24 |
function movino_node_help($section) {
|
| 25 |
switch ($section) {
|
| 26 |
case 'admin/help#help':
|
| 27 |
// TODO: expand
|
| 28 |
$output = '<p>'. t('Turns Movino content into Drupal nodes. Useful for interactivity such as commenting and voting as well as for creting customized lists with the Views module.') . '</p>';
|
| 29 |
return $output;
|
| 30 |
}
|
| 31 |
}
|
| 32 |
|
| 33 |
|
| 34 |
/*************************** MOVINO HOOKS ******************************/
|
| 35 |
|
| 36 |
|
| 37 |
/**
|
| 38 |
* Implementation of hook_movino_video_prepare().
|
| 39 |
*/
|
| 40 |
function movino_node_movino_video_prepare(&$video) {
|
| 41 |
|
| 42 |
if (movino_node_deleted($video['vid'])) {
|
| 43 |
$video['disabled'] = TRUE;
|
| 44 |
}
|
| 45 |
|
| 46 |
// Add node specific video info.
|
| 47 |
$nid = movino_node_get_video_nid($video['vid']);
|
| 48 |
if (!empty($nid)) {
|
| 49 |
$node = node_load($nid);
|
| 50 |
$video['nid'] = $nid;
|
| 51 |
$video['page'] = 'node/' . $nid;
|
| 52 |
$video['title'] = $node->title;
|
| 53 |
// $video['author'] = theme('username', $node);
|
| 54 |
}
|
| 55 |
|
| 56 |
// Optional: add more actions here.
|
| 57 |
}
|
| 58 |
|
| 59 |
|
| 60 |
/**
|
| 61 |
* Implementation of hook_movino_video_insert().
|
| 62 |
*/
|
| 63 |
function movino_node_movino_video_insert($video) {
|
| 64 |
if (MOVINO_DEBUG) {
|
| 65 |
drupal_set_message('Movino Node: insert node: ' . $video['title'] . ' (' . $video['vid']. ')');
|
| 66 |
}
|
| 67 |
|
| 68 |
// Do not add deleted videos.
|
| 69 |
if (movino_node_deleted($video['vid'])) {
|
| 70 |
return;
|
| 71 |
}
|
| 72 |
|
| 73 |
// Create node.
|
| 74 |
$nid = FALSE;
|
| 75 |
|
| 76 |
$node = (object) NULL;
|
| 77 |
$node->type = 'movino_node';
|
| 78 |
$node->title = $video['title'];
|
| 79 |
$node->body = ' '; // Empty description.
|
| 80 |
$node->teaser = ' '; // Empty teaser.
|
| 81 |
$node->created = $video['created']; // Use create time of the video server.
|
| 82 |
|
| 83 |
// Optional: add more actions here.
|
| 84 |
|
| 85 |
// Code from node_form() that populates the "Publishing options" checkboxes.
|
| 86 |
$node_options = variable_get('node_options_'. $node->type, array('status' => 1, 'promote' => 1, 'comment' => 2));
|
| 87 |
$node_options['status'] = 1; // Always publish nodes.
|
| 88 |
|
| 89 |
// If this is a new node, fill in the default values.
|
| 90 |
foreach (array('status', 'promote', 'sticky') as $nkey) {
|
| 91 |
$node->$nkey = (empty($node_options[$nkey]) ? 0 : 1);
|
| 92 |
}
|
| 93 |
|
| 94 |
// Code from node_submit() that populates the uid field.
|
| 95 |
if (!empty($video['username']) && $account = user_load(array('name' => $video['username']))) {
|
| 96 |
$node->uid = $account->uid;
|
| 97 |
}
|
| 98 |
else {
|
| 99 |
$node->uid = 1;
|
| 100 |
}
|
| 101 |
|
| 102 |
$node->comment = 2;
|
| 103 |
|
| 104 |
node_save($node);
|
| 105 |
_movino_node_set_video_nid($video['vid'], $node->nid);
|
| 106 |
|
| 107 |
if (MOVINO_DEBUG) {
|
| 108 |
drupal_set_message('Movino node saved.');
|
| 109 |
}
|
| 110 |
}
|
| 111 |
|
| 112 |
|
| 113 |
/**
|
| 114 |
* Implementation of hook_movino_video_update().
|
| 115 |
*/
|
| 116 |
function movino_node_movino_video_update($video) {
|
| 117 |
if (MOVINO_DEBUG) {
|
| 118 |
drupal_set_message('Movino Node: activate / update node: ' . $video['title'] . ' (' . $video['vid'] . ')' . print_r($video, TRUE));
|
| 119 |
}
|
| 120 |
|
| 121 |
// Do not udpate deleted videos.
|
| 122 |
if (movino_node_deleted($video['vid'])) {
|
| 123 |
return;
|
| 124 |
}
|
| 125 |
|
| 126 |
$nid = movino_node_get_video_nid($video['vid']);
|
| 127 |
if (empty($nid)) {
|
| 128 |
if (MOVINO_DEBUG) {
|
| 129 |
drupal_set_message('Video nid for ' . $video['vid'] . ' not found.');
|
| 130 |
}
|
| 131 |
// Not found.
|
| 132 |
|
| 133 |
return FALSE;
|
| 134 |
}
|
| 135 |
|
| 136 |
// Update node.
|
| 137 |
|
| 138 |
$node = node_load(array('nid' => $nid));
|
| 139 |
if (empty($node)) {
|
| 140 |
if (MOVINO_DEBUG) {
|
| 141 |
drupal_set_message('Could not load node ' . $nid);
|
| 142 |
}
|
| 143 |
return FALSE;
|
| 144 |
}
|
| 145 |
if (MOVINO_DEBUG) {
|
| 146 |
drupal_set_message('Updating node ' . $nid);
|
| 147 |
}
|
| 148 |
$node->status = 1; // Make sure the node is published.
|
| 149 |
|
| 150 |
// Optional: add more actions here.
|
| 151 |
|
| 152 |
// Save the node to DB.
|
| 153 |
node_save($node);
|
| 154 |
}
|
| 155 |
|
| 156 |
|
| 157 |
/**
|
| 158 |
* Implementation of hook_movino_video_deactivate().
|
| 159 |
*/
|
| 160 |
function movino_node_movino_video_deactivate($video) {
|
| 161 |
if (MOVINO_DEBUG) {
|
| 162 |
drupal_set_message('Movino Node: deactivate node - video vid = ' . $video['vid']);
|
| 163 |
}
|
| 164 |
$nid = movino_node_get_video_nid($video['vid']);
|
| 165 |
if (!empty($nid)) {
|
| 166 |
$node = node_load($nid);
|
| 167 |
$node->status = 0; // Make sure the node gets unpublished.
|
| 168 |
|
| 169 |
// Optional: add more actions here.
|
| 170 |
|
| 171 |
// Save the node to DB.
|
| 172 |
node_save($node);
|
| 173 |
}
|
| 174 |
}
|
| 175 |
|
| 176 |
|
| 177 |
/*************************** DRUPAL NODE HOOKS ******************************/
|
| 178 |
|
| 179 |
|
| 180 |
/**
|
| 181 |
* Implementation of hook_node_info().
|
| 182 |
*/
|
| 183 |
function movino_node_node_info() {
|
| 184 |
return array(
|
| 185 |
'movino_node' => array(
|
| 186 |
'name' => t('Movino content'),
|
| 187 |
'module' => 'movino_node',
|
| 188 |
'description' => t("Movino content"),
|
| 189 |
)
|
| 190 |
);
|
| 191 |
}
|
| 192 |
|
| 193 |
|
| 194 |
/**
|
| 195 |
* Implementation of hook_access().
|
| 196 |
*/
|
| 197 |
function movino_node_access($op, $node) {
|
| 198 |
|
| 199 |
global $user;
|
| 200 |
|
| 201 |
if ($user->uid == 1) {
|
| 202 |
return TRUE;
|
| 203 |
}
|
| 204 |
|
| 205 |
switch ($op) {
|
| 206 |
case 'create':
|
| 207 |
// Never allow users to create Movino nodes.
|
| 208 |
// All nodes should be automatically created based on video server feed info.
|
| 209 |
return FALSE;
|
| 210 |
|
| 211 |
case 'update':
|
| 212 |
if (user_access('edit own Movino content') && ($user->uid == $node->uid)) {
|
| 213 |
return TRUE;
|
| 214 |
}
|
| 215 |
return FALSE;
|
| 216 |
|
| 217 |
case 'delete':
|
| 218 |
// Never allow users to delete Movino nodes, as they would be re-generated automatically
|
| 219 |
return FALSE;
|
| 220 |
|
| 221 |
case 'view':
|
| 222 |
if (movino_node_is_live($node->nid)) {
|
| 223 |
if (user_access('view live Movino content')) {
|
| 224 |
return TRUE;
|
| 225 |
}
|
| 226 |
} elseif (user_access('view archived Movino content')) {
|
| 227 |
return TRUE;
|
| 228 |
}
|
| 229 |
return FALSE;
|
| 230 |
}
|
| 231 |
|
| 232 |
return FALSE;
|
| 233 |
}
|
| 234 |
|
| 235 |
|
| 236 |
/**
|
| 237 |
* Implementation of hook_form().
|
| 238 |
*/
|
| 239 |
function movino_node_form(&$node) {
|
| 240 |
$type = node_get_types('type', $node);
|
| 241 |
|
| 242 |
$video = movino_node_get_video($node->nid);
|
| 243 |
$content_info = theme('movino_content', $video, FALSE);
|
| 244 |
|
| 245 |
if (!empty($content_info)) {
|
| 246 |
$form['movino_content_info'] = array(
|
| 247 |
'#type' => 'item',
|
| 248 |
'#title' => t('Movino content'),
|
| 249 |
'#value' => $content_info . '<div style="clear: both; "></div>', // TODO: remove <div> at some point.
|
| 250 |
'#weight' => -6,
|
| 251 |
);
|
| 252 |
}
|
| 253 |
|
| 254 |
$form['title'] = array(
|
| 255 |
'#type'=> 'textfield',
|
| 256 |
'#title' => check_plain($type->title_label),
|
| 257 |
'#default_value' => $node->title,
|
| 258 |
'#description' => t('Tip: You can set the title from your phone before starting the broadcast, by going to <em>Options » Settings » Title</em> in the phone app.'),
|
| 259 |
'#required' => TRUE,
|
| 260 |
'#weight' => -5,
|
| 261 |
);
|
| 262 |
|
| 263 |
/*
|
| 264 |
$form['body'] = array(
|
| 265 |
'#type' => 'textarea',
|
| 266 |
'#title' => check_plain($type->body_label),
|
| 267 |
'#rows' => 20,
|
| 268 |
'#default_value' => $node->body,
|
| 269 |
'#required' => TRUE,
|
| 270 |
);
|
| 271 |
*/
|
| 272 |
|
| 273 |
return $form;
|
| 274 |
}
|
| 275 |
|
| 276 |
|
| 277 |
/**
|
| 278 |
* Implementation of hook_delete().
|
| 279 |
*/
|
| 280 |
function movino_node_delete(&$node) {
|
| 281 |
if ($video = movino_node_get_video($node->nid)) {
|
| 282 |
// Mark as deleted
|
| 283 |
db_query('UPDATE {movino_node_mapping} SET deleted = 1 WHERE nid = %d', $node->nid);
|
| 284 |
movino_remove_video($video['vid']);
|
| 285 |
}
|
| 286 |
}
|
| 287 |
|
| 288 |
|
| 289 |
/**
|
| 290 |
* Implementation of hook_update();
|
| 291 |
*/
|
| 292 |
function movino_node_update($node) {
|
| 293 |
if ($video = movino_node_get_video($node->nid)) {
|
| 294 |
|
| 295 |
if ($node->status) {
|
| 296 |
movino_republish_video($video['vid']);
|
| 297 |
} else {
|
| 298 |
movino_unpublish_video($video['vid']);
|
| 299 |
}
|
| 300 |
}
|
| 301 |
}
|
| 302 |
|
| 303 |
|
| 304 |
/**
|
| 305 |
* Implementation of hook_view().
|
| 306 |
*/
|
| 307 |
function movino_node_view(&$node, $teaser = FALSE, $page = FALSE) {
|
| 308 |
|
| 309 |
if (movino_node_is_live($node->nid)) {
|
| 310 |
if (!user_access('view live Movino content')) {
|
| 311 |
return $node;
|
| 312 |
}
|
| 313 |
} elseif (!user_access('view archived Movino content')) {
|
| 314 |
return $node;
|
| 315 |
}
|
| 316 |
|
| 317 |
$video = movino_node_get_video($node->nid);
|
| 318 |
|
| 319 |
if (!empty($video)) {
|
| 320 |
|
| 321 |
if ($page) {
|
| 322 |
$node->content['movino'] = array(
|
| 323 |
'#value' => theme('movino_content', $video, TRUE),
|
| 324 |
'#weight' => -5,
|
| 325 |
);
|
| 326 |
}
|
| 327 |
if($teaser) {
|
| 328 |
$node->content['movino'] = array(
|
| 329 |
'#value' => theme('movino_content', $video, FALSE),
|
| 330 |
'#weight' => -5,
|
| 331 |
);
|
| 332 |
}
|
| 333 |
}
|
| 334 |
|
| 335 |
return $node;
|
| 336 |
}
|
| 337 |
|
| 338 |
|
| 339 |
/*************************** DATABASE FUNCTIONS ******************************/
|
| 340 |
|
| 341 |
|
| 342 |
/**
|
| 343 |
* Checks whether the given node's video is live or in the archive.
|
| 344 |
*/
|
| 345 |
function movino_node_is_live($nid) {
|
| 346 |
$result = db_query("SELECT nid FROM {movino_node_mapping} n LEFT JOIN {movino_videos} v ON n.vid = v.vid WHERE v.type = 'live' AND n.deleted != 1 AND n.nid = %d", $nid);
|
| 347 |
if ($node = db_fetch_array($result)) {
|
| 348 |
return TRUE;
|
| 349 |
}
|
| 350 |
return FALSE;
|
| 351 |
|
| 352 |
}
|
| 353 |
|
| 354 |
|
| 355 |
/**
|
| 356 |
* Returns true if a mapping between the video and a node has been set up,
|
| 357 |
* Returns true even after the node is deleted, so that it's possible to
|
| 358 |
* delete videos from the site without deleting them from the server.
|
| 359 |
*/
|
| 360 |
function movino_node_deleted($vid) {
|
| 361 |
|
| 362 |
$result = db_query("SELECT nid FROM {movino_node_mapping} WHERE deleted = 1 AND vid = %d ", $vid);
|
| 363 |
if ($node = db_fetch_array($result)) {
|
| 364 |
return TRUE;
|
| 365 |
}
|
| 366 |
return FALSE;
|
| 367 |
}
|
| 368 |
|
| 369 |
|
| 370 |
/**
|
| 371 |
* Returns the movino_node nid for a video.
|
| 372 |
*/
|
| 373 |
function movino_node_get_video_nid($vid) {
|
| 374 |
|
| 375 |
$result = db_query("SELECT nid FROM {movino_node_mapping} WHERE deleted != 1 AND vid = %d ", $vid);
|
| 376 |
if ($node = db_fetch_array($result)) {
|
| 377 |
return $node['nid'];
|
| 378 |
}
|
| 379 |
return FALSE;
|
| 380 |
}
|
| 381 |
|
| 382 |
|
| 383 |
/**
|
| 384 |
* Returns the video metadata given a node id.
|
| 385 |
*/
|
| 386 |
function movino_node_get_video($nid) {
|
| 387 |
$nodemapping = db_fetch_array(db_query('SELECT v.id, v.server FROM {movino_videos} v LEFT JOIN {movino_node_mapping} n ON n.vid = v.vid WHERE n.deleted != 1 AND n.nid = %d ', $nid));
|
| 388 |
|
| 389 |
if (empty($nodemapping['id']) || empty($nodemapping['server'])) {
|
| 390 |
return FALSE;
|
| 391 |
}
|
| 392 |
$video = movino_get_video($nodemapping['server'], $nodemapping['id'], TRUE, TRUE);
|
| 393 |
if (empty($video)) {
|
| 394 |
return FALSE;
|
| 395 |
}
|
| 396 |
return $video;
|
| 397 |
}
|
| 398 |
|
| 399 |
|
| 400 |
/**
|
| 401 |
* Maps a movino_node to the movino_video table.
|
| 402 |
*/
|
| 403 |
function _movino_node_set_video_nid($vid, $nid) {
|
| 404 |
|
| 405 |
$result = db_query("INSERT INTO {movino_node_mapping} SET nid = %d, vid = %d ", $nid, $vid);
|
| 406 |
|
| 407 |
}
|