? rework.patch
Index: README.txt
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/wysiwyg_template/README.txt,v
retrieving revision 1.3
diff -u -p -r1.3 README.txt
--- README.txt	10 Jun 2009 19:53:53 -0000	1.3
+++ README.txt	31 Oct 2010 00:36:38 -0000
@@ -19,27 +19,22 @@ Bug reports, feature suggestions and lat
 
 * Install as usual, see http://drupal.org/node/70151 for further information.
 
-* Go to Administer > Site configuration > Wysiwyg Template, and verify the values
-  for your path and file name of your template registry file  (this file will 
-  hold the names and locations of your templates)
-
-* Create the template registry file in the specified location, its contents should
-  look something like this: 
-  
-  // These templates will be displayed as a dropdown
-  var tinyMCETemplateList = [
-  // Name, URL, Description
-  ["Template One", "sites/all/files/templates/template1.html", "Template One"],
-  ["Template Two", "sites/all/files/templates/template2.html", "Template Two"]
-  ];
-  
-*  Create your template files at the locations specified in your registry file
-
--- CONFIGURATION --
-
 * Go to Administer > Site configuration > Wysiwyg and
 
   - click "edit" to set up your editor profile
 
   - under "Buttons and Plugins" you'll see "Insert Templates" as a new option
 
+* Administer > Site configuration > Wysiwyg > Wysiwyg Templates to manage templates.
+
+
+-- EDITOR COMPATIBILITY --
+
+Supported editors:
+
+ - FCK Editor 2.66
+ 
+ - CK Editor 3.4.0
+ 
+ - Tiny MCE 3.3.8
+
Index: wysiwyg_template.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/wysiwyg_template/wysiwyg_template.module,v
retrieving revision 1.4
diff -u -p -r1.4 wysiwyg_template.module
--- wysiwyg_template.module	10 Jun 2009 21:50:19 -0000	1.4
+++ wysiwyg_template.module	31 Oct 2010 00:36:38 -0000
@@ -1,5 +1,4 @@
 <?php
-// $Id: wysiwyg_template.module,v 1.4 2009/06/10 21:50:19 jenlampton Exp $
 
 /**
  * @file
@@ -7,79 +6,284 @@
  * Wysiwyg API.
  */
 
+ 
+ /**
+ * Implementation of hook_menu().
+ */
+function wysiwyg_template_menu() {
+  $items = array();
+  
+  // template overview settings page
+  $items['admin/settings/wysiwyg/templates'] = array(
+	  'title' => 'Templates',
+	  'description' => 'Create and modify Wysiwyg Templates',
+	  'page callback' => 'wysiwyg_template_overview',
+	  'type' => MENU_LOCAL_TASK,
+	  'access arguments' => array('administer filters'),
+	  'file' => 'wysiwyg_template.admin.inc',
+  );
+  // add template
+  $items['admin/settings/wysiwyg/templates/add'] = array(
+    'title' => 'Add Template',
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('wysiwyg_template_template_form'),
+    'access arguments' => array('administer filters'),
+	  'type' => MENU_CALLBACK,
+	  'file' => 'wysiwyg_template.admin.inc',
+  );
+  // edit template
+  $items['admin/settings/wysiwyg/templates/edit/%wysiwyg_template'] = array(
+    'title' => 'Edit Wysiwyg Template',
+    'page callback' => 'drupal_get_form',
+	  'page arguments' => array('wysiwyg_template_template_form', 5),
+    'access arguments' => array('administer filters'),
+    'type' => MENU_CALLBACK,
+    'parent' => 'admin/settings/wysiwyg/templates',
+	  'file' => 'wysiwyg_template.admin.inc',
+  );
+  //delete template
+  $items['admin/settings/wysiwyg/templates/delete'] = array(
+      'title' => 'Delete Wysiwyg template',
+      'page callback' => 'drupal_get_form',
+      'page arguments' => array('wysiwyg_template_delete_confirm'),
+      'access arguments' => array('administer filters'),
+      'file' => 'wysiwyg_template.admin.inc',
+      'type' => MENU_CALLBACK,
+  );
+  // javascript template list
+  $items['wysiwyg-templates/%/list'] = array(
+	'page callback' => 'wysiwyg_template_list_js',
+	'page arguments' => array(1),
+	'access arguments' => array('access content'),
+	'type' => MENU_CALLBACK,
+  );
+  // individual template html - referenced by javascript above
+  $items['wysiwyg-templates/%/load/%wysiwyg_template_html'] = array(
+	'page callback' => 'wysiwyg_template_html_print',
+	'page arguments' => array(3, 1),
+	'access arguments' => array('access content'),
+	'type' => MENU_CALLBACK,
+  );
+  return $items;
+}
+
+/**
+ * Wysiwyg template Wildcard menu loader - prepare item for the create/modify admin form
+ * 
+ * @ tid - template ID
+ *
+ * return $template - the loaded template from the db
+ */
+function wysiwyg_template_load($tid){
+	$template = wysiwyg_template_load_template($tid);
+	return $template;
+} 
+
+//how too look at each editor as a plugin (inc)
+//three hooks:
+//config
+//template definition
+//template loading
+//do two first, then generalise
 /**
  * Implementation of hook_wysiwyg_plugin().
  */
-function wysiwyg_template_wysiwyg_plugin($editor, $version) {
-  static $integrated = array();
+function wysiwyg_template_wysiwyg_plugin($editorName, $version) {
+	switch ($editorName) {
+		case 'tinymce':
+			return array(
+				'template' => array(
+					'buttons' => array('template' => t('Insert templates')),
+					'url' => 'admin/content/wysiwyg-template',
+					'internal' => TRUE,
+					'options' => array(
+						'template_external_list_url' => url('wysiwyg-templates/' . $editorName . '/list'), //load the javascript template list
+						'template_selected_content_classes' => variable_get('wysiwyg_template_selected_content_classes', ''),
+						//'template_replace_values' => array('caption' => 'Replace with this content')
+					),
+					'extended_valid_elements' => array( //tinymce specific - allows additional elements to be used
+					  'img[class|src|border=0|alt|title|width|height|align|name|style]',
+					),
+					'load' => TRUE,
+				),
+			);
+		break;
+		case 'ckeditor':
+			return array(
+				'templates' => array(
+					'buttons' => array('Templates' => t('Insert templates')),
+					'url' => 'admin/content/wysiwyg-template',
+					'internal' => TRUE,
+					'options' => array(
+						'templates_files' => array(url('wysiwyg-templates/' . $editorName . '/list')), //load the javascript template list
+					),
+					'load' => TRUE,
+				),
+			);
+		break;
+		case 'fckeditor':
+			return array(
+				'templates' => array(
+					'buttons' => array('Templates' => t('Insert templates')),
+					'url' => 'admin/content/wysiwyg-template',
+					'internal' => TRUE,
+					'options' => array(
+						'TemplatesXmlPath' => array(url('wysiwyg-templates/' . $editorName . '/list')), //load the javascript template list
+					),
+					'load' => TRUE,
+				),
+			);
+		break;
+	}
+}
 
-  switch ($editor) {
-    case 'tinymce':
-      // get values defined on settings page
-      $jspath = variable_get('wysiwyg_template_path', 'sites/all/files');
-      $jsfile = variable_get('wysiwyg_template_file', 'templates.js');
-      // include path to js file
-      /*
-      $settings = array(
-        'template' => array('template_external_list_url' => base_path() . $jspath . '/' . $jsfile),
-      );
-      drupal_add_js($settings, 'setting');
-      */
-      $tinymce = wysiwyg_get_editor($editor);
-      return array(
-        'template' => array(
-          'path' => $tinymce['library path'] .'/plugins/template',
-          'buttons' => array('template' => t('Insert templates')),
-          'url' => 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/template',
-          'internal' => TRUE,
-          'options' => array(
-            'template_external_list_url' => base_path() . $jspath . '/' . $jsfile,
-          ),
-          'load' => TRUE,
-        ),
-      );
-  }
+/**
+ * Generate javascript for template loading
+ */
+function wysiwyg_template_list_js($editorName){
+	//don't cache templates
+	drupal_set_header("CacheControl: no-cache");
+	drupal_set_header("Expires: -1");
+	drupal_set_header("Content-Type:text/javascript; charset=UTF-8");
+	$templates = wysiwyg_template_load_all();
+	switch($editorName){
+		case 'tinymce':
+			print 'var tinyMCETemplateList = ';
+			$outArray = array();
+			foreach($templates as $template){
+				$outArray[] = array(
+					$template['title'],
+					url('wysiwyg-templates/' . $editorName . '/load/') . $template['tid'],
+					$template['description'],
+				);
+			}
+			print json_encode($outArray) . ";";
+		break;
+		case 'ckeditor':
+			print "CKEDITOR.addTemplates( 'default', { templates:";
+			//todo: include an image with templates...
+			//imagesPath : CKEDITOR.getUrl( CKEDITOR.plugins.getPath( 'templates' ) + 'templates/images/' ),
+			// load the templates into the json array structure
+			foreach($templates as &$template){
+				$template['html'] = $template['body'];
+				unset($template['body']);
+				unset($template['tid']);
+			}
+			print json_encode($templates);
+			print "});";
+		break;
+		case 'fckeditor':
+			print '<?xml version="1.0" encoding="utf-8" ?>';
+			print '<Templates imagesBasePath="">';
+			foreach($templates as $template){
+				print '<Template title="' . check_plain($template['title']) . '" image="">';
+				print '<Description>' . check_plain($template['description']) . '</Description>';
+				print '<Html><![CDATA[';
+				print $template['body'];
+				print ']]></Html></Template>';
+			}
+ 			print '</Templates>';
+		break;
+		case '':
+		break;
+	}
 }
 
 /**
- * Implementation of hook_menu().
+ * Menu callback to display the loaded template
  */
-function wysiwyg_template_menu() {
-  $items = array();
+function wysiwyg_template_html_print($body, $editorName){
+	//don't cache templates
+	drupal_set_header("CacheControl: no-cache");
+	drupal_set_header("Expires: -1");
+	drupal_set_header("Content-Type:text/html; charset=utf-8");
+	switch($editorName){
+		case 'tinymce':
+			print $body;
+		break;
+	}
+}
 
-  $items['admin/settings/wysiwyg_template'] = array(
-    'title' => 'Wysiwyg Templates',
-    'description' => 'Wysiwyg Template plugin settings',
-    'page callback' => 'drupal_get_form',
-    'page arguments' => array('wysiwyg_template_admin_settings'),
-    'access arguments' => array('administer filters'),
-  );
+/**
+ * Load a specific template for viewing, as called through the menu wildcard
+ */
+function wysiwyg_template_html_load($tid){
+	$template = wysiwyg_template_load_template($tid);
+	return $template['body'];
+}
 
-  return $items;
+/**
+ * Wysiwyg template database load function
+ */
+function wysiwyg_template_load_template($tid){
+	$templateResults = db_query("SELECT * FROM {wysiwyg_templates} WHERE tid = %d", $tid);
+	$template = db_fetch_object($templateResults);
+	$tItem = null;
+	if($template != null){
+		$tItem = array(
+			'title' => $template->title,
+			'description' => $template->description,
+			'body' => $template->body,
+			'tid' => $tid,
+		);
+	}
+	
+	return $tItem;
 }
 
 /**
- * Implementation of hook_menu().
+ * Wysiwyg load all templates from the database
  */
-function wysiwyg_template_admin_settings() {
-  $form = array();
-  
-  $form['wysiwyg_template_path'] = array(
-    '#type' => 'textfield',
-    '#title' => t('Path to your js file'),
-    '#default_value' => variable_get('wysiwyg_template_path', 'sites/all/files'),
-    '#description' => t('defaults to sites/all/files'),
-  );
-  $form['wysiwyg_template_file'] = array(
-    '#type' => 'textfield',
-    '#title' => t('Name of your js file'),
-    '#default_value' => variable_get('wysiwyg_template_file', 'templates.js'),
-    '#description' => t('defaults to templates.js'),
-  );
-  $form['info'] = array(
-    '#title' => t('Next Steps'),
-    '#value' => '<p>'.t('Create a file called ').variable_get('wysiwyg_template_file', 'template.js').t('(from above) and upload it to ').variable_get('wysiwyg_template_path', 'sites/all/files').t('(from above).  The file should define the names and locations of the HTML templates you would like to use in your WYSIWYG editor.  You may copy and paste the syntax from the README.txt file.').'</p>',
-  );
-  
-  return system_settings_form($form);
+function wysiwyg_template_load_all(){
+	$templateResults = db_query("SELECT * FROM {wysiwyg_templates}");
+	$templates = array();
+	while ($dbTemplate = db_fetch_object($templateResults)) {
+		$template = array(
+			'title' => $dbTemplate->title,
+			'description' => $dbTemplate->description,
+			'body' => $dbTemplate->body,
+			'tid' => $dbTemplate->tid,
+		);
+		$templates[] = $template;
+	}
+	return $templates;
+}
+
+/**
+ * Wysiwyg template database save function
+ */
+function wysiwyg_template_save_template($template){
+	$tItem = array(
+		'title' => $template['title'],
+		'description' => $template['description'],
+		'body' => $template['body'],
+	);
+	
+	//if it's an existing item we're updating
+	$key = null;
+	if($template['tid']){
+		$key = 'tid';
+		$tItem['tid'] = $template['tid'];
+	}
+	
+	//save to database
+	return drupal_write_record('wysiwyg_templates', &$tItem, $key);
+}
+
+/**
+ * Definition needed TODO
+ */
+function wysiwyg_template_delete_template($tid){
+	db_query("DELETE FROM {wysiwyg_templates} WHERE tid = %s", $tid);
+}
+
+/**
+ * Implementation of hook_help.
+ */
+function wysiwyg_template_help($path, $arg) {
+  switch ($path) {
+    case 'admin/settings/wysiwyg/templates':
+      $output = t('<p>The Wysiwyg Template module allows you to create templates to be used with a Wysiwyg editor of your choice. The template button for the editor will need to be enabled from the !profiles settings. The currently supported editors are FCK Editor, CK Editor and TinyMCE.</p>', array('!profiles', l(t('WYSIWYG Profile'), 'admin/settings/filters')));
+      return $output;
+  }
 }
