Skip to content

Commit

Permalink
Added a few more things
Browse files Browse the repository at this point in the history
  • Loading branch information
joaogl committed Apr 10, 2016
1 parent 8760465 commit 6e04bcf
Show file tree
Hide file tree
Showing 24 changed files with 738 additions and 17 deletions.
Empty file modified CHANGELOG.md
100644 → 100755
Empty file.
Empty file modified CONDUCT.md
100644 → 100755
Empty file.
Empty file modified CONTRIBUTING.md
100644 → 100755
Empty file.
Empty file modified LICENSE.md
100644 → 100755
Empty file.
Empty file modified README.md
100644 → 100755
Empty file.
Empty file modified composer.json
100644 → 100755
Empty file.
77 changes: 77 additions & 0 deletions src/Blog.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php namespace jlourenco\blog;

use jlourenco\blog\Repositories\BlogPostRepositoryInterface;
use jlourenco\blog\Repositories\BlogCategoryRepositoryInterface;

class Blog
{

/**
* The Post repository.
*
* @var \jlourenco\blog\Repositories\BlogPostRepositoryInterface
*/
protected $posts;

/**
* The Category repository.
*
* @var \jlourenco\blog\Repositories\BlogCategoryRepositoryInterface
*/
protected $categories;

/**
* Create a new Blog instance.
*
* @param \jlourenco\blog\Repositories\BlogPostRepositoryInterface $posts
* @param \jlourenco\blog\Repositories\BlogCategoryRepositoryInterface $categories
*/
public function __construct(BlogPostRepositoryInterface $posts, BlogCategoryRepositoryInterface $categories)
{
$this->categories = $categories;
$this->posts = $posts;
}

/**
* Returns the posts repository.
*c
* @return \jlourenco\blog\Repositories\BlogPostRepositoryInterface
*/
public function getPostsRepository()
{
return $this->posts;
}

/**
* Sets the posts repository.
*
* @param \jlourenco\blog\Repositories\BlogPostRepositoryInterface $posts
* @return void
*/
public function setPostsRepository(BlogPostRepositoryInterface $posts)
{
$this->posts = $posts;
}

/**
* Returns the categories repository.
*c
* @return \jlourenco\blog\Repositories\BlogCategoryRepositoryInterface
*/
public function getCategoriesRepository()
{
return $this->categories;
}

/**
* Sets the categories repository.
*
* @param \jlourenco\blog\Repositories\BlogCategoryRepositoryInterface $categories
* @return void
*/
public function setCategoriesRepository(BlogCategoryRepositoryInterface $categories)
{
$this->categories = $categories;
}

}
126 changes: 126 additions & 0 deletions src/Controllers/BlogController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?php namespace jlourenco\blog\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Blog;
use Sentinel;
use DB;
use Searchy;

class BlogController extends Controller
{

/**
* Show the posts.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$posts = Blog::getPostsRepository()->all();
$categories = Blog::getCategoriesRepository()->all();

return view('public.blog.list', compact('posts', 'categories'));
}

public function edit($id)
{
$post = Blog::getPostsRepository()->findOrFail($id);

return view('public.blog.edit', compact('post'));
}

public function update($id, Request $request)
{
$this->validate($request, [ 'value' => 'required' ]);

$move = Blog::getPostsRepository()->findOrFail($id);

$move->update($request->all());

return redirect('blog');
}

public function show($id)
{
$post = Blog::getPostsRepository()->findOrFail($id);
$categories = Blog::getCategoriesRepository()->all();

// Show the page
return View('public.blog.post', compact('post', 'categories'));
}

public function showByCategory($id)
{
$category = Blog::getCategoriesRepository()->findOrFail($id);
$posts = $category->posts;
$categories = Blog::getCategoriesRepository()->all();

// Show the page
return view('public.blog.list', compact('posts', 'categories'));
}

public function search($in)
{
// Setting up vars
$search = $in;
$terms = explode(' ', $search);
array_push($terms, $search);

// Checking the users
/*
$users = $this->applySearch(Sentinel::createModel(), $terms, ['first_name', 'last_name'])->join('BlogPost', 'User.id', '=', 'BlogPost.author')
->select(DB::raw('CONCAT(User.first_name, " ", User.last_name) AS a'), DB::raw('\'User\' AS type'))->distinct()->get()->toArray();
$users = $this->evaluate($users, $terms, ['first_name', 'last_name']);*/

$users = Sentinel::createModel()->search($in)
/*->distinct()*/->select(DB::raw('CONCAT(User.first_name, " ", User.last_name) AS a'), DB::raw('\'User\' AS type'))->get()->toArray();

dd($users);

// Checking the posts
$posts = $this->applySearch(Blog::getPostsRepository(), $terms, ['title', 'contents', 'keywords'])->select('title as a', DB::raw('\'Post\' AS type'))->distinct()->get()->toArray();

// Checking the Categories
$categories = $this->applySearch(Blog::getCategoriesRepository(), $terms, ['name', 'description'])->select('name as a', DB::raw('\'Category\' AS type'))->distinct()->get()->toArray();

// Returning the results
return response()->json( [ 'data' => array_merge($users, $categories, $posts) ] );
}

public function applySearch($repo, $terms, $fields)
{
foreach($terms as $term)
foreach($fields as $field)
$repo = $repo->orWhere($field, 'LIKE', '%' . $term . '%');

foreach($repo as $data)
{
dd($repo);
}
dd($repo);

return $repo;
}

public function evaluate($repo, $terms, $fields)
{

return $repo;
}

/*
* Admin section
*/
public function getAdminIndex()
{
// Grab all the users
$posts = Blog::getPostsRepository()->all();

// Show the page
return View('admin.blog.list', compact('posts'));
}

}
16 changes: 16 additions & 0 deletions src/Facades/Blog.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php namespace jlourenco\blog\Facades;

use Illuminate\Support\Facades\Facade;

class Blog extends Facade
{

/**
* {@inheritDoc}
*/
protected static function getFacadeAccessor()
{
return 'blog';
}

}
12 changes: 11 additions & 1 deletion src/Migrations/2016_01_15_123600_create_blog_tables.php
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use \jlourenco\support\Database\Blueprint;

class CreateBlogTables extends Migration
{
Expand All @@ -16,16 +16,22 @@ public function up()
Schema::create('BlogCategory', function (Blueprint $table) {
$table->increments('id');
$table->string('name', 100);
$table->string('slug', 100);
$table->string('description', 250);

$table->timestamps();
$table->softDeletes();
$table->creation();
});

Schema::table('BlogCategory', function (Blueprint $table) {
$table->creationRelation();
});

Schema::create('BlogPost', function (Blueprint $table) {
$table->increments('id');
$table->string('title', 100);
$table->string('slug', 100);
$table->text('contents');
$table->integer('category')->unsigned();
$table->integer('author')->unsigned();
Expand All @@ -42,6 +48,10 @@ public function up()
$table->foreign('author')->references('id')->on('User');
});

Schema::table('BlogPost', function (Blueprint $table) {
$table->creationRelation();
});

}

/**
Expand Down
60 changes: 60 additions & 0 deletions src/Models/BlogCategory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php namespace jlourenco\blog\Models;

use Illuminate\Database\Eloquent\Model;
use jlourenco\support\Traits\Creation;

class BlogCategory extends Model
{

/**
* To allow user actions identity (Created_by, Updated_by, Deleted_by)
*/
use Creation;

/**
* {@inheritDoc}
*/
protected $table = 'BlogCategory';

/**
* {@inheritDoc}
*/
protected $fillable = [
'name',
'description'
];

/**
* The Blog post model name.
*
* @var string
*/
protected static $postsModel = 'jlourenco\blog\Models\BlogPost';

/**
* Returns the post model.
*
* @return string
*/
public static function getPostsModel()
{
return static::$postsModel;
}

/**
* Sets the post model.
*
* @param string $postsModel
* @return void
*/
public static function setPostsModel($postsModel)
{
static::$postsModel = $postsModel;
}

public function posts()
{
return $this->hasMany(static::$postsModel, 'category');
}

}
Loading

0 comments on commit 6e04bcf

Please sign in to comment.