From 7529460e808e92550b62b282bbd432eaef409fe4 Mon Sep 17 00:00:00 2001 From: Sean Harvey Date: Tue, 12 May 2009 01:48:04 +0000 Subject: [PATCH] BUGFIX Fixed userforms SQL to work with ANSI compatibility turned on --- code/UserDefinedForm.php | 12 ++++-- code/editor/FieldEditor.php | 42 +++++++++++++------ code/submissions/SubmittedFormReportField.php | 7 +++- 3 files changed, 44 insertions(+), 17 deletions(-) diff --git a/code/UserDefinedForm.php b/code/UserDefinedForm.php index bda1962..3589f40 100755 --- a/code/UserDefinedForm.php +++ b/code/UserDefinedForm.php @@ -473,9 +473,15 @@ JS // a user selected if($recipient->SendEmailFromFieldID) { $name = Convert::raw2sql($recipient->SendEmailFromField()->Name); - $SubmittedFormField = DataObject::get_one("SubmittedFormField", "Name = '$name' AND ParentID = '$submittedForm->ID'"); - if($SubmittedFormField) { - $email->setTo($SubmittedFormField->Value); + + if(defined('Database::USE_ANSI_SQL')) { + $submittedFormField = DataObject::get_one("SubmittedFormField", "\"Name\" = '$name' AND \"ParentID\" = '$submittedForm->ID'"); + } else { + $submittedFormField = DataObject::get_one("SubmittedFormField", "Name = '$name' AND ParentID = '$submittedForm->ID'"); + } + + if($submittedFormField) { + $email->setTo($submittedFormField->Value); } } $email->send(); diff --git a/code/editor/FieldEditor.php b/code/editor/FieldEditor.php index 8020bcc..081d45f 100755 --- a/code/editor/FieldEditor.php +++ b/code/editor/FieldEditor.php @@ -86,9 +86,7 @@ class FieldEditor extends FormField { * @param DataObject Record to Save it In */ function saveInto(DataObject $record) { - $name = $this->name; - $fieldSet = $record->$name(); // @todo shouldn't we deal with customFormActions on that object? @@ -100,16 +98,22 @@ class FieldEditor extends FormField { // alternatively, we could delete all the fields and re add them $missingFields = array(); - foreach($fieldSet as $existingField){ - $missingFields[$existingField->ID] = $existingField; - } - - if($_REQUEST[$name]){ - foreach( array_keys( $_REQUEST[$name] ) as $newEditableID ) { - $newEditableData = $_REQUEST[$name][$newEditableID]; - - $editable = DataObject::get_one('EditableFormField', "(`ParentID`='{$record->ID}' OR `ParentID`=0) AND `EditableFormField`.`ID`='$newEditableID'" ); + foreach($fieldSet as $existingField) { + $missingFields[$existingField->ID] = $existingField; + } + if($_REQUEST[$name]) { + foreach(array_keys($_REQUEST[$name]) as $newEditableID) { + if(!is_numeric($newEditableID)) continue; + + $newEditableData = $_REQUEST[$name][$newEditableID]; + + if(defined('Database::USE_ANSI_SQL')) { + $editable = DataObject::get_one('EditableFormField', "(\"ParentID\" = '{$record->ID}' OR \"ParentID\" = '0') AND \"EditableFormField\".\"ID\" = '$newEditableID'"); + } else { + $editable = DataObject::get_one('EditableFormField', "(`ParentID` = '{$record->ID}' OR `ParentID` = 0) AND `EditableFormField`.`ID`='$newEditableID'" ); + } + // check if we are updating an existing field. One odd thing is a 'deleted' field // still exists in the post data (ID) so we need to check for type. if($editable && isset($missingFields[$editable->ID]) && isset($newEditableData['Type'])) { @@ -154,7 +158,13 @@ class FieldEditor extends FormField { if($parentID) { $parentID = Convert::raw2sql($parentID); // who knows what could happen - $highestSort = DB::query("SELECT MAX(Sort) FROM EditableFormField WHERE ParentID = '$parentID'"); + + if(defined('Database::USE_ANSI_SQL')) { + $highestSort = DB::query("SELECT MAX(\"Sort\") FROM \"EditableFormField\" WHERE \"ParentID\" = '$parentID'"); + } else { + $highestSort = DB::query("SELECT MAX(Sort) FROM EditableFormField WHERE ParentID = '$parentID'"); + } + $sort = $highestSort->value() + 1; $className = (isset($_REQUEST['Type'])) ? $_REQUEST['Type'] : ''; @@ -185,7 +195,13 @@ class FieldEditor extends FormField { // work out the sort by getting the sort of the last field in the form +1 if($parent) { $sql_parent = Convert::raw2sql($parent); - $highestSort = DB::query("SELECT MAX(Sort) FROM EditableOption WHERE ParentID = '$sql_parent'"); + + if(defined('Database::USE_ANSI_SQL')) { + $highestSort = DB::query("SELECT MAX(\"Sort\") FROM \"EditableOption\" WHERE \"ParentID\" = '$sql_parent'"); + } else { + $highestSort = DB::query("SELECT MAX(Sort) FROM EditableOption WHERE ParentID = '$sql_parent'"); + } + $sort = $highestSort->value() + 1; if($parent) { diff --git a/code/submissions/SubmittedFormReportField.php b/code/submissions/SubmittedFormReportField.php index c6cb049..4643370 100755 --- a/code/submissions/SubmittedFormReportField.php +++ b/code/submissions/SubmittedFormReportField.php @@ -60,7 +60,12 @@ class SubmittedFormReportField extends FormField { } // Get the CSV header rows from the database - $tmp = DB::query("SELECT DISTINCT Name, Title FROM SubmittedFormField LEFT JOIN SubmittedForm ON SubmittedForm.ID = SubmittedFormField.ParentID WHERE SubmittedFormField.ParentID IN (" . implode(',', $inClause) . ")"); + + if(defined('Database::USE_ANSI_SQL')) { + $tmp = DB::query("SELECT DISTINCT \"Name\", \"Title\" FROM \"SubmittedFormField\" LEFT JOIN \"SubmittedForm\" ON \"SubmittedForm\".\"ID\" = \"SubmittedFormField\".\"ParentID\" WHERE \"SubmittedFormField\".\"ParentID\" IN (" . implode(',', $inClause) . ")"); + } else { + $tmp = DB::query("SELECT DISTINCT Name, Title FROM SubmittedFormField LEFT JOIN SubmittedForm ON SubmittedForm.ID = SubmittedFormField.ParentID WHERE SubmittedFormField.ParentID IN (" . implode(',', $inClause) . ")"); + } // Sort the Names and Titles from the database query into separate keyed arrays foreach($tmp as $array) {