You are here

function media_acquiadam_browser_choose_asset_form in Media: Acquia DAM 7

Acquia DAM browser form.

Parameters

array $form: A form render array.

array $form_state: The current form state.

array $options: An array of options as passed from the Media Browser plugin.

Return value

array A form render array.

2 string references to 'media_acquiadam_browser_choose_asset_form'
AcquiaDAMMediaBrowserPlugin::view in modules/media_acquiadam_browser/includes/AcquiaDAMMediaBrowserPlugin.inc
Implements MediaBrowserPlugin::view().
media_acquiadam_browser_menu in modules/media_acquiadam_browser/media_acquiadam_browser.module
Implements hook_menu().

File

modules/media_acquiadam_browser/includes/media_acquiadam_browser.forms.inc, line 45
Form related hooks and functions.

Code

function media_acquiadam_browser_choose_asset_form(array $form, array &$form_state, array $options = []) {

  // Used for building reset and other URLs to the current page.
  $qp = drupal_get_query_parameters(NULL, [
    'q',
    'page',
    'code',
  ]);
  $client = AcquiaDAM_API::getClient();

  // 'aa' parameter is used to determine if the user has clicked the
  // authenticate link in our warning message. The state parameter is used by
  // the OAuth2 authentication mechanism and is a way to tell if we're in the
  // process of authenticating.
  $is_authing = !empty($qp['aa']) || !empty($qp['state']);
  $needs_auth = !empty($client) && !$client
    ->isAuthenticated() && !$client
    ->hasAuthenticationError();
  if ($needs_auth && $is_authing) {
    $client
      ->authenticate(TRUE);
  }
  $is_authenticated = !empty($client) && $client
    ->isAuthenticated();
  $search = !empty($qp['search']) ? $qp['search'] : [];
  unset($qp['search']);
  $form['search'] = [
    '#type' => 'container',
    '#access' => $is_authenticated,
  ];
  $form['search']['flex-container'] = [
    '#type' => 'container',
    '#tree' => FALSE,
    '#attributes' => [
      'class' => [
        'acquiadam-search-fields',
      ],
    ],
  ];
  $form['search']['description'] = [
    '#prefix' => '<div class="search-description description"><p>',
    '#suffix' => '</p></div>',
    '#markup' => t('You may search for assets by name, metadata, and other properties. If you have navigated into a folder then the search is limited to that folder and below.'),
  ];
  $form['search']['flex-container']['keywords'] = [
    '#type' => 'textfield',
    '#title' => t('Keywords'),
    '#description' => t('Refine your search using operations like AND, OR, * and -.'),
    '#default_value' => !empty($search['keywords']) ? $search['keywords'] : '',
  ];
  $form['search']['flex-container']['type_filter'] = [
    '#type' => 'select',
    '#title' => t('Type'),
    '#empty_option' => t('Any'),
    '#empty_value' => '',
    '#options' => [
      'image' => t('Image'),
      'audiovideo' => t('Audio/Video'),
      'document' => t('Documents'),
      'presentation' => t('Presentations'),
      'other' => t('Other'),
    ],
    '#default_value' => !empty($search['type']) ? $search['type'] : '',
  ];
  $form['search']['flex-container']['sort'] = [
    '#type' => 'select',
    '#title' => t('Sort'),
    '#options' => [
      'filename_asc' => t('File name (A-Z)'),
      'filename_desc' => t('File name (Z-A)'),
      'filesize_asc' => t('File size (Smallest)'),
      'filesize_desc' => t('File size (Largest)'),
      'datecreated_asc' => t('Date created (Oldest)'),
      'datecreated_desc' => t('Date created (Newest)'),
      'datemodified_asc' => t('Date modified (Oldest)'),
      'datemodified_desc' => t('Date modified (Newest)'),
    ],
    '#default_value' => !empty($search['sort']) ? $search['sort'] : 'datecreated_desc',
  ];
  $form['search']['flex-container']['search'] = [
    '#type' => 'submit',
    '#value' => t('Search'),
    '#name' => 'search',
    '#attributes' => [
      'class' => [
        'no-label',
      ],
    ],
  ];

  // Gives the user a way to easily reset their search without losing their
  // place.
  $form['search']['flex-container']['cancel'] = [
    '#type' => 'link',
    '#title' => t('Reset'),
    '#href' => current_path(),
    '#options' => [
      'query' => drupal_get_query_parameters(NULL, [
        'q',
        'page',
        'search',
        'aa',
      ]),
    ],
    '#access' => !empty($search),
    '#attributes' => [
      'class' => [
        'form-item',
        'reset-search',
        'no-label',
      ],
    ],
  ];
  $form['results'] = [
    '#type' => 'fieldset',
    '#attributes' => [
      'class' => [
        'media-browser',
      ],
    ],
    '#attached' => [
      'js' => [
        drupal_get_path('module', 'media_acquiadam_browser') . '/js/media_acquiadam_browser.browser.js',
      ],
      'css' => [
        drupal_get_path('module', 'media_acquiadam_browser') . '/css/media_acquiadam_browser.browser.css',
      ],
    ],
  ];
  $form['results']['#attached']['js'][] = [
    'type' => 'setting',
    'data' => [
      'acquiadam-info' => [
        'closeText' => t('Close'),
        'modalSize' => [
          'type' => 'scale',
          'width' => empty($options) ? 0.6 : 1,
          'height' => empty($options) ? 0.6 : 1,
        ],
      ],
    ],
  ];
  $form['results']['trail'] = [
    '#theme' => 'media_acquiadam_browser_trail',
    '#title' => t('Trail'),
    '#access' => FALSE,
    '#assets' => [],
  ];

  // There is logic in the after_build and submit handlers that enable the
  // seamless switching between checkboxes and radios.
  $allow_multi = !isset($options['multiselect']);
  $allow_multi |= isset($options['multiselect']) && !empty($options['multiselect']);
  $form['results']['assets'] = [
    '#type' => $allow_multi ? 'checkboxes' : 'radios',
    '#title' => t('Assets'),
    '#options' => [],
    '#assets' => [],
    '#after_build' => [
      'media_acquiadam_browser_checkboxes_after_build',
    ],
  ];
  $form['results']['empty_message'] = [
    '#markup' => '<p>' . t('There are no available assets.') . '</p>',
    '#access' => empty($form['results']['assets']['#assets']),
  ];

  // Pager is primed in media_acquiadam_browser_choose_asset_form_fill_assets().
  $form['results']['pager'] = [
    '#theme' => 'pager',
  ];
  try {
    $me = !empty($client) ? $client
      ->getUser() : NULL;
    $user = !empty($me['username']) ? $me['username'] : t('Not authenticated');
  } catch (Exception $x) {

    // Not a big deal if this fails.
    $user = t('Error');
  }
  $form['admin'] = [
    '#type' => 'fieldset',
    '#title' => t('Administration options'),
    '#description' => t('Only Acquia DAM administrators can see these options.'),
    '#access' => user_access('administer media acquiadam'),
    'user' => [
      '#type' => 'markup',
      '#markup' => '<p>' . t('Authenticated as %user.', [
        '%user' => $user,
      ]) . '</p>',
    ],
    'clear' => [
      '#type' => 'markup',
      '#markup' => '<p>' . l(t('Clear access token'), 'user/' . $GLOBALS['user']->uid . '/acquiadam/deauth', [
        'query' => [
          'destination' => current_path(),
        ],
      ]) . '</p>',
      '#access' => variable_get('media_acquiadam_client_mode') == 'mixed',
    ],
  ];
  $form['actions'] = [
    '#type' => 'actions',
    'back' => [
      '#type' => 'link',
      '#title' => t('Back'),
      '#href' => '#',
      '#options' => [
        'external' => TRUE,
      ],
      '#attributes' => [
        'rel' => 'no-follow',
        'class' => [
          'back-link',
        ],
      ],
      '#access' => FALSE,
    ],
    'submit' => [
      '#type' => 'submit',
      '#value' => t('Submit'),
      '#access' => $is_authenticated,
    ],
  ];
  if ($is_authenticated) {
    media_acquiadam_browser_choose_asset_form_fill_assets($form, $form_state, $options);
  }
  elseif (!empty($client)) {
    drupal_set_message(t('The Acquia DAM browser does not have access to your Acquia DAM account. <a href="@auth-url">Please click here to authorize access to Acquia DAM.</a>', [
      '@auth-url' => url(current_path(), [
        'query' => [
          'aa' => TRUE,
        ],
      ]),
    ]), 'warning');
  }
  elseif (empty($client)) {
    if (user_access('administer media acquiadam')) {
      drupal_set_message(t('Unable to get a valid DAM client. Check your internet connection and try again or verify the <a href="@url">module configuration</a>.', [
        '@url' => url('admin/config/media/acquiadam'),
      ]), 'warning');
    }
    else {
      drupal_set_message(t('Unable to get a valid DAM client. Check your internet connection and try again or please contact the site administrator.'), 'error');
    }
    watchdog('media_acquiadam_browser', 'Unable to get a DAM client object.', NULL, WATCHDOG_NOTICE);
  }
  return $form;
}