store data in array - coldfusion

I have used cfhttp to read a .cfm file, but I want to store the data in one variable or array and pass this variable to a cfchart to display it in chart format. How can I do this?

Use XMLParse and then pull the information you need out of the XML Object and put it into an array or struct or what ever format you need.
As an aside you need to take a look at the online references I've been giving you before asking a question and you also need to mark the previous questions you've asked as answered.

The answer to this will depend on what the .cfm page is returning. If the returned value is XML, then #stephen is pretty much dead on. XMLParse will convert well-formed XML into nested structures and arrays. You can dump them to view the structure, and loop over them to insert it into the array you need.
If the .cfm page returns a list, you can use listToArray() to convert that directly into an array.
If you get name-value pairs, you'll have to do a little work to assign the data correctly, but there are several approaches.
If you edit your question to include more info about the data being returned, and maybe a sample of both the data and what you need it transformed into, we could probably give more specific advice.

Depending on just exactly what your accessing on the .cfm page you're loading via cfhttp, you could try something like:
<cfhttp method="Get"
url="somepage.cfm"
result="myResult">
<cfset PageLoad = ArrayNew(1) />
<cfset PageLoad = #ArrayAppend(PageLoad, myResult.FileContent)# />
I use a variation of that code to return and store a call/response to my custom url shortener...
Hope it helps!

Related

Drupal 8 Webform: how to display text input on one page on the next page?

I am trying to develop a multistep webform in Drupal 8 using Webform 8.x-5.1. I have written a WebformHandler that extends Drupal\webform\Plugin\WebformHandlerBase and made it available to the webform.
In the first step of the webform, I collect a text-field. I would like to display the value of that text-field in an HTML element (Advanced HTML/Text or Basic HTML) on the second page after doing some computation.
I have overwritten submitForm() in the WebformHandler and in it assign the value I want to the HTML element as follows:
$form['elements']['page_name']
['advanced_html_element']['#text'] = '...my HTML...';
Using ksm() I can see that this assignment works, but the the HTML element is not rendered with my HTML: the element is either invisible or contains the initial value set up in the form editor.
Clearly I'm missing something. Should I be using something other than submitForm? Can anyone help me?
It's been a long haul, but I've finally worked out how to do what I want to. The following works for me.
Firstly, I discovered the method validateForm in WebformHandlerBase. On each page in a form with multiple pages, you will find that the following methods are called in the order given here:
submitForm (called once)
alterForm(called possibly more than once)
validateForm (called once)
The name validateForm leads me to believe I may be misusing this method, but that is where I set up the elements on the following page that I wish to programmatically initialise. It works, so what the hey!
In validateForm, I initialise the elements that appear on the following page as follows:
$form_state->setValue(<element name>, <data structure>);
The <element name> is the name you give the element in the form editor ("Build" tab). The <data structure> has to be correct, of course: I suggest you find the appropriate structure by first filling in the element on the next page manually and seeing what turns up in $form_state.
There is also a $form_state->getValue(<element name>), which seems to me to mean that $form_state can also be used for storing session data, say in hidden fields. I initially used Drupal::service('tempstore.private')->get('xxx') for storing data that had to be available across page boundaries, but $form_state might be a cleaner solution.
I hope this helps someone: I spent a horribly long time trying to get this to work.

Coldfusion: How to dump out arguments scope with cfthrow?

After my form submits I am calling a controller method that runs an orm EntitySave in my cfc. I would like to dump out the arguments before I save my data via ORM just to visually validate those are indeed the values I want to save in the database.
So when I use this
<cfthrow message="value = #arguments#">
I am getting this:
Error: Complex object types cannot be converted to simple values.
I understand you are not allowed to do this with complex objects, so in those cases I would use <cfdump> but I can't find a way to dump in a <cfthrow>. I am sure there is a better way to accomplish this. I have also tried doing a <cfmail> to myself which works amazingly but the email will take a minute or two. Any suggestions would be greatly appreciated. I am currently checking into ValidateThis.
You could serialise it:
<cfthrow message="value = #serializeJson(arguments)#">
But I don't think you want that sort of thing showing up on the screen.
I'd log it if I was you (so same notion, just <cflog> before the <cfthrow>, and put the arguments in the log entry, and in the <cfthrow> just put a brief explanation of the error (you should also use a TYPE attribute, for subsequent handling of the exception that you've raised.
Rather than throwing it, you could try dumping it to a file and see if that meets your needs:
<cfdump var="#arguments#" output="C:\dump.html" format="html">
If you need to abort (as a throw would do), you can add abort on to the end of the tag, <cfdump... abort>.
You could do the following in order to use <cfdump>:
<cfsavecontent variable="arguments_dump">
<cfdump var="#arguments#" />
</cfsavecontent>
<cfthrow message="#arguments_dump#" />
Takes a little bit more code however and is not as elegant as Adam Cameron's answer above.

How can I have ColdFusion tags in a variable and have them evaluated?

I've get a variable that can contain a CF custom tag. E.g.
<cfset a = '<model:sparkline id="1"/>'/>
And I'd like that to be evaluated into HTML and outputted. Not sure how/if I can do this.
Can you modify the custom tag? If so you can use the caller scope to set a variable in the calling page. So inside the custom tag you could do <cfset caller.a = "whatever" /> and that will set the value in the calling page's variables scope.
If you don't want to modify the custom tag, then you can use <cfsavecontent> to save the output to a variable. Example:
<cfsavecontent variable="a">
<model:sparkline id="1" />
</cfsavecontent>
Sean Coyne's answer is the correct one, provided the import is included within the same context as the cfsavecontent tag:
<cfimport taglib="./tags" prefix="model">
<cfsavecontent variable="a">
<model:sparkline id="1" />
</cfsavecontent>
<cfoutput>#a#</cfoutput>
Will result in the dynamically evaluated output of the sparkline customtag.
It's impossible to OUTPUT the code and have it execute. OUTPUT just means output. It doesn't mean "run".
The only way to get CF code to be executed by CF is to follow normal channels:
* request a template;
* include a template;
* call a template as a custom tag or CFMODULE;
* call a method in a CFC;
* any others? ANyway, you get the point.
So if you have code that you create dynamically and want to execute... you need to write it to a file and then call it via the most appropriate of those mechanisms. Be warned though: running dynamic code like this has a fair overhead, as the code needs to be compiled before it's run, and compilation is not the fastest process in the scheme of things. The "best" thing to do here is to try to write and compile the file before it's needed, and only re-write the file it it needs updatng. Don't re-do it every request. But, ideally, don't do this sort of thing at all. One can usually approach things a different way.

Can we pass a whole Structure over an URL?

I am opening a new popup window on click of an URL in my page.
My question over here is Can i pass whole structure to the new page ?
If thats not possible is there any simple method to do that ?
Is the page being opened in the URL part of the same application?
If so a better way would be to save the structure in the user's session and pull in the information in that way. Cleaner URLs, code and more secure.
Cheers,
James
Expanding on James Buckingham's answer...
(This assumes you have session management set to true.)
In the calling page, simply copy your structure to a session variable:
<cfset session.myTempStruct=variables.myTempStruct />
Then, in the popup, copy the structure back to the local scope for that request:
<cfset variables.myTempStruct=session.myTempStruct />
If you don't want that structure to hang around in the session, you can have the request for the popup remove it from the session right after copying it to the local scope.
<cfset structDelete(session, "myTempStruct") />
Although HIGHLY NOT recommended, you could do this:
<cfset tmp = {} />
<cfset tmp.name="Marcos" />
<cfset tmp.lname="Placona" />
<cfwddx action="cfml2wddx" input="#tmp" output="tmpWDDX">
link
If you decide to take this approach, I'd suggest sending the information via form as opposed to URL.
You always have the option to store the data in a persistent object such as a bean, or a more simple approach such as a session.
Hope this helps you
You can add your data points as parameters to the end of the URI, but I do not suggest using the method you see as it would be highly subject to injection.
Serializing Struct (with serializeJSON() or something) and puttin git in URL seems reasonable in case struct is not too big (read: less than 3-4k characters in total).
Other solution would be to put this in some shared scope: session, application etc.
Third, would be to call cfm with POST request which can handle larger structs then GET.

Data type support in ColdFusion querynew()

Does anyone know of a way to store values as NVARCHAR in a manually created query in ColdFusion using the querynew() function? I have multiple parts of a largish program relying on using a query as an input point to construct an excel worksheet (using Ben's POI) so it's somewhat important I can continue to use it as a query to avoid a relatively large rewrite.
The problem came up when a user tried storing something that is outside of the VARCHAR range, some Japanese characters and such.
Edit: If this is not possible, and you are 100% sure, I'd like to know that too :)
When creating a ColdFusion query with queryNew(), you can pass a list of datatypes as a second argument. For example:
<cfset x = queryNew("foo,bar","integer,varchar") />
Alternatively, you can use cf_sql_varchar (which you would use in queryparam tags). According to the livedocs, nvarchar is accepted for the CF varchar data type.
QueryParam livedoc (referenced for nvarchar data type)
QueryNew livedoc (referenced for data type definition)
Managing Data Types livedoc (referenced for using cf_sql_datatype)
The only thing I've been able to come up with so far is this:
<cfset x = QueryNew("foobar")/>
<cfset queryAddRow(x) />
<cfset querySetCell(x, "foobar", chr(163)) />
<cfdump var="#x#">
When dumped, this query does contain the British Pound symbol.
I haven't tried this with Ben's POI utility, but hopefully it helps you some.
You might try using JavaCast() to set the values, as shown here:
Kinky Solutions (Ben Nadel) on JavaCast()
Make sure you're using Unicode end-to-end.
This is pretty much all you need:
<cfprocessingdirective pageEncoding="utf-8">
ColdFusion (& java) stores string in UTF-8 by default. All you need is to tell CF that the encoding of the page is UTF8. The alternative way is to save the Byte-order mark (BOM), but Eclipse/CFEclipse doesn't do it.