You are here

function js_injector_edit in JS injector 6.2

Same name and namespace in other branches
  1. 6 js_injector.admin.inc \js_injector_edit()
  2. 7 js_injector.admin.inc \js_injector_edit()

Constructor for the js rule edit form.

1 string reference to 'js_injector_edit'
js_injector_menu in ./js_injector.module
Implementation of hook_menu(). Defines menu callbacks for js Injector's configuration pages.

File

./js_injector.admin.inc, line 97
Administration pages for js_injector.

Code

function js_injector_edit($form_state, $crid = NULL) {
  if (isset($crid)) {
    $rule = _js_injector_load_rule($crid, TRUE);
    $form['crid'] = array(
      '#type' => 'value',
      '#value' => $crid,
    );
    $path = file_create_path(_js_injector_rule_path($rule['crid']));
    if (file_exists($path)) {
      $rule['js_text'] = file_get_contents($path);
    }
    else {
      $rule['js_text'] = '';
    }
  }
  else {
    $rule = array(
      'title' => '',
      'rule_type' => 0,
      'rule_conditions' => '',
      'scope' => 'header',
      'preprocess' => 1,
      'cache' => 1,
      'js_text' => '',
    );
  }
  $form['title'] = array(
    '#type' => 'textfield',
    '#title' => t('Title'),
    '#default_value' => $rule['title'],
    '#required' => TRUE,
  );
  $form['js_text'] = array(
    '#type' => 'textarea',
    '#title' => t('js code'),
    '#rows' => 10,
    '#default_value' => $rule['js_text'],
    '#required' => TRUE,
  );

  // Shamelessly ripped from block.module. Who doesn't use this snippet
  // of code, really?
  $access = user_access('use PHP for block visibility');
  if ($rule['rule_type'] == 2 && !$access) {
    $form['conditional'] = array();
    $form['conditional']['rule_type'] = array(
      '#type' => 'value',
      '#value' => 2,
    );
    $form['conditional']['rule_conditions'] = array(
      '#type' => 'value',
      '#value' => $rule['rule_conitions'],
    );
  }
  else {
    $options = array(
      t('Add on every page except the listed pages.'),
      t('Add on only the listed pages.'),
    );
    $description = t("Enter one page per line as Drupal paths. The '*' character is a wildcard. Example paths are %blog for the blog page and %blog-wildcard for every personal blog. %front is the front page.", array(
      '%blog' => 'blog',
      '%blog-wildcard' => 'blog/*',
      '%front' => '<front>',
    ));
    if ($access) {
      $options[] = t('Add if the following PHP code returns <code>TRUE</code> (PHP-mode, experts only).');
      $description .= ' ' . t('If the PHP-mode is chosen, enter PHP code between %php. Note that executing incorrect PHP-code can break your Drupal site.', array(
        '%php' => '<?php ?>',
      ));
    }
    $form['conditional']['rule_type'] = array(
      '#type' => 'radios',
      '#title' => t('Add the js on specific pages'),
      '#options' => $options,
      '#default_value' => $rule['rule_type'],
    );
    $form['conditional']['rule_conditions'] = array(
      '#type' => 'textarea',
      '#title' => t('Pages'),
      '#default_value' => $rule['rule_conditions'],
      '#description' => $description,
    );
  }
  $form['scope'] = array(
    '#type' => 'select',
    '#title' => t('Scope'),
    '#options' => array(
      'header' => t('Header'),
      'footer' => t('Footer'),
    ),
    '#default_value' => $rule['scope'],
  );
  $form['preprocess'] = array(
    '#type' => 'checkbox',
    '#title' => t('Preprocess js'),
    '#default_value' => $rule['preprocess'],
  );
  $form['cache'] = array(
    '#type' => 'checkbox',
    '#title' => t('Cache js'),
    '#default_value' => $rule['cache'],
  );
  $form['buttons']['save'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
    '#submit' => array(
      'js_injector_edit_save',
    ),
  );
  $form['buttons']['save_and_continue'] = array(
    '#type' => 'submit',
    '#value' => t('Save and Continue'),
    '#submit' => array(
      'js_injector_edit_save_and_continue',
    ),
  );
  if (!empty($rule['crid'])) {
    $form['buttons']['delete'] = array(
      '#type' => 'submit',
      '#value' => t('Delete'),
      '#submit' => array(
        'js_injector_admin_delete_button',
      ),
      '#crid' => $rule['crid'],
    );
  }
  return $form;
}