From 8b1e5a3c41e21034d2616add7a8e89cd099b5e8c Mon Sep 17 00:00:00 2001 From: Ryan Cramer Date: Mon, 14 Nov 2016 07:57:29 -0500 Subject: [PATCH] Fix issue processwire/processwire-issues#66 where deleting item from 2 different repeater fields at same time resulted in only item from first repeater being deleted --- wire/core/Pages.php | 7 ++++-- wire/core/PagesEditor.php | 25 +++++++++++++------ .../FieldtypeRepeater.module | 3 ++- 3 files changed, 25 insertions(+), 10 deletions(-) diff --git a/wire/core/Pages.php b/wire/core/Pages.php index be9f91c5..344d1c39 100644 --- a/wire/core/Pages.php +++ b/wire/core/Pages.php @@ -476,8 +476,11 @@ class Pages extends Wire { * #pw-group-manipulation * * @param Page $page Page to delete - * @param bool $recursive If set to true, then this will attempt to delete all children too. - * @param array $options Optional settings to change behavior (for the future, none currently in use). + * @param bool|array $recursive If set to true, then this will attempt to delete all children too. + * If you don't need this argument, optionally provide $options array instead. + * @param array $options Optional settings to change behavior: + * - uncacheAll (bool): Whether to clear memory cache after delete (default=false) + * - recursive (bool): Same as $recursive argument, may be specified in $options array if preferred. * @return bool|int Returns true (success), or integer of quantity deleted if recursive mode requested. * @throws WireException on fatal error * @see Pages::trash() diff --git a/wire/core/PagesEditor.php b/wire/core/PagesEditor.php index ce8e2a59..2f021bd0 100644 --- a/wire/core/PagesEditor.php +++ b/wire/core/PagesEditor.php @@ -969,24 +969,34 @@ class PagesEditor extends Wire { * this method will throw an exception. If a recursive delete fails for any reason, an exception will be thrown. * * @param Page $page - * @param bool $recursive If set to true, then this will attempt to delete all children too. - * @param array $options Optional settings to change behavior (for the future) + * @param bool|array $recursive If set to true, then this will attempt to delete all children too. + * If you don't need this argument, optionally provide $options array instead. + * @param array $options Optional settings to change behavior: + * - uncacheAll (bool): Whether to clear memory cache after delete (default=false) + * - recursive (bool): Same as $recursive argument, may be specified in $options array if preferred. * @return bool|int Returns true (success), or integer of quantity deleted if recursive mode requested. * @throws WireException on fatal error * */ public function delete(Page $page, $recursive = false, array $options = array()) { + + $defaults = array( + 'uncacheAll' => false, + 'recursive' => is_bool($recursive) ? $recursive : false, + ); + + if(is_array($recursive)) $options = $recursive; + $options = array_merge($defaults, $options); - if($options) {} // to ignore unused parameter inspection if(!$this->isDeleteable($page)) throw new WireException("This page may not be deleted"); $numDeleted = 0; if($page->numChildren) { - if(!$recursive) { + if(!$options['recursive']) { throw new WireException("Can't delete Page $page because it has one or more children."); } else foreach($page->children("include=all") as $child) { /** @var Page $child */ - if($this->pages->delete($child, true)) { + if($this->pages->delete($child, true, $options)) { $numDeleted++; } else { throw new WireException("Error doing recursive page delete, stopped by page $child"); @@ -1008,6 +1018,7 @@ class PagesEditor extends Wire { } catch(\Exception $e) { } + /** @var PagesAccess $access */ $access = $this->wire(new PagesAccess()); $access->deletePage($page); @@ -1026,10 +1037,10 @@ class PagesEditor extends Wire { $page->status = Page::statusDeleted; // no need for bitwise addition here, as this page is no longer relevant $this->pages->deleted($page); $numDeleted++; - $this->pages->uncacheAll($page); + if($options['uncacheAll']) $this->pages->uncacheAll($page); $this->pages->debugLog('delete', $page, true); - return $recursive ? $numDeleted : true; + return $options['recursive'] ? $numDeleted : true; } /** diff --git a/wire/modules/Fieldtype/FieldtypeRepeater/FieldtypeRepeater.module b/wire/modules/Fieldtype/FieldtypeRepeater/FieldtypeRepeater.module index 2fc3f055..cb568159 100644 --- a/wire/modules/Fieldtype/FieldtypeRepeater/FieldtypeRepeater.module +++ b/wire/modules/Fieldtype/FieldtypeRepeater/FieldtypeRepeater.module @@ -1057,6 +1057,7 @@ class FieldtypeRepeater extends Fieldtype implements ConfigurableModule { } if(in_array($operator, array('*=', '~=', '^=', '$=', '%='))) { + /** @var DatabaseQuerySelectFulltext $ft */ $ft = $this->wire(new DatabaseQuerySelectFulltext($query)); $ft->match($table, $subfield, $operator, $value); @@ -1221,7 +1222,7 @@ class FieldtypeRepeater extends Fieldtype implements ConfigurableModule { if($value->has($p)) continue; // $this->message("Deleted Repeater", Notice::debug); // delete the repeater page - $this->wire('pages')->delete($p); + $this->wire('pages')->delete($p, $saveOptions); } $result = parent::___savePageField($page, $field);