May 15th, 2015, 1:19 PM
So, this is a bit of a hack, but I decided to make use of the default value functionality and extend it to allow reading from the database.
I found that the default values are read in ft_get_new_view_submission_defaults (in global/code/views.php)
I couldn't find a hook for this function, so had to modify it directly.
I am integrating this into another system, so I can push data into GET/POST variables and redirect the user to the add new submission page.
I can then use those variables to construct a SQL statement that will load the correct data.
So, in the default value for a field, I specify a string something like this:
SQL:SELECT name FROM member WHERE member.id = '{{member_id}}';
{{member_id}} is a placeholder to read the value from $_REQUEST, so in ft_get_new_view_submission_defaults, I check for the "SQL:" at the start of the string, if it's present, substitute the variables, execute the query and store the result back as the default value.
This is my new modified function;
I found that the default values are read in ft_get_new_view_submission_defaults (in global/code/views.php)
I couldn't find a hook for this function, so had to modify it directly.
I am integrating this into another system, so I can push data into GET/POST variables and redirect the user to the add new submission page.
I can then use those variables to construct a SQL statement that will load the correct data.
So, in the default value for a field, I specify a string something like this:
SQL:SELECT name FROM member WHERE member.id = '{{member_id}}';
{{member_id}} is a placeholder to read the value from $_REQUEST, so in ft_get_new_view_submission_defaults, I check for the "SQL:" at the start of the string, if it's present, substitute the variables, execute the query and store the result back as the default value.
This is my new modified function;
PHP Code:
function ft_get_new_view_submission_defaults($view_id)
{
global $g_table_prefix;
$query = mysql_query("
SELECT *
FROM {$g_table_prefix}new_view_submission_defaults
WHERE view_id = $view_id
ORDER BY list_order
");
$info = array();
while ($row = mysql_fetch_assoc($query))
{
/*
* Modification to allow an SQL query be specified as a source of default values
* GET/POST variables are read for WHERE clause
*/
// look for the "sql" string start
if(strtolower(substr($row['default_value'],0, 4)) == "sql:"){
$sql=substr($row['default_value'], 4);
// do template variable replacement {{variable_name}} against REQUEST vars
if (preg_match_all("/{{(.*?)}}/", $row['default_value'], $m)) {
foreach ($m[1] as $i => $varname) {
$sql = str_replace($m[0][$i], sprintf('%s', mysql_real_escape_string($_REQUEST[$varname])), $sql);
$row['default_value']=mysql_result(mysql_query($sql),0);
}
}
}
$info[] = $row;
}
return $info;
}