Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Templates #183

Merged
merged 11 commits into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
290 changes: 223 additions & 67 deletions app/Console/Commands/GenerateTemplate.php

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion app/Http/Controllers/Forms/AiFormController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace App\Http\Controllers\Forms;

use App\Console\Commands\GenerateTemplate;
use App\Http\Controllers\Controller;
use App\Http\Requests\AiGenerateFormRequest;
use App\Models\Forms\AI\AiFormCompletion;
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Controllers/SitemapController.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ private function createUrl($url, $priority, $frequency = 'daily')

private function addTemplatesUrls(Sitemap $sitemap)
{
Template::chunk(100, function ($templates) use ($sitemap) {
Template::where('publicly_listed', true)->chunk(100, function ($templates) use ($sitemap) {
foreach ($templates as $template) {
$sitemap->add($this->createUrl('/templates/' . $template->slug, 0.7));
$sitemap->add($this->createUrl('/form-templates/' . $template->slug, 0.8));
}
});
}
Expand Down
56 changes: 49 additions & 7 deletions app/Http/Controllers/TemplateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,28 @@
namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Http\Requests\Templates\CreateTemplateRequest;
use App\Http\Resources\TemplateResource;
use Illuminate\Http\Request;
use App\Http\Requests\Templates\FormTemplateRequest;
use App\Http\Resources\FormTemplateResource;
use App\Models\Template;
use Illuminate\Http\Request;

class TemplateController extends Controller
{
public function index()
public function index(Request $request)
{
return TemplateResource::collection(Template::all());
$limit = null;
if ($request->offsetExists('limit') && $request->get('limit') > 0) {
$limit = (int) $request->get('limit');
}
return FormTemplateResource::collection(
Template::where('publicly_listed', true)
->orderByDesc('created_at')
->limit($limit)
->get()
);
}

public function create(CreateTemplateRequest $request)
public function create(FormTemplateRequest $request)
{
$this->authorize('create', Template::class);

Expand All @@ -24,8 +33,41 @@ public function create(CreateTemplateRequest $request)
$template->save();

return $this->success([
'message' => 'Template created.',
'message' => 'Template was created.',
'template_id' => $template->id
]);
}

public function update(FormTemplateRequest $request, string $id)
{
$template = Template::findOrFail($id);
$this->authorize('update', $template);

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

return $this->success([
'message' => 'Template was updated.',
'template_id' => $template->id,
'data' => new FormTemplateResource($template)
]);
}

public function destroy($id)
{
$template = Template::findOrFail($id);
$this->authorize('delete', $template);

$template->delete();

return $this->success([
'message' => 'Template was deleted.',
]);
}

public function show(string $slug)
{
return new FormTemplateResource(
Template::whereSlug($slug)->firstOrFail()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use App\Models\Template;
use Illuminate\Foundation\Http\FormRequest;

class CreateTemplateRequest extends FormRequest
class FormTemplateRequest extends FormRequest
{
const IGNORED_KEYS = [
'id',
Expand Down Expand Up @@ -48,12 +48,21 @@ class CreateTemplateRequest extends FormRequest
*/
public function rules()
{
$slugRule = '';
if($this->id){
$slugRule = ','.$this->id;
}
return [
'form' => 'required|array',
'publicly_listed' => 'boolean',
'name' => 'required|string|max:60',
'slug' => 'required|string|unique:templates',
'slug' => 'required|string|alpha_dash|unique:templates,slug'.$slugRule,
'short_description' => 'required|string|max:1000',
'description' => 'required|string|max:2000',
'image_url' => 'required|string',
'types' => 'nullable|array',
'industries' => 'nullable|array',
'related_templates' => 'nullable|array',
'questions' => 'array',
];
}
Expand All @@ -66,12 +75,18 @@ public function getTemplate(): Template
unset($structure[$key]);
}
}

return new Template([
'publicly_listed' => $this->publicly_listed,
'name' => $this->name,
'slug' => $this->slug,
'short_description' => $this->short_description,
'description' => $this->description,
'image_url' => $this->image_url,
'structure' => $structure,
'types' => $this->types ?? [],
'industries' => $this->industries ?? [],
'related_templates' => $this->related_templates ?? [],
'questions' => $this->questions ?? []
]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

namespace App\Http\Resources;

use App\Http\Requests\Templates\CreateTemplateRequest;
use Illuminate\Http\Resources\Json\JsonResource;

class TemplateResource extends JsonResource
class FormTemplateResource extends JsonResource
{
/**
* Transform the resource into an array.
Expand All @@ -15,10 +14,8 @@ class TemplateResource extends JsonResource
*/
public function toArray($request)
{
$data = parent::toArray($request);
foreach (CreateTemplateRequest::IGNORED_KEYS as $key) {
unset($data[$key]);
}
return $data;
return array_merge(parent::toArray($request), [
'is_new' => $this->created_at->isAfter(now()->subDays(7))
]);
}
}
56 changes: 56 additions & 0 deletions app/Models/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Spatie\Sluggable\HasSlug;
use Spatie\Sluggable\SlugOptions;
use Stevebauman\Purify\Facades\Purify;
Expand All @@ -16,14 +17,28 @@ class Template extends Model
'name',
'slug',
'description',
'short_description',
'image_url',
'structure',
'questions',
'publicly_listed',
'industries',
'types',
'related_templates'
];

protected $casts = [
'structure' => 'array',
'questions' => 'array',
'industries' => 'array',
'types' => 'array',
'related_templates' => 'array',
'created_at' => 'datetime',
'updated_at' => 'datetime',
];

protected $attributes = [
'publicly_listed' => false,
];

public function setDescriptionAttribute($value)
Expand All @@ -32,6 +47,11 @@ public function setDescriptionAttribute($value)
$this->attributes['description'] = Purify::clean($value);
}

public function scopePubliclyListed($query)
{
return $this->where('publicly_listed', true);
}

/**
* Config/options
*/
Expand All @@ -42,4 +62,40 @@ public function getSlugOptions(): SlugOptions
->generateSlugsFrom('name')
->saveSlugsTo('slug');
}

public function getTypes(): Collection
{
return self::getAllTypes()->filter(function ($type) {
return in_array($type['slug'], $this->types);
});
}

public function getIndustries(): Collection
{
return self::getAllIndustries()->filter(function ($type) {
return in_array($type['slug'], $this->industries);
});
}

public static function getAllTypes(): Collection
{
return collect(
array_values(
json_decode(
file_get_contents(resource_path('data/forms/templates/types.json')),
true)
)
)->values();
}

public static function getAllIndustries(): Collection
{
return collect(
array_values(
json_decode(
file_get_contents(resource_path('data/forms/templates/industries.json')),
true)
)
)->values();
}
}
26 changes: 25 additions & 1 deletion app/Policies/TemplatePolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,30 @@ class TemplatePolicy
*/
public function create(User $user)
{
return $user->template_editor;
return $user->admin || $user->template_editor;
}

/**
* Determine whether the user can update the model.
*
* @param \App\Models\User $user
* @param \App\Models\Template $template
* @return mixed
*/
public function update(User $user, Template $template)
{
return $user->admin || $user->template_editor;
}

/**
* Determine whether the user can delete the model.
*
* @param \App\Models\User $user
* @param \App\Models\Template $template
* @return mixed
*/
public function delete(User $user, Template $template)
{
return $user->admin || $user->template_editor;
}
}
Loading