/** * Clone page action. * @param integer $id * @param array|string $returnUrl * @throws \yii\web\NotFoundHttpException */ public function actionClone($id, $returnUrl = ['index']) { /** @var Page|HasProperties $model */ $model = Page::findOne($id); if ($model === null) { throw new NotFoundHttpException(); } /** @var Page|HasProperties $newModel */ $newModel = new Page(); $newModel->setAttributes($model->attributes, false); $time = time(); $newModel->name .= ' (copy ' . date('Y-m-d h:i:s', $time) . ')'; $newModel->slug .= '-copy-' . date('Ymdhis', $time); $newModel->title .= '-copy-' . date('Ymdhis', $time); $newModel->id = null; if ($newModel->validate() === false) { $newModel->slug = substr(uniqid() . "-" . $model->slug, 0, 80); } if ($newModel->save()) { $object = Object::getForClass(get_class($newModel)); $query = new Query(); // save images bindings $params = $query->select(['object_id', 'filename', 'image_title', 'image_alt', 'sort_order'])->from(Image::tableName())->where(['object_id' => $object->id, 'object_model_id' => $model->id])->all(); if (!empty($params)) { $rows = []; foreach ($params as $param) { $rows[] = [$param['object_id'], $newModel->id, $param['filename'], $param['image_title'], $param['image_alt'], $param['sort_order']]; } Yii::$app->db->createCommand()->batchInsert(Image::tableName(), ['object_id', 'object_model_id', 'filename', 'image_title', 'image_alt', 'sort_order'], $rows)->execute(); } $newModelProps = []; foreach (array_keys($model->propertyGroups) as $key) { $opg = new ObjectPropertyGroup(); $opg->attributes = ['object_id' => $object->id, 'object_model_id' => $newModel->id, 'property_group_id' => $key]; $opg->save(); $props = Property::getForGroupId($key); foreach ($props as $prop) { $propValues = $model->getPropertyValuesByPropertyId($prop->id); if ($propValues !== null) { foreach ($propValues->values as $val) { $valueToSave = ArrayHelper::getValue($val, 'psv_id', $val['value']); $newModelProps[$prop->key][] = $valueToSave; } } } } $newModel->saveProperties(['Properties_Page_' . $newModel->id => $newModelProps]); $view = ViewObject::findOne(['object_id' => $model->object->id, 'object_model_id' => $model->id]); if ($view !== null) { $newView = new ViewObject(); $newView->setAttributes($view->attributes, false); $newView->id = null; $newView->object_model_id = $newModel->id; $newView->save(); } Yii::$app->session->setFlash('success', Yii::t('app', 'Page has been cloned successfully.')); $this->redirect(['edit', 'id' => $newModel->id, 'parent_id' => $newModel->parent_id, 'returnUrl' => $returnUrl]); } }