Skip to content
Browse files

BUGFIX Fixed MemberTableField->addtogroup() to fetch existing Member …

…records by ID or $unique_identifier_field instead of relying on the (now removed) "auto-merging" in Member->onBeforeWrite() (see r100705)

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/cms/trunk@100716 467b73ca-7a2a-4603-9d3b-597d59a354a9
  • Loading branch information...
1 parent 9c75acb commit 4eed1360c4e8daab4ce5d9ae25ee3234586435a4 @chillu chillu committed
Showing with 24 additions and 7 deletions.
  1. +24 −7 code/MemberTableField.php
View
31 code/MemberTableField.php
@@ -160,23 +160,40 @@ function SearchForm() {
*/
function addtogroup() {
$data = $_REQUEST;
- unset($data['ID']);
- $ctfID = isset($data['ctf']) ? $data['ctf']['ID'] : null;
+ $groupID = (isset($data['ctf']['ID'])) ? $data['ctf']['ID'] : null;
- if(!is_numeric($ctfID)) {
+ if(!is_numeric($groupID)) {
FormResponse::status_messsage(_t('MemberTableField.ADDINGFIELD', 'Adding failed'), 'bad');
+ return;
}
+ // Get existing record either by ID or unique identifier.
+ $identifierField = Member::get_unique_identifier_field();
$className = self::$data_class;
- $record = new $className();
-
+ $record = null;
+ if(isset($data['ID']) && $data['ID']) {
+ $record = DataObject::get_by_id($className, $data['ID']);
+ } elseif(isset($data[$identifierField])) {
+ $record = DataObject::get_one(
+ $className,
+ sprintf('"%s" = \'%s\'', $identifierField, $data[$identifierField])
+ );
+ }
+
+ // Fall back to creating a new record
+ if(!$record) $record = new $className();
+
+ // Update an existing record, or populate a new one.
+ // If values on an existing (autocompleted) record have been changed,
+ // they will overwrite current data.
$record->update($data);
+ // Validate record, mainly password restrictions.
+ // Note: Doesn't use Member_Validator
$valid = $record->validate();
-
if($valid->valid()) {
$record->write();
- $record->Groups()->add($ctfID);
+ $record->Groups()->add($groupID);
$this->sourceItems();

0 comments on commit 4eed136

Please sign in to comment.
Something went wrong with that request. Please try again.