You are here

function patterns_edit in Patterns 7

Same name and namespace in other branches
  1. 5 patterns.module \patterns_edit()
  2. 6.2 patterns.module \patterns_edit()
  3. 6 patterns.module \patterns_edit()
  4. 7.2 includes/forms/editor.inc \patterns_edit()

Form constructor for editing a pattern. TODO:params

See also

patterns_edit_validate()

patterns_edit_submit()

2 string references to 'patterns_edit'
patterns_editor_create_page in includes/forms/editor.inc
Display the page for creating a new pattern.
patterns_edit_page in includes/forms/editor.inc
Display the page for editing a pattern.

File

includes/forms/editor.inc, line 109
Functions, forms related to the Patterns editor.

Code

function patterns_edit($form, &$form_state, $pattern, $full = TRUE) {

  // TODO: this form could be reused, see function patterns_enable_pattern().
  // Default
  $content = '# Pattern';
  $format = empty($pattern->format) ? PATTERNS_FORMAT_UNKNOWN : $pattern->format;
  $title = t('Pattern File');
  $description = '';
  $validation = patterns_db_analyze_patterns_status($pattern->status);
  $form['name'] = array(
    '#type' => 'value',
    '#value' => $pattern->name,
  );
  $form['pid'] = array(
    '#type' => 'value',
    '#value' => $pattern->pid,
  );
  $form = patterns_forms_get_formats_selector($form, $format);
  if (!empty($pattern->file)) {
    if (file_exists($pattern->file) && is_readable($pattern->file)) {
      $content = file_get_contents($pattern->file);
    }
    else {
      $content = FALSE;
    }
  }
  if (!$content) {
    drupal_set_message(t('The pattern file was removed from its stored location in the file system. Trying to load from the database.'), 'error');
    if (empty($pattern->pattern)) {
      drupal_set_message(t('No valid pattern found in the database either.', 'ERR'));
    }
    else {
      $content = _patterns_editor_dump_from_db($pattern->pattern, $format);
    }
    $title = t('Pattern file recovered from the database');
    $description = t('Comments are not stored in the database.') . '</br></br>';
  }
  else {

    // Pattern was updated in the file system. Show what was in the database
    if (patterns_db_is_pattern_updated($pattern)) {
      $db_content = _patterns_editor_dump_from_db($pattern->pattern, $format);
      $form['db'] = array(
        '#type' => 'fieldset',
        '#title' => t('The Pattern file stored in the database'),
        '#prefix' => '<strong>' . t('Warning!! A newer version of this pattern was found in the file system, and both the version stored in the database, and the one just found are displayed here for comparison.') . '</strong><br/>',
        '#description' => t('This is the old version of the pattern found in the database. This is what will be executed if this pattern is run.') . '</br></br>',
        '#collapsibale' => TRUE,
      );
      $form['db']['validation_result_db'] = array(
        '#markup' => '<strong>' . $validation . '</strong>',
        '#prefix' => '<div id="validation_result_db">',
        '#suffix' => '</div>',
      );
      $form['db']['content_db'] = array(
        '#type' => 'textarea',
        '#title' => t('Pattern\'s code'),
        '#rows' => 25,
        '#default_value' => $db_content,
        '#disabled' => 'true',
      );

      // Update the validation string for the new pattern in the fs.
      $title = t('Pattern file as loaded from the file system');
      $validation = 'This pattern has not been validated yet.';
      $description = t('Newer version of the pattern found in the file system.') . '</br></br>';
    }
  }
  $form['fs'] = array(
    '#type' => 'fieldset',
    '#title' => $title,
    '#description' => $description,
    '#suffix' => '</br>',
  );
  $form['fs']['validation_result'] = array(
    '#markup' => '<strong>' . $validation . '</strong>',
    '#prefix' => '<div id="validation_result">',
    '#suffix' => '</div>',
  );
  $form['fs']['content'] = array(
    '#type' => 'textarea',
    '#title' => t('Pattern\'s code'),
    '#rows' => 25,
    '#default_value' => $content,
  );
  $form['fs']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
  );
  $form['fs']['validate'] = array(
    '#prefix' => '<span style=\'margin-right:10px\'>',
    '#markup' => "<a href='#' id='validate_pattern'>" . t('Validate') . "</a>",
    '#suffix' => '</span> ',
  );
  $form['fs']['run'] = array(
    '#prefix' => '<span style=\'margin-right:10px\'>',
    '#markup' => l(t('Run current pattern'), 'admin/patterns/enable/' . $pattern->pid),
    '#suffix' => '</span> ',
  );
  $form['fs']['cancel'] = array(
    '#markup' => l(t('Back'), 'admin/patterns'),
  );
  return $form;
}