[{{mminutes}}:{{sseconds}}] X
Пользователь приглашает вас присоединиться к открытой игре игре с друзьями .
Yii. Отрывки кода из демо-блога
(1)       Используют 7 человек

Комментарии

Ни одного комментария.
Написать тут
Описание:
Yii. Отрывки кода из демо-блога
Автор:
ArtVT
Создан:
21 февраля 2015 в 19:53 (текущая версия от 21 февраля 2015 в 20:40)
Публичный:
Нет
Тип словаря:
Тексты
Цельные тексты, разделяемые пустой строкой (единственный текст на словарь также допускается).
Содержание:
1 class PostController extends Controller
{
public $layout='column2';
private $_model;
public function filters()
{
return array(
'accessControl',
);
}
}
public function accessRules()
{
return array(
array('allow',
'actions'=>array('index','view'),
'users'=>array('*'),
),
array('allow',
'users'=>array('@'),
),
array('deny', // deny all users
'users'=>array('*'),
),
);
}
2 public function actionView()
{
$post=$this->loadModel();
$comment=$this->newComment($post);
$this->render('view',array(
'model'=>$post,
'comment'=>$comment,
));
}
public function actionCreate()
{
$model=new Post;
if(isset($_POST['Post']))
{
$model->attributes=$_POST['Post'];
if($model->save())
$this->redirect(array('view','id'=>$model->id));
}
$this->render('create',array(
'model'=>$model,
));
}
3 public function actionUpdate()
{
$model=$this->loadModel();
if(isset($_POST['Post']))
{
$model->attributes=$_POST['Post'];
if($model->save())
$this->redirect(array('view','id'=>$model->id));
}
$this->render('update',array(
'model'=>$model,
));
}
public function actionDelete()
{
if(Yii::app()->request->isPostRequest)
{
$this->loadModel()->delete();
if(!isset($_GET['ajax']))
$this->redirect(array('index'));
}
else
throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
}
4 public function actionIndex()
{
$criteria=new CDbCriteria(array(
'condition'=>'status='.Post::STATUS_PUBLISHED,
'order'=>'update_time DESC',
'with'=>'commentCount',
));
if(isset($_GET['tag']))
$criteria->addSearchCondition('tags',$_GET['tag']);
$dataProvider=new CActiveDataProvider('Post', array(
'pagination'=>array(
'pageSize'=>Yii::app()->params['postsPerPage'],
),
'criteria'=>$criteria,
));
$this->render('index',array(
'dataProvider'=>$dataProvider,
));
}
public function actionAdmin()
{
$model=new Post('search');
if(isset($_GET['Post']))
$model->attributes=$_GET['Post'];
$this->render('admin',array(
'model'=>$model,
));
}
5 public function actionSuggestTags()
{
if(isset($_GET['q']) && ($keyword=trim($_GET['q']))!=='')
{
$tags=Tag::model()->suggestTags($keyword);
if($tags!==array())
echo implode("
",$tags);
}
}
public function loadModel()
{
if($this->_model===null)
{
if(isset($_GET['id']))
{
if(Yii::app()->user->isGuest)
$condition='status='.Post::STATUS_PUBLISHED.' OR status='.Post::STATUS_ARCHIVED;
else
$condition='';
$this->_model=Post::model()->findByPk($_GET['id'], $condition);
}
if($this->_model===null)
throw new CHttpException(404,'The requested page does not exist.');
}
return $this->_model;
}
6 protected function newComment($post)
{
$comment=new Comment;
if(isset($_POST['ajax']) && $_POST['ajax']==='comment-form')
{
echo CActiveForm::validate($comment);
Yii::app()->end();
}
if(isset($_POST['Comment']))
{
$comment->attributes=$_POST['Comment'];
if($post->addComment($comment))
{
if($comment->status==Comment::STATUS_PENDING)
Yii::app()->user->setFlash('commentSubmitted','Thank you for your comment. Your comment will be posted once it is approved.');
$this->refresh();
}
}
return $comment;
}
7 <?php
class Comment extends CActiveRecord
{
const STATUS_PENDING = 1;
const STATUS_APPROVED = 2;
public static function model($className = __CLASS__)
{
return parent::model($className);
}
public function tableName()
{
return '';
}
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('content, author, email', 'required'),
array('author, email, url', 'length', 'max' => 128),
array('email', 'email'),
array('url', 'url'),
);
}
}
8 public function relations()
{
return array(
'post' => array(self::BELONGS_TO, 'Post', 'post_id'),
);
}
public function attributeLabels()
{
return array(
'id' => 'Id',
'content' => 'Comment',
'status' => 'Status',
'create_time' => 'Create Time',
'author' => 'Name',
'email' => 'Email',
'url' => 'Website',
'post_id' => 'Post',
);
}
9 public function approve()
{
$this->status=Comment::STATUS_APPROVED;
$this->update(array('status'));
}
public function getUrl($post=null)
{
if($post===null)
$post=$this->post;
return $post->url.'#c'.$this->id;
}
public function getAuthorLink()
{
if(!empty($this->url))
return CHtml::link(CHtml::encode($this->author),$this->url);
else
return CHtml::encode($this->author);
}
10 public function getPendingCommentCount()
{
return $this->count('status='.self::STATUS_PENDING);
}
public function findRecentComments($limit=10)
{
return $this->with('post')->findAll(array(
'condition'=>'t.status='.self::STATUS_APPROVED,
'order'=>'t.create_time DESC',
'limit'=>$limit,
));
}
protected function beforeSave()
{
if(parent::beforeSave())
{
if($this->isNewRecord)
$this->create_time=time();
return true;
}
else
return false;
}

Связаться
Выделить
Выделите фрагменты страницы, относящиеся к вашему сообщению
Скрыть сведения
Скрыть всю личную информацию
Отмена