Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Небольшие исправления кода #1

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion console/migrations/m130524_201442_init.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function up()
'password_hash' => $this->string()->notNull(),
'password_reset_token' => $this->string()->unique(),
'email' => $this->string()->notNull()->unique(),

'role' => $this->integer()->notNull()->defaultValue(20),
'status' => $this->smallInteger()->notNull()->defaultValue(10),
'created_at' => $this->integer()->notNull(),
'updated_at' => $this->integer()->notNull(),
Expand All @@ -30,6 +30,7 @@ public function up()
$user->username = 'admin';
$user->email = 'admin@localhost';
$user->setPassword('admin');
$user->role = \common\models\User::ROLE_ADMIN;
$user->generateAuthKey();

$user->save();
Expand Down
19 changes: 0 additions & 19 deletions console/migrations/m161009_175734_user_roles.php

This file was deleted.

84 changes: 34 additions & 50 deletions frontend/controllers/AjaxController.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,59 +41,43 @@ public function actionLike()
{
$id = Yii::$app->request->post("id");

if ($id !== null)
{
$id = (int) $id;
$anek = Aneks::findOne($id);
/* @var $anek Aneks */

if ($anek)
{
$likes = count($anek->likes);

if ($anek->userLikes())
{
Likes::deleteAll(["user_id" => Yii::$app->user->id, "anek_id" => $anek->id]);
$like = false;
$likes--;
}
else
{
$like = new Likes();

$like->user_id = Yii::$app->user->id;
$like->anek_id = $anek->id;

if (!$like->save())
{
throw new ErrorException("Не удалось сохранить лайк");
}
else
{
$like = true;
$likes++;
}
}
$result = new \stdClass;

$result->like = $like;
$result->likes = $likes;

Yii::$app->response->format = Response::FORMAT_JSON;

return $result;
if (!$id) {
throw new BadRequestHttpException("Данные не переданы");
}

}
else
{
throw new ErrorException("Анекдот не найден");
}
$anek = Aneks::findOne($id);

if (!$anek) {
throw new ErrorException("Анекдот не найден");
}
else
{
throw new BadRequestHttpException("Данные не переданы");

$likes = count($anek->likes);

if ($anek->userLikes()) {
Likes::deleteAll(["user_id" => Yii::$app->user->id, "anek_id" => $anek->id]);
$like = false;
$likes--;
} else {
$like = new Likes();

$like->user_id = Yii::$app->user->id;
$like->anek_id = $anek->id;

if (!$like->save()) {
throw new ErrorException("Не удалось сохранить лайк");
}

$like = true;
$likes++;
}
}
$result = new \stdClass;

$result->like = $like;
$result->likes = $likes;

Yii::$app->response->format = Response::FORMAT_JSON;

return $result;
}

}
24 changes: 10 additions & 14 deletions frontend/controllers/ImageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,25 +51,21 @@ public function actions()
*/
public function actionGet($url = null)
{
$image = AnekPicture::readPicture($url);

if ($url === null)
{
if ($url === null) {
throw new BadRequestHttpException("Картинка не запрошена");
}

if ($image)
{
$response = Yii::$app->getResponse();
$response->headers->set('Content-Type', 'image/jpeg');
$response->format = Response::FORMAT_RAW;

return $image;
$image = AnekPicture::readPicture($url);

}
else
{
if (!$image) {
throw new NotFoundHttpException("Картинка не найдена");
}

$response = Yii::$app->getResponse();
$response->headers->set('Content-Type', 'image/jpeg');
$response->format = Response::FORMAT_RAW;

return $image;

}
}
13 changes: 1 addition & 12 deletions frontend/controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,15 +179,13 @@ public function actionPostAnek()
if (count($post))
{
$picture = UploadedFile::getInstance($anek_form,'image');
$image = null;

if ($picture)
{
$image = AnekPicture::uploadImage($picture);
$anek_form->image = AnekPicture::uploadImage($picture);
}

$anek_form->text = $post['text'];
$anek_form->image = $image;
$anek_form->category_id = $post['category_id'];

if ($anek_form->createAnek())
Expand All @@ -205,31 +203,22 @@ public function actionUploadAnek()
{
$upload_form = new ExcelForm();


if (Yii::$app->request->isPost)
{
$file = UploadedFile::getInstance($upload_form,'word');
$content = null;

if ($file)
{
$content = ExcelForm::getContent($file);


$anek = new Aneks();
$anek->text = $content;

if ($anek->save())
{
return $this->redirect(["feed"]);
}
else
{
//var_dump($anek->errors);die();
}

}
//die($content);
}

return $this->render("upload-anek",[
Expand Down
104 changes: 33 additions & 71 deletions frontend/views/anek/feed.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
/* @var $users array */



$this->registerJsFile("/js/jquery.grid-a-licious.min.js",['depends' => [\yii\web\JqueryAsset::className()]]);
$this->registerJsFile("/js/jquery.grid-a-licious.min.js", ['depends' => [\yii\web\JqueryAsset::className()]]);

$like_url = Url::to(['ajax/like']);
$paginator_url = Url::to(['feed', 'page' => '']);
Expand Down Expand Up @@ -233,14 +232,13 @@ function (msg)
'options' => ['class' => 'form-horizontal'],
]);

foreach ($aneks as $a)
{
foreach ($aneks as $a) {
if (!isset($users[$a->user->id])) {
$users[$a->user->id] = $a->user->username;
}
}
?>
<?= $form->field($filter, 'user')->dropDownList($users,['prompt' => 'Все пользователи']) ?>
<?= $form->field($filter, 'user')->dropDownList($users, ['prompt' => 'Все пользователи']) ?>
<?= $form->field($filter, 'mode')->checkboxList(Aneks::$modes) ?>
<?= Html::submitButton('Искать', ['class' => 'btn btn-primary']) ?>
<?php
Expand All @@ -250,77 +248,41 @@ function (msg)

<div class="anek-row">
<?php


foreach ($aneks as $a)
{
/* @var $a Aneks */

?>
<div class="anek-item">
<?php
foreach ($aneks as $a) {
$anek_content = $a->getContent();

$image_html = '';
$text_html = '';
if (($anek_content->mode === Aneks::MODE_BOTH)||($anek_content->mode === Aneks::MODE_IMAGE))
{
$image_src = $a->getImage();

if (($anek_content->mode === Aneks::MODE_BOTH) || ($anek_content->mode === Aneks::MODE_IMAGE)) {
$image_url = Url::to(['image/get', 'url' => $a->image]);

$image_html = <<<HTML
<span class="zoom"><img src="{$image_url}" class="img-responsive"/></span>
HTML;
}
if (($anek_content->mode === Aneks::MODE_BOTH)||($anek_content->mode === Aneks::MODE_TEXT))
{
$text_html = <<<HTML
<p>{$anek_content->text}</p>
HTML;
}
?>

<section class="blog-post">
<div class="panel panel-default">
<?= $image_html ?>
<div class="panel-body">
<div class="blog-post-meta">
<span class="label label-light label-primary"><?= $a->getCategory() ?></span>
<p class="blog-post-date pull-right"><?= date("d.m.Y H:i", strtotime($a->publish_time)) ?></p>
</div>
<div class="blog-post-content">
<a href="<?= Url::to(['user/profile', 'id' => $a->user->id]) ?>">
<h2 class="blog-post-title pull-right"><?= $a->user->username ?></h2>
</a>
<?= $text_html ?>
<a class="btn btn-info" href="<?= Url::to(['anek/related', 'id' => $a->id]) ?>">Похожие</a>
<a class="blog-post-share pull-right" href="#">
<i class="material-icons">&#xE80D;</i>

</a>
<?php
$red = '';
if ($a->userLikes())
{
$red = ' btn-danger';
}
?>
<button class="btn like-btn <?= $red ?>" data-id="<?= $a->id ?>"><?= count($a->likes) ?> <i class="fa fa-heart"></i></button>
<div class="anek-item">
<section class="blog-post">
<div class="panel panel-default">
<?php if ($image_url) { ?>
<span class="zoom"><img src="<?= $image_url ?>" class="img-responsive"/></span>
<?php } ?>
<div class="panel-body">
<div class="blog-post-meta">
<span class="label label-light label-primary"><?= $a->getCategory() ?></span>
<p class="blog-post-date pull-right"><?= date("d.m.Y H:i", strtotime($a->publish_time)) ?></p>
</div>
<div class="blog-post-content">
<a href="<?= Url::to(['user/profile', 'id' => $a->user->id]) ?>">
<h2 class="blog-post-title pull-right"><?= $a->user->username ?></h2>
</a>
<?php if ($anek_content->text) { ?>
<p><?= $anek_content->text ?></p>
<?php } ?>
<a class="btn btn-info" href="<?= Url::to(['anek/related', 'id' => $a->id]) ?>">Похожие</a>
<a class="blog-post-share pull-right" href="#">
<i class="material-icons">&#xE80D;</i>
</a>
<button class="btn like-btn <?php $a->userLikes() AND print ' btn-danger' ?>"
data-id="<?= $a->id ?>"><?= count($a->likes) ?> <i class="fa fa-heart"></i></button>
</div>
</div>
</div>
</div>
</section><!-- /.blog-post -->


</div>



<?php
}
?>


</section><!-- /.blog-post -->
</div>
<?php } ?>
</div>
<button class="load-more btn btn-default">Загрузить еще</button>
Loading