Skip to content

Commit

Permalink
added custom theme each post, category, group, product
Browse files Browse the repository at this point in the history
  • Loading branch information
farzady committed Oct 2, 2024
1 parent a75eda9 commit 3d7a03e
Show file tree
Hide file tree
Showing 94 changed files with 672 additions and 446 deletions.
60 changes: 42 additions & 18 deletions app/Helpers/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -757,12 +757,19 @@ function hasPart($areaName)
/**
* get parts of area
* @param $areaName
* @param null $custom custom theme
* @return Part[]|\Illuminate\Database\Eloquent\Collection|\LaravelIdea\Helper\App\Models\_IH_Part_C
*/
function getParts($areaName)
function getParts($areaName, $custom = null)
{
$a = Area::where('name', $areaName)->first();
return $a->parts()->orderBy('sort')->get();
if ($custom != null) {

$customs = Part::where('custom', $custom)->orderBy('sort');
if ($customs->count() > 0) {
return $customs->get();
}
}
return Area::where('name', $areaName)->first()->parts()->orderBy('sort')->get();
}


Expand Down Expand Up @@ -1278,7 +1285,8 @@ function sendingSMS($text, $number, $args)
* @param $html
* @return array
*/
function generateTOC($html) {
function generateTOC($html)
{
// Load HTML into a DOMDocument for parsing
$doc = new DOMDocument();
@$doc->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'));
Expand Down Expand Up @@ -1333,7 +1341,8 @@ function generateTOC($html) {
* @param $counter
* @return string
*/
function generateHeadingID($text, $counter) {
function generateHeadingID($text, $counter)
{
// Convert to lowercase and replace non-alphanumeric characters with dashes
$id = strtolower(preg_replace('/[^a-zA-Z0-9]+/', '-', $text));

Expand All @@ -1352,7 +1361,8 @@ function generateHeadingID($text, $counter) {
}

// The buildTOC function remains unchanged
function buildTOC($items) {
function buildTOC($items)
{
$html = '<ul>';
foreach ($items as $item) {
$html .= '<li>';
Expand All @@ -1376,21 +1386,35 @@ function buildTOC($items) {
* @param $evaluation
* @return int|mixed
*/
function detectRateCustomer($type,$id,$evaluation)
function detectRateCustomer($type, $id, $evaluation)
{
if (!auth('customer')->check()){
if (!auth('customer')->check()) {
return 0;
}
$rate = Rate::where('rater_id',auth('customer')->id())
->where('rater_type', \App\Models\Customer::class)
->where('rateable_type',$type)
->where('rateable_id',$id)
->where('evaluation_id',$evaluation);

if ($rate->count() == 0){
return 0;
}else{
return $rate->first()->rate;
$rate = Rate::where('rater_id', auth('customer')->id())
->where('rater_type', \App\Models\Customer::class)
->where('rateable_type', $type)
->where('rateable_id', $id)
->where('evaluation_id', $evaluation);

if ($rate->count() == 0) {
return 0;
} else {
return $rate->first()->rate;
}

}

/**
* @param $name string area name
* @param $model \Illuminate\Database\Eloquent\Model $custom model
* @return Area|mixed
*/
function findArea($name,$model = null)
{

if ($model != null && $model->theme != null){
return json_decode($model->theme);
}
return \App\Models\Area::where('name', $name)->first();
}
121 changes: 118 additions & 3 deletions app/Http/Controllers/Admin/AreaController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@

use App\Http\Controllers\Controller;
use App\Models\Area;
use App\Models\Category;
use App\Models\Group;
use App\Models\Part;
use App\Models\Post;
use App\Models\Product;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\File;

Expand All @@ -17,7 +21,7 @@ public function index()
return view('admin.areas.area-list', compact('areas'));
}

public function desgin(Area $area)
public function design(Area $area)
{

$valids = [];
Expand All @@ -38,6 +42,53 @@ public function desgin(Area $area)
return view('admin.areas.area-design', compact('area', 'valids'));
}

public function designModel(Area $area, $model, $id)
{

switch ($model) {
case 'Group':
$m = Group::whereId($id)->first();
break;
case 'Category':
$m = Category::whereId($id)->first();
break;
case 'Post':
$m = Post::whereId($id)->first();
break;
case 'Product':
$m = Product::whereId($id)->first();
break;
default:
return abort(404);
}

$valids = [];
foreach ($area->segment as $seg) {
if (File::exists(resource_path() . '/views/segments/' . $seg)) {
$dirs = File::directories(resource_path() . '/views/segments/' . $seg);
foreach ($dirs as $dir) {
$temp = explode('/', $dir);
$valids[] = [
'segment' => $temp[count($temp) - 2],
'part' => $temp[count($temp) - 1],
'data' => json_decode(file_get_contents($dir . '/' . $temp[count($temp) - 1] . '.json'), true)
];
}
}
}

$parts = $area->parts;
foreach ($parts as $part) {
$part->id = null;
}
if ($m->theme == null) {
$data = ['parts' => $parts, 'use_default' => $area->use_default,'max' => 10];
} else {
$data = json_decode($m->theme, true);
}
return view('admin.areas.model-design', compact('m', 'valids', 'data', 'model'));
}

/**
* screenshot segment
* @param $segment
Expand Down Expand Up @@ -80,9 +131,9 @@ public function update(Request $request, Area $area)

logAdmin(__METHOD__, __CLASS__, $area->id);

if ($request->has('use_default')){
if ($request->has('use_default')) {
$area->use_default = 1;
}else{
} else {
$area->use_default = 0;
}
$area->save();
Expand All @@ -91,6 +142,70 @@ public function update(Request $request, Area $area)
return redirect()->back()->with(['message' => __('area :NAME of website updated', ['NAME' => $area->name])]);
}

public function updateModel(Request $request, $model, $id)
{

// return $request->all();
switch ($model) {
case 'Group':
$m = Group::whereId($id)->first();
break;
case 'Category':
$m = Category::whereId($id)->first();
break;
case 'Post':
$m = Post::whereId($id)->first();
break;
case 'Product':
$m = Product::whereId($id)->first();
break;
default:
return abort(404);
}


foreach ($request->input('parts', []) as $i => $item) {
$data = json_decode($item);
if ($data == null) {
continue;
}
if ($data->id == null) {
// create
$part = new Part();
$part->area_id = null;
$part->segment = $data->segment;
$part->part = $data->part;
$part->sort = $i;
$part->custom = $model.$m->id;
$part->save();
} else {
$part = Part::whereId($data->id)->first();
$part->segment = $data->segment;
$part->part = $data->part;
$part->sort = $i;
$part->save();
}
}
foreach (json_decode($request->input('removed')) as $id) {
Part::where('id', $id)->first()->delete();
}
\Artisan::call('client');

logAdmin(__METHOD__, __CLASS__, $m->id);

$m->theme = [
'parts' => Part::where('custom',$model.$m->id)->get(),
'use_default' => ($request->has('use_default')),
'max' => 10,
];

$m->save();


return redirect()->back()->with(['message' => __('area :NAME of website updated', ['NAME' => $model.$m->id ])]);

}

public function sort(Area $area)
{
return view('admin.areas.area-sort', compact('area'));
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/ClientController.php
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ public function product($slug)
$breadcrumb[$product->category->parent->name] = $product->category->parent->webUrl();
}
$breadcrumb[$product->name] = null;
return view('client.default-list', compact('area', 'product', 'title', 'subtitle', 'breadcrumb'));
return view('client.product', compact('area', 'product', 'title', 'subtitle', 'breadcrumb'));
}

public function category($slug, Request $request)
Expand Down
9 changes: 7 additions & 2 deletions app/Models/Part.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,13 @@ public function getBladeWithData($item = null)
return ['blade' => 'segments.' . $this->segment . '.' . $this->part . '.' . $this->part, 'data' => $handle::onMount($this, $item)];
}

public function area()
{

public function area(){
return $this->belongsTo(Area::class);
}
public function getAreaNameAttribute()
{
return $this->area_id ? $this->area->name : $this->custom;
}

}
3 changes: 2 additions & 1 deletion database/migrations/2024_07_04_053952_create_parts_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ public function up(): void
{
Schema::create('parts', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('area_id');
$table->unsignedBigInteger('area_id')->nullable();
$table->string('segment');
$table->string('part');
$table->json('data')->default('[]');
$table->integer('sort')->default(0);
$table->string('custom')->nullable();
$table->timestamps();
});
}
Expand Down
3 changes: 3 additions & 0 deletions resources/sass/panel/_common.scss
Original file line number Diff line number Diff line change
Expand Up @@ -335,3 +335,6 @@ a.btn,a.action-btn,a.circle-btn{
}
}

.btn-light,a.btn-light{
color: black !important;
}
52 changes: 52 additions & 0 deletions resources/views/admin/areas/model-design.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
@extends('layouts.app')

@section('title')
{{__("Design :AREA",['AREA' => $model . ' [' . $m->id.']'])}}
@endsection

@section('content')

@include('components.err')
<form action="{{route('admin.area.update.model',[$model,$m->id])}}" method="post">
@csrf
<div class="general-form mb-5">
<h1>
{{__("Design :AREA",['AREA' => $model . ' [' . $m->id.']'])}}
</h1>


<div class="form-group p-3">

<div class="form-check form-switch">
<input value="1" class="form-check-input @error('use_default') is-invalid @enderror" name="use_default" @if( $data['use_default']) checked @endif type="checkbox" id="use_default">
<label class="form-check-label" for="use_default"> {{__('Use default')}}</label>
</div>
</div>
<area-designer
image-link="{{route('admin.area.image',['',''])}}"
:parts='@json($data['parts'])'
:valids='@json($valids)'
:area='@json($data)'
></area-designer>
{{-- <div class="row">--}}
{{-- @foreach($valids as $valid)--}}
{{-- <div class="col-md-3">--}}
{{-- <img class="img-fluid" src="{{route('admin.area.image',[$valid['segment'],$valid['part']])}}" alt="{{$valid['segment'].'.'.$valid['part']}}">--}}
{{-- <h5 class="mt-2 text-center">--}}
{{-- {{$valid['data']['name']}} [v{{$valid['data']['version']}}]--}}
{{-- </h5>--}}
{{-- </div>--}}
{{-- @endforeach--}}
{{-- </div>--}}
</div>
<button
class="action-btn circle-btn"
data-bs-toggle="tooltip"
data-bs-placement="top"
data-bs-custom-class="custom-tooltip"
data-bs-title="{{__("Save")}}"
>
<i class="ri-save-2-line"></i>
</button>
</form>
@endsection
2 changes: 1 addition & 1 deletion resources/views/admin/groups/group-form.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class="form-control">

<h1>
@if(isset($item))
{{__("Edit group")}} [{{$item->name}}]
{{__("Edit group")}} [{{$item->name}}] <a href="{{route('admin.area.design.model',['group','Group',$item->id])}}" class="btn btn-secondary"> <i class="ri-palette-line"></i> </a>
@else
{{__("Add new group")}}
@endif
Expand Down
6 changes: 3 additions & 3 deletions resources/views/client/category.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@
@endphp
@section('content')
<main>
@if(\App\Models\Area::where('name',$area)->first()->use_default)
@if(findArea($area,$category)->use_default)
@foreach(getParts('defaultHeader') as $part)
@php($p = $part->getBladeWithData())
@include($p['blade'],['data' => $p['data']])
@endforeach
@endif
@foreach(getParts($area) as $part)
@foreach(getParts($area,$category) as $part)
@php($p = $part->getBladeWithData())
@include($p['blade'],['data' => $p['data']])
@endforeach
@if(\App\Models\Area::where('name',$area)->first()->use_default)
@if(findArea($area,$category)->use_default)
@foreach(getParts('defaultFooter') as $part)
@php($p = $part->getBladeWithData())
@include($p['blade'],['data' => $p['data']])
Expand Down
Loading

0 comments on commit 3d7a03e

Please sign in to comment.