#1067 - Функция коппи блоков страниц

parent d2403a38
...@@ -22,6 +22,7 @@ class BlockAdminController extends AdminController ...@@ -22,6 +22,7 @@ class BlockAdminController extends AdminController
'Manage' => 'Управление блоками', 'Manage' => 'Управление блоками',
'Create' => 'Добавление блока', 'Create' => 'Добавление блока',
'Update' => 'Редактирование блока', 'Update' => 'Редактирование блока',
'Copy' => 'Копирование блока',
'Delete' => 'Удаление блока', 'Delete' => 'Удаление блока',
'View' => 'Просмотр блока', 'View' => 'Просмотр блока',
]; ];
...@@ -156,6 +157,47 @@ class BlockAdminController extends AdminController ...@@ -156,6 +157,47 @@ class BlockAdminController extends AdminController
]); ]);
} }
public function actionCopy($id)
{
$model = $this->findModel($id);
$model->name = $model->title = null;
Yii::$app->controller->page_title = 'Копировать блок';
Yii::$app->controller->breadcrumbs = [
['Управление блоками' => \yii\helpers\Url::toRoute('manage')],
'Копировать блок',
];
if (Yii::$app->request->isPost)
{
$transaction = Yii::$app->db->beginTransaction();
try
{
$model = new CoBlocks;
$model->attributes = Yii::$app->request->post('CoBlocks');
if($model->save())
{
$transaction->commit();
return $this->redirect(['manage']);
}
}
catch (\Exception $e)
{
$transaction->rollBack();
throw $e;
}
}
return $this->render('update', [
'model' => $model,
]);
}
/** /**
* Deletes an existing CoBlocks model. * Deletes an existing CoBlocks model.
* If deletion is successful, the browser will be redirected to the 'index' page. * If deletion is successful, the browser will be redirected to the 'index' page.
......
...@@ -178,56 +178,41 @@ class ContentAdminController extends AdminController ...@@ -178,56 +178,41 @@ class ContentAdminController extends AdminController
{ {
$model = $this->findModel($id); $model = $this->findModel($id);
$transaction = Yii::$app->db->beginTransaction(); $model->url = null;
try Yii::$app->controller->page_title = 'Копирование страницы';
{
$copy = new CoContent();
$data = $model->attributes; Yii::$app->controller->breadcrumbs = [
unset($data['id']); ['Управление контентом' => \yii\helpers\Url::toRoute('manage')],
$copy->setAttributes($data); $model->url,
$copy->save(false); ];
if($model->metaTags) if (Yii::$app->request->isPost)
{
foreach ($model->metaTags as $mt)
{ {
$mtn = new \common\models\MetaTags; $transaction = Yii::$app->db->beginTransaction();
$data = $mt->attributes;
unset($data['id']);
$mtn->setAttributes($data);
$mtn->object_id = $copy->id;
$mtn->save(false);
}
}
if($model->langs) try
{
foreach ($model->langs as $lang)
{ {
$lng = new CoContentLang; $model = new CoContent;
$data = $lang->attributes; $model->attributes = Yii::$app->request->post('CoContent');
unset($data['id']);
$lng->setAttributes($data);
$lng->name .= ' (Копия)';
$lng->content_id = $copy->id;
$lng->save(false);
}
}
if($model->save())
{
$transaction->commit(); $transaction->commit();
return $this->redirect(['manage']);
} }
catch (Exception $e) }
catch (\Exception $e)
{ {
$transaction->rollBack(); $transaction->rollBack();
throw $e; throw $e;
} }
}
return $this->render('update', [
$this->redirect(['manage']); 'model' => $model,
]);
} }
/** /**
......
...@@ -43,7 +43,7 @@ class CoBlocks extends \common\components\ActiveRecordModel ...@@ -43,7 +43,7 @@ class CoBlocks extends \common\components\ActiveRecordModel
'class' => 'common\modules\languages\components\LanguageHelperBehavior', 'class' => 'common\modules\languages\components\LanguageHelperBehavior',
'field' => 'block_id', 'field' => 'block_id',
'langClass' => 'common\modules\content\models\CoBlocksLang', 'langClass' => 'common\modules\content\models\CoBlocksLang',
'actions' => ['create', 'update'] 'actions' => ['create', 'update', 'copy']
], ],
]; ];
} }
...@@ -55,6 +55,7 @@ class CoBlocks extends \common\components\ActiveRecordModel ...@@ -55,6 +55,7 @@ class CoBlocks extends \common\components\ActiveRecordModel
{ {
return [ return [
[['title', 'name'], 'required'], [['title', 'name'], 'required'],
[['title'], 'unique'],
[['category_id'], 'safe'], [['category_id'], 'safe'],
[['title'], 'string', 'max' => 250], [['title'], 'string', 'max' => 250],
[['name'], 'string', 'max' => 50], [['name'], 'string', 'max' => 50],
......
...@@ -32,6 +32,8 @@ class CoContent extends \common\components\ActiveRecordModel ...@@ -32,6 +32,8 @@ class CoContent extends \common\components\ActiveRecordModel
const CUSTOM_DARK = 'dark'; const CUSTOM_DARK = 'dark';
const CUSTOM_WHITE = 'white'; const CUSTOM_WHITE = 'white';
public $copyLangs;
public static $cutom_list = [ public static $cutom_list = [
self::CUSTOM_DARK => 'Темный', self::CUSTOM_DARK => 'Темный',
self::CUSTOM_WHITE => 'Светлый', self::CUSTOM_WHITE => 'Светлый',
...@@ -53,13 +55,13 @@ class CoContent extends \common\components\ActiveRecordModel ...@@ -53,13 +55,13 @@ class CoContent extends \common\components\ActiveRecordModel
return [ return [
'meta' => [ 'meta' => [
'class' => 'common\components\activeRecordBehaviors\MetaTagBehavior', 'class' => 'common\components\activeRecordBehaviors\MetaTagBehavior',
'actions' => ['create', 'update'] 'actions' => ['create', 'update', 'copy']
], ],
'langs' => [ 'langs' => [
'class' => 'common\modules\languages\components\LanguageHelperBehavior', 'class' => 'common\modules\languages\components\LanguageHelperBehavior',
'field' => 'content_id', 'field' => 'content_id',
'langClass' => 'common\modules\content\models\CoContentLang', 'langClass' => 'common\modules\content\models\CoContentLang',
'actions' => ['create', 'update'] 'actions' => ['create', 'update', 'copy']
], ],
'file' => [ 'file' => [
'class' => 'common\components\activeRecordBehaviors\FileUploadBehavior', 'class' => 'common\components\activeRecordBehaviors\FileUploadBehavior',
......
<?php <?php
use yii\helpers\Html; use yii\helpers\Html;
use yii\helpers\Url;
use \yii\helpers\ArrayHelper; use \yii\helpers\ArrayHelper;
use \common\modules\content\models\CoCategory; use \common\modules\content\models\CoCategory;
/* @var $this yii\web\View */ /* @var $this yii\web\View */
...@@ -44,9 +45,17 @@ use \common\modules\content\models\CoCategory; ...@@ -44,9 +45,17 @@ use \common\modules\content\models\CoCategory;
[ [
'class' => 'common\components\ColorActionColumn', 'class' => 'common\components\ColorActionColumn',
'template' => '{update} {delete}', 'template' => '{copy} {update} {delete}',
] 'buttons' => [
, 'copy' => function ($url, $model, $key) {
return '<a href="'.Url::toRoute(['copy', 'id' => $model->id]).'">'.Html::beginTag('i', [
'title' => "Копировать блок",
'data-toggle' => 'tooltip',
'class' => 'fa fa-copy fa-lg'
]) . Html::endTag('i') . '</a>';
},
],
],
], ],
]); ?> ]); ?>
......
...@@ -133,7 +133,7 @@ class LanguageHelperBehavior extends Behavior ...@@ -133,7 +133,7 @@ class LanguageHelperBehavior extends Behavior
public function Delete($event) public function Delete($event)
{ {
if($this->owner->langs) if(isset($this->owner->langs))
{ {
foreach ($this->owner->langs as $lang) foreach ($this->owner->langs as $lang)
{ {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment