Jump to content


[Snippet] save_content_externally


  • You cannot reply to this topic
19 replies to this topic

#1 Cris D.

    Loves Etomite Forums!

  • Developers
  • PipPipPipPip
  • 1,104 posts
  • Gender:Male

Posted 28 March 2008 - 11:43 AM

File Name: save_content_externally
File Submitter: Cris D.
File Submitted: 28 Mar 2008
File Updated: 2 Aug 2008
File Category: Miscellaneous

Snippet: save_content_external
Version: 1.0
Author: Cris D
Date: 2008/03/28
For: Etomite V 0.6.1.4
Uses: Basically, it creates new etomite pages without having to use the manager.
It collects variables and saves them to a new site document under a parent you specify.
Document variables are collected from (in order of priority)
1) $_POST variables with the $prefix pre-pended
2) snippet call parameters sent in the snippet call
3) snippet default parameters set in the snippet

NOTE: To use this snippet for 0.6.1.4 and earlier, you will need to update your manager/processors/cache_sync.class.processor.php file.
See Etomite forum thread: http://www.etomite.com/index.php?s=...ost&p=46982 for more detail. If you do not, you will get a "no file exists" error.
For Etomite V1.0 and later, this modification has already been made.

This snippet collects $_POST['params']first then if blank, uses snippet call and in-snippet parameters as defaults.

Installation Instructions:

Copy and paste this text file to your snippet library.

Either call this snippet by placing it on a page and pointing the form action="nn" to it or use $etomite->runSnippet() to call it.

Testing can be done easily by manually placing the $_POST['vars']; in the top of the snippet until you work out what you want to actually send to it!

2008/06/08 Upon review, I discovered that the delete action actually removes the document from the table: it does not mark it as deleted-to be purged later when the recycle bin is emptied. This action can not be undone. For a safer way of deleting a document, the deleted='1', deletedon='[timestamp]' and deletedby='['internalKey']' could be used. The deleted, deletedon and deletedby fields have not been included to be collected in this snippet, but could be added to the CODEswitch ($action){case 'delete': part quite easily.

CHANGELOG: 2008/08/02
Fixed typo with $_SESSION['internakKey']
Changed default user to 1 (admin) instead of '0'- NULL

Warning: This snippet plays with the etomite_site_content table. It creates documents on the fly and if you accidentally pass parameters to the table that arte incorrect, you can completely stop your site (and manager) from functioning! The default parameters being passed are OK so if in doubt, don't send it! If you know what you are doing and still want to send parameters and stuff it up, all can be restored by simply using phpMyAdmin to delete the documents you have created. This snippet is sailing close to the wind with manager modifications: if use of the snippet causes major problems with your installation because you are passing invalid parameters- you are on your own. Be warned!

Click here to download this file

#2 Ralph

    Loves Etomite Forums!

  • Admin
  • 6,524 posts
  • Gender:Male

Posted 28 March 2008 - 12:55 PM

You can use $etomite->syncsite(); right in your snippet to update the Etomite cache...

#3 Cris D.

    Loves Etomite Forums!

  • Developers
  • PipPipPipPip
  • 1,104 posts
  • Gender:Male

Posted 28 March 2008 - 12:56 PM

It is in the snippet, but if the file is not updated, it causes the errors mentioned.

#4 Ralph

    Loves Etomite Forums!

  • Admin
  • 6,524 posts
  • Gender:Male

Posted 28 March 2008 - 12:59 PM

Right... The fix will be in Prelude v1.0...

#5 mikef

    Loves Etomite Forums!

  • Member
  • PipPipPipPip
  • 1,551 posts

Posted 01 August 2008 - 07:52 PM

Thanks for this Cris - easy to use and does what I need for repeatedly creating small groups of pages. Now I just need to sort out how to create the groups needed as well!

#6 Cris D.

    Loves Etomite Forums!

  • Developers
  • PipPipPipPip
  • 1,104 posts
  • Gender:Male

Posted 01 August 2008 - 10:26 PM

I use the snippet to create a parent (for an individual person) and then create sub-pages for each submitted book. I identify the corrent parent the page the sub-page belongs to by cross referencing the $_SESSION['internalKey'] with the parent created_by with a database query, and send the 'parent'=$etomite->getDocument() in the sub page array. This may not suit your needs, but might assist in tackling the problem.

If you know the values of your parents, you could easily create an array of parents you want to create:
$groups=array(group1, group2,group3);
foreach($groups as $g){
$params=array('parent'=>$g);
$ids[]=$etomite->runSnippet('save_content_external',$params);
}
print_r($ids);

Glad you can use it...

Edited by Cris D., 01 August 2008 - 10:31 PM.


#7 mikef

    Loves Etomite Forums!

  • Member
  • PipPipPipPip
  • 1,551 posts

Posted 01 August 2008 - 10:51 PM

Thanks Cris, but not waht I'm trying to do. I'll look at it over the weekend if I can sort out another unrelated problem thats worrying me.

The biggest problem I had getting the page trees created turned out to be (I think) a bug in runSnippet (reported to the tracker).

#8 mikef

    Loves Etomite Forums!

  • Member
  • PipPipPipPip
  • 1,551 posts

Posted 02 August 2008 - 01:17 AM

There's a small typo in the download file: the line
$defaultUser= isset($_SESSION['internakKey']) ? $_SESSION['internalKey'] : '0';
should be
$defaultUser= isset($_SESSION['internalKey']) ? $_SESSION['internalKey'] : '0';


#9 Cris D.

    Loves Etomite Forums!

  • Developers
  • PipPipPipPip
  • 1,104 posts
  • Gender:Male

Posted 02 August 2008 - 06:39 AM

Thanks Mike, snippet updated!

#10 mikef

    Loves Etomite Forums!

  • Member
  • PipPipPipPip
  • 1,551 posts

Posted 03 August 2008 - 06:52 PM

This code does what I wanted to do with document groups for the new pages.

It may help others work out how to solve other similar problems

// create a group and add pages into it
// params group (name - text) pages (id,id)
// returns group id or message 

	$sql = "INSERT INTO ".$etomite->db."documentgroup_names(name) VALUES ('$name');";
	$rs = mysql_query($sql);
	if(!$rs){
	  return "An error occured while attempting to create the new group ($name).";
	}
	if(!$key=mysql_insert_id()) {
	  return "Couldn't get the id for group $name!";
	}
	foreach (explode(',' , $pages) as $id) {
		$sql = "INSERT INTO ".$etomite->db."document_groups(document_group, document) VALUES ('$key','$id');";
		$rs = mysql_query($sql);
		if(!$rs){
			  $err .= "An error occured while attempting to add the document to the group (key=$key id=$id).";
		}
	}
if ($err) return $err;
return $key;


#11 Jelmer

    Loves Etomite Forums!

  • Member
  • PipPipPipPip
  • 1,173 posts

Posted 03 August 2008 - 07:10 PM

Thnx Mike, this was exactly what I need, I think. I'll get back to you!

#12 Ralph

    Loves Etomite Forums!

  • Admin
  • 6,524 posts
  • Gender:Male

Posted 04 August 2008 - 01:54 AM

I'd suggest using as many API function calls as possible as opposed to using direct MySQL database calls... Doing so allows Etomite to use its internal error handling and can usually reduce the overall amount of code required... But that's just me...

#13 Cris D.

    Loves Etomite Forums!

  • Developers
  • PipPipPipPip
  • 1,104 posts
  • Gender:Male

Posted 04 August 2008 - 09:53 AM

I agree Ralph, however, the above code uses the mysql_insert_id() and without numerous iterations of putIntTableRow()
$temp=getIntTableRow(with some convoluted where query to ensure you are collecting the right row)
$key=$temp[0]['id'];
$fields=array($key, $id);
putIntTableRow($fields,'table');

Unless there are API's that return the key of inserted rows, sometimes they are more trouble than they are worth. I too have resorted to direct database queries in cases like the above. Is there an etomite-friendly way of doing what Mike has done using existing API's? If there is, it escapes me.

#14 mikef

    Loves Etomite Forums!

  • Member
  • PipPipPipPip
  • 1,551 posts

Posted 04 August 2008 - 10:48 AM

I'll use the APIs if I think they'll save me time, or result in clearer code. That wasn't the case here.

#15 Ralph

    Loves Etomite Forums!

  • Admin
  • 6,524 posts
  • Gender:Male

Posted 04 August 2008 - 01:20 PM

@Cris

$etomite->insertId() and $etomite->affectedRows() come in handy and will work with both internal and external database API functions... Probably another one of those areas that would benefit from a tutorial if I can ever muster up the time...

#16 mikef

    Loves Etomite Forums!

  • Member
  • PipPipPipPip
  • 1,551 posts

Posted 05 August 2008 - 09:15 AM

View PostRalph, on Aug 4 2008, 02:54 AM, said:

I'd suggest using as many API function calls as possible as opposed to using direct MySQL database calls... Doing so allows Etomite to use its internal error handling and can usually reduce the overall amount of code required... But that's just me...
having now seen what Etomite's "internal error handling" does on database errors, I'm glad I'm not using it!
:(

#17 Cris D.

    Loves Etomite Forums!

  • Developers
  • PipPipPipPip
  • 1,104 posts
  • Gender:Male

Posted 05 August 2008 - 09:22 AM

No offence Ralph, but I think that's a fair call Mike... They're not the prettiest of pages! :Nerd:
What about outputting system error messages with a user-defined chunk in cocoon so that admin can style an etomite error message to look like part of the site?- just a thought (and actually waaaaay of topic - sorry).

#18 mikef

    Loves Etomite Forums!

  • Member
  • PipPipPipPip
  • 1,551 posts

Posted 05 August 2008 - 09:44 AM

I'd prefer cocoon to give the snippet writer the option of handling database errors. The use of a duplicate key is in many cases a recoverable error (by the snippet, not the core), rather than just cause for crashing the page, especially as transactions aren't supported.

Edited by mikef, 05 August 2008 - 09:48 AM.


#19 Ralph

    Loves Etomite Forums!

  • Admin
  • 6,524 posts
  • Gender:Male

Posted 05 August 2008 - 12:48 PM

Hey, I'll be the first to chime right in on the error handling issue... All I can say is "I didn't write it." and haven't bothered making changes because I would like an entire rewrite of that module... For a production site, however, the errors should never present themselves to end users if adequate checks and balances are in place... Also remember that some of the error message is purposely ambiguous for security reasons and that additional information can be seen if debugging is turned on...

#20 mikef

    Loves Etomite Forums!

  • Member
  • PipPipPipPip
  • 1,551 posts

Posted 05 August 2008 - 03:02 PM

View PostRalph, on Aug 5 2008, 01:48 PM, said:

Hey, I'll be the first to chime right in on the error handling issue... All I can say is "I didn't write it."
Which is why I started with

Quote

I'd prefer cocoon to ...

New post started at http://www.etomite.c...?showtopic=7990





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users