-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added copy pages feature, Web UI and CLI command, tests, Tree model r…
…efactored
- Loading branch information
Christopher Stebe
committed
Feb 21, 2017
1 parent
0bbc5c8
commit 6706908
Showing
22 changed files
with
618 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,188 @@ | ||
<?php | ||
/** | ||
* @link http://www.diemeisterei.de/ | ||
* @copyright Copyright (c) 2017 diemeisterei GmbH, Stuttgart | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
namespace dmstr\modules\pages\commands; | ||
|
||
use dmstr\modules\pages\models\Tree; | ||
use yii\console\Exception; | ||
use yii\db\Expression; | ||
|
||
/** | ||
* Pages module copy command | ||
* @package dmstr\modules\pages\commands | ||
* @author Christopher Stebe <[email protected]> | ||
*/ | ||
class CopyController extends \yii\console\Controller | ||
{ | ||
/** | ||
* @const string | ||
*/ | ||
const DESCRIPTION = "Pages module copy command"; | ||
|
||
/** | ||
* @var integer | ||
*/ | ||
public $rootId; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
public $destinationLanguage; | ||
|
||
/** | ||
* @param string $id | ||
* | ||
* @return array | ||
*/ | ||
public function options($id) | ||
{ | ||
return array_merge( | ||
parent::options($id), | ||
[ | ||
'rootId', | ||
'destinationLanguage', | ||
] | ||
); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function init() | ||
{ | ||
parent::init(); | ||
} | ||
|
||
/** | ||
* Show information about this command | ||
*/ | ||
public function actionIndex() | ||
{ | ||
$actions = [ | ||
$this->id . '/root-node', | ||
]; | ||
echo "\n" . self::DESCRIPTION . "\n"; | ||
echo "----------------------------------------\n\n"; | ||
foreach ($actions as $action) { | ||
echo "yii " . $action . "\n"; | ||
} | ||
echo "\n\n"; | ||
} | ||
|
||
/** | ||
* Copy a root node to another language | ||
* | ||
* @param $rootId | ||
* @param $destinationLanguage | ||
* | ||
* @return bool | ||
*/ | ||
public function actionRootNode($rootId, $destinationLanguage) | ||
{ | ||
// disable access trait | ||
Tree::$activeAccessTrait = false; | ||
|
||
// transaction begin | ||
$transaction = \Yii::$app->db->beginTransaction(); | ||
|
||
// try copy root node with children | ||
try { | ||
/** | ||
* Find source root node | ||
* | ||
* @var Tree $sourceRootNode | ||
*/ | ||
$sourceRootNode = Tree::findOne([Tree::ATTR_ID => $rootId, Tree::ATTR_LVL => Tree::ROOT_NODE_LVL]); | ||
if ($sourceRootNode === null) { | ||
throw new Exception(\Yii::t('pages', 'Root node with ID={ID} not found', ['ID' => $rootId]), 404); | ||
} | ||
|
||
/** | ||
* make new root in destination language | ||
*/ | ||
|
||
// check if not already exists | ||
$newRootNodeExists = Tree::findOne( | ||
[Tree::ATTR_DOMAIN_ID => $sourceRootNode->domain_id, Tree::ATTR_ACCESS_DOMAIN => $destinationLanguage] | ||
); | ||
|
||
|
||
if ($newRootNodeExists instanceof Tree) { | ||
throw new Exception( | ||
\Yii::t( | ||
'pages', | ||
'Root node with domain_id="{DOMAIN_ID}" and access_domain="{ACCESS_DOMAIN}" already exists', | ||
['DOMAIN_ID' => $sourceRootNode->domain_id, 'ACCESS_DOMAIN' => $destinationLanguage] | ||
), 500 | ||
); | ||
} | ||
|
||
// make new root node | ||
$newRootNode = new Tree($sourceRootNode->attributes); | ||
$newRootNode->id = null; | ||
$newRootNode->name = str_replace( | ||
$sourceRootNode->access_domain, | ||
$destinationLanguage, | ||
$sourceRootNode->name | ||
); | ||
$newRootNode->access_domain = $destinationLanguage; | ||
$newRootNode->created_at = new Expression('NOW()'); | ||
$newRootNode->updated_at = new Expression('NOW()'); | ||
|
||
// detach nested set behavior to be able to raw insert records | ||
$newRootNode->detachBehavior('tree'); | ||
$newRootNode->save(); | ||
|
||
// set the new page id as root | ||
$newRootNode->root = $newRootNode->id; | ||
$newRootNode->save(); | ||
|
||
|
||
if (!empty($newRootNode->getErrors())) { | ||
throw new Exception(implode(', ', $newRootNode->getErrors())); | ||
} | ||
|
||
|
||
// make new child leaves in destination language | ||
$childLeaveQuery = Tree::find()->where([Tree::ATTR_ROOT => $sourceRootNode->id]); | ||
$childLeaveQuery->andWhere(['NOT', [Tree::ATTR_ID => $sourceRootNode->id]]); | ||
|
||
|
||
foreach ($childLeaveQuery->all() as $sourceChildLeave) { | ||
/** | ||
* make new child leave | ||
* | ||
* @var Tree $childLeave | ||
*/ | ||
$newChildNode = new Tree($sourceChildLeave->attributes); | ||
$newChildNode->id = null; | ||
$newChildNode->root = $newRootNode->id; | ||
$newChildNode->access_domain = $destinationLanguage; | ||
$newChildNode->created_at = new Expression('NOW()'); | ||
$newChildNode->updated_at = new Expression('NOW()'); | ||
|
||
// detach nested set behavior to be able to raw insert records | ||
$newChildNode->detachBehavior('tree'); | ||
$newChildNode->save(); | ||
|
||
if (!empty($newChildNode->getErrors())) { | ||
throw new Exception(implode(', ', $newRootNode->getErrors())); | ||
} | ||
} | ||
|
||
// Success | ||
$this->stdout('"' . Tree::optsSourceRootId()[$rootId] . '" successfully copied to language "' . $destinationLanguage . '"'); | ||
$transaction->commit(); | ||
\Yii::$app->end(); | ||
} catch (Exception $e) { | ||
$transaction->rollBack(); | ||
$this->stderr($e->getMessage()); | ||
\Yii::$app->end(1); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
/** | ||
* @link http://www.diemeisterei.de/ | ||
* @copyright Copyright (c) 2017 diemeisterei GmbH, Stuttgart | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace dmstr\modules\pages\controllers; | ||
|
||
use dmstr\modules\pages\models\forms\CopyForm; | ||
use mikehaertl\shellcommand\Command; | ||
use rmrevin\yii\fontawesome\AssetBundle; | ||
use yii\helpers\Url; | ||
use yii\web\Controller; | ||
|
||
|
||
/** | ||
* Class CopyController | ||
* @package dmstr\modules\pages\controllers | ||
* @author Christopher Stebe <[email protected]> | ||
*/ | ||
class CopyController extends Controller | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
public $defaultAction = 'root-node'; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function init() | ||
{ | ||
parent::init(); | ||
|
||
// Register font-awesome asset bundle | ||
AssetBundle::register(\Yii::$app->view); | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function actionRootNode() | ||
{ | ||
Url::remember(); | ||
|
||
$model = new CopyForm(); | ||
if ($model->load(\Yii::$app->request->post()) && $model->validate()) { | ||
|
||
// RUN copy-pages cli command | ||
$command = new Command('yii copy-pages/root-node'); | ||
$command->addArg('--rootId', $model->sourceRootId); | ||
$command->addArg('--destinationLanguage', $model->destinationLanguage); | ||
if ($command->execute() && empty($command->getError())) { | ||
\Yii::$app->session->setFlash('success', $command->getOutput()); | ||
} else { | ||
\Yii::$app->session->setFlash('danger', $command->getError()); | ||
} | ||
|
||
return $this->refresh(); | ||
} | ||
return $this->render('root-node', ['copyForm' => $model]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
use yii\db\Migration; | ||
|
||
class m170220_121800_auth_items extends Migration | ||
{ | ||
public function up() | ||
{ | ||
$auth = Yii::$app->authManager; | ||
|
||
if ($auth) { | ||
$permission = $auth->createPermission('pages_copy'); | ||
$permission->description = 'Pages Copy'; | ||
$auth->add($permission); | ||
} | ||
} | ||
|
||
public function down() | ||
{ | ||
echo "m170220_121800_auth_items cannot be reverted.\n"; | ||
|
||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.