improved field size of url_path in DB, improved naming, update url_path of children on saving

This commit is contained in:
MonaHartdegen 2019-12-09 08:22:37 +01:00
parent 0eb9cfc86d
commit ebcae41f3c
9 changed files with 91 additions and 71 deletions

View File

@ -3,6 +3,7 @@
namespace Webkul\Category\Observers;
use Illuminate\Support\Facades\Storage;
use Webkul\Category\Models\Category;
class CategoryObserver
{
@ -16,4 +17,16 @@ class CategoryObserver
{
Storage::deleteDirectory('category/' . $category->id);
}
/**
* Handle the Category "saved" event.
*
* @param Category $category
*/
public function saved($category)
{
foreach($category->children as $child) {
$child->touch();
}
}
}

View File

@ -156,17 +156,9 @@ class CategoryRepository extends Repository
*
* @return mixed
*/
public function findByPathOrFail(string $urlPath)
public function findByPath(string $urlPath)
{
$category = $this->model->whereTranslation('url_path', $urlPath)->first();
if ($category) {
return $category;
}
throw (new ModelNotFoundException)->setModel(
get_class($this->model), $urlPath
);
return $this->model->whereTranslation('url_path', $urlPath)->first();
}
/**

View File

@ -227,6 +227,22 @@ class ProductRepository extends Repository
return $product;
}
/**
* Retrieve product from slug without throwing an exception (might return null)
*
* @param $slug
*
* @return mixed
*/
public function findBySlug($slug)
{
return app('Webkul\Product\Repositories\ProductFlatRepository')->findOneWhere([
'url_key' => $slug,
'locale' => app()->getLocale(),
'channel' => core()->getCurrentChannelCode(),
]);
}
/**
* Returns newly added product
*

View File

@ -14,7 +14,7 @@ class AddColumnUrlPathToCategoryTranslations extends Migration
public function up()
{
Schema::table('category_translations', function (Blueprint $table) {
$table->string('url_path')
$table->string('url_path', 2048)
->comment('maintained by database triggers');
});
}

View File

@ -23,7 +23,8 @@ class AddStoredFunctionToGetUrlPathOfCategory extends Migration
BEGIN
DECLARE urlPath VARCHAR(255);
IF categoryId != 1
-- Category with id 1 is root by default
IF categoryId <> 1
THEN
SELECT
GROUP_CONCAT(parent_translations.slug SEPARATOR '/') INTO urlPath

View File

@ -63,7 +63,8 @@ SQL;
DECLARE parentUrlPath varchar(255);
DECLARE urlPath varchar(255);
IF NEW.category_id != 1
-- Category with id 1 is root by default
IF NEW.category_id <> 1
THEN
SELECT

View File

@ -15,41 +15,7 @@ class AddTriggerToCategories extends Migration
*/
public function up()
{
$triggerBody = <<< SQL
DECLARE urlPath VARCHAR(255);
DECLARE localeCode VARCHAR(255);
DECLARE done INT;
DECLARE curs CURSOR FOR (SELECT category_translations.locale
FROM category_translations
WHERE category_id = NEW.id);
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
IF EXISTS (
SELECT *
FROM category_translations
WHERE category_id = NEW.id
)
THEN
OPEN curs;
SET done = 0;
REPEAT
FETCH curs INTO localeCode;
SELECT get_url_path_of_category(NEW.id, localeCode) INTO urlPath;
UPDATE category_translations
SET url_path = urlPath
WHERE category_translations.category_id = NEW.id;
UNTIL done END REPEAT;
CLOSE curs;
END IF;
SQL;
$triggerBody = $this->getTriggerBody();
$insertTrigger = <<< SQL
CREATE TRIGGER %s
@ -86,4 +52,48 @@ SQL;
DB::unprepared(sprintf('DROP TRIGGER IF EXISTS %s;', self::TRIGGER_NAME_INSERT));
DB::unprepared(sprintf('DROP TRIGGER IF EXISTS %s;', self::TRIGGER_NAME_UPDATE));
}
/**
* Returns trigger body as string
*
* @return string
*/
private function getTriggerBody(): string
{
return <<< SQL
DECLARE urlPath VARCHAR(255);
DECLARE localeCode VARCHAR(255);
DECLARE done INT;
DECLARE curs CURSOR FOR (SELECT category_translations.locale
FROM category_translations
WHERE category_id = NEW.id);
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
IF EXISTS (
SELECT *
FROM category_translations
WHERE category_id = NEW.id
)
THEN
OPEN curs;
SET done = 0;
REPEAT
FETCH curs INTO localeCode;
SELECT get_url_path_of_category(NEW.id, localeCode) INTO urlPath;
UPDATE category_translations
SET url_path = urlPath
WHERE category_translations.category_id = NEW.id;
UNTIL done END REPEAT;
CLOSE curs;
END IF;
SQL;
}
}

View File

@ -54,36 +54,23 @@ class ProductsCategoriesProxyController extends Controller
* Display a listing of the resource which can be a category or a product.
*
*
* @param string $slug
* @param string $slugOrPath
*
* @return \Illuminate\View\View
*/
public function index(string $slug)
public function index(string $slugOrPath)
{
$slug = rtrim($slug, '/ ');
if (preg_match('/^([a-z0-9-]+\/?)+$/', $slug)) {
if ($category = $this->categoryRepository->findByPath($slugOrPath)) {
if (DB::table(app(CategoryTranslation::class)->getTable())
->where('url_path', '=', $slug)
->exists()
) {
return view($this->_config['category_view'], compact('category'));
}
$category = $this->categoryRepository->findByPathOrFail($slug);
if ($product = $this->productRepository->findBySlug($slugOrPath)) {
return view($this->_config['category_view'], compact('category'));
}
$customer = auth()->guard('customer')->user();
if (DB::table(app(ProductFlat::class)->getTable())
->where('url_key', '=', $slug)
->exists()
) {
$product = $this->productRepository->findBySlugOrFail($slug);
$customer = auth()->guard('customer')->user();
return view($this->_config['product_view'], compact('product', 'customer'));
}
return view($this->_config['product_view'], compact('product', 'customer'));
}
abort(404);

View File

@ -297,12 +297,12 @@ Route::group(['middleware' => ['web', 'locale', 'theme', 'currency']], function
Route::get('page/{slug}', 'Webkul\CMS\Http\Controllers\Shop\PagePresenterController@presenter')->name('shop.cms.page');
Route::get('{slug}', \Webkul\Shop\Http\Controllers\ProductsCategoriesProxyController::class . '@index')
Route::get('{slugOrPath}', \Webkul\Shop\Http\Controllers\ProductsCategoriesProxyController::class . '@index')
->defaults('_config', [
'product_view' => 'shop::products.view',
'category_view' => 'shop::products.index'
])
->where('slug', '^([a-z0-9-]+\/?)+$')
->where('slugOrPath', '^([a-z0-9-]+\/?)+$')
->name('shop.productOrCategory.index');
Route::fallback('Webkul\Shop\Http\Controllers\HomeController@notFound');