You are here

function media_browser_plus_thumbnailsJSON in Media Browser Plus 7.2

Same name and namespace in other branches
  1. 7 media_browser_plus.module \media_browser_plus_thumbnailsJSON()

Called by the JS fronted (ajax) to get the media list for a given folder.

1 string reference to 'media_browser_plus_thumbnailsJSON'
media_browser_plus_menu in ./media_browser_plus.module
Implements hook_menu().

File

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

Code

function media_browser_plus_thumbnailsJSON() {
  if (isset($_GET['folder'])) {
    $folder = (int) str_replace('folder_load_', '', $_GET['folder']);

    // Create conditions.
    $conditions = array();

    // Check for filter set by library.
    if (isset($_GET['filter'])) {
      $filter = drupal_json_decode($_GET['filter']);

      // Bugfix - $conditions = $filter;
      foreach ($filter as $key => $value) {

        // Checking each filter.
        $valid = TRUE;
        foreach ($value as $type => $params) {
          foreach ($params as $param) {
            if (is_array($param)) {
              $valid = !empty($param);
              foreach ($param as $media_type) {
                if (strlen(trim($media_type)) == 0) {
                  $valid = FALSE;
                  break;
                }
              }
            }
            else {
              if (strlen(trim($param)) == 0) {
                $valid = FALSE;
                break;
              }
            }
          }
        }
        if ($valid) {
          $conditions[] = $value;
        }
      }
    }

    // More conditions.
    if ($folder) {
      $conditions[] = array(
        'field' => array(
          'field_folder',
          'tid',
          array(
            $folder,
          ),
          'IN',
        ),
      );
    }
    elseif ($folder === 0) {

      // Get unsorted files. We can't use EFQ conditions because the field
      // tables are joined using inner join, what excludes unsorted files from
      // the whole query at all. Thus fetch the fids of the unsorted fields and
      // use this as condition.
      $query = db_select('file_managed')
        ->fields('file_managed', array(
        'fid',
        'fid',
      ))
        ->condition('uri', db_like('temporary://') . '%', 'NOT LIKE')
        ->isNull('entity_id');
      $query
        ->leftJoin('field_data_field_folder', 'folder', "entity_type = 'file' AND entity_id = file_managed.fid");
      $result = $query
        ->execute()
        ->fetchAll(PDO::FETCH_KEY_PAIR);
      $conditions[] = array(
        'property' => array(
          'fid',
          $result,
          'IN',
        ),
      );
    }
    $order = array(
      array(
        'property' => array(
          'fid',
          'DESC',
        ),
      ),
    );
    $media_entities = media_browser_plus_load_multiple(array(
      'conditions' => $conditions,
      'order' => $order,
    ));
    module_load_include('inc', 'media', 'includes/media.browser');
    foreach ($media_entities->results as $media) {
      media_browser_build_media_item($media);
    }
    $output = array(
      'media' => array_values($media_entities->results),
      'folder_loaded' => 'folder_load_' . $folder,
      'overall_count' => $media_entities->overall_count,
    );
    drupal_json_output($output);
    die;
  }
}