You are here

function js_injector_rule_save in JS injector 7.2

Save a single JS injector rule. This custom save callback is required in order to write (or update) the JavaScript file on the filesystem

1 string reference to 'js_injector_rule_save'
js_injector_schema in ./js_injector.install
Implements hook_schema().

File

./js_injector.module, line 75
js_injector.module

Code

function js_injector_rule_save($rule) {
  $schema = ctools_export_get_schema('js_injector_rule');
  $export = $schema['export'];

  // objects should have a serial primary key. If not, simply fail to write.
  if (empty($export['primary key'])) {
    return FALSE;
  }
  $key = $export['primary key'];
  if ($rule->export_type & EXPORT_IN_DATABASE) {

    // existing record.
    $update = array(
      $key,
    );
  }
  else {

    // new record.
    $update = array();
    $rule->export_type = EXPORT_IN_DATABASE;
  }

  // rule is passed by reference, and the crid is written into it upon save
  $success = drupal_write_record('js_injector_rule', $rule, $update);
  if ($success == FALSE || empty($rule->crid)) {
    return FALSE;
  }

  // write the JS file to the filesystem
  $file_written = file_unmanaged_save_data($rule->js, _js_injector_rule_uri($rule->crid), FILE_EXISTS_REPLACE);
  return $file_written != FALSE && is_numeric($rule->crid);
}