Jump to content

Pages: Remove database name from URL, or mix records?


AvatharTOS

Recommended Posts

Posted

This is in regards to Pages.

Let's assume a website with a homepage, news articles and review articles. The goal is for the home page to show a mix of news and reviews in order of publication, and news to be browsable at http://www.example.com/news and reviews at http://www.example.com/reviews.

When I say "mix", I mean that a plain list (or array) of recent articles could contain any number of news (N) articles and review (R) articles, in any order, e.g. "NRRNRNRRNNRNNRRNN".

Using a single database called "articles" for both types of articles (not a problem as such, fields are the same) and categories for news and reviews, I get for example http://www.example.com/articles/news. Is it possible to remove the database name from the URL? I considered adding a FURL entry but couldn't figure out the parameters for index.php, e.g. http://www.example.com/news <- http://www.example.com/index.php?{something here to point to articles database, news category}.

The alternative of using two databases named "news" and "reviews" (no categories) solves the URL issue. But how can I then create a page or block that mixes records from different databases? That is, what would the code look like?

Any other alternative is also welcome.

Posted

The best solution I can think of:

  1. Make a page called home. Or anything you want.
  2. Set it as the default Page for Pages. This will make it appear at http://example.com instead of at, for example, http://example.com/articles
  3. Put the database Articles on it.
  4. I don't know exactly what you mean by a mix of "NRRNR" etc- a mix of reviews and news in any random order, probably chronologically? This is possible with IPS but I don't think it's how you want. If you want a fancy news-like article grid, you'd need to get into working on some custom stuff. 
  5. If you want a news site style news feed, a good guide I found is linked below.

Now... to get the category Articles and the category News to appear on their own pages sounds tricky. I think it would actually work just fine if you have Home as the default page, then News and Articles as the FURL for those categories, though. Because for example, I have a wiki database. It has a rules category. The FURL for that is rules so the link is http://example.com/wiki/rules/ - now, I'm not sure nor have I tested it, but since home is the default page... any categories should appear as http://example.com/category - don't quote me on that! But give it a shot. If not reply here and I'll try to find another solution.

Posted
14 hours ago, AvatharTOS said:

When I say "mix", I mean that a plain list (or array) of recent articles could contain any number of news (N) articles and review (R) articles, in any order, e.g. "NRRNRNRRNNRNNRRNN".

With stock options only possible when they are in the same database. 

 

14 hours ago, AvatharTOS said:

 Is it possible to remove the database name from the URL?

No. The page has an URL slug (unless it’s the homepage) and each category brings another URL slug. 

 

Posted

Thank you for your replies.

8 hours ago, Lyonharted said:

The best solution I can think of:

  1. Make a page called home. Or anything you want.
  2. Set it as the default Page for Pages. This will make it appear at http://example.com instead of at, for example, http://example.com/articles
  3. Put the database Articles on it.
  4. I don't know exactly what you mean by a mix of "NRRNR" etc- a mix of reviews and news in any random order, probably chronologically? This is possible with IPS but I don't think it's how you want. If you want a fancy news-like article grid, you'd need to get into working on some custom stuff. 
  5. If you want a news site style news feed, a good guide I found is linked below.

Now... to get the category Articles and the category News to appear on their own pages sounds tricky. I think it would actually work just fine if you have Home as the default page, then News and Articles as the FURL for those categories, though. Because for example, I have a wiki database. It has a rules category. The FURL for that is rules so the link is http://example.com/wiki/rules/ - now, I'm not sure nor have I tested it, but since home is the default page... any categories should appear as http://example.com/category - don't quote me on that! But give it a shot. If not reply here and I'll try to find another solution.

I currently have the "articles" page (showing the "articles" database) as the default, such that the database listing is at example.com/, but categories and articles are indeed listed below example.com/articles/news and example.com/articles/news/some-news-r1 etc.

With "NRRNR..." I mean a (chronological) mix of articles, regardless of presentation. But that linked grid view was pretty nice, I may have to do something like that when I've decided on the way forward!

2 hours ago, opentype said:

With stock options only possible when they are in the same database. 

 

No. The page has an URL slug (unless it’s the homepage) and each category brings another URL slug. 

 

At least it seems I haven't overlooked anything.

I am nominally a programmer (although not for PHP, at least not since the early 2000's), so I've no problem going beyond stock functionality, but I just can't find good, comprehensive documentation on classes and methods and how they intend programmers to work with the system. Conceptually, I guess I would like to make a block that retrieves records from one database, retrieves records from another database, and sorts them chronologically, offering a mix of $records to some template, but I just have no idea of how.

I'll have to study the source code, I guess.

Posted

They're completely reworking their documentation at the moment. So some is on the old docs page and some is on the new guides page. 

https://invisionpower.com/4docs/ | https://invisionpower.com/4guides/

The 2 of those are useful. But also- I'd suggest joining a Skype chat some others and myself are in, that Hulu made. The people there are very friendly and will gladly help you with any questions you have! They've helped me work on a few plugins, even with obscure css / php questions. ^_^

You can probably get some custom blocks and setups going to work as you want.

Posted

It required some source code reading and a fair bit of experimenting, but I got something working!

First, I made a custom block, Mixed Records, containing this code:

$databases_to_choose_from = array('my_news', 'my_reviews');// The databases keys.
$databaseIds = array();
$allDatabases = \IPS\cms\Databases::databases();

/* Getting ids of the databases in the upmost array.
 * Of course $databaseIds could be populated manually beforehand instead, I just wanted to see their names in the code. */
foreach($allDatabases as $database)
{
  $databaseData = $database->_data;
  if(in_array($databaseData[key], $databases_to_choose_from))
    array_push($databaseIds, $databaseData[id]);
}
// Loading all records (articles) from the relevant databases, inserting them into one array.
$allRecords = array();
if(count($databaseIds) > 0)
{
  foreach($databaseIds as $databaseId)
  {
    $database = \IPS\cms\Databases::load($databaseId);
    $database->preLoadWords();
    $recordsClass = '\IPS\cms\Records' . $databaseId;
    $records = $recordsClass::getItemsWithPermission( );// Could limit number of records loaded here.
    foreach($records as $record)
    {
      array_push($allRecords, $record);
    }
  }
}

// Sorting the article array by publishing date; newest comes first.
usort($allRecords, function ($record1, $record2) {
  if ($record1->get__publishDate() == $record2->get__publishDate()) return 0;
  return $record1->get__publishDate() < $record2->get__publishDate() ? 1 : -1;
});

// Exporting the articles; thanks to Marcher Technologies. https://invisionpower.com/forums/topic/428507-pages-accessing-a-blocks-variables/
if(count($allRecords) > 0)
{
  \IPS\Output::i()->jsVars['mixedRecords'] = $allRecords;
}

Then I made a page:

{block="mixed_records"}
{{$articles = \IPS\Output::i()->jsVars['mixedRecords'];}}
TEMPLATE CODE GOES HERE

I copied some template code from Database Templates > Category Articles > index, but I had to modify it. The template code has an insertion of the Database Templates > Category Articles > entry template, requiring a $database variable. Simply setting the variable with $record->database() didn't work, but it did when I inserted the entry template code directly, e.g.:

	{{if count($articles)}}
		{{foreach $articles as $id => $record}}
			{{$database = $record->database();}}
			ENTRY TEMPLATE CODE GOES HERE
		{{endforeach}}
	{{endif}}

I have further work to do, like figuring out how to paginate it, but is this a decent solution?

  • 3 weeks later...

Archived

This topic is now archived and is closed to further replies.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...