Support

Home Forums Event Espresso Premium Duplicate event issue with copying People Admin people

Duplicate event issue with copying People Admin people

Posted: April 25, 2023 at 12:04 pm

Viewing 9 reply threads


Daniel

April 25, 2023 at 12:04 pm

I am using AHEE__Extend_Events_Admin_Page___duplicate_event__after

and I have this section of code that copies people to the new event. That copying to the new event works, but the original event loses those people!

I feel like this was working in the past, but have now noticed it is not. It’s possible that my duplication code was always erasing the people from the original event but I never noticed until now.

Additionally, I must have a poor understanding about the way the related tables are handled because duplicating an event should never change the original event, and yet my code below is doing that.

How should I fix this so that it correctly copies the people assigned to the new event and leaves the original event intact.


// add the instructors to the listing
$instructors = $original_event->get_many_related('Person_Post');
foreach ($instructors as $instructor) {
$new_event->_add_relation_to($instructor, 'Person_Post');
}
// save new event with the instructors
$new_event->save();

Thanks in advance for any help!
D


Tony

  • Support Staff

April 25, 2023 at 1:36 pm

Hi Daniel,

Can you post the full snippet you are using for this, please?

I’ll test this locally but I want to test the exact code you are using to confirm this.


Daniel

April 25, 2023 at 2:23 pm

Sure…


function tasny_post_duplication_EE_tasks(EE_Event $new_event, EE_Event $original_event){
	$oe_id = $original_event->ID();
	$ne_id = $new_event->ID();
	
	// bump the new event's datetimes by number of days in Utility ACF
	$days = esc_html( get_post_meta($oe_id, 'post_duplication_days_shift', true) );
	$date_interval_string = 'P'.$days.'D';
	if (is_numeric($days) && ($days > 0)) {
		$new_datetimes = $new_event->get_many_related('Datetime');
		foreach ($new_datetimes as $new_dtt) {
			$newdate = new DateTime(date("Y-m-d",strtotime( $new_dtt->start_date() )));
			$newdateplusone = date_format($newdate->add(new DateInterval($date_interval_string)), 'F d, Y');
			$new_dtt->set_start_date($newdateplusone);
			$new_dtt->set_end_date($newdateplusone);
			$new_dtt->set('DTT_EVT_start', $new_dtt->start_date_and_time());
			$new_dtt->set('DTT_EVT_end', $new_dtt->end_date_and_time());
			$new_dtt->save();
		}
	}

	// add the instructors to the listing
	$instructors = $original_event->get_many_related('Person_Post');
    foreach ($instructors as $instructor) {
        $new_event->_add_relation_to($instructor, 'Person_Post');
    }
    // save new event with the instructors
    $new_event->save();

    // add the post_metas - any ACF data not 
    // copied here is wiped on the duplication
    $acf_to_copy = array(
    	'online-classroom'
    );

    foreach ($acf_to_copy as $field_name) {
    	$pm = esc_html( get_post_meta($oe_id, $field_name, true) );
		if ( $pm ) {
			update_post_meta($ne_id, $field_name, $pm);
		}
    }

	return;
}
add_action('AHEE__Extend_Events_Admin_Page___duplicate_event__after','tasny_post_duplication_EE_tasks', 10, 2 );


Tony

  • Support Staff

April 25, 2023 at 2:48 pm

Hmm, I’m pretty sure you’re basically updating the relation with that code.

$instructors = $original_event->get_many_related('Person_Post');

That’s pulling the relationships between ‘people’ and the event, NOT the people themselves, so then adding a relation to a relation updates the relation.

I’ll check in with our developers on this, may take a little.


Daniel

April 25, 2023 at 2:55 pm

So, it’s kind of stealing the related record? I’d love it if you could provide me some sample code that will create the correct relationships!


Tony

  • Support Staff

April 25, 2023 at 3:03 pm

So, it’s kind of stealing the related record?

Basically, yeah.

Each ‘Person_Post’ is basically:

Person_ID = 1016
Obj_ID = 1031
Obj_Type = Event

So your looping over the relationships and saying relate those to the new event, which will update it to be:

Person_ID = 1016
Obj_ID = 5555
Obj_Type = Event

Where 5555 in this example is the ID of the new event.

I’d love it if you could provide me some sample code that will create the correct relationships!

Thinking on this a little 😉

Right now it’s basically, ‘gimme the relationships for people on {original_event}, update those for {new_event}’

What you need is ‘gimme the people related to {original_event} and create NEW relationships for {new_event}’

Your original query doesn’t need to know about the relationships, just the people as those what you need to relate to the new event.


Daniel

April 26, 2023 at 9:03 am

So, where can I find syntax for creating a new Person_Post?
TY in advance for any assistance or pointers!
D


Tony

  • Support Staff

April 26, 2023 at 9:37 am

As mentioned, I’ve asked for some feedback from our developers on this so will let you know once they’ve replied.


Daniel

April 26, 2023 at 10:27 am

I think I got it – this seems to work to create the new related record and doesn’t steal the existing one.


	// add the instructors to the listing
	$instructor_type_id = 8;
	$existing_people_ids = EE_Registry::instance()->load_model('Person_Post')->get_all_people_ids_for_post_and_type($oe_id, $instructor_type_id);
    foreach ($existing_people_ids as $per_id) {
        // adapted from: espresso_events_People_Hooks.class.php
        $values_to_save = array(
	        'PER_ID' => $per_id,
	        'OBJ_ID' => $ne_id,
	        'OBJ_type' => 'EE_Event',
	        'PT_ID' => $instructor_type_id
        );
    	$new_rel = EE_Person_Post::new_instance($values_to_save);
    	$new_rel->save();
    }


Daniel

April 26, 2023 at 10:29 am

Edit: should be ‘Event’ not ‘EE_Event’

Viewing 9 reply threads

The support post ‘Duplicate event issue with copying People Admin people’ is closed to new replies.

Have a question about this support post? Create a new support post in our support forums and include a link to this existing support post so we can help you.

Event Espresso