Skip to content

Commit b112b28

Browse files
committed
Moved secret to file table. Added "all" array to twig. Prepare for URL generation snippet.
1 parent 05cbe3b commit b112b28

File tree

10 files changed

+89
-82
lines changed

10 files changed

+89
-82
lines changed

_bootstrap/index.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,10 @@
107107
// Instruct Commerce to load modules from our directory, providing the base namespace and module path twice
108108
$commerce->loadModulesFromDirectory($modulePath, 'RogueClarity\\Digitalproduct\\Modules\\', $modulePath);
109109

110-
$manager = $modx->getManager();
111110
$modx->addPackage('commerce_digitalproduct', $componentPath . '/core/components/commerce_digitalproduct/model/');
111+
$manager = $modx->getManager();
112+
$generator = $manager->getGenerator();
113+
$generator->parseSchema($componentPath . '/core/components/commerce_digitalproduct/model/schema/commerce_digitalproduct.mysql.schema.xml', $componentPath . '/core/components/commerce_digitalproduct/model/');
112114
$manager->createObjectContainer('Digitalproduct');
113115
$manager->createObjectContainer('DigitalproductFile');
114116

_build/data/settings.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,9 @@
88
'expiration_times' => [
99
'area' => 'general',
1010
'value' => '1 day==+1 day||1 week==+1 week||1 month==+1 month||Never=='
11+
],
12+
'download_method' => [
13+
'area' => 'general',
14+
'value' => 'redirect'
1115
]
1216
];
Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?php
2-
require_once strtr(realpath(dirname(dirname(__FILE__))), '\\', '/') . '/digitalproduct.class.php';
32
/**
43
* Digitalproduct for Commerce.
54
*
@@ -10,7 +9,5 @@
109
* @package commerce_digitalproduct
1110
* @license See core/components/commerce_digitalproduct/docs/license.txt
1211
*/
13-
class Digitalproduct_mysql extends Digitalproduct
14-
{
15-
16-
}
12+
require_once (dirname(__DIR__) . '/digitalproduct.class.php');
13+
class Digitalproduct_mysql extends Digitalproduct {}

core/components/commerce_digitalproduct/model/commerce_digitalproduct/mysql/digitalproduct.map.inc.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
'order' => 0,
2525
'product' => 0,
2626
'user' => 0,
27-
'secret' => '',
2827
),
2928
'fieldMeta' =>
3029
array (
@@ -55,14 +54,6 @@
5554
'null' => false,
5655
'default' => 0,
5756
),
58-
'secret' =>
59-
array (
60-
'dbtype' => 'varchar',
61-
'precision' => '190',
62-
'phptype' => 'string',
63-
'null' => false,
64-
'default' => '',
65-
),
6657
),
6758
'composites' =>
6859
array (
Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?php
2-
require_once strtr(realpath(dirname(dirname(__FILE__))), '\\', '/') . '/digitalproductfile.class.php';
32
/**
43
* Digitalproduct for Commerce.
54
*
@@ -10,7 +9,5 @@
109
* @package commerce_digitalproduct
1110
* @license See core/components/commerce_digitalproduct/docs/license.txt
1211
*/
13-
class DigitalproductFile_mysql extends DigitalproductFile
14-
{
15-
16-
}
12+
require_once (dirname(__DIR__) . '/digitalproductfile.class.php');
13+
class DigitalproductFile_mysql extends DigitalproductFile {}

core/components/commerce_digitalproduct/model/commerce_digitalproduct/mysql/digitalproductfile.map.inc.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
'fields' =>
2323
array (
2424
'digitalproduct_id' => NULL,
25+
'secret' => '',
2526
'resource' => NULL,
2627
'file' => NULL,
2728
'download_count' => 0,
@@ -37,6 +38,14 @@
3738
'phptype' => 'int',
3839
'null' => false,
3940
),
41+
'secret' =>
42+
array (
43+
'dbtype' => 'varchar',
44+
'precision' => '190',
45+
'phptype' => 'string',
46+
'null' => false,
47+
'default' => '',
48+
),
4049
'resource' =>
4150
array (
4251
'dbtype' => 'text',
Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?php
2-
require_once strtr(realpath(dirname(dirname(__FILE__))), '\\', '/') . '/digitalproductordershipment.class.php';
32
/**
43
* Digitalproduct for Commerce.
54
*
@@ -10,7 +9,5 @@
109
* @package commerce_digitalproduct
1110
* @license See core/components/commerce_digitalproduct/docs/license.txt
1211
*/
13-
class DigitalproductOrderShipment_mysql extends DigitalproductOrderShipment
14-
{
15-
16-
}
12+
require_once (dirname(__DIR__) . '/digitalproductordershipment.class.php');
13+
class DigitalproductOrderShipment_mysql extends DigitalproductOrderShipment {}
Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?php
2-
require_once strtr(realpath(dirname(dirname(__FILE__))), '\\', '/') . '/digitalproductproduct.class.php';
32
/**
43
* Digitalproduct for Commerce.
54
*
@@ -10,7 +9,5 @@
109
* @package commerce_digitalproduct
1110
* @license See core/components/commerce_digitalproduct/docs/license.txt
1211
*/
13-
class DigitalproductProduct_mysql extends DigitalproductProduct
14-
{
15-
16-
}
12+
require_once (dirname(__DIR__) . '/digitalproductproduct.class.php');
13+
class DigitalproductProduct_mysql extends DigitalproductProduct {}

core/components/commerce_digitalproduct/model/schema/commerce_digitalproduct.mysql.schema.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
<field key="order" dbtype="int" attributes="unsigned" precision="10" phptype="int" null="false" default="0" />
99
<field key="product" dbtype="int" attributes="unsigned" precision="10" phptype="int" null="false" default="0" />
1010
<field key="user" dbtype="int" attributes="unsigned" precision="10" phptype="int" null="false" default="0" />
11-
<!-- Used in URLs for tracking and viewing -->
12-
<field key="secret" dbtype="varchar" precision="190" phptype="string" null="false" default="" />
1311

1412
<aggregate alias="Order" class="comOrder" local="order" foreign="id" cardinality="one" owner="foreign" />
1513
<aggregate alias="Product" class="comProduct" local="product" foreign="id" cardinality="one" owner="foreign" />
@@ -19,6 +17,8 @@
1917

2018
<object class="DigitalproductFile" table="commerce_digitalproduct_file" extends="comSimpleObject">
2119
<field key="digitalproduct_id" dbtype="int" attributes="unsigned" precision="10" phptype="int" null="false" />
20+
<!-- Used in URLs for tracking and viewing -->
21+
<field key="secret" dbtype="varchar" precision="190" phptype="string" null="false" default="" />
2222

2323
<field key="resource" dbtype="text" phptype="string" null="true" />
2424
<field key="file" dbtype="text" phptype="string" null="true" />

core/components/commerce_digitalproduct/src/Modules/Digitalproduct.php

Lines changed: 63 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use Twig\Loader\FilesystemLoader;
77
use modmore\Commerce\Events\Checkout;
88
use modmore\Commerce\Frontend\Steps\ThankYou;
9-
// use modmore\Commerce\Frontend\Steps\Payment;
9+
use modmore\Commerce\Frontend\Steps\Payment;
1010

1111
require_once dirname(dirname(__DIR__)) . '/vendor/autoload.php';
1212

@@ -53,7 +53,7 @@ public function initialize(EventDispatcher $dispatcher)
5353
public function getDigitalProducts(Checkout $event)
5454
{
5555
$step = $event->getStep();
56-
if(!($step instanceof ThankYou)) {
56+
if (!($step instanceof ThankYou)) {
5757
return;
5858
}
5959

@@ -81,53 +81,66 @@ public function processDigitalProducts($order)
8181

8282
foreach ($orderItems as $orderItem) {
8383
$product = $orderItem->getProduct();
84-
if ($product->get('class_key') === 'DigitalproductProduct') {
85-
// Add the product to the digitalproduct table for tracking
86-
$digitalProduct = $this->adapter->newObject('Digitalproduct', [
87-
'order' => $order->get('id'),
88-
'product' => $product->get('id'),
89-
'user' => $user ? $user->get('id') : 0,
84+
// This could support class variations in the future?
85+
if ($product->get('class_key') !== 'DigitalproductProduct') {
86+
continue;
87+
}
88+
89+
// Add the product to the digitalproduct table for tracking
90+
$digitalProduct = $this->adapter->newObject('Digitalproduct', [
91+
'order' => $order->get('id'),
92+
'product' => $product->get('id'),
93+
'user' => $user ? $user->get('id') : 0,
94+
]);
95+
$digitalProduct->save();
96+
97+
$id = $product->get('id');
98+
// Get the resources attached to this product
99+
$resources = $this->getDigitalProductResources($product);
100+
if ($resources) {
101+
$output['resources'][$id] = [
102+
'resources' => $resources,
103+
'product' => $product->toArray(),
104+
];
105+
}
106+
107+
// Get the files attached to this product
108+
$files = $this->getDigitalProductFiles($product);
109+
if ($files) {
110+
$output['files'][$id] = [
111+
'files' => $files,
112+
'product' => $product->toArray(),
113+
];
114+
}
115+
116+
// Add the files/resources
117+
if ($resource || $files) {
118+
$digitalProductFile = $this->adapter->newObject('DigitalproductFile', [
119+
'digitalproduct_id' => $digitalProduct->get('id'),
120+
'resource' => $resources ? serialize($resources) : '',
121+
'file' => $files ? serialize($files) : '',
122+
'download_expiry' => $this->getDownloadExpiry($product),
90123
'secret' => $this->generateSecret()
91124
]);
92-
$digitalProduct->save();
93-
94-
$id = $product->get('id');
95-
// Get the resources attached to this product
96-
$resources = $this->getDigitalProductResources($product);
97-
if ($resources) {
98-
$output['resources'][] = [
99-
'resources' => $resources,
100-
'product' => $product->toArray()
101-
];
102-
}
125+
$digitalProductFile->save();
103126

104-
// Get the files attached to this product
105-
$files = $this->getDigitalProductFiles($product);
106-
if ($files) {
107-
$output['files'][] = [
108-
'files' => $files,
109-
'product' => $product->toArray()
110-
];
127+
if (!empty($digitalProductFile->get('file'))) {
128+
$output['resources'][$id]['data'] = $digitalProductFile->toArray();
111129
}
112-
113-
// Add the files/resources
114-
if ($resource || $files) {
115-
$digitalProductFile = $this->adapter->newObject('DigitalproductFile', [
116-
'digitalproduct_id' => $digitalProduct->get('id'),
117-
'resource' => $resources ? serialize($resources) : '',
118-
'file' => $files ? serialize($files) : '',
119-
'download_expiry' => $this->getDownloadExpiry($product)
120-
]);
121-
$digitalProductFile->save();
130+
if (!empty($digitalProductFile->get('resource'))) {
131+
$output['files'][$id]['data'] = $digitalProductFile->toArray();
122132
}
123133

124-
// Joins the user to the product's usergroup
125-
if ($user && $product->getProperty('usergroup')) {
126-
$user->joinGroup($product->getProperty('usergroup'));
127-
}
134+
// Also make these accessible in the all array in twig
135+
$output['all'][$id] = array_merge($resources, $files);
128136
}
129-
}
130137

138+
// Joins the user to the product's usergroup if they are logged in
139+
if ($user && $product->getProperty('usergroup')) {
140+
$user->joinGroup($product->getProperty('usergroup'));
141+
}
142+
}
143+
131144
return $output;
132145
}
133146

@@ -195,15 +208,20 @@ public function getDownloadExpiry($product)
195208

196209
/**
197210
* Generates secret to use for tracking and viewing order products.
198-
*
211+
* @todo support modifying bytes via system setting
212+
*
199213
* @return string
200214
*/
201-
public function generateSecret($bytes = 10, $check = true)
215+
public function generateSecret($secret = null, $bytes = 20, $check = true)
202216
{
203-
$secret = bin2hex(openssl_random_pseudo_bytes($bytes));
217+
// Allow future customization of secret for custom downloads.
218+
if (!$secret) {
219+
// $secret = random_bytes($bytes);
220+
$secret = bin2hex(openssl_random_pseudo_bytes($bytes));
221+
}
204222
// Check to ensure random generated string has not been used before
205223
if ($check) {
206-
$query = $this->adapter->getObject('Digitalproduct', ['secret' => $secret]);
224+
$query = $this->adapter->getObject('DigitalproductFile', ['secret' => $secret]);
207225

208226
if ($query) {
209227
// Generate a new one if it is being used.
@@ -216,11 +234,6 @@ public function generateSecret($bytes = 10, $check = true)
216234
public function getModuleConfiguration(\comModule $module)
217235
{
218236
$fields = [];
219-
220-
// $fields[] = new DescriptionField($this->commerce, [
221-
// 'description' => $this->adapter->lexicon('commerce_digitalproduct.module_description'),
222-
// ]);
223-
224237
return $fields;
225238
}
226239
}

0 commit comments

Comments
 (0)