Skip to content

Commit

Permalink
Merge pull request Silvanite#73 from SGS-Optimisation/main
Browse files Browse the repository at this point in the history
fix rule wysywig editor, add stats for pm
  • Loading branch information
yayann authored Jul 30, 2021
2 parents e471ecc + b061295 commit 802896c
Show file tree
Hide file tree
Showing 20 changed files with 15,071 additions and 54 deletions.
10 changes: 10 additions & 0 deletions app/Features/BaseFeature.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php


namespace App\Features;


abstract class BaseFeature
{
abstract public function handle();
}
76 changes: 76 additions & 0 deletions app/Features/Stats/RuleCreationPerClientAccount.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php


namespace App\Features\Stats;


use App\Features\BaseFeature;
use App\Models\ClientAccount;
use App\Models\Rule;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Query\Expression;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use Laravel\Nova\Metrics\Trend;
use Laravel\Nova\Metrics\TrendDateExpressionFactory;

class RuleCreationPerClientAccount extends Trend
{
public $dataset = [];

/**
* RuleCreationPerClientAccount constructor.
* @param int $range
* @param string $count
*/
public function __construct(
public ?string $count = self::BY_WEEKS,
public ?int $range = 24,
public ?string $column = 'created_at'
) {
parent::__construct();
}


public function handle()
{
foreach (ClientAccount::withCount('rules')->get() as $client_account) {
$this->dataset[$client_account->name] = [
'client_id' => $client_account->id,
'rules_count' => $client_account->rules_count,
'created_at' => $client_account->created_at->format('Y-m-d H:i:s'),
'trend' => $this->processClientAccount($client_account)->trend,
];
}

return $this->dataset;
}

public function processClientAccount($client_account)
{
$query = Rule::forClient($client_account);

$request = new Request();
$request->merge(['range' => $this->range, 'twelveHourTime' => false, 'timezone' => 'UTC']);

return $this->{'countBy'.Str::title(Str::plural($this->count))}($request, $query, $this->column);
}

public function ranges()
{
return [
5 => __('5 Weeks'),
10 => __('10 Weeks'),
15 => __('15 Weeks'),
24 => __('24 Weeks'),
];
}

public function name()
{
return 'Client Account Rules Per Week';
}

}
51 changes: 28 additions & 23 deletions app/Http/Controllers/PMs/RuleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ class RuleController extends Controller
/**
* TODO: Fix attachments not created in db
*
* @param Request $request
* @param Rule $rule
* @param Request $request
* @param Rule $rule
*/
protected function parseContent($request, $rule)
{
Expand All @@ -47,7 +47,7 @@ protected function parseContent($request, $rule)
}

if ($request->state && $rule->state != $request->state) {
logger('transitioning rule '.$rule->id.' to '.$request->state);
logger('transitioning rule ' . $rule->id . ' to ' . $request->state);
$rule->state->transitionTo($request->state, $request->user());
}
}
Expand All @@ -65,15 +65,15 @@ protected static function buildStates(Rule $rule)
if (!$transitionClass
|| (new $transitionClass($rule, auth()->user()))->canTransition()
) {
$shown_states[] = $state;
$shown_states[] = $state;
}
}

return $shown_states;
}

/**
* @param Request $request
* @param Request $request
* @param $client_account_slug
* @return \Inertia\Response
*/
Expand All @@ -100,7 +100,7 @@ public function index(Request $request, $client_account_slug)
/**
* Show the form for creating a new resource.
*
* @param Request $request
* @param Request $request
* @param $client_account_slug
* @return \Inertia\Response
*/
Expand All @@ -126,7 +126,7 @@ public function create(Request $request, $client_account_slug)
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \Illuminate\Http\Request $request
* @return Application|JsonResponse|RedirectResponse|Redirector
*/
public function store(CreateRuleRequest $request, $client_account_slug)
Expand All @@ -139,7 +139,7 @@ public function store(CreateRuleRequest $request, $client_account_slug)
$rule = $client_account->rules()->create($rule_fields);
$this->parseContent($request, $rule);

logger('rule added: '.$rule->id);
logger('rule added: ' . $rule->id);
event(new Updated($rule));

$request->session()->flash('success', 'Rule successfully created!');
Expand All @@ -153,7 +153,7 @@ public function store(CreateRuleRequest $request, $client_account_slug)
/**
* Display the specified resource.
*
* @param int $id
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
Expand All @@ -164,9 +164,9 @@ public function show($id)
/**
* Show the form for editing the specified resource.
*
* @param Request $request
* @param Request $request
* @param $client_account_slug
* @param int $id
* @param int $id
* @return \Inertia\Response
*/
public function edit(Request $request, $client_account_slug, $id)
Expand All @@ -182,14 +182,19 @@ public function edit(Request $request, $client_account_slug, $id)
'<div>&nbsp;</div>',
'<p></p>',
'<p><br></p>',
'<ul>',
'</ul>',
'<li>',
'</li>',
'</span>',
],
['', '', '', '', '', '', '', '', '', '</span><br/>'],
strip_tags($rule->content, '<p><img><a><span><br><i><strong><b>')
['', '', '', '', '', '</span><br/>'],
strip_tags(
$rule->content,
[
'p', 'img', 'a', 'span', 'br',
'i', 'strong', 'b', 'u', 's',
'ul', 'ol', 'li',
'blockquote', 'pre',
'h1', 'h2', 'h3', 'h4', 'h5', 'h6',
]
)
);


Expand All @@ -207,8 +212,8 @@ public function edit(Request $request, $client_account_slug, $id)
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @param \Illuminate\Http\Request $request
* @param int $id
* @return JsonResponse|RedirectResponse
*/
public function update(Request $request, $client_account_slug, $id)
Expand All @@ -230,9 +235,9 @@ public function update(Request $request, $client_account_slug, $id)
/**
* Remove the specified resource from storage.
*
* @param Request $request
* @param Request $request
* @param $client_account_slug
* @param int $id
* @param int $id
* @return JsonResponse|RedirectResponse
* @throws \Exception
*/
Expand All @@ -252,9 +257,9 @@ public function destroy(Request $request, $client_account_slug, $id)
/**
* Restore deleted resource
*
* @param Request $request
* @param Request $request
* @param $client_account_slug
* @param int $id
* @param int $id
* @return JsonResponse|RedirectResponse
* @throws \Exception
*/
Expand Down
28 changes: 28 additions & 0 deletions app/Http/Controllers/Stats/StatsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace App\Http\Controllers\Stats;

use App\Features\Stats\RuleCreationPerClientAccount;
use App\Http\Controllers\Controller;
use App\Models\ClientAccount;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Laravel\Jetstream\Jetstream;

class StatsController extends Controller
{
public function index(Request $request)
{
$count = $request->get('count', 'week');
$range = $request->get('range', 24);
$column = $request->get('column', 'created_at');


return Jetstream::inertia()->render($request, 'Stats/ClientAccountStats', [
'stats' => (new RuleCreationPerClientAccount($count, $range, $column))->handle(),
'count' => Str::title($count),
'range' => $range,
'column' => $column,
]);
}
}
6 changes: 6 additions & 0 deletions app/Models/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
* @method static Builder|Rule isPublished()
* @method static Builder|Rule whereNotState(string $column, $states)
* @method static Builder|Rule whereState($value)
* @method static Builder|Rule forClient(\App\Models\ClientAccount $clientAccount)
*/
class Rule extends Model implements Auditable
{
Expand Down Expand Up @@ -98,6 +99,11 @@ public function scopeIsOmnipresent(Builder $query)
});
}

public function scopeForClient(Builder $query, ClientAccount $clientAccount)
{
return $query->where('client_account_id', $clientAccount->id);
}

public function scopeIsFlagged(Builder $query)
{
return $query->where('flagged', true);
Expand Down
44 changes: 44 additions & 0 deletions app/Nova/Dashboards/ClientDashboard.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace App\Nova\Dashboards;

use App\Models\ClientAccount;
use App\Nova\Metrics\RulesPerWeek;
use Laravel\Nova\Dashboard;

class ClientDashboard extends Dashboard
{
public ClientAccount $client_account;

/**
* ClientDashboard constructor.
* @param ClientAccount $client_account
*/
public function __construct(ClientAccount $client_account)
{
$this->client_account = $client_account;
parent::__construct();
}

/**
* Get the cards for the dashboard.
*
* @return array
*/
public function cards()
{
return [
new RulesPerWeek($this->client_account),
];
}

/**
* Get the URI key for the dashboard.
*
* @return string
*/
public static function uriKey()
{
return 'client-dashboard';
}
}
23 changes: 22 additions & 1 deletion app/Nova/Metrics/RulesPerWeek.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,32 @@

namespace App\Nova\Metrics;

use App\Models\ClientAccount;
use App\Models\Rule;
use Laravel\Nova\Http\Requests\NovaRequest;
use Laravel\Nova\Metrics\Trend;

class RulesPerWeek extends Trend
{

public ClientAccount $client_account;

/**
* RulesPerWeek constructor.
* @param ClientAccount $client_account
*/
public function __construct(ClientAccount $client_account)
{
$this->client_account = $client_account;
parent::__construct();
}


public function name()
{
return $this->client_account->name . ' Rules Per Week';
}

/**
* Calculate the value of the metric.
*
Expand All @@ -16,7 +36,8 @@ class RulesPerWeek extends Trend
*/
public function calculate(NovaRequest $request)
{
return $this->countByWeeks($request, Rule::class);
logger(print_r($request->all(), true));
return $this->countByWeeks($request, Rule::forClient($this->client_account));
}

/**
Expand Down
1 change: 1 addition & 0 deletions app/Providers/AuthServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public function boot()
'createRules', 'updateRules', 'deleteRules', 'publishRules',
'createClientAccounts', 'updateClientAccounts', 'deleteClientAccounts',
'accessPM',
'accessStats',
'accessBackend',
'viewTaxonomies', 'manageTaxonomies',
'viewTerms', 'manageTerms',
Expand Down
Loading

0 comments on commit 802896c

Please sign in to comment.