Ninja Forms 3 - Pre-select radio button based on URL variable - ninja-forms

A WordPress page with the Ninja Form (v3) has a radio-list, and I need to have a specific radio-button pre-selected based on a URL variable passed in:
e.g. http://the-website.com/ninja-form-page/?tour=2 would result in the second radio button in the list (with a value=2) to be pre-selected when the form page loads.
This was easily accomplished in Ninja-Forms v2x with a filter in the functions.php that checks for the URL variable and loops through the form-field labels for a match (http://www.wpmusketeer.com/pre-populate-ninja-forms-fields-from-url-parameters/). But I can't figure out a solution in v3x. It seems that the form fields render quite differently, and there is no API section in the v3x documentation like there was in v2x.

Use "ninja_forms_render_default_value" filter:
add_filter( 'ninja_forms_render_default_value', 'my_ninja_forms_pre_populate', 10, 3 );
function my_ninja_forms_pre_populate( $default_value, $field_type, $field_settings ){
if( 'field_key' == $field_settings[ 'key' ] ){
$default_value = 2;
}
return $default_value;
}
Of course - register your URL variable as "query_vars"

Related

Get input type in Geb

I am using Geb to write a script that will test a survey many times with random input to make sure nothing breaks. I would like to iterate through every input on a form and do different things depending on the input type.
For example:
while ($("form").find("input", j)) {
if($("form").find("input", j) == "checkbox"){
//check it sometimes
}
else if($("form").find("input", j) == "select"){
//select a random option
}
j++
}
I'm not exactly sure how the survey is going to work yet, so I would rather go down the page as a human would instead of doing all checkboxes first and then all selects etc. It is possible to check the type of input like my example?
Additionally, I notice I am repeating the $("form").find("input", j). Can I make that into a variable like in jQuery?
Simply collect all your inputs and selects in a navigator and iterate over all of them. Use is(String tagName) to detect select elements and #type attribute accessor to detect other element types:
def form = $("form")
def formElements = form.find("input") + form.find("select")
formElements.each { Navigator element ->
if (element.is("select") {
//select a random option
}
if (element.#type == "checkbox") {
//check it sometimes
}
}

Dojo:how to find if the widget has focus in dojo

how do I find out if my custom widget has focus in Dojo?
i have dojo editor i wnat to know if the editor has already focus or not?
you can use the module dijit/focus to find out the focus
FROM DOJO DOCS
Tracking active widgets
At any point in time there is a set of (for lack of a better word)
“active” or “focused” widgets, meaning the currently focused widget
and that widget’s ancestors. “Ancestor” can mean either DOM ancestor
(ex: TextBox –> Form), or a logical parent-child relationship (ex:
TooltipDialog –> DropDownButton).
For example, if focus is on a TextBox inside a TabContainer inside a
TooltipDialog triggered by a DropDownButton, the stack would be
TextBox –> ContentPane –> TabContainer –> TooltipDialog –>
DropDownButton.
The activeStack[] parameter indicates this set of widgets, and an app
can monitor changes to activeStack[] by:
require([ "dijit/focus" ], function(focusUtil){
focusUtil.watch("activeStack", function(name, oldValue, newValue){
console.log("Focused widget + ancestors: ", newValue.join(", "));
});
});
the question in title has a different answer than the one in the descriptions.
there are two ways achieving the question in the title, by using dojo's focusUtil ("dijit/focus"). both ways give you something that you could find the widget using it and the dijit's registry ("dijit/registry").
focusUtil.curNode: gives you the DOM Node that currently has the focus. the function below, you could get the widget reference.
function getWidgetByNode(node){
var result;
while (!result && node){
result = registry.byNode(node);
if (node.parentElement)
node = node.parentElement;
else
node = null;
}
return result;
}
var focusedWidget = getWidgetByNode(focusUtil.curNode)
focusUtil.activeStack: gives you an array of the widgets (parent to child) that has the focus. so the last item in the array is the direct widget which has the focus. index values are widget ids, so you should get the widget by the following code
var focusedWidgetId = focusUtil.activeStack[focusUtil.activeStack.length-1];
var focusedWidget = registry.byId(focusedWidgetId);
now if you want to know if the currently focused widget is some specific one, it depends on what you have in hands from that specific widget:
widget itself: like the return values of above samples. now you have to compare if these are the same thing. you can not compare two widget objects using the == operator. you could compare their ids like this:
myWidget.id == focusedWidget.id
widget's id: this way you just easily get the id of the current node from focusUtil and compare it with the id you have liek this:
myWidgetId == focusedWidgetId
references:
http://dojotoolkit.org/reference-guide/1.9/dijit/focus.html
http://dojotoolkit.org/reference-guide/1.9/dijit/registry.html
require([ "dijit/focus" ], function(focusUtil){
var activeElement = focusUtil.curNode; // returns null if there is no focused element
});
check blow url here you can see some examples
http://dojotoolkit.org/reference-guide/1.8/dijit/focus.html#dijit-focus
a) For dojo 1.6: call dijit.getFocus(). This will return an object containing the currently focused dom node, among other things (selected text, etc.). To get the corresponding widget, simply do:
var activeElement = dijit.getEnclosingWidget(dijit.getFocus().node);
This is the full reference for dijit.getFocus(), from the source code:
// summary:
// Called as getFocus(), this returns an Object showing the current focus
// and selected text.
//
// Called as getFocus(widget), where widget is a (widget representing) a button
// that was just pressed, it returns where focus was before that button
// was pressed. (Pressing the button may have either shifted focus to the button,
// or removed focus altogether.) In this case the selected text is not returned,
// since it can't be accurately determined.
//
// menu: dijit._Widget or {domNode: DomNode} structure
// The button that was just pressed. If focus has disappeared or moved
// to this button, returns the previous focus. In this case the bookmark
// information is already lost, and null is returned.
//
// openedForWindow:
// iframe in which menu was opened
//
// returns:
// A handle to restore focus/selection, to be passed to `dijit.focus`.
b) For dojo 1.7 and up, use dijit/focus:
require([ "dijit/focus" ], function(focusUtil) {
var activeElement = focusUtil.curNode; // returns null if there is no focused element
});

DOMPDF: page_script(): Check current page content?

I am using dompdf to generate reports which sometimes has a front page, and sometimes has some attachments. The main content has a header and a footer, but the front page and the attachments (the last 3 to 5 pages of the pdf) should not contain header and footer. I'm placing the header and footer with a inline php page_script(), like this:
<script type="text/php">
if (isset($pdf) ) {
$pdf->page_script('
if ( $PAGE_COUNT > 10 && $PAGE_NUM == 1) {
//front page footer
}else{
//other pages' header and footer
}
');
}
</script>
The whole report is built by a database engine which outputs it all as a temporary html.txt-file which is then read into DOMPDF.
Now, to recognize the front page I just have to check if the page number is = 1. If any attachments are added (which are comprised of 1000px-height jpg images, each on their own page)
Does anyone have an idea for how to identify these "attachment"-pages and get DOMPDF to not render a header and footer on those pages? Or is there any way I could check within the page_script()-script whether the current page contains only an image (or perhaps an image with a specific class or identifier)?
Thanks for any help,
David
Detecting the current page contents may be possible, but I'd have to research exactly what you can do at this level. An easier method would be if you could inject some inline script in your generated document. After the main content and before the attachments you could add something like $GLOBALS['attachments'] = true; and then add a check on the status of this variable to your conditional.
<script type="text/php">
if (isset($pdf) ) {
$pdf->page_script('
if ( $PAGE_COUNT > 10 && $PAGE_NUM == 1) {
//front page footer
}elseif ($GLOBALS['attachments']) {
//attachments actions
}else{
//other pages' header and footer
}
');
}
</script>
Of course, don't forget to initialize the variable to false at the top of the document.
(from https://groups.google.com/d/topic/dompdf/mDsYi8Efnhc/discussion)

Dojo EnhancedGrid and programmatic selection

Here's my problem: in my application I have a Dojo EnhancedGrid, backed up by an ItemFileReadStore. The page flow looks like this:
The user selects a value from a selection list.
The item from the list is posted on a server and then the grid is updated with data from the server (don't ask why, this is how it's supposed to work)
The new item is highlighted in the grid.
Now, the first two steps work like a charm; however, the third step gave me some headaches. After the data is successfully POSTed to the server (via dojo.xhrPost() ) the following code runs:
myGrid.store.close();
myGrid._refresh();
myGrid.store.fetch({
onComplete : function(items) {
for ( var i = 0; i < items.length; i++) {
if (items[i].documentType[0].id == documentTypeId) {
var newItemIndex = myGrid.getItemIndex(items[i]);
exportMappingGrid.selection.deselectAll();
exportMappingGrid.selection.addToSelection(newItemIndex);
}
}
}
});
Now, the selection of the grid is updated (i.e. the selection object has a selectedIndex > 0), but visually there's no response, unless I hover the mouse over the "selected" row. If I remove the .deselectAll() line (which I suspected as the culprit) then I sometimes end up with two items selected at once, although the grid selectionMode attribute is set to single.
Any thoughts on this one?
Thanks a lot.
You need to use setSelected(), like so
exportMappingGrid.selection.setSelected(newItemIndex, true);
The second parameter is true to select the row, false to unselect it.
This is what works for me:
grid.selection.clear();
grid.selection.addToSelection(newItemIndex);
grid.selection.getFirstSelected();
Jon

Phpbb new successful member registration pop up window

I would like to create a pop up welcome message for the new member that's successfully registered.
But I m having problem of finding where should I put the code, I have check the ucp_register.html ,, but I don't think that is the display content after the member successfully registered, can anyone help me please? Thanks
It would likely to be a more robust solution to display the popup on the first time the user is logged in as an activated user -- after registration they may not be activated, or they may close the browser window immediately after registration.
The way to do this would be to add a column (say, user_JBL_seen_message INT to the phpbb_users table in the database, then modify functions.php to check that column:
In functions.php, find:
// The following assigns all _common_ variables that may be used at any point in a template.
Before, add:
if($user->data['is_registered'] && $user->data['is_active'] && !$user->data['is_bot'])
{
if(isset($user->data['user_JBL_seen_message']) && !$user->data['user_JBL_seen_message']))
{
$showPopup = true;
$sql = 'UPDATE ' . USERS_TABLE . ' SET user_JBL_seen_message = 1
WHERE user_id = ' . (int)$user->data['user_id'];
if (!$result = $db->sql_query($sql))
{
return false;
}
}
}
Then, find:
$template->assign_vars(array(
After, add:
'JBL_POPUP' => $showPopup,
Then, you can add the popup HTML code to your overall_header.html template file, where appropriate...
<!-- IF JBL_POPUP -->
.... your HTML popup code here.....
<!-- END IF -->
If you don't want existing users to see the popup, then fill the new column with 1s.
I also agree with Damien's suggestion to use a jQuery UI dialog rather than a popup -- most users' browsers will block popups. However, use jQuery in noconflict mode to avoid conflicts with other mods.