Skip to content

Commit

Permalink
[ADD] Product content.
Browse files Browse the repository at this point in the history
  • Loading branch information
Seiger committed Jan 8, 2024
1 parent 2d78da9 commit e98a437
Show file tree
Hide file tree
Showing 22 changed files with 728 additions and 58 deletions.
8 changes: 8 additions & 0 deletions builder/richtext/config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php return [
'active' => 1,
'title' => 'RichText',
'type' => 'richtext',
'id' => 'richtext',
'order' => 1,
'script' => '',
];
1 change: 1 addition & 0 deletions builder/richtext/render.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{!!$value ?? ''!!}
1 change: 1 addition & 0 deletions builder/richtext/template.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<textarea id="{{$id ?? ''}}" name="builder[{{$i ?? '9999'}}][richtext]" rows="3" onchange="documentDirty=true;">{!!$value ?? ''!!}</textarea>
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use EvolutionCMS\Models\SiteTemplate;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Query\Expression;
Expand All @@ -24,8 +25,9 @@ public function up(): void
$table->unsignedInteger('category')->default(0)->index()->comment('Resource ID as Category');
$table->string('sku')->index()->comment('It is the SKU Product code');
$table->string('alias', 512)->index()->comment('It using for generate url');
$table->unsignedInteger('position')->default(0)->comment('Position the product in list');
$table->unsignedInteger('views')->default(0)->comment('Count view the product');
$table->unsignedInteger('position')->default(0)->index()->comment('Position the product in list');
$table->unsignedInteger('views')->default(0)->index()->comment('Count view the product');
$table->unsignedInteger('rating')->default(5)->index()->comment('Rating the product base on votes');
$table->integer('quantity')->default(0)->comment('Quantity products in stock');
$table->unsignedDecimal('price_regular', 9, 2)->default(0);
$table->unsignedDecimal('price_special', 9, 2)->default(0);
Expand Down Expand Up @@ -62,8 +64,22 @@ public function up(): void
Schema::create('s_product_category', function (Blueprint $table) {
$table->foreignId('product')->comment('Product ID')->constrained('s_products')->cascadeOnDelete();
$table->unsignedInteger('category')->default(0)->index()->comment('Resource ID as Category');
$table->timestamps();
});

/*
|--------------------------------------------------------------------------
| Create a Product template
|--------------------------------------------------------------------------
*/
$templateProduct = SiteTemplate::whereTemplatealias('s_commerce_product')->first();
if (!$templateProduct) {
$templateProduct = new SiteTemplate();
$templateProduct->templatealias = 's_commerce_product';
$templateProduct->templatename = 'sCommerce Product';
$templateProduct->description = 'Template for sCommerce Product';
$templateProduct->icon = 'fa fa-store';
$templateProduct->save();
}
}

/**
Expand All @@ -79,5 +95,12 @@ public function down(): void
Schema::dropIfExists('s_product_category');
Schema::dropIfExists('s_product_translates');
Schema::dropIfExists('s_products');

/*
|--------------------------------------------------------------------------
| Delete a Product template
|--------------------------------------------------------------------------
*/
$templateProduct = SiteTemplate::whereTemplatealias('s_commerce_product')->delete();
}
};
11 changes: 11 additions & 0 deletions lang/en/global.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,15 @@
'product_link_rule_root' => 'from the root of the site',
'product_link_rule_catalog' => 'from the root of catalog directory',
'product_link_rule_category' => 'from the product category',
'seodescription' => 'SEO Description',
'seodescription_help' => 'Enter an SEO Description for the details page.',
'seorobots' => 'SEO Robot',
'seorobots_help' => 'Adjust the visibility of the detail page to search bots.',
'seotitle' => 'SEO Title',
'seotitle_help' => 'Enter an SEO Title for the details page.',
'add_block' => 'Add a block',
'confirm_delete' => 'Confirm deletion',
'you_sure' => 'Are you sure you want to delete the entry',
'with_id' => 'with ID',
'deleted' => 'Record deleted.',
];
11 changes: 11 additions & 0 deletions lang/ru/global.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,15 @@
'product_link_rule_root' => 'от корня сайта',
'product_link_rule_catalog' => 'от корня каталога',
'product_link_rule_category' => 'от категории товара',
'seodescription' => 'SEO Описание',
'seodescription_help' => 'Введите SEO Описание для страницы деталей.',
'seorobots' => 'SEO Робот',
'seorobots_help' => 'Установите видимость страницы деталей для поисковых ботов.',
'seotitle' => 'SEO Заголовок',
'seotitle_help' => 'Введите SEO Заголовок для страницы деталей.',
'add_block' => 'Добавить блок',
'confirm_delete' => 'Подтвердить удаление',
'you_sure' => 'Вы уверены, что хотите удалить запись',
'with_id' => 'с ID',
'deleted' => 'Запись удалено.',
];
11 changes: 11 additions & 0 deletions lang/uk/global.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,15 @@
'product_link_rule_root' => 'від корня сайту',
'product_link_rule_catalog' => 'від корня каталогу',
'product_link_rule_category' => 'від категорії товару',
'seodescription' => 'SEO Опис',
'seodescription_help' => 'Введіть SEO Опис для сторінки деталей.',
'seorobots' => 'SEO Робот',
'seorobots_help' => 'Налаштуйте видимість сторінки деталей для пошукових ботів.',
'seotitle' => 'SEO Заголовок',
'seotitle_help' => 'Введіть SEO Заголовок для сторінки деталей.',
'add_block' => 'Додати блок',
'confirm_delete' => 'Підтвердити видалення',
'you_sure' => 'Ви впевнені, що хочете видалити запис',
'with_id' => 'з ID',
'deleted' => 'Запис видалено.',
];
168 changes: 160 additions & 8 deletions module/sCommerceModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
$sCommerceController = new sCommerceController();
Paginator::defaultView('sCommerce::partials.pagination');
$get = request()->get ?? (sCommerce::config('basic.orders_on', 1) == 1 ? "orders" : "products");
$editor = '';
$iUrl = (int)request()->input('i', 0) > 0 ? '&i=' . (int)request()->input('i', 0) : '';
$editor = [];

$tabs = ['products'];
if (evo()->hasPermission('settings')) {
Expand All @@ -45,7 +46,10 @@
$order = request()->input('order', 'id');
$direc = request()->input('direc', 'desc');

$data['items'] = sProduct::orderBy($order, $direc)->paginate($perpage);
$data['items'] = sProduct::lang($sCommerceController->langDefault())->orderBy($order, $direc)->paginate($perpage);
$data['total'] = sProduct::count();
$data['active'] = sProduct::wherePublished(1)->count();
$data['disactive'] = $data['total'] - $data['active'];
break;
/*
|--------------------------------------------------------------------------
Expand All @@ -54,10 +58,10 @@
*/
case "product":
$tabs = ['product', 'content'];
$iUrl = trim($iUrl) ?: '&i=0';
$product = sCommerce::getProduct((int)request()->input('i', 0));
//dd($product);
$data['categories'] = [];
$data['product'] = $product;
$data['item'] = $product;
break;
case "productSave":
$requestId = (int)request()->input('i', 0);
Expand All @@ -68,12 +72,12 @@
$type = $product->type ?: 'simple';
$cover = sGallery::first('product', $requestId);

if (empty($alias)) {
if (empty($alias) || str_starts_with($alias, 'new-product')) {
$translate = sProductTranslate::whereProduct($requestId)
->whereIn('lang', ['en', $sCommerceController->langDefault()])->orderByRaw('FIELD(lang, "en", "' . $sCommerceController->langDefault() . '")')
->first();
if ($translate) {
$alias = $translate->pagetitle;
$alias = trim($translate->pagetitle) ?: 'new-product';
} else {
$alias = 'new-product';
}
Expand Down Expand Up @@ -102,7 +106,7 @@
$product->price_opt_regular = $sCommerceController->validatePrice(request()->input('price_opt_regular', 0));
$product->price_opt_special = $sCommerceController->validatePrice(request()->input('price_opt_special', 0));
$product->weight = (float)request()->input('weight', 0);
$product->cover = $cover->src ?? '/assets/site/noimage.png';
$product->cover = str_replace(MODX_SITE_URL, '', $cover->src ?? '/assets/site/noimage.png');
$product->relevants = json_encode(request()->input('relevants', []));
$product->similar = json_encode(request()->input('similar', []));
$product->tmplvars = json_encode(request()->input('tmplvars', []));
Expand All @@ -119,7 +123,154 @@
$sCommerceController->setProductsListing();
$back = str_replace('&i=0', '&i=' . $product->id, (request()->back ?? '&get=product'));
return header('Location: ' . sCommerce::moduleUrl() . $back);
case "productDelete":
$product = sCommerce::getProduct((int)request()->input('i', 0));

if ($product) {
$sCommerceController->removeDirRecursive(MODX_BASE_PATH . 'assets/sgallery/product/' . $product->id);

$product->categories()->sync([]);
$product->texts()->delete();
$product->delete();
}

$sCommerceController->setProductsListing();
$back = '&get=products';
return header('Location: ' . sCommerce::moduleUrl() . $back);
case "content":
$tabs = ['product', 'content'];
$iUrl = trim($iUrl) ?: '&i=0';
$requestId = (int)request()->input('i', 0);
$requestLang = request()->input('lang');
$content = sProductTranslate::whereProduct($requestId)->whereLang($requestLang)->firstOrNew();
$content->id = $content->product ?? $requestId;

$buttons = [];
$elements = [];
$templates = [];
$richtexts = [];
$fields = glob(MODX_BASE_PATH . 'assets/modules/scommerce/builder/*/config.php');
View::getFinder()->setPaths([MODX_BASE_PATH . 'assets/modules/scommerce/builder']);

if (count($fields)) {
foreach ($fields as $idx => $field) {
$template = str_replace('config.php', 'template.blade.php', $field);

if (is_file($template)) {
$template = basename(dirname($template));
$field = require $field;

if ((int)$field['active']) {
$id = $field['id'];
$templates[$id] = $template;
$order = ($field['order'] ?? ($idx + 25));
while (isset($buttons[$order])) {
$order++;
}
$buttons[$order] = $sCommerceController->view('partials.addBlockButton', compact(['id', 'field']))->render();
$elements[] = view($template . '.template', compact(['id']))->render();
if (strtolower($field['type']) == 'richtext') {
$richtexts[$id] = [];
}
}
}
}
}
ksort($buttons);

$chunks = [];
$builder = data_is_json($content->builder ?? '', true);
if (is_array($builder) && count($builder)) {
foreach ($builder as $i => $item) {
$key = array_key_first($item);
if (isset($templates[$key])) {
$id = $key . $i;
$value = $item[$key];
$chunks[] = view($template . '.template', compact(['id', 'value']))->render();
$richtexts[$key][] = $i;
}
}
}

foreach ($richtexts as $key => $items) {
if(count($items)) {
$start = last($items);
} else {
$start = 1;
}

foreach (range($start, 10) as $y) {
$items[] = $y;
}

foreach ($items as $item) {
$editor[] = $key . $item;
}
}

$data['item'] = $content;
$data['buttons'] = $buttons;
$data['elements'] = $elements;
$data['chunks'] = $chunks;
break;
case "contentSave":
$requestId = (int)request()->input('i', 0);
$requestLang = request()->input('lang');
$contentField = '';
$renders = [];
$fields = glob(MODX_BASE_PATH . 'assets/modules/scommerce/builder/*/config.php');
View::getFinder()->setPaths([MODX_BASE_PATH . 'assets/modules/scommerce/builder']);

if (count($fields)) {
foreach ($fields as $field) {
$render = str_replace('config.php', 'render.blade.php', $field);
if (is_file($render)) {
$render = basename(dirname($render));
$field = require $field;
$id = $field['id'];
$renders[$id] = $render;
}
}
}

$contentBuilder = request()->input('builder', '');
if (is_array($contentBuilder) && count($contentBuilder)) {
foreach ($contentBuilder as $position => $item) {
$id = array_key_first($item);
if (isset($renders[$id])) {
$value = $item[$id];
$contentField .= view($renders[$id] . '.render', compact(['id', 'value']))->render();
}
}
}
$contentField = str_replace([chr(9), chr(10), chr(13), ' '], '', $contentField);

$content = sProductTranslate::whereProduct($requestId)->whereLang($requestLang)->firstOrNew();
$content->pagetitle = request()->input('pagetitle', '');
$content->longtitle = request()->input('longtitle', '');
$content->introtext = request()->input('introtext', '');
$content->content = $contentField;
$content->seotitle = request()->input('seotitle', '');
$content->seodescription = request()->input('seodescription', '');
$content->seorobots = request()->input('seorobots', '');
$content->builder = json_encode(array_values(request()->input('builder', [])));
$content->constructor = json_encode(request()->input('constructor', []));
if (($content->product ?? 0) == 0) {
$product = sCommerce::getProduct($requestId);
if (!$product->id) {
$product->alias = $sCommerceController->validateAlias(trim($content->pagetitle) ?: 'new-product', $requestId);
$product->save();
}
$content->product = $product->id;
}
if (!$content->tid) {
$content->lang = $requestLang;
}
$content->save();

$sCommerceController->setProductsListing();
$back = str_replace('&i=0', '&i=' . $content->product, (request()->back ?? '&get=product'));
return header('Location: ' . sCommerce::moduleUrl() . $back);
/*
|--------------------------------------------------------------------------
| Settings
Expand All @@ -142,9 +293,10 @@
}

$data['sCommerceController'] = $sCommerceController;
$data['editor'] = $editor;
$data['editor'] = $sCommerceController->textEditor(implode(',', $editor));
$data['tabs'] = $tabs;
$data['get'] = $get;
$data['iUrl'] = $iUrl;
$data['moduleUrl'] = sCommerce::moduleUrl();

echo $sCommerceController->view('index', $data);
Loading

0 comments on commit e98a437

Please sign in to comment.