Skip to content

Commit

Permalink
Dropdown for users, products, events, committees replaced with search…
Browse files Browse the repository at this point in the history
… fields. (Fixes #655)

* Initial ajax search functionality. (Refs #655)

* Further search field functionalit. (Refs #655)

* Further search field functionalit. (Refs #655)

* Finalized search fields! (Fixes #655)
  • Loading branch information
jonathanjuursema committed May 15, 2018
1 parent c9a4555 commit e7afaa7
Show file tree
Hide file tree
Showing 29 changed files with 497 additions and 402 deletions.
2 changes: 1 addition & 1 deletion app/Console/Commands/FeeCron.php
Expand Up @@ -113,7 +113,7 @@ public function handle()
$charged->count++;

$product = Product::findOrFail($fee);
$product->buyForUser($member->user, 1, $product->price);
$product->buyForUser($member->user, 1);

Mail::to($member->user)->queue((new FeeEmail($member->user, $email_fee, $product->price, $email_remmitance_reason))->onQueue('high'));

Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/AliasController.php
Expand Up @@ -103,7 +103,7 @@ public function store(Request $request)
return Redirect::route('alias::index');


} elseif ($request->input('user') != 'off') {
} elseif ($request->input('user') != '') {

$user = User::findOrFail($request->input('user'));

Expand Down
23 changes: 0 additions & 23 deletions app/Http/Controllers/ApiController.php
Expand Up @@ -19,29 +19,6 @@

class ApiController extends Controller
{
public function members(Request $request)
{

if (!Auth::check() || !Auth::user()->member) {
abort(403);
}

$users = User::all();
$data = array();

foreach ($users as $user) {
if (!$user->member) continue;
if ($request->has('term') && strpos(strtolower($user->name), strtolower($request->term)) === false) continue;

$member = new \stdClass();
$member->name = $user->name;
$member->id = $user->id;
$data[] = $member;
}

return $data;

}

public function train(Request $request)
{
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/EventController.php
Expand Up @@ -317,7 +317,7 @@ public function finclose(Request $request, $id)
$product->save();

foreach ($activity->users as $user) {
$product->buyForUser($user, 1, $product->price);
$product->buyForUser($user, 1);
}

$activity->closed = true;
Expand Down
28 changes: 27 additions & 1 deletion app/Http/Controllers/OrderLineController.php
Expand Up @@ -114,21 +114,47 @@ public function adminindex(Request $request)
*/
public function bulkStore(Request $request)
{

for ($i = 0; $i < count($request->input('user')); $i++) {

$user = User::findOrFail($request->input('user')[$i]);
$product = Product::findOrFail($request->input('product')[$i]);
$price = ($request->input('price')[$i] != "" ? floatval(str_replace(",", ".", $request->input('price')[$i])) : $product->price);
$units = $request->input('units')[$i];

$product->buyForUser($user, $units, $price * $units);
$product->buyForUser($user, $units);

}

$request->session()->flash('flash_message', 'Your manual orders have been added.');
return Redirect::back();
}

/**
* Store (a) simple orderline(s).
*
* @param Request $request
* @return \Illuminate\Http\RedirectResponse
*/
public function store(Request $request)
{

for ($u = 0; $u < count($request->input('user')); $u++) {
for ($p = 0; $p < count($request->input('product')); $p++) {

$user = User::findOrFail($request->input('user')[$u]);
$product = Product::findOrFail($request->input('product')[$p]);

$product->buyForUser($user, 1);

}
}

$request->session()->flash('flash_message', 'Your manual orders have been added.');
return Redirect::back();

}

/**
* Remove the specified resource from storage.
*
Expand Down
40 changes: 40 additions & 0 deletions app/Http/Controllers/SearchController.php
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Http\Request;

use Proto\Models\Product;
use Proto\Models\User;
use Proto\Models\Event;
use Proto\Models\Page;
Expand Down Expand Up @@ -254,4 +255,43 @@ public function openSearch()
return Response::make(View::make('website.opensearch'))->header('Content-Type', 'text/xml');
}

public function getUserSearch(Request $request)
{
$search_attributes = ['id', 'name', 'calling_name', 'email', 'phone', 'edu_username'];
return $this->getGenericSearch(User::class, $request->get('q'), $search_attributes);
}

public function getEventSearch(Request $request)
{
$search_attributes = ['id', 'title', 'description'];
return $this->getGenericSearch(Event::class, $request->get('q'), $search_attributes);
}

public function getCommitteeSearch(Request $request)
{
$search_attributes = ['id', 'name', 'slug', 'description'];
return $this->getGenericSearch(Committee::class, $request->get('q'), $search_attributes);
}

public function getProductSearch(Request $request)
{
$search_attributes = ['id', 'name', 'nicename'];
return $this->getGenericSearch(Product::class, $request->get('q'), $search_attributes);
}

private function getGenericSearch($model, $query, $attributes)
{
$terms = explode(' ', $query);
$results = collect([]);
foreach ($terms as $term) {
$query = $model::query();
$t = sprintf('%%%s%%', $term);
foreach ($attributes as $attr) {
$query = $query->orWhere($attr, 'LIKE', $t);
}
$results = $results->merge($query->get());
}
return $results->unique();
}

}
2 changes: 1 addition & 1 deletion app/Models/Committee.php
Expand Up @@ -20,7 +20,7 @@ class Committee extends Model
* @var string
*/
protected $table = 'committees';
protected $hidden = ['id', 'image_id'];
protected $hidden = ['image_id'];

public function getPublicId()
{
Expand Down
17 changes: 17 additions & 0 deletions app/Models/Event.php
Expand Up @@ -20,6 +20,8 @@ class Event extends Model
*/
protected $table = 'events';

protected $appends = ['is_future', 'formatted_date'];

public function getPublicId()
{
return Hashids::connection('event')->encode($this->id);
Expand Down Expand Up @@ -165,4 +167,19 @@ public function shouldShowDietInfo()
return $this->involves_food && $this->end > strtotime('-1 week');
}

public function getIsFutureAttribute()
{
return date('U') < $this->start;
}

public function getFormattedDateAttribute()
{
return (object)[
'simple' => date('M d, Y', $this->start),
'year' => date('Y', $this->start),
'month' => date('M Y', $this->start),
'time' => date('H:i', $this->start)
];
}

}
4 changes: 3 additions & 1 deletion app/Models/Product.php
Expand Up @@ -44,12 +44,14 @@ public function ticket()
return $this->hasOne('Proto\Models\Ticket', 'product_id');
}

public function buyForUser(User $user, $amount, $total, $withCash = false)
public function buyForUser(User $user, $amount, $total = null, $withCash = false)
{

$this->stock -= $amount;
$this->save();

$total = ($total ? $total : $this->price * $amount);

$orderline = OrderLine::create([
'user_id' => ($withCash ? null : $user->id),
'cashier_id' => ($withCash || $total == 0 ? $user->id : null),
Expand Down
12 changes: 11 additions & 1 deletion app/Models/User.php
Expand Up @@ -42,12 +42,14 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon

protected $guarded = ['password', 'remember_token'];

protected $appends = ['is_member', 'photo_preview'];

/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = ['id', 'password', 'remember_token', 'personal_key', 'deleted_at', 'created_at', 'image_id', 'tfa_totp_key', 'updated_at'];
protected $hidden = ['password', 'remember_token', 'personal_key', 'deleted_at', 'created_at', 'image_id', 'tfa_totp_key', 'updated_at', 'diet'];

public function getPublicId()
{
Expand Down Expand Up @@ -440,4 +442,12 @@ public function helperReminderSubscriptions()
return $this->belongsTo('Proto\Models\HelperReminder');
}

public function getIsMemberAttribute() {
return $this->member !== null;
}

public function getPhotoPreviewAttribute() {
return $this->generatePhotoPath();
}

}
3 changes: 2 additions & 1 deletion bower.json
Expand Up @@ -27,7 +27,8 @@
"Croppie": "croppie#^2.3.0",
"jquery-dateFormat": "^1.0.2",
"angular": "^1.6.4",
"TheaterJS": "theaterjs#^3.1.0"
"TheaterJS": "theaterjs#^3.1.0",
"select2": "~4.0"
},
"devDependencies": {
"font-awesome": "^4.6.3",
Expand Down
119 changes: 118 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e7afaa7

Please sign in to comment.