/** * @return array */ public function getInternalLinks() { $result = []; foreach (Yii::$app->getModule('content')->functionalPages as $link => $name) { $result[ContentModule::t('app', 'Functional pages')][$link] = $name; } $existingTextPages = ContentPage::find()->andWhere(['type' => static::TYPE_TEXT])->asArray()->all(); foreach ($existingTextPages as $existingTextPage) { $result[ContentModule::t('app', 'Existing text pages')][$existingTextPage['id']] = $existingTextPage['name']; } return $result; }
/** * When $menuId is null - show pages without menu * * @param int|null $menuId * * @return string * @throws \yii\web\NotFoundHttpException */ public function actionTree($menuId = null) { $hasSubmenu = 0; $menuName = ContentMenu::withoutMenuName(); if ($menuId) { $contentMenu = ContentMenu::find()->andWhere(['id' => $menuId, 'active' => 1])->one(); if (!$contentMenu) { throw new NotFoundHttpException(ContentModule::t('app', 'Menu not found')); } $hasSubmenu = $contentMenu->has_submenu; $menuName = $contentMenu->name; } $pages = ContentPage::find()->where(['content_page.content_menu_id' => $menuId ? $menuId : null])->orderBy('content_page.sorter ASC')->all(); return $this->render('tree', compact('pages', 'menuId', 'hasSubmenu', 'menuName')); }
public function search($params) { $query = ContentPage::find(); if (!Yii::$app->request->get('sort')) { $query->orderBy('content_page.sorter'); } $query->joinWith(['contentTemplate', 'contentMenu']); $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['pageSize' => Yii::$app->request->cookies->getValue('_grid_page_size', 20)], 'sort' => ['defaultOrder' => ['id' => SORT_DESC]]]); if (!($this->load($params) && $this->validate())) { return $dataProvider; } if ($this->created_at) { $tmp = explode(' - ', $this->created_at); if (isset($tmp[0], $tmp[1])) { $query->andFilterWhere(['between', 'content_page.created_at', strtotime($tmp[0]), strtotime($tmp[1])]); } } $query->andFilterWhere(['content_page.id' => $this->id, 'content_page.active' => $this->active, 'content_page.sorter' => $this->sorter, 'content_page.is_main' => $this->is_main, 'content_page.type' => $this->type, 'content_page.parent_id' => $this->parent_id, 'content_page.content_template_id' => $this->content_template_id, 'content_page.content_menu_id' => $this->content_menu_id]); $query->andFilterWhere(['like', 'content_page.name', $this->name])->andFilterWhere(['like', 'content_page.slug', $this->slug])->andFilterWhere(['like', 'content_page.meta_title', $this->meta_title])->andFilterWhere(['like', 'content_page.meta_keywords', $this->meta_keywords])->andFilterWhere(['like', 'content_page.meta_description', $this->meta_description]); return $dataProvider; }
/** * @param string $slug * * @throws \yii\web\NotFoundHttpException * @return string */ public function actionView($slug) { $contentPage = ContentPage::getDb()->cache(function () use($slug) { return ContentPage::find()->andWhere(['active' => 1, 'slug' => $slug])->one(); }, ContentModule::CACHE_TIME, new TagDependency(['tags' => ContentModule::CACHE_TAG])); if (!$contentPage) { throw new NotFoundHttpException(Yii::t('yii', 'Page not found.')); } // content_template_id is set in ContentUrlRule $templateId = Singleton::getData('content_template_id'); if ($templateId && $this->module->enableTemplates) { $template = ContentTemplate::getDb()->cache(function () use($templateId) { return ContentTemplate::find()->andWhere(['id' => $templateId, 'active' => 1])->one(); }, ContentModule::CACHE_TIME, new TagDependency(['tags' => ContentModule::CACHE_TAG])); if ($template && is_file(Yii::getAlias('@app/templates/') . $template->layout . '/layout.php')) { $this->layout = '@app/templates/' . $template->layout . '/layout.php'; } } else { $this->layout = $this->module->defaultLayout; } $breadcrumbs[] = ['label' => $contentPage->name]; // $this->prepareBreadcrumbs($contentPage, $breadcrumbs); return $this->renderIsAjax('view', compact('contentPage', 'breadcrumbs')); }
/** * Prepare array of all pages indexed by slug * * @return array */ protected function getAllPages() { if ($this->_pagesBySlug === null) { $this->_pagesBySlug = ContentPage::getDb()->cache(function () { return ContentPage::find()->select(['id', 'slug', 'is_main', 'parent_id', 'type', 'content_template_id'])->asArray()->andWhere(['active' => 1, 'type' => [ContentPage::TYPE_TEXT, ContentPage::TYPE_INTERNAL_LINK]])->indexBy('slug')->all(); }, ContentModule::CACHE_TIME, new TagDependency(['tags' => ContentModule::CACHE_TAG])); } return $this->_pagesBySlug; }
echo ButtonDropdown::widget(['label' => '<span class="glyphicon glyphicon-plus-sign"></span> ' . ContentModule::t('app', 'Create'), 'options' => ['class' => 'btn btn-success btn-sm'], 'encodeLabel' => false, 'dropdown' => ['items' => [['label' => ContentModule::t('app', 'Text page'), 'url' => Url::to(['create', 'type' => ContentPage::TYPE_TEXT, 'menuId' => $menuId])], ['label' => ContentModule::t('app', 'Internal link'), 'url' => Url::to(['create', 'type' => ContentPage::TYPE_INTERNAL_LINK, 'menuId' => $menuId])], ['label' => ContentModule::t('app', 'External link'), 'url' => Url::to(['create', 'type' => ContentPage::TYPE_EXTERNAL_LINK, 'menuId' => $menuId])]]]]); ?> <?php echo JQTreeWidget::widget(['models' => $pages, 'modelName' => 'webvimark\\ybc\\content\\models\\ContentPage', 'parentIdField' => 'parent_id', 'statusField' => 'active', 'orderField' => 'sorter', 'showExpandAndCollapse' => false, 'withChildren' => $hasSubmenu === 1, 'leafName' => function (ContentPage $model) { $pageName = $model->is_main == 1 ? '<span style="color:green; font-style: italic;">' . $model->name . '</span>' : $model->name; if ($model->type == ContentPage::TYPE_TEXT) { $pageType = ' <span class="page-tree-type">' . ContentModule::t('app', 'Text page') . '</span>'; if ($model->is_main == 1) { $viewUrl = Yii::$app->homeUrl; } else { $viewUrl = ['/content/default/view', 'slug' => $model->slug]; } } elseif ($model->type == ContentPage::TYPE_INTERNAL_LINK) { $pageType = ' <span class="page-tree-type">' . ContentModule::t('app', 'Internal link') . '</span>'; if (is_numeric($model->slug)) { $page = ContentPage::find()->andWhere(['id' => $model->slug])->one(); $viewUrl = $page ? ['/content/default/view', 'slug' => $page->slug] : Yii::$app->homeUrl; } else { $viewUrl = Yii::$app->homeUrl . $model->slug; } } else { $pageType = ' <span class="page-tree-type">' . ContentModule::t('app', 'External link') . '</span>'; $viewUrl = $model->slug; } $editLink = Html::a($pageName . $pageType, ['/content/content-page/update', 'id' => $model->id]); $viewLink = Html::a('<i class="fa fa-eye"></i>', $viewUrl, ['target' => '_blank']); return $editLink . ' ' . $viewLink; }]); ?> </div>
?> </p> </div> <div class="col-xs-6 text-right"> <?php echo GridPageSize::widget(['pjaxId' => 'content-page-grid-pjax']); ?> </div> </div> <?php Pjax::begin(['id' => 'content-page-grid-pjax']); ?> <?php echo GridView::widget(['id' => 'content-page-grid', 'dataProvider' => $dataProvider, 'pager' => ['options' => ['class' => 'pagination pagination-sm'], 'hideOnSinglePage' => true, 'lastPageLabel' => '>>', 'firstPageLabel' => '<<'], 'layout' => '{items}<div class="row"><div class="col-sm-8">{pager}</div><div class="col-sm-4 text-right">{summary}' . GridBulkActions::widget(['gridId' => 'content-page-grid']) . '</div></div>', 'filterModel' => $searchModel, 'columns' => [['class' => 'yii\\grid\\SerialColumn', 'options' => ['style' => 'width:10px']], ['attribute' => 'name', 'value' => function ($model) { return Html::a($model->name, ['update', 'id' => $model->id], ['data-pjax' => 0]); }, 'format' => 'raw'], ['class' => 'webvimark\\components\\StatusColumn', 'attribute' => 'is_main', 'toggleUrl' => Url::to(['toggle-attribute', 'attribute' => 'is_main', 'id' => '_id_'])], 'type', 'slug', ['value' => function (ContentPage $model) { return Html::img($model->getImageUrl('small', 'menu_image')); }, 'contentOptions' => ['width' => '10px'], 'format' => 'raw'], ['attribute' => 'parent_id', 'filter' => ArrayHelper::map(ContentPage::find()->asArray()->all(), 'id', 'name'), 'value' => 'parent.name'], ['attribute' => 'content_template_id', 'filter' => ArrayHelper::map(ContentTemplate::find()->asArray()->all(), 'id', 'name'), 'value' => 'contentTemplate.name'], ['attribute' => 'content_menu_id', 'filter' => ArrayHelper::map(ContentMenu::find()->asArray()->all(), 'id', 'name'), 'value' => 'contentMenu.name'], ['class' => 'webvimark\\components\\StatusColumn', 'attribute' => 'active', 'toggleUrl' => Url::to(['toggle-attribute', 'attribute' => 'active', 'id' => '_id_'])], ['class' => 'webvimark\\components\\SorterColumn'], ['class' => 'yii\\grid\\CheckboxColumn', 'options' => ['style' => 'width:10px']], ['class' => 'yii\\grid\\ActionColumn', 'contentOptions' => ['style' => 'width:70px; text-align:center;']]]]); ?> <?php Pjax::end(); ?> </div> </div> </div>
/** * Helper for "getChildrenForMenu" * * @param array $output * @param ContentPage|array $page */ protected static function makeUrlForMenu(&$output, $page) { if ($page->is_main == 1) { $output[$page->id]['url'] = Yii::$app->homeUrl; } elseif ($page->type == ContentPage::TYPE_TEXT) { $output[$page->id]['url'] = ['/content/default/view', 'slug' => $page->slug]; } elseif ($page->type == ContentPage::TYPE_INTERNAL_LINK) { // Slug contains linked ContentPage ID if (is_numeric($page->slug)) { $linkedPage = ContentPage::find()->andWhere(['id' => $page->slug])->one(); if ($linkedPage) { $output[$page->id]['url'] = ['/content/default/view', 'slug' => $linkedPage->slug]; } } else { $output[$page->id]['url'] = ['/' . ltrim($page->slug, '/')]; } } else { $output[$page->id]['url'] = $page->slug; } }