You are here

function drupalgap_sdk_form_submit in DrupalGap 7

Same name and namespace in other branches
  1. 7.2 drupalgap.module \drupalgap_sdk_form_submit()

File

./drupalgap.module, line 441
A module to provide a bridge between Drupal websites and PhoneGap mobile applications.

Code

function drupalgap_sdk_form_submit($form, &$form_state) {
  global $base_url;

  // Set up vars.
  $dir = check_plain($form_state['values']['dir']);
  $file = "drupalgap-sdk.zip";

  // Dev release.

  //$source = "https://github.com/signalpoint/DrupalGap/archive/7.x-1.x.zip";

  //$name = "DrupalGap-7.x-1.x";

  // Recommended release.
  $source = "https://github.com/signalpoint/DrupalGap/archive/7.0.2.zip";
  $name = "DrupalGap-7.0.2";

  // Download a zip file of the SDK from github.
  if (file_put_contents($file, file_get_contents($source)) === false) {
    return;
  }

  // Unzip the SDK.
  $path = pathinfo(realpath($file), PATHINFO_DIRNAME);
  $zip = new ZipArchive();
  $res = $zip
    ->open($file);
  if ($res === TRUE) {
    $zip
      ->extractTo($path);
    $zip
      ->close();
  }
  else {
    drupal_set_message("Failed to unzip the {$file} file!", 'error');
  }

  // Rename the folder.
  if (!rename($name, $dir)) {
    drupal_set_message("Failed to move unzipped files to the {$dir} directory!", 'error');
    return;
  }

  // Make a copy of default.settings.js, set the site_path and save it as settings.js.
  $settings = file_get_contents("{$dir}/app/default.settings.js");
  $settings = str_replace("Drupal.settings.site_path = '';", "Drupal.settings.site_path = '{$base_url}';", $settings);
  if (file_put_contents("{$dir}/app/settings.js", $settings) === false) {
    drupal_set_message(t('Failed to create the settings.js file!'), 'error');
  }

  // Delete the zip file.
  if (!unlink($file)) {
    drupal_set_message(t("Tried deleting the %file file, but failed", array(
      '%file' => $file,
    )), 'warning');
  }

  // Tell Drupal that everything is installed.
  variable_set('drupalgap_sdk_installed', 1);
  variable_set('drupalgap_sdk_dir', $dir);
  drupal_set_message(t('The DrupalGap SDK was installed successfully! You may now test it below...'));
}