Skip to content

Commit

Permalink
Use 'cb' to mark callback and call it with call_user_func or call_use…
Browse files Browse the repository at this point in the history
…r_func_array. Added function requiredFieldsJs() that builds global array in javascript with required fields for a form so it can be checked before submitting form.

git-svn-id: http://voip.null.ro/svn/ansql/trunk@138 dbfed7de-b0aa-0410-b6a1-c7e608b77fc9
  • Loading branch information
monica committed Jan 22, 2015
1 parent 9c65560 commit 4eb66d8
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 28 deletions.
2 changes: 1 addition & 1 deletion config.php.sample
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ $custom_actionlog = false; // if true you can define your own action log class s

// make callback to this function in case backup connection is used
// log_used_backup_conn(2); // 2/3.. - index of backup connection used
// $callback_when_backup = "log_used_backup_conn";
// $cb_when_backup = "log_used_backup_conn";

// password to be allowed to control error reporting and displaying of the queries
$pass_debug_page = "secret_debug";
Expand Down
6 changes: 3 additions & 3 deletions framework.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ public static function disconnect()
*/
public static function connect($connection_index="")
{
global $db_type, $callback_when_backup, $exit_gracefully;
global $db_type, $cb_when_backup, $exit_gracefully;

Debug::func_start(__METHOD__,func_get_args(),"framework");

Expand Down Expand Up @@ -251,8 +251,8 @@ public static function connect($connection_index="")
die("Could not connect to the database");
else
return;
} elseif (isset($callback_when_backup) && function_exists($callback_when_backup))
$callback_when_backup($next_index);
} elseif (isset($cb_when_backup) && is_callable($cb_when_backup))
call_user_func_array($cb_when_backup, array($next_index));
} else if ($db_type == "mysql")
mysql_select_db(${"db_database$connection_index"},self::$_connection);

Expand Down
48 changes: 40 additions & 8 deletions javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,13 @@ function set_html_obj(id, html)
obj.innerHTML = (html == null) ? "" : html;
}

function make_request(url, callback)
function make_request(url, cb)
{
url = encodeURI(url);
make_api_request(url, callback);
make_api_request(url, cb);
}

function make_api_request(url, callback)
function make_api_request(url, cb)
{
xmlhttp = GetXmlHttpObject();
if (xmlhttp == null) {
Expand All @@ -164,20 +164,20 @@ function make_api_request(url, callback)
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4) {
var response = xmlhttp.responseText;
call_function(callback,response);
call_function(cb,response);
}
}
xmlhttp.open("GET", url, true);
xmlhttp.send(null);
}

function call_function(callback, response)
function call_function(cb, response)
{
if (callback && typeof(callback) === "function") {
if (cb && typeof(cb) === "function") {
// execute the callback, passing parameters as necessary
callback(response);
cb(response);
} else
alert(typeof(callback));
console.error("Trying to call invalid callback "+cb.toString()+", type for it is: "+typeof(cb));
}

function GetXmlHttpObject()
Expand All @@ -194,3 +194,35 @@ function GetXmlHttpObject()
}
return null;
}

/**
* Check if required fields were set.
* The required_fields global variable is checked
* Ex: required_fields={"username":"username", "contact_info":"Contact information"}
* "contact_info" is the actual field_name in the form while "Contact information" is what the user sees associated to that form element
* @return Bool. True when required fields are set, false otherwise
* If required_fields is undefined then function returns true and a message is logged in console
*/
function check_required_fields()
{
if (typeof(required_fields) === "undefined") {
console.log("The required fields are not defined!");
return next_step(step_no, wizard_name);
}

var err = "";
// variable required_fields is a global array
// it is usually created from method requiredFieldsJs() from Wizard class

var field_name, field_value;
for (field_name in required_fields) {
field_value = window.document.getElementById(field_name).value;
if (field_value=="")
err += "Please set "+required_fields[field_name]+"! ";
}
if (err!="") {
error(err);
return false;
}
return true;
}
65 changes: 49 additions & 16 deletions lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,25 +252,25 @@ function link_to_main_page($path, $return_text)
/**
* Prints a message as a notice or an error type message and calls a function
* @param $message String The message to be displayed.
* @param $next String Setting it to 'no' stops the callback function.
* @param $next_cb Callable/String. Setting it to 'no' stops the performing of the callback.
* @param $no_error Boolean If is true a message id displayed
* else an error type message is displayed. Defaults to true.
*/
function notice($message, $next=NULL, $no_error = true)
function notice($message, $next_cb=NULL, $no_error = true)
{
Debug::func_start(__FUNCTION__,func_get_args(),"ansql");
global $module;

if (!$next)
$next = $module;
if (!$next_cb)
$next_cb = $module;

if ($no_error)
print '<div class="notice">'.$message.'</div>';
else
print '<div class="notice"><font class="error">Error!! </font>'.$message.'</div>';

if ($next != "no")
$next();
if ($next_cb != "no")
call_user_func($next_cb);
}

/**
Expand Down Expand Up @@ -906,11 +906,11 @@ function display_pair($field_name, $field_format, $object, $form_identifier, $cs
if (isset($field_format["value"]))
$value = $field_format["value"];

if (!strlen($value) && isset($field_format["callback_for_value"]) && is_callable($field_format["callback_for_value"]["name"])) {
if (count($field_format["callback_for_value"])==2)
$value = call_user_func_array($field_format["callback_for_value"]["name"],$field_format["callback_for_value"]["params"]);
if (!strlen($value) && isset($field_format["cb_for_value"]) && is_callable($field_format["cb_for_value"]["name"])) {
if (count($field_format["cb_for_value"])==2)
$value = call_user_func_array($field_format["cb_for_value"]["name"],$field_format["cb_for_value"]["params"]);
else
$value = call_user_func($field_format["callback_for_value"]["name"]);
$value = call_user_func($field_format["cb_for_value"]["name"]);
}

print '<tr id="tr_'.$form_identifier.$field_name.'"';
Expand Down Expand Up @@ -1127,11 +1127,11 @@ function display_pair($field_name, $field_format, $object, $form_identifier, $cs
if (isset($field_format["advanced"]))
print '<input type="hidden" name="'.$form_identifier.$field_name.'">';

if(!function_exists($display))
Debug::trigger_report('critical', "Function $display is not implemented.");
if (!is_callable($display))
Debug::trigger_report('critical', "Callable ".print_r($display,true)." is not implemented.");

// callback here
$value = $display($value,$form_identifier.$field_name);
$value = call_user_func_array($display, array($value,$form_identifier.$field_name));
if ($value)
print $value;
}
Expand Down Expand Up @@ -3644,7 +3644,7 @@ function missing_param($param)
function generic_tabbed_settings($options,$config,$section_to_open=array(),$show_advanced=false, $form_name=null,$form_submit=true,$specif_ind="",$custom_css=null)
{
Debug::func_start(__FUNCTION__,func_get_args(),"ansql");
global $module, $tabs_callback;
global $module, $tabs_cb;

if (!$form_name)
$form_name = $module;
Expand Down Expand Up @@ -3720,8 +3720,8 @@ function generic_tabbed_settings($options,$config,$section_to_open=array(),$show
$names[] = $name;
generic_tabbed_settings($names, $sub_sections, array("$first_subsec"=>""), false, "", false, $js_name."_", "subsec");
} else {
if (isset($tabs_callback))
$tabs_callback($name, $current_fields);
if (isset($tabs_cb))
$tabs_cb($name, $current_fields);
editObject(NULL,$current_fields,NULL,"no");
}
print "</div>";
Expand Down Expand Up @@ -3792,5 +3792,38 @@ function include_javascript($module=null)
include("javascript/$module.php");
}

/**
* Builds global javascript array map variable: required_fields
* Ex on generated object: required_fields={"username":"username", "contact_info":"Contact information"}
* @param $form_fields Array. Fields
* @param $exclude_fields Array. Array with key step names that should be excluded
* Ex: array("step_name", "step_image", "step_description") -- usage when used from Wizard class
*/
function requiredFieldsJs($form_fields, $excluded_fields)
{
?><script>
// save the name of the required fields: field_name:column_name
// column_name is the field the user sees, field_name is the key in the current step array
required_fields = {};
<?php

foreach ($form_fields as $field_name=>$field_def) {
if (isset($excluded_fields[$field_name]))
continue;
if (isset($field_def["display"])) {
if ($field_def["display"] == "message" || $field_def["display"] == "fixed")
continue;
}

if ( (isset($field_def["required"]) && ($field_def["required"]===true || $field_def["required"]=="true")) ||
(isset($field_def["compulsory"]) && ($field_def["compulsory"]===true || $field_def["compulsory"]=="true"))
) {
echo "required_fields[\"".$field_name."\"]=\"".$real_name."\";";
}
}
?>
</script><?php
}

/* vi: set ts=8 sw=4 sts=4 noet: */
?>

0 comments on commit 4eb66d8

Please sign in to comment.