Skip to content

Commit

Permalink
Merge pull request #109 from danitoro97/foroPublico
Browse files Browse the repository at this point in the history
Foro publico
  • Loading branch information
danitoro97 authored May 22, 2018
2 parents b3c337a + 40ab4d3 commit 00a0ba7
Show file tree
Hide file tree
Showing 12 changed files with 269 additions and 113 deletions.
76 changes: 63 additions & 13 deletions controllers/PostsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ public function behaviors()
],
'access' => [
'class' => AccessControl::className(),
'only' => ['index', 'create', 'view'],
'only' => ['index', 'create', 'view', 'create-publico'],
'rules' => [
[
'allow' => true,
'actions' => ['index', 'create', 'view'],
'actions' => ['index', 'create', 'view', 'create-publico'],
'roles' => ['@'],
],
],
Expand All @@ -45,7 +45,7 @@ public function behaviors()
}

/**
* Lists all Posts models.
* Lista de todos los posts de un equipo concreto.
* @return mixed
* @param mixed $id Representa el identificador del equipo
*/
Expand All @@ -60,6 +60,19 @@ public function actionIndex($id)
'equipo' => $this->findEquipo($id),
]);
}
/**
* Lista todos los posts publico.
* @return [type] [description]
*/
public function actionPublico()
{
return $this->render('publico', [
'model' => Posts::find()
->where('equipo_usuario_id is null')
->orderBy('created_at DESC')
->all(),
]);
}

/**
* Displays a single Posts model.
Expand All @@ -69,12 +82,14 @@ public function actionIndex($id)
*/
public function actionView($id)
{
/*if (!$this->isParticipante($id)) {
$model = $this->findModel($id);
if (!$this->isParticipante($model->equipo_usuario_id)) {
Yii::$app->session->setFlash('error', 'This is the message');
return $this->goBack();
}*/
}

return $this->render('view', [
'model' => $this->findModel($id),
'model' => $model,
//'equipo' => $this->findEquipo($id),
]);
}
Expand All @@ -94,20 +109,55 @@ public function actionCreate($id)
$model = new Posts();
$model->creador_id = Yii::$app->user->identity->id;
$model->equipo_usuario_id = $id;
$model->scenario = Posts::ESCENARIO_EQUIPO;
return $this->create($model, ['index', 'id' => $id]);
}

/**
* Crea un post publico.
* @return [type] [description]
*/
public function actionCreatePublico()
{
$model = new Posts();
$model->creador_id = Yii::$app->user->identity->id;
return $this->create($model, ['publico']);
}

/**
* Ves un post publico.
* @param [type] $id [description]
* @return [type] [description]
*/
public function actionViewPublico($id)
{
return $this->render('view-publico', [
'model' => $this->findModel($id),
//'equipo' => $this->findEquipo($id),
]);
}

/**
* Crea un post.
* @param [type] $model [description]
* @param [type] $ruta [description]
* @return [type] [description]
*/
public function create($model, $ruta)
{
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
$model->upload();
$model->save();
return $this->redirect(['index', 'id' => $id]);
return $this->redirect($ruta);
}
$imagenes = array_merge(Plantilla::find()->all(), PlantillaUsuario::find()
->where(['usuario_id' => Yii::$app->user->identity->id])
->all());
->where(['usuario_id' => Yii::$app->user->identity->id])
->all());

return $this->render('create', [
'model' => $model,
'equipo' => $this->findEquipo($id),
'imagenes' => $imagenes,
]);
'model' => $model,
'imagenes' => $imagenes,
]);
}

/**
Expand Down
22 changes: 20 additions & 2 deletions controllers/RespuestasController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ public function behaviors()
return [
'access' => [
'class' => AccessControl::className(),
'only' => ['create-padre', 'crear'],
'only' => ['create-padre', 'create', 'create-publico', 'create-padre-foro'],
'rules' => [
[
'allow' => true,
'actions' => ['create-padre', 'crear'],
'actions' => ['create-padre', 'create', 'create-publico', 'create-padre-foro'],
'roles' => ['@'],
],
],
Expand All @@ -47,6 +47,24 @@ public function actionCreate()
return $this->crear(Respuestas::ESCENARIO_EQUIPO);
}

/**
* Crea el comentario de un post publico.
* @return [type] [description]
*/
public function actionCreatePublico()
{
return $this->crear(Respuestas::ESCENARIO_FORO);
}

/**
* Crea el comentario de un comentario de un post publico.
* @return [type] [description]
*/
public function actionCreatePadreForo()
{
return $this->crear(Respuestas::ESCENARIO_PADRE);
}

/**
* Añade un comentario al post con un determinado escenario.
* @param [type] $escenario [description]
Expand Down
18 changes: 17 additions & 1 deletion models/Respuestas.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ class Respuestas extends \yii\db\ActiveRecord

public const ESCENARIO_EQUIPO_PADRE = 'padre';

public const ESCENARIO_FORO = 'foro';

public const ESCENARIO_PADRE = 'padre foro';

/**
* {@inheritdoc}
*/
Expand All @@ -43,7 +47,7 @@ public function rules()
{
return [
[['comentario', 'usuario_id', 'post_id'], 'required'],
[['padre_id'], 'required', 'on' => self::ESCENARIO_EQUIPO_PADRE],
[['padre_id'], 'required', 'on' => [self::ESCENARIO_EQUIPO_PADRE, self::ESCENARIO_PADRE]],
[['comentario'], 'string'],
[['usuario_id', 'post_id', 'padre_id'], 'default', 'value' => null],
[['usuario_id', 'post_id', 'padre_id'], 'integer'],
Expand All @@ -59,6 +63,18 @@ public function rules()
$this->addError('usuario_id', ' Usuario no valido');
}
}, 'on' => [self::ESCENARIO_EQUIPO, self::ESCENARIO_EQUIPO_PADRE]],
[['post_id'], function ($attributes) {
$post = Posts::findOne($this->post_id);
if ($post->equipo_usuario_id != null) {
$this->addError('post_id', ' Posts no valido');
}
}, 'on' => self::ESCENARIO_FORO],
[['post_id'], function ($attributes) {
$post = Respuestas::findOne($this->padre_id);
if ($post == null || $post->post->equipo_usuario_id != null) {
$this->addError('post_id', ' Posts no valido');
}
}, 'on' => self::ESCENARIO_PADRE],
[['post_id'], 'exist', 'skipOnError' => true, 'targetClass' => Posts::className(), 'targetAttribute' => ['post_id' => 'id']],
[['padre_id'], 'exist', 'skipOnError' => true, 'targetClass' => self::className(), 'targetAttribute' => ['padre_id' => 'id']],
[['usuario_id'], 'exist', 'skipOnError' => true, 'targetClass' => UsuariosId::className(), 'targetAttribute' => ['usuario_id' => 'id']],
Expand Down
4 changes: 2 additions & 2 deletions views/comentarios/_comentarios.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<?php

$nombre = ($model->usuario->usuarios) ? Html::a(
Html::encode($model->usuario->usuarios->nombre), ['usuarios/view', 'id' => $model->usuario_id])
Html::encode($model->usuario->usuarios->nombre), ['/usuarios/view', 'id' => $model->usuario_id])
: 'anonimo';

?>
Expand All @@ -28,7 +28,7 @@
<p>
<?php
if (isset($model->padre_id)){
echo '#' . $model->padre->usuario->usuarios->nombre;
echo '#' . Html::a(Html::encode($model->padre->usuario->usuarios->nombre) , ['/usuarios/view', 'id' => $model->padre->usuario_id]);
}
?>
<?=Html::encode($model->comentario)?>
Expand Down
1 change: 1 addition & 0 deletions views/layouts/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
$item = [
['label' => 'Inicio', 'url' => ['/noticias/index']],
['label' => 'Ligas', 'items' => $i],
['label' => 'Foro' , 'url' => ['/posts/publico']],
];

if (Yii::$app->user->isGuest) {
Expand Down
2 changes: 1 addition & 1 deletion views/posts/_post.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<h3><?=Html::encode($model->titulo)?></h3>
</div>
<div class="col-xs-4 boton">
<?=Html::a('Ver detalles', ['posts/view', 'id' => $model->id], ['class' => 'btn btn-xs btn-info'])?>
<?=Html::a('Ver detalles', $ruta, ['class' => 'btn btn-xs btn-info'])?>
</div>
<div class="col-sm-3 col-xs-4 boton">
<?= Html::encode($model->creador->usuarios->nombre)?>
Expand Down
98 changes: 98 additions & 0 deletions views/posts/_view.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

use yii\data\ActiveDataProvider;


use yii\helpers\Html;

use yii\widgets\ListView;


/* @var $this yii\web\View */
/* @var $model app\models\Posts */


$js=<<<EOT
$('#botonComentario').on('click', function(){
var textarea = $(this).prev();
var texto = textarea.val();
$.ajax({
url: '$ruta',
type:'post',
data: {
comentario: texto,
noticia: '$model->id',
},
success: function (data){
console.log(data)
console.log($('#comentario'))
textarea.val(null);
$('#respuesta').append(data);
}
})
})
$('.posts-view').on('click','.responder',responder);
function responder (){
var id = $(this).parent().data('id');
$(this).after($('<textarea>'));
var boton = $('<button>');
boton.text('Enviar');
boton.addClass('btn btn-xs btn-info enviar');
comentar(boton,'$rutaPadre','$model->id');
$(this).after(boton);
$(this).remove();
}
EOT;
$this->registerJsFile('/js/comentarios.js',['depends' => [\yii\web\JqueryAsset::className()]]);
$this->registerJs($js);
?>
<div class="posts-view">

<div class="container">
<div class="row">
<h3><?=Html::encode($model->titulo)?></h3>
</div>
<div class="row">
<div class="col-md-8 col-md-offset-1">
<?=Html::img($model->img)?>
</div>
</div>
<hr>
<div class="row">
<div class="col-md-8">
<p>
<?=Html::encode($model->texto) ?>
</p>
</div>
</div>
<hr>
<div class="row">
<div class="col-md-7 col-md-offset-2">
<textarea name="comentario" rows="5" cols="50"></textarea>
<input class="btn btn-success" type="button" id="botonComentario" value="Comentar">
</div>

</div>
<hr>
<div id='respuesta'>
<?php if ($model->respuestas) {
echo ListView::widget([
'dataProvider' => new ActiveDataProvider([
'query' => $model->getRespuestas()->where('padre_id is null')->orderBy('created_at ASC'),
]),
'itemView' => '/comentarios/_comentarios',
'summary' =>''
]);
} ?>
</div>

</div>

</div>
9 changes: 7 additions & 2 deletions views/posts/create.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@
/* @var $model app\models\Posts */

$this->title = 'Crear Posts';
$this->params['breadcrumbs'][] = ['label' => 'Mis Equipos', 'url' => ['/equipos-usuarios/index']];
$this->params['breadcrumbs'][] = ['label' => $equipo->nombre, 'url' => ['index', 'id' => $equipo->id]];
if ($model->equipoUsuario) {
$this->params['breadcrumbs'][] = ['label' => 'Mis Equipos', 'url' => ['/equipos-usuarios/index']];
$this->params['breadcrumbs'][] = ['label' => $model->equipoUsuario->nombre, 'url' => ['index', 'id' => $model->equipo_usuario_id]];
}else {
$this->params['breadcrumbs'][] = ['label' => 'Foro', 'url' => ['/posts/publico']];
}

$this->params['breadcrumbs'][] = $this->title;
?>
<div class="posts-create">
Expand Down
3 changes: 2 additions & 1 deletion views/posts/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
</div>
<?php foreach ($model as $post) : ?>
<?= $this->render('_post', [
'model' => $post
'model' => $post,
'ruta' => ['/posts/view','id' => $post->id]
]) ?>
<?php endforeach ?>

Expand Down
33 changes: 33 additions & 0 deletions views/posts/publico.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

use yii\helpers\Html;


/* @var $this yii\web\View */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = 'Foro';
$this->params['breadcrumbs'][] = $this->title;
$this->registerCssFile('/css/equiposUsuarios.css');
?>
<div class="posts-index">


<?=Html::a('Crear post', ['/posts/create-publico'], ['class' => 'btn btn-success']) ?>

<div class="container col-md-8 col-md-offset-1">
<div class="row">
<?php foreach ($model as $post) : ?>
<?= $this->render('_post', [
'model' => $post,
'ruta' => ['/posts/view-publico','id' => $post->id]
]) ?>
<?php endforeach ?>
</div>
</div>





</div>
Loading

0 comments on commit 00a0ba7

Please sign in to comment.