You are here

function fonts_com_browse_form in @font-your-face 7.2

Shows browse filters and results.

1 string reference to 'fonts_com_browse_form'
fonts_com_browse in modules/fonts_com/fonts_com.module
Over-rides default browse interface for Fonts.com.

File

modules/fonts_com/fonts_com.module, line 947

Code

function fonts_com_browse_form($form, &$form_state) {

  // Map internal filter names to Fonts.com keys.
  $filter_map = array(
    'name' => 'wfsKeyword',
    'language' => 'wfsLangId',
    'classification' => 'wfsClassId',
    'designer' => 'wfsDesignerId',
    'foundry' => 'wfsFountryId',
    'alpha' => 'wfsAlphabet',
    'free' => 'wfsfree',
  );
  $form_state['method'] = 'get';
  module_load_include('inc', 'fonts_com', 'api');
  $destination = drupal_get_destination();
  $per_page = 20;
  $start = 0;
  if (isset($_GET['page'])) {
    $start += intval($_GET['page']) * $per_page;
  }

  // if
  // Make API request
  $filters = array();
  $filter_defaults = array();
  foreach ($filter_map as $key => $value) {
    $filter_defaults[$key] = '';
    if (isset($_GET[$key]) && $_GET[$key] != '' && (!isset($_GET['op']) || $_GET['op'] != t('Reset'))) {
      $filter_defaults[$key] = $_GET[$key];
    }

    // if
    if (isset($form_state['values'][$key]) && $form_state['values'][$key] != '') {
      $filter_defaults[$key] = $form_state['values'][$key];
    }

    // if
    if ($filter_defaults[$key] != '') {
      $filters[$value] = $filter_defaults[$key];
    }

    // if
  }

  // foreach
  if (count($filters) > 0) {
    $filter_choices = fonts_com_filter_choices($filters);
  }
  else {
    $filter_choices = fonts_com_filter_choices();
  }

  // else
  $browse_font_results = fonts_com_get_all_fonts($start, $per_page, $filters);
  $browse_fonts = $browse_font_results['fonts'];
  $count = $browse_font_results['count'];

  // Create filters
  $ajax = array(
    'callback' => 'fonts_com_browse_ajax_callback',
    'wrapper' => 'fonts-com-browse-wrapper',
    'progress' => array(
      'type' => 'throbber',
      'message' => NULL,
    ),
  );
  $form['#attached']['css'] = array(
    drupal_get_path('module', 'fonts_com') . '/css/browse.css',
  );
  $form['wrapper'] = array(
    '#prefix' => '<div id="fonts-com-browse-wrapper">',
    '#suffix' => '</div>',
  );
  $form['wrapper']['filters'] = array(
    '#prefix' => '<div class="views-filter">',
    '#suffix' => '</div>',
  );
  $form['wrapper']['filters']['name'] = array(
    '#type' => 'textfield',
    '#title' => 'Name',
    '#size' => 30,
    '#default_value' => $filter_defaults['name'],
    '#ajax' => $ajax,
  );
  $language_options = array(
    '' => '- Any -',
  );
  foreach ($filter_choices['Language'] as $language) {
    $language_options[$language->ValueID] = $language->ValueName;
  }

  // foreach
  $form['wrapper']['filters']['language'] = array(
    '#type' => 'select',
    '#title' => t('Language'),
    '#options' => $language_options,
    '#default_value' => $filter_defaults['language'],
    '#ajax' => $ajax,
  );
  $designer_options = array(
    '' => '- Any -',
  );
  foreach ($filter_choices['Designer'] as $designer) {
    $designer_options[$designer->ValueID] = $designer->ValueName;
  }

  // foreach
  $form['wrapper']['filters']['designer'] = array(
    '#type' => 'select',
    '#title' => t('Designer'),
    '#options' => $designer_options,
    '#default_value' => $filter_defaults['designer'],
    '#ajax' => $ajax,
  );
  $foundry_options = array(
    '' => '- Any -',
  );
  foreach ($filter_choices['Foundry'] as $foundry) {
    $foundry_options[$foundry->ValueID] = $foundry->ValueName;
  }

  // foreach
  $form['wrapper']['filters']['foundry'] = array(
    '#type' => 'select',
    '#title' => t('Foundry'),
    '#options' => $foundry_options,
    '#default_value' => $filter_defaults['foundry'],
    '#ajax' => $ajax,
  );
  $classification_options = array(
    '' => '- Any -',
  );
  foreach ($filter_choices['Classification'] as $classification) {
    $classification_options[$classification->ValueID] = $classification->ValueName;
  }

  // foreach
  $form['wrapper']['filters']['classification'] = array(
    '#type' => 'select',
    '#title' => t('Classification'),
    '#options' => $classification_options,
    '#default_value' => $filter_defaults['classification'],
    '#ajax' => $ajax,
  );
  $alpha_options = array(
    '' => '- Any -',
  );
  foreach ($filter_choices['Alpha'] as $alpha) {
    $alpha_options[$alpha->ValueID] = $alpha->ValueName;
  }

  // foreach
  $form['wrapper']['filters']['alpha'] = array(
    '#type' => 'select',
    '#title' => t('Alpha'),
    '#options' => $alpha_options,
    '#default_value' => $filter_defaults['alpha'],
    '#ajax' => $ajax,
  );
  $free_options = array(
    '' => '- Any -',
  );
  foreach ($filter_choices['FreeOrPaid'] as $free) {
    $free_options[$free->ValueID] = $free->ValueName;
  }

  // foreach
  $form['wrapper']['filters']['free'] = array(
    '#type' => 'select',
    '#title' => t('Free or Paid'),
    '#options' => $free_options,
    '#default_value' => $filter_defaults['free'],
    '#ajax' => $ajax,
  );
  $form['wrapper']['filters']['filter'] = array(
    '#type' => 'submit',
    '#value' => t('Filter'),
  );
  $form['wrapper']['filters']['reset'] = array(
    '#type' => 'submit',
    '#value' => t('Reset'),
  );

  // Display results
  $form['wrapper']['results'] = array(
    '#prefix' => '<div class="view-content" id="fonts-com-view-results"><ul class="fonts-listing">',
    '#suffix' => '</ul></div>',
  );
  $row = 1;
  $css = '';
  foreach ($browse_fonts as $index => $import_font) {
    $class = 'row-' . $row;
    if ($row == 1) {
      $class .= ' row-first';
    }

    // if
    if (count($browse_fonts) == 0) {
      $class .= ' row-last';
    }

    // if
    $form['wrapper']['results']['row-' . $row] = array(
      '#prefix' => '<li class="' . $class . ' font">',
      '#suffix' => '</li>',
    );
    $font = fonts_com_saved_api_font($import_font);
    $css .= fonts_com_font_inline_css($font) . "\n\n";
    $enabled = 'no';
    if ($font->enabled) {
      $enabled = 'yes';
    }

    // if
    $form['wrapper']['results']['row-' . $row]['header'] = array(
      '#type' => 'markup',
      '#markup' => '<div class="fontyourface-header"><h4>' . check_plain($font->name) . '<br />' . check_plain($import_font->FontLanguage) . '</h4>' . l('<span>' . t('Edit') . '</span>', 'admin/appearance/fontyourface/edit/' . $font->fid, array(
        'html' => TRUE,
        'query' => array(
          'destination' => 'admin/appearance/fontyourface/browse/fonts_com',
        ),
      )) . '</div>',
    );
    if ($font->enabled) {
      $enable_disable_link = l(t('Disable'), 'admin/appearance/fontyourface/disable/' . $font->fid . '?' . drupal_http_build_query($destination), array(
        'attributes' => array(
          'class' => 'disable-link',
        ),
      ));
    }
    else {
      $enable_disable_link = l(t('Enable'), 'admin/appearance/fontyourface/enable/' . $font->fid . '?' . drupal_http_build_query($destination), array(
        'attributes' => array(
          'class' => 'enable-link',
        ),
      ));
    }

    // else
    $form['wrapper']['results']['row-' . $row]['footer'] = array(
      '#type' => 'markup',
      '#markup' => '<div class="fontyourface-footer enabled-' . $enabled . '" data-fid="' . $font->fid . '">' . $enable_disable_link . '</div>',
    );
    $form['wrapper']['results']['row-' . $row]['preview'] = array(
      '#type' => 'markup',
      '#markup' => '<div class="fontyourface-preview enabled-' . $enabled . '">' . fontyourface_preview($font, fontyourface_short_preview_text($font), 40) . '</div>',
    );
    $row++;
  }

  // foreach
  // Make sure JS loads on AJAX requests.
  fonts_com_ajax_css_registry($css);
  $page = pager_default_initialize($count, $per_page);
  if (isset($form_state['values'])) {

    /**
     * Make pager work with AJAX. This is a bit of a hack until core
     * supports AJAX pagers.
     */
    $temp_get = $_GET;
    $_GET = $form_state['values'];
    $_GET['q'] = 'admin/appearance/fontyourface/browse/fonts_com';
  }

  // if
  $pager = theme('pager');
  if (isset($form_state['values'])) {

    // Revert AJAX temporary pager hack.
    $_GET = $temp_get;
  }

  // if
  $form['wrapper']['pager'] = array(
    '#type' => 'markup',
    '#markup' => $pager,
  );
  return $form;
}