You are here

function media_browser_plus_thumbnailsJSON in Media Browser Plus 7

Same name and namespace in other branches
  1. 7.2 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 338
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)) {
              if (empty($param)) {
                $valid = FALSE;
                break;
              }
              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.
    $conditions[] = array(
      'field' => array(
        'field_folder',
        'tid',
        array(
          $folder,
        ),
        '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);
  }
}