Skip to content

Commit

Permalink
Manage edit/view link for customers in HelperList (#14721)
Browse files Browse the repository at this point in the history
Manage edit/view link for customers in HelperList
  • Loading branch information
Pablo Borowicz committed Aug 9, 2019
2 parents d506e9c + 64e0108 commit ef8e018
Show file tree
Hide file tree
Showing 12 changed files with 680 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,14 @@ $(function() {
</div>
{/if}

{if isset($orders) && $orders}
{if isset($orders) && $orders &&
isset($orderCount) && $orderCount}
<div class="panel">
<h3>
{if $orders|@count == 1}
{if $orderCount == 1}
{l s='1 order' d='Admin.Navigation.Search'}
{else}
{l s='%d orders' sprintf=[$orders|@count] d='Admin.Navigation.Search'}
{l s='%d orders' sprintf=[$orderCount] d='Admin.Navigation.Search'}
{/if}
</h3>
{$orders}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@
{if isset($params.class)} {$params.class}{/if}
{if isset($params.align)} {$params.align}{/if}{/strip}"
{if (!isset($params.position) && !$no_link && !isset($params.remove_onclick))}
onclick="document.location = '{$current_index|addslashes|escape:'html':'UTF-8'}&amp;{$identifier|escape:'html':'UTF-8'}={$tr.$identifier|escape:'html':'UTF-8'}{if $view}&amp;view{else}&amp;update{/if}{$table|escape:'html':'UTF-8'}{if $page > 1}&amp;page={$page|intval}{/if}&amp;token={$token|escape:'html':'UTF-8'}'">
{if isset($tr.link) }
onclick="document.location = '{$tr.link}'">
{else}
onclick="document.location = '{$current_index|addslashes|escape:'html':'UTF-8'}&amp;{$identifier|escape:'html':'UTF-8'}={$tr.$identifier|escape:'html':'UTF-8'}{if $view}&amp;view{else}&amp;update{/if}{$table|escape:'html':'UTF-8'}{if $page > 1}&amp;page={$page|intval}{/if}&amp;token={$token|escape:'html':'UTF-8'}'">
{/if}
{else}
>
{/if}
Expand Down
92 changes: 75 additions & 17 deletions classes/helper/HelperList.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
* International Registered Trademark & Property of PrestaShop SA
*/
use PrestaShop\PrestaShop\Adapter\SymfonyContainer;
use PrestaShop\PrestaShop\Core\Routing\EntityLinkBuilderFactory;
use PrestaShop\PrestaShop\Adapter\Routing\AdminLinkBuilder;
use PrestaShop\PrestaShop\Adapter\Routing\LegacyHelperLinkBuilder;
use PrestaShop\PrestaShop\Core\Routing\Exception\BuilderNotFoundException;

/**
* @since 1.5.0
Expand Down Expand Up @@ -124,11 +128,23 @@ class HelperListCore extends Helper

public $page;

/** @var EntityLinkBuilderFactory */
private $linkBuilderFactory;

public function __construct()
{
$this->base_folder = 'helpers/list/';
$this->base_tpl = 'list.tpl';

$adminLinkBuilder = new AdminLinkBuilder(Context::getContext()->link, [
'customer' => 'AdminCustomers',
'product' => 'AdminProducts',
]);
$this->linkBuilderFactory = new EntityLinkBuilderFactory([
$adminLinkBuilder,
new LegacyHelperLinkBuilder(),
]);

parent::__construct();
}

Expand Down Expand Up @@ -269,6 +285,8 @@ public function displayListContent()
}
}

$this->_list[$index]['link'] = in_array('view', $this->actions) ? $this->getViewLink($this->token, $id) : $this->getEditLink($this->token, $id);

// @todo skip action for bulk actions
// $this->_list[$index]['has_bulk_actions'] = true;
foreach ($this->fields_list as $key => $params) {
Expand Down Expand Up @@ -473,8 +491,9 @@ public function displayViewLink($token, $id, $name = null)
self::$cache_lang['View'] = Context::getContext()->getTranslator()->trans('View', array(), 'Admin.Actions');
}

$href = $this->getViewLink($token, $id);
$tpl->assign(array(
'href' => $this->currentIndex . '&' . $this->identifier . '=' . $id . '&view' . $this->table . '&token=' . ($token != null ? $token : $this->token),
'href' => $href,
'action' => self::$cache_lang['View'],
));

Expand All @@ -491,22 +510,7 @@ public function displayEditLink($token, $id, $name = null)
self::$cache_lang['Edit'] = Context::getContext()->getTranslator()->trans('Edit', array(), 'Admin.Actions');
}

$href = $this->currentIndex . '&' . $this->identifier . '=' . $id . '&update' . $this->table . ($this->page && $this->page > 1 ? '&page=' . (int) $this->page : '') . '&token=' . ($token != null ? $token : $this->token);

switch ($this->currentIndex) {
case 'index.php?controller=AdminProducts':
case 'index.php?tab=AdminProducts':
// New architecture modification: temporary behavior to switch between old and new controllers.
$pagePreference = SymfonyContainer::getInstance()->get('prestashop.core.admin.page_preference_interface');
$redirectLegacy = $pagePreference->getTemporaryShouldUseLegacyPage('product');
if (!$redirectLegacy && $this->identifier == 'id_product') {
$href = Context::getContext()->link->getAdminLink('AdminProducts', true, ['id_product' => $id, 'updateproduct' => 1]);
}

break;
default:
}

$href = $this->getEditLink($token, $id);
$tpl->assign(array(
'href' => $href,
'action' => self::$cache_lang['Edit'],
Expand Down Expand Up @@ -826,4 +830,58 @@ public function displayListFooter()

return $this->footer_tpl->fetch();
}

/**
* @param string|null $token
* @param int $id
*
* @return string
*
* @throws BuilderNotFoundException
* @throws PrestaShopException
*/
protected function getViewLink($token, $id)
{
$linkBuilder = $this->linkBuilderFactory->getBuilderFor($this->table);
$parameters = $this->buildLinkParameters($token, $id);

return $linkBuilder->getViewLink($this->table, $parameters);
}

/**
* @param string|null $token
* @param int $id
*
* @return string
*
* @throws BuilderNotFoundException
* @throws PrestaShopException
*/
protected function getEditLink($token, $id)
{
$linkBuilder = $this->linkBuilderFactory->getBuilderFor($this->table);
$parameters = $this->buildLinkParameters($token, $id);

return $linkBuilder->getEditLink($this->table, $parameters);
}

/**
* @param string|null $token
* @param int $id
*
* @return array
*/
protected function buildLinkParameters($token, $id)
{
$parameters = [
$this->identifier => $id,
'current_index' => $this->currentIndex,
'token' => $token != null ? $token : $this->token,
];
if ($this->page && $this->page > 1) {
$parameters['page'] = $this->page;
}

return $parameters;
}
}
1 change: 1 addition & 0 deletions controllers/admin/AdminSearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,7 @@ public function renderView()

$view = $helper->generateList($this->_list['orders'], $this->fields_list['orders']);
$this->tpl_view_vars['orders'] = $view;
$this->tpl_view_vars['orderCount'] = count($this->_list['orders']);
}

if ($this->isCountableAndNotEmpty($this->_list, 'modules')) {
Expand Down
109 changes: 109 additions & 0 deletions src/Adapter/Routing/AdminLinkBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php
/**
* 2007-2019 PrestaShop SA and Contributors
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2019 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/

namespace PrestaShop\PrestaShop\Adapter\Routing;

use Link;
use PrestaShop\PrestaShop\Core\Routing\EntityLinkBuilderInterface;

/**
* Class AdminLinkBuilder is able to build entity links based on the Link::getAdminLink
* method (which indirectly allows it to build symfony url as well).
*/
class AdminLinkBuilder implements EntityLinkBuilderInterface
{
/** @var Link */
private $link;

/** @var array */
private $entityControllers;

/**
* This class can manage entities based on the $entityControllers parameter,
* you need to specify an array map with then entity/table short name and its
* associated legacy controller:
* e.g. $entityControllers = [
* 'product' => 'AdminProducts',
* 'customer' => 'AdminCustomers',
* ];
*
* @param Link $link Link class that generates links
* @param array $entityControllers List of entities with appropriate controller
*/
public function __construct(Link $link, array $entityControllers)
{
$this->link = $link;
$this->entityControllers = $entityControllers;
}

/**
* {@inheritdoc}
*/
public function getViewLink($entity, array $parameters)
{
$controller = $this->entityControllers[$entity];
$parameters = $this->buildActionParameters('view', $entity, $parameters);

return $this->link->getAdminLink($controller, true, $parameters);
}

/**
* {@inheritdoc}
*/
public function getEditLink($entity, array $parameters)
{
$controller = $this->entityControllers[$entity];
$parameters = $this->buildActionParameters('update', $entity, $parameters);

return $this->link->getAdminLink($controller, true, $parameters);
}

/**
* @param string $action
* @param string $entity
* @param array $parameters
*
* @return array
*/
private function buildActionParameters($action, $entity, array $parameters)
{
$editAction = $action . $entity;
$entityId = 'id_' . $entity;

return array_merge(
$parameters,
[$entityId => $parameters[$entityId], $editAction => 1]
);
}

/**
* {@inheritdoc}
*/
public function canBuild($entity)
{
return !empty($this->entityControllers[$entity]);
}
}
87 changes: 87 additions & 0 deletions src/Adapter/Routing/LegacyHelperLinkBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php
/**
* 2007-2019 PrestaShop SA and Contributors
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2019 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/

namespace PrestaShop\PrestaShop\Adapter\Routing;

use PrestaShop\PrestaShop\Core\Routing\EntityLinkBuilderInterface;

/**
* Class LegacyHelperLinkBuilder is able to build entity links "manually" by concatenating
* the parameters to the current index. This way of building links is deprecated and should
* be replaced with Symfony router or Link::getAdminLink
*/
class LegacyHelperLinkBuilder implements EntityLinkBuilderInterface
{
/**
* {@inheritdoc}
*/
public function getViewLink($entity, array $parameters)
{
$currentIndex = $parameters['current_index'];
$parameters = $this->buildActionParameters('view', $entity, $parameters);

return $currentIndex . '&' . http_build_query($parameters);
}

/**
* {@inheritdoc}
*/
public function getEditLink($entity, array $parameters)
{
$currentIndex = $parameters['current_index'];
$parameters = $this->buildActionParameters('update', $entity, $parameters);

return $currentIndex . '&' . http_build_query($parameters);
}

/**
* @param string $action
* @param string $entity
* @param array $parameters
*
* @return array
*/
private function buildActionParameters($action, $entity, array $parameters)
{
unset($parameters['current_index']);
$viewAction = $action . $entity;
$entityId = 'id_' . $entity;
$parameters = array_merge(
$parameters,
[$entityId => $parameters[$entityId], $viewAction => 1]
);

return $parameters;
}

/**
* {@inheritdoc}
*/
public function canBuild($entity)
{
return true;
}
}
Loading

0 comments on commit ef8e018

Please sign in to comment.