Index: tinymce.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/tinymce/tinymce.module,v
retrieving revision 1.42
diff -u -r1.42 tinymce.module
--- tinymce.module	14 Nov 2005 16:32:15 -0000	1.42
+++ tinymce.module	22 Nov 2005 16:08:41 -0000
@@ -93,32 +93,45 @@
 }
 
 /**
- * Implementation of hook_textarea().
+ * Implementation of hook_elements().
  */
-function tinymce_textarea($op, $textarea_name) {
+function tinymce_elements() {
+  $type['textarea'] = array('#process' => 'tinymce_process_textarea');
+  return $type;
+}
+
+/**
+ * Attach tinymce to a textarea
+ */
+function tinymce_process_textarea($element) {
   static $is_running = FALSE;
   if (!user_access('access tinymce')) return NULL;
 
   global $user;
   global $base_url;
   static $profile_name;
+  
+  //$element is an array of attributes for the textarea but there is no just 'name' value, so we extract this from the #id field
+  $textarea_name = substr($element['#id'], strpos($element['#id'], '-') + 1);
 
   // Since tinymce_config() makes a db hit, only call it when we're pretty sure
   // we're gonna render tinymce.
-  $valid_so_far = FALSE;
-  if ($op == 'post') {
-    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 = tinymce_profile_load($profile_name);
-    $init = tinymce_config($profile);
-    $init['elements'] = 'edit['. $textarea_name .']';
-    $valid_so_far = TRUE;
+  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 = tinymce_profile_load($profile_name);
+  $init = tinymce_config($profile);
+  $init['elements'] = 'edit['. $textarea_name .']';
 
-  if ($valid_so_far && _tinymce_page_match($profile)) {
+  if (_tinymce_page_match($profile)) {
     // Merge user-defined TinyMCE settings.
     $init = (array) theme('tinymce_theme', $init, $textarea_name, $init['theme'], $is_running);
+
+    // If $init array is empty no need to execute rest of code since there are no textareas to theme with TinyMCE
+    if (count($init) < 1) {
+      return $element;
+    }
+    
     $settings = array();
     foreach ($init as $k => $v) {
       $v = is_array($v) ? implode(',', $v) : $v;
@@ -218,9 +231,12 @@
     }
     // Load a TinyMCE init for each textarea.
     if ($init) drupal_set_html_head($tinymce_invoke);
-
-  return $wysiwyg_link;
+        
+    // Make sure to append to #suffix so it isn't completely overwritten
+    $element['#suffix'] .= $wysiwyg_link;
   }
+
+  return $element;
 }
 
 /**
@@ -270,7 +286,7 @@
     case 'img_assist_pages':
     case 'caption': // signature
     case 'pages':
-    case 'settings][access_pages': //TinyMCE profile settings.
+    case 'access_pages': //TinyMCE profile settings.
       unset($init);
       break;
 
@@ -278,7 +294,7 @@
     case 'signature':
     case 'site_mission':
     case 'site_footer':
-    case 'settings][access_pages':
+    case 'access_pages':
       $init['theme'] = 'simple';
       foreach ($init as $k => $v) {
         if (strstr($k, 'theme_advanced_')) unset($init[$k]);
@@ -456,8 +472,6 @@
 
   // Build a default list of TinyMCE settings.
 
-  //dprint_r($settings);
-
   // Is tinymce on by default?
   $status = isset($user->tinymce_status) ? $user->tinymce_status : variable_get('tinymce_default_state', 0);
   $init['mode']               = $status == 1 ? 'exact' : 'none';
@@ -475,7 +489,7 @@
     $init['theme_advanced_resizing']          = $settings['resizing'] ? $settings['resizing'] : 'false';
     $init['theme_advanced_blockformats']      = $settings['block_formats'] ? $settings['block_formats'] : 'p,address,pre,h1,h2,h3,h4,h5,h6';
 
-    if (is_array($settings['plugins'])) {
+    if (is_array($settings['buttons'])) {
       // This gives us the $plugins variable.
       $plugins = _tinymce_get_buttons();
 
@@ -485,7 +499,7 @@
       foreach ($plugins as $rname => $rplugin) { // Plugin name
         foreach ($rplugin as $mce_key => $mce_value) { // TinyMCE key
           foreach ($mce_value as $k => $v) { // Buttons
-            if ($settings['plugins'][$rname][$v]) {
+            if ($settings['buttons'][$rname . '-' . $v]) {
               if (!in_array($rname, $plugin_tracker)) $plugin_tracker[] = $rname;
               $init[$mce_key][] = $v;
             }
@@ -571,7 +585,10 @@
     while ($data = db_fetch_object($result)) {
       unset($roles[$data->rid]);
     }
-    if (!$roles) {
+    if (!$orig_roles) {
+      drupal_set_message(t('You must assign at least one role with the \'access tinymce\' permission before creating a profile.'), 'error');
+    }
+    else if (!$roles) {
       drupal_set_message(t('You will not be allowed to create a new profile since all user roles have already been assigned profiles. Either remove an existing tinymce profile from at least one role or assign another role the \'access tinymce\' permission.'), 'error');
     }
     else if (count($orig_roles) != count($roles)) {
@@ -591,7 +608,7 @@
   $form['basic']['access'] = array('#type' => 'radios', '#title' => t('Make tinymce visible on'), '#default_value' => $edit->settings['access'], '#options' => array(t('all pages'), t('specific pages')));
   $form['basic']['access_pages'] = array('#type' => 'textarea', '#title' => t('Specific pages'), '#default_value' => $edit->settings['access_pages'] ? $edit->settings['access_pages'] : tinymce_help('admin/settings/tinymce#pages'), '#cols' => 40, '#rows' => 5, '#description' => t("Enter one page per line as Drupal paths. The '*' character is a wildcard. Example paths are '<em>blog</em>' for the blog page and '<em>blog/*</em>' for every personal blog. '<em>&lt;front&gt;</em>' is the front page."));
 
-  $form['buttons'] = array('#type' => 'fieldset', '#title' => t('Buttons'), '#collapsible' => TRUE, '#collapsed' => TRUE, '#theme' => 'tinymce_profile_form_buttons');
+  $form['buttons'] = array('#type' => 'fieldset', '#title' => t('Buttons'), '#collapsible' => TRUE, '#collapsed' => TRUE, '#tree' => TRUE, '#theme' => 'tinymce_profile_form_buttons');
   
   $metadata = _tinymce_get_buttons(FALSE);
   // Generate the button list.
@@ -600,11 +617,16 @@
       foreach ($meta['buttons'] as $button) {
         if ($name != 'default') {
           $img_src = drupal_get_path('module', 'tinymce'). "/tinymce/jscripts/tiny_mce/plugins/$name/images/$name.gif";
+          
+          //correct for plugins that have more than one button
+          if (!file_exists($img_src)) {
+            $img_src = drupal_get_path('module', 'tinymce'). "/tinymce/jscripts/tiny_mce/plugins/$name/images/$button.gif";
+          }
         }
         else {
           $img_src = drupal_get_path('module', 'tinymce'). "/tinymce/jscripts/tiny_mce/themes/advanced/images/$button.gif";
         }
-        $b = file_exists($img_src) ? '<img src="'. $img_src .'" title="'. $button .'" style="border: 1px solid grey; align: baseline;" />' : $button;
+        $b = file_exists($img_src) ? '<img src="'. $img_src .'" title="'. $button .'" style="border: 1px solid grey; vertical-align: middle;" />' : $button;
 
         if ($name == 'default') {
           $title = $b;
@@ -614,11 +636,10 @@
           if ($metadata[$name]['infourl']) {
             $title = '<a href="'. $metadata[$name]['infourl'] .'" target="_blank">'. $title .'</a>';
           }
-          $title .= ' - '. $b;
+          $title = $b . ' &#8211; '. $title;
         }
-
-        $form_value = $edit->settings[$name. $button];
-        $form['buttons'][$name . $button] = array('#type' => 'checkbox', '#title' => $title, '#default_value' => $form_value, '#description' => $description);
+        $form_value = $edit->settings['buttons'][$name . '-' . $button];
+        $form['buttons'][$name . '-' . $button] = array('#type' => 'checkbox', '#title' => $title, '#default_value' => $form_value, '#description' => $description);
       }
     }
     else {
@@ -626,11 +647,11 @@
       if ($metadata[$name]['infourl']) {
         $title = '<a href="'. $metadata[$name]['infourl'] .'" target="_blank">'. $title .'</a>';
       }
-      $form_value = $edit->settings[$name];
+      $form_value = $edit->settings['buttons'][$name];
       $form['buttons'][$name] = array('#type' => 'checkbox', '#title' => $title, '#default_value' => $form_value, '#description' => $description);
     }
   }
-  
+
   $form['appearance'] = array('#type' => 'fieldset', '#title' => t('Editor appearance'), '#collapsible' => TRUE, '#collapsed' => TRUE);
   $form['appearance']['toolbar_loc'] = array('#type' => 'select', '#title' => t('Toolbar location'), '#default_value' => $edit->settings['toolbar_loc'], '#options' => array('bottom' => 'bottom', 'top' => 'top'), '#description' => t('Show toolbar at the top or bottom of the editor area?'));
   $form['appearance']['toolbar_align'] = array('#type' => 'select', '#title' => t('Toolbar alignment'), '#default_value' => $edit->settings['toolbar_align'], '#options' => array('center' => 'center', 'left' => 'left', 'right' => 'right'), '#description' => t('Align tool icons left, center, or right within the toolbar.'));
@@ -666,17 +687,18 @@
     $buttons[] = form_render($form[$key]);
   }
 
-  // Split the checkboxes into 2 columns for a table view
-  $split = ceil(count($buttons) / 2);
-  $col[] = array_slice($buttons, 0, $split);
-  $col[] = array_slice($buttons, $split);
-
-  foreach ($col as $c) {
-    $r[] = array('data' => implode('<br />', $c), 'valign' => 'top');
+  //split checkboxes into rows with 3 columns
+  $total = count($buttons);
+  $rows = array();
+  for ($i = 0; $i < $total; $i++) {
+    $row = array();
+    $row[] = array('data' => $buttons[$i]);
+    $row[] = array('data' => $buttons[++$i]);
+    $row[] = array('data' => $buttons[++$i]);
+    $rows[] = $row;
   }
-  $row[] = $r;
-    
-  $output = theme('table', array(), $row, array('width' => '100%'));
+
+  $output = theme('table', array(), $rows, array('width' => '100%'));
 
   return $output;
 }
