Create if condition with modx UrlParam - modx-revolution

I got the value of the urlParam "image". No i want to show an image only if the Param is 1.
[[getUrlParam? &name=`image` &int=`1`]]
How can i do that?

You can use MODX output filters to achieve this.
Using the output filters (:eq and :then) you can show the image as you wish.
The output of the getUrlParam snippet is automatically submitted to the output filter :eq which then checks the desired value.
Please make sure that you use an uncached snippet call in this case ([[!getUrlParam...]]) because you are dealing with data that changes every time the page is re-rendered/called. If you fail to do this the value of the GET variable will be cached after the first page call which will lead to inconsitencies in your desired result.
[[!getUrlParam:eq=`1`:then=`<img src="/foobar.png" alt="Some img">`? &name=`image` &int=`1`]]

Related

How to access a scope if its name is being used as a query column

Dealing with some legacy code we came across a rather annoying situation. We are looping through a query with the <cfoutput query="x"> tag. That query has a column named 'url'. Within that loop we need to check if a key exists within the url scope. Since CF puts a priority on what's in the query over general page scopes I can't use a structKeyExists(url,"key") since as far as CF is concerned at this point, url is a string with the value from the current row of the query.
How can I break out of the query scope and inspect what's in my url?
As a temporary we are using isDefined("url.key"), but I would still like to know if there is a way to break out of the query scope.
Also can't really change the column, or even the column name in the query without a few hours of work tracking down an changing all references to it, so we're going to avoid that if at all possible.
EDIT:
There seems to be some confusion as to how this code is set up, and why the simple solutions don't apply. It would be hard for me to give a thorough example but I will try to clarify the situation.
There are many pages that would count as 'pageA' for the following example. Enough that changing how things work would require a change in scope and investment in time that's just not going to happen in the time allotted.
PageA runs a query with one of the columns being named url, then starts an output loop via cfoutput, inside that loop PageB is included. One PageA may have different variables in the URL scope than another PageA, actually they are the same, but may be named differently(varID=x in one case vid=x in another). Inside of PageB I need to use the value from that url scope, so I want to run through the different possible names (if key 'varID' exists in url, use it, otherwise use 'vid').
This is why I want to "punch through" the query scope to get the url structure, and not the url column from the query. Any other method seems to require modifying the many PageAs.
So the question is not how to solve this problem specifically, as there are many ways to do it, I would just really like to avoid them as they all add a lot of time in implementation and testing. The question remains, is there a way to access the url scope as a variable if url exists as a query column and you are in the query scope.
I thought it might work to create a function that returned the url scope, but upon testing it, even with a local-scoped query (which prevents the function using the query itself) the use of url inside the function is still corrupted:
<cffunction name="getUrlScope"><cfreturn Url /></cffunction>
...
<cfoutput query="x">
<cfif StructKeyExists( getUrlScope() , 'key' )>
<!--- still fails :( --->
There is however an undocumented (meaning unsupported and liable to change) option. If you dump getPageContext() you will see a bunch of functions that do interesting things, including dealing with scopes.
You can use getPageContext().SymTab_findBuiltinScope('URL') to get at the URL scope.
You can also use getPageContext().getCfScopes() to get an array of scopes. I'm not sure if the order is guaranteed fixed but it seems to be [cgi,?,url,form,cookie,?] checking on both CF10 and cflive (CF9), so possibly is.
(In CF8 there was the method getBuiltinScopes, which returned a struct instead of an array - this no longer appears to exist, reinforcing the whole unsupported and changeable nature of these methods.)
On Railo those don't work, but there is getPageContext.UrlScope() and similarly-named functions for the other scopes.
One solution would be to assign the url struct to a new variable outside of the cfoutput tag and then reference that variable instead of url. Example:
<cfset urlScope = url>
<cfoutput query="x">
<cfset keyExists = structKeyExists(urlScope, "key")>
</cfoutput>
My solution for this is always to alias the url column in the query as int
SELECT URL as qURL FROM myTable ...
IF you don't have access to the query (it's a stored precedure or used elswhere etc) you can always use query of a query to reselect it with your alias.
I don't care for the idea of creating a separate reference to URL outside the output - but that would also work. I just want to KNOW what is user input (i.e. comes from the URL or FORM) and what is generated internally (i.e. comes from a query).
Couldn't you move structKeyExists(url,"key") outside of the cfoutput block, and store that into a variable? Or do a structAppend to copy the url struct into another struct named something else?
Another approach is to replace your cfoutput block with a cfloop block.
<cfloop from="1' to = "#YourQuery.recordcount#" index = "idx">
<cfif StructKeyExits(url,"key")>
<cfoutput>
#url.key# is not the same as #YourQuery.url[idx]#
which can also be referenced like this #YourQuery["url"][idx]
etc

How do I copy the values of one form element to another?

This is a follow up to this questions:
How can I manipulate a form / inputs to be ignored when a form is submitted
I have the info displayed, the form, the show()s and hide()s, etc. Most everything seems to be progressing fairly well. Where I could use some input - no pun ontended - is on how to take the new values in the form and copy those to the display only div as part of the ajaxSuccess.
I can copy type=text and textarea but what about checkboxes, multi-selects and radios.
In short, once the new values in the form are submitted, I need to update the page (not as a form) with those new values. Perhaps I need to write some for each form element type? Even so, I'm not quite sure where to start.
Pardon me if my working isn't clear. If that's the case just ask what you'd like me to clarify.
The way that will make most sense is to have the script that is processing your form return the form contents in the ajaxSuccess data. Here's an example:
The javascript:
$.ajax({
url: 'ajax/process.php',
success: function(data) {
// Load returned data into page
$('#result').html(data);
}
});
The php:
var $input1 = $_POST['input1'];
var $input1 = $_POST['input1'];
// DO SOMETHING WITH VARS AND IF SUCCESSFUL RETURN
if($success){
echo "Thanks! Here is your info: ".$input1." and ".$input2;
}
Once the data has been returned you can manipulate it anyway you like. For example if you needed to separate out the different values from the form you could post a comma separated string back from the PHP and use the javascript .split() method to create an array from that string.
Hope that's helpful :)
I'd say that your best bet is to take a different approach altogether, and reload your content from the database after it's been updated - in this case via an AJAX call. The reason being that your posted content from the form may not match what ends up being saved to the database, as some of your fieldtypes may process and alter the data they're passed (for example, stripping tags, formatting text, etc). This way the refreshed content exactly matches what is displayed when the same content comes from the Channel Entries call.

Can someone explain Rails behavior for multiple fields with identical names and ids?

I needed to create a input mask for a jQuery datepicker in my Rails app, where the first form field uses m/d/yy format and the datepicker populates a hidden input with the proper database format.
I was using SimpleForm, and so I extended my own input so that the input is preceded by the mask.
I got everything set up and when checking out the browser, it all just worked well before I thought I would be done.
The form ends up with two inputs for the same attribute, each with the same id and name. I never thought this would work. Checking the development log I only see one date getting submitted, the second of the two which is the one that has the proper format for the database.
Is this all okay? Should I take some extra steps even though this appears to work fine, and more importantly, can someone explain what's going on under the hood that results in this behavior?
Thanks!!
Rails uses the Hash params to store fields submitted. When you declare two or more inputs using the same name, it happens the same as if you do something like
h=Hash.new
h[:name]="foo"
h[:name]="bar"
Result is bar because the foo was overwritten. So the "winner" is always the field value which the browser last appended to postdata.
But if I where you, I would not rely on the browser that the second field gets appended at last.

Fill a rails form with a hashmap

I have a difficult situation.
I let the the user create a form through a Rich Text Editor and then I save this.
So for example, I save this literally into my DB:
http://pastebin.com/DNdeetJp (how can you post HTML here? It gets interpreted, so now I use pastebin...)
On another page I wrap this in a form_tag and it gets presented as it should be.
What I want to do is save this as a template and save the answers as a hashmap to my DB.
This works well, but the problem is I want to recreate what checkbox/radiobutton/... is selected when the user goes back to the page. So I want to fill the form with the answers from the hashmap.
Is there a way to use a 'dummy' model or something else to accomplish this?
Thanks!
Since you're pasting in raw HTML which is not properly configured as a template, it is more difficult to enable the proper options based on whatever might be stored in your DB.
The reliable approach to making this work is to use Hpricot or Nokogiri to manipulate the bit of HTML you have and substitute values accordingly. This isn't too hard so long as you can define the elements in that form using a proper selector. For example, create a div with a unique id and operate on all input elements within it, comparing the name attribute with your properties. There may even be a library for this somewhere.
The second approach is to use JavaScript to enable the options in much the same fashion. This seems like a bit of a hack since the form itself will not have a proper default state.

Changing the order of params in pagination links

I am using Cakephp to perform a search through sphinx. I wanted to do modify the default structre of pagination links generated by cakephp
For example
From:
localhost/search/page:1/key1:google/key2:code
To:
localhost/search/key1:google/key2:code/page:1
I want the page number to appear at the end. Is there a way this can be done?
Any help appreciated
I would suggest that you modify the PaginatorHelper. I would recommend that you extend it and load it when Cake launches so that you dont modify the Cake file in cake/libs...
Then, wherever you want the output URL to be printed w/ the page number at the end, you'll need to modify the PaginatorHelper... I'd suggest you search for the page/# key/value pair. If you find it, remove it, then append it to the end of the string. Then return that value.
Edit: link to PaginatorHelper in the API - http://api.cakephp.org/class/paginator-helper

Resources