Skip to content

Commit

Permalink
Ability to view submissions in CP
Browse files Browse the repository at this point in the history
  • Loading branch information
mediabeastnz committed Jul 10, 2022
1 parent 26110af commit 9c6859a
Show file tree
Hide file tree
Showing 8 changed files with 154 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Release Notes for Back In Stock

## 2.1.0 - 2022-07-10
### Added
- Ability to view customer submissions in the Control Panel.

## 2.0.0 - 2022-07-09
### Added
- Craft CMS and Commerce 4 support.
Expand Down
4 changes: 1 addition & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "mediabeastnz/craft-commerce-back-in-stock",
"description": "Back in stock plugin for Craft Commerce that notifies subscribed users via an automated email",
"type": "craft-plugin",
"version": "2.0.0",
"version": "2.1.0",
"keywords": [
"craft",
"cms",
Expand Down Expand Up @@ -35,8 +35,6 @@
"extra": {
"name": "Back In Stock",
"handle": "craft-commerce-back-in-stock",
"hasCpSettings": true,
"hasCpSection": false,
"changelogUrl": "https://raw.githubusercontent.com/mediabeastnz/craft-commerce-back-in-stock/master/CHANGELOG.md",
"components": {
"backInStockService": "mediabeastnz\\backinstock\\services\\BackInStockService"
Expand Down
17 changes: 17 additions & 0 deletions src/BackInStock.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ class BackInStock extends Plugin
// Public Properties
// =========================================================================

public bool $hasCpSettings = true;
public bool $hasCpSection = true;

// Public Methods
// =========================================================================
Expand All @@ -72,6 +74,13 @@ function (RegisterUrlRulesEvent $event) {
}
);

Event::on(UrlManager::class, UrlManager::EVENT_REGISTER_CP_URL_RULES, function (RegisterUrlRulesEvent $event) {
$event->rules = array_merge($event->rules, [
'back-in-stock' => 'craft-commerce-back-in-stock/base/logs',
'back-in-stock/logs' => 'craft-commerce-back-in-stock/base/logs',
]);
});

Craft::info(
Craft::t(
'craft-commerce-back-in-stock',
Expand All @@ -90,6 +99,14 @@ function (RegisterUrlRulesEvent $event) {

}

public function getCpNavItem(): ?array
{
$navItem = parent::getCpNavItem();
$navItem['label'] = "Back in Stock";
$navItem['url'] = 'back-in-stock';
return $navItem;
}

// Protected Methods
// =========================================================================

Expand Down
43 changes: 43 additions & 0 deletions src/controllers/BaseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
use Craft;
use craft\web\Controller;
use craft\commerce\elements\Variant;
use yii\web\Response;
use craft\db\Paginator;
use craft\db\Query;
use craft\web\twig\variables\Paginate;

/**
* @author Myles Beardsmore
Expand Down Expand Up @@ -152,4 +156,43 @@ public function actionRegisterInterest()

return $this->redirectToPostedUrl();
}

public function actionLogs(): Response
{
$records = [];
$c = new Query();
$c->select('*')->from(['{{%backinstock_records}}'])->orderBy('dateCreated desc');
$paginator = new Paginator($c, [
'pageSize' => 30,
'currentPage' => \Craft::$app->request->pageNum,
]);

$pageResults = $paginator->getPageResults();
if ($pageResults && count($pageResults)) {
foreach ($pageResults as $pageResult) {
$records[] = new BackInStockRecord($pageResult);
}

$pageOffset = $paginator->getPageOffset();
$page = Paginate::create($paginator);

return $this->renderTemplate('craft-commerce-back-in-stock/logs', [
'logEntries' => $records,
'pageInfo' => [
'first' => $pageOffset + 1,
'last' => $pageOffset + count($pageResults),
'total' => $paginator->getTotalResults(),
'currentPage' => $paginator->getCurrentPage(),
'totalPages' => $paginator->getTotalPages(),
'prevUrl' => $page->getPrevUrl(),
'nextUrl' => $page->getNextUrl(),
],
]);
} else {
return $this->renderTemplate('craft-commerce-back-in-stock/logs', [
'logEntries' => $records,
]);
}
}

}
1 change: 1 addition & 0 deletions src/icon-mask.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions src/records/BackInStockRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace mediabeastnz\backinstock\records;

use mediabeastnz\backinstock\BackInStock;
use craft\commerce\elements\Variant;

use Craft;
use craft\db\ActiveRecord;
Expand All @@ -21,6 +22,14 @@
*/
class BackInStockRecord extends ActiveRecord
{

public function getVariant()
{
$query = Variant::find();
$query->where(['commerce_variants.id' => $this->variantId]);
return $query->one();
}

// Public Static Methods
// =========================================================================

Expand Down
24 changes: 24 additions & 0 deletions src/templates/layout.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{% extends parentLayout | default("_layouts/cp") %}

{% set tabs = {
logs: { label: "Logs" | t('craft-commerce-back-in-stock'), url: url('back-in-stock/logs') },
settings: { label: "Settings" | t('craft-commerce-back-in-stock'), url: url('settings/plugins/craft-commerce-back-in-stock') },
} %}

{% if title is not defined %}
{% set title = 'Back in Stock ' ~ tabs[selectedTab].label %}
{% endif %}

{% block content %}
{% block blockContent %}

{% endblock %}

<div id="plugin-footer">
<div class="footer-left">
{% block footerButton %}

{% endblock %}
</div>
</div>
{% endblock %}
55 changes: 55 additions & 0 deletions src/templates/logs.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{% extends 'craft-commerce-back-in-stock/layout' %}

{% set crumbs = [
{ label: "Back in Stock" | t('craft-commerce-back-in-stock'), url: url('back-in-stock') },
{ label: "Logs" | t('craft-commerce-back-in-stock'), url: url('back-in-stock/logs') }
] %}

{% set selectedTab = 'logs' %}

{% block blockContent %}
<table id="logs" class="data fullwidth collapsible">
<thead>
<tr>
<th scope="col">{{ "Email" | t('craft-commerce-back-in-stock') }}</th>
<th scope="col">{{ "Product/Variant" | t('craft-commerce-back-in-stock') }}</th>
<th scope="col">{{ "Locale" | t('craft-commerce-back-in-stock') }}</th>
<th scope="col">{{ "Notified" | t('craft-commerce-back-in-stock') }}</th>
<th scope="col">{{ "Submitted" | t('craft-commerce-back-in-stock') }}</th>
</tr>
</thead>
<tbody>
{% for entry in logEntries %}
<tr>
<td class="col-label" data-title="{{ 'Email' | t('craft-commerce-back-in-stock') }}">
<span class="">{{ entry.email }}</span>
</td>

<td class="col-label" data-title="{{ 'Product/Variant' | t('craft-commerce-back-in-stock') }}">
<a href="{{entry.getVariant.product.cpEditUrl}}" class="log-result">{{ entry.getVariant.product.title }}/{{entry.getVariant.title}}</a>
</td>

<td class="col-label" data-title="{{ 'Locale' | t('craft-commerce-back-in-stock') }}">
<span class="log-result">{{ entry.locale }}</span>
</td>

<td class="col-label" data-title="{{ 'Notified' | t('craft-commerce-back-in-stock') }}">
{{ entry.isNotified ? '<span class="status live"></span> Yes' : '<span class="status"></span> No' }}
</td>

<td data-title="{{ 'Submitted' | t('craft-commerce-back-in-stock') }}">
<code>
{{ entry.dateCreated | date('short') ~ ' ' ~ entry.dateCreated | time('short') }}
</code>
</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock %}

{% block footer %}
{% if logEntries|length %}
{% include "_includes/pagination" %}
{% endif %}
{% endblock %}

0 comments on commit 9c6859a

Please sign in to comment.