Skip to content

Commit

Permalink
book模块,鉴于很多功能并不是必需,模块有利于拆除,按需分配
Browse files Browse the repository at this point in the history
  • Loading branch information
yidashi committed Feb 16, 2017
1 parent 7ca7511 commit 9878431
Show file tree
Hide file tree
Showing 28 changed files with 296 additions and 96 deletions.
37 changes: 37 additions & 0 deletions backend/widgets/ActiveField.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
/**
* Created by PhpStorm.
* User: yidashi
* Date: 2017/2/11
* Time: 下午9:43
*/

namespace backend\widgets;


use yii\helpers\ArrayHelper;
use yii\helpers\Html;

class ActiveField extends \yii\widgets\ActiveField
{
public function staticControl($options = [])
{
$this->adjustLabelFor($options);
$this->parts['{input}'] = Html::activeStaticControl($this->model, $this->attribute, $options);
return $this;
}

public function suffix($suffix = '', $suffixType = 'addon', $size = 300)
{
$size = !empty($size) ? "input-group-{$size} " : '';
$this->template = "{label}\n<div class=\"input-group $size\">{input}\n<div class=\"input-group-" . $suffixType . "\">" . $suffix . "</div></div>\n{hint}\n{error}";
return $this;
}

public function prefix($prefix = '', $prefixType = 'addon', $size = 300)
{
$size = !empty($size) ? "input-group-{$size} " : '';
$this->template = "{label}\n<div class=\"input-group $size\"><div class=\"input-group-" . $prefixType . "\">" . $prefix . "</div>\n{input}</div>\n{hint}\n{error}";
return $this;
}
}
63 changes: 14 additions & 49 deletions backend/widgets/ActiveForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
namespace backend\widgets;

use yii\helpers\ArrayHelper;
use yii\base\Model;

class ActiveForm extends \yii\widgets\ActiveForm
{
public $fieldClass = 'backend\widgets\ActiveField';
public $boxFieldClass = '\backend\widgets\BoxField';

/**
Expand All @@ -38,56 +40,19 @@ public function boxField($model, $attribute, $options = [])
}

/**
* 有后缀的
* @param $model
* @param $attribute
* @param string $suffix
* @param string $suffixType
* @param array $options
* @return object
* Generates a form field.
* A form field is associated with a model and an attribute. It contains a label, an input and an error message
* and use them to interact with end users to collect their inputs for the attribute.
* @param Model $model the data model.
* @param string $attribute the attribute name or expression. See [[Html::getAttributeName()]] for the format
* about attribute expression.
* @param array $options the additional configurations for the field object. These are properties of [[ActiveField]]
* or a subclass, depending on the value of [[fieldClass]].
* @return ActiveField the created ActiveField object.
* @see fieldConfig
*/
public function suffixField($model, $attribute, $suffix = '', $suffixType = 'addon', $options = [])
public function field($model, $attribute, $options = [])
{
$config = $this->fieldConfig;
if ($config instanceof \Closure) {
$config = call_user_func($config, $model, $attribute);
}
if (!isset($config['class'])) {
$config['class'] = $this->fieldClass;
}
$defaultOptions = ['template' => "{label}\n<div class=\"input-group\">{input}\n<div class=\"input-group-" . $suffixType . "\">" . $suffix . "</div></div>\n{hint}\n{error}"];
$options = array_merge($defaultOptions, $options);
return \Yii::createObject(ArrayHelper::merge($config, $options, [
'model' => $model,
'attribute' => $attribute,
'form' => $this,
]));
}

/**
* 有前缀的
* @param $model
* @param $attribute
* @param string $prefix
* @param string $prefixType
* @param array $options
* @return object
*/
public function prefixField($model, $attribute, $prefix = '', $prefixType = 'addon', $options = [])
{
$config = $this->fieldConfig;
if ($config instanceof \Closure) {
$config = call_user_func($config, $model, $attribute);
}
if (!isset($config['class'])) {
$config['class'] = $this->fieldClass;
}
$defaultOptions = ['template' => "{label}\n<div class=\"input-group\"><div class=\"input-group-" . $prefixType . "\">" . $prefix . "</div>\n{input}</div>\n{hint}\n{error}"];
$options = array_merge($defaultOptions, $options);
return \Yii::createObject(ArrayHelper::merge($config, $options, [
'model' => $model,
'attribute' => $attribute,
'form' => $this,
]));
return parent::field($model, $attribute, $options);
}
}
3 changes: 1 addition & 2 deletions backend/widgets/BoxField.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
namespace backend\widgets;

use yii\helpers\Html;
use yii\widgets\ActiveField;

class BoxField extends ActiveField
class BoxField extends \yii\widgets\ActiveField
{

public $collapsed = false;
Expand Down
24 changes: 24 additions & 0 deletions common/helpers/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,30 @@ public static function icon($name)
return self::tag('i', '', $options);
}

public static function staticControl($value, $options = [])
{
static::addCssClass($options, 'form-control-static');
$value = (string) $value;
if (isset($options['encode'])) {
$encode = $options['encode'];
unset($options['encode']);
} else {
$encode = true;
}
return static::tag('p', $encode ? static::encode($value) : $value, $options);
}

public static function activeStaticControl($model, $attribute, $options = [])
{
if (isset($options['value'])) {
$value = $options['value'];
unset($options['value']);
} else {
$value = static::getAttributeValue($model, $attribute);
}
return static::staticControl($value, $options);
}

/**
* 标红字符串中含有的关键词
* @param $q string 关键词
Expand Down
31 changes: 31 additions & 0 deletions common/modules/book/Module.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

/**
* Created by PhpStorm.
* User: yidashi
* Date: 2017/2/16
* Time: 下午9:32
*/

namespace common\modules\book;

use yii\base\BootstrapInterface;

class Module extends \yii\base\Module implements BootstrapInterface
{
public function bootstrap($app)
{
if ($app->id == 'app-backend') {
$app->urlManager->addRules([
'book/<action:\S+>' => 'book/admin/<action>',
], false);
} else if ($app->id == 'app-frontend') {
$app->urlManager->addRules([
'books' => '/book/default/index',
'book/<id:\d+>' => '/book/default/view',
'book/chapter/<id:\d>' => '/book/default/chapter',
'book/<action:\S+>' => 'book/default/<action>',
], false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,17 @@
* Time: 下午2:05
*/

namespace backend\controllers;
namespace common\modules\book\controllers;


use backend\actions\Position;
use common\models\Book;
use common\models\BookChapter;
use yii\base\InvalidParamException;
use common\modules\book\models\Book;
use common\modules\book\models\BookChapter;
use Yii;
use yii\data\ActiveDataProvider;
use yii\helpers\Url;
use yii\web\Controller;
use Yii;

class BookController extends Controller
class AdminController extends Controller
{

public function actions()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@
* Time: 下午2:05
*/

namespace frontend\controllers;
namespace common\modules\book\controllers;


use common\models\Book;
use common\models\BookChapter;
use common\modules\book\models\Book;
use common\modules\book\models\BookChapter;
use yii\data\ActiveDataProvider;
use yii\web\Controller;
use Yii;
use yii\web\NotFoundHttpException;

class BookController extends Controller
class DefaultController extends Controller
{

public function actionIndex()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php

namespace common\models;
namespace common\modules\book\models;

use common\helpers\Tree;
use common\models\Comment;
use Yii;
use yii\behaviors\TimestampBehavior;
use yii\helpers\Url;

/**
* This is the model class for table "{{%book}}".
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace common\models;
namespace common\modules\book\models;

use common\behaviors\PositionBehavior;
use Yii;
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
* Time: 下午5:50
*/

use backend\widgets\ActiveForm;
use yii\helpers\Html;
use yii\helpers\HtmlPurifier;
use yii\helpers\Markdown;
use backend\widgets\ActiveForm;

?>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@

/**
* @var \yii\web\View $this
* @var common\models\BookChapter $model
* @var common\modules\book\models\BookChapter $model
*/

use yii\helpers\Url;
?>
<style>
.btn-operate-chapter {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@
*/
/**
* @var \yii\web\View $this
* @var \common\models\BookChapter $model
* @var \common\modules\book\models\BookChapter $model
*/

use yii\helpers\Html;
use yii\helpers\HtmlPurifier;
use yii\helpers\Markdown;

$this->title = $model->chapter_name;
$this->params['breadcrumbs'][] = ['label' => '书', 'url' => ['/book/index']];
$this->params['breadcrumbs'][] = ['label' => $model->book->book_name, 'url' => ['/book/view', 'id' => $model->book->id]];
$this->params['breadcrumbs'][] = ['label' => '书', 'url' => ['index']];
$this->params['breadcrumbs'][] = ['label' => $model->book->book_name, 'url' => ['view', 'id' => $model->book->id]];
$this->params['breadcrumbs'][] = Html::encode($model->chapter_name);
?>
<?php $this->beginContent('@backend/views/book/_layout.php', ['book' => $model->book]) ?>
<?php $this->beginContent('@common/modules/book/views/admin/_layout.php', ['book' => $model->book]) ?>
<div class="view-title">
<h1><?= Html::encode($model->chapter_name) ?></h1>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/
/**
* @var \yii\web\View $this
* @var \common\models\BookChapter $model
* @var \common\modules\book\models\BookChapter $model
*/

use yii\helpers\Html;
Expand All @@ -16,10 +16,10 @@
use backend\widgets\ActiveForm;

$this->title = $model->book->book_name . ':新增章节';
$this->params['breadcrumbs'][] = ['label' => '书', 'url' => ['/book/index']];
$this->params['breadcrumbs'][] = ['label' => $model->book->book_name, 'url' => ['/book/view', 'id' => $model->book->id]];
$this->params['breadcrumbs'][] = ['label' => '书', 'url' => ['index']];
$this->params['breadcrumbs'][] = ['label' => $model->book->book_name, 'url' => ['view', 'id' => $model->book->id]];
$this->params['breadcrumbs'][] = Html::encode($model->chapter_name);
?>
<?php $this->beginContent('@backend/views/book/_layout.php', ['model' => $model]) ?>
<?php $this->beginContent('@common/modules/book/views/admin/_layout.php', ['model' => $model]) ?>
<?= $this->render('_form_chapter', ['model' => $model]) ?>
<?php $this->endContent() ?>
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/
/**
* @var \yii\web\View $this
* @var \common\models\Book $model
* @var \common\modules\book\models\Book $model
*/

use yii\helpers\Html;
Expand All @@ -16,7 +16,7 @@
use backend\widgets\ActiveForm;

$this->title = '新建书';
$this->params['breadcrumbs'][] = ['label' => '书', 'url' => ['/book/index']];
$this->params['breadcrumbs'][] = ['label' => '书', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<?= $this->render('_form', ['model' => $model]) ?>
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/
/**
* @var \yii\web\View $this
* @var \common\models\BookChapter $model
* @var \common\modules\book\models\BookChapter $model
*/

use yii\helpers\Html;
Expand All @@ -16,10 +16,10 @@
use backend\widgets\ActiveForm;

$this->title = $model->chapter_name;
$this->params['breadcrumbs'][] = ['label' => '书', 'url' => ['/book/index']];
$this->params['breadcrumbs'][] = ['label' => $model->book->book_name, 'url' => ['/book/view', 'id' => $model->book->id]];
$this->params['breadcrumbs'][] = ['label' => '书', 'url' => ['index']];
$this->params['breadcrumbs'][] = ['label' => $model->book->book_name, 'url' => ['view', 'id' => $model->book->id]];
$this->params['breadcrumbs'][] = Html::encode($model->chapter_name);
?>
<?php $this->beginContent('@backend/views/book/_layout.php', ['model' => $model]) ?>
<?php $this->beginContent('@common/modules/book/views/admin/_layout.php', ['model' => $model]) ?>
<?= $this->render('_form_chapter', ['model' => $model]) ?>
<?php $this->endContent() ?>
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/
/**
* @var \yii\web\View $this
* @var \common\models\Book $model
* @var \common\modules\book\models\Book $model
*/

use yii\helpers\Html;
Expand All @@ -16,7 +16,7 @@
use backend\widgets\ActiveForm;

$this->title = '编辑书:' . $model->book_name;
$this->params['breadcrumbs'][] = ['label' => '书', 'url' => ['/book/index']];
$this->params['breadcrumbs'][] = ['label' => '书', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<?= $this->render('_form', ['model' => $model]) ?>

0 comments on commit 9878431

Please sign in to comment.