You are here

function media_browser_plus_load_multiple in Media Browser Plus 7

Same name and namespace in other branches
  1. 7.2 media_browser_plus.module \media_browser_plus_load_multiple()

Loads media entities and allows filtering, sorting and paging.

Parameters

$variables holds an array with the following optional parameters: $ids one dimensional array of entity_ids $conditions multidemsional array build like this: array( array('entity' => array('comlumn', 'value', 'condition')), array('property' => array('comlumn', 'value', 'condition')), array('field' => array('field_name', 'comlumn', 'value', 'condition')), ) $order multidemsional array build like this: array(array('entity' => array('comlumn', 'direction')), array('property' => array('comlumn', 'direction')), array('field' => array('comlumn', 'direction'))) $header table header used for sorting $per_page items per page

array $paging: enables/disables paging - default is paging on

6 calls to media_browser_plus_load_multiple()
media_browser_plus_download_images_submit in ./media_browser_plus.module
Puts all selected media items into a zip archive and sends it as download.
media_browser_plus_folder_media_import in includes/media_browser_plus.admin.inc
@todo Document what this function does.
media_browser_plus_folder_update_file_locations_batch in includes/media_browser_plus.folders.inc
Batch function that updates all media URIs inside the given folders.
media_browser_plus_thumbnailsJSON in ./media_browser_plus.module
Called by the JS fronted (ajax) to get the media list for a given folder.
_media_browser_plus_folder_empty in ./media_browser_plus.module
Check if a given folder has media in it (does include current filter)

... See full list

File

./media_browser_plus.module, line 1274
Adds fields to the media browser forms for better UX

Code

function media_browser_plus_load_multiple($variables) {

  // Set up default parameter.
  $params = array(
    'ids' => array(),
    'conditions' => array(),
    'order' => array(),
    'header' => array(),
    'page' => -1,
    'per_page' => variable_get('media_media_per_page'),
    'paging' => TRUE,
    'ids_only' => FALSE,
    'count_only' => FALSE,
    'apply_filter' => TRUE,
  );

  // Override defaults.
  // @todo Is this loop really necessary. Something like
  // $params = $variables + $params; should work too, right?
  foreach ($variables as $key => $value) {
    if (isset($params[$key])) {
      $params[$key] =& $variables[$key];
    }
  }
  if ($params['apply_filter'] && isset($_SESSION['media-filter'])) {
    if (strlen($_SESSION['media-filter']['filename'])) {
      $params['conditions'][] = array(
        'property' => array(
          'filename',
          '%' . $_SESSION['media-filter']['filename'] . '%',
          'LIKE',
        ),
      );
    }
    if (count($_SESSION['media-filter']['type'])) {
      $params['conditions'][] = array(
        'property' => array(
          'type',
          explode(',', $_SESSION['media-filter']['type']),
          'IN',
        ),
      );
    }
    if (count($_SESSION['media-filter']['field_folder'])) {
      $params['conditions'][] = array(
        'field' => array(
          'field_folder',
          'tid',
          $_SESSION['media-filter']['field_folder'],
          'IN',
        ),
      );
    }
    if (count($_SESSION['media-filter']['field_tags'])) {
      foreach ($_SESSION['media-filter']['field_tags'] as $tag_id) {
        $params['conditions'][] = array(
          'field' => array(
            'field_tags',
            'tid',
            $tag_id,
            '=',
          ),
        );
      }
    }
  }

  // Allow other modules to add/alter conditions.
  foreach (module_implements('media_access_conditions') as $module) {
    $params['conditions'] = array_merge(module_invoke($module, 'media_access_conditions'), $params['conditions']);
  }
  $query = new EntityFieldQuery();

  // Set entity type to media.
  $query
    ->entityCondition('entity_type', 'file');

  // Parse ids if any have been passed.
  if (count($params['ids'])) {
    $query
      ->entityCondition('entity_id', $params['ids'], 'IN');
  }

  // Check for table header.
  if (count($params['header'])) {
    $query
      ->tableSort($params['header']);
  }

  // Parse conditions.
  foreach ($params['conditions'] as $condition) {

    // Look what type we have.
    $condition_keys = array_keys($condition);
    switch (array_pop($condition_keys)) {
      case 'entity':
        $query
          ->entityCondition($condition['entity'][0], $condition['entity'][1], $condition['entity'][2]);
        break;
      case 'property':
        $query
          ->propertyCondition($condition['property'][0], $condition['property'][1], $condition['property'][2]);
        break;
      case 'field':
        $query
          ->fieldCondition($condition['field'][0], $condition['field'][1], $condition['field'][2], $condition['field'][3]);
        break;
    }
  }

  // Parse order array.
  foreach ($params['order'] as $condition) {

    // Look what type we have.
    $condition_keys = array_keys($condition);
    switch (array_pop($condition_keys)) {
      case 'entity':
        $query
          ->entityOrderBy($condition['entity'][0], $condition['entity'][1]);
        break;
      case 'property':
        $query
          ->propertyOrderBy($condition['property'][0], $condition['property'][1]);
        break;
      case 'field':
        $query
          ->fieldOrderBy($condition['field'][0], $condition['field'][1], $condition['field'][2]);
        break;
    }
  }

  // Prepare result object.
  $res_object = new stdClass();
  if ($params['paging'] && !$params['count_only']) {

    // Doing a seperate count query here, because including the internal pager
    // doesn't work but seems the only way to get a total result count.
    $count = clone $query;
    $count
      ->pager();
    $count
      ->execute();
    $res_object->overall_count = $count->pager['total'];

    // Using range here, because as said above the internal pager seems to be
    // bugged.
    $page = $params['page'];
    if ($page == -1) {
      $page = isset($_REQUEST['page']) ? $_REQUEST['page'] : 0;
    }
    $query
      ->range($page * $params['per_page'], $params['per_page']);

    // Adding the addition result fields.
    $res_object->page = $page;
    $res_object->pages = ceil($res_object->overall_count / variable_get('media_media_per_page'));
    $res_object->per_page = $params['per_page'];
  }

  // Count only query.
  if ($params['count_only']) {
    $query
      ->pager();
    $query
      ->execute();
    return $query->pager['total'];
  }
  $res_object->results = array();

  // Execute query and load results.
  $result = $query
    ->execute();
  if (!empty($result['file'])) {
    $res_object->results = $params['ids_only'] ? array_keys($result['file']) : entity_load('file', array_keys($result['file']));
  }
  return $res_object;
}