Index: htmlarea.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/htmlarea/htmlarea.module,v
retrieving revision 1.92
diff -u -r1.92 htmlarea.module
--- htmlarea.module	7 Oct 2005 13:35:34 -0000	1.92
+++ htmlarea.module	12 Nov 2005 03:25:46 -0000
@@ -135,7 +135,7 @@
  * Implementation of hook_form_alter
  */
 
-function htmlarea_form_alter(&$form) {
+function htmlarea_form_alter($form_id, &$form) {
   global $htmlarea_init, $htmlarea_fields, $htmlarea_codeview;
 
   if (!$form[built]) {
@@ -156,7 +156,7 @@
     }
     if (is_array($form[$key])) {
       foreach (element_children($form[$key]) as $k) {
-        htmlarea_form_alter($form[$key][$k]);
+        htmlarea_form_alter($form_id, $form[$key][$k]);
       }
     }
   }
@@ -287,81 +287,215 @@
 }
 
 function htmlarea_settings() {
-
-  if (variable_get("filter_html", 0)) {
-    $output .= form_item(t("Filter HTML tags"), t("The Filter HTML Tags is Enabled, This conflicts with htmlarea. "). l(t("Please disable"), "admin/system/filters"));
+  if (variable_get('filter_html', 0)) {
+    $form['html_area_filtertags'] = array(
+      '#type' => 'item',
+      '#title' => t('Filter HTML tags'),
+      '#value' => t('The Filter HTML Tags is Enabled, This conflicts with htmlarea. '). l(t('Please disable'), 'admin/system/filters'),
+    );
   }
-  
-  $output.= form_checkbox(t("Default for New Users"), "htmlarea_user_default", true, variable_get("htmlarea_user_default", true), t("Default setting which determines if a new user will be allowed to use htmlarea"));
-  $output.= form_checkbox(t("kill word on paste"), "htmlarea_killwordonpaste", true, variable_get("htmlarea_killwordonpaste", false), t("This option configures htmlarea so that when pasting from word documents will clean up the word html"));
-  $output.= form_checkbox(t("add revision to web page"), "htmlarea_add_rev", TRUE, variable_get("htmlarea_add_rev", FALSE), t("If you have any problems with htmlarea check this box, and the revision will be added to the html. When you submit a bug report please upload the html source from the web browser to enable better support."));
-  $output.= form_checkbox(t("insert linebreaks in toolbar"), "htmlarea_add_linebreak", TRUE, variable_get("htmlarea_add_linebreak", TRUE), t("If you are using the forked version of Xinha from <a href=\"http://xinha.python-hosting.com/wiki/DownloadsPage\">http://xinha.python-hosting.com/wiki/DownloadsPage</a> the linebreaks have been corrected and work as they should. Enabling this will cause problems with the original version, and some plugins."));
-  $output.= form_textfield(t("Page Style"), "htmlarea_bodystyle", variable_get("htmlarea_bodystyle", "body { background-color: #fff; font-family: arial,helvetica,sans-serif; }"), 100, 100, t("Set the style of the htmlarea body"));
-  
-  $group = form_radios(t('Show Textarea on specific pages'), 'htmlarea_visibility', variable_get('htmlarea_visibility', 0), array(t('Show on every page except the listed pages.'), t('Show on only the listed pages.')));
-  $group .= form_textarea(t('Pages'), 'htmlarea_display', variable_get('htmlarea_display',"admin/*"), 40, 5, t("Enter one page per line as Drupal paths. The path to an individual is the Drupal path followed by '.' and the name of the textarea. The '*' character is a wildcard. Example paths are '<em>blog</em>' for the blog page and '<em>blog/*</em>' for every personal blog. '<em>node/*/blog.body</em>' is for the textarea named body on the blog pages. '<em>&lt;front&gt;</em>' is the front page."));
-  $output .= form_group(t('Textarea specific visibility settings'), $group);
-  return $output;
+
+  $form['htmlarea_user_default'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Default for New Users'),
+    '#return_value' => true,
+    '#default_value' => variable_get('htmlarea_user_default', true),
+    '#description' => t('Default setting which determines if a new user will be allowed to use htmlarea'),
+  );
+  $form['htmlarea_killwordonpaste'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('kill word on paste'),
+    '#return_value' => true,
+    '#default_value' => variable_get('htmlarea_killwordonpaste', false),
+    '#description' => t('This option configures htmlarea so that when pasting from word documents will clean up the word html'),
+  );
+  $form['htmlarea_add_rev'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('add revision to web page'),
+    '#return_value' => TRUE,
+    '#default_value' => variable_get('htmlarea_add_rev', FALSE),
+    '#description' => t('If you have any problems with htmlarea check this box, and the revision will be added to the html. When you submit a bug report please upload the html source from the web browser to enable better support.'),
+  );
+  $form['htmlarea_add_linebreak'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('insert linebreaks in toolbar'),
+    '#return_value' => TRUE,
+    '#default_value' => variable_get('htmlarea_add_linebreak',  TRUE),
+    '#description' => t("If you are using the forked version of Xinha from <a href=\"http://xinha.python-hosting.com/wiki/DownloadsPage\">http://xinha.python-hosting.com/wiki/DownloadsPage</a> the linebreaks have been corrected and work as they should. Enabling this will cause problems with the original version, and some plugins."),
+  );
+  $form['htmlarea_bodystyle'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Page Style'),
+    '#default_value' => variable_get('htmlarea_bodystyle', 'body { background-color: #fff; font-family: arial,helvetica,sans-serif; }'),
+    '#size' => 100,
+    '#maxlength' => 100,
+    '#description' => t('Set the style of the htmlarea body'),
+  );
+
+  $form['htmlarea_visgroup'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Textarea specific visibility settings'),
+    '#collapsible' => FALSE,
+    '#collapsed' => FALSE,
+  );
+  $form['htmlarea_visgroup']['htmlarea_visibility'] = array(
+    '#type' => 'radios',
+    '#title' => t('Show Textarea on specific pages'),
+    '#default_value' => variable_get('htmlarea_visibility', 0),
+    '#options' => array(
+      t('Show on every page except the listed pages.'),
+      t('Show on only the listed pages.')),
+  );
+  $form['htmlarea_visgroup']['htmlarea_display'] = array(
+    '#type' => 'textarea',
+    '#title' => t('Pages'),
+    '#default_value' => variable_get('htmlarea_display', 'admin/*'),
+    '#cols' => 40,
+    '#rows' => 5,
+    '#description' => t("Enter one page per line as Drupal paths. The path to an individual is the Drupal path followed by '.' and the name of the textarea. The '*' character is a wildcard. Example paths are '<em>blog</em>' for the blog page and '<em>blog/*</em>' for every personal blog. '<em>node/*/blog.body</em>' is for the textarea named body on the blog pages. '<em>&lt;front&gt;</em>' is the front page."),
+  );
+
+  return $form;
 }
 
 function htmlarea_settings_toolbar() {
-  $edit = $_POST['edit'];
-  $op = $_POST['op'];
-  if($edit) {
-    if ($op == t('Add separator')) {
-      $_POST['edit']['htmlarea_toolbar'][] = array('data' => 'separator', 'weight' => '-1', 'enabled' => '1');
-      $_POST['op'] = t('Save configuration');
-    }
-    elseif ($op == t('Add space')) {
-      $_POST['edit']['htmlarea_toolbar'][] = array('data' => 'space', 'weight' => '-1', 'enabled' => '1');
-      $_POST['op'] = t('Save configuration');
-    }
-    elseif ($op == t('Add linebreak')) {
-      $_POST['edit']['htmlarea_toolbar'][] = array('data' => 'linebreak', 'weight' => '-1', 'enabled' => '1');
-      $_POST['op'] = t('Save configuration');
-    }
-    system_settings_save($edit);
-  }
   $toolbar = _htmlarea_build_toolbar(true);
   usort($toolbar, "_htmlarea_sort");
-  $header = array(array('data' => t('function')), array('data' => t('enabled')), array('data' => t('weight')));
+
+  $form['htmlarea_toolbar'] = array('#tree' => TRUE,);
   foreach ($toolbar as $k => $v) {
-    if (!($v['enabled'] == 0 && ($v['data'] == 'space' ||$v['data'] == 'separator' ||$v['data'] == 'linebreak'))) {
-      $rows[] = array(array('data' => t($v['data']) .form_hidden("htmlarea_toolbar][$k][data", $v['data'])), array('data' => form_checkbox('', "htmlarea_toolbar][$k][enabled", 1, $v['enabled']), 'align' => 'center'), array('data' => form_weight('',"htmlarea_toolbar][$k][weight", $v['weight'], 20)));
+    if (!($v['enabled'] == 0 && ($v['data'] == 'space' || $v['data'] == 'separator' || $v['data'] == 'linebreak'))) {
+      // display name
+      $form['htmlarea_toolbar'][$k]['desc'] = array(
+        '#type' => 'item',
+        '#value' => t($v['data']),
+      );
+      $form['htmlarea_toolbar'][$k]['data'] = array(
+        '#type' => 'value',
+        '#value' => $v['data'],
+      );
+      $form['htmlarea_toolbar'][$k]['enabled'] = array(
+        '#type' => 'checkbox',
+        '#default_value' =>  $v['enabled'],
+        '#return_value' => 1,
+      );
+      $form['htmlarea_toolbar'][$k]['weight'] = array(
+        '#type' => 'weight',
+        '#default_value' =>  $v['weight'],
+        '#delta' => 20,
+      );
+    }
+  }
+  $form['buttons']['separator'] = array('#type' => 'submit', '#value' => t('Add separator') );
+  $form['buttons']['space'] = array('#type' => 'submit', '#value' => t('Add space') );
+  $form['buttons']['linebreak'] = array('#type' => 'submit', '#value' => t('Add linebreak') );
+  $form['buttons']['submit'] = array('#type' => 'submit', '#value' => t('Save configuration') );
+  $form['buttons']['reset'] = array('#type' => 'submit', '#value' => t('Reset to defaults') );
+
+  return drupal_get_form('htmlarea_settings_toolbar', $form, 'system_settings');
+  //return theme("page", $output, t('toolbar'));
+}
+
+function theme_htmlarea_settings_toolbar($form) {
+  $rows = array();
+  foreach (element_children($form['htmlarea_toolbar']) as $key) {
+    $row = array();
+    if (is_array($form['htmlarea_toolbar'][$key]['desc'])) {
+      $row[] = form_render($form['htmlarea_toolbar'][$key]['desc']) . form_render($form['htmlarea_toolbar'][$key]['data']);
+      $row[] = form_render($form['htmlarea_toolbar'][$key]['enabled']);
+      $row[] = form_render($form['htmlarea_toolbar'][$key]['weight']);
     }
+    $rows[] = $row;
   }
-  $output.= theme('table', $header, $rows);
-  $output.= form_submit(t('Add separator'));
-  $output.= form_submit(t('Add space'));
-  $output.= form_submit(t('Add linebreak'));
-  $output = system_settings_form($output);
-  drupal_set_title('toolbar');
+  $header = array(t('function'), t('enabled'), t('weight'));
+
+  $output = theme('table', $header, $rows);
+  $output .= form_render($form);
+
   return $output;
 }
 
-function htmlarea_settings_plugins() {
-  $edit = $_POST['edit'];
-  if($edit) {
-    system_settings_save($edit);
+function htmlarea_settings_toolbar_execute($form_id, $form_values) {
+  $op = $_POST['op'];
+  if ($op == t('Add separator')) {
+    $form_values['htmlarea_toolbar'][] = array('data' => 'separator', 'weight' => '-1', 'enabled' => '1');
+    $form_values['op'] = t('Save configuration');
+  }
+  elseif ($op == t('Add space')) {
+    $form_values['htmlarea_toolbar'][] = array('data' => 'space', 'weight' => '-1', 'enabled' => '1');
+    $form_values['op'] = t('Save configuration');
   }
-// build list of plugins
+  elseif ($op == t('Add linebreak')) {
+    $form_values['htmlarea_toolbar'][] = array('data' => 'linebreak', 'weight' => '-1', 'enabled' => '1');
+    $form_values['op'] = t('Save configuration');
+  }
+
+  system_settings_form_execute($form_id, $form_values);
+  drupal_goto('admin/settings/htmlarea/toolbar');
+}
 
+function htmlarea_settings_plugins() {
+  // build list of plugins
   $files = array_merge(file_scan_directory(_htmlarea_get_jsdir() . '/plugins', '.*', array('.', '..', 'CVS', '.svn'), 0, false), file_scan_directory(drupal_get_path('module', 'htmlarea') . '/plugins', '.*', array('.', '..', 'CVS', '.svn'), 0, false));
   usort($files, '_htmlarea_sort_plugins');
-  $plugins = variable_get("htmlarea_plugins", array());
-  $header = array(t("plugin"), t("enable"));
+  $plugins = variable_get('htmlarea_plugins', array());
+
+  $form['htmlarea_plugins'] = array('#tree' => TRUE,);
   foreach ($files as $f) {
-    $row = array(array("data" => $f->basename. form_hidden("htmlarea_plugins][$f->filename][name", $f->basename), "align" => "left"), array("data" => form_checkbox("", "htmlarea_plugins][$f->filename][enable", 1, $plugins[$f->filename]['enable'])));
-    $rows[] = $row;
-  }
-  if ($rows) {
-    $output.= theme("table", $header, $rows);
+    // display name
+    $form['htmlarea_plugins'][$f->filename]['desc'] = array(
+      '#type' => 'item',
+      '#value' => $f->basename,
+    );
+    // name saved with the enable setting
+    $form['htmlarea_plugins'][$f->filename]['name'] = array(
+      '#type' => 'value',
+      '#value' => $f->basename,
+    );
+    // enable checkbox
+    $form['htmlarea_plugins'][$f->filename]['enable'] = array(
+      '#type' => 'checkbox',
+      '#default_value' => $plugins[$f->filename]['enable'],
+      '#return_value' => 1,
+    );
   }
-  
-  $output = system_settings_form($output);
+  $form['buttons']['submit'] = array('#type' => 'submit', '#value' => t('Save configuration') );
+  $form['buttons']['reset'] = array('#type' => 'submit', '#value' => t('Reset to defaults') );
+
+  $output = drupal_get_form('htmlarea_settings_plugins', $form, 'system_settings');
   print theme("page", $output, t("plugins"));
 }
 
+/**
+ * Format the pluigin settings page into a table
+ */
+function theme_htmlarea_settings_plugins($form) {
+  $rows = array();
+  foreach (element_children($form['htmlarea_plugins']) as $key) {
+    $row = array();
+    if (is_array($form['htmlarea_plugins'][$key]['desc'])) {
+      $row[] = form_render($form['htmlarea_plugins'][$key]['desc']) . form_render($form['htmlarea_plugins'][$key]['name']);
+      $row[] = form_render($form['htmlarea_plugins'][$key]['enable']);
+    }
+    $rows[] = $row;
+  }
+  $header = array(t("plugin"), t("enable"));
+
+  $output = theme('table', $header, $rows);
+  $output .= form_render($form);
+
+  return $output;
+}
+
+/**
+ * Save the plugins settings, we can't use system_settings_form_execute() as the
+ * form callback because it wouldn't call theme_htmlarea_settings_plugins().
+ */
+function htmlarea_settings_plugins_execute($form_id, $form_values) {
+  system_settings_form_execute($form_id, $form_values);
+  drupal_goto('admin/settings/htmlarea/plugins');
+}
+
 function htmlarea_settings_plugins_css() {
   $edit = $_POST['edit'];
   if($edit) {
@@ -415,16 +549,6 @@
 }
 
 function htmlarea_settings_array($arg) {
-  $edit = $_POST['edit'];
-  if($edit) {
-    foreach ($edit["htmlarea_$arg"] as $k => $v) {
-      if ($v['delete'] == '1' || $v['data'] == '') {
-        unset($_POST['edit']["htmlarea_$arg"][$k]);
-      }
-    }
-    system_settings_save($edit);
-  }
-
   switch($arg) {
     case "fontnames":
       $header_data = t('font name');
@@ -443,27 +567,99 @@
       break;
   }
 
-  $header = array(array('data' => $header_data), array('data' => $header_value), array('data' => t('weight')), array('data' => t('delete')));
   $data = variable_get("htmlarea_$arg", _htmlarea_defaults($arg));
   usort($data, "_htmlarea_sort");
+  // add blank last row
+  $data[] = array('data' => '', 'value' => '', 'weight' => '');
+
+  $form['htmlarea_' . $arg] = array('#tree' => TRUE,);
   foreach ($data as $k => $v) {
-    $rows[] = array(array('data' => form_textfield('', "htmlarea_$arg][$k][data", $v['data'], 20, 20)), array('data' => form_textfield('', "htmlarea_$arg][$k][value", $v['value'], 40, 80)), array('data' => form_weight('', "htmlarea_$arg][$k][weight", $v['weight'])), array('data' => form_checkbox('', "htmlarea_$arg][$k][delete", 1, 0), 'align' => 'center'));
+    $form['htmlarea_' . $arg][$k]['data'] = array(
+      '#type' => 'textfield',
+      '#default_value' => $v['data'],
+      '#size' => 20,
+      '#maxlength' => 20,
+    );
+    $form['htmlarea_' . $arg][$k]['value'] = array(
+      '#type' => 'textfield',
+      '#default_value' => $v['value'],
+      '#size' => 20,
+      '#maxlength' => 20,
+    );
+    $form['htmlarea_' . $arg][$k]['weight'] = array(
+      '#type' => 'weight',
+      '#default_value' => $v['weight'],
+    );
+    $form['htmlarea_' . $arg][$k]['delete'] = array(
+      '#type' => 'checkbox',
+      '#return_value' => 1,
+      '#default_value' => 0,
+    );
   }
-  $k = count($rows);
-  $rows[] = array(array('data' => form_textfield('', "htmlarea_$arg][$k][data", '', 20, 20)), array('data' => form_textfield('', "htmlarea_$arg][$k][value", '', 40, 80)), array('data' => form_weight('', "htmlarea_$arg][$k][weight", '')), array('data' => '&nbsp;'));
+  $form['buttons']['submit'] = array('#type' => 'submit', '#value' => t('Save configuration') );
+  $form['buttons']['reset'] = array('#type' => 'submit', '#value' => t('Reset to defaults') );
 
-  $output.= theme("table", $header, $rows);
-  $output = system_settings_form($output);
-  print theme("page", $output, $page_title);
+  $form['args'] = array('#tree' => TRUE);
+  $form['args']['name'] = array(
+    '#type' => 'value',
+    '#value' => $arg,
+  );
+  $form['args']['header'] = array(
+    '#type' => 'value',
+    '#value' => array($header_data, $header_value, t('weight'), t('delete')),
+  );
+
+  $output = drupal_get_form('htmlarea_settings_array', $form);
+  print theme('page', $output, $page_title);
+}
+
+function theme_htmlarea_settings_array(&$form) {
+  if (isset($form['args']['name']['#value'])) {
+    $var = 'htmlarea_' . $form['args']['name']['#value'];
+    $header = $form['args']['header']['#value'];
+
+    foreach (element_children($form[$var]) as $key) {
+      $row = array();
+      if (is_array($form[$var][$key]['data'])) {
+        $row[] = form_render($form[$var][$key]['data']);
+        $row[] = form_render($form[$var][$key]['value']);
+        $row[] = form_render($form[$var][$key]['weight']);
+        $row[] = form_render($form[$var][$key]['delete']);
+      }
+      $rows[] = $row;
+    }
+    $output = theme('table', $header, $rows);
+  }
+  $output .= form_render($form);
+
+  return $output;
 }
 
-function htmlarea_settings_custom() {
-  $edit = $_POST['edit'];
-  if($edit) {
-    system_settings_save($edit);
+function htmlarea_settings_array_execute($form_id, $form_values) {
+  // unset any thing that was deleted
+  $arg = $form_values['args']['name'];
+  foreach ($form_values['htmlarea_' . $arg] as $k => $v) {
+    if ($v['delete'] == '1' || $v['data'] == '') {
+      unset($form_values['htmlarea_' . $arg][$k]);
+    }
   }
-  $output.= form_textarea(t('custom javascript'), 'htmlarea_customjs', variable_get('htmlarea_customjs', ''), 80, 15, t('Enter any javascript that you wish to include in the attacheditor() before the htmlarea is generated.'));
-  $output = system_settings_form($output);
+  // remove our temp values
+  unset($form_values['args']);
+
+  system_settings_form_execute($form_id, $form_values);
+  drupal_goto('admin/settings/htmlarea/' . $arg);
+}
+
+function htmlarea_settings_custom() {
+  $form['htmlarea_customjs'] = array(
+    '#type' => 'textarea',
+    '#title' => t('custom javascript'),
+    '#default_value' => variable_get('htmlarea_customjs', ''),
+    '#cols' => 80,
+    '#rows' => 15,
+    '#description' => t('Enter any javascript that you wish to include in the attacheditor() before the htmlarea is generated.'),
+  );
+  $output = system_settings_form('htmlarea_settings_custom', $form);
   print theme("page", $output, t("custom javascript"));
 }
 

