Hopefully by now everyone is on board Edmund's new method of saving forms rather than nodes (5.2).

Now that this is so close to working i thought i would go down the path of adding support for TinyMCE - here's where i am at so far.

When AS is saving nodes it correctly saves the content from any Tiny enabled fields on the form.. yeaaay!!!

but it doesn't "recover" them when i return to saved node and VIEW.

From what i gather the fields are actually being updated but these are the hidden textarea fields that are behind the tiny iframe that would get enabled if tiny was disabled. We need to have jquery.field.js update both fields (the visible Tiny iframe one as well as the hidden one).

The place where this needs to be done is around line 242 or this code:

	case "text":
		this.value = v.join(defaults.delimiter);
	break;

and, sadly i am not a jq expert but if someone could tell me how to access the innerHTML of body within "this" iframe with class=mceEditorIframe then i think we be about done.. :)

Comments

edmund.kwok’s picture

Try looking at updateContent() here http://wiki.moxiecode.com/index.php/TinyMCE:Functions

liquidcms’s picture

eddie.. you da man!! works like a charm.. hopefully i'll wrap this up int he next hour and post a new ZIP

liquidcms’s picture

StatusFileSize
new18.38 KB

and here it is.. version 5.2 of autosave (single form per page only) with host of new features including support for TinyMCE.

- i likely should have some code to check if field is Tiny enabled.. but i left it off and non-tniy textareas still function ok.
- i should test with IE (all my testing is with FF)

sorry it's a zip and not patch..

i think outstanding issues to tie Edmund's branch and my "branch" together are:

- need generic solution for single or mutliforms per page
- need agreement on how to select which forms get autosaved; my solution only uses AS for node forms and therefore adds a node config setting to select if using autosave - possibly something like ajaxsubmit uses could be used - it adds cool admin UI that pops up on every form and asks if you want to add that one to the list of forms it knows about to enable for AS.
- anything else??

still toying with idea of adding AS period as user profile definable setting.

edmund.kwok’s picture

Great! I'll look at the differences and commit the change soon, thanks!

Btw, regarding the interface for autosave configuration UI, did you try the one that I was toying around with? To see it in action, browse to any page with form(s) and append /configure-autosave to the url. For example, admin/content/taxonomy/add/vocabulary/configure-autosave

liquidcms’s picture

I was wondering what the configure_autosave was for.

So this ZIP includes version which fixes 2 bugs (almost).

- removes unsetting form_token so that form validates on sumbit
- adds nodeapi submit hook to remove AS table entry when node is submitted

bug #2 is only partially fixed with simple submit hook that i have added. There still exists issue of ajax firing off save near the same time user hits submit. Therefore submit hook will delete entry but action will already have been fired to save another copy - if it comes in after submit hook does the delete it will simply be recreated.

Likely need some sort of semaphore type lockout to prevent this properly - possibly a simple test if post[op]=submit is set when doing ajax submit would suffice? I'll test this out tomorrow.

liquidcms’s picture

StatusFileSize
new19.07 KB

hmm.. for some reason the ZIP didn't attach..

liquidcms’s picture

One more issue which i have not tested in much depth - i don't think my integration with Tiny works for IE. I think it does the save ok, just not the VIEW (pulling saved content back into Tiny fields).

liquidcms’s picture

StatusFileSize
new550 bytes

there is/was also an issue with the Tiny fields not being reset when RESET is selected. This patches fixes that. Patch is on my ZIP'ed code in #6.

Still have outstanding IE issues - thats next.

liquidcms’s picture

an update on where i am at with this:

ADDED:

- i added this code to end of autosave.module to do the AS table clenup when a node is submitted:

/**
 * Implementation of hook_nodeapi().
 * 
 * Delete autosave table entry on successful submit of node
 *
 */
function autosave_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  if ($op == 'submit') {
      global $user;
      db_query("DELETE FROM {autosaved_forms} WHERE form_id = '%s' AND path = '%s' AND uid = %d", $node->form_id, $_GET['q'], $user->uid);
  }
}

but then realized their is an ajaxy issue that the submit could fire off around the time the ajax autosave routine fires andif it comes in after submit hook cleans the table - the entry will be replaced.

so i changed that function to this (somewhat cludgy but likely very reliable) way to ensure autosave routine doesn't add an entry if node has "just been saved:

/**
 * Menu callback; autosaves the node.
 */
function autosave_save() {
  global $user;  
  $path = $_POST['q'];
  $form_id = $_POST['form_id'];
  // Not all variables need to be serialized.
  //unset($_POST['form_token'], $_POST['q']);    
  unset($_POST['q']);  
  $serialized = serialize($_POST);
  
  // check if node has just been saved - if it has then it's because AS ajax fired off as user was submitting
  // if it had just been submitted - no need to AS now
  $path_args = explode("/", $path);
  $submitted = node_load($path_args[1]);
  
  if ((time() - $submitted->changed) > 10) {
    // Currently, each user can have only one autosave form at a particular path.
    db_query("DELETE FROM {autosaved_forms} WHERE form_id = '%s' AND path = '%s' AND uid = %d", $form_id, $path, $user->uid);
    db_query("INSERT INTO {autosaved_forms} (form_id, path, uid, timestamp, serialized) VALUES ('%s', '%s', %d, %d, '%s')",
      $form_id, $path, $user->uid, time(), $serialized);
  }
    
  exit();
}
liquidcms’s picture

Other issues/solutions i have finished or am working on include:

- in IE7 when i select VIEW only some fields are displayed correctly; others are blank

I originally thought this was a Tiny issue but appears to only be an IE7 issue. I have back reved a little ways and the issue is still there. I think jquery is fully functional with IE7 so that narrows the code down a bit. I'll back rev even further to see if i can narrow it down any more - hopefully shouldn't take to long to get this sorted out.

Pretty sure the issue is either in the formHash function or form.reset().

- there seems to be some issue with cursor jumping around inside a field

haven't looked at this much yet but will be next on my agenda.

- i use the checkout module (although revisions made to it) and i think it may be intefering with the autosave function - need to check that out as well.

and that's all i know of at the momeent.

liquidcms’s picture

figured out cause of IE7 issue: the line in jqeury.field.js:

keys = n.split(/\[(\w*)\]/);

is not supoprted by IE7

i'll see if i can find a more generic way to do this.

liquidcms’s picture

solution for IE7

it turns out IE7 doesn't do regex splits very well.. so using this code for the part that Edmund added to handle Drupal names with [] should be something like this (around line 303 of jquery.field.js):

else {
  // Deal with Drupal form elements with square brackets in their name (eg field_name[key]).
              var value=null;
              n = n.replace(/\[/g,','); n = n.replace(/\]/g,',');  
              keys = n.split(",");
              if (keys.length > 1) {
                for (var key in keys) {
                  if (keys[key]) {
                    if (!value) value = inHash[keys[key]];
                    else        value = value[keys[key]];
                  }
                }
              }
              jel[defaults.useArray ? "fieldArray" : "setValue"](value);

i hate JS, i hate IE... so what could be worse.. JS in IE... grrr!!!

liquidcms’s picture

StatusFileSize
new18.67 KB

Ok, here's my final update of this module. This fixes all the IE, Tiny, etc issues i have seen to date, including:

- Tiny fields loading properly
- cursor jumping to start of Tiny fields when autosave kicks in
- proper cleanup of autosave table on node submit.
- likely a couple other things.

enjoy.

bbenone’s picture

Maybe I am missing something here--- how do I turn on autosave for a form? I enabled the module and configed it to save every 10 seconds.

I went to /node/add/page
and waited... nothing...

So I looked through above and saw the discussion about appending /configure-autosave to the path. So I went to /node/add/page/configure-autosave
it says "autosave enabled" around the input fields. I would guess that means it should be working?

I can't get it to actually do anything though...

It should also be noted that (at least in firefox) I can not expand and then collapse a fieldset anywhere on my site w/ the module installed.

bbenone’s picture

BTW, I have this version of the module up and running here so that you can see the symptoms I'm describing above. I can't seem to get it to anything:
http://cakwak.com/autosavetest2/?q=node/add/page

login as
u: admin
p: admin

It's a bare Drupal 5.3 install with nothing extra on but this module and JQuery Update

liquidcms’s picture

off the top of my head i would suggest you don't "really" have jquery update. Do you know that it is necessary to copy the version of jquery.js that comes with jqueyr_update into the /misc folder? Can you view the JS file that gets loaded in your browser and tell me what version it is?

bbenone’s picture

Its showing as JQuery 1.1.2

I did forget to copy over the JS file initially, but it's there now and it is still acting the same way.

It did fix the fieldsets so that they expand/collapse properly though :)

liquidcms’s picture

have you configured?

did you set update period (not sure what default is)?

did you enable autosave for your node type?

bbenone’s picture

did you enable autosave for your node type?

That's exactly what I was asking how to do in my original post :) Based on the discussion in this thread I kept going to (for example) /node/add/page/configure-autosave to configure it.... I didn't think that there was another spot.

I found it though, inside the content type's configuration page. So now I'm up and running (sorta :) )

On a basic form like /node/add/page in firefox I am showing 1 JS error:

tinyMCE is not defined
http://cakwak.com/autosavetest2/modules/autosave/autosave.js
Line 37

This prevents the form from ever re-saving after the first time.

In IE (6.0), it doesn't even save the first time. Just tells me "Unknown runtime error" in the JS console on page load.

If you'd like, you can login to my autosave test site as uid 1 to see the issue:
http://cakwak.com/autosavetest2/?q=node/add/page (admin/admin)

Thanks, dude-- this is looking pretty sweet. Among other things, I like the "Keep" link ya added :)

bbenone’s picture

just realized I didn't get ya all the error information that FF was giving... in case it helps:

tinyMCE is not defined
saveForm()autosave.js (line 37)
  tinyMCE.selectedInstance.selection.moveToBookmark(editorBookmark);

also, the following PHP errors show on the next page load:

    * warning: Invalid argument supplied for foreach() in /home/cakwak/public_html/autosavetest2/modules/node/node.module on line 521.
    * warning: implode() [function.implode]: Bad arguments. in /home/cakwak/public_html/autosavetest2/modules/node/node.module on line 525.
    * user warning: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1 query: SELECT n.nid, n.vid, n.type, n.status, n.created, n.changed, n.comment, n.promote, n.sticky, r.timestamp AS revision_timestamp, r.title, r.body, r.teaser, r.log, r.format, u.uid, u.name, u.picture, u.data FROM node n INNER JOIN users u ON u.uid = n.uid INNER JOIN node_revisions r ON r.vid = n.vid WHERE in /home/cakwak/public_html/autosavetest2/includes/database.mysql.inc on line 172.

Thanks...

Tamar Badichi-Levy’s picture

the php warnings about line 521 in node.module are the result of calling node_load without a numeric parameter. This is sometimes done in autosave_save()
In order to solve that I have changed the above line:
$submitted = node_load($path_args[1]);
to

if (is_numeric($path_args[1])){
  $submitted = node_load($path_args[1]);
}else{
  $submitted->changed=0;
}

and no more warnings...

Crell’s picture

Status: Active » Closed (duplicate)