From 8ae5ee7e89c8d6fa4226fc019ba4099df1644c20 Mon Sep 17 00:00:00 2001 From: Piotr Konieczny Date: Fri, 17 Jul 2015 15:36:03 +0200 Subject: [PATCH 1/2] Unique fields bugfix --- modules/core/Collections/Controller/Api.php | 37 ++++++++++++--------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/modules/core/Collections/Controller/Api.php b/modules/core/Collections/Controller/Api.php index f77850ca1..dd89384d7 100644 --- a/modules/core/Collections/Controller/Api.php +++ b/modules/core/Collections/Controller/Api.php @@ -176,21 +176,28 @@ public function saveentry(){ $col = "collection".$collection["_id"]; // Check for uniqueeness of fields set as unique - foreach ($collection['fields'] as $fieldDefinition) { - - $fieldName = $fieldDefinition['name']; - - if ( - $fieldDefinition['unique'] && - isset($entry[$fieldName]) && - $this->app->db->findOne("collections/{$col}", [$fieldName => $entry[$fieldName]]) - ) { - $this->app->response->status = 409; - - return sprintf( - $this->app->helper('i18n')->get("There is already an entry in this collection with this slug for field '%s'."), - $fieldName - ); + foreach ($collection["fields"] as $fieldDefinition) { + + $fieldName = $fieldDefinition["name"] . "_slug"; + + if ($fieldDefinition["slug"] && $fieldDefinition["unique"] && isset($entry[$fieldName])) { + + $collision = $this->app->db->findOne("collections/{$col}", [$fieldName => $entry[$fieldName]]); + + if (!$collision) { + continue; + } + + // New and colliding or other entry + if (!isset($entry["_id"]) || $entry["_id"] != $collision["_id"]) { + + $this->app->response->status = 409; + + return sprintf( + $this->app->helper("i18n")->get("There is already an entry in this collection with this slug for field '%s'."), + $fieldDefinition["label"] + ); + } } } From 8d42136652c91e9aaa54269a358ee6e22277954a Mon Sep 17 00:00:00 2001 From: Piotr Konieczny Date: Fri, 17 Jul 2015 15:55:49 +0200 Subject: [PATCH 2/2] Fix for PHP notice when entry params (slug, unique, field_slug) are not available in array. --- modules/core/Collections/Controller/Api.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/modules/core/Collections/Controller/Api.php b/modules/core/Collections/Controller/Api.php index dd89384d7..3eb9d7526 100644 --- a/modules/core/Collections/Controller/Api.php +++ b/modules/core/Collections/Controller/Api.php @@ -178,11 +178,11 @@ public function saveentry(){ // Check for uniqueeness of fields set as unique foreach ($collection["fields"] as $fieldDefinition) { - $fieldName = $fieldDefinition["name"] . "_slug"; + $slugName = $fieldDefinition["name"] . "_slug"; - if ($fieldDefinition["slug"] && $fieldDefinition["unique"] && isset($entry[$fieldName])) { + if (!empty($fieldDefinition["slug"]) && !empty($fieldDefinition["unique"]) && isset($entry[$slugName])) { - $collision = $this->app->db->findOne("collections/{$col}", [$fieldName => $entry[$fieldName]]); + $collision = $this->app->db->findOne("collections/{$col}", [$slugName => $entry[$slugName]]); if (!$collision) { continue;