Last active
December 22, 2015 21:19
-
-
Save sweded/6532795 to your computer and use it in GitHub Desktop.
Controller to create, update, delete records
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @file /components/qbControl.cfc | |
* @author Gernot Bartels 2013 | |
* @description Controller to create, update, delete records | |
*/ | |
component | |
output="false" | |
accessors="true" | |
displayname="Record control" | |
{ | |
property | |
name="uuid" | |
type="string" | |
default="0" | |
hint="The object ID in a persistence context."; | |
public any function init() { | |
this.setUuid(CreateUUID()); | |
return this; | |
} | |
public function controller( urlParamString, query_data ) { | |
/* | |
====================================================================================== | |
Set up query object. This receives struct containing params qb_tableID, | |
qb_appToken, uField (qb_responseFid), uValue (response_id), and uList (whatever FID's) | |
====================================================================================== */ | |
qb_query = new qbQuery().qFieldValue( query_data ); | |
/* | |
An array of returned records, empty if new record */ | |
arrQbRecords = qb_query.getQbRecords(); | |
/* | |
The number of records */ | |
lenQbRecords = arrayLen(arrQbRecords); | |
/* | |
The latest Record ID to edit */ | |
qbLatestRid = qb_query.getQbLatestRecordId(); | |
/* | |
The latest record URL */ | |
recordUrl = URLDecode("#SESSION.qb_url##query_data.qb_tableID#?a=dr&rid=#qbLatestRid#"); | |
/* | |
====================================================================================== | |
Create, Update, or Delete dependant on query result | |
====================================================================================== */ | |
if (lenQbRecords < 1) { // Create | |
urlRequest = "#SESSION.qb_url#/#query_data.qb_tableID#?act=API_AddRecord&ticket=#SESSION.qb_ticket#&apptoken=#query_data.qb_appToken#&#urlParamString#"; | |
create_record = new qbCrud().createRecord(urlRequest, query_data.qb_tableID); | |
} else { // Update | |
urlRequest = "#SESSION.qb_url#/#query_data.qb_tableID#?act=API_EditRecord&rid=#qbLatestRid#&ticket=#SESSION.qb_ticket#&apptoken=#query_data.qb_appToken#&#urlParamString#" | |
update_record = new qbCrud().updateRecord(urlRequest, query_data.qb_tableID); | |
} | |
if (lenQbRecords >= 2) { // Check for duplicates | |
savecontent variable="app_message" { | |
writeOutput( | |
"I'm removing the #lenQbRecords - 1# extra records except for Record ID: <a href=""#recordUrl#"">#qbLatestRid#</a>" | |
); | |
} | |
error_data = structNew(); | |
error_data.app_message = app_message; | |
error_data.app_name = "app_name"; | |
error_data.errcode = "derp"; | |
error_data.errdetail = "There are #lenQbRecords# records with the same Response ID: #FORM.response_id#."; | |
error_data.errtext = "Too much records"; | |
error_data.log_name = "log_name"; | |
error_data.query_data.qb_tableID = "111"; | |
error_data.response_id = "222"; | |
qb_error = new qbError(error_data); | |
i = lenQbRecords; | |
do { i = i - 1; // Delete | |
qbRecordID = qb_query.getQbRecordIds()[i].XmlText; | |
urlRequest = "#SESSION.qb_url#/#query_data.qb_tableID#?act=API_DeleteRecord&rid=#qbRecordID#&ticket=#SESSION.qb_ticket#&apptoken=#query_data.qb_appToken#" | |
delete_record = new qbCrud().deleteRecord(urlRequest, query_data.qb_tableID); | |
} while ( i > 1 ); // Delete all duplicates except the latest record. | |
} | |
return; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment