Skip to content

Commit

Permalink
fix problem with amount value
Browse files Browse the repository at this point in the history
  • Loading branch information
alnv committed Apr 14, 2020
1 parent 560ad98 commit a9acdd1
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 40 deletions.
24 changes: 5 additions & 19 deletions WishlistInserttag.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,17 @@

class WishlistInserttag extends \Frontend {


public function getInsertTagValue( $strTag ) {

public function getInsertTagValue($strTag) {
$arrTables = [];
$arrTags = explode( '::', $strTag );
$arrTags = explode('::', $strTag);

if ( empty( $arrTags ) || !is_array( $arrTags ) ) {

return false;
}

if ( isset( $arrTags[0] ) && $arrTags[0] == 'WISHLIST' ) {

$objWishlistView = new WishlistView();

$arrChunks = explode('?', urldecode( $arrTags[2] ), 2 );
$strSource = \StringUtil::decodeEntities( $arrChunks[1] );
$strSource = str_replace( '[&]', '&', $strSource );
Expand All @@ -27,45 +23,35 @@ public function getInsertTagValue( $strTag ) {
foreach ( $arrParams as $strParam ) {

list($strKey, $strOption) = explode('=', $strParam);

switch ( $strKey ) {

case 'tables':

$arrOnlyTables = explode( ',', $strOption );

if ( !empty( $arrOnlyTables ) && is_array( $arrOnlyTables ) ) {

$arrTables = $arrOnlyTables;
}

break;
}
}

if ( is_array( $arrTables ) && !empty( $arrTables ) ) $objWishlistView->setExplicit( $arrTables );
if ( is_array( $arrTables ) && !empty( $arrTables ) ) {
$objWishlistView->setExplicit( $arrTables );
}

return $objWishlistView->render();
}

if ( isset( $arrTags[0] ) && $arrTags[0] == 'WISHLIST_AMOUNT' && $arrTags[1] ) {

$numReturn = 0;
$objSession = \Session::getInstance();
$arrTables = $objSession->get( 'wishlist_tables' );

if ( !is_array( $arrTables ) ) $arrTables = [];

if ( in_array( $arrTags[1], $arrTables ) ) {

$arrValue = $objSession->get( 'wishlist_' . $arrTags[1] );

if ( isset( $arrValue['ids'] ) ) {

$numReturn = count( $arrValue['ids'] );
}
}

return $numReturn;
}

Expand Down
13 changes: 0 additions & 13 deletions WishlistModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,16 @@

class WishlistModule extends CatalogController {


protected $strTable = '';
protected $intColIndex = 0;
protected $blnUseWishlist = false;


public function __construct() {

parent::__construct();

$this->import( 'Database' );
}


public function initialize( &$objCatalogView ) {

$this->strTable = $objCatalogView->catalogTablename;
Expand Down Expand Up @@ -70,7 +66,6 @@ public function initialize( &$objCatalogView ) {
}
}


public function renderCatalog( &$arrCatalog, $strTablename, $objCatalogView ) {

if ( $this->blnUseWishlist ) {
Expand Down Expand Up @@ -131,7 +126,6 @@ public function renderCatalog( &$arrCatalog, $strTablename, $objCatalogView ) {
}
}


public function setQuery( $arrQuery, $objCatalogView ) {

if ( !$objCatalogView->wishlistEnableFilter ) return $arrQuery;
Expand All @@ -152,14 +146,12 @@ public function setQuery( $arrQuery, $objCatalogView ) {
return $arrQuery;
}


protected function addToWishlist() {

$objSession = \Session::getInstance();
$objSession->set( 'wishlist_' . $this->strTable, $this->getWishlistData() );
}


protected function removeFromWishlist() {

$objSession = \Session::getInstance();
Expand All @@ -182,7 +174,6 @@ protected function removeFromWishlist() {
}
}


protected function request( $objCatalogView ) {

if ( \Input::get( 'wishlist_ajax' ) ) {
Expand All @@ -195,10 +186,8 @@ protected function request( $objCatalogView ) {
header('Content-Type: application/json');

echo json_encode([

'id' => md5( \Input::get('wishlist_id') . $this->strTable ),
'reload' => $arrCatalog['wishlistForm']

], 512 );

exit;
Expand All @@ -214,7 +203,6 @@ protected function request( $objCatalogView ) {
\Controller::redirect( $strRedirect );
}


protected function validateInput() {

if ( !$this->Database->tableExists( $this->strTable ) ) return false;
Expand All @@ -224,7 +212,6 @@ protected function validateInput() {
return $objRow->numRows ? true : false;
}


protected function getWishlistData() {

$arrIds = [];
Expand Down
2 changes: 1 addition & 1 deletion WishlistView.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?phpnamespace CMWishlist;use CatalogManager\I18nCatalogTranslator;use CatalogManager\CatalogFieldBuilder;use CatalogManager\CatalogController;use CatalogManager\Toolkit;use CatalogManager\Entity;class WishlistView extends CatalogController { protected $arrWishlist = []; protected $blnExplicit = false; protected $arrExplicitTables = []; protected $arrWishlistTables = []; protected $objI18nCatalogTranslator = null; public function __construct() { parent::__construct(); $this->import( 'SQLQueryBuilder' ); $objSession = \Session::getInstance(); $this->objI18nCatalogTranslator = new I18nCatalogTranslator(); $this->objI18nCatalogTranslator->initialize(); if ( is_array( $objSession->get( 'wishlist_tables' ) ) && !empty( $objSession->get( 'wishlist_tables' ) ) ) { $this->arrWishlistTables = $objSession->get( 'wishlist_tables' ); } $this->getWishlistData(); } public function setExplicit( $arrTables ) { $arrExplicitTables = []; $this->blnExplicit = true; foreach ( $arrTables as $strTable ) { if ( in_array( $strTable, $this->arrWishlistTables ) ) { $arrExplicitTables[] = $strTable; } } $this->arrExplicitTables = $arrExplicitTables; } public function render() { $arrData = []; $arrTables = $this->arrWishlistTables; $objTemplate = new \FrontendTemplate( 'wishlist_view' ); if ( !empty( $this->arrExplicitTables ) ) $arrTables = $this->arrExplicitTables; foreach ( $arrTables as $strTable ) { $objFieldBuilder = new CatalogFieldBuilder(); $objFieldBuilder->initialize( $strTable ); $arrCatalog = $objFieldBuilder->getCatalog(); $arrRow = $this->getRow( $strTable, $arrCatalog ); if ( is_bool( $arrRow ) && $arrRow === false ) continue; $arrData[ $strTable ] = []; $arrData[ $strTable ]['rows'] = $arrRow; $arrData[ $strTable ]['table'] = $this->getTable( $strTable, $arrCatalog ); } $objTemplate->setData( [ 'data' => $arrData, 'wishlist' => $this->arrWishlist ] ); return $objTemplate->parse(); } protected function getTable( $strTable, $arrCatalog ) { $arrModuleLabels = $this->objI18nCatalogTranslator->get( 'module', $arrCatalog['tablename'] ); $strTitleLabel = $this->objI18nCatalogTranslator->get( 'field', 'title', [ 'titleOnly' => true ] ); return [ 'table' => $strTable, 'titleLabel' => $strTitleLabel, 'headline' => $arrModuleLabels[0], 'description' => $arrModuleLabels[1], 'amountLabel' => $GLOBALS['TL_LANG']['MSC']['CATALOG_MANAGER']['wishlistAmount'] ]; } protected function getRow( $strTable, $arrCatalog ) { $arrWishlist = $this->arrWishlist[ $strTable ]; $arrIds = $arrWishlist['ids']; $arrCatalogs = []; if ( empty( $arrIds ) ) { $arrIds = ['0']; } foreach ( $arrIds as $strId ) { $objEntity = new Entity( $strId, $strTable ); $arrEntity = $objEntity->getEntity(); $arrEntity['wishlistTable'] = $strTable; $arrEntity['wishlistAmountValue'] = $arrWishlist['amounts'][ $arrCatalog['id'] ] ? $arrWishlist['amounts'][ $arrCatalog['id'] ] : '1'; $arrCatalogs[] = $arrEntity; } return $arrCatalogs; } protected function getWishlistData() { if ( empty( $this->arrWishlistTables ) ) return null; $objSession = \Session::getInstance(); foreach ( $this->arrWishlistTables as $strTable ) { $arrWishlist = $objSession->get( 'wishlist_' . $strTable ); if ( Toolkit::isEmpty( $arrWishlist ) ) continue; if ( is_array( $arrWishlist ) && !isset( $arrWishlist['ids'] ) ) continue; $this->arrWishlist[ $strTable ] = $arrWishlist; } }}
<?phpnamespace CMWishlist;use CatalogManager\I18nCatalogTranslator;use CatalogManager\CatalogFieldBuilder;use CatalogManager\CatalogController;use CatalogManager\Toolkit;use CatalogManager\Entity;class WishlistView extends CatalogController { protected $arrWishlist = []; protected $blnExplicit = false; protected $arrExplicitTables = []; protected $arrWishlistTables = []; protected $objI18nCatalogTranslator = null; public function __construct() { parent::__construct(); $this->import( 'SQLQueryBuilder' ); $objSession = \Session::getInstance(); $this->objI18nCatalogTranslator = new I18nCatalogTranslator(); $this->objI18nCatalogTranslator->initialize(); if ( is_array( $objSession->get( 'wishlist_tables' ) ) && !empty( $objSession->get( 'wishlist_tables' ) ) ) { $this->arrWishlistTables = $objSession->get( 'wishlist_tables' ); } $this->getWishlistData(); } public function setExplicit( $arrTables ) { $arrExplicitTables = []; $this->blnExplicit = true; foreach ( $arrTables as $strTable ) { if ( in_array( $strTable, $this->arrWishlistTables ) ) { $arrExplicitTables[] = $strTable; } } $this->arrExplicitTables = $arrExplicitTables; } public function render() { $arrData = []; $arrTables = $this->arrWishlistTables; $objTemplate = new \FrontendTemplate( 'wishlist_view' ); if ( !empty( $this->arrExplicitTables ) ) $arrTables = $this->arrExplicitTables; foreach ( $arrTables as $strTable ) { $objFieldBuilder = new CatalogFieldBuilder(); $objFieldBuilder->initialize( $strTable ); $arrCatalog = $objFieldBuilder->getCatalog(); $arrRow = $this->getRow($strTable, $arrCatalog); if ( is_bool( $arrRow ) && $arrRow === false ) continue; $arrData[$strTable] = []; $arrData[$strTable]['rows'] = $arrRow; $arrData[$strTable]['table'] = $this->getTable($strTable, $arrCatalog); } $objTemplate->setData(['data' => $arrData, 'wishlist' => $this->arrWishlist]); return $objTemplate->parse(); } protected function getTable( $strTable, $arrCatalog ) { $arrModuleLabels = $this->objI18nCatalogTranslator->get( 'module', $arrCatalog['tablename'] ); $strTitleLabel = $this->objI18nCatalogTranslator->get( 'field', 'title', [ 'titleOnly' => true ] ); return [ 'table' => $strTable, 'titleLabel' => $strTitleLabel, 'headline' => $arrModuleLabels[0], 'description' => $arrModuleLabels[1], 'amountLabel' => $GLOBALS['TL_LANG']['MSC']['CATALOG_MANAGER']['wishlistAmount'] ]; } protected function getRow($strTable, $arrCatalog) { $arrWishlist = $this->arrWishlist[ $strTable ]; $arrIds = $arrWishlist['ids']; $arrCatalogs = []; if ( empty( $arrIds ) ) { $arrIds = ['0']; } foreach ($arrIds as $strId) { $objEntity = new Entity($strId, $strTable); $arrEntity = $objEntity->getEntity(); $arrEntity['wishlistTable'] = $strTable; $arrEntity['wishlistAmountValue'] = $arrWishlist['amounts'][$strId] ? $arrWishlist['amounts'][$strId] : '1'; $arrCatalogs[] = $arrEntity; } return $arrCatalogs; } protected function getWishlistData() { if ( empty( $this->arrWishlistTables ) ) return null; $objSession = \Session::getInstance(); foreach ( $this->arrWishlistTables as $strTable ) { $arrWishlist = $objSession->get( 'wishlist_' . $strTable ); if ( Toolkit::isEmpty( $arrWishlist ) ) continue; if ( is_array( $arrWishlist ) && !isset( $arrWishlist['ids'] ) ) continue; $this->arrWishlist[ $strTable ] = $arrWishlist; } }}
Expand Down
2 changes: 0 additions & 2 deletions config/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@
ClassLoader::addNamespace( 'CMWishlist' );

ClassLoader::addClasses([

'CMWishlist\WishlistView' => 'system/modules/catalog-manager-wishlist/WishlistView.php',
'CMWishlist\WishlistModule' => 'system/modules/catalog-manager-wishlist/WishlistModule.php',
'CMWishlist\WishlistInserttag' => 'system/modules/catalog-manager-wishlist/WishlistInserttag.php'
]);

TemplateLoader::addFiles([

'wishlist_view' => 'system/modules/catalog-manager-wishlist/templates',
'wishlist_form_list' => 'system/modules/catalog-manager-wishlist/templates',
'wishlist_form_table' => 'system/modules/catalog-manager-wishlist/templates',
Expand Down
9 changes: 4 additions & 5 deletions config/config.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php

$GLOBALS['TL_HOOKS']['catalogManagerViewQuery'][] = [ 'CMWishlist\WishlistModule', 'setQuery' ];
$GLOBALS['TL_HOOKS']['catalogManagerInitializeView'][] = [ 'CMWishlist\WishlistModule', 'initialize' ];
$GLOBALS['TL_HOOKS']['catalogManagerRenderCatalog'][] = [ 'CMWishlist\WishlistModule', 'renderCatalog' ];

$GLOBALS['TL_HOOKS']['replaceInsertTags'][] = [ 'CMWishlist\WishlistInserttag', 'getInsertTagValue' ];
$GLOBALS['TL_HOOKS']['catalogManagerViewQuery'][] = ['CMWishlist\WishlistModule', 'setQuery'];
$GLOBALS['TL_HOOKS']['catalogManagerInitializeView'][] = ['CMWishlist\WishlistModule', 'initialize'];
$GLOBALS['TL_HOOKS']['catalogManagerRenderCatalog'][] = ['CMWishlist\WishlistModule', 'renderCatalog'];
$GLOBALS['TL_HOOKS']['replaceInsertTags'][] = ['CMWishlist\WishlistInserttag', 'getInsertTagValue'];

0 comments on commit a9acdd1

Please sign in to comment.