added bestseller twig function (fixed comment)

This commit is contained in:
Muammer Top 2021-06-28 16:42:00 +03:00
parent f987be6bc2
commit 8b320f93fc
3 changed files with 442 additions and 439 deletions

View File

@ -415,6 +415,13 @@ class AdvModel extends AdvsAdvsEntryModel implements AdvInterface
} }
} }
public function currentAds() {
return $this->whereDate('finish_at', '>=', date("Y-m-d H:i:s"))
->where('status', '=', 'approved')
->where('slug', '!=', '')
->orderBy('publish_at', 'desc');
}
public function inStock() public function inStock()
{ {
return $this->is_get_adv && $this->stock; return $this->is_get_adv && $this->stock;

View File

@ -17,383 +17,377 @@ use Visiosoft\LocationModule\District\DistrictModel;
class AdvRepository extends EntryRepository implements AdvRepositoryInterface class AdvRepository extends EntryRepository implements AdvRepositoryInterface
{ {
protected $model; protected $model;
private $fileRepository; private $fileRepository;
private $folderRepository; private $folderRepository;
public function __construct( public function __construct(
AdvModel $model, AdvModel $model,
FileRepositoryInterface $fileRepository, FileRepositoryInterface $fileRepository,
FolderRepositoryInterface $folderRepository FolderRepositoryInterface $folderRepository
) )
{ {
$this->model = $model; $this->model = $model;
$this->fileRepository = $fileRepository; $this->fileRepository = $fileRepository;
$this->folderRepository = $folderRepository; $this->folderRepository = $folderRepository;
} }
public function searchAdvs( public function searchAdvs(
$type, $param = null, $customParameters = [], $type, $param = null, $customParameters = [],
$limit = null, $category = null, $city = null, $paginate = true $limit = null, $category = null, $city = null, $paginate = true
) )
{ {
$isActiveDopings = new AdvModel(); $isActiveDopings = new AdvModel();
$isActiveDopings = $isActiveDopings->is_enabled('dopings'); $isActiveDopings = $isActiveDopings->is_enabled('dopings');
$query = $this->model; $query = $this->model;
$query = $query->where('advs_advs.slug', '!=', ""); $query = $query->where('advs_advs.slug', '!=', "");
$query = $query->where('advs_advs.status', 'approved'); $query = $query->where('advs_advs.status', 'approved');
$query = $query->where('advs_advs.finish_at', '>', date('Y-m-d H:i:s')); $query = $query->where('advs_advs.finish_at', '>', date('Y-m-d H:i:s'));
$query = $query->leftJoin('advs_advs_translations', function ($join) { $query = $query->leftJoin('advs_advs_translations', function ($join) {
$join->on('advs_advs.id', '=', 'advs_advs_translations.entry_id'); $join->on('advs_advs.id', '=', 'advs_advs_translations.entry_id');
$join->where('advs_advs_translations.locale', '=', Request()->session()->get('_locale', setting_value('streams::default_locale'))); $join->where('advs_advs_translations.locale', '=', Request()->session()->get('_locale', setting_value('streams::default_locale')));
}); });
if (!empty($param['keyword'])) { if (!empty($param['keyword'])) {
if (is_numeric($param['keyword'])) { if (is_numeric($param['keyword'])) {
$query = $query->where('advs_advs.id', $param['keyword']); $query = $query->where('advs_advs.id', $param['keyword']);
} else { } else {
$delimiter = '_'; $delimiter = '_';
$keyword = str_slug($param['keyword'], $delimiter); $keyword = str_slug($param['keyword'], $delimiter);
$query = $query->where(function ($query) use ($keyword) { $query = $query->where(function ($query) use ($keyword) {
$query->where('slug', 'like', '%' . $keyword . '%') $query->where('slug', 'like', '%' . $keyword . '%')
->orWhere('advs_advs_translations.name', 'like', '%' . $keyword . '%'); ->orWhere('advs_advs_translations.name', 'like', '%' . $keyword . '%');
}); });
} }
} }
if (!setting_value('visiosoft.module.location::hide_location_filter')) { if (!setting_value('visiosoft.module.location::hide_location_filter')) {
$country = !empty($param['country']) ? $param['country'] : setting_value('visiosoft.module.location::default_country'); $country = !empty($param['country']) ? $param['country'] : setting_value('visiosoft.module.location::default_country');
if ($country) { if ($country) {
$query = $query->where('country_id', $country); $query = $query->where('country_id', $country);
} }
if ($city) { if ($city) {
$query = $query->where('city', $city->id); $query = $query->where('city', $city->id);
} elseif (isset($param['city']) and !empty(array_filter($param['city']))) { } elseif (isset($param['city']) and !empty(array_filter($param['city']))) {
$query = $query->whereIn('city', explode(',', array_first($param['city']))); $query = $query->whereIn('city', explode(',', array_first($param['city'])));
} }
if (isset($param['district']) and !empty(array_filter($param['district']))) { if (isset($param['district']) and !empty(array_filter($param['district']))) {
$query = $query->whereIn('district', explode(',', array_first($param['district']))); $query = $query->whereIn('district', explode(',', array_first($param['district'])));
} }
if (isset($param['neighborhood']) and !empty(array_filter($param['neighborhood']))) { if (isset($param['neighborhood']) and !empty(array_filter($param['neighborhood']))) {
$query = $query->whereIn('neighborhood', explode(',', array_first($param['neighborhood']))); $query = $query->whereIn('neighborhood', explode(',', array_first($param['neighborhood'])));
} }
if (isset($param['village']) and !empty(array_filter($param['village']))) { if (isset($param['village']) and !empty(array_filter($param['village']))) {
$query = $query->whereIn('village', explode(',', array_first($param['village']))); $query = $query->whereIn('village', explode(',', array_first($param['village'])));
} }
} }
if ($category) { if ($category) {
$category_repository = app(CategoryRepositoryInterface::class); $category_repository = app(CategoryRepositoryInterface::class);
$catLevel = $category_repository->getLevelById($category->id); $catLevel = $category_repository->getLevelById($category->id);
$catLevel = "cat" . $catLevel; $catLevel = "cat" . $catLevel;
$query = $query->where($catLevel, $category->id); $query = $query->where($catLevel, $category->id);
} }
if (!empty($param['user'])) { if (!empty($param['user'])) {
$query = $query->where('advs_advs.created_by_id', $param['user']); $query = $query->where('advs_advs.created_by_id', $param['user']);
} }
$currency = setting_value('streams::currency'); $currency = setting_value('streams::currency');
if (!empty($param['currency'])) { if (!empty($param['currency'])) {
$currency = $param['currency']; $currency = $param['currency'];
} }
if (!empty($param['min_price'])) { if (!empty($param['min_price'])) {
$num = $param['min_price']; $num = $param['min_price'];
$int = (int)$num; $int = (int)$num;
$column = "JSON_EXTRACT(foreign_currencies, '$." . $currency . "') >= " . $int; $column = "JSON_EXTRACT(foreign_currencies, '$." . $currency . "') >= " . $int;
$query = $query->whereRaw($column); $query = $query->whereRaw($column);
} }
if (!empty($param['max_price'])) { if (!empty($param['max_price'])) {
$num = $param['max_price']; $num = $param['max_price'];
$int = (int)$num; $int = (int)$num;
$column = "JSON_EXTRACT(foreign_currencies, '$." . $currency . "') <= " . $int; $column = "JSON_EXTRACT(foreign_currencies, '$." . $currency . "') <= " . $int;
$query = $query->whereRaw($column); $query = $query->whereRaw($column);
} }
if (!empty($param['date'])) { if (!empty($param['date'])) {
if ($param['date'] === 'day') { if ($param['date'] === 'day') {
$query = $query->where('advs_advs.publish_at', '>=', Carbon::now()->subDay()); $query = $query->where('advs_advs.publish_at', '>=', Carbon::now()->subDay());
} elseif ($param['date'] === 'two_days') { } elseif ($param['date'] === 'two_days') {
$query = $query->where('advs_advs.publish_at', '>=', Carbon::now()->subDays(2)); $query = $query->where('advs_advs.publish_at', '>=', Carbon::now()->subDays(2));
} elseif ($param['date'] === 'week') { } elseif ($param['date'] === 'week') {
$query = $query->where('advs_advs.publish_at', '>=', Carbon::now()->subWeek()); $query = $query->where('advs_advs.publish_at', '>=', Carbon::now()->subWeek());
} elseif ($param['date'] === 'month') { } elseif ($param['date'] === 'month') {
$query = $query->where('advs_advs.publish_at', '>=', Carbon::now()->subMonth()); $query = $query->where('advs_advs.publish_at', '>=', Carbon::now()->subMonth());
} }
} }
if (!empty($param['photo'])) { if (!empty($param['photo'])) {
$query = $query->whereNotNull('cover_photo'); $query = $query->whereNotNull('cover_photo');
} }
if (!empty($param['video'])) { if (!empty($param['video'])) {
$query = $query->where('cover_photo', 'like', '%video/upload/w_400,e_loop%'); $query = $query->where('cover_photo', 'like', '%video/upload/w_400,e_loop%');
} }
if (!empty($param['map']) && $param['map'] == true) { if (!empty($param['map']) && $param['map'] == true) {
$query = $query->whereNotNull('map_Val'); $query = $query->whereNotNull('map_Val');
} }
if (!empty($param['get_ads']) && $param['get_ads'] == true) { if (!empty($param['get_ads']) && $param['get_ads'] == true) {
$query = $query->where('is_get_adv', 1); $query = $query->where('is_get_adv', 1);
} }
if (!empty($param['created_at'])) { if (!empty($param['created_at'])) {
$query = $query->whereDate('advs_advs.created_at', $param['created_at']); $query = $query->whereDate('advs_advs.created_at', $param['created_at']);
} }
foreach ($param as $para => $value) { foreach ($param as $para => $value) {
if (substr($para, 0, 3) === "cf_") { if (substr($para, 0, 3) === "cf_") {
$id = substr($para, 3); $id = substr($para, 3);
$customParameters[] = ['id' => "$.cf" . $id, 'value' => $param[$para]]; $customParameters[] = ['id' => "$.cf" . $id, 'value' => $param[$para]];
} }
} }
if ($this->model->is_enabled('customfields')) { if ($this->model->is_enabled('customfields')) {
$query = app('Visiosoft\CustomfieldsModule\Http\Controller\CustomFieldsController')->filterSearch($customParameters, $param, $query); $query = app('Visiosoft\CustomfieldsModule\Http\Controller\CustomFieldsController')->filterSearch($customParameters, $param, $query);
} }
// //UPDATE `default_advs_advs` SET `coor` = (PointFromText('POINT(41.085022 28.804754)')) WHERE `default_advs_advs`.`id` = 8 // //UPDATE `default_advs_advs` SET `coor` = (PointFromText('POINT(41.085022 28.804754)')) WHERE `default_advs_advs`.`id` = 8
// //SELECT * FROM `default_advs_advs` WHERE ST_DISTANCE(ST_GeomFromText('POINT(41.0709052 28.829627)'), coor) < 20 // //SELECT * FROM `default_advs_advs` WHERE ST_DISTANCE(ST_GeomFromText('POINT(41.0709052 28.829627)'), coor) < 20
if (!empty($param['dlong']) && !empty($param['dlat']) && !empty($param['distance'])) { if (!empty($param['dlong']) && !empty($param['dlat']) && !empty($param['distance'])) {
$query = $query->whereNotNull('coor'); $query = $query->whereNotNull('coor');
$query = $query->whereRaw("ST_DISTANCE(ST_GeomFromText('POINT(" . $param['dlong'] . " " . $param['dlat'] . ")'), coor) < " . $param['distance']); $query = $query->whereRaw("ST_DISTANCE(ST_GeomFromText('POINT(" . $param['dlong'] . " " . $param['dlat'] . ")'), coor) < " . $param['distance']);
} }
if ($isActiveDopings) { if ($isActiveDopings) {
$query = app('Visiosoft\DopingsModule\Http\Controller\DopingsController')->search($query, $param); $query = app('Visiosoft\DopingsModule\Http\Controller\DopingsController')->search($query, $param);
} }
if (!empty($param['sort_by'])) { if (!empty($param['sort_by'])) {
switch ($param['sort_by']) { switch ($param['sort_by']) {
case "popular": case "popular":
$query = $query->orderBy('advs_advs.count_show_ad', 'desc'); $query = $query->orderBy('advs_advs.count_show_ad', 'desc');
break; break;
case "sort_price_up": case "sort_price_up":
$query = $query->orderBy('advs_advs.price', 'desc'); $query = $query->orderBy('advs_advs.price', 'desc');
break; break;
case "sort_price_down": case "sort_price_down":
$query = $query->orderBy('advs_advs.price', 'asc'); $query = $query->orderBy('advs_advs.price', 'asc');
break; break;
case "sort_time_newest": case "sort_time_newest":
$query = $query->orderBy('advs_advs.created_at', 'desc'); $query = $query->orderBy('advs_advs.created_at', 'desc');
break; break;
case "sort_time_oldest": case "sort_time_oldest":
$query = $query->orderBy('advs_advs.created_at', 'asc'); $query = $query->orderBy('advs_advs.created_at', 'asc');
break; break;
case "address_a_z": case "address_a_z":
$query = $query->join('location_cities_translations', 'advs_advs.city', '=', 'location_cities_translations.entry_id') $query = $query->join('location_cities_translations', 'advs_advs.city', '=', 'location_cities_translations.entry_id')
->orderBy('location_cities_translations.name', 'ASC'); ->orderBy('location_cities_translations.name', 'ASC');
break; break;
case "address_z_a": case "address_z_a":
$query = $query->join('location_cities_translations', 'advs_advs.city', '=', 'location_cities_translations.entry_id') $query = $query->join('location_cities_translations', 'advs_advs.city', '=', 'location_cities_translations.entry_id')
->orderBy('location_cities_translations.name', 'DESC'); ->orderBy('location_cities_translations.name', 'DESC');
break; break;
} }
} else { } else {
$query = $query->orderBy('advs_advs.created_at', 'desc'); $query = $query->orderBy('advs_advs.created_at', 'desc');
} }
if ($isActiveDopings) { if ($isActiveDopings) {
$query = app('Visiosoft\DopingsModule\Http\Controller\DopingsController')->querySelect($query, $param); $query = app('Visiosoft\DopingsModule\Http\Controller\DopingsController')->querySelect($query, $param);
} else { } else {
$query = $query->select('advs_advs.*', 'advs_advs_translations.name as name', $query = $query->select('advs_advs.*', 'advs_advs_translations.name as name',
'advs_advs_translations.advs_desc as advs_desc'); 'advs_advs_translations.advs_desc as advs_desc');
} }
if ($type == "list") { if ($type == "list") {
return $paginate ? $query->paginate(setting_value('streams::per_page')) : $query; return $paginate ? $query->paginate(setting_value('streams::per_page')) : $query;
} else { } else {
return $query->get(); return $query->get();
} }
} }
public function softDeleteAdv($id) public function softDeleteAdv($id)
{ {
return $this->find($id)->update(['deleted_at' => date('Y-m-d H:i:s')]); return $this->find($id)->update(['deleted_at' => date('Y-m-d H:i:s')]);
} }
public function getLocationNames($adv) public function getLocationNames($adv)
{ {
$country = CountryModel::query()->where('location_countries.id', $adv->country_id)->first(); $country = CountryModel::query()->where('location_countries.id', $adv->country_id)->first();
$city = CityModel::query()->where('location_cities.id', $adv->city)->first(); $city = CityModel::query()->where('location_cities.id', $adv->city)->first();
$district = DistrictModel::query()->where('location_districts.id', $adv->district)->first(); $district = DistrictModel::query()->where('location_districts.id', $adv->district)->first();
if ($country != null) { if ($country != null) {
$adv->setAttribute('country_name', $country->name); $adv->setAttribute('country_name', $country->name);
} }
if ($city != null) { if ($city != null) {
$adv->setAttribute('city_name', $city->name); $adv->setAttribute('city_name', $city->name);
} }
if ($district != null) { if ($district != null) {
$adv->setAttribute('district_name', $district->name); $adv->setAttribute('district_name', $district->name);
} }
return $adv; return $adv;
} }
public function getCatNames($adv) public function getCatNames($adv)
{ {
$cat1 = CategoryModel::query()->where('cats_category.id', $adv->cat1)->first(); $cat1 = CategoryModel::query()->where('cats_category.id', $adv->cat1)->first();
$cat2 = CategoryModel::query()->where('cats_category.id', $adv->cat2)->first(); $cat2 = CategoryModel::query()->where('cats_category.id', $adv->cat2)->first();
if (!is_null($cat1)) if (!is_null($cat1))
$adv->setAttribute('cat1_name', $cat1->name); $adv->setAttribute('cat1_name', $cat1->name);
else else
$adv->setAttribute('cat1_name', ""); $adv->setAttribute('cat1_name', "");
if (!is_null($cat2)) if (!is_null($cat2))
$adv->setAttribute('cat2_name', $cat2->name); $adv->setAttribute('cat2_name', $cat2->name);
else else
$adv->setAttribute('cat2_name', ""); $adv->setAttribute('cat2_name', "");
return $adv; return $adv;
} }
public function findByIDAndSlug($id, $slug) public function findByIDAndSlug($id, $slug)
{ {
$adv = $this->newQuery() $adv = $this->newQuery()
->where('advs_advs.id', $id) ->where('advs_advs.id', $id)
->where('slug', $slug) ->where('slug', $slug)
->first(); ->first();
if ($adv) { if ($adv) {
$adv = $this->getLocationNames($adv); $adv = $this->getLocationNames($adv);
} }
return $adv; return $adv;
} }
public function getListItemAdv($id) public function getListItemAdv($id)
{ {
$adv = $this->model $adv = $this->model
->where('advs_advs.id', $id) ->where('advs_advs.id', $id)
->leftJoin('users_users as u1', 'advs_advs.created_by_id', '=', 'u1.id') ->leftJoin('users_users as u1', 'advs_advs.created_by_id', '=', 'u1.id')
->select('advs_advs.*', 'u1.first_name as first_name', 'u1.last_name as last_name', 'u1.id as owner_id') ->select('advs_advs.*', 'u1.first_name as first_name', 'u1.last_name as last_name', 'u1.id as owner_id')
->inRandomOrder() ->inRandomOrder()
->first(); ->first();
if ($adv) { if ($adv) {
$adv = $this->getLocationNames($adv); $adv = $this->getLocationNames($adv);
} }
return $adv; return $adv;
} }
public function addAttributes($advs) public function addAttributes($advs)
{ {
foreach ($advs as $adv) { foreach ($advs as $adv) {
$adv = $this->getLocationNames($adv); $adv = $this->getLocationNames($adv);
$adv = $this->getCatNames($adv); $adv = $this->getCatNames($adv);
} }
return $advs; return $advs;
} }
public function cover_image_update($adv) public function cover_image_update($adv)
{ {
if (count($adv->files) != 0) { if (count($adv->files) != 0) {
$fileName = 'tn-' . $adv->files[0]->name; $fileName = 'tn-' . $adv->files[0]->name;
$folder = $this->folderRepository->findBySlug('images'); $folder = $this->folderRepository->findBySlug('images');
$thumbnail = $this->fileRepository->findByNameAndFolder($fileName, $folder); $thumbnail = $this->fileRepository->findByNameAndFolder($fileName, $folder);
if (!$thumbnail) { if (!$thumbnail) {
// Create thumbnail image // Create thumbnail image
$image = Image::make(file_get_contents($adv->files[0]->make()->url())); $image = Image::make(file_get_contents($adv->files[0]->make()->url()));
$image->resize( $image->resize(
null, null,
setting_value('visiosoft.module.advs::thumbnail_height'), setting_value('visiosoft.module.advs::thumbnail_height'),
function ($constraint) { function ($constraint) {
$constraint->aspectRatio(); $constraint->aspectRatio();
}); });
if (setting_value('visiosoft.module.advs::add_canvas')) { if (setting_value('visiosoft.module.advs::add_canvas')) {
$image->resizeCanvas( $image->resizeCanvas(
setting_value('visiosoft.module.advs::thumbnail_width'), setting_value('visiosoft.module.advs::thumbnail_width'),
setting_value('visiosoft.module.advs::thumbnail_height'), setting_value('visiosoft.module.advs::thumbnail_height'),
'center', false, 'fff' 'center', false, 'fff'
); );
} }
$fileName = 'tn-' . $adv->files[0]->name; $fileName = 'tn-' . $adv->files[0]->name;
$image->save(app_storage_path() . '/files-module/local/images/' . $fileName); $image->save(app_storage_path() . '/files-module/local/images/' . $fileName);
// Create file entry for the image // Create file entry for the image
$this->fileRepository->create([ $this->fileRepository->create([
'folder_id' => $folder->getId(), 'folder_id' => $folder->getId(),
'name' => $fileName, 'name' => $fileName,
'disk_id' => 1, 'disk_id' => 1,
'size' => $image->filesize(), 'size' => $image->filesize(),
'mime_type' => $image->mime, 'mime_type' => $image->mime,
'extension' => $image->extension, 'extension' => $image->extension,
]); ]);
} }
$coverPhoto = 'files/images/' . $fileName; $coverPhoto = 'files/images/' . $fileName;
} else { } else {
$coverPhoto = null; $coverPhoto = null;
} }
$adv->update(['cover_photo' => $coverPhoto]); $adv->update(['cover_photo' => $coverPhoto]);
} }
public function getRecommendedAds($id) public function getRecommendedAds($id)
{ {
return AdvModel::query() return AdvModel::query()
->where('advs_advs.id', '!=', $id) ->where('advs_advs.id', '!=', $id)
->where('advs_advs.status', 'approved') ->where('advs_advs.status', 'approved')
->select('advs_advs.*') ->select('advs_advs.*')
->orderBy('id', 'desc') ->orderBy('id', 'desc')
->take(4) ->take(4)
->get(); ->get();
} }
public function getLastAd($id) public function getLastAd($id)
{ {
return AdvsAdvsEntryModel::query()->where('advs_advs.created_by_id', '=', $id)->max('id'); return AdvsAdvsEntryModel::query()->where('advs_advs.created_by_id', '=', $id)->max('id');
} }
public function getAdvArray($id) public function getAdvArray($id)
{ {
$ad = AdvsAdvsEntryModel::query()->where('advs_advs.id', $id)->first(); $ad = AdvsAdvsEntryModel::query()->where('advs_advs.id', $id)->first();
return ($ad !== null) ? $ad->toArray() : null; return ($ad !== null) ? $ad->toArray() : null;
} }
public function getQuantity($quantity, $type, $item) public function getQuantity($quantity, $type, $item)
{ {
if ($type == "minus") { if ($type == "minus") {
return $quantity - 1; return $quantity - 1;
} elseif ($type == "plus") { } elseif ($type == "plus") {
return $quantity + 1; return $quantity + 1;
} else { } else {
return $quantity; return $quantity;
} }
} }
public function findByIds($ids) public function findByIds($ids)
{ {
return $this->model->orderBy('created_at', 'DESC')->whereIn('advs_advs.id', $ids)->get(); return $this->model->orderBy('created_at', 'DESC')->whereIn('advs_advs.id', $ids)->get();
} }
/** /**
* Get Latest Ads * Get Latest Ads
* @return mixed * @return mixed
*/ */
public function latestAds() public function latestAds()
{ {
$latest_advs = $this->model $latest_advs = $this->model->currentAds()
->whereDate('finish_at', '>=', date("Y-m-d H:i:s")) ->limit(setting_value('visiosoft.module.advs::latest-limit'))
->where('status', '=', 'approved') ->get();
->where('slug', '!=', '') return $this->model->getLocationNames($latest_advs);
->orderBy('publish_at', 'desc') }
->limit(setting_value('visiosoft.module.advs::latest-limit'))->get();
$ads = $this->model->getLocationNames($latest_advs);
return $ads;
}
public function bestsellerAds($catId = null, $limit = 10) public function bestsellerAds($catId = null, $limit = 10)
{ {
return $this->model->orderBy('total_sales', 'desc') return $this->model->currentAds()->orderBy('total_sales', 'desc')
->where(function ($query) use ($catId) { ->where(function ($query) use ($catId) {
if ($catId) { if ($catId) {
$query->where('cat1', $catId); $query->where('cat1', $catId);
@ -402,134 +396,134 @@ class AdvRepository extends EntryRepository implements AdvRepositoryInterface
->limit($limit)->get(); ->limit($limit)->get();
} }
public function getByCat($catID, $level = 1, $limit = 20) public function getByCat($catID, $level = 1, $limit = 20)
{ {
$advs = $this->model $advs = $this->model
->whereDate('finish_at', '>=', date("Y-m-d H:i:s")) ->whereDate('finish_at', '>=', date("Y-m-d H:i:s"))
->where('status', 'approved') ->where('status', 'approved')
->where('slug', '!=', '') ->where('slug', '!=', '')
->where('cat' . $level, $catID); ->where('cat' . $level, $catID);
if ($limit) { if ($limit) {
$advs = $advs->limit($limit); $advs = $advs->limit($limit);
} }
$advs = $advs->get(); $advs = $advs->get();
$ads = $this->model->getLocationNames($advs); $ads = $this->model->getLocationNames($advs);
foreach ($ads as $index => $ad) { foreach ($ads as $index => $ad) {
$ads[$index]->detail_url = $this->model->getAdvDetailLinkByModel($ad, 'list'); $ads[$index]->detail_url = $this->model->getAdvDetailLinkByModel($ad, 'list');
$ads[$index]->currency_price = app(Currency::class)->format($ad->price, $ad->currency); $ads[$index]->currency_price = app(Currency::class)->format($ad->price, $ad->currency);
$ads[$index] = $this->model->AddAdsDefaultCoverImage($ad); $ads[$index] = $this->model->AddAdsDefaultCoverImage($ad);
} }
return $ads; return $ads;
} }
public function getAdsCountByCategory($catID, $level = 1) public function getAdsCountByCategory($catID, $level = 1)
{ {
return DB::table('advs_advs') return DB::table('advs_advs')
->whereDate('finish_at', '>=', date("Y-m-d H:i:s")) ->whereDate('finish_at', '>=', date("Y-m-d H:i:s"))
->where('status', 'approved') ->where('status', 'approved')
->whereNull('deleted_at') ->whereNull('deleted_at')
->where('slug', '!=', '') ->where('slug', '!=', '')
->where('cat' . $level, $catID) ->where('cat' . $level, $catID)
->count(); ->count();
} }
public function getCategoriesWithAdID($id) public function getCategoriesWithAdID($id)
{ {
$adv = $this->model->find($id); $adv = $this->model->find($id);
if (!is_null($adv)) { if (!is_null($adv)) {
$categories = array(); $categories = array();
foreach ($adv->toArray() as $key => $field) { foreach ($adv->toArray() as $key => $field) {
if (preg_match('/cat\d/', $key) and !is_null($field)) { if (preg_match('/cat\d/', $key) and !is_null($field)) {
$categories[$key] = $field; $categories[$key] = $field;
} }
} }
return $categories; return $categories;
} }
return null; return null;
} }
public function extendAds($allAds, $isAdmin = false) public function extendAds($allAds, $isAdmin = false)
{ {
if (is_array($allAds)) { if (is_array($allAds)) {
$advs = $this->newQuery()->whereIn('id', $allAds); $advs = $this->newQuery()->whereIn('id', $allAds);
} elseif (!is_numeric($allAds)) { } elseif (!is_numeric($allAds)) {
if ($isAdmin && auth()->user()->hasRole('admin')) { if ($isAdmin && auth()->user()->hasRole('admin')) {
$advs = $this->newQuery(); $advs = $this->newQuery();
} else { } else {
$advs = $this->newQuery()->where('created_by_id', auth()->id()); $advs = $this->newQuery()->where('created_by_id', auth()->id());
} }
} else { } else {
$advs = $this->newQuery()->where('id', $allAds); $advs = $this->newQuery()->where('id', $allAds);
} }
$newDate = date('Y-m-d H:i:s', strtotime(date('Y-m-d H:i:s') . ' + ' . setting_value('visiosoft.module.advs::default_published_time') . ' day')); $newDate = date('Y-m-d H:i:s', strtotime(date('Y-m-d H:i:s') . ' + ' . setting_value('visiosoft.module.advs::default_published_time') . ' day'));
return $advs return $advs
->where('slug', '!=', '') ->where('slug', '!=', '')
->update(['finish_at' => $newDate]); ->update(['finish_at' => $newDate]);
} }
public function getByUsersIDs($usersIDs, $status = 'approved', $withDraft = false) public function getByUsersIDs($usersIDs, $status = 'approved', $withDraft = false)
{ {
$ads = $this $ads = $this
->newQuery() ->newQuery()
->whereIn('advs_advs.created_by_id', $usersIDs); ->whereIn('advs_advs.created_by_id', $usersIDs);
if ($status) { if ($status) {
$ads = $ads->where('advs_advs.status', 'approved'); $ads = $ads->where('advs_advs.status', 'approved');
} }
if (!$withDraft) { if (!$withDraft) {
$ads = $ads $ads = $ads
->where('advs_advs.slug', '!=', "") ->where('advs_advs.slug', '!=', "")
->where('advs_advs.finish_at', '>', date('Y-m-d H:i:s')); ->where('advs_advs.finish_at', '>', date('Y-m-d H:i:s'));
} }
return $ads; return $ads;
} }
public function getPopular() public function getPopular()
{ {
return $this->newQuery() return $this->newQuery()
->whereDate('finish_at', '>=', date("Y-m-d H:i:s")) ->whereDate('finish_at', '>=', date("Y-m-d H:i:s"))
->where('status', '=', 'approved') ->where('status', '=', 'approved')
->where('slug', '!=', '') ->where('slug', '!=', '')
->orderBy('count_show_ad', 'desc') ->orderBy('count_show_ad', 'desc')
->paginate(setting_value('visiosoft.module.advs::popular_ads_limit', setting_value('streams::per_page'))); ->paginate(setting_value('visiosoft.module.advs::popular_ads_limit', setting_value('streams::per_page')));
} }
public function getName($id) public function getName($id)
{ {
return $this->find($id)->name; return $this->find($id)->name;
} }
public function approveAds($adsIDs) public function approveAds($adsIDs)
{ {
$defaultAdPublishTime = setting_value('visiosoft.module.advs::default_published_time'); $defaultAdPublishTime = setting_value('visiosoft.module.advs::default_published_time');
$ads = $this->newQuery()->whereIn('advs_advs.id', $adsIDs)->update([ $ads = $this->newQuery()->whereIn('advs_advs.id', $adsIDs)->update([
'status' => 'approved', 'status' => 'approved',
'finish_at' => date('Y-m-d H:i:s', strtotime(date('Y-m-d H:i:s') . ' + ' . $defaultAdPublishTime . ' day')), 'finish_at' => date('Y-m-d H:i:s', strtotime(date('Y-m-d H:i:s') . ' + ' . $defaultAdPublishTime . ' day')),
'publish_at' => date('Y-m-d H:i:s') 'publish_at' => date('Y-m-d H:i:s')
]); ]);
return $ads; return $ads;
} }
public function getUserAds($userID = null, $status = "approved") public function getUserAds($userID = null, $status = "approved")
{ {
$userID = auth_id_if_null($userID); $userID = auth_id_if_null($userID);
$query = $this->newQuery() $query = $this->newQuery()
->where('advs_advs.created_by_id', $userID); ->where('advs_advs.created_by_id', $userID);
if ($status) { if ($status) {
$query = $query->where('status', $status); $query = $query->where('status', $status);
} }
return $query->where('finish_at', '>', date('Y-m-d H:i:s')) return $query->where('finish_at', '>', date('Y-m-d H:i:s'))
->get(); ->get();
} }
} }

View File

@ -1,20 +1,22 @@
<?php namespace Visiosoft\AdvsModule\OptionHandler; <?php namespace Visiosoft\AdvsModule\OptionHandler;
use Anomaly\CheckboxesFieldType\CheckboxesFieldType; use Anomaly\CheckboxesFieldType\CheckboxesFieldType;
use Visiosoft\AdvsModule\Adv\AdvModel;
use Visiosoft\AdvsModule\Adv\Contract\AdvRepositoryInterface; use Visiosoft\AdvsModule\Adv\Contract\AdvRepositoryInterface;
use Visiosoft\MultipleFieldType\MultipleFieldType;
class AdvsOptions class AdvsOptions
{ {
private $advRepository; private $advModel;
public function __construct(AdvRepositoryInterface $advRepository) public function __construct(AdvModel $advModel)
{ {
$this->advRepository = $advRepository; $this->advModel = $advModel;
} }
public function handle(CheckboxesFieldType $fieldType) public function handle(CheckboxesFieldType $fieldType)
{ {
$categories = $this->advRepository->all(); $categories = $this->advModel->currentAds()->get();
$options = $categories->pluck('name', 'id')->all(); $options = $categories->pluck('name', 'id')->all();
$fieldType->setOptions($options); $fieldType->setOptions($options);
} }