You are here

function drush_javascript_libraries_jslib_switch_url in JavaScript Libraries Manager 7

Drush command callback; switches the URL of an external JavaScript library.

File

./javascript_libraries.drush.inc, line 39
Drush commands for managing JavaScript libraries.

Code

function drush_javascript_libraries_jslib_switch_url($current_url = NULL, $new_url = NULL) {

  // The new URL must be valid in order to proceed.
  if (!javascript_libraries_valid_external_url($new_url)) {
    return drush_set_error(NULL, dt('The URL @url is invalid; it must begin with http:// or https:// and end with .js or .js.txt.', array(
      '@url' => $new_url,
    )));
  }

  // Replace all occurrences of the old URL with the new one.
  $changed = array();
  $libraries = variable_get('javascript_libraries_custom_libraries', array());
  foreach ($libraries as &$library) {
    if ($library['type'] == 'external' && $library['uri'] == $current_url && $library['uri'] != $new_url) {
      $library['uri'] = $new_url;
      $changed[] = $library;
    }
  }

  // Only save the variable if it changed.
  if ($changed) {
    drush_op('variable_set', 'javascript_libraries_custom_libraries', $libraries);
    javascript_libraries_flush_caches();
    $info = array();
    foreach ($changed as $lib) {
      $info[] = "{$lib['id']} : {$lib['name']} : {$lib['scope']}";
    }
    drush_log(dt('External library URL was updated from @old to @new for @count URI(s): @info', array(
      '@old' => $current_url,
      '@new' => $new_url,
      '@count' => count($changed),
      '@info' => implode(' | ', $info),
    )), 'success');
  }
  else {
    drush_set_error(NULL, dt('There are no existing libraries with the URL @old.', array(
      '@old' => $current_url,
    )));
  }
}