Skip to content

Commit

Permalink
Merge branch 'release/2.x' of https://github.com/dmstr/yii2-pages-module
Browse files Browse the repository at this point in the history
 into release/2.x
  • Loading branch information
schmunk42 committed Dec 9, 2018
2 parents 23cac16 + 1b89f3f commit 77e7910
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 7 deletions.
2 changes: 1 addition & 1 deletion assets/less/frontend.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
$(document).on('ready cookieUpdate',function () {

// TODO: check value
if ($.cookie('app-frontend-view-mode')) {
if (typeof $.cookie === 'function' && $.cookie('app-frontend-view-mode')) {
$('.dmstr-pages-invisible-frontend').hide();
} else {
$('.dmstr-pages-invisible-frontend').show();
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"yiisoft/yii2": "^2.0.13",
"kartik-v/yii2-tree-manager": ">=1.0.3 <=1.0.5",
"kartik-v/yii2-widget-select2": "^2.0.1",
"2amigos/yii2-translateable-behavior": "^1.1.0",
"insolita/yii2-adminlte-widgets": "^1.1.4",
"rmrevin/yii2-fontawesome": "~2.9",
"devgroup/yii2-jsoneditor": "1.0.*",
Expand Down
10 changes: 7 additions & 3 deletions models/BaseTree.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use dmstr\db\traits\ActiveRecordAccessTrait;
use dmstr\modules\pages\Module as PagesModule;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\db\Expression;
use yii\helpers\ArrayHelper;
use yii\web\HttpException;
Expand Down Expand Up @@ -377,7 +378,9 @@ public function behaviors()
self::ATTR_PAGE_TITLE,
self::ATTR_DEFAULT_META_KEYWORDS,
self::ATTR_DEFAULT_META_DESCRIPTION,
]
],
'deleteEvent' => ActiveRecord::EVENT_BEFORE_DELETE,
'restrictDeletion' => TranslateableBehavior::DELETE_LAST,
];

$behaviors['translation_meta'] = [
Expand All @@ -389,7 +392,8 @@ public function behaviors()
'translationAttributes' => [
self::ATTR_DISABLED,
self::ATTR_VISIBLE,
]
],
'deleteEvent' => ActiveRecord::EVENT_BEFORE_DELETE,
];

return $behaviors;
Expand Down Expand Up @@ -440,7 +444,7 @@ public function rules()
self::ATTR_ACCESS_DELETE
],
'default',
'value' => null
'value' => static::getDefaultAccessUpdateDelete()
],
[
[self::ATTR_DOMAIN_ID, self::ATTR_ACCESS_DOMAIN],
Expand Down
52 changes: 50 additions & 2 deletions models/Tree.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use dmstr\modules\pages\Module as PagesModule;
use rmrevin\yii\fontawesome\FA;
use Yii;
use yii\caching\TagDependency;
use yii\helpers\ArrayHelper;
use yii\helpers\Inflector;
use yii\helpers\Json;
Expand Down Expand Up @@ -55,6 +56,18 @@ public function afterFind()
$this->setNameId($this->domain_id.'_'.$this->access_domain);
}

public function afterSave($insert, $changedAttributes)
{
parent::afterSave($insert, $changedAttributes);
TagDependency::invalidate(\Yii::$app->cache, 'pages');
}

public function afterDelete()
{
parent::afterDelete();
TagDependency::invalidate(\Yii::$app->cache, 'pages');
}

/**
* Override isDisabled method if you need as shown in the
* example below. You can override similarly other methods
Expand All @@ -67,6 +80,21 @@ public function isDisabled()
return parent::isDisabled();
}

/**
* Disallow node movement when user has no update permissions
*
* @param string $dir
* @return bool
*/
public function isMovable($dir)
{
if (!$this->hasPermission('access_update')) {
return false;
} else {
return parent::isMovable($dir);
}
}

/**
* @return array
*/
Expand Down Expand Up @@ -205,6 +233,15 @@ public function createUrl($additionalParams = [])
*/
public static function getMenuItems($domainId, $checkUserPermissions = false, array $linkOptions = [])
{
$cache = Yii::$app->cache;
$cacheKey = Json::encode([self::class,Yii::$app->language,$domainId,$checkUserPermissions,$linkOptions]);
$data = $cache->get($cacheKey);

if ($data !== false && Yii::$app->user->isGuest) {
return $data;
}

Yii::trace(["Building menu items", $cacheKey], __METHOD__);
// Get root node by domain id
$rootCondition[self::ATTR_DOMAIN_ID] = $domainId;
$rootCondition[self::ATTR_ACCESS_DOMAIN] = [self::GLOBAL_ACCESS_DOMAIN,mb_strtolower(\Yii::$app->language)];
Expand Down Expand Up @@ -263,6 +300,7 @@ public static function getMenuItems($domainId, $checkUserPermissions = false, ar
$linkOptions,
[
'data-page-id' => $page->id,
'data-domain-id' => $page->domain_id,
'data-lvl' => $page->lvl,
'class' => $page->isDisabled() ? 'dmstr-pages-invisible-frontend' : ''
]
Expand All @@ -286,6 +324,9 @@ public static function getMenuItems($domainId, $checkUserPermissions = false, ar
'url' => $page->createRoute() ? $page->createRoute() : null,
'icon' => $page->icon,
'linkOptions' => $linkOptions,
'dropDownOptions' => [
'data-parent-domain-id' => $page->domain_id,
],
'visible' => $visible,
];
$item = $itemTemplate;
Expand Down Expand Up @@ -317,12 +358,19 @@ public static function getMenuItems($domainId, $checkUserPermissions = false, ar
}
}

return array_filter($treeMap);
$data = array_filter($treeMap);

if (Yii::$app->user->isGuest) {
$cacheDependency = new TagDependency(['tags' => 'pages']);
$cache->set($cacheKey, $data, 3600, $cacheDependency);
}

return $data;
}

public function getMenuLabel()
{
return !empty($this->name) ? $this->name : "({$this->domain_id})";
return !empty($this->name) ? htmlentities($this->name) : "({$this->domain_id})";
}

/**
Expand Down
13 changes: 13 additions & 0 deletions models/TreeTranslation.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace dmstr\modules\pages\models;

use yii\caching\TagDependency;
use yii\db\ActiveRecord;
use bedezign\yii2\audit\AuditTrailBehavior;
use yii\behaviors\TimestampBehavior;
Expand All @@ -22,6 +23,18 @@
*/
class TreeTranslation extends ActiveRecord
{
public function afterSave($insert, $changedAttributes)
{
parent::afterSave($insert, $changedAttributes);
TagDependency::invalidate(\Yii::$app->cache, 'pages');
}

public function afterDelete()
{
parent::afterDelete();
TagDependency::invalidate(\Yii::$app->cache, 'pages');
}

/**
* @inheritdoc
*
Expand Down
14 changes: 14 additions & 0 deletions models/TreeTranslationMeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace dmstr\modules\pages\models;

use yii\caching\TagDependency;
use yii\db\ActiveRecord;
use yii\behaviors\TimestampBehavior;
use yii\db\Expression;
Expand All @@ -21,6 +22,19 @@
*/
class TreeTranslationMeta extends ActiveRecord
{

public function afterSave($insert, $changedAttributes)
{
parent::afterSave($insert, $changedAttributes);
TagDependency::invalidate(\Yii::$app->cache, 'pages');
}

public function afterDelete()
{
parent::afterDelete();
TagDependency::invalidate(\Yii::$app->cache, 'pages');
}

/**
* @inheritdoc
*
Expand Down
2 changes: 1 addition & 1 deletion views/treeview/_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@
</div>
<?php else: ?>
<?= Html::a(
'<span class="glyphicon glyphicon-trash"></span> ' . \Yii::t('pages', 'Delete Translation'),
'<span class="glyphicon glyphicon-remove"></span> ' . \Yii::t('pages', 'Delete Translation'),
['/pages/crud/tree-translation/delete', 'id' => $node->getTranslation()->id],
[
'class' => 'btn btn-default pull-right',
Expand Down

0 comments on commit 77e7910

Please sign in to comment.