Skip to content

Commit

Permalink
This battle station is now fully operational
Browse files Browse the repository at this point in the history
  • Loading branch information
UnrulyNatives committed Sep 16, 2016
1 parent 0fd48fb commit dd1ba57
Show file tree
Hide file tree
Showing 9 changed files with 430 additions and 64 deletions.
44 changes: 35 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
# A complete upvote and downvote solution
# This is a complete upvote and downvote solution for Laravel >= 5.3 application

This package delivers several solutions. Pick and use all or the one which meets your needs.
This package delivers several solutions working in the same BD table. Pick and use all or the one which meets your needs.



Current version:
[![Latest Stable Version](https://poser.pugx.org/unrulynatives/attitudes-laravel-upvote-solutions/v/stable)](https://packagist.org/packages/unrulynatives/attitudes-laravel-upvote-solutions)


Note: while the package does the job perfectly now, please give me some time to make the code look more professional. Contributors welcome!


## Features

Three different values available for storing likes and upvotes
Expand Down Expand Up @@ -81,7 +84,7 @@ Now run the migrations with command `php artisan migrate`. Verify that the table

5. (unfinished) Update your models with this package's trait.

The trait is under development. For now just paste the below functions to your models:
This package's trait is still under development. For now just paste the below functions to the models for which you wish to enable:

```
Expand All @@ -106,14 +109,15 @@ The trait is under development. For now just paste the below functions to your m
```
6. Routes (optional)

Properly working routes necessary to service voting are locatad within ths package:
Properly working routes necessary for this package to work are locatad within ths package:

```
Route::any('{itemkind}/{id}/set_user_attitude', ['as' => 'attitudes.set_user_attitude', 'uses' => 'AttitudesController@set_user_attitude']);
Route::any('{itemkind}/{id}/set_user_importance', ['as' => 'attitudes.set_user_importance', 'uses' => 'AttitudesController@set_user_importance']);
```

You can put them in your location of choice
You can put them in your location of choice, but make sure that the new location is parsed first. In case of problems, reverse the order of service providers of this package and `App\Providers\RouteServiceProvider::class,`


7. Attach the js and css files to your template. Mind the file paths if you decide to place them somewhere else than they are published to.

Expand All @@ -126,7 +130,7 @@ You can put them in your location of choice
8. Include the below view files in your `foreach` loop. Note that the looped variable should be changed accordingly. Here I use `$o->`.

```
$itemkind = 'features'; // features is a name of your model
<?php $itemkind = 'quotes'; ?> // features is a name of your model
@include('userattitudes._userattitudes_attitude_toggle_abstracted', ['itemkind' => $itemkind,'o' => $o, 'attitude' => (($cua = $o->user_approach(Auth::user())) ? $cua->attitude : NULL)])
@include('userattitudes._userattitudes_importance_toggle_abstracted', ['itemkind' => $itemkind,'o' => $o, 'importance' => (($cua = $o->user_approach(Auth::user())) ? $cua->importance : NULL)])
Expand All @@ -135,15 +139,37 @@ You can put them in your location of choice
```
Note: `itemkind` is plural lovercase name of your model. Take a look at the controller function: the `itemkind` name is changed into class name in order to process your request.

Note 2: The models which are attituded should have the morph class defined. The names are stored in the DB table `userattitudes` in column `item_type`.
9. Define the morph class
The models which are attituded should have the morph class defined. The names are stored in the DB table `userattitudes` in column `item_type`.
I myself define the morph class definitions manually, just to be sure that Laravel functions won't use some unusual defaults.
Do it in `app\Providers\AppServiceProvider.php`. Use the instructions in https://laravel.com/docs/5.3/upgrade.


For this package to work with predefined example, open the file and inside the
put the following code:

```
use Illuminate\Database\Eloquent\Relations\Relation;
// ...
public function boot()
{
Relation::morphMap([
'user' => User::class,
'quotation' => \App\Models\UnAQuotation::class,
]);
}
```

10. Define a csrf token
Be sure that the head of your page contains the below declaration, otherwise you will meet with unexpected behavior of the script:
`<meta name="csrf-token" content="{{ csrf_token() }}" />`

9. That's it! Now user choices should be stored in the database table `userattitudes`.
11. That's it! Now user choices should be stored in the database table `userattitudes`.

10. Working demo.
12. Working demo.

If you installed this package correctly, point your browser to `attitudes-demo`.
- You should be logged in
Expand Down
55 changes: 55 additions & 0 deletions src/Attitudes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace UnrulyNatives\Attitudes;


// use Illuminate\Auth\Authenticatable;
// use Illuminate\Auth\Passwords\CanResetPassword;
// use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
// use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
// use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
// use Illuminate\Database\Eloquent\Model;
// use Illuminate\Foundation\Auth\Access\Authorizable;
// use App\Role;
// use Laraveldaily\Quickadmin\Observers\UserActionsObserver;
// use Laraveldaily\Quickadmin\Traits\AdminPermissionsTrait;
// use Spatie\Permission\Traits\HasRoles;

trait Attitudes

{
// use Authenticatable, Authorizable, CanResetPassword, AdminPermissionsTrait, HasRoles;



public function importances() {
return $this->hasMany(\App\Models\Userattitude::class, 'creator_id');
}

public function attitudes() {
return $this->hasMany(\App\Models\Userattitude::class, 'creator_id');
}


public function user_approach($user)
{
return $this->morphMany(\App\Models\Userattitude::class, 'item')->where('creator_id', ($user ? $user->id : NULL))->first();
}



public static function boot()
{
parent::boot();

// User::observe(new UserActionsObserver);
}

public function getAllattitudescountAttribute() {

return "testing if the package is correctly integrated";
}


}

104 changes: 103 additions & 1 deletion src/AttitudesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Http\Controllers\Controller;
use Carbon\Carbon;
use App\Models\Feature;
use App\Models\Quote;

class AttitudesController extends Controller
{
Expand Down Expand Up @@ -39,8 +40,109 @@ public function demo($timezone = NULL)

$features = Feature::all();

return view('userattitudes.feature.index', compact('current_time','features'))->with('itemkind', 'features');

// As I cannot make seeding work (see http://stackoverflow.com/questions/39521913/laravel-5-3-dbseed-command-simply-doesnt-work) this will make sure that you have data for test run.
if(Feature::count() < 10){
Feature::insert([
'name' => 'Feature. Random integer: '.rand(100,200).'.',
'description' => 'A random description of feature. Random integer: '.rand(100,200).'.',

]);
}

// As I cannot make seeding work (see http://stackoverflow.com/questions/39521913/laravel-5-3-dbseed-command-simply-doesnt-work) this will make sure that you have data for test run.
if(Quote::count() < 10){
Quote::insert([
'text' => 'I never learned from a man who agreed with me. Random integer: '.rand(100,200).'.',
'author' => 'Robert A. Heinlein',

]);
}

$object = Quote::get();

return view('userattitudes.feature.index', compact('current_time','features','object'))->with('itemkind', 'features');
}




public function set_user_attitude($itemkind, $id) {
// $id = (int)$id;
$value = (int)Input::get('value');

// $itemtype = (int)Input::get('itemtype');
// $itemtype = (int)$itemtype;

if (!Auth::check()) return App::abort(403);

//getting Class name
$itemtype = str_singular($itemkind);
$class_name = ucfirst($itemtype);
$name = "App\\Models\\" . $class_name;
$class = new $name;
if (class_exists($name) && get_parent_class($class) == 'Illuminate\Database\Eloquent\Model') {
$model = $class->find($id);
}


$object = $model->attitudes()->where('creator_id', Auth::id())->first();
if (!$object) {
$object = new Userattitude;
$object->creator_id = Auth::id();
$object->item_type = $itemtype;
$object->item_id = $id;
$object->importance = '1';
$object->attitude = $value;
}
$object->attitude = $value;
$object->save();



// return Response::json(['status' => true]);
return Response::json();
}




public function set_user_importance($itemkind, $id) {
// $id = (int)$id;

$value = (int)Input::get('value');

// $itemtype = Input::get('itemtype');

if (!Auth::check()) return App::abort(403);

//getting Class name
$itemtype = str_singular($itemkind);
$class_name = ucfirst($itemtype);
$name = "App\\Models\\" . $class_name;
$class = new $name;
if (class_exists($name) && get_parent_class($class) == 'Illuminate\Database\Eloquent\Model') {
$model = $class->find($id);
}

// $user_id = Auth::id()->id;
$object = $model->importances()->where('creator_id', Auth::id())->first();
if (!$object) {
$object = new Userattitude;
$object->creator_id = Auth::id();
$object->item_type = $itemtype;
$object->item_id = $id;
}
$object->importance = $value;

$object->save();

// return Response::json(['status' => true]);
return Response::json();
}





}
43 changes: 36 additions & 7 deletions src/AttitudesServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,56 @@ public function boot()


$this->publishes([
__DIR__.'/views' => base_path('resources/views/vendor/unrulynatives/attitudes'),

// publish migrations to the app's app/Http/Controllers folder
__DIR__.'/attitudes_model' => base_path('app/Models'),

// publish templates to vote models
__DIR__.'/attitudes_views' => base_path('resources/views/userattitudes'),

// publish migrations to the app's app/Http/Controllers folder
// publish controllers to the app's app/Http/Controllers folder
__DIR__.'/attitudes_controller' => base_path('app/Http/Controllers'),

// publish Models to the app's app/Http/Controllers folder
__DIR__.'/attitudes_migrations' => base_path('database/migrations'),


], 'app');



// publishing the basic views
$this->publishes([
// publish templates to your `resources/views` location
__DIR__.'/attitudes_views' => base_path('resources/views/userattitudes'),

__DIR__.'/views' => base_path('resources/views/vendor/unrulynatives/attitudes'),
], 'views');






$this->publishes([
// publish migrations for all registered packages
__DIR__.'/unstarter_migrations' => base_path('database/migrations')


], 'migrations');


$this->publishes([
// publish seeds for all registered packages
__DIR__.'/unstarter_seeds' => base_path('database/seeds'),
], 'seeds');


$this->publishes([
// publish public folder content: css and js
// Public
__DIR__.'/../public' => public_path(''),
], 'publicassets');




]);



Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUnAQuotationsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('un_a_quotations', function (Blueprint $table) {
$table->increments('id');


$table->longText('text');
$table->string('author', 128);

// these two will come in handy in later development of this package
$table->integer('language_id')->unsigned()->nullable()->default(null);
$table->integer('author_id')->unsigned()->nullable()->default(null);

$table->integer('creator_id')->unsigned()->index('creator_id')->nullable()->default(null);
$table->integer('updater_id')->unsigned()->index('updater_id')->nullable()->default(null);
$table->timestamps();
$table->softDeletes();
$table->integer('status')->nullable()->default(null);
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{

Schema::dropIfExists('un_a_quotations');

}
}
Loading

0 comments on commit dd1ba57

Please sign in to comment.