--- tinymce.module	2007-08-01 00:40:36.000000000 +1000
+++ tinymce.module.profile_priority	2009-01-04 19:40:41.000000000 +1100
@@ -84,7 +84,17 @@ function tinymce_process_textarea($eleme
   // Since tinymce_config() makes a db hit, only call it when we're pretty sure
   // we're gonna render tinymce.
   if (!$profile_name) {
-    $profile_name = db_result(db_query('SELECT s.name FROM {tinymce_settings} s INNER JOIN {tinymce_role} r ON r.name = s.name WHERE r.rid IN (%s)', implode(',', array_keys($user->roles))));
+    $profile_name = db_result(db_query("
+        SELECT name
+        FROM {tinymce_settings}
+        WHERE priority = (
+          SELECT MIN(s.priority)
+          FROM {tinymce_settings} s
+          INNER JOIN {tinymce_role} r
+          ON r.name = s.name
+          WHERE r.rid IN (%s)
+        )
+      ", implode(',', array_keys($user->roles))));
     if (!$profile_name) {
       return $element;
     }
@@ -1020,7 +1030,7 @@ function tinymce_profile_load($name = ''
 
   if (!$profiles) {
     $roles = user_roles();
-    $result = db_query('SELECT * FROM {tinymce_settings}');
+    $result = db_query('SELECT * FROM {tinymce_settings} ORDER BY priority');
     while ($data = db_fetch_object($result)) {
       $data->settings = unserialize($data->settings);
       $result2 = db_query("SELECT rid FROM {tinymce_role} WHERE name = '%s'", $data->name);
@@ -1041,22 +1051,79 @@ function tinymce_profile_load($name = ''
  * Controller for tinymce profiles.
  */
 function tinymce_profile_overview() {
-  $output = '';
-
   $profiles = tinymce_profile_load();
-  if ($profiles) {
-    $roles = user_roles();
-    $header = array(t('Profile'), t('Roles'), t('Operations'));
-    foreach ($profiles as $p) {
-      $rows[] = array(array('data' => $p->name, 'valign' => 'top'), array('data' => implode("<br />\n", $p->rids)), array('data' => l(t('edit'), 'admin/settings/tinymce/edit/'. urlencode($p->name)) . ' '. l(t('delete'), 'admin/settings/tinymce/delete/'. urlencode($p->name)), 'valign' => 'top'));
-    }
-    $output .= theme('table', $header, $rows);
+  if (!empty($profiles)) {
+    $output = drupal_get_form('tinymce_form_profile_overview', $profiles);
     $output .= t('<p><a href="!create-profile-url">Create new profile</a></p>', array('!create-profile-url' => url('admin/settings/tinymce/add')));
   }
   else {
+    $output = '';
     drupal_set_message(t('No profiles found. Click here to <a href="!create-profile-url">create a new profile</a>.', array('!create-profile-url' => url('admin/settings/tinymce/add'))));
   }
+  return $output;
+}
 
+function tinymce_form_profile_overview($profiles) {
+  $form = array();
+  $count = count($profiles);
+  $option = 0;
+  while ($option < $count) {
+    $options[] = ++$option;
+  }
+  foreach ($profiles as $p) {
+    $form['profile']['#tree'] = TRUE;
+    $form['profile'][$p->name] = array(
+      '#type' => 'select',
+      '#options' => $options,
+      '#default_value' => db_result(db_query("SELECT priority FROM {tinymce_settings} WHERE name = '%s'", $p->name)),
+    );
+  }
+  $form['submit'] = array(
+    '#type' => 'submit',
+    '#value' => t('Sort'),
+  );
+  return $form;
+}
+
+function tinymce_form_profile_overview_validate($form_id, $form_values) {
+  foreach ($form_values['profile'] as $profile => $priority) {
+    if (!is_numeric($priority)) {
+      form_set_error($form_values['profile'][$profile], t('Priority must be a numeric value.'));
+    }
+  }
+}
+
+function tinymce_form_profile_overview_submit($form_id, $form_values) {
+  foreach ($form_values['profile'] as $profile => $priority) {
+    db_query("UPDATE {tinymce_settings} SET priority = %d WHERE name = '%s'", $priority, $profile);
+  }
+}
+
+function theme_tinymce_form_profile_overview($form) {
+  $header = array(t('Priority'), t('Profile'), t('Roles'), t('Operations'));
+  $rows = array();
+  $profiles = tinymce_profile_load();
+  foreach ($profiles as $p) {
+    $rows[] = array(
+      array(
+        'data' => drupal_render($form['profile'][$p->name]),
+        'valign' => 'top',
+      ),
+      array(
+        'data' => $p->name,
+        'valign' => 'top',
+      ),
+      array(
+        'data' => implode("<br />\n", $p->rids),
+      ),
+      array(
+        'data' => l(t('edit'), 'admin/settings/tinymce/edit/'. urlencode($p->name)) .' '. l(t('delete'), 'admin/settings/tinymce/delete/'. urlencode($p->name)),
+        'valign' => 'top',
+      ),
+    );
+  }
+  $output = theme('table', $header, $rows);
+  $output .= drupal_render($form);
   return $output;
 }
 
@@ -1141,7 +1208,17 @@ function _tinymce_page_match($edit) {
 }
 
 function tinymce_user_get_profile($account) {
-  $profile_name = db_result(db_query('SELECT s.name FROM {tinymce_settings} s INNER JOIN {tinymce_role} r ON r.name = s.name WHERE r.rid IN (%s)', implode(',', array_keys($account->roles))));
+  $profile_name = db_result(db_query("
+    SELECT name
+    FROM {tinymce_settings}
+    WHERE priority = (
+      SELECT MIN(s.priority)
+      FROM {tinymce_settings} s
+      INNER JOIN {tinymce_role} r
+      ON r.name = s.name
+      WHERE r.rid IN (%s)
+    )
+  ", implode(',', array_keys($account->roles))));
   if ($profile_name){
     return tinymce_profile_load($profile_name);
   }
