You are here

function biblio_citeproc_style_manager_form in Bibliography Module 7.2

Same name and namespace in other branches
  1. 6.2 modules/CiteProc/biblio_citeproc.admin.inc \biblio_citeproc_style_manager_form()
  2. 7 modules/CiteProc/biblio_citeproc.admin.inc \biblio_citeproc_style_manager_form()
1 string reference to 'biblio_citeproc_style_manager_form'
biblio_citeproc_menu in modules/CiteProc/biblio_citeproc.module

File

modules/CiteProc/biblio_citeproc.admin.inc, line 2

Code

function biblio_citeproc_style_manager_form($form, &$form_state) {
  $form = array();
  $options = array();
  if ($has_curl = function_exists('curl_init')) {
    $cache = cache_get('github_csl_repo');
    if (!$cache || $cache->expire < time()) {
      module_load_include('php', 'biblio_citeproc', '/Github/Autoloader');
      Github_Autoloader::register();
      $github = new Github_Client();
      $branches = $github
        ->getRepoApi()
        ->getRepoBranches('citation-style-language', 'styles');
      $tree = $github
        ->getObjectApi()
        ->listBlobs('citation-style-language', 'styles', $branches['master']);
      $tree = array_flip($tree);
      $options = array();
      foreach ($tree as $sha => $file) {
        if (strstr($file, '.csl')) {
          $options[$sha] = basename($file);
        }
      }
      if (!empty($options)) {
        $expire = time() + 86400;

        //one day from now
        cache_set('github_csl_repo', $options, 'cache', $expire);
      }
    }
    else {
      $options = $cache->data;
    }
  }
  if (!$has_curl) {
    $message = t('Additional styles cannot be ' . 'installed because the PHP <a href="@curl_url">cURL</a> library is ' . 'not available.', array(
      '@curl_url' => 'http://www.php.net/manual/en/book.curl.php',
    ));
    drupal_set_message($message, 'warning');
  }
  $form['remote_names'] = array(
    '#type' => 'value',
    '#value' => $options,
  );
  asort($options);
  $form['avialable_styles'] = array(
    '#type' => 'select',
    '#title' => t('Available styles'),
    '#size' => 15,
    '#multiple' => TRUE,
    '#disabled' => !$has_curl,
    '#description' => t('Choose the styles you would like to download and install.'),
  );
  $form['install'] = array(
    '#type' => 'submit',
    '#disabled' => !$has_curl,
    '#value' => '<--',
    '#description' => t('Install the selected styles from GitHub'),
  );
  $form['remove'] = array(
    '#type' => 'submit',
    '#disabled' => !$has_curl,
    '#value' => '-->',
    '#description' => t('Un-install the selected styles'),
  );
  $form['default'] = array(
    '#type' => 'submit',
    '#value' => t('Set as site default'),
  );
  $form['edit'] = array(
    '#type' => 'submit',
    '#value' => t('Edit selected'),
  );
  $form['install_all'] = array(
    '#type' => 'submit',
    '#disabled' => !$has_curl,
    '#value' => t('Install all'),
  );
  $form['#attributes']['enctype'] = 'multipart/form-data';
  $form['import_csl_file'] = array(
    '#type' => 'file',
    '#title' => t('Import Local CSL file'),
    '#default_value' => '',
    '#size' => 60,
  );
  $form['import'] = array(
    '#type' => 'submit',
    '#value' => t('Import'),
    '#submit' => array(
      'biblio_citeproc_csl_file_import_submit',
    ),
  );
  $result = db_select('biblio_citeproc_styles', 'csl')
    ->fields('csl', array(
    'filename',
    'id',
    'sha1',
    'title',
  ))
    ->orderBy('filename', 'ASC')
    ->execute();
  $titles = array();
  $installed = array();
  foreach ($result as $style) {
    $installed[$style->id] = $style->filename;
    $titles[$style->filename] = $style->title;
    if ($sha = array_search($style->filename, $options)) {
      unset($options[$sha]);
    }
  }
  $form['avialable_styles']['#options'] = $options;
  $form['installed_styles'] = array(
    '#type' => 'select',
    '#title' => t('Installed styles'),
    '#size' => 15,
    '#options' => biblio_get_styles(),
    '#multiple' => TRUE,
    '#description' => t('Currently installed styles.'),
  );
  $form['current_default'] = array(
    '#markup' => empty($titles) ? '' : $titles[variable_get('biblio_citeproc_style', 'cse.csl')],
  );
  return $form;
}