Page 1 of 1

Altering a post's $data array in an extension

Posted: Tue Jan 20, 2015 4:38 pm
by gothick
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

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

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

b) yes that is a good way to make a post unapproved at the point of posting.

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

Posted: Tue Jan 20, 2015 6:51 pm
by gothick
Great, thanks for the reassurance! At least I know I'm facing the right way before I start my walking now :)

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

Posted: Mon Mar 25, 2019 7:56 am
by pabhu
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!

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

Posted: Mon Mar 25, 2019 8:09 am
by pabhu
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!

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

Posted: Mon Mar 25, 2019 8:20 am
by pabhu
got it..

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

thank you!!!!