Many to Many relationship with cfwheels without composite keys - coldfusion

I've been following the information from here:
cfwheels.org/docs/1-1/chapter/nested-properties
I've ended up downloading a sample application which breaks down at the same place
code executes fine, in the sense that I get no errors, but the many-many table does not get the new entries, and when I add the entries manually in the database, they are not reflected with the checkboxes and sometimes they are removed when the model is updated.
EDIT
I found out the issue... just not how to solve it. There's a small detail there that is very easy to miss. The application seems to rely on composite keys and the order of the keys matters. But I'm not using composite keys.
(following https://github.com/mhenke/cfwheels-training/blob/develop/03-tags.md as an example...)
How do I get a table with cols: id,tagsid, and commentsid to work?
the problems I see is that cfwheels keeps trying to use the id tag when creating the taggings model

As much as I love about CFWheels, I have to admit that I am not a fan of the form helper functions or the "shortcut" feature. In this example case, I'd just "revert" to the more straightforward/simple CFML to construct the checkboxes (if not the whole form) and looping logic to save the values in the join table. For example:
<fieldset>
<legend>PropertyLanguages</legend>
<cfloop query="Languages">
<label>
#Languages.language#
<input type="checkbox" name="Property[PropertyLanguages]" value="#Languages.id#">
</label>
</cfloop>
</fieldset>
Then change the update controller logic like so:
<!--- CONTROLLER - update.cfm - updateProperty --->
<cffunction name="updateProperty">
<cfscript>
Property = model("Property").findByKey(key=params.Property.id);
Property.update(params.Property);
if (IsDefined("params.Property.PropertyLanguages"))
{
model("PropertyLanguages").deleteAll(where="propertyid=#params.Property.id# AND languageid NOT IN (#params.Property.PropertyLanguages#)");
for (var i = 1; i<=ListLen(params.Property.PropertyLanguages); i++)
{
languageid = ListGetAt(params.Property.PropertyLanguages, i);
if (! IsObject(model("PropertyLanguages").findOne(where="propertyid=#params.Property.id# AND languageid=#languageid#")))
{
pl = model("PropertyLanguages").new();
pl.langugageid = languageid;
pl.propertyid = params.Property.id;
pl.save();
}
}
}
else
{
model("PropertyLanguages").deleteAll(where="propertyid=#params.Property.id#");
}
</cfscript>
</cffunction>
I haven't tested this, but it should work, more or less. It's not as simple as it could (should?) be using the wheels helpers, but it doesn't seem too bad.

Related

In C1 CMS How to create a list dynamically by getting input from user at the function property window?

Actually I am trying to create two controls one is drop down and another is List. Both are similar and easy for static values or values which are already stored somewhere.
But what I want is, I want the Power user to create or add / edit list items at the run time ( When he is inserting function to the page )
so similar concept to this : http://jsfiddle.net/DVbGY/1/
<div data-role="content">
<div id="items">
</div>
<input type="text" id="item" />
<input type="button" value="Add item to list" onclick="appendToList()"/>
<script>
var listCreated = false;
function appendToList(){
if(!listCreated){
$("#items").append("<ul id='list' data-role='listview' data-inset='true'></ul>");
listCreated = true;
$("#items").trigger("create");
}
var value = $("#item").val();
var listItem = "<li>" + value + "</li>";
$("#list").append(listItem);
}
</script>
but in the function property window.
Currently I am using comma separated list from user but its not viable solution as my next step is to add url as well with the input data from user so Lets say user wants to create a drop-down button and user is adding items and associating particular link to its items.
As you can see in above image i am getting data from user but instead of that text box i want to use above mentioned similar concept.
How can I make this possible ? or Is it possible in C1-CMS ? if yes please explain with Example in detail.
Thank you for your time and thanks reading this post.
It is not possible with the currently built in widgets.

CFWheels: Display form errors on redirectto instead of renderpage

I have a form which I am validating using CFWheels model validation and form helpers.
My code for index() Action/View in controller:
public function index()
{
title = "Home";
forms = model("forms");
allforms = model("forms").findAll(order="id ASC");
}
#startFormTag(controller="form", action="init_form")#
<select class="form-control">
<option value="">Please select Form</option>
<cfloop query="allforms">
<option value="#allforms.id#">#allforms.name#</option>
</cfloop>
</select>
<input type="text" name="forms[name]" value="#forms.name#">
#errorMessageOn(objectName="forms", property="name")#
<button type="submit">Submit</button>
#endFormTag()#
This form is submitted to init_form() action and the code is :
public function init_form()
{
title = "Home";
forms = get_forms(params.forms);
if(isPost())
{
if(forms.hasErrors())
{
// don't want to retype allforms here ! but index page needs it
allforms = model(tables.forms).findAll(order="id ASC");
renderPage(action="index");
//redirectTo(action="index");
}
}
}
As you can see from the above code I am validating the value of form field and if any errors it is send to the original index page. My problem is that since I am rendering page, I also have to retype the other variables that page need such as "allforms" in this case for the drop down.
Is there a way not to type such variables? And if instead of renderPage() I use redirectTo(), then the errors don't show? Why is that?
Just to be clear, I want to send/redirect the page to original form and display error messages but I don't want to type other variables that are required to render that page? Is there are way.
Please let me know if you need more clarification.
This may seem a little off topic, but my guess is that this is an issue with the form being rendered using one controller (new) and processed using another (create) or in the case of updating, render using edit handle form using update.
I would argue, IMHO, etc... that the way that cfWheels routes are done leaves some room for improvement. You see in many of the various framework's routing components you can designate a different controller function for POST than your would use for GET. With cfWheels, all calls are handled based on the url, so a GET and a POST would be handled by the same controller if you use the same url (like when a form action is left blank).
This is the interaction as cfwheels does it:
While it is possible to change the way it does it, the documentation and tutorials you'll find seem to prefer this way of doing it.
TL; DR;
The workaround that is available, is to have the form be render (GET:new,edit) and processing (POST:create,update) handled by the same controller function (route). Within the function...
check if the user submitted using POST
if it is POST, run a private function (i.e. handle_create()) that handles the form
within the handle_create() function you can set up all your error checking and create the errors
if the function has no errors, create (or update) the model and optionally redirect to a success page
otherwise return an object/array of errors
make the result error object/array available to view
handle the form creation
In the view, if the errors are present, show them in the form or up top somewhere. Make sure that the form action either points to self or is empty. Giving the submit button a name and value can also help in determining whether a form was submitted.
This "pattern" works pretty well without sessions.
Otherwise you can use the Flash, as that is what it was created for, but you do need to have Sessions working. their use is described here: http://docs.cfwheels.org/docs/using-the-flash and here:http://docs.cfwheels.org/v1.4/docs/flashmessages
but it really is as easy as adding this to your controller
flashInsert(error="This is an error message.");
and this to your view
<cfif flashKeyExists("error")>
<p class="errorMessage">
#flash("error")#
</p>
</cfif>

Coldfusion - How to prevent multiple clicks?

I have a button (anchor tag) that send a confirm message if you press it.
The problem is that for example if you press it 5 times very quickly it will send 5 confirm messages, if you press it 2 times it will send 2 messages.
This can occur when the user has low connection speed and while the page is refreshing he presses again the button.
How can I manage this situation? I though of disabling the button but for other reasons this is not possible.
<a class="msg" href="/manage/conversations.cfm?destination=#destination#">
#ucase(request.l('Send'))#
</a>
Thank you for your time
Ultimately, you need to have code on your server to prevent processing the link multiple times from the same user.
However, to solve the UI issue, have you link call a function instead of the cf file directly.
<a class="msg" href="javascript: processLink(#destination#);">
#ucase(request.l('Send'))#
</a>
<script>
runCount = 0;
function processLink(destination){
runCount++;
if (runCount == 1){
window.location.href = "/manage/conversations.cfm?destination=" + destination;
}
}
</script>
As mentioned in the previous answer it's nice to have some client side javascript to stop duplicate submissions from trigger happy users however you should also do this checking server side.
One approach would be to create a hidden formfield with a GUID that coldfusion generates when coldfusion renders your form.
So something like:
<cfset GUID = createUUID()>
<cfoutput>
<form id="frm" action="/target.cfm" method="post">
<input type="hidden" name="guid" value="#GUID#">
<!-- all your formfields go here -->
<input type="submit">
</form>
</cfoutput>
On the server side the target page then checks if it has already previously received the GUID. There are lots of ways to do, here are two of many ways.
a) Use Session Scope.
This is probably the quickest way if you are not running in a clustered environment and just need something quick for a tiny application.
<cfif isDefined("session.MYPAGE_GUID") AND session.MYPAGE_GUID EQ form.guid>
<cfoutput>Duplicate Form Submission</cfoutput>
<cfabort>
<cfelse>
<cfset session.MYPAGE_GUID = form.guid>
<!-- Do Business Logic Here -->
</cfif>
b) Use a Database Table.
Create a database table with a column called GUID. Make sure that GUID is the primary key or has a unique constraint.
Before you run your business logic insert the form.GUID into the database table. If you can do the insert process your business logic, if not the database query will throw an error that the record exists. You can then catch this error and take the appropriate action for a duplicate submission.
I prefer the database option as it works across clustered environments and database server are solid protecting against race conditions to ensure that a GUID is only set once.
Please be aware that this is just demonstrating the basic concepts and is not a drop in solution. There is a bit of more work to get these concepts into an e-commerce solution.
The best way is to disable the link once it's selected. If you don't want to do that, an alternative is to structure conversations.cfm like this.
<div id="pageContent">
small amount of text
</div>
<cfflush>
</body>
</html>
<cfsavecontent variable = "actualPageContent">
code
</cfsavecontent>
<cfoutput>
<script>
var #toScript(actualPageContent, "newPageContent")#;
document.getElementById("pageContent").innerHTML = "newPageContent";
</script>
</cfoutput>

Execute cfquery after button press or behind the scenes action page?

My page creates a few random variables to pick prizes and winners for a drawing, and I want to write the prize winners and their prizes to a table after the winner clicks a button to claim the prize.
The problem is I want them to stay on the page so that a prize can be picked again if it isn't claimed.
The best solution I can come up with is an action page that the cfinput button references, but I don't want the action page to open up, I just want the cfquery on that page to run behind the scenes.
Here's an example of my query:
<cfquery name="updateQuantity" datasource="christmas">
UPDATE PRIZES
SET QUANTITY = QUANTITY - 1
WHERE prize_ID = #prizeID#
</cfquery>
I tried making my button a "submit" button and using cfif isDefined("form.Submit") to run the cfquery on the same page, but the submit button refreshes the page (which I don't want) and writes the next picked winner instead of the prizeID of the current session:
<cfform>
<cfinput name="submit" type="submit" value="Claim Your Prize!" onClick="">
</cfform>
<cfif isDefined("form.Submit")>
<cfquery name="updateQuantity" datasource="christmas">
UPDATE PRIZES
SET QUANTITY = QUANTITY - 1
WHERE prize_ID = #prizeID# </cfquery>
</cfif>
prizeID is determined by the randomly selected prize.
Adam is correct.
While AJAX is what you're after, ColdFusion's AJAX related tags are not the way to go.
Take the time to learn JQuery and AJAX (it isn't too hard, and the CF community has some great examples and guides) and do it properly; with your example requirements, you should be up and running in no time.
That way you can properly manage any logging and error handling that you may need to as well. Good luck!
It sounds like ColdFusion.Ajax.submitForm would work for you.
In general, I think an AJAX request is what you are looking for.

cfgrid boolean column as Yes/No

I have a boolean type column in an html cfgrid. The data is stored in the database as 1/0 and is returned from CF as such. I want the user to see Yes/No instead of 1/0. I tried QuerySetCell, and couldn't get it to work.
The form is editable, when you double click the cell, the checkboxes show and it updates as it should. The only issue is the display.
<cfform>
<cfgrid name="blah" format="html" bind="mycfccall" selectmode="edit">
<cfgridcolumn name="bitCol" header="Is it" width="75" type="boolean">
</cfgrid>
</cfform>
Thanks in advance...
You'll need to apply a custom field renderer. You'll need to add an init() js function to your page, along with a renderer method. I have the basic process of applying a custom renderer on my blog:
CF8 Ajax Grid: Renderers and Events
Basically, you'll call your init() method after the grid has initially rendered, by using the ajaxOnLoad() method:
<cfset ajaxOnLoad("init") />
Within your init() method, you would get a reference to the grid and it's ColumnModel:
init = function() {
var myGrid = ColdFusion.Grid.getGridObject('myGridID');
var gridCM = myGrid.getColumnModel();
// The rest goes here
}
You'll also need your renderer method, that you can apply to any column:
yesNoRenderer = function(value,meta,record,row,column,store) {
if (value === 1){
return "Yes";
} else {
return "No";
}
}
After which, you'll need to apply the renderer to the column of your choice:
gridCM.setRenderer(cm.getIndexById('myColumnName'), yesNoRenderer);
The setRenderer method takes the column index (starting from 0) and the function to apply as a renderer. The getIndexById() method should work here, but you should test it first to be sure, and remember that casing is important in JavaScript.
Most of the CF Ajax components use Ext 1.1 under the hood. Carefully read through The Adobe documentation on the ColdFusion JavaScript Functions, and remember that you can tap into the underlying Ext 1.1 API.
I think it will be easier to use Decode in your database query:
Decode(bitCol,1,'Yes','No') bitCol