localhost/blog/backend/web/index.php?r=gii
return [ [['title', 'content', 'status'], 'required'], ['title','string','max'=>128], ['status','in', 'range'=>[1,2,3]], ['tags', 'match', 'pattern'=>'/^[\w\s,]+$/', 'message'=>' .'], ['tags', function($attribute,$params){ $this->tags=Tag::array2string(array_unique(Tag::string2array($this->tags))); }], ];
public function getComments() { return $this->hasMany(Comment::className(), ['post_id' => 'id']) ->where('status = '. Comment::STATUS_APPROVED) ->orderBy('create_time DESC'); } public function getCommentCount() { return $this->hasMany(Comment::className(), ['post_id' => 'id'])->where(['status'=>Comment::STATUS_APPROVED])->count(); } public function getAllCommentCount() { return $this->hasMany(Comment::className(), ['post_id' => 'id'])->where(['status'=>Comment::STATUS_APPROVED])->count(); }
public function getUrl() { return Yii::$app->urlManager->createUrl([ 'post/view', 'id'=>$this->id, 'title'=>$this->title]); }
public static function items($type) { if(!isset(self::$_items[$type])) self::loadItems($type); return self::$_items[$type]; } public static function item($type,$code) { if(!isset(self::$_items[$type])) self::loadItems($type); return isset(self::$_items[$type][$code]) ? self::$_items[$type][$code] : false; } private static function loadItems($type) { self::$_items[$type]=[]; $models=self::find()->where(['type'=>$type])->orderBy('position')->all(); foreach ($models as $model) self::$_items[$type][$model->code]=$model->name; }
'access' => [ 'class' => AccessControl::className(), 'rules' => [ [ 'allow' => true, 'roles' => ['@'], ], ], ],
<?= $form->field($model, 'status')->dropDownList(Lookup::items('PostStatus'),['prompt'=>'Select...']) ?>
'timestamp' => [ 'class' => TimestampBehavior::className(), 'attributes' => [ ActiveRecord::EVENT_BEFORE_INSERT => ['create_time', 'update_time'], ActiveRecord::EVENT_BEFORE_UPDATE => ['update_time'], ], ], [ 'class' => BlameableBehavior::className(), 'createdByAttribute' => 'author_id', 'updatedByAttribute' => 'author_id', ],
public function afterSave($insert, $changedAttributes) { parent::afterSave($insert, $changedAttributes); Tag::updateFrequency($this->_oldTags, $this->tags); } public function afterFind() { parent::afterFind(); $this->_oldTags=$this->tags; }
public function rules() { return [ [['content', 'author', 'email'], 'required'], [['author', 'email', 'url'], 'string', 'max' => 128], ['email','email'], [['content'], 'string'], ['url','url'], [['status', 'create_time', 'post_id'], 'integer'], ]; }
public function behaviors(){ return [ 'timestamp' => [ 'class' => TimestampBehavior::className(), 'attributes' => [ ActiveRecord::EVENT_BEFORE_INSERT => ['create_time'], ], ] ]; }
namespace frontend\controllers; /* * */ class PostController extends Controller { /* * */ public function actionView($id) { $this->layout='column1'; $model = $this->findModel($id); //$comment=$this->newComment($model); $comment=new Comment(); if($comment->load($_POST) && $model->addComment($comment)) { if($comment->status==Comment::STATUS_PENDING){ Yii::$app->getSession()->setFlash('warning','Thank you for your comment. Your comment will be posted once it is approved.'); } return $this->refresh(); } return $this->render('view',array( 'model'=>$model, 'comment'=>$comment, )); }
<?php use yii\helpers\Html; use yii\widgets\DetailView; /* @var $this yii\web\View */ /* @var $model common\models\Post */ $this->title = $model->title; $this->params['breadcrumbs'][] = ['label' => 'Posts', 'url' => ['index']]; $this->params['breadcrumbs'][] = $this->title; ?> <div class="post-view"> <?php echo $this->context->renderPartial('_item', array( 'model'=>$model ));?> <div id="comments" class="row-fluid"> <?php if($model->commentCount>=1): ?> <h4> <?php echo $model->commentCount>1 ? $model->commentCount . ' comments' : 'One comment'; ?> </h4> <?php echo $this->context->renderPartial('_comments',array( 'post'=>$model, 'comments'=>$model->comments, )); ?> <?php endif; ?> <?php echo $this->context->renderPartial('/comment/_form',array( 'model'=>$comment, )); ?> </div><!-- comments --> </div>
<?php use \yii\helpers\Html; ?> <div> <h1><?php echo Html::a(Html::encode($model->title), $model->url); ?></h1> <p class="meta">Posted by <?php echo $model->author->username . ' on ' . date('F j, Y',$model->create_time); ?></p> <p class='lead'> <?php echo $model->content; ?> <p> <div> <p> <strong>Tags:</strong> <?php echo $model->tags; ?> </p> <?php echo Html::a('Permalink', $model->url); ?> | <?php echo Html::a("Comments ({$model->commentCount})",$model->url.'#comments'); ?> | Last updated on <?php echo date('F j, Y',$model->update_time); ?> </div> </div>
<?php use \yii\helpers\Html; foreach($comments as $comment): ?> <div class="well" id="c<?php echo $comment->id; ?>"> <div class="row"> <div class="col-md-8"> <h4><?php echo $comment->authorLink; ?> says:</h4> </div> <div class="col-md-4 text-right"> <?php echo Html::a("#{$comment->id}", $comment->getUrl(),[ 'class'=>'cid', 'title'=>'Permalink to this comment!', ]); ?> </div> </div> <hr style="margin:2px 0px;"> <p class='lead'> <?php echo nl2br(Html::encode($comment->content)); ?> </p> <h5> <?php echo date('F j, Y \a\th:i a',$comment->create_time); ?> </h5> </div><!-- comment --> <?php endforeach; ?>
<?php use \yii\helpers\Html; use \yii\widgets\ActiveForm; ?> <div class="panel panel-success"> <div class="panel-heading"> <h3 class="panel-title">Leave a Comment</h3> </div> <div class="panel-body"> <?php $form = ActiveForm::begin(); ?> <?php echo $form->field($model,'author')->textInput(); ?> <?php echo $form->field($model,'email')->textInput(); ?> <?php echo $form->field($model,'url')->textInput(); ?> <?php echo $form->field($model,'content')->textArea(array('rows'=>6, 'cols'=>50)); ?> <div class="form-actions text-center"> <?php echo Html::submitButton('Save',['class' => 'btn btn-success btn-block']); ?> </div> <?php ActiveForm::end(); ?> </div> </div>
<?= GridView::widget([ 'dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'columns' => [ ['class' => 'yii\grid\SerialColumn'], [ 'attribute'=>'id', 'contentOptions'=>['style'=>'width:64px;'], ], 'content:ntext', [ 'attribute'=>'status', 'format'=>'raw', 'value'=>function ($model){ $text=\common\models\Lookup::item('CommentStatus',$model->status); $url=Url::to(["comment/approve","id"=>$model->id]); Url::remember(); return $text=='Pending Approval'?Html::a($text,$url):$text; }, 'contentOptions'=>['style'=>'width:136px;'], ], 'create_time:datetime', 'author', [ 'class' => 'yii\grid\ActionColumn', 'header' => 'Actions', 'contentOptions'=>['style'=>'width:96px;'], ], ], ]); ?>
public static function findRecentComments($limit=10) { return static::find()->where('status='.self::STATUS_APPROVED) ->orderBy('create_time DESC') ->limit($limit) ->with('post')->all(); }
class RecentComments extends Widget { public $comments; public function init() { parent::init(); $this->comments = Comment::findRecentComments(); } public function run() { return $this->render('recent-comments'); } }
<div class="panel panel-default"> <div class="panel-heading"> <h3 class="panel-title">Recent Comments</h3> </div> <div class="panel-body"> <ul class="list-unstyled"> <?php foreach($this->context->comments as $comment): ?> <li><span class="glyphicon glyphicon-pencil" aria-hidden="true"></span> <?php echo $comment->authorLink; ?> on <?php echo Html::a(Html::encode($comment->post->title), $comment->getUrl()); ?> </li> <?php endforeach; ?> </ul> </div> </div>
Source: https://habr.com/ru/post/269085/
All Articles