Sep 19, 2008

Do you need a page browser for your TYPO3 extension?

I have one for you! Yesterday I uploaded the first version of the "Universal page browser" extension into TER. This extension provides page browsing services to other extensions.

Currently extensions either use their own home made page browser or use default from tslib_pibase. Neither of these solutions is good. Home made page browsers requires time to create and also to maintain. It takes space in the code. The default one is hard to configure, it is not flexible for any modern application. In addition it is table–based, which is not how HTML code should be made these days.

Recently I had to write my own page browser three times in three different extensions and twice used the default one. All these times it was loss of time. If I had a "normal" page browser, I could save on development and debugging.

Armed with this idea I decided to make a page browser that I could use in any other project. I started developing it and then there came one of Netcreators projects, where I could finish this development.

As a result I made a new extension that any other extension case use to integrate a modern highly customizable CSS-based page browser. It is very easy to use. Here is the code:

protected function getListGetPageBrowser($numberOfPages) {
  // Get default configuration
  $conf = $GLOBALS['TSFE']->tmpl->setup['plugin.']['tx_pagebrowse_pi1.'];
  // Modify this configuration
  $conf += array(
    'pageParameterName' => $this->prefixId . '|page',
    'numberOfPages' => intval($numberOfPages/$this->conf['pageSize']) +
(($numberOfPages % $this->conf['pageSize']) == 0 ? 0 : 1),
  );
  // Get page browser
  $cObj = t3lib_div::makeInstance('tslib_cObj');
  /* @var $cObj tslib_cObj */
  $cObj->start(array(), '');
  return $cObj->cObjGetSingle('USER', $conf);
}


It is 7 lines (without comments) to get a fully functional page browser for your extension. It provides a tx_yourextkey_pi1[whatevervar] to your extension, takes care about caches, etc. Here is a feature list:
  • Custom templates are possible
  • Default CSS–based template available
  • Customizable number of pages before current and after
  • The following links can be generated (or switched off):
    • First page
    • Previous page
    • Next page
    • Last page
  • There can be indicators that there are more or less pages available that numeric links show (can be switched off)
  • Extensions can add extra query parameters to the generated page link. These parameters can be generated automatically from TS template using stdWrap
  • Current query string is automatically appended to the page link
  • cHash is properly generated
  • First page does not have a page parameter, which prevents double content issue (SEO–related)
  • Full detailed manual with all options explained
I am already using this extension privately and I plan to migrate comments extension to use this page browser. So if you need a page browser in your extension, give pagebrowse a try. I am sure you will like it.

8 comments:

  1. Hello dimitry,

    I am currently using your pagebrowser in comments, and the page is multilingual.
    Now I am trying to change the wordung of e.g. " < Last>>" for the protuguese language and am failing...
    I tried things like the following without changing anything:
    plugin.tx_pagebrowse_pi1 {
    _LOCAL_LANG.default{
    text_no_comments = Sem comentários
    text_first = Primeiro
    text_prev = Anterior
    text_next = Seguinte
    text_last = Último
    }
    }

    do you have any idea how to get this done?

    Thanks,

    Emil

    ReplyDelete
  2. Hi, Emil. I never used the method with _LOCAL_LANG, so I am not sure. You could simply copy html template and put strings there. Than use TS conditions to change the template:

    [globalVar = GP:L=1]
    plugin.tx_pagebrowse_pi1.templateFile = ....
    [global]

    ReplyDelete
  3. Is it just me or is the wording used in here and in the TER docs really off?

    Shouldn't the name of the variable $numberOfPages rather be $numberOfResults? This one made me stumble and even look through your source code if I misunderstood some basic thing.

    ReplyDelete
  4. hy! this is exactly what i'm looking for, but i can't get it to work. please help! how exactly do i have to place the function and call it? i always get a: "Using $this when not in object context in.." help is much appreciated! thanks * greets Akin

    ReplyDelete
  5. Hi Dmitry!

    The same thing lie Emil. I'm from Poland and I have to use such TS to have polish labels:

    plugin.tx_pagebrowse_pi1 {
    _LOCAL_LANG.default{
    text_first = <<
    text_prev = <
    text_next = >
    text_last = >>
    }
    _LOCAL_LANG.pl < ._LOCAL_LANG.default
    }

    In all other extensions I use it is enough to make such TS:


    plugin.tx_pagebrowse_pi1 {
    _LOCAL_LANG.pl {
    text_first = <<
    text_prev = <
    text_next = >
    text_last = >>
    }
    }

    I don't know why.

    ReplyDelete
  6. Hi Dimitry,

    I use your pagebrowser a lot, but also run into this anoying problem all the time. The page links contain contain the GET parameter but not the POST parameter. The only solution I've found so far was by modifying the code and adding '"method" => "POST,GET"' to $queryConf in getPageLink($page). $this->cObj->getQueryArguments() will than also take all POST vars into account.

    That can't be the way to go?

    Uwe

    ReplyDelete
  7. Page browser is made for flexibility. Adding GET variables is natural behaviour of page browser. POST is a different story because it is a part of CRUD concept, where only POST requests are allowed to modify data. Forcing POST would be inflexible and violate CRUD principles.

    ReplyDelete
    Replies
    1. In this particular case it is a search form with lots of input fields. The reason for using post is mostly simplicity, because it is much easier to create a link (containing the page id, the language parameter and probably other config linkvars) for the action attribute, than to put all vars (page id, language, cache) into hidden fields. I wonder if there is an elegant way in typo3 to add those basic parameters (page id, etc.) to a custom form?

      Uwe

      Delete