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

Renato #3

Open
wants to merge 2 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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
composer.phar
/vendor
composer.lock
.env.*.php
.env.php
.DS_Store
Thumbs.db
6 changes: 6 additions & 0 deletions app/config/app.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
return [

'locale' => 'pt-BR'

];
10 changes: 10 additions & 0 deletions app/config/database.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

return [

'host' => 'localhost',
'database' => 'insta_mkt',
'username' => 'root',
'password' => '',

];
10 changes: 10 additions & 0 deletions app/config/development/database.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

return [

'host' => 'localhost',
'database' => 'insta_mkt',
'username' => 'root',
'password' => '',

];
7 changes: 7 additions & 0 deletions app/config/plugins.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
return [

'EloquentPlugin' => 'Skp\Database\EloquentPlugin',
'AuthPlugin' => 'Skp\Auth\AuthPlugin',

];
8 changes: 8 additions & 0 deletions app/controllers/BaseController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

class BaseController extends \Skp\Foundation\Controller
{

protected $layout = 'layouts/default.php';

}
11 changes: 11 additions & 0 deletions app/controllers/IndexController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

class IndexController extends BaseController
{

public function indexAction()
{
return new \Symfony\Component\HttpFoundation\Response($this->request->get('a'));
}

}
158 changes: 158 additions & 0 deletions app/controllers/PhotoController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
<?php

use Symfony\Component\HttpFoundation\Response;
use Skp\Validation\Validator;
use Intervention\Image\ImageManager;

class PhotoController extends BaseController
{

public function addAction()
{
if (!isLogged()) {
return new Response($this->render('user/register.php', []));
}

return new Response($this->render('photo/add.php', []));
}

public function sendPhotoAction()
{
if (!$this->request->isMethod('POST') || !isLogged()) {
return new \Illuminate\Http\RedirectResponse('/');
}

/**
* @var \Symfony\Component\HttpFoundation\File\UploadedFile $photo
*/
$photo = $this->request->files->get('photo', null);
$data = $this->request->get('form', []);

$data['photo'] = $photo;

$validator = Validator::make(
$data,
Post::$rules[Validator::RULE_CREATE],
Post::$messages
);

if ($validator->fails()) {
return new Response($this->render('photo/add.php', [
'errors' => $validator->messages(),
'form' => $data
]));
}

$guard = \Skp\Registry::get('AuthGuard');
$user = $guard->user();

$post = new Post();
$post->user_id = $user->id;
$post->description = (isset($data['description'])) ? $data['description'] : '';

$saved = $post->save();

if ($saved) {

$path = POOL_PATH . 'photos/' . $post->id . '/';

if (!file_exists($path)) {
mkdir($path, 0755, true);
}

$fileName = $photo->getClientOriginalName();

if (file_exists($path . $fileName)) {
unlink($path . $fileName);
}

if (file_exists($path . 'thumb_' . $fileName)) {
unlink($path . 'thumb_' . $fileName);
}

try {

$photo->move($path, $fileName);

$manager = new ImageManager();
$manager->make($path . $fileName)->fit(640, 640)->save($path . $fileName);

$manager->make($path . $fileName)->fit(306, 306)->save($path . 'thumb_' . $fileName);

$post->name = $fileName;
$post->save();

return new \Illuminate\Http\RedirectResponse('/' . $user->username);

} catch (Exception $e) {
echo $e->getMessage();exit;
}
}

if ($post->id) {
$post->delete();
}

return new Response($this->render('photo/add.php', [
'error' => 'Houve um erro inesperado ao adicionar a foto',
'form' => $data
]));
}

public function listAction($username)
{
$user = User::where('username', $username)->first();

$photos = [];
$last = false;

if ($user) {

$perPage = 9;
$total = $user->posts->count();

$pages = ceil($total / $perPage);
$page = $this->request->get('page', 1);
$page = ($page < 1) ? 1 : $page;
$page = ($page > $pages) ? $pages : $page;
$offset = $perPage * ($page - 1);

$photos = $user->posts()->offset($offset)->take($perPage)->get();
$last = (bool)($page == $pages);
}

$this->layout = null;
return new Response($this->render('photo/list.php', [
'photos' => $photos,
'last' => $last,
'loggedUser' => \Skp\Registry::get('loggedUser')
]));
}

public function likeAction($id)
{
$response = ['liked' => false];

if (isLogged()) {

$photo = Post::find($id);
$loggedUser = \Skp\Registry::get('loggedUser');

if (!$photo->liked($loggedUser)) {
$photo->like($loggedUser);

$response['liked'] = true;
} else {
$photo->unlike($loggedUser);

$response['liked'] = false;
}

$response['likes'] = $photo->likes()->count();

}

return new \Symfony\Component\HttpFoundation\JsonResponse($response);
}

}
132 changes: 132 additions & 0 deletions app/controllers/UserController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?php

use Symfony\Component\HttpFoundation\Response;
use Skp\Validation\Validator;

class UserController extends BaseController
{

public function registerAction()
{
if (!isLogged()) {
return new Response($this->render('user/register.php', []));
}

$guard = \Skp\Registry::get('AuthGuard');
return new \Illuminate\Http\RedirectResponse('/' . $guard->user()->username);
}

public function perfilAction($username)
{
$user = User::where('username', $username)->first();

return new Response($this->render('user/perfil.php', [
'user' => $user
]));
}

public function followAction($username)
{
$response = ['followed' => false];

if (isLogged()) {

$user = User::where('username', $username)->first();
$loggedUser = \Skp\Registry::get('loggedUser');

if ($user->id != $loggedUser->id) {

if (!$loggedUser->followed($user)) {
$loggedUser->follow($user);

$response['followed'] = true;
} else {
$loggedUser->unfollow($user);

$response['followed'] = false;
}

}

}

return new \Symfony\Component\HttpFoundation\JsonResponse($response);
}

public function sendRegisterAction()
{
if (!$this->request->isMethod('POST') || isLogged()) {
return new \Illuminate\Http\RedirectResponse('/');
}

$data = $this->request->get('form', []);

$validator = Validator::make(
$data,
User::$rules[Validator::RULE_CREATE],
User::$messages
);

if ($validator->fails()) {
return new Response($this->render('user/register.php', [
'errors' => $validator->messages(),
'form' => $data
]));
}

$hasher = new \Illuminate\Hashing\BcryptHasher();

$user = new User();
$user->username = $data['username'];
$user->email = $data['email'];
$user->password = $hasher->make($data['password']);
$user->confirmed_at = date('Y-m-d H:i:s');
$user->confirmed = 1;

$saved = $user->save();

if ($saved) {
$guard = \Skp\Registry::get('AuthGuard');
$guard->login($user);

return new \Illuminate\Http\RedirectResponse('/' . $user->username);
}

return new Response($this->render('user/register.php', [
'error' => 'Houve um erro inesperado ao criar sua conta',
'form' => $data
]));
}

public function sendLoginAction()
{
if (!$this->request->isMethod('POST') || isLogged()) {
return new \Illuminate\Http\RedirectResponse('/');
}

$data = $this->request->get('login', []);

$guard = \Skp\Registry::get('AuthGuard');
$logged = $guard->attempt($data);

if (!$logged) {
return new Response($this->render('user/register.php', [
'error' => 'Usuário e/ou senha inválido(s)',
'login' => $data
]));
}

$user = $guard->user();

return new \Illuminate\Http\RedirectResponse('/' . $user->username);
}

public function logoutAction()
{
$guard = \Skp\Registry::get('AuthGuard');
$guard->logout();

return new \Illuminate\Http\RedirectResponse('/');
}

}
Loading