Simple Machines Community Forum

Customizing SMF => SMF Coding Discussion => Topic started by: rcane on January 27, 2022, 09:41:48 AM

Title: Can I make a portal to show the most current “document”?
Post by: rcane on January 27, 2022, 09:41:48 AM
Our forum members get blastmails each week (or thereabouts) in their email.

I have a topic that is a bbcode list of those PDFs (links to the actual pdf files uploaded to the server and not attachments).

That gets tedious.

Here's what I envisioned:

1. A folder on the server to dump the pdf(s)

2. A portal that has clickable image (or some clickable eye-catcher) to read it.

3. But, I'd like the "button" to grab the most recently added document so I don't have to keep renaming files or changing the link. 

Does that make sense?

Drop a new file in the folder and it becomes the target based on its submission date.

I'm not sure if this qualifies as a portal question but it seems like the most likely way to display this.

Title: Re: Can I make a portal to show the most current “document”?
Post by: Kindred on January 27, 2022, 12:56:37 PM
this would require a custom coding...   I'm going to move this out of support and into coding.
Title: Re: Can I make a portal to show the most current “document”?
Post by: rcane on January 27, 2022, 01:14:35 PM
Quote from: Kindred on January 27, 2022, 12:56:37 PMthis would require a custom coding...   I'm going to move this out of support and into coding.

ok
Title: Re: Can I make a portal to show the most current “document”?
Post by: Doug Heffernan on January 27, 2022, 01:43:32 PM
Quote from: rcane on January 27, 2022, 09:41:48 AMOur forum members get blastmails each week (or thereabouts) in their email.

I have a topic that is a bbcode list of those PDFs (links to the actual pdf files uploaded to the server and not attachments).

That gets tedious.

Here's what I envisioned:

1. A folder on the server to dump the pdf(s)

2. A portal that has clickable image (or some clickable eye-catcher) to read it.

3. But, I'd like the "button" to grab the most recently added document so I don't have to keep renaming files or changing the link. 

Does that make sense?

Drop a new file in the folder and it becomes the target based on its submission date.

I'm not sure if this qualifies as a portal question but it seems like the most likely way to display this.


As per your title, yes it can be done, if I understood it correctly. However this will require some work imo. I think that it would be best if this was moved to the Mod Request section. It might receive more attention there.
Title: Re: Can I make a portal to show the most current “document”?
Post by: Arantor on January 27, 2022, 02:10:02 PM
So you'll need to put this in a PHP page or a PHP block - there's no way to just do this in a portal page otherwise (at least not without a lot more faffing about)

<?php

// These should end in /
$real_path '/var/www/html/';
$url_path 'https://domain.com/path/to/pdfs/';
$files = [];

$real_path rtrim($real_path) . '/';

foreach (
scandir($real_path) as $file) {
if (is_dir($real_path $file)) {
continue;
}

$files[$file] = filemtime($real_path $file);
}

arsort($files);
$latest array_keys($files)[0];

echo 
'<a href="' $url_path $latest '"><img src="banner.jpg"></a>';

You will need to set $real_path to the path to the PDFs (the full path, e.g. /home/username/public_html) and not just whatever your FTP client reports (since most FTP clients present /home/username as if it is / when it really isn't), and you will need to set $url_path to the URL for the folder.

This isn't clever, it will just look at all the things in the folder and return the most recent as a link with an image in it - I don't know the URL to your eye-catching banner image, but that's in the code as banner.jpg, which you might need to change.
Title: Re: Can I make a portal to show the most current “document”?
Post by: rcane on January 27, 2022, 09:35:10 PM
wow thanks.

So, understanding what's done here.  Why did you trim the $real_path?

that would help me understand the loop, which seems to exit the loop when it discovers a file--and then goes on to grab it's mod time and sort them in the array (descending order).

Is that close?
Title: Re: Can I make a portal to show the most current “document”?
Post by: rcane on January 28, 2022, 01:15:02 AM
Quote from: Arantor on January 27, 2022, 02:10:02 PMSo you'll need to put this in a PHP page or a PHP block - there's no way to just do this in a portal page otherwise (at least not without a lot more faffing about)

<?php

// These should end in /
$real_path '/var/www/html/';
$url_path 'https://domain.com/path/to/pdfs/';
$files = [];

$real_path rtrim($real_path) . '/';

foreach (
scandir($real_path) as $file) {
if (is_dir($real_path $file)) {
continue;
}

$files[$file] = filemtime($real_path $file);
}

arsort($files);
$latest array_keys($files)[0];

echo 
'<a href="' $url_path $latest '"><img src="banner.jpg"></a>';

You will need to set $real_path to the path to the PDFs (the full path, e.g. /home/username/public_html) and not just whatever your FTP client reports (since most FTP clients present /home/username as if it is / when it really isn't), and you will need to set $url_path to the URL for the folder.

This isn't clever, it will just look at all the things in the folder and return the most recent as a link with an image in it - I don't know the URL to your eye-catching banner image, but that's in the code as banner.jpg, which you might need to change.

how would you call this from within the forums if it's in a php file?
Title: Re: Can I make a portal to show the most current “document”?
Post by: @rjen on January 28, 2022, 02:00:45 AM
You would need to put it in a php block in the portal... which makes sense, since you specially mentioned portals..
Title: Re: Can I make a portal to show the most current “document”?
Post by: Arantor on January 28, 2022, 05:09:29 AM
Argh, I meant that to be rtrim($real_path, '/') so that whether there was a / on the end or not, that got normalised out, though Linux doesn't care *too* much about // in a path.

I explicitly aimed for this being a portal block rather than a file, because it would have to be done differently as a raw file.
Title: Re: Can I make a portal to show the most current “document”?
Post by: rcane on January 28, 2022, 01:19:06 PM
Quote from: Arantor on January 28, 2022, 05:09:29 AMArgh, I meant that to be rtrim($real_path, '/') so that whether there was a / on the end or not, that got normalised out, though Linux doesn't care *too* much about // in a path.

I explicitly aimed for this being a portal block rather than a file, because it would have to be done differently as a raw file.

Ok, just checking.  I'm just taking a php course online and trying to convert Swift into php on the fly.  It's starting to make some sense.

I've never added a block to a portal so that should be fun too.  Simple or Tiny portals seems to be the popular  ones for 2.0
Title: Re: Can I make a portal to show the most current “document”?
Post by: Oldiesmann on January 29, 2022, 12:24:36 PM
TinyPortal is the one I'd recommend. SimplePortal is no longer developed.