Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…k-in-stock into craft-5

# Conflicts:
#	CHANGELOG.md
  • Loading branch information
engram-design committed Nov 13, 2024
2 parents 93601dd + 6983ec3 commit f662ace
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 33 deletions.
83 changes: 80 additions & 3 deletions src/controllers/LogsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,102 @@
use verbb\backinstock\models\Settings;

use Craft;
use craft\db\Query;
use craft\helpers\AdminTable;
use craft\helpers\ArrayHelper;
use craft\helpers\DateTimeHelper;
use craft\helpers\Json;
use craft\i18n\Locale;
use craft\web\Controller;

use yii\web\BadRequestHttpException;
use yii\web\NotFoundHttpException;
use yii\web\Response;

use craft\commerce\Plugin as Commerce;

class LogsController extends Controller
{
// Public Methods
// =========================================================================

public function actionIndex(): Response
{
$logs = BackInStock::$plugin->getLogs()->getAllLogs();
return $this->renderTemplate('craft-commerce-back-in-stock/logs');
}

public function actionGetLogs(): Response
{
$this->requireAcceptsJson();

$page = $this->request->getParam('page', 1);
$sort = $this->request->getParam('sort');
$limit = $this->request->getParam('per_page', 10);
$search = $this->request->getParam('search');
$offset = ($page - 1) * $limit;

$query = (new Query())
->from(['logs' => '{{%backinstock_records}}'])
->select(['logs.*', 'products.typeId AS productTypeId'])
->leftJoin('{{%commerce_variants}} variants', 'logs.variantId = variants.id')
->leftJoin('{{%commerce_products}} products', 'variants.productId = products.id')
->orderBy(['id' => SORT_DESC]);

if ($search) {
$likeOperator = Craft::$app->getDb()->getIsPgsql() ? 'ILIKE' : 'LIKE';

$query->andWhere([
'or',
[$likeOperator, 'logs.email', '%' . str_replace(' ', '%', $search) . '%', false],
[$likeOperator, 'logs.locale', '%' . str_replace(' ', '%', $search) . '%', false],
]);
}

$total = $query->count();

$query->limit($limit);
$query->offset($offset);

if ($sort) {
$sortField = $sort[0]['sortField'] ?? null;
$direction = $sort[0]['direction'] ?? null;

if ($sortField && $direction) {
$query->orderBy($sortField . ' ' . $direction);
}
}

$logs = $query->all();

$tableData = [];
$productTypeMap = [];

$dateFormat = Craft::$app->getFormattingLocale()->getDateTimeFormat('short', Locale::FORMAT_PHP);

foreach (Commerce::getInstance()->getProductTypes()->getAllProductTypes() as $productType) {
$productTypeMap[$productType->id] = [
'title' => $productType->name,
'cpEditUrl' => $productType->cpEditUrl,
];
}

foreach ($logs as $log) {
$productLink = $productTypeMap[$log['productTypeId']] ?? [];
$dateCreated = $log['dateCreated'] ? DateTimeHelper::toDateTime($log['dateCreated']) : null;

$tableData[] = [
'title' => $log['email'],
'email' => $log['email'],
'variantId' => $productLink,
'locale' => $log['locale'],
'isNotified' => $log['isNotified'],
'dateCreated' => $dateCreated?->format($dateFormat) ?? null,
];
}

return $this->renderTemplate('craft-commerce-back-in-stock/logs', [
'logs' => $logs,
return $this->asJson([
'pagination' => AdminTable::paginationLinks($page, $total, $limit),
'data' => $tableData,
]);
}
}
38 changes: 8 additions & 30 deletions src/templates/logs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,55 +16,33 @@

{% endblock %}

{% set tableData = [] %}

{% for log in logs %}
{% set productLink = {} %}

{% if log.getVariant() %}
{% set product = log.getVariant().getProduct() %}

{% set productLink = {
title: product.title,
cpEditUrl: product.cpEditUrl,
} %}
{% endif %}

{% set tableData = tableData | merge([{
title: log.email,
email: log.email,
variantId: productLink,
locale: log.locale,
isNotified: log.isNotified,
dateCreated: log.dateCreated | datetime('short'),
}]) %}
{% endfor %}

{% js %}
var columns = [
{ name: '__slot:title', title: Craft.t('craft-commerce-back-in-stock', 'Email') },
{ name: 'variantId', title: Craft.t('craft-commerce-back-in-stock', 'Product/Variant'), callback: function(value) {
{ name: '__slot:title', title: Craft.t('craft-commerce-back-in-stock', 'Email'), sortField: 'email' },
{ name: 'variantId', title: Craft.t('craft-commerce-back-in-stock', 'Product/Variant'), sortField: 'variantId', callback: function(value) {
if (value) {
return '<a href="' + value.cpEditUrl + '">' + value.title + '</a>';
} else {
return '-';
}
} },
{ name: 'locale', title: Craft.t('craft-commerce-back-in-stock', 'Locale') },
{ name: 'isNotified', title: Craft.t('craft-commerce-back-in-stock', 'Notified'), callback: function(value) {
{ name: 'locale', title: Craft.t('craft-commerce-back-in-stock', 'Locale'), sortField: 'locale' },
{ name: 'isNotified', title: Craft.t('craft-commerce-back-in-stock', 'Notified'), sortField: 'isNotified', callback: function(value) {
if (value) {
return '<span class="status on"></span>' + Craft.t('craft-commerce-back-in-stock', 'Yes');
} else {
return '<span class="status"></span>' + Craft.t('craft-commerce-back-in-stock', 'No');
}
} },
{ name: 'dateCreated', title: Craft.t('craft-commerce-back-in-stock', 'Submitted') },
{ name: 'dateCreated', title: Craft.t('craft-commerce-back-in-stock', 'Submitted'), sortField: 'dateCreated' },
];

new Craft.VueAdminTable({
columns: columns,
container: '#logs-vue-admin-table',
emptyMessage: Craft.t('craft-commerce-back-in-stock', 'No logs exist yet.'),
tableData: {{ tableData | json_encode | raw }},
tableDataEndpoint: Craft.getActionUrl('craft-commerce-back-in-stock/logs/get-logs'),
search: true,
perPage: 100,
});
{% endjs %}

0 comments on commit f662ace

Please sign in to comment.