diff --git a/Model/Behavior/SavePageBehavior.php b/Model/Behavior/SavePageBehavior.php index 3751edb..f079e0b 100644 --- a/Model/Behavior/SavePageBehavior.php +++ b/Model/Behavior/SavePageBehavior.php @@ -219,6 +219,7 @@ public function saveTheme(Model $model, $data) { * @param array $data request data * @return bool * @throws InternalErrorException + * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function saveMove(Model $model, $data) { //トランザクションBegin @@ -243,8 +244,19 @@ public function saveMove(Model $model, $data) { $result = $model->moveDown($model->id, $childCount); } elseif ($data[$model->alias]['type'] === 'move') { //callbacksは必要 - $result = - $model->saveField('parent_id', $data[$model->alias]['parent_id'], ['callbacks' => true]); + $permalink = + $model->getParentPermalink($data['Page']) . '/' . Current::read('Page.slug'); + if (substr($permalink, 0, 1) === '/') { + $permalink = substr($permalink, 1); + } + $data[$model->alias]['permalink'] = $permalink; + $model->create(false); + $options = [ + 'validate' => false, + 'fieldList' => ['parent_id', 'permalink'], + 'callbacks' => true + ]; + $result = $model->save($data, $options); } else { $result = false; } diff --git a/Model/Page.php b/Model/Page.php index d364210..bf5f250 100644 --- a/Model/Page.php +++ b/Model/Page.php @@ -344,9 +344,9 @@ public function isUniquePermalink($fields) { */ public function afterSave($created, $options = array()) { if (Hash::get($this->data, 'Page.id') && - Hash::get($this->data, 'Page.slug') !== Current::read('Page.slug')) { + Hash::get($this->data, 'Page.permalink')) { $chidren = $this->children( - Hash::get($this->data, 'Page.id'), false, array('Page.id', 'Page.permalink') + Hash::get($this->data, 'Page.id'), false, array('Page.id', 'Page.permalink', 'Page.slug') ); $data = $this->data; @@ -354,13 +354,11 @@ public function afterSave($created, $options = array()) { foreach ($chidren as $child) { $this->id = $child[$this->alias]['id']; - $pattern = '/^' . preg_quote(Current::read('Page.permalink') . '/', '/') . '/'; - $permalink = preg_replace( - $pattern, Hash::get($data, 'Page.permalink') . '/', $child[$this->alias]['permalink'] - ); - - if (! $this->saveField('permalink', $permalink, array('callbacks' => false))) { - throw new InternalErrorException(__d('net_commons', 'Internal Server Error')); + $permalink = Hash::get($data, 'Page.permalink') . '/' . $child[$this->alias]['slug']; + if ($permalink !== $child[$this->alias]['permalink']) { + if (! $this->saveField('permalink', $permalink, array('callbacks' => false))) { + throw new InternalErrorException(__d('net_commons', 'Internal Server Error')); + } } }