Merge pull request #165 from openclassify/dia

#162 Creating Multiple cats once
This commit is contained in:
Fatih Alp 2019-11-29 18:09:41 +03:00 committed by GitHub
commit 01fdfead45
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 96 additions and 5 deletions

View File

@ -20,7 +20,7 @@ return [
'name' => 'Seo Description' 'name' => 'Seo Description'
], ],
'icon' => [ 'icon' => [
' name' => 'ICON' 'name' => 'Icon'
], ],
'please_wait' => 'Please wait.Deleting Sub Categories', 'please_wait' => 'Please wait.Deleting Sub Categories',

View File

@ -2,9 +2,11 @@
use Anomaly\Streams\Platform\Image\Command\MakeImageInstance; use Anomaly\Streams\Platform\Image\Command\MakeImageInstance;
use Anomaly\Streams\Platform\Model\Cats\CatsCategoryEntryModel; use Anomaly\Streams\Platform\Model\Cats\CatsCategoryEntryModel;
use Anomaly\Streams\Platform\Model\Cats\CatsCategoryEntryTranslationsModel;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use Sunra\PhpSimple\HtmlDomParser; use Sunra\PhpSimple\HtmlDomParser;
use Visiosoft\CatsModule\Category\CategoryCollection;
use Visiosoft\CatsModule\Category\CategoryModel; use Visiosoft\CatsModule\Category\CategoryModel;
use Visiosoft\CatsModule\Category\Contract\CategoryRepositoryInterface; use Visiosoft\CatsModule\Category\Contract\CategoryRepositoryInterface;
use Visiosoft\CatsModule\Category\Form\CategoryFormBuilder; use Visiosoft\CatsModule\Category\Form\CategoryFormBuilder;
@ -13,6 +15,22 @@ use Anomaly\Streams\Platform\Http\Controller\AdminController;
class CategoryController extends AdminController class CategoryController extends AdminController
{ {
private $categoryRepository;
private $categoryEntryTranslationsModel;
private $str;
public function __construct(
CategoryRepositoryInterface $categoryRepository,
CatsCategoryEntryTranslationsModel $categoryEntryTranslationsModel,
Str $str
)
{
parent::__construct();
$this->categoryRepository = $categoryRepository;
$this->categoryEntryTranslationsModel = $categoryEntryTranslationsModel;
$this->str = $str;
}
public function index(CategoryTableBuilder $table, Request $request) public function index(CategoryTableBuilder $table, Request $request)
{ {
if ($this->request->action == "delete") { if ($this->request->action == "delete") {
@ -55,10 +73,76 @@ class CategoryController extends AdminController
return $this->redirect->back(); return $this->redirect->back();
} }
$form->make(); $locale = DB::table('cats_category_translations')->select('locale')->distinct()->get()->toArray();
if ($form->hasFormErrors()) { $translatable = array();
return $this->redirect->to('/admin/cats/create'); foreach ($all as $key => $value) {
foreach ($locale as $lang) {
if ($this->endsWith($key, "_$lang->locale") && !in_array(substr($key, 0, -3), $translatable)) {
$translatable[] = substr($key, 0, -3);
}
}
} }
$translatableEntries = array();
foreach ($locale as $lang) {
$translatableEntries[$lang->locale] = array();
foreach ($translatable as $translatableEntry) {
$translatableEntries[$lang->locale][$translatableEntry] = $all[$translatableEntry . '_' . $lang->locale];
}
}
// Check if there is multiple categories in the name filed
$isMultiCat = array();
foreach ($translatableEntries as $key => $translatableEntry) {
$multiCat = explode(",", $translatableEntry['name']);
if (count($multiCat) > 1) {
$firstArray = array();
foreach ($multiCat as $cat) {
$secondArray = array();
foreach ($locale as $lang) {
if ($key === $lang->locale) {
$secondArray[$key]['name'] = trim($cat);
}
}
array_push($firstArray, $secondArray);
}
array_push($isMultiCat, $firstArray);
}
}
if (empty($isMultiCat)) {
$this->categoryRepository->create(array_merge($translatableEntries, [
'slug' => $all['slug'],
'parent_category' => $all['parent_category'],
'icon' => $all['icon'],
'seo_keyword' => $all['seo_keyword'],
'seo_description' => $all['seo_description'],
]));
} else {
for ($i = 0; $i < count($isMultiCat[0]); $i++) {
foreach ($isMultiCat as $cat) {
$translatableEntries = array_merge($translatableEntries, $cat[$i]);
}
$this->categoryRepository->create(array_merge($translatableEntries, [
'slug' => $this->str->slug(reset($translatableEntries)['name'], '_'),
'parent_category' => $all['parent_category'],
'icon' => $all['icon'],
'seo_keyword' => $all['seo_keyword'],
'seo_description' => $all['seo_description'],
]));
}
};
// $this->categoryRepository->create(array_merge($translatableEntries, [
// 'slug' => $all['slug'],
// 'parent_category' => $all['parent_category'],
// 'icon' => $all['icon'],
// 'seo_keyword' => $all['seo_keyword'],
// 'seo_description' => $all['seo_description'],
// ]));
// $form->make();
// if ($form->hasFormErrors()) {
// return $this->redirect->to('/admin/cats/create');
// }
if ($parent_id != "") { if ($parent_id != "") {
return $this->redirect->to('/admin/cats?cat=' . $parent_id); return $this->redirect->to('/admin/cats?cat=' . $parent_id);
} }
@ -81,6 +165,13 @@ class CategoryController extends AdminController
return $this->view->make('visiosoft.module.cats::cats/admin-cat', compact('nameField', 'formBuilder')); return $this->view->make('visiosoft.module.cats::cats/admin-cat', compact('nameField', 'formBuilder'));
} }
public function endsWith($string, $test) {
$strlen = strlen($string);
$testlen = strlen($test);
if ($testlen > $strlen) return false;
return substr_compare($string, $test, $strlen - $testlen, $testlen) === 0;
}
public function edit(CategoryFormBuilder $form, Request $request, $id) public function edit(CategoryFormBuilder $form, Request $request, $id)
{ {