Skip to content

Commit

Permalink
- Adjusting the structure to a license child table
Browse files Browse the repository at this point in the history
- Bugfix: Considering the quantity of buyed products
- Assigning a logged-in user
  • Loading branch information
doishub committed Jun 4, 2020
1 parent d4fb2c4 commit fb8778b
Show file tree
Hide file tree
Showing 13 changed files with 513 additions and 109 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

This extension complements a license key management and its assignment to products which can then be picked up (via insert tags) and sent via the Notification Center.

> If a member is logged in, this member will automatically be assigned the license key.
The following insert tags are available:
- `{{license_collection::*}}`
- `{{license_product::*}}`
Expand Down
14 changes: 9 additions & 5 deletions src/EventListener/InsertTagsListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,23 @@ private function replaceLicenseInsertTags($insertTag, $intId)

while($productsInCollection->next())
{
$arrProductIds[] = $productsInCollection->product_id;
for($i=0; $i<$productsInCollection->quantity; $i++)
{
$arrProductIds[] = $productsInCollection->product_id;
}
}
}else{
$arrProductIds[] = $intId;
}

foreach ($arrProductIds as $productId)
{
$product = LicenseModel::findOneByProduct($productId);
$objLicences = LicenseModel::findOneByProduct($productId);

$newLicense = LicenseHandler::getNextLicense($product);

$arrProductLicenses[] = sprintf("%s: %s", $product->title, $newLicense);
if($newLicence = LicenseHandler::getNextLicense($objLicences))
{
$arrProductLicenses[] = sprintf("%s: %s", $objLicences->title, $newLicence);
}
}

return implode("\n", $arrProductLicenses);
Expand Down
48 changes: 27 additions & 21 deletions src/Resources/contao/classes/LicenseHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,55 +9,61 @@
namespace Oveleon\IsotopeProductLicenses;

use Contao\CoreBundle\Monolog\ContaoContext;
use Contao\FrontendUser;
use Contao\System;
use Psr\Log\LogLevel;

class LicenseHandler
{
/**
* Generate and return the next valid license of a product
*
* @param int|object $product
* @param int|object $varProductLicence
*
* @return string
*/
public static function getNextLicense($product)
public static function getNextLicense($varProductLicence)
{
if(is_int($product))
$logger = \System::getContainer()->get('monolog.logger.contao');

if(is_int($varProductLicence))
{
$product = LicenseModel::findOneByProduct($product);
$varProductLicence = LicenseModel::findOneByProduct($varProductLicence);
}

$logger = \System::getContainer()->get('monolog.logger.contao');
$objLicence = LicenseItemModel::findBy(['pid=?', 'published=?'], [$varProductLicence->id, 0]);

if($product !== null)
if($objLicence !== null)
{
$arrValidLicenses = array_filter(\StringUtil::deserialize($product->listitems, true));
$arrUsedLicenses = array_filter(\StringUtil::deserialize($product->useditems, true));
$intCount = $objLicence->count();

// ToDo: The number from when the warning is to be output should come from a configuration.
// ToDo: Send a warning e-mail to the administrator
if(count($arrValidLicenses) < 10 && !empty($arrValidLicenses))
// Warning when licences are almost exhausted
if($intCount < 10)
{
$logger->log(LogLevel::WARNING, sprintf($GLOBALS['TL_LANG']['ERR']['lowNumberOfLicenses'], $product->product), array('contao' => new ContaoContext('getNextLicense', 'WARNING')));
$logger->log(LogLevel::WARNING, sprintf($GLOBALS['TL_LANG']['ERR']['lowNumberOfLicenses'], $intCount, $varProductLicence->product), array('contao' => new ContaoContext('getNextLicense', 'WARNING')));
}
elseif(empty($arrValidLicenses))
// Warning when licences are exhausted
elseif($intCount === 0)
{
$logger->log(LogLevel::ERROR, sprintf($GLOBALS['TL_LANG']['ERR']['noMoreLicenses'], $product->product), array('contao' => new ContaoContext('getNextLicense', 'ERROR')));
$logger->log(LogLevel::ERROR, sprintf($GLOBALS['TL_LANG']['ERR']['noMoreLicenses'], $varProductLicence->product), array('contao' => new ContaoContext('getNextLicense', 'ERROR')));
return '';
}

$strNewLicense = array_shift($arrValidLicenses);
$arrUsedLicenses[] = $strNewLicense;
$objUser = System::importStatic(FrontendUser::class, 'Member');

$product->listitems = serialize($arrValidLicenses);
$product->useditems = serialize($arrUsedLicenses);
if(FE_USER_LOGGED_IN)
{
$objLicence->member = $objUser->id;
}

$product->save();
$objLicence->published = 1;
$objLicence->save();

return $strNewLicense;
return $objLicence->licence;
}

$logger->log(LogLevel::ERROR, $GLOBALS['TL_LANG']['ERR']['noLicenseFound'], array('contao' => new ContaoContext('getNextLicense', 'ERROR')));
$logger->log(LogLevel::ERROR, sprintf($GLOBALS['TL_LANG']['ERR']['noLicenseFound'], $varProductLicence->product), array('contao' => new ContaoContext('getNextLicense', 'ERROR')));

return '';
}
}
4 changes: 2 additions & 2 deletions src/Resources/contao/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@

array_insert($GLOBALS['BE_MOD']['isotope']['iso_license'], 0, array
(
'tables' => array('tl_license'),
'list' => array('Contao\CoreBundle\Controller\BackendCsvImportController', 'importListWizardAction')
'tables' => array('tl_license', 'tl_license_item')
));

// Register Model
$GLOBALS['TL_MODELS']['tl_license'] = '\\Oveleon\\IsotopeProductLicenses\\LicenseModel';
$GLOBALS['TL_MODELS']['tl_license_item'] = '\\Oveleon\\IsotopeProductLicenses\\LicenseItemModel';

// Register hooks
$GLOBALS['TL_HOOKS']['replaceInsertTags'][] = array('isotope_product_licenses.listener.insert_tags', 'onReplaceInsertTags');
71 changes: 15 additions & 56 deletions src/Resources/contao/dca/tl_license.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
'config' => array
(
'dataContainer' => 'Table',
'enableVersioning' => true,
'ctable' => array('tl_license_item'),
'notCopyable' => true,
'enableVersioning' => true,
'sql' => array
(
'keys' => array
Expand Down Expand Up @@ -47,16 +49,16 @@
),
'operations' => array
(
'edit' => array
(
'href' => 'act=edit',
'icon' => 'edit.svg'
),
'copy' => array
(
'href' => 'act=copy',
'icon' => 'copy.svg'
),
'edit' => array
(
'href' => 'table=tl_license_item',
'icon' => 'edit.svg',
),
'editheader' => array
(
'href' => 'act=edit',
'icon' => 'header.svg',
),
'delete' => array
(
'href' => 'act=delete',
Expand All @@ -74,7 +76,7 @@
// Palettes
'palettes' => array
(
'default' => '{title_legend},title,product;{license_legend},listitems,useditems',
'default' => '{title_legend},title,product',
),

// Fields
Expand Down Expand Up @@ -102,49 +104,6 @@
'inputType' => 'text',
'eval' => array('mandatory'=>true, 'tl_class'=>'w50'),
'sql' => "varchar(255) NOT NULL default ''"
),
'listitems' => array
(
'inputType' => 'listWizard',
'eval' => array('mandatory'=>true, 'tl_class'=>'w50 clr'),
'xlabel' => array
(
array('tl_license', 'listImportWizard')
),
'sql' => "blob NULL"
),
'useditems' => array
(
'inputType' => 'listWizard',
'eval' => array('tl_class'=>'w50 clr'),
'sql' => "blob NULL"
),
)
)
);

/**
* Provide miscellaneous methods that are used by the data configuration array.
*
* @author Daniele Sciannimanica <https://github.com/doishub>
*/
class tl_license extends Contao\Backend
{
/**
* Import the back end user object
*/
public function __construct()
{
parent::__construct();
$this->import('Contao\BackendUser', 'User');
}

/**
* Add a link to the list items import wizard
*
* @return string
*/
public function listImportWizard()
{
return ' <a href="' . $this->addToUrl('key=list') . '" title="' . Contao\StringUtil::specialchars($GLOBALS['TL_LANG']['MSC']['lw_import'][1]) . '" onclick="Backend.getScrollOffset()">' . Contao\Image::getHtml('tablewizard.svg', $GLOBALS['TL_LANG']['MSC']['tw_import'][0]) . '</a>';
}
}
Loading

0 comments on commit fb8778b

Please sign in to comment.