PageRenderTime 68ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/wheels/view/formsassociation.cfm

http://cfwheels.googlecode.com/
ColdFusion | 161 lines | 144 code | 17 blank | 0 comment | 44 complexity | 363ec3874ab41d43142f230f893fc532 MD5 | raw file
Possible License(s): Apache-2.0, CPL-1.0
  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. if (!loc.included)
  29. {
  30. loc.included = "";
  31. }
  32. if (loc.value == arguments.tagValue || (arguments.checkIfBlank && loc.value != arguments.tagValue))
  33. loc.checked = true;
  34. loc.tagId = "#arguments.objectName#-#arguments.association#-#Replace(arguments.keys, ",", "-", "all")#-#arguments.property#-#arguments.tagValue#";
  35. loc.tagName = "#arguments.objectName#[#arguments.association#][#arguments.keys#][#arguments.property#]";
  36. loc.returnValue = radioButtonTag(name=loc.tagName, id=loc.tagId, value=arguments.tagValue, checked=loc.checked, label=arguments.label);
  37. </cfscript>
  38. <cfreturn loc.returnValue />
  39. </cffunction>
  40. <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."
  41. examples='
  42. <!--- Show check boxes for associating authors with the current book --->
  43. <cfloop query="authors">
  44. ##hasManyCheckBox(
  45. label=authors.fullName,
  46. objectName="book",
  47. association="bookAuthors",
  48. keys="##book.key()##,##authors.id##"
  49. )##
  50. </cfloop>
  51. '
  52. categories="view-helper,forms-association" chapters="nested-properties" functions="hasMany,hasManyRadioButton,includedInObject,nestedProperties">
  53. <cfargument name="objectName" type="string" required="true" hint="See documentation for @hasManyRadioButton." />
  54. <cfargument name="association" type="string" required="true" hint="See documentation for @hasManyRadioButton." />
  55. <cfargument name="keys" type="string" required="true" hint="See documentation for @hasManyRadioButton." />
  56. <cfargument name="label" type="string" required="false" hint="See documentation for @textField.">
  57. <cfargument name="labelPlacement" type="string" required="false" hint="See documentation for @textField.">
  58. <cfargument name="prepend" type="string" required="false" hint="See documentation for @textField.">
  59. <cfargument name="append" type="string" required="false" hint="See documentation for @textField.">
  60. <cfargument name="prependToLabel" type="string" required="false" hint="See documentation for @textField.">
  61. <cfargument name="appendToLabel" type="string" required="false" hint="See documentation for @textField.">
  62. <cfargument name="errorElement" type="string" required="false" hint="See documentation for @textField.">
  63. <cfargument name="errorClass" type="string" required="false" hint="See documentation for @textField.">
  64. <cfscript>
  65. var loc = {};
  66. $args(name="hasManyCheckBox", args=arguments);
  67. loc.checked = true;
  68. loc.returnValue = "";
  69. loc.included = includedInObject(argumentCollection=arguments);
  70. if (!loc.included)
  71. {
  72. loc.included = "";
  73. loc.checked = false;
  74. }
  75. loc.tagId = "#arguments.objectName#-#arguments.association#-#Replace(arguments.keys, ",", "-", "all")#-_delete";
  76. loc.tagName = "#arguments.objectName#[#arguments.association#][#arguments.keys#][_delete]";
  77. StructDelete(arguments, "keys", false);
  78. StructDelete(arguments, "objectName", false);
  79. StructDelete(arguments, "association", false);
  80. loc.returnValue = checkBoxTag(name=loc.tagName, id=loc.tagId, value=0, checked=loc.checked, uncheckedValue=1, argumentCollection=arguments);
  81. </cfscript>
  82. <cfreturn loc.returnValue />
  83. </cffunction>
  84. <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."
  85. examples=
  86. '
  87. <!--- 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 --->
  88. <cfif not includedInObject(objectName="customer", association="subscriptions", keys="##customer.key()##,##swimsuitEdition.id##")>
  89. <cfset assignSalesman(customer)>
  90. </cfif>
  91. '
  92. categories="view-helper,forms-association" chapters="nested-properties" functions="hasMany,hasManyCheckBox,hasManyRadioButton,nestedProperties">
  93. <cfargument name="objectName" type="string" required="true" hint="See documentation for @hasManyRadioButton." />
  94. <cfargument name="association" type="string" required="true" hint="See documentation for @hasManyRadioButton." />
  95. <cfargument name="keys" type="string" required="true" hint="See documentation for @hasManyRadioButton." />
  96. <cfscript>
  97. var loc = {};
  98. loc.returnValue = false;
  99. loc.object = $getObject(arguments.objectName);
  100. // clean up our key argument if there is a comma on the beginning or end
  101. arguments.keys = REReplace(arguments.keys, "^,|,$", "", "all");
  102. if (!StructKeyExists(loc.object, arguments.association) || !IsArray(loc.object[arguments.association]))
  103. return loc.returnValue;
  104. if (!Len(arguments.keys))
  105. return loc.returnValue;
  106. loc.iEnd = ArrayLen(loc.object[arguments.association]);
  107. for (loc.i = 1; loc.i lte loc.iEnd; loc.i++)
  108. {
  109. loc.assoc = loc.object[arguments.association][loc.i];
  110. if (IsObject(loc.assoc) && loc.assoc.key() == arguments.keys)
  111. {
  112. loc.returnValue = loc.i;
  113. break;
  114. }
  115. }
  116. </cfscript>
  117. <cfreturn loc.returnValue />
  118. </cffunction>
  119. <cffunction name="$hasManyFormValue" returntype="string" access="public" output="false">
  120. <cfargument name="objectName" type="string" required="true" />
  121. <cfargument name="association" type="string" required="true" />
  122. <cfargument name="property" type="string" required="true" />
  123. <cfargument name="keys" type="string" required="true" />
  124. <cfscript>
  125. var loc = {};
  126. loc.returnValue = "";
  127. loc.object = $getObject(arguments.objectName);
  128. if (!StructKeyExists(loc.object, arguments.association) || !IsArray(loc.object[arguments.association]))
  129. return loc.returnValue;
  130. if (!Len(arguments.keys))
  131. return loc.returnValue;
  132. loc.iEnd = ArrayLen(loc.object[arguments.association]);
  133. for (loc.i = 1; loc.i lte loc.iEnd; loc.i++)
  134. {
  135. loc.assoc = loc.object[arguments.association][loc.i];
  136. if (isObject(loc.assoc) && loc.assoc.key() == arguments.keys && StructKeyExists(loc.assoc, arguments.property))
  137. {
  138. loc.returnValue = loc.assoc[arguments.property];
  139. break;
  140. }
  141. }
  142. </cfscript>
  143. <cfreturn loc.returnValue />
  144. </cffunction>