EEConf 2024 is around the corner! EEConf 2024
hi, I have a module that among other things, saves the output of a form to a weblog. Problem is this form can also be submitted by people who are not logged in. It works perfect as long as your logged in, as a test i took away “members” permission to publish to the weblog that this module saves the form to, and sure enough.
Here is how we are saving the form to the weblog.
function add_application()
{
global $DB, $IN, $LOC;
// get weblog id
$query = $DB->query("SELECT weblog_id FROM exp_weblogs WHERE blog_name = 'applications'");
if ($query->num_rows == 0)
{
return;
}
$weblog_id = $query->row['weblog_id'];
// set title
$title = $IN->GBL('first_name', 'POST')." ".$IN->GBL('last_name', 'POST')." - ".$IN->GBL('application_type', 'POST');
// get application weblog fields
$fields = array();
$query = $DB->query("SELECT * FROM exp_weblog_fields JOIN exp_field_groups ON exp_weblog_fields.group_id = exp_field_groups.group_id WHERE group_name = 'Applications'");
foreach ($query->result as $row)
{
$fields[$row['field_name']] = "field_id_".$row['field_id'];
}
// convert inputed data to custom fields
foreach ($fields as $field_name => $field_id)
{
// if custom weblog field does not exist then unset
if (isset($_POST[$field_name]))
{
$_POST[$field_id] = $_POST[$field_name];
}
}
// add extra data
$_POST['weblog_id'] = $weblog_id;
$_POST['title'] = $title;
$_POST['entry_date'] = $LOC->set_human_time($LOC->now);
// set return url
$_POST['return_url'] = 'application/sent ';
// insert
if ( ! class_exists('Publish'))
{
require PATH_CP.'cp.publish'.EXT;
}
$PB = new Publish();
$PB->submit_new_entry(FALSE);
}
So how do we get this to still publish to the weblog for non-logged in users??
I would probably write an extension (and extension hook) instead of instantiating the publish class itself - I’m always scared to start calling classes directly, who knows what development plans are going forward for EE… As for the logged in part, how about registering a new user in the system named “Guest” or “Anonymous”. Then , in your extension, you check to see if the person if logged in. When they aren’t, assign the entry to that Guest user so it technically has an author?
EDIT - actually you don’t even need to have your extension do that probably. On page load you could see if the person is logged in - if not then in the form itself set the user to your Guest user.
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.