gurl_o/plugins/tps/birzha/components/CategoryProfile.php

186 lines
5.0 KiB
PHP

<?php namespace TPS\Birzha\Components;
use Cms\Classes\ComponentBase;
use TPS\Birzha\Models\Category;
use TPS\Birzha\Models\Product;
use TPS\Birzha\Models\City;
use Session;
use DB;
use TPS\Birzha\Models\Favourites;
use RainLab\User\Facades\Auth;
use ValidationException;
use Validator;
use Flash;
class CategoryProfile extends ComponentBase
{
public $category;
public $users;
public $products;
public $subcategories;
public $categories;
public $cities;
public $categoryUsers;
public function componentDetails() {
return [
'name' => 'Category profile',
'description' => 'Category profile page component',
];
}
public function defineProperties()
{
return [
'categorySlug' => [
'title' => 'Select by category :slug',
'description' => 'Select by category',
'type' => 'string',
'default' => ''
],
];
}
public function onRun() {
$this->users = $this->getCategoryUsers();
$this->category = $this->getCategory();
$this->categoryUsers = $this->category->users;
$this->subcategories = $this->getSubs();
$this->categories = $this->getCategories();
$this->cities = $this->getCities();
}
protected function getCategories(){
$categories = Category::where('primary_key', 0)->get();
return $categories;
}
protected function getCities(){
$cities = City::all();
return $cities;
}
protected function getCategory(){
$cSlug = $this->property('categorySlug');
$category = Category::transWhere('slug', $cSlug, Session::get('rainlab.translate.locale'))->with('users')->first();
return $category;
}
protected function getSubs(){
$cSlug = $this->property('categorySlug');
$category = Category::transWhere('slug', $cSlug, Session::get('rainlab.translate.locale'))->with('users')->first();
$subs = $category ? Category::where("primary_key", $category->id)->get() : null;
if($subs){
foreach ($subs as $subcategory) {
$page = input('page', 1);
$products = $subcategory->products()->paginate(12, $page);
$subcategory->setRelation('products', $products);
}
}
return $subs;
}
protected function getCategoryUsers(){
$cSlug = $this->property('categorySlug');
$category = Category::where('slug', $cSlug)->first();
$users = $category ? $category->users()->where('is_category', 1)->get() : null;
return $users;
}
protected function getProducts(){
$cSlug = $this->property('categorySlug');
$category = Category::where('slug', $cSlug)->first();
$users = $category->users()->get();
return $users;
}
public function onCreateFav()
{
$data = input();
$validator = Validator::make($data, [
'product_id' => 'required'
]);
if($validator->fails()) {
Flash::error("Haryt maglumatyny nädogry");
}
$favourite = new Favourites;
$favourite->user_id = \Auth::user()->id;
$favourite->product_id = (int)$data['product_id'];
$favourite->save();
Flash::success("Haryt halanlaryma goşuldy");
}
public function onGetCategorySubs(){
$data = post();
$catgegory = null;
if(strlen($data["mainCat"]) > 0){
$category = Category::where('slug', $data["mainCat"])->first();
}
$subs = $this->getSubCatsq($category ? $category->id : $data["mainCat"]);
$checkSubs = $this->checkSubs($subs);
if($checkSubs == ''){
$checkSubs = [];
}
$allCats = $subs + $checkSubs;
$subCatsAll = Category::whereIn("id", $allCats)->orderBy("primary_key", "ASC")->get();
return $subCatsAll;
}
protected function checkSubs($catIds){
$data = '';
$subCats = [];
foreach ($catIds as $id){
$subs = $this->getSubCatsq($id);
if(count($subs) > 0){
foreach($subs as $subId){
array_unshift($subCats, $subId);
}
array_unshift($subCats, $id);
$data = $subCats;
}
}
return $data;
}
protected function getSubCatsq($catId){
$categoriesCustoms = Category::where("primary_key", $catId)->select("id", "primary_key")->get()->pluck("id")->toArray();
return $categoriesCustoms;
}
//----------
protected function checking($catIds){
$data=[];
foreach ($catIds as $id){
$subs = $this->getSubCatsq($id);
$data[] = $id;
if(count($subs) > 0){
$data[] = $subs;
}
}
}
}