mirror of
https://github.com/Einundzwanzig-Podcast/einundzwanzig-portal.git
synced 2025-12-11 06:46:47 +00:00
voting added
This commit is contained in:
@@ -54,6 +54,7 @@ class CreatePermissions extends Command
|
||||
'OrangePillPolicy',
|
||||
'ParticipantPolicy',
|
||||
'PodcastPolicy',
|
||||
'ProjectProposalPolicy',
|
||||
'RegistrationPolicy',
|
||||
'TeamPolicy',
|
||||
'UserPolicy',
|
||||
|
||||
@@ -82,74 +82,75 @@ class Header extends Component
|
||||
Cookie::queue('lang', $this->l, 60 * 24 * 365);
|
||||
|
||||
return view('livewire.frontend.header', [
|
||||
'news' => LibraryItem::query()
|
||||
->with([
|
||||
'createdBy.roles',
|
||||
'lecturer',
|
||||
'tags',
|
||||
])
|
||||
->where('type', 'markdown_article')
|
||||
->where('approved', true)
|
||||
->where('news', true)
|
||||
->orderByDesc('created_at')
|
||||
->take(2)
|
||||
->get(),
|
||||
'meetups' => MeetupEvent::query()
|
||||
->with([
|
||||
'meetup.users',
|
||||
'meetup.city.country',
|
||||
])
|
||||
->where('start', '>', now())
|
||||
->orderBy('start')
|
||||
->take(2)
|
||||
->get(),
|
||||
'courseEvents' => CourseEvent::query()
|
||||
->with([
|
||||
'venue.city.country',
|
||||
'course.lecturer',
|
||||
])
|
||||
->where('from', '>', now())
|
||||
->orderBy('from')
|
||||
->take(2)
|
||||
->get(),
|
||||
'libraryItems' => LibraryItem::query()
|
||||
->with([
|
||||
'lecturer',
|
||||
])
|
||||
->where('type', '<>', 'markdown_article')
|
||||
->orderByDesc('created_at')
|
||||
->take(2)
|
||||
->get(),
|
||||
'bitcoinEvents' => BitcoinEvent::query()
|
||||
->with([
|
||||
'venue',
|
||||
])
|
||||
->where('from', '>', now())
|
||||
->orderBy('from')
|
||||
->take(2)
|
||||
->get(),
|
||||
'orangePills' => OrangePill::query()
|
||||
->with([
|
||||
'user',
|
||||
'bookCase',
|
||||
])
|
||||
->orderByDesc('date')
|
||||
->take(2)
|
||||
->get(),
|
||||
'cities' => City::query()
|
||||
->select(['latitude', 'longitude'])
|
||||
->get(),
|
||||
'countries' => Country::query()
|
||||
->select('id', 'name', 'code')
|
||||
->orderBy('name')
|
||||
->get()
|
||||
->map(function (Country $country) {
|
||||
$country->name = config('countries.emoji_flags')[str($country->code)
|
||||
->upper()
|
||||
->toString()].' '.$country->name;
|
||||
'news' => LibraryItem::query()
|
||||
->with([
|
||||
'createdBy.roles',
|
||||
'lecturer',
|
||||
'tags',
|
||||
])
|
||||
->where('type', 'markdown_article')
|
||||
->where('approved', true)
|
||||
->where('news', true)
|
||||
->orderByDesc('created_at')
|
||||
->take(2)
|
||||
->get(),
|
||||
'meetups' => MeetupEvent::query()
|
||||
->with([
|
||||
'meetup.users',
|
||||
'meetup.city.country',
|
||||
])
|
||||
->where('start', '>', now())
|
||||
->orderBy('start')
|
||||
->take(2)
|
||||
->get(),
|
||||
'courseEvents' => CourseEvent::query()
|
||||
->with([
|
||||
'venue.city.country',
|
||||
'course.lecturer',
|
||||
])
|
||||
->where('from', '>', now())
|
||||
->orderBy('from')
|
||||
->take(2)
|
||||
->get(),
|
||||
'libraryItems' => LibraryItem::query()
|
||||
->with([
|
||||
'lecturer',
|
||||
])
|
||||
->where('type', '<>', 'markdown_article')
|
||||
->orderByDesc('created_at')
|
||||
->take(2)
|
||||
->get(),
|
||||
'bitcoinEvents' => BitcoinEvent::query()
|
||||
->with([
|
||||
'venue',
|
||||
])
|
||||
->where('from', '>', now())
|
||||
->orderBy('from')
|
||||
->take(2)
|
||||
->get(),
|
||||
'orangePills' => OrangePill::query()
|
||||
->with([
|
||||
'user',
|
||||
'bookCase',
|
||||
])
|
||||
->orderByDesc('date')
|
||||
->take(2)
|
||||
->get(),
|
||||
'projectProposals' => [],
|
||||
'cities' => City::query()
|
||||
->select(['latitude', 'longitude'])
|
||||
->get(),
|
||||
'countries' => Country::query()
|
||||
->select('id', 'name', 'code')
|
||||
->orderBy('name')
|
||||
->get()
|
||||
->map(function (Country $country) {
|
||||
$country->name = config('countries.emoji_flags')[str($country->code)
|
||||
->upper()
|
||||
->toString()].' '.$country->name;
|
||||
|
||||
return $country;
|
||||
}),
|
||||
return $country;
|
||||
}),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Livewire\ProjectProposal\Form;
|
||||
|
||||
use App\Models\Country;
|
||||
use App\Models\ProjectProposal;
|
||||
use Livewire\Component;
|
||||
use Livewire\WithFileUploads;
|
||||
|
||||
class ProjectProposalForm extends Component
|
||||
{
|
||||
use WithFileUploads;
|
||||
|
||||
public Country $country;
|
||||
|
||||
public ?ProjectProposal $projectProposal = null;
|
||||
|
||||
public $image;
|
||||
|
||||
public ?string $fromUrl = '';
|
||||
|
||||
protected $queryString = [
|
||||
'fromUrl' => ['except' => ''],
|
||||
];
|
||||
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
'image' => [
|
||||
'nullable', 'mimes:jpeg,png,jpg,gif', 'max:10240'
|
||||
],
|
||||
|
||||
'projectProposal.user_id' => 'required',
|
||||
'projectProposal.name' => 'required',
|
||||
'projectProposal.support_in_sats' => 'required|numeric',
|
||||
'projectProposal.description' => 'required',
|
||||
];
|
||||
}
|
||||
|
||||
public function mount()
|
||||
{
|
||||
if (!$this->projectProposal) {
|
||||
$this->projectProposal = new ProjectProposal([
|
||||
'user_id' => auth()->id(),
|
||||
'description' => '',
|
||||
]);
|
||||
}
|
||||
if (!$this->fromUrl) {
|
||||
$this->fromUrl = url()->previous();
|
||||
}
|
||||
}
|
||||
|
||||
public function save()
|
||||
{
|
||||
$this->validate();
|
||||
$this->projectProposal->save();
|
||||
|
||||
return redirect($this->fromUrl);
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.project-proposal.form.project-proposal-form', [
|
||||
|
||||
]);
|
||||
}
|
||||
}
|
||||
16
app/Http/Livewire/ProjectProposal/ProjectProposalTable.php
Normal file
16
app/Http/Livewire/ProjectProposal/ProjectProposalTable.php
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Livewire\ProjectProposal;
|
||||
|
||||
use App\Models\Country;
|
||||
use Livewire\Component;
|
||||
|
||||
class ProjectProposalTable extends Component
|
||||
{
|
||||
public Country $country;
|
||||
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.project-proposal.project-proposal-table');
|
||||
}
|
||||
}
|
||||
107
app/Http/Livewire/ProjectProposal/ProjectProposalVoting.php
Normal file
107
app/Http/Livewire/ProjectProposal/ProjectProposalVoting.php
Normal file
@@ -0,0 +1,107 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Livewire\ProjectProposal;
|
||||
|
||||
use App\Models\Country;
|
||||
use App\Models\ProjectProposal;
|
||||
use App\Models\User;
|
||||
use App\Models\Vote;
|
||||
use Illuminate\Validation\Rule;
|
||||
use Livewire\Component;
|
||||
|
||||
class ProjectProposalVoting extends Component
|
||||
{
|
||||
|
||||
public Country $country;
|
||||
|
||||
public ?ProjectProposal $projectProposal = null;
|
||||
public ?Vote $vote = null;
|
||||
|
||||
public ?string $fromUrl = '';
|
||||
|
||||
protected $queryString = [
|
||||
'fromUrl' => ['except' => ''],
|
||||
];
|
||||
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
'vote.user_id' => 'required',
|
||||
'vote.project_proposal_id' => 'required',
|
||||
'vote.value' => 'required|boolean',
|
||||
'vote.reason' => [
|
||||
Rule::requiredIf(!$this->vote->value),
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
public function mount()
|
||||
{
|
||||
$vote = Vote::query()
|
||||
->where('user_id', auth()->id())
|
||||
->where('project_proposal_id', $this->projectProposal->id)
|
||||
->first();
|
||||
if ($vote) {
|
||||
$this->vote = $vote;
|
||||
} else {
|
||||
$this->vote = new Vote();
|
||||
$this->vote->user_id = auth()->id();
|
||||
$this->vote->project_proposal_id = $this->projectProposal->id;
|
||||
$this->vote->value = false;
|
||||
}
|
||||
}
|
||||
|
||||
public function yes()
|
||||
{
|
||||
$this->vote->value = true;
|
||||
$this->vote->save();
|
||||
|
||||
return to_route('project.voting.projectFunding',
|
||||
['country' => $this->country, 'projectProposal' => $this->projectProposal, 'fromUrl' => $this->fromUrl]);
|
||||
}
|
||||
|
||||
public function no()
|
||||
{
|
||||
$this->validate();
|
||||
|
||||
$this->vote->value = false;
|
||||
$this->vote->save();
|
||||
|
||||
return to_route('project.voting.projectFunding',
|
||||
['country' => $this->country, 'projectProposal' => $this->projectProposal, 'fromUrl' => $this->fromUrl]);
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.project-proposal.project-proposal-voting', [
|
||||
'entitledVoters' => User::query()
|
||||
->with([
|
||||
'votes' => fn($query) => $query->where('project_proposal_id',
|
||||
$this->projectProposal->id)
|
||||
])
|
||||
->withCount([
|
||||
'votes' => fn($query) => $query->where('project_proposal_id',
|
||||
$this->projectProposal->id)
|
||||
])
|
||||
->whereHas('roles', function ($query) {
|
||||
return $query->where('roles.name', 'entitled-voter');
|
||||
})
|
||||
->orderByDesc('votes_count')
|
||||
->get(),
|
||||
'otherVoters' => User::query()
|
||||
->with([
|
||||
'votes' => fn($query) => $query->where('project_proposal_id',
|
||||
$this->projectProposal->id)
|
||||
])
|
||||
->withCount([
|
||||
'votes' => fn($query) => $query->where('project_proposal_id',
|
||||
$this->projectProposal->id)
|
||||
])
|
||||
->whereDoesntHave('roles', function ($query) {
|
||||
return $query->where('roles.name', 'entitled-voter');
|
||||
})
|
||||
->orderByDesc('votes_count')
|
||||
->get(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
66
app/Http/Livewire/Tables/ProjectProposalTable.php
Normal file
66
app/Http/Livewire/Tables/ProjectProposalTable.php
Normal file
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Livewire\Tables;
|
||||
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Rappasoft\LaravelLivewireTables\DataTableComponent;
|
||||
use Rappasoft\LaravelLivewireTables\Views\Column;
|
||||
use App\Models\ProjectProposal;
|
||||
|
||||
class ProjectProposalTable extends DataTableComponent
|
||||
{
|
||||
public ?string $country = null;
|
||||
|
||||
public string $tableName = 'project_proposals';
|
||||
|
||||
public function configure(): void
|
||||
{
|
||||
$this->setPrimaryKey('id')
|
||||
->setAdditionalSelects(['id', 'created_by'])
|
||||
->setThAttributes(function (Column $column) {
|
||||
return [
|
||||
'class' => 'px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider dark:bg-gray-800 dark:text-gray-400',
|
||||
'default' => false,
|
||||
];
|
||||
})
|
||||
->setTdAttributes(function (Column $column, $row, $columnIndex, $rowIndex) {
|
||||
return [
|
||||
'class' => 'px-6 py-4 text-sm font-medium dark:text-white',
|
||||
'default' => false,
|
||||
];
|
||||
})
|
||||
->setColumnSelectStatus(false)
|
||||
->setPerPage(10)
|
||||
->setConfigurableAreas([
|
||||
'toolbar-left-end' => [
|
||||
'columns.project_proposals.areas.toolbar-left-end', [
|
||||
'country' => $this->country,
|
||||
],
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
public function columns(): array
|
||||
{
|
||||
return [
|
||||
Column::make("Id", "id")
|
||||
->sortable(),
|
||||
Column::make("Name", "name")
|
||||
->sortable(),
|
||||
Column::make(__('Intended support in sats'), "support_in_sats")
|
||||
->format(
|
||||
fn ($value, $row, Column $column) => number_format($value, 0, ',', '.')
|
||||
)
|
||||
->sortable(),
|
||||
Column::make('')
|
||||
->label(
|
||||
fn ($row, Column $column) => view('columns.project_proposals.action')->withRow($row)->withCountry($this->country)
|
||||
),
|
||||
];
|
||||
}
|
||||
|
||||
public function builder(): Builder
|
||||
{
|
||||
return ProjectProposal::query();
|
||||
}
|
||||
}
|
||||
67
app/Models/ProjectProposal.php
Normal file
67
app/Models/ProjectProposal.php
Normal file
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Spatie\Image\Manipulations;
|
||||
use Spatie\MediaLibrary\HasMedia;
|
||||
use Spatie\MediaLibrary\InteractsWithMedia;
|
||||
use Spatie\MediaLibrary\MediaCollections\Models\Media;
|
||||
|
||||
class ProjectProposal extends Model implements HasMedia
|
||||
{
|
||||
use InteractsWithMedia;
|
||||
use HasFactory;
|
||||
|
||||
/**
|
||||
* The attributes that aren't mass assignable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $guarded = [];
|
||||
|
||||
/**
|
||||
* The attributes that should be cast to native types.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $casts = [
|
||||
'id' => 'integer',
|
||||
'user_id' => 'integer',
|
||||
];
|
||||
|
||||
protected static function booted()
|
||||
{
|
||||
static::creating(function ($model) {
|
||||
if (! $model->created_by) {
|
||||
$model->created_by = auth()->id();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public function registerMediaConversions(Media $media = null): void
|
||||
{
|
||||
$this
|
||||
->addMediaConversion('preview')
|
||||
->fit(Manipulations::FIT_CROP, 300, 300)
|
||||
->nonQueued();
|
||||
$this->addMediaConversion('thumb')
|
||||
->fit(Manipulations::FIT_CROP, 130, 130)
|
||||
->width(130)
|
||||
->height(130);
|
||||
}
|
||||
|
||||
public function registerMediaCollections(): void
|
||||
{
|
||||
$this->addMediaCollection('main')
|
||||
->singleFile()
|
||||
->useFallbackUrl(asset('img/einundzwanzig.png'));
|
||||
}
|
||||
|
||||
public function user(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
}
|
||||
@@ -36,7 +36,6 @@ class User extends Authenticatable implements MustVerifyEmail, CanComment, Ciphe
|
||||
|
||||
/**
|
||||
* The attributes that should be hidden for serialization.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $hidden = [
|
||||
@@ -48,7 +47,6 @@ class User extends Authenticatable implements MustVerifyEmail, CanComment, Ciphe
|
||||
|
||||
/**
|
||||
* The attributes that should be cast.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $casts = [
|
||||
@@ -57,7 +55,6 @@ class User extends Authenticatable implements MustVerifyEmail, CanComment, Ciphe
|
||||
|
||||
/**
|
||||
* The accessors to append to the model's array form.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $appends = [
|
||||
@@ -93,4 +90,9 @@ class User extends Authenticatable implements MustVerifyEmail, CanComment, Ciphe
|
||||
{
|
||||
return $this->morphMany('QCod\Gamify\Reputation', 'subject');
|
||||
}
|
||||
|
||||
public function votes()
|
||||
{
|
||||
return $this->hasMany(Vote::class);
|
||||
}
|
||||
}
|
||||
|
||||
41
app/Models/Vote.php
Normal file
41
app/Models/Vote.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class Vote extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
/**
|
||||
* The attributes that aren't mass assignable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $guarded = [];
|
||||
|
||||
/**
|
||||
* The attributes that should be cast to native types.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $casts = [
|
||||
'id' => 'integer',
|
||||
'user_id' => 'integer',
|
||||
'project_proposal_id' => 'integer',
|
||||
'value' => 'bool',
|
||||
];
|
||||
|
||||
public function user(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
public function projectProposal(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(ProjectProposal::class);
|
||||
}
|
||||
}
|
||||
66
app/Policies/ProjectProposalPolicy.php
Normal file
66
app/Policies/ProjectProposalPolicy.php
Normal file
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
namespace App\Policies;
|
||||
|
||||
use App\Models\ProjectProposal;
|
||||
use App\Models\User;
|
||||
use Illuminate\Auth\Access\Response;
|
||||
|
||||
class ProjectProposalPolicy extends BasePolicy
|
||||
{
|
||||
/**
|
||||
* Determine whether the user can view any models.
|
||||
*/
|
||||
public function viewAny(User $user): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether the user can view the model.
|
||||
*/
|
||||
public function view(User $user, ProjectProposal $projectProposal): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether the user can create models.
|
||||
*/
|
||||
public function create(User $user): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether the user can update the model.
|
||||
*/
|
||||
public function update(User $user, ProjectProposal $projectProposal): bool
|
||||
{
|
||||
return $projectProposal->created_by === $user->id || $user->can((new \ReflectionClass($this))->getShortName().'.'.__FUNCTION__);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether the user can delete the model.
|
||||
*/
|
||||
public function delete(User $user, ProjectProposal $projectProposal): bool
|
||||
{
|
||||
return $projectProposal->created_by === $user->id || $user->can((new \ReflectionClass($this))->getShortName().'.'.__FUNCTION__);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether the user can restore the model.
|
||||
*/
|
||||
public function restore(User $user, ProjectProposal $projectProposal): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether the user can permanently delete the model.
|
||||
*/
|
||||
public function forceDelete(User $user, ProjectProposal $projectProposal): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user