PageRenderTime 21ms CodeModel.GetById 15ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 1ms

/wheels/view/formsassociation.cfm

http://cfwheels.googlecode.com/
ColdFusion | 161 lines | 144 code | 17 blank | 0 comment | 44 complexity | 363ec3874ab41d43142f230f893fc532 MD5 | raw file
  1<cffunction name="hasManyRadioButton" returntype="string" access="public" output="false" hint="Used as a shortcut to output the proper form elements for an association. Note: Pass any additional arguments like `class`, `rel`, and `id`, and the generated tag will also include those values as HTML attributes."
  2	examples='
  3		<!--- Show radio buttons for associating a default address with the current author --->
  4		<cfloop query="addresses">
  5			##hasManyRadioButton(
  6				label=addresses.title,
  7				objectName="author",
  8				association="authorsDefaultAddresses",
  9				keys="##author.key()##,##addresses.id##"
 10			)##
 11		</cfloop>
 12	'
 13	categories="view-helper,forms-association" chapters="nested-properties" functions="hasMany,hasManyCheckBox,includedInObject,nestedProperties">
 14	<cfargument name="objectName" type="string" required="true" hint="Name of the variable containing the parent object to represent with this form field." />
 15	<cfargument name="association" type="string" required="true" hint="Name of the association set in the parent object to represent with this form field." />
 16	<cfargument name="property" type="string" required="true" hint="Name of the property in the child object to represent with this form field." />
 17	<cfargument name="keys" type="string" required="true" hint="Primary keys associated with this form field." />
 18	<cfargument name="tagValue" type="string" required="true" hint="The value of the radio button when `selected`." />
 19	<cfargument name="checkIfBlank" type="boolean" required="false" default="false" hint="Whether or not to check this form field as a default if there is a blank value set for the property." />
 20	<cfargument name="label" type="string" required="false" hint="See documentation for @textField.">
 21	<cfscript>
 22		var loc = {};
 23		$args(name="hasManyRadioButton", args=arguments);
 24		loc.checked = false;
 25		loc.returnValue = "";
 26		loc.value = $hasManyFormValue(argumentCollection=arguments);
 27		loc.included = includedInObject(argumentCollection=arguments);
 28
 29		if (!loc.included)
 30		{
 31			loc.included = "";
 32		}
 33
 34		if (loc.value == arguments.tagValue || (arguments.checkIfBlank && loc.value != arguments.tagValue))
 35			loc.checked = true;
 36
 37		loc.tagId = "#arguments.objectName#-#arguments.association#-#Replace(arguments.keys, ",", "-", "all")#-#arguments.property#-#arguments.tagValue#";
 38		loc.tagName = "#arguments.objectName#[#arguments.association#][#arguments.keys#][#arguments.property#]";
 39		loc.returnValue = radioButtonTag(name=loc.tagName, id=loc.tagId, value=arguments.tagValue, checked=loc.checked, label=arguments.label);
 40	</cfscript>
 41	<cfreturn loc.returnValue />
 42</cffunction>
 43
 44<cffunction name="hasManyCheckBox" returntype="string" access="public" output="false" hint="Used as a shortcut to output the proper form elements for an association. Note: Pass any additional arguments like `class`, `rel`, and `id`, and the generated tag will also include those values as HTML attributes."
 45	examples='
 46		<!--- Show check boxes for associating authors with the current book --->
 47		<cfloop query="authors">
 48			##hasManyCheckBox(
 49				label=authors.fullName,
 50				objectName="book",
 51				association="bookAuthors",
 52				keys="##book.key()##,##authors.id##"
 53			)##
 54		</cfloop>
 55	'
 56	categories="view-helper,forms-association" chapters="nested-properties" functions="hasMany,hasManyRadioButton,includedInObject,nestedProperties">
 57	<cfargument name="objectName" type="string" required="true" hint="See documentation for @hasManyRadioButton." />
 58	<cfargument name="association" type="string" required="true" hint="See documentation for @hasManyRadioButton." />
 59	<cfargument name="keys" type="string" required="true" hint="See documentation for @hasManyRadioButton." />
 60	<cfargument name="label" type="string" required="false" hint="See documentation for @textField.">
 61	<cfargument name="labelPlacement" type="string" required="false" hint="See documentation for @textField.">
 62	<cfargument name="prepend" type="string" required="false" hint="See documentation for @textField.">
 63	<cfargument name="append" type="string" required="false" hint="See documentation for @textField.">
 64	<cfargument name="prependToLabel" type="string" required="false" hint="See documentation for @textField.">
 65	<cfargument name="appendToLabel" type="string" required="false" hint="See documentation for @textField.">
 66	<cfargument name="errorElement" type="string" required="false" hint="See documentation for @textField.">
 67	<cfargument name="errorClass" type="string" required="false" hint="See documentation for @textField.">
 68	<cfscript>
 69		var loc = {};
 70		$args(name="hasManyCheckBox", args=arguments);
 71		loc.checked = true;
 72		loc.returnValue = "";
 73		loc.included = includedInObject(argumentCollection=arguments);
 74
 75		if (!loc.included)
 76		{
 77			loc.included = "";
 78			loc.checked = false;
 79		}
 80
 81		loc.tagId = "#arguments.objectName#-#arguments.association#-#Replace(arguments.keys, ",", "-", "all")#-_delete";
 82		loc.tagName = "#arguments.objectName#[#arguments.association#][#arguments.keys#][_delete]";
 83
 84		StructDelete(arguments, "keys", false);
 85		StructDelete(arguments, "objectName", false);
 86		StructDelete(arguments, "association", false);
 87
 88		loc.returnValue = checkBoxTag(name=loc.tagName, id=loc.tagId, value=0, checked=loc.checked, uncheckedValue=1, argumentCollection=arguments);
 89	</cfscript>
 90	<cfreturn loc.returnValue />
 91</cffunction>
 92
 93<cffunction name="includedInObject" returntype="boolean" access="public" output="false" hint="Used as a shortcut to check if the specified IDs are a part of the main form object. This method should only be used for `hasMany` associations."
 94	examples=
 95	'
 96		<!--- Check to see if the customer is subscribed to the Swimsuit Edition. Note that the order of the `keys` argument should match the order of the `customerid` and `publicationid` columns in the `subscriptions` join table --->
 97		<cfif not includedInObject(objectName="customer", association="subscriptions", keys="##customer.key()##,##swimsuitEdition.id##")>
 98			<cfset assignSalesman(customer)>
 99		</cfif>
100	'
101	categories="view-helper,forms-association" chapters="nested-properties" functions="hasMany,hasManyCheckBox,hasManyRadioButton,nestedProperties">
102	<cfargument name="objectName" type="string" required="true" hint="See documentation for @hasManyRadioButton." />
103	<cfargument name="association" type="string" required="true" hint="See documentation for @hasManyRadioButton." />
104	<cfargument name="keys" type="string" required="true" hint="See documentation for @hasManyRadioButton." />
105	<cfscript>
106		var loc = {};
107		loc.returnValue = false;
108		loc.object = $getObject(arguments.objectName);
109
110		// clean up our key argument if there is a comma on the beginning or end
111		arguments.keys = REReplace(arguments.keys, "^,|,$", "", "all");
112
113		if (!StructKeyExists(loc.object, arguments.association) || !IsArray(loc.object[arguments.association]))
114			return loc.returnValue;
115
116		if (!Len(arguments.keys))
117			return loc.returnValue;
118
119		loc.iEnd = ArrayLen(loc.object[arguments.association]);
120		for (loc.i = 1; loc.i lte loc.iEnd; loc.i++)
121		{
122			loc.assoc = loc.object[arguments.association][loc.i];
123			if (IsObject(loc.assoc) && loc.assoc.key() == arguments.keys)
124			{
125				loc.returnValue = loc.i;
126				break;
127			}
128		}
129	</cfscript>
130	<cfreturn loc.returnValue />
131</cffunction>
132
133<cffunction name="$hasManyFormValue" returntype="string" access="public" output="false">
134	<cfargument name="objectName" type="string" required="true" />
135	<cfargument name="association" type="string" required="true" />
136	<cfargument name="property" type="string" required="true" />
137	<cfargument name="keys" type="string" required="true" />
138	<cfscript>
139		var loc = {};
140		loc.returnValue = "";
141		loc.object = $getObject(arguments.objectName);
142
143		if (!StructKeyExists(loc.object, arguments.association) || !IsArray(loc.object[arguments.association]))
144			return loc.returnValue;
145
146		if (!Len(arguments.keys))
147			return loc.returnValue;
148
149		loc.iEnd = ArrayLen(loc.object[arguments.association]);
150		for (loc.i = 1; loc.i lte loc.iEnd; loc.i++)
151		{
152			loc.assoc = loc.object[arguments.association][loc.i];
153			if (isObject(loc.assoc) && loc.assoc.key() == arguments.keys && StructKeyExists(loc.assoc, arguments.property))
154			{
155				loc.returnValue = loc.assoc[arguments.property];
156				break;
157			}
158		}
159	</cfscript>
160	<cfreturn loc.returnValue />
161</cffunction>