Altering a post's $data array in an extension

Discussion forum for Extension Writers regarding Extension Development.
Post Reply
User avatar
gothick
Registered User
Posts: 69
Joined: Sat Apr 21, 2007 7:20 am
Name: Matt Gibson
Contact:

Altering a post's $data array in an extension

Post by gothick » Tue Jan 20, 2015 4:38 pm

Hi,

I'm just starting to re-implement an old anti-spam mod I wrote for my forum as an extension. One of the things I need to do is to mark a post as un-approved at the point of posting, depending on a few variables.

As a proof-of-concept of what I wanted to do, I hooked a listener onto the core.posting_modify_submit_post_before event. Then it looked like I needed to add force_approved_state to the post's $data array.

I stumbled a little when I found that I couldn't just set $event['data']['force_approved_state'] directly in my code (because $event['data'] is done with ArrayAccess?) so I ended up with the following code:

Code: Select all

        $data = $event['data'];
        $data['force_approved_state'] = ITEM_UNAPPROVED;
        $event['data'] = $data; 
Now, this works, and with the above code as the only code in my listener, all posts are marked as needing approval, but I have a couple of questions:

a) Is this the right way to add a value to $data in the event listener? Copying the whole array, adding a value, and writing it back again seems a little clunky.

b) Does this look like a sane approach to changing the approval status of a post at the point of submission?

Thanks!

Matt

User avatar
VSE
Extensions Development Coordinator
Extensions Development Coordinator
Posts: 4864
Joined: Sat Jan 17, 2009 9:37 am
Location: Los Angeles, CA
Name: Matt Friedman
Contact:

Re: Altering a post's $data array in an extension

Post by VSE » Tue Jan 20, 2015 5:18 pm

a) yes this is the correct pattern.

b) yes that is a good way to make a post unapproved at the point of posting.
Dictated but not read.
Official phpBB Extensions My Extensions & MODs
Please do not PM me for support.

User avatar
gothick
Registered User
Posts: 69
Joined: Sat Apr 21, 2007 7:20 am
Name: Matt Gibson
Contact:

Re: Altering a post's $data array in an extension

Post by gothick » Tue Jan 20, 2015 6:51 pm

Great, thanks for the reassurance! At least I know I'm facing the right way before I start my walking now :)

pabhu
Registered User
Posts: 10
Joined: Sat Feb 23, 2019 10:49 am

Re: Altering a post's $data array in an extension

Post by pabhu » Mon Mar 25, 2019 7:56 am

Hi, Here I am trying to change the post_time value onto the listener core.posting_modify_submit_post_before event

Code: Select all

		$data = $event['data'];
			$oldtime = $this->user->create_datetime()->setTimestamp($data['post_time']);
			$from_oldtime = $oldtime->format();

			$year = $this->request->variable('p_year', 0);
			$month = $this->request->variable('p_month', 0);
			$day = $this->request->variable('p_day', 0);
			$hour = $this->request->variable('p_hour', -1);
			$minute = $this->request->variable('p_minute', -1);

			$year = ($year) ? $year : (int) $oldtime->format('Y');
			$month = ($month) ? $month : (int) $oldtime->format('n');
			$day = ($day) ? $day : (int) $oldtime->format('j');
			$hour = ($hour>=0) ? $hour : (int) $oldtime->format('G');
			$minute = ($minute>=0) ? $minute : (int) $oldtime->format('i');

			// Use user timezone to create UNIX timestamp
			$newtime = $this->user->create_datetime()->setDate($year, $month, $day)->setTime($hour, $minute);
			$update_time = $newtime->getTimestamp();
		
		
        $data['post_time'] = ($update_time) ? $update_time : ($data['post_time']) ? $data['post_time'] : time();
        $event['data'] = $data; 
this is not working for me;

but the below code is working

Code: Select all

$data = $event['data'];
         $data['post_time'] = 1504948147;
        $event['data'] = $data; 
can you help
thanks!

pabhu
Registered User
Posts: 10
Joined: Sat Feb 23, 2019 10:49 am

Re: Altering a post's $data array in an extension

Post by pabhu » Mon Mar 25, 2019 8:09 am

VSE wrote:
Tue Jan 20, 2015 5:18 pm
a) yes this is the correct pattern.

b) yes that is a good way to make a post unapproved at the point of posting.
Hi, Here I am trying to change the post_time value onto the listener core.posting_modify_submit_post_before event

Code: Select all


		$data = $event['data'];
			$oldtime = $this->user->create_datetime()->setTimestamp($data['post_time']);
			$from_oldtime = $oldtime->format();

			$year = $this->request->variable('p_year', 0);
			$month = $this->request->variable('p_month', 0);
			$day = $this->request->variable('p_day', 0);
			$hour = $this->request->variable('p_hour', -1);
			$minute = $this->request->variable('p_minute', -1);

			$year = ($year) ? $year : (int) $oldtime->format('Y');
			$month = ($month) ? $month : (int) $oldtime->format('n');
			$day = ($day) ? $day : (int) $oldtime->format('j');
			$hour = ($hour>=0) ? $hour : (int) $oldtime->format('G');
			$minute = ($minute>=0) ? $minute : (int) $oldtime->format('i');

			// Use user timezone to create UNIX timestamp
			$newtime = $this->user->create_datetime()->setDate($year, $month, $day)->setTime($hour, $minute);
			$update_time = $newtime->getTimestamp();
		
		
        $data['post_time'] = ($update_time) ? $update_time : ($data['post_time']) ? $data['post_time'] : time();
        $event['data'] = $data; 
this is not working for me;

but the below code is working

Code: Select all

$data = $event['data'];
         $data['post_time'] = 1504948147;
        $event['data'] = $data;

can you help
thanks!

pabhu
Registered User
Posts: 10
Joined: Sat Feb 23, 2019 10:49 am

Re: Altering a post's $data array in an extension

Post by pabhu » Mon Mar 25, 2019 8:20 am

got it..

below code fixed the issue
$data['post_time'] = $update_time;

thank you!!!!

Post Reply

Return to “Extension Writers Discussion”