How to: Default-value hack for more flexibility (Part 1)

AppGini offers several ways for defining static or dynamic default values for new records. Sometimes there is need for more flexibility. In this tutorial I am going to show a small PHP hack which will allow you to define default values on output fields dynamically in a very flexible way.

Built in options

Static default values

If you need static initial values (default values) for new records in AppGini tables you can use the Default property in AppGini when modeling a column.

Dynamic default values

There are even dynamic default values which can be selected using the Auto-button. AppGini will insert a placeholder and dynamically replace it be the current value on insert.

These options a working well. But sometimes there is need for different default values which are not supported by default.

Example

In this example I am going to dynamically change a field named label in a table named contacts.

Please note that this is for demonstration purposes, only. It does not make sense in a real world app.

I would like to put the current user's full name as default value.

Please note that there is an option for putting the current users's memberID (username) there, but not for the full name.

Step 1: Locate the hook function

  1. Open the file hooks/contacts.php in your code editor and
  2. Find the function named contacts_dv.
function contacts_dv($selectedID, $memberInfo, &$html, &$args) {
}

Step 2: Only in “Insert” mode

We only want to change the value in "Add new" mode, so first of all we have to check if we are in insert-mode or not:

function contacts_dv($selectedID, $memberInfo, &$html, &$args) {

  if (!$selectedID) {
    // there is no $selectedID. This means that we as in INSERT-mode
  }

}

Step 3: Fieldname and Default value

Next we define the name of the field we want to change AND we define the new default value.

  • In this case I want to change the field named label.
  • It shall be set to the current user's full name.

Question:
How can we get the current user's full name?

In a standard installation you can get the member's full name from the $memberInfo array, which contains several custom values. Have a look at your profile:

After the email address, there are four custom values:

Please note that the four indexes here (see blue boxes above) are zero-based (0-3). So the first one is number 0.

Answer:
We can get the user's full name from $memberInfo["custom"][0]:

function contacts_dv($selectedID, $memberInfo, &$html, &$args)
{
  if (!$selectedID) {
    $fieldname = "label";
    $default = $memberInfo["custom"][0];

  }
}

Step 4: Change the value

Unfortunately there is no $data array in contacts_dv()-function. This means we cannot change the value of the field using PHP directly. But we can use PHP to inject Javascript code for changing the value of the field at runtime.

The TABLEAME_dv()-function gives us the $html variable which can be used for inserting additional html code and also javascript code. I'm using this option to add a <script>-tag and a jQuery call to change the value of the field.

The small script itself:

<script>
  $j("#FIELDNAME").val("DEFAULTVALUE");
</script>

The script embedded into PHP code:

$html .= '<script>$j("#' . $fieldname . '").val("' . $default . '");</script>';

The complete code of contacts_dv()-function:

function contacts_dv($selectedID, $memberInfo, &$html, &$args)
{
  if (!$selectedID) {
    $fieldname = "label";
    $default = $memberInfo["custom"][0];
    $html .= '<script>$j("#' . $fieldname . '").val("' . $default . '");</script>';
  }
}

Result

Now, on adding a new contact...


... the current user's full name will be inserted into the label field automatically:

That's it. This should work for all standard <input /> fields which can be modified using jquery's val("new value") function.

Next: Multiple fields

What about populating multiple fields at once?

Please continue reading with Part 2

Do you like it?

One Comment

  1. Pingback: How to: Default-value hack for more flexibility (Part 2) | AppGini Helper

Comments are closed.