Forum

Thread tagged as: Question, Runway

Website Notification Messages with Expiry

Hi all,

I was wondering if anyone had any idea how to create a notification message (or message bar) at the top of my website that would expire at a date/time that I input when I create the message in Perch?

Basically, I want to display messages like "Bank holiday on Monday. The office will be closed and will reopen on Tuesday" but I would set an expiry of 23:59:59 on Monday so that I do not constantly have to update and remove messages.

Any ideas?

Cheers!

Conor Harkins

Conor Harkins 0 points

  • 3 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

Use perch_content_custom() with a filter on the start and end date fields, comparing them to the current date.

You can get the current date/time with date("Y-m-d H:i:s")

Hi Drew,

Thanks for that. I tried your suggestion but still can't get it to work. Here's what I have done so far...

<?php
                        perch_content_custom('Alert Message', [
                            'filter'=> [
                                [
                                    'filter'=>'Date1',
                                    'match'=>'lte',
                                    'value'=>date("Y-m-d H:i:s")
                                ],
                                [
                                    'filter'=>'Date2',
                                    'match'=>'gte',
                                    'value'=>date("Y-m-d H:i:s")
                                ],
                            ]
                        ]);
                        ?>

<div align="center" class="<perch:content id="class" type="text" label="Class" required="true"/>">
    <perch:content id="Alert" type="textarea" label="Alert Text" html="true" editor="simplemde" required="true"/>
</div>
<div hidden>
<perch:content id="Date1" type="date" label="Start Date" format="Y-m-d H:i:s" required="true"/>
<perch:content id="Date2" type="date" label="End Date" format="Y-m-d H:i:s" required="true"/>
</div>
Drew McLellan

Drew McLellan 2638 points
Perch Support

That looks fine - how is it failing?

Simon Clay

Simon Clay 127 points

Y-m-d H:i:s outputs the date and time like this 2001-03-10 17:16:18 I don't think that can be used as a value to match against can it?

I'm guessing it would need something more like:

'filter' => date("Y-m-d H:i:s"),
'match' => 'eqbetween',
'value' => '$date1, $date2',

You'd need to save Date1 & Date2 as variables. But I'm not sure if 'filter' must be a region ID

Hi Drew,

The notification isn't showing unless the end date is at least 2 days in the future. e.g 24th viewed on the 22nd. If set to 23rd, post will not show. Start date is also not working correctly, not hiding the post until start date is set to 24th when viewed on 22nd. If set to 23rd, the post will show.

Simon, i'm not sure how to use variables yet in Perch. Would you be able to explain your above solution?

Thank you.

Drew McLellan

Drew McLellan 2638 points
Perch Support

What does debug output for the page?

SELECT * FROM ( SELECT idx.itemID, c.regionID, idx.pageID, c.itemJSON, idx2.indexValue as sortval FROM perch3_content_index idx JOIN perch3_content_items c ON idx.itemID=c.itemID AND idx.itemRev=c.itemRev AND idx.regionID=c.regionID JOIN perch3_content_index idx2 ON idx.itemID=idx2.itemID AND idx.itemRev=idx2.itemRev AND idx2.indexKey='_order' WHERE ((idx.regionID=14 AND idx.itemRev=106)) AND ((idx.indexKey='Date1' AND idx.indexValue <= '2018-06-24 13:23:14') OR (idx.indexKey='Date2' AND idx.indexValue >= '2018-06-24 13:23:14')) AND idx.itemID=idx2.itemID AND idx.itemRev=idx2.itemRev ) as tbl GROUP BY itemID, pageID, itemJSON, sortval, regionID HAVING count(*)=2 ORDER BY sortval ASC
[1] Using template: /templates/content/alert.html

It's working now. Just realised my timezone was set to an hour early... Doh!