Skip to content

Instantly share code, notes, and snippets.

@KrunchMuffin
Created June 12, 2012 11:33
Show Gist options
  • Save KrunchMuffin/2917005 to your computer and use it in GitHub Desktop.
Save KrunchMuffin/2917005 to your computer and use it in GitHub Desktop.
cfwheels help
<!--- PetMEdicalCondition --->
<cfcomponent extends="Model" output="false">
<cffunction name="init" output="false" access="public">
<cfset belongsTo(name="pet", dependent="deleteAll")>
<cfset belongsTo(name="medicalcondition", dependent="deleteAll", joinType="outer")>
</cffunction>
</cfcomponent>
<!--- Pet --->
<cfcomponent extends="Model" output="false">
<cffunction name="init" output="false" access="public">
<!--- relationships --->
<cfset belongsTo(name="personPets")>
<cfset belongsTo(name="breed")>
<cfset hasMany(name="petDocuments", dependent="deleteAll", joinType="outer")>
<cfset hasMany(name="petEvents", dependent="deleteAll", joinType="outer")>
<cfset hasMany(name="petRoutineSchedules", dependent="deleteAll", joinType="outer")>
<cfset hasMany(name="petMedicalConditions", dependent="deleteAll", joinType="outer", shortcut="medicalconditions")>
<cfset hasOne(name="petProfile", joinType="outer")>
<!--- nested properties --->
<cfset nestedProperties(associations="petProfile,petEvents,petMedicalConditions")>
<!--- validations --->
<cfset validatesPresenceOf(property="pettypeid", when="onCreate")>
<!--- 3rd party --->
<cfset uploadableFile(property="image",nullWhenBlank=true)>
<!--- properties --->
<cfset property(name="breedId", label="Breed")>
<cfset property(name="name", label="Name")>
<cfset property(name="weight", label="Weight")>
<cfset property(name="isfixed", label="Spayed/Neutered")>
<cfset property(name="birthdate", label="Date of Birth")>
<cfset property(name="pettype", label="Pet Type")>
<!--- callbacks --->
<cfset afterFind("formatDate,getPetType")>
</cffunction>
<cffunction name="formatDate">
<cfif StructKeyExists(arguments,"birthdate")>
<cfset arguments.birthdate = DateFormat(arguments.birthdate,"mm/dd/yyyy")>
</cfif>
<cfreturn arguments>
</cffunction>
<cffunction name="getPetType">
<cfset arguments.pettype = "">
<cfif StructKeyExists(arguments,"breed") && StructKeyExists(arguments.breed,"pettypeid")>
<cfquery name="qGetPetType" datasource="PetTrackr">
SELECT pettype,id
FROM pettypes
WHERE id = <cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.breed.pettypeid#">
</cfquery>
<cfset arguments.pettype = qGetPetType.pettype>
<cfset arguments.pettypeid = qGetPetType.id>
</cfif>
<cfreturn arguments>
</cffunction>
</cfcomponent>
<!--- PetProfile--->
<cfcomponent extends="Model" output="false">
<cffunction name="init" output="false" access="public">
<cfset belongsTo(name="pet")>
</cffunction>
</cfcomponent>
<!--- FORM --->
#startFormTag(action=includeContent("formAction"), multipart=true, id="frmPetRegister",class="form-inline",key=pet.id,params="i=#RandRange(9999,99999999)#")#
<fieldset>
<legend>General Information</legend>
<div class="row" style="padding-top:10px">
<div class="span3">
#textField(label="Pet Name*", objectName="pet", property="name", id="name", tabindex="1")#
#select(label="Pet Type*", property="pettypeid", objectName="pet", id="pettype", options=pettypes, includeBlank=true, tabindex="3")#
#select(label="Gender*", property="gender", objectName="pet", id="gender", options=genderStruct, valueField="value", textField="gender", includeBlank=true, tabindex="5")#
#textField(label="Color*", objectName="pet", id="color", property="color", tabindex="7")#
#select(label="Spayed/Neutered*", objectName="pet", id="isfixed", property="isfixed", options=yesNoStruct, valueField="value", textField="isfixed", includeBlank=true, tabindex="9")#
</div>
<div class="span3">
#textField(label="Date of Birth*", objectName="pet", property="birthdate", id="birthdate", tabindex="2")#
#select(label="Breed*", property="breedid", id="breedid", objectName="pet", options="", includeBlank="Select Pet Type First", tabindex="4")#
#textField(label=includeContent("weightBlurb"), objectName="pet", id="weight", property="weight", append="<span class=""add-on"">lbs</span></div></div></div>", prepend="<div class=""controls""><div class=""input-append"">", tabindex="6")#
#textField(label="Microchip Number", objectName="pet",association="petProfile", id="microchip", property="microchipnumber", tabindex="7")#
#textArea(label="Other Notes", objectName="pet", id="notes", property="notes", append="<span class=""help-block"">Use for anything that needs to be noted.</span></div></div>", tabindex="8")#
</div>
<div class="span3">
<div class="control-group">
<label class="control-label">Does your pet have any medical conditions?</label>
<div class="controls">
#radioButton(objectName="pet",property="hasMedicalConditions",tagValue="0",labelClass="radio inline",id="hasMedicalConditionsNo",class="MedicalConditionsRdo",label="No")#
#radioButton(objectName="pet",property="hasMedicalConditions",tagValue="1",labelClass="radio inline",id="hasMedicalConditionsYes",class="MedicalConditionsRdo",label="Yes")#
</div>
</div>
<div id="PetIssues" class="hide">
<div class="control-group">
<label class="control-label">Please select all that apply</label>
<div class="controls" style="line-height:30px">
<cfloop query="medicalconditions">
<cfif ListFindNoCase("Other,Allergies",medicalconditions.condition)><br></cfif>
#hasManyCheckBox(objectName="pet",association="petmedicalconditions",keys="#pet.key()#,#medicalconditions.id#",label="#medicalconditions.condition#",id="#Left(medicalconditions.condition,3)#")#
<cfif ListFindNoCase("Other,Allergies",medicalconditions.condition)>
#textField(objectName="pet",property="medicalconditions#medicalconditions.condition#",association="petprofile",labelPlacement="after",label="",class="input-medium",id="MedicalConditions#medicalconditions.condition#",prependToLabel="", prepend="", append="")#
</cfif>
</cfloop>
</div>
</div>
</div>
</div> <!---/span3 --->
<div class="span3">
<!---image stuff --->
<cfif FileExists("#expandPath('.')#/images/petimages/#pet.id#/#pet.id#_profile.png")>
<cfset txtImage = "<img height=""100"" src=""/images/petimages/#pet.id#/#pet.id#_profile.png"" width=""100"">">
<cfset showDelete = "<span class=""imageDelete""><a rel=""tooltip"" title=""Make sure to click 'Save Pet' after"" href=""##"" id=""imgDelete""><sup>delete</sup></a></span>">
<cfelse>
<cfset txtImage = "None">
<cfset showDelete = "">
</cfif>
<input type="hidden" name="isImageDeleted" id="isImageDeleted" value="0">
#fileField(label="Pet Image", objectName="pet", id="image", property="image", tabindex="10",append="<span class=""help-inline"" style=""vertical-align:top"">Image will be resized to 120x120. <br><br>Current Image: <span id=""ImageTxt"">#txtImage#</span>#showDelete#<span id=""NewImageWrapper""><br><br>New Image: <span id=""NewImage""></span></span></span></div></div>")#
</div>
</div>
<div class="form-actions">
#buttonTag(value="register", content="Save Pet", class="btn btn-large btn-primary", type="submit", tabindex="7")#
#linkTo(text="Cancel", class="btn btn-large", action="main", confirm="Are you sure you want to cancel this action?")#
</div>
</fieldset>
#endFormTag()#
<!--- //end form --->
<!--- COntroller Update --->
<cffunction name="updatepet">
<!---<cfdump var="#params#" abort>--->
<cfset imageUploadFailedTxt = "">
<cfset pet = model("pet").findByKey(key=params.key,include="petProfile,petMedicalConditions")>
<cfdump var="#pet#" abort>
<cfif pet.update(params.pet)>
<cfif Len(Trim(params.pet.image)) gt 0>
<cftry>
<cfif !DirectoryExists(application.petImagePath&pet.id)>
<cfdirectory action="create" directory="#application.petImagePath##pet.id#">
</cfif>
<cfimage action="resize" destination="#application.petImagePath##pet.id#/#pet.id#_profile.png" height="120" isbase64="false" overwrite="true" source="#params.pet.image#" width="120">
<cfcatch>
<cflog text="updatepet - File upload or directory creation failed. #application.petImagePath##pet.id#/#pet.id#_profile.png - #cfcatch.Message#" file="User_#session.security.userid#">
<cfset imageUploadFailedTxt = ", but there was an error uploading the image.">
</cfcatch>
</cftry>
<cfelseif params.isImageDeleted>
<cftry>
<cffile action="delete" file="#application.petImagePath##pet.id#/#pet.id#_profile.png">
<cfcatch>
<cflog text="updatepet - Could not delete image file. #cfcatch.Message#" file="User_#session.security.userid#">
</cfcatch>
</cftry>
</cfif>
<cfset redirectTo(action="main",success="#params.pet.name# was updated successfully #imageUploadFailedTxt#")>
<cfelse>
<cfset renderPage(action="addpet")>
</cfif>
</cffunction>
<!--- Controller insert --->
<cffunction name="createpet">
<cfset imageUploadFailedTxt = "">
<!---<cfdump var="#params#" abort>--->
<cfset pet = model("pet").new(params.pet)>
<cfif pet.save()>
<cfif Len(Trim(params.pet.image)) gt 0>
<cftry>
<cfif !DirectoryExists(application.petImagePath&pet.id)>
<cfdirectory action="create" directory="#application.petImagePath##pet.id#">
</cfif>
<cfimage action="resize" destination="#application.petImagePath##pet.id#/#pet.id#_profile.png" height="120" isbase64="false" overwrite="true" source="#params.pet.image#" width="120">
<cfcatch>
<cflog text="updatepet - File upload or directory creation failed. #application.petImagePath##pet.id#/#pet.id#_profile.png - #cfcatch.Message#" file="User_#session.security.userid#">
<cfset imageUploadFailedTxt = ", but there was an error uploading the image.">
</cfcatch>
</cftry>
</cfif>
<cfset personPet = model("PeoplePet").create(personId="#session.security.userid#", petId="#pet.id#")>
<cfset petprofilenew = model("PetProfile").create(petid="#pet.id#")>
<cfset redirectTo(action="main",success="#params.pet.name# was added successfully #imageUploadFailedTxt#")>
<cfelse>
<cfset arrErrors = pet.allErrors()>
<cfset renderPage(action="addpet")>
</cfif>
</cffunction>
<cffunction name="addpet">
<cfset var newPetProfile = model("petProfile").new()>
<cfset var newPetMedicalCond = model("petMedicalConditions").new()>
<cfset contentFor(pageHeading="Add New Pet")>
<cfset contentFor(blurb="Please fill in the following information to add your new pet.")>
<cfset contentFor(thisPageTitle="My PetTrackr - Add Pet")>
<cfset contentFor(formAction="createpet")>
<cfset contentFor(mode="Add")>
<cfset contentFor(weightBlurb="Current Weight")>
<cfset contentFor(iconStyle="<i class=""icon-plus""></i>")>
<cfset pet = model("pet").new(petProfile=newPetProfile,petMedicalConditions=newPetMedicalCond)>
<cfset medicalconditions = model("medicalcondition").findAll(order="sortorder,condition", returnAs="query")>
</cffunction>
<cffunction name="editpet">
<cfset pet = model("pet").findByKey(key=params.key, include="petProfile,breed,petMedicalConditions")>
<cfset contentFor(pageHeading="Currently Editing #pet.name#")>
<cfset contentFor(blurb="Make the necessary changes then click the ""Save Pet"" button.")>
<cfset contentFor(thisPageTitle="My PetTrackr - Edit Pet")>
<cfset contentFor(formAction="updatepet")>
<cfset contentFor(mode="Edit")>
<cfset contentFor(weightBlurb="Initial Weight @ Signup <i class=""icon-question-sign showPointer"" rel=""tooltip"" title=""You may edit this in the events section"" id=""WeightQuestion""></i> ")>
<cfset contentFor(iconStyle="<i class=""icon-pencil""></i>")>
<cfset medicalconditions = model("medicalcondition").findAll(order="sortorder,condition", returnAs="query")>
<cfset renderPage(action="addpet")>
</cffunction>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment