media.module 12.3 KB
Newer Older
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 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
<?php

// $Id$


/* ***************************************** */
/* DRUPAL API FUNCTIONS                      */
/* ***************************************** */


/**
 * Implementation of hook_menu.
 * 
 */
function media_menu() {
  // ajax formater 
  $items['media/ajax'] = array(
    'title' => 'media Ajax',
    'page callback' => 'media_ajax_formater_load',
    'access arguments' => array('access content'),
    'file' => 'media_ajax.inc',
  );
  return $items;
}


/**
 * Implementation of hook_form_alter
 * @param $form
 * @param $form_state
 * @param $form_id
 * 
 */
function media_form_alter (&$form, $form_state, $form_id) {
  global $user;
  
  // $check_js is wether or not the client has JS
  

  // get the formaters for specified type 
  
  //print_r(media_registration_item_formaters(null, media_registration_kinds('formater')));
  
  $form['test'] = media_get_user_file_options('', '', $user->uid);
 
  $form['test']['#weight'] = -9;
  
  // we only handle media stuff on the node add/edit form
  if (strstr($form_id, 'node_form') ) {
    
    // need to control display here
  
    
    // collect all the form elements we need to hide

    // fire hook
    
    // put these elements into JS
    //drupal_add_js($elements, 'inline');
    
    // load the media JS file
    //drupal_add_js(drupal_get_path('module', 'media') .'/media.js');
    
    // do we need to add a field to store data in?
    
  }
  
}


/**
 * Register theming functions
 * @return array
 */
function media_theme() {
  return array(
    'media_no_tabs_display' => array(
      'file' => 'media_theme.inc',
      'arguments' => array(
        'tabs' => NULL,
      ),
    ),
  );
}


/* ***************************************** */
/* media hook calls                          */
/* ***************************************** */


/**
 * Gets all of the modules which register with ReMiG
 */
function media_get_registered_modules() {
  static $registrations;
  if (! $registrations) {
    $registrations = array();
    // get all the modules which implement 
    foreach (module_implements('media_register') as $module) {
      $function = $module .'_media_register';
      // get all the registrations
      $registration = $function();
      // add the module name to each registration
      foreach (array_keys($function()) as $key) {
        $registration[$key]['module'] = $module;
      }
      $registrations = array_merge($registrations, $registration); 
    }
  }
  return $registrations;  
}


/**
 * Get all module data that uses the "media_user_files_select" hook
 *
 * @param string $node_type
 * @param string $field
 * @param uid $uid
 * @return array
 *   drupal form array
 */
function media_get_user_file_options($node_type, $field, $uid) {
  $items = array();
  $form = array();
  // get all the modules which implement file select options
  foreach (module_implements('media_user_files_select') as $module) {
    $function = $module .'_media_user_files_select';
    $items = array_merge($items, $function($node_type, $field, $uid));
  }
  // now we order modules into tabs structure
  // @TODO implement admin weighting here some how
  foreach ($items as $id => $item) {
    $name = key($item);
    // now create the sub element (drawer)
    $my_files_elements[$id][$name] = array(
      '#type' => 'markup',
      '#value' => '<div class="media drawer">'. $name .'</div>',
      '#attributes' => array('class' => 'media drawer'),
      'content' => $item[$name]['form'],
    );
  }    
  
  return media_tabs_form($my_files_elements);  
}


/**
 * This handles all the admin hooks that do not
 * require the passing of a file object. This does not use
 * module_invoke_all() so that the array of data can be 
 * keyed by the module name for ease of usage later
 * @param string $hook
 *  name of the hook being called
 * @return array
 *  array of data from each module that implements the hook
 */
function media_admin_extend($hook) {
  $data = array();
  // get a list of the modules that implement this hook
  foreach (module_implements($hook) as $module) {
    // add the data keyed by module name
    $data[$module] = module_invoke($module, $hook);
  }
  if (count($data)) {return $data; }
}


/**
 * This allows admins to to configure specific settings
 * per module for media.
 * @return array
 *   drupal form array
 */
function media_admin_module_options_settings_form() {
  if (! $options = media_admin_extend('media_admin_options_settings')) {
    drupal_set_message(t('Sorry, there are no modules which have configurable settings.'));
    return;
  }
  // build a fieldset per module
  foreach ($options as $module_name => $option) {
    $form[$module_name] = array(
      '#type' => 'fieldset',
      '#title' => $module_name
    );
    $form[$module_name][] = $options;
  }
  return $form;
}


/**
 * This takes all of the modules that implement the media permissions 
 * hook, gets them on a page, and adds a roll setting to control access
 * @return array
 *  drupal form array
 */
function media_admin_module_options_permissions_form() {
  // get any cached permissions for the permisisons 
  $permissions = unserialize(variable_get('media_admin_permissions', array()));
  if (! $options = media_admin_extend('media_admin_permissions_settings', $permissions)) {
    drupal_set_message(t('Sorry, there are no modules which have configurable permissions settings.'));
    return;
  }
  
  // build a fieldset per module
  foreach ($options as $module_name => $option) {
    $form[$module_name] = array(
      '#type' => 'fieldset',
      '#title' => $module_name
    );
    
    // create the roll form item
    $append = array(
      '#type' => 'select',
      '#title' => t('Permissions'),
      '#multiple' => true,
    );
    // now we need to itterate through the incoming form and append 
    // the roll setting to each item
    $form[$module_name] = _media_admin_form_perm_append($option, $append, $permissions['module']);
    
    $form[$module_name][] = $options;
  }
}


/**
 * This is a helper function to add in a permissions element 
 * for each of the options that are access
 * @param array $form
 *   the drupal form array that will have items appended to it
 * @param array $append
 *   the form element that will be appended to the form
 * @param array $permissions
 *   permissions saved for this module
 * @return array
 *   drupal form array
 */
function _media_admin_form_perm_append($form, $append, $permissions) {
  foreach ($form as $type => $element) {
    if ($type = '#title') {
      $form[$element .'_perm'] = $append;
      $form[$element .'_perm']['#default_value'] = $permissions[$element .'_perm'];
    }
    elseif (is_array($element)) {
      $form[$type][] = _media_admin_form_perm_append($element, $append, $permissions);
    }
  }
  return $form;
}


/* ***************************************** */
/* media Internal Functions                  */
/* ***************************************** */

/**
 * Parsing function for the registrations to hand back the kinds 
 * of modules registering. Used to select all formaters, containers, etc
 * @param string $kinds
 *   return all the matching registrations of this kind
 * @return array
 */
function media_registration_kinds($kind = null) {
  $kinds = array();
  // get the registered modules
  $registrations = media_get_registered_modules();
  // parse the registrations
  foreach ($registrations as $id => $registration) {
    if ($kind) {
      // get the kind that is being looked for
      if ($registration['kind'][$kind]) {
        $kinds[$id] = $registration;
      } 
    }
    else {
       $kinds[$id] = $registration;
    }
  }
  return $kinds;
}


/**
 * Returns a set of formaters which can format the specified 
 * item. If $description is null, all formaters will be returned. If 
 * a set of registered modules can be passed in to narrow the
 * formater options. 
 * @param string $description
 *   file extension to return
 * @param unknown_type $registrations
 * @return array
 */
function media_registration_item_formaters($description = '*', $registrations = null) {
  $formaters = array();
  // get all the registrations if we don't have any
  if (! $registrations) {
    $registrations = media_get_registered_modules();
  }
  // itterate through each of the registered modules and find the formaters
  foreach ($registrations as  $id => $registration) {
    // look for the formater, or just take all
    if ((is_array($registration['kind']['formater']['types']) && in_array($registration['kind']['formater']['types'], $description))
      || $description == '*' 
      || $registration['kind']['formater']['types'] == '*') {
      $formaters[$registration['module']] = $id;
    }
  }
  return $formaters;
}


/**
 * parsing function for the registrations to hand back the kinds 
 * of modules registering
 * @param string $type
 *   only hand back data for the specified type
 * @return array
 */
function media_registration_types($type = null) {
  // get the registered modules
  $registrations = media_get_registered_modules();
  // parse the registrations
  foreach ($registrations as $registration) {
    
  } 
}



/**
 * parsing function for the registrations to hand back the kinds 
 * of modules registering
 * @param string $array
 *   name of the element we want to get data from
 * @return array
 */
function media_registration_data($name) {
  $data = array();
  // get the registered modules
  $registrations = media_get_registered_modules();
  // parse the registrations
  foreach ($registrations as $registration) {
    // get the item that was requested
    $data = array_merge($data, $registration[$name]);
  }
  return $data;
}


/**
 * Build the file browser / uploader 
 */
function media_form () {
  global $user;
  $files = media_get_user_files($user->uid);
  $form['media'] = array(
    '#type' => 'tabset',
    array(
      media_display_user_files_form($files),
      media_display_upload_form(),
    ),    
  );
  return $form;
}

/* *************************************************** */
/* media forms                                         */
/* *************************************************** */

/**
 * Display files in a form element 
 * @param array $files
 *   array of (uri => uri, filename => filename, meta => array(key => value))
 * @param string $title
 *   option title argument
 * @return string
 */
function media_display_user_files_form($files, $title = null) {
  // pase files into options array
  $options = array();
  foreach ($files as $file) {
    $options[$file['uri']] = $file['filename'];
  }
  // parse files into form elemet
  $form['media_files'] = array( 
    '#type' => 'select',
    '#options' => $options,
    '#title' => $title ? $title : '',
    '#size' => 10,
  );
  return $form;
}


/**
 * Display the upload form for the tab 
 * @return string
 */ 
function media_display_upload_form() {
  $form['media_formaters'] = array(
    '#title' => 'formaters placeholder',
    '#value' => '<br />fomaters will be loaded in here via ajax',
  );
  $form['media_upload']['upload'] = array(
    '#type' => 'file',
    '#title' => t('Upload your file'),
    '#size' => 30,
   );
   return $form;
}


/**
 * wrapper to build the media tabs
 * @param array $forms
 * @return array
 */
function media_tabs_form($forms) {
  if (module_exists('tabs')) {
    $form = array();    
    $form['remig'] = array(
      '#type' => 'tabset',
    );
    foreach ($forms as $id => $element) {
      $form['remig']['tab_'. $id] = array(
        '#type' => 'tabpage',
        '#title' => $id,
        'content' => $element,
      );
    }
  }
  else {
    $form = theme('media_no_tab_display', $tabs);
  }
  return $form;
}

/* ***************************************** */
/* media Hook Implementations                */
/* ***************************************** */

/**
 * Implementation of hook_media_user_files_select
 *
 * @param string $node_type
 * @param string $field
 * @param int $uid
 */
function media_media_user_files_select($node_type, $field, $uid) {
  $files = array();
  $results = db_query('SELECT filepath, filename FROM {files} WHERE uid = %d', $uid);
  while ($file = db_fetch_array($results)) {
    $files[] = array(
      'uri' => $file['filepath'],
      'filename' => $file['filename'],
      'meta' => '',
    );
  }
  $return = array( 
    t('My files') => array(
      t('Local') => array( 
        'form' => media_display_user_files_form($files, t('Your files')),
      )
    ),
    t('Add files') => array(
     t('New file') => array(
       'form' => media_display_upload_form(),
     )
    ),    
  );
  return $return;
}