Egorka
7 years ago
53 changed files with 1683 additions and 854 deletions
@ -0,0 +1,57 @@
|
||||
<?php |
||||
|
||||
namespace common\modules\links; |
||||
|
||||
use common\modules\links\widgets\MenuItemCreatorWidget; |
||||
use core\components\modules\ModuleInterface; |
||||
use yii\helpers\ArrayHelper; |
||||
|
||||
|
||||
/** |
||||
* blog module definition class |
||||
*/ |
||||
class LinksModule extends \yii\base\Module implements ModuleInterface |
||||
{ |
||||
/** |
||||
* @inheritdoc |
||||
*/ |
||||
public $controllerNamespace = 'common\modules\links\controllers'; |
||||
|
||||
/** |
||||
* @inheritdoc |
||||
*/ |
||||
public function init() |
||||
{ |
||||
parent::init(); |
||||
|
||||
// custom initialization code goes here |
||||
} |
||||
|
||||
public function bootstrap($app) |
||||
{ |
||||
// add languages |
||||
$app->getI18n()->translations = ArrayHelper::merge($app->getI18n()->translations, [ |
||||
'link' => [ |
||||
'class' => 'yii\i18n\PhpMessageSource', |
||||
'basePath' => '@common/modules/links/messages', |
||||
], |
||||
'link_public' => [ |
||||
'class' => 'yii\i18n\PhpMessageSource', |
||||
'basePath' => '@common/modules/links/messages', |
||||
], |
||||
]); |
||||
} |
||||
|
||||
public static function getMenuItemCreator($menu_id): array |
||||
{ |
||||
$widgets = []; |
||||
$widgets[] = [ |
||||
'id' => 'links', |
||||
'title' => \Yii::t('link', 'Links'), |
||||
'content' => MenuItemCreatorWidget::widget([ |
||||
'menu_id' => $menu_id, |
||||
]), |
||||
]; |
||||
return $widgets; |
||||
} |
||||
} |
@ -0,0 +1,4 @@
|
||||
<?php |
||||
return [ |
||||
'Links' => 'Ссылки', |
||||
]; |
@ -0,0 +1,26 @@
|
||||
<?php |
||||
/** |
||||
* Created by Error202 |
||||
* Date: 10.07.2018 |
||||
*/ |
||||
|
||||
namespace common\modules\links\widgets; |
||||
|
||||
use core\forms\menu\MenuItemForm; |
||||
use yii\base\Widget; |
||||
|
||||
class MenuItemCreatorWidget extends Widget |
||||
{ |
||||
public $menu_id; |
||||
|
||||
public function run() |
||||
{ |
||||
$form = new MenuItemForm(); |
||||
$form->module = \Yii::t('link', 'Links'); |
||||
$form->menu_id = $this->menu_id; |
||||
|
||||
return $this->render('menu-item/creator', [ |
||||
'model' => $form, |
||||
]); |
||||
} |
||||
} |
@ -0,0 +1,40 @@
|
||||
<?php |
||||
/** |
||||
* Created by Error202 |
||||
* Date: 10.07.2018 |
||||
*/ |
||||
|
||||
/** |
||||
* @var $this \yii\web\View |
||||
* @var $model \core\forms\menu\MenuItemForm |
||||
*/ |
||||
|
||||
use yii\widgets\ActiveForm; |
||||
use yii\helpers\Html; |
||||
?> |
||||
|
||||
<div class="menu_item_widget"> |
||||
|
||||
<?php $form = ActiveForm::begin(); ?> |
||||
|
||||
<?= $form->field($model, 'name')->input('text') ?> |
||||
|
||||
<?= $form->field($model, 'title_attr')->input('text') ?> |
||||
|
||||
<?= $form->field($model, 'module')->hiddenInput()->label(false) ?> |
||||
|
||||
<?= $form->field($model, 'menu_id')->hiddenInput()->label(false) ?> |
||||
|
||||
<?= $form->field($model, 'url')->input('text') ?> |
||||
|
||||
<?= $form->field($model, 'url_params')->hiddenInput()->label(false) ?> |
||||
|
||||
<div class="form-group"> |
||||
<?= Html::submitButton(Yii::t('buttons', 'Add to menu'), [ |
||||
'class' => 'btn btn-info btn-sm pull-right' |
||||
]) ?> |
||||
</div> |
||||
|
||||
<?php ActiveForm::end(); ?> |
||||
|
||||
</div> |
@ -0,0 +1,78 @@
|
||||
<?php |
||||
|
||||
namespace common\modules\pages; |
||||
|
||||
use common\modules\pages\widgets\MenuItemCreatorWidget; |
||||
use core\components\modules\ModuleInterface; |
||||
use yii\helpers\ArrayHelper; |
||||
|
||||
|
||||
/** |
||||
* blog module definition class |
||||
*/ |
||||
class PagesModule extends \yii\base\Module implements ModuleInterface |
||||
{ |
||||
/** |
||||
* @inheritdoc |
||||
*/ |
||||
public $controllerNamespace = 'common\modules\pages\controllers'; |
||||
|
||||
/** |
||||
* @inheritdoc |
||||
*/ |
||||
public function init() |
||||
{ |
||||
parent::init(); |
||||
|
||||
// custom initialization code goes here |
||||
} |
||||
|
||||
public function bootstrap($app) |
||||
{ |
||||
// add search rules |
||||
$app->params['search_rules'][] = "SELECT title, content, CONCAT('/pages/manage/page/view/', id) AS url FROM {{pages}}"; |
||||
|
||||
$app->getUrlManager()->addRules([ |
||||
['class' => 'common\modules\pages\urls\PageMainUrlRule'], |
||||
]); |
||||
|
||||
$app->getUrlManager()->addRules([ |
||||
'pages/manage/page/view/<id:\d+>' => 'pages/manage/page/view', |
||||
]); |
||||
|
||||
// add languages |
||||
$app->getI18n()->translations = ArrayHelper::merge($app->getI18n()->translations, [ |
||||
'page' => [ |
||||
'class' => 'yii\i18n\PhpMessageSource', |
||||
'basePath' => '@common/modules/pages/messages', |
||||
], |
||||
'page_public' => [ |
||||
'class' => 'yii\i18n\PhpMessageSource', |
||||
'basePath' => '@common/modules/pages/messages', |
||||
], |
||||
]); |
||||
|
||||
// add menu items |
||||
if (basename($app->getBasePath()) === 'backend') { |
||||
$app->params['adminMenu'][] = [ |
||||
'label' => \Yii::t( 'page', 'Pages' ), |
||||
'icon' => 'file-o', |
||||
'url' => [ '/pages/manage/page/index' ], |
||||
'visible' => \Yii::$app->user->can( 'admin' ) || \Yii::$app->user->can( 'PagesManagement' ) |
||||
]; |
||||
} |
||||
} |
||||
|
||||
public static function getMenuItemCreator($menu_id): array |
||||
{ |
||||
$widgets = []; |
||||
$widgets[] = [ |
||||
'id' => 'pages', |
||||
'title' => \Yii::t('page', 'Pages'), |
||||
'content' => MenuItemCreatorWidget::widget([ |
||||
'menu_id' => $menu_id, |
||||
]), |
||||
]; |
||||
return $widgets; |
||||
} |
||||
} |
@ -1,6 +1,6 @@
|
||||
<?php |
||||
|
||||
namespace core\entities; |
||||
namespace common\modules\pages\entities; |
||||
|
||||
use paulzi\nestedsets\NestedSetsBehavior; |
||||
use core\behaviors\MetaBehavior; |
@ -1,8 +1,10 @@
|
||||
<?php |
||||
|
||||
namespace core\forms; |
||||
namespace common\modules\pages\forms; |
||||
|
||||
use core\entities\Page; |
||||
use core\forms\CompositeForm; |
||||
use core\forms\MetaForm; |
||||
use common\modules\pages\entities\Page; |
||||
use core\validators\SlugValidator; |
||||
use yii\helpers\ArrayHelper; |
||||
use Yii; |
@ -1,10 +1,10 @@
|
||||
<?php |
||||
|
||||
namespace backend\forms; |
||||
namespace common\modules\pages\forms; |
||||
|
||||
use yii\base\Model; |
||||
use yii\data\ActiveDataProvider; |
||||
use core\entities\Page; |
||||
use common\modules\pages\entities\Page; |
||||
|
||||
class PageSearch extends Model |
||||
{ |
@ -1,8 +1,8 @@
|
||||
<?php |
||||
|
||||
namespace core\repositories\page; |
||||
namespace common\modules\pages\repositories; |
||||
|
||||
use core\entities\Page; |
||||
use common\modules\pages\entities\Page; |
||||
use core\repositories\NotFoundException; |
||||
|
||||
class PageRepository |
@ -1,8 +1,8 @@
|
||||
<?php |
||||
|
||||
namespace core\repositories\page\read; |
||||
namespace common\modules\pages\repositories\read; |
||||
|
||||
use core\entities\Page; |
||||
use common\modules\pages\entities\Page; |
||||
|
||||
class PageReadRepository |
||||
{ |
@ -1,11 +1,11 @@
|
||||
<?php |
||||
|
||||
namespace core\services; |
||||
namespace common\modules\pages\services; |
||||
|
||||
use core\entities\Meta; |
||||
use core\entities\Page; |
||||
use core\forms\PageForm; |
||||
use core\repositories\page\PageRepository; |
||||
use common\modules\pages\entities\Page; |
||||
use common\modules\pages\forms\PageForm; |
||||
use common\modules\pages\repositories\PageRepository; |
||||
|
||||
class PageManageService |
||||
{ |
@ -0,0 +1,89 @@
|
||||
<?php |
||||
|
||||
namespace common\modules\pages\urls; |
||||
|
||||
use common\modules\pages\repositories\read\PageReadRepository; |
||||
use yii\base\BaseObject; |
||||
use yii\caching\Cache; |
||||
use yii\caching\TagDependency; |
||||
use yii\web\UrlNormalizerRedirectException; |
||||
use yii\web\UrlRuleInterface; |
||||
use InvalidArgumentException; |
||||
|
||||
class PageMainUrlRule extends BaseObject implements UrlRuleInterface |
||||
{ |
||||
//public $prefix = 'pages/page'; |
||||
public $prefix = ''; |
||||
|
||||
private $repository; |
||||
private $cache; |
||||
|
||||
public function __construct(PageReadRepository $repository, Cache $cache, $config = []) |
||||
{ |
||||
parent::__construct($config); |
||||
$this->repository = $repository; |
||||
$this->cache = $cache; |
||||
} |
||||
|
||||
public function parseRequest($manager, $request) |
||||
{ |
||||
if (preg_match('#^' . $this->prefix . '(.*[a-z])$#is', $request->pathInfo, $matches)) { |
||||
$path = $matches['1']; |
||||
|
||||
$result = $this->cache->getOrSet( [ 'page_main_route', 'path' => $path ], function () use ( $path ) { |
||||
if ( ! $post = $this->repository->findBySlug($this->getPathSlug( $path ) ) ) { |
||||
return [ 'id' => null, 'path' => null ]; |
||||
} |
||||
return [ 'id' => $post->id, 'path' => $post->slug ]; |
||||
}, null, new TagDependency(['tags' => ['pages']]) ); |
||||
|
||||
if ( empty( $result['id'] ) ) { |
||||
return false; |
||||
} |
||||
|
||||
if ( $path != $result['path'] ) { |
||||
throw new UrlNormalizerRedirectException( [ 'pages/page/view', 'id' => $result['id'] ], 301 ); |
||||
} |
||||
|
||||
return ['pages/page/view', ['id' => $result['id']]]; |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
public function createUrl($manager, $route, $params) |
||||
{ |
||||
if ($route == 'pages/page/view') { |
||||
if (empty($params['id'])) { |
||||
throw new InvalidArgumentException('Empty id.'); |
||||
} |
||||
$id = $params['id']; |
||||
|
||||
$url = $this->cache->getOrSet(['page_main_route', 'id' => $id], function () use ($id) { |
||||
if (!$page = $this->repository->find($id)) { |
||||
return null; |
||||
} |
||||
return $page->slug; |
||||
}, null, new TagDependency(['tags' => ['pages']])); |
||||
|
||||
if (!$url) { |
||||
return '#'; |
||||
//throw new InvalidArgumentException('Undefined id.'); |
||||
} |
||||
|
||||
$url = $this->prefix . '/' . $url; |
||||
unset($params['id']); |
||||
if (!empty($params) && ($query = http_build_query($params)) !== '') { |
||||
$url .= '?' . $query; |
||||
} |
||||
|
||||
return $url; |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
private function getPathSlug($path): string |
||||
{ |
||||
$chunks = explode('/', $path); |
||||
return end($chunks); |
||||
} |
||||
} |
@ -1,7 +1,7 @@
|
||||
<?php |
||||
|
||||
/* @var $this yii\web\View */ |
||||
/* @var $model core\forms\PageForm */ |
||||
/* @var $model \common\modules\pages\forms\PageForm */ |
||||
|
||||
$this->title = Yii::t('page', 'Create Page'); |
||||
$this->params['breadcrumbs'][] = ['label' => Yii::t('page', 'Pages'), 'url' => ['index']]; |
@ -1,12 +1,12 @@
|
||||
<?php |
||||
|
||||
use core\entities\Page; |
||||
use common\modules\pages\entities\Page; |
||||
use yii\grid\ActionColumn; |
||||
use yii\helpers\Html; |
||||
use yii\grid\GridView; |
||||
|
||||
/* @var $this yii\web\View */ |
||||
/* @var $searchModel backend\forms\PageSearch */ |
||||
/* @var $searchModel \common\modules\pages\forms\PageSearch */ |
||||
/* @var $dataProvider yii\data\ActiveDataProvider */ |
||||
|
||||
$this->title = Yii::t('page', 'Pages'); |
@ -1,8 +1,8 @@
|
||||
<?php |
||||
|
||||
/* @var $this yii\web\View */ |
||||
/* @var $page core\entities\Page */ |
||||
/* @var $model core\forms\PageForm */ |
||||
/* @var $page \common\modules\pages\entities\Page */ |
||||
/* @var $model \common\modules\pages\forms\PageForm */ |
||||
|
||||
$this->title = Yii::t('page', 'Update Page: {name}', ['name' => $page->title]); |
||||
$this->params['breadcrumbs'][] = ['label' => Yii::t('page', 'Pages'), 'url' => ['index']]; |
@ -0,0 +1,29 @@
|
||||
<?php |
||||
/** |
||||
* Created by Error202 |
||||
* Date: 10.07.2018 |
||||
*/ |
||||
|
||||
namespace common\modules\pages\widgets; |
||||
|
||||
use core\forms\menu\MenuItemForm; |
||||
use yii\base\Widget; |
||||
|
||||
class MenuItemCreatorWidget extends Widget |
||||
{ |
||||
public $menu_id; |
||||
|
||||
public function run() |
||||
{ |
||||
$form = new MenuItemForm(); |
||||
$form->module = \Yii::t('page', 'Pages'); |
||||
$form->name = \Yii::t('page', 'Pages'); |
||||
$form->title_attr = \Yii::t('page', 'Pages'); |
||||
$form->menu_id = $this->menu_id; |
||||
$form->url = '/pages/page/index'; |
||||
|
||||
return $this->render('menu-item/creator', [ |
||||
'model' => $form, |
||||
]); |
||||
} |
||||
} |
@ -0,0 +1,95 @@
|
||||
<?php |
||||
/** |
||||
* Created by Error202 |
||||
* Date: 10.07.2018 |
||||
*/ |
||||
|
||||
/** |
||||
* @var $this \yii\web\View |
||||
* @var $model \core\forms\menu\MenuItemForm |
||||
*/ |
||||
|
||||
use yii\widgets\ActiveForm; |
||||
use yii\helpers\Html; |
||||
use yii\web\JsExpression; |
||||
use yii\helpers\Url; |
||||
|
||||
$block_name = Yii::t('page', 'Pages'); |
||||
$block_title_attr = Yii::t('page', 'Pages'); |
||||
|
||||
$js = <<<JS |
||||
function updatePagesUrl() { |
||||
var select = $("#page_select"); |
||||
var data = select.select2('data'); |
||||
var selected_post = select.val(); |
||||
$("#page_menu_item_url").val('/pages/page/view/'); |
||||
$("#page_menu_item_url_params").val('{"id":'+selected_post+'}'); |
||||
$("#page_menu_item_name").val(data[0].text); |
||||
$("#page_menu_item_title_attr").val(data[0].text); |
||||
} |
||||
JS; |
||||
$this->registerJs($js, $this::POS_HEAD); |
||||
|
||||
$fetchUrl = Url::to( [ '/pages/manage/page/page-search' ] ); |
||||
?> |
||||
|
||||
<div class="menu_item_widget"> |
||||
|
||||
<div class="form-group"> |
||||
<div> |
||||
<?= \kartik\widgets\Select2::widget([ |
||||
'name' => 'page_select', |
||||
'value' => '', |
||||
'options' => [ |
||||
'placeholder' => Yii::t('page', 'Select page...'), |
||||
'id' => 'page_select', |
||||
'onchange' => new JsExpression("updatePagesUrl()"), |
||||
], |
||||
'pluginOptions' => [ |
||||
'ajax' => [ |
||||
'url' => $fetchUrl, |
||||
'dataType' => 'json', |
||||
'data' => new JsExpression('function(params) { return {q:params.term}; }') |
||||
], |
||||
'escapeMarkup' => new JsExpression('function (markup) { return markup; }'), |
||||
'templateResult' => new JsExpression('function(tag) { return tag.text; }'), |
||||
'templateSelection' => new JsExpression('function (tag) { return tag.text; }'), |
||||
], |
||||
]) ?> |
||||
</div> |
||||
</div> |
||||
|
||||
<?php $form = ActiveForm::begin(); ?> |
||||
|
||||
<?= $form->field($model, 'name')->hiddenInput([ |
||||
'id' => 'page_menu_item_name', |
||||
])->label(false) ?> |
||||
|
||||
<?= $form->field($model, 'title_attr')->hiddenInput([ |
||||
'id' => 'page_menu_item_title_attr', |
||||
])->label(false) ?> |
||||
|
||||
<?= $form->field($model, 'module')->hiddenInput([ |
||||
])->label(false) ?> |
||||
|
||||
<?= $form->field($model, 'menu_id')->hiddenInput([ |
||||
])->label(false) ?> |
||||
|
||||
<?= $form->field($model, 'url')->hiddenInput([ |
||||
'id' => 'page_menu_item_url', |
||||
])->label(false) ?> |
||||
|
||||
<?= $form->field($model, 'url_params')->hiddenInput([ |
||||
'value' => '', |
||||
'id' => 'page_menu_item_url_params', |
||||
])->label(false) ?> |
||||
|
||||
<div class="form-group"> |
||||
<?= Html::submitButton(Yii::t('buttons', 'Add to menu'), [ |
||||
'class' => 'btn btn-info btn-sm pull-right' |
||||
]) ?> |
||||
</div> |
||||
|
||||
<?php ActiveForm::end(); ?> |
||||
|
||||
</div> |
@ -0,0 +1,22 @@
|
||||
<?php |
||||
/** |
||||
* Created by Error202 |
||||
* Date: 19.07.2018 |
||||
*/ |
||||
|
||||
namespace core\widgets\menu; |
||||
|
||||
use core\entities\menu\Menu; |
||||
use yii\base\Widget; |
||||
|
||||
class MenuWidget extends Widget |
||||
{ |
||||
public $menu_id; |
||||
|
||||
public function run() { |
||||
$menu = Menu::findOne($this->menu_id); |
||||
return $this->render('menu', [ |
||||
'menu' => $menu, |
||||
]); |
||||
} |
||||
} |
@ -0,0 +1,30 @@
|
||||
<?php |
||||
/** |
||||
* Created by Error202 |
||||
* Date: 19.07.2018 |
||||
*/ |
||||
|
||||
/** |
||||
* @var $this \yii\web\View |
||||
* @var $menu \core\entities\menu\Menu |
||||
*/ |
||||
|
||||
?> |
||||
|
||||
<ul class="navbar-nav ml-auto"> |
||||
<?php foreach ($menu->items as $item): ?> |
||||
<li class="<?= $item->isActive() ? 'active' : '' ?> nav-item<?= $item->hasChildren() ? ' dropdown' : '' ?>">
|
||||
<?php if ($item->hasChildren() && $item->parent_id == 0): ?> |
||||
<a <?= $item->target ? 'target="'.$item->target.'"' : '' ?> href="<?= $item->getUrl() ?>" class="nav-link dropdown-toggle" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"><?= $item->name ?></a>
|
||||
<div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink"> |
||||
<?php $children = $item->children; ?> |
||||
<?php foreach ($children as $child): ?> |
||||
<a <?= $child->target ? 'target="'.$child->target.'"' : '' ?> class="dropdown-item" href="<?= $child->url ?>"><?= $child->name ?></a>
|
||||
<?php endforeach; ?> |
||||
</div> |
||||
<?php elseif ($item->parent_id == 0): ?> |
||||
<a <?= $item->target ? 'target="'.$item->target.'"' : '' ?> href="<?= $item->getUrl() ?>" class="nav-link"><?= $item->name ?></a>
|
||||
<?php endif; ?> |
||||
</li> |
||||
<?php endforeach; ?> |
||||
</ul> |
@ -0,0 +1,28 @@
|
||||
<?php $this->beginContent('@frontend/web/themes/start/layouts/main.php') ?> |
||||
|
||||
<!-- Page Content --> |
||||
<div class="container"> |
||||
|
||||
<?= \yii\widgets\Breadcrumbs::widget([ |
||||
'tag' => 'ul', |
||||
'itemTemplate' => '<li class="breadcrumb-item">{link}</li>' . "\n", |
||||
'activeItemTemplate' => '<li class="breadcrumb-item active">{link}</li>' . "\n", |
||||
'links' => isset($this->params['breadcrumbs']) ? $this->params['breadcrumbs'] : [], |
||||
]) ?> |
||||
|
||||
<div class="row"> |
||||
|
||||
<!-- Page Content --> |
||||
<div class="col-md-12"> |
||||
|
||||
<?= $content ?> |
||||
|
||||
</div> |
||||
|
||||
</div> |
||||
<!-- /.row --> |
||||
|
||||
</div> |
||||
<!-- /.container --> |
||||
<?php $this->endContent() ?> |
||||
|
@ -0,0 +1,32 @@
|
||||
<?php |
||||
|
||||
use yii\helpers\Html; |
||||
|
||||
/* @var $this yii\web\View */ |
||||
/* @var $page \common\modules\pages\entities\Page */ |
||||
|
||||
$this->title = $page->getSeoTitle(); |
||||
|
||||
$this->registerMetaTag(['name' => 'description', 'content' => $page->meta->description]); |
||||
$this->registerMetaTag(['name' => 'keywords', 'content' => $page->meta->keywords]); |
||||
|
||||
foreach ($page->parents as $parent) { |
||||
if (!$parent->isRoot()) { |
||||
$this->params['breadcrumbs'][] = ['label' => $parent->title, 'url' => ['view', 'id' => $parent->id]]; |
||||
} |
||||
} |
||||
$this->params['breadcrumbs'][] = $page->title; |
||||
?> |
||||
<article class="page-view"> |
||||
|
||||
<h1><?= Html::encode($page->title) ?></h1>
|
||||
|
||||
<?= Yii::$app->formatter->asHtml($page->content, [ |
||||
'Attr.AllowedRel' => array('nofollow'), |
||||
'HTML.SafeObject' => true, |
||||
'Output.FlashCompat' => true, |
||||
'HTML.SafeIframe' => true, |
||||
'URI.SafeIframeRegexp'=>'%^(https?:)?//(www\.youtube(?:-nocookie)?\.com/embed/|player\.vimeo\.com/video/)%', |
||||
]) ?> |
||||
|
||||
</article> |
Loading…
Reference in new issue