sarga/packages/Webkul/Velocity/src/Repositories/ContentRepository.php

154 lines
4.0 KiB
PHP
Raw Normal View History

2020-01-26 08:47:05 +00:00
<?php
namespace Webkul\Velocity\Repositories;
use Illuminate\Container\Container as App;
use Webkul\Core\Eloquent\Repository;
use Illuminate\Support\Facades\Event;
use Webkul\Product\Repositories\ProductRepository;
class ContentRepository extends Repository
2020-01-28 11:35:22 +00:00
{
2020-01-26 08:47:05 +00:00
/**
* Product Repository object
*
2020-03-05 13:37:08 +00:00
* @var \Webkul\Product\Repositories\ProductRepository
2020-01-26 08:47:05 +00:00
*/
protected $productRepository;
/**
* Create a new controller instance.
*
2020-03-05 13:37:08 +00:00
* @param \Webkul\Product\Repositories\ProductRepository $productRepository
* @param \Illuminate\Container\Container $app
2020-01-26 08:47:05 +00:00
* @return void
*/
public function __construct(
ProductRepository $productRepository,
App $app
2020-03-05 13:37:08 +00:00
)
2020-01-26 08:47:05 +00:00
{
$this->productRepository = $productRepository;
parent::__construct($app);
}
/**
* Specify Model class name
*
2020-03-05 13:37:08 +00:00
* @return string
2020-01-26 08:47:05 +00:00
*/
function model()
{
return 'Webkul\Velocity\Contracts\Content';
2020-01-26 08:47:05 +00:00
}
2020-03-05 13:37:08 +00:00
/**
* @param array $data
* @return \Webkul\Velocity\Models\Content
*/
2020-01-26 08:47:05 +00:00
public function create(array $data)
{
2020-01-28 11:35:22 +00:00
// Event::fire('velocity.content.create.before');
2020-01-26 08:47:05 +00:00
if (isset($data['locale']) && $data['locale'] == 'all') {
$model = app()->make($this->model());
foreach (core()->getAllLocales() as $locale) {
foreach ($model->translatedAttributes as $attribute) {
if (isset($data[$attribute])) {
$data[$locale->code][$attribute] = $data[$attribute];
}
}
}
}
2020-01-28 11:35:22 +00:00
2020-01-26 08:47:05 +00:00
$content = $this->model->create($data);
2020-01-28 11:35:22 +00:00
// Event::fire('velocity.content.create.after', $content);
2020-01-26 08:47:05 +00:00
return $content;
}
2020-03-05 13:37:08 +00:00
/**
* @param array $data
* @param int $id
* @return \Webkul\Velocity\Models\Content
*/
2020-01-26 08:47:05 +00:00
public function update(array $data, $id)
{
$content = $this->find($id);
2020-01-28 11:35:22 +00:00
// Event::fire('velocity.content.update.before', $id);
2020-01-26 08:47:05 +00:00
$content->update($data);
2020-01-28 11:35:22 +00:00
// Event::fire('velocity.content.update.after', $id);
2020-01-26 08:47:05 +00:00
return $content;
}
2020-03-05 13:37:08 +00:00
/**
* @param int $id
* @return array
*/
2020-01-26 08:47:05 +00:00
public function getProducts($id)
{
$results = [];
2020-01-28 11:35:22 +00:00
2020-01-26 08:47:05 +00:00
$locale = request()->get('locale') ?: app()->getLocale();
2020-01-28 11:35:22 +00:00
2020-01-26 08:47:05 +00:00
$content = $this->model->find($id);
2020-01-28 11:35:22 +00:00
2020-01-26 08:47:05 +00:00
if ($content->content_type == 'product') {
$contentLocale = $content->translate($locale);
2020-01-28 11:35:22 +00:00
2020-01-26 08:47:05 +00:00
$products = json_decode($contentLocale->products, true);
2020-02-20 07:38:52 +00:00
if (! empty($products)) {
2020-01-26 08:47:05 +00:00
foreach ($products as $product_id) {
$product = $this->productRepository->find($product_id);
2020-01-28 11:35:22 +00:00
2020-02-20 07:38:52 +00:00
if (isset($product->id)) {
2020-01-26 08:47:05 +00:00
$results[] = [
2020-02-20 07:38:52 +00:00
'id' => $product->id,
2020-01-26 08:47:05 +00:00
'name' => $product->name,
];
}
}
}
}
return $results;
}
2020-03-05 13:37:08 +00:00
/**
* @return array
*/
2020-01-26 08:47:05 +00:00
public function getAllContents()
{
2020-01-30 12:51:04 +00:00
$query = $this->model::orderBy('position', 'ASC');
2020-02-04 13:34:53 +00:00
$contentCollection = $query
2020-03-04 06:32:53 +00:00
->select('velocity_contents.*', 'velocity_contents_translations.*')
->where('velocity_contents.status', 1)
->leftJoin('velocity_contents_translations', 'velocity_contents.id', 'velocity_contents_translations.content_id')
->distinct('velocity_contents_translations.id')
->where('velocity_contents_translations.locale', app()->getLocale())
->limit(5)
->get();
2020-01-26 08:47:05 +00:00
2020-02-04 13:34:53 +00:00
$formattedContent = [];
2020-02-20 07:38:52 +00:00
2020-02-04 13:34:53 +00:00
foreach ($contentCollection as $content) {
array_push($formattedContent, [
2020-02-20 07:38:52 +00:00
'title' => $content->title,
'page_link' => $content->page_link,
'link_target' => $content->link_target,
2020-02-04 13:34:53 +00:00
'content_type' => $content->content_type,
]);
}
return $formattedContent;
2020-01-26 08:47:05 +00:00
}
}