1. NOTICE: If you are using Fabrik and update to Joomla 3.10, you will need to update to Fabrik 3.10. And, if you are using Fabrik, do not upgrade to Joomla 4, we do not have a supported version ready for release. More information on a release date coming soon. Also, please note that Fabrik 3.10 will not install on any Joomla sites less than 3.8.
    Dismiss Notice

Setting a default value for a dropdown from the front end

Discussion in 'Community' started by BasilC, Nov 26, 2021.

  1. BasilC

    BasilC Member

    Level: Community
    I have a membership database which includes a fees payments table with an element recording the year that a payment relates to. The membership year runs from 1st May to 30th April. While payments are usually for the current year or for the year that will commence in a few weeks time, this is not always the case, so the person administering the database has to be able to choose which membership year to assign each payment to.

    The field uses the dropdown plugin, which works well except for one point which could be improved. For the sake of convenience, I set a default value for the dropdown corresponding ti the year that most new payments will relate to. As things are, this can only be done from the back end. It would be good if the person using the database was able to set the default from the front end, but I can't find any way of enabling this (they are able to add new years to the dropdown, but not new defaults).

    Alternatively, if that isn't possible, is there any way that some php code could be used to automatically generate the values for the dropdown list (presumably quite easy to generate a list of the required years, both before now and in the future) while also generating the default? I suspect not, as the default is stored in the params field in the fabrik_elements table ("sub_initial_selection").

    It's obviously no big deal for me to edit the default once a year, I'm really just curious as to whether there's a way to enable the front end or to automate it.
     
  2. juuser

    juuser Well-Known Member

    Level: Community
    There are very few things that cannot be accomplished with Fabrik when extending it with a bit of custom code :).

    You can populate dropdown options in Advanced -> Eval populate field like:
    Code (Text):
    $start_year = 2015;
    $end_year = date("Y") + 1;

    $options[] = JHTML::_('select.option', 0, "Select year...");
    for ($x = $start_year; $x <= $end_year; $x++) {
         $options[] = JHTML::_('select.option', $x, $x);
    }

    return $options;
    And set the default in Options -> Default field (set "Eval" to "Yes"). With a custom query you can use a "dynamic" default year something like:
    Code (Text):
    $mydb = JFactory::getDBO();
    $mydb->setQuery("SELECT MAX(year) FROM table-name WHERE user = '{table-name___user_id_raw}' ");
    return $mydb->loadResult();
    Of course adjust the code to match your set-up and table/element names.

    Please note that the "default" only works for new records. When editing existing one, it takes the value that is saved in database field for that element.
     
    Last edited: Nov 26, 2021
    troester likes this.
  3. BasilC

    BasilC Member

    Level: Community
    Thanks juuser. I'm tied up with other things at the moment but will give this a go in a couple of days time. The default is definitely needed only for new records, so that's not a problem.
     
  4. BasilC

    BasilC Member

    Level: Community
    How foolish of me to think there was something that couldn't be done using Fabrik!

    I've now got the dropdown population and default value both working, thanks to your help.

    The scenario I described was somewhat simplified, so I had to adapt.

    The dropdown has both value (eg 2022-04-30) and label (eg 2021/22). The value is actually stored as a date so that I can calculate whether a member is late with their payments. (I have to go into the database table and change the field from text to date after changing anything in Fabrik).

    PHP:
    $start_year = 2014;
    $end_year = date("Y") + 3;

    $options[] = JHTML::_('select.option', 0, "Select year...");
    for ($x = $start_year; $x <= $end_year; $x++) {
         $options[] = JHTML::_('select.option', $x . '-04-30', $x - 1 . '/' . substr($x,-2));
    }
    return $options;
    The extra years are there just to reassure the membership admin person that she isn't going to run out of years just yet!

    The default doesn't depend on who's paying but on what the current date is. The membership year ends on 30th April and it's assumed that anyone paying during March will be paying towards the next membership year not the current membership year that only has two months to run.

    So the code I used was

    PHP:
    if (date("m") > 2)
    {
    $expiry = date("Y") + 1 . '-04-30';
    }
    else
    {
    $expiry = date("Y") . '-04-30';
    }
    return $expiry;
    Many thanks, I would never have worked it out for myself!
     
    juuser likes this.
  5. achartier

    achartier Active Member

    Level: Community
    Just a heads up. The select options as created above are calculated when the form is created, but they also run when viewing the list data. If you have older records for years prior to the start_year you may not get the result you want in the list view. The value stored in the database will not have a corresponding option when the eval populate runs. I suggest you check in the eval populate if this is an existing record and if so make sure the existing database year is included in the options list.

    I have run into this before. It is also problematic in the same case when you trim the options list based on existing chosen records (not your case but just for further info). When you do this the eval populate will never have the value in the database. Again, if you check for an existing record you can handle this.
     
    troester likes this.
  6. BasilC

    BasilC Member

    Level: Community
    Thanks, I wasn't aware of that, but fortunately I used 2014 as the start_year, as I was pretty sure that was when the U3A was set up. After reading your post, I checked, and I was right, it began in April 2014 and the earliest payment records still show the correct year.

    I assume that there's no need to run the additional check you suggest, as all years from 2014/15 onwards are being generated by the php code?
     
  7. juuser

    juuser Well-Known Member

    Level: Community
    As long as your start and end year in the php-code cover the years that might be in database, there's no need to worry.
     

Share This Page