Skip to content

Commit

Permalink
Merge pull request #278 from humhub/fix/277-delete-template
Browse files Browse the repository at this point in the history
Delete not published linked contents on delete template
  • Loading branch information
luke- committed May 17, 2023
2 parents 26db489 + 98a19b1 commit 5a05ad3
Show file tree
Hide file tree
Showing 9 changed files with 203 additions and 19 deletions.
7 changes: 5 additions & 2 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
Changelog
=========

1.9.1 (May 1, 2023)
1.9.2 (Unreleased)
-------------------
- Fix #277: Delete not published linked content on delete template

- 1.9.1 (May 1, 2023)
-------------------
- Fix #274: Hard delete records on disable module
- Fix #265: Fix tests for core v1.14
Expand Down Expand Up @@ -320,4 +324,3 @@ New defaults: php-pages/container_pages/, php-pages/container_snippets/, php-pag
- Enh: #56 Use of select2 dropdown as icon chooser
- Fix: #40 Image/File upload ajax error handling
- Fix: HumHub 1.2.beta.3 support

2 changes: 1 addition & 1 deletion module.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"name": "Custom Pages",
"description": "Allows admins to create custom pages (html or markdown) or external links to various navigations (e.g. top navigation, account menu).",
"keywords": ["pages", "custom", "iframe", "markdown", "link", "navigation", "spaces"],
"version": "1.9.1",
"version": "1.9.2",
"homepage": "https://github.com/humhub/custom-pages",
"humhub": {
"minVersion": "1.14"
Expand Down
35 changes: 32 additions & 3 deletions modules/template/controllers/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use humhub\modules\custom_pages\modules\template\models\RichtextContent;
use humhub\modules\custom_pages\modules\template\models\TemplateSearch;
use humhub\modules\custom_pages\modules\template\models\TextContent;
use Yii;
use humhub\modules\custom_pages\modules\template\models\Template;
use humhub\modules\custom_pages\modules\template\models\forms\AddElementForm;
use humhub\modules\custom_pages\modules\template\models\forms\EditElementForm;
Expand All @@ -27,7 +26,9 @@
use humhub\modules\custom_pages\modules\template\widgets\EditMultipleElementsModal;
use humhub\modules\custom_pages\modules\template\widgets\TemplateContentTable;
use humhub\modules\custom_pages\modules\template\components\TemplateCache;
use Yii;
use yii\base\Response;
use yii\data\ActiveDataProvider;

/**
* Admin controller for managing templates.
Expand Down Expand Up @@ -125,6 +126,30 @@ public function actionEditSource()
]);
}

/**
* Show pages/snippets/containers where the template is used in.
*/
public function actionEditUsage()
{
$model = Template::findOne(['id' => Yii::$app->request->get('id')]);

if ($model == null) {
throw new \yii\web\HttpException(404, Yii::t('CustomPagesModule.modules_template_controllers_AdminController', 'Template not found!'));
}

$dataProvider = new ActiveDataProvider([
'query' => $model->getLinkedRecordsQuery(),
'pagination' => [
'pageSize' => 10
]
]);

return $this->render('@custom_pages/modules/template/views/admin/editUsage', [
'model' => $model,
'dataProvider' => $dataProvider
]);
}

/**
* Returns a selection of all available template content types.
* @return type
Expand Down Expand Up @@ -276,8 +301,12 @@ public function actionDeleteTemplate($id)
{
$template = Template::findOne(['id' => $id]);

if (!$template->delete()) {
$this->view->error(Yii::t('CustomPagesModule.modules_template_controllers_AdminController', 'The template could not be deleted, please get sure that this template is not in use.'));
if ($template) {
if ($template->delete()) {
$this->view->success(Yii::t('CustomPagesModule.base', 'Deleted.'));
} else {
$this->view->error(Yii::t('CustomPagesModule.modules_template_controllers_AdminController', 'The template could not be deleted, please get sure that this template is not in use.'));
}
}

return $this->redirect(['index']);
Expand Down
13 changes: 10 additions & 3 deletions modules/template/models/OwnerContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace humhub\modules\custom_pages\modules\template\models;

use Yii;
use humhub\components\ActiveRecord;
use humhub\modules\content\components\ContentActiveRecord;
use humhub\modules\content\models\Content;
use Yii;

/**
* This is the model class for table "custom_pages_template_content".
Expand Down Expand Up @@ -53,8 +55,13 @@ public function rules()
*/
public function beforeDelete()
{
if ($this->getInstance() != null) {
$this->getInstance()->delete();
$instance = $this->getInstance();
if ($instance !== null) {
if ($instance instanceof ContentActiveRecord || $instance instanceof Content) {
$instance->hardDelete();
} else {
$instance->delete();
}
}

return parent::beforeDelete();
Expand Down
44 changes: 39 additions & 5 deletions modules/template/models/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

namespace humhub\modules\custom_pages\modules\template\models;

use humhub\modules\custom_pages\lib\templates\TemplateEngineFactory;
use humhub\components\ActiveRecord;
use humhub\modules\content\models\Content;
use humhub\modules\custom_pages\lib\templates\TemplateEngineFactory;
use yii\db\ActiveQuery;
use yii\helpers\ArrayHelper;

Expand Down Expand Up @@ -34,6 +35,8 @@
* @property $type string
* @property $allow_for_spaces boolean
* @property $allow_inline_activation boolean
*
* @property-read TemplateElement[] $elements
*/
class Template extends ActiveRecord implements TemplateContentOwner
{
Expand Down Expand Up @@ -124,12 +127,15 @@ public function validType($attribute, $model) {
*/
public function beforeDelete()
{
if(!parent::beforeDelete()) {
if (!parent::beforeDelete()) {
return false;
}

// We just allow the template deletion if there are template owner relations.
if(!$this->isInUse()) {
if (!$this->isInUse()) {
foreach ($this->getContents()->all() as $content) {
$content->hardDelete();
}
foreach ($this->elements as $element) {
$element->delete();
}
Expand All @@ -141,13 +147,29 @@ public function beforeDelete()

public function isInUse()
{
if($this->isLayout()) {
return TemplateInstance::findByTemplateId($this->id)->count() > 0;
if ($this->isLayout()) {
return TemplateInstance::findByTemplateId($this->id, Content::STATE_PUBLISHED)->count() > 0;
} else {
return ContainerContentTemplate::find()->where(['template_id' => $this->id])->count() > 0;
}
}

public function getLinkedRecordsQuery(): ActiveQuery
{
if ($this->isLayout()) {
return $this->getContents()
->andWhere([Content::tableName() . '.state' => Content::STATE_PUBLISHED]);
} else {
return Template::find()
->leftJoin(OwnerContent::tableName(), Template::tableName() . '.id = ' . OwnerContent::tableName() . '.owner_id')
->leftJoin(ContainerContent::tableName(), ContainerContent::tableName() . '.id = ' . OwnerContent::tableName() . '.content_id')
->leftJoin(ContainerContentTemplate::tableName(), ContainerContentTemplate::tableName() . '.definition_id = ' . ContainerContent::tableName() . '.definition_id')
->where([OwnerContent::tableName() . '.owner_model' => Template::class])
->andWhere([OwnerContent::tableName() . '.content_type' => ContainerContent::class])
->andWhere([ContainerContentTemplate::tableName() . '.template_id' => $this->id]);
}
}

/**
* Checks if this template is a root layout template.
* @return boolean
Expand Down Expand Up @@ -176,6 +198,18 @@ public function getElements()
return $this->hasMany(TemplateElement::class, ['template_id' => 'id']);
}

/**
* Returns all Contents linked with this Template.
* @return ActiveQuery
*/
public function getContents(): ActiveQuery
{
return Content::find()->leftJoin(TemplateInstance::tableName(),
Content::tableName() . '.object_model = ' . TemplateInstance::tableName() . '.object_model AND ' .
Content::tableName() . '.object_id = ' . TemplateInstance::tableName() . '.object_id')
->where([TemplateInstance::tableName() . '.template_id' => $this->id]);
}

/**
* Renders the template for the given $owner or with all default content if
* no $owner was given.
Expand Down
15 changes: 13 additions & 2 deletions modules/template/models/TemplateInstance.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace humhub\modules\custom_pages\modules\template\models;

use humhub\components\ActiveRecord;
use humhub\modules\content\models\Content;
use yii\db\ActiveQuery;

/**
* A TemplateInstance represents an acutal instantiation of an Template model.
Expand Down Expand Up @@ -72,9 +74,18 @@ public static function getDefaultElement($id, $elementName)
return $pageTemplate->template->getElement($elementName);
}

public static function findByTemplateId($templateId)
public static function findByTemplateId($templateId, ?int $contentState = null): ActiveQuery
{
return self::find()->where(['template_id' => $templateId]);
$query = self::find()->where(['template_id' => $templateId]);

if ($contentState !== null) {
$query->leftJoin(Content::tableName(),
Content::tableName() . '.object_model = ' . self::tableName() . '.object_model AND ' .
Content::tableName() . '.object_id = ' . self::tableName() . '.object_id')
->andWhere([Content::tableName() . '.state' => $contentState]);
}

return $query;
}

public function render($editMode = false)
Expand Down
7 changes: 5 additions & 2 deletions modules/template/views/admin/edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,15 @@
<?php if (!$model->isNewRecord): ?>
</div>
<ul class="nav nav-tabs tab-sub-menu" id="tabs">
<li class=" active">
<li class="active">
<?= Html::a(Yii::t('CustomPagesModule.base', 'General'), Url::to(['edit', 'id' => $model->id])); ?>
</li>
<li class="">
<li>
<?= Html::a(Yii::t('CustomPagesModule.base', 'Source'), Url::to(['edit-source', 'id' => $model->id])); ?>
</li>
<li>
<?= Html::a(Yii::t('CustomPagesModule.base', 'Usage'), Url::to(['edit-usage', 'id' => $model->id])); ?>
</li>
</ul>
<div class="panel-body">
<?php endif; ?>
Expand Down
5 changes: 4 additions & 1 deletion modules/template/views/admin/editSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@
<li class="active">
<?= Html::a(Yii::t('CustomPagesModule.base', 'Source'), Url::to(['edit-source', 'id' => $model->id])); ?>
</li>
<li>
<?= Html::a(Yii::t('CustomPagesModule.base', 'Usage'), Url::to(['edit-usage', 'id' => $model->id])); ?>
</li>
<!-- <li>
<?// echo Html::a('<i aria-hidden="true" class="fa fa-question-circle"></i> '.Yii::t('CustomPagesModule.base', 'Help'), Url::to(['info', 'id' => $model->id])); ?>
</li> -->
Expand Down Expand Up @@ -84,4 +87,4 @@

<?= \humhub\modules\custom_pages\modules\template\widgets\TemplateContentTable::widget(['template' => $model]) ?>
</div>
</div>
</div>
94 changes: 94 additions & 0 deletions modules/template/views/admin/editUsage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

use humhub\modules\content\models\Content;
use humhub\modules\custom_pages\helpers\Url;
use humhub\modules\custom_pages\models\CustomContentContainer;
use humhub\modules\custom_pages\modules\template\models\Template;
use humhub\modules\custom_pages\widgets\AdminMenu;
use humhub\widgets\GridView;
use humhub\widgets\Link;
use yii\data\ActiveDataProvider;
use yii\grid\ActionColumn;
use yii\grid\DataColumn;
use yii\helpers\Html;

/* @var Template $model */
/* @var ActiveDataProvider $dataProvider */

$columnLabel = $model->type === Template::TYPE_CONTAINER ? Yii::t('CustomPagesModule.base', 'Template')
: ($model->type === Template::TYPE_SNIPPED_LAYOUT ? Yii::t('CustomPagesModule.base', 'Snippet')
: Yii::t('CustomPagesModule.base', 'Page'));
?>
<div class="panel panel-default">
<div class="panel-heading"><?= Yii::t('CustomPagesModule.base', '<strong>Custom</strong> Pages') ?></div>

<?= AdminMenu::widget() ?>

<div class="panel-body">
<?= Html::a('<i class="fa fa-arrow-left" aria-hidden="true"></i>&nbsp;&nbsp;' . Yii::t('CustomPagesModule.base', 'Back to overview'), Url::to(['index']), ['class' => 'btn btn-default pull-right', 'data-ui-loader' => '']); ?>
<h4><?= Yii::t('CustomPagesModule.modules_template_views_admin_editSource', 'Edit template \'{templateName}\'', ['templateName' => Html::encode($model->name)]); ?></h4>
<div class="help-block">
<?= Yii::t('CustomPagesModule.base', 'Here you can review where the template is used in.') ?>
</div>
</div>

<ul class="nav nav-tabs tab-sub-menu" id="tabs">
<li>
<?= Html::a(Yii::t('CustomPagesModule.base', 'General'), Url::to(['edit', 'id' => $model->id])); ?>
</li>
<li>
<?= Html::a(Yii::t('CustomPagesModule.base', 'Source'), Url::to(['edit-source', 'id' => $model->id])); ?>
</li>
<li class="active">
<?= Html::a(Yii::t('CustomPagesModule.base', 'Usage'), Url::to(['edit-usage', 'id' => $model->id])); ?>
</li>
</ul>

<div class="panel-body">
<?= GridView::widget([
'dataProvider' => $dataProvider,
'layout' => '{items}{pager}',
'columns' => [
[
'class' => DataColumn::class,
'label' => $columnLabel,
'format' => 'raw',
'value' => function ($model) {
if ($model instanceof Content) {
/* @var $record CustomContentContainer */
$record = $model->getPolymorphicRelation();
return Link::to(Html::encode($record->getTitle()), $record->getUrl())->icon(Html::encode($record->icon));
} else if ($model instanceof Template) {
return $model->name;
}
}
],
[
'class' => ActionColumn::class,
'options' => ['width' => '80px'],
'buttons' => [
'update' => function ($url, $model) {
if ($model instanceof Content) {
/* @var $record CustomContentContainer */
$record = $model->getPolymorphicRelation();
return $record->canEdit()
? Link::primary()->icon('fa-pencil')->link($record->getEditUrl())->xs()->right()
: '';
} else if ($model instanceof Template) {
return Link::primary()->icon('fa-pencil')->link(Url::toRoute(['edit-source', 'id' => $model->id]))->xs();
}
},
'view' => function () {
return '';
},
'delete' => function ($url, $model) {
if ($model instanceof Template) {
return Link::danger()->icon('fa-times')->link(Url::toRoute(['delete-template', 'id' => $model->id]))->xs()->confirm();
}
},
],
],
]
]) ?>
</div>
</div>

0 comments on commit 5a05ad3

Please sign in to comment.