Skip to content

Commit

Permalink
Merge pull request #644 from ktomk/bugfix/issue-643-fatal
Browse files Browse the repository at this point in the history
[BUGFIX] #643 fatal error in customer:delete
  • Loading branch information
ktomk committed Aug 15, 2015
2 parents ae20b1d + 8a860aa commit 7e76268
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 18 deletions.
1 change: 1 addition & 0 deletions changes.txt
Expand Up @@ -6,6 +6,7 @@ RECENT CHANGES
1.97.5
======

* #643 Fix fatal error in customer:delete (by Tom Klingenberg)
* #640 Fix db:dump duplicate help (by Alexander Menk)
* #616 Add db:dump exclude option (by Alexander Menk)
* Add js and css merging commands (by Simon Sippert)
Expand Down
13 changes: 9 additions & 4 deletions src/N98/Magento/Command/Customer/AbstractCustomerCommand.php
Expand Up @@ -2,6 +2,11 @@

namespace N98\Magento\Command\Customer;

use Mage_Customer_Model_Address;
use Mage_Customer_Model_Customer;
use Mage_Customer_Model_Resource_Customer_Collection;
use Mage_Directory_Model_Resource_Country_Collection;
use Mage_Directory_Model_Resource_Region_Collection;
use N98\Magento\Command\AbstractMagentoCommand;
use N98\Util\Exec;

Expand All @@ -21,31 +26,31 @@ protected function getCustomerModel()
}

/**
* @return \Mage_Customer_Model_Resource_Customer_Collection
* @return Mage_Customer_Model_Resource_Customer_Collection
*/
protected function getCustomerCollection()
{
return $this->_getResourceModel('customer/customer_collection', 'Mage_Customer_Model_Resource_Customer_Collection');
}

/**
* @return \Mage_Customer_Model_Address
* @return Mage_Customer_Model_Address
*/
protected function getAddressModel()
{
return $this->_getModel('customer/address', 'Mage_Customer_Model_Address');
}

/**
* @return \Mage_Directory_Model_Resource_Region_Collection
* @return Mage_Directory_Model_Resource_Region_Collection
*/
protected function getRegionCollection()
{
return $this->_getResourceModel('directory/region_collection', 'Mage_Directory_Model_Resource_Region_Collection');
}

/**
* @return \Mage_Directory_Model_Resource_Country_Collection
* @return Mage_Directory_Model_Resource_Country_Collection
*/
protected function getCountryCollection()
{
Expand Down
54 changes: 40 additions & 14 deletions src/N98/Magento/Command/Customer/DeleteCommand.php
Expand Up @@ -2,6 +2,8 @@

namespace N98\Magento\Command\Customer;

use N98\Util\Console\Helper\ParameterHelper;
use Symfony\Component\Console\Helper\DialogHelper;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
Expand All @@ -25,7 +27,7 @@ class DeleteCommand extends AbstractCustomerCommand
protected $output;

/**
* @var \Composer\Command\Helper\DialogHelper
* @var DialogHelper
*/
protected $dialog;

Expand Down Expand Up @@ -67,9 +69,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
$this->detectMagento($output, true);
if ($this->initMagento()) {

$this->input = $input;
$this->output = $output;
$this->dialog = $this->getHelperSet()->get('dialog');
$this->input = $input;
$this->output = $output;
/** @var DialogHelper dialog */
$this->dialog = $this->getHelperSet()->get('dialog');

// Defaults
$range = $all = false;
Expand All @@ -83,22 +86,22 @@ protected function execute(InputInterface $input, OutputInterface $output)
// Delete more than one customer ?
$batchDelete = $this->dialog->askConfirmation(
$this->output,
$this->dialog->getQuestion('Delete more than 1 customer?', 'n'),
$this->getQuestion('Delete more than 1 customer?', 'n'),
false
);

if ($batchDelete) {
// Batch deletion
$all = $this->dialog->askConfirmation(
$this->output,
$this->dialog->getQuestion('Delete all customers?', 'n'),
$this->getQuestion('Delete all customers?', 'n'),
false
);

if (!$all) {
$range = $this->dialog->askConfirmation(
$this->output,
$this->dialog->getQuestion('Delete a range of customers?', 'n'),
$this->getQuestion('Delete a range of customers?', 'n'),
false
);

Expand All @@ -114,7 +117,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
if (!$range && !$all) {
// Single customer deletion
if (!$id) {
$id = $this->dialog->ask($this->output, $this->dialog->getQuestion('Customer Id'), null);
$id = $this->dialog->ask($this->output, $this->getQuestion('Customer Id'), null);
}

try {
Expand All @@ -132,7 +135,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
}
} else {

$customers = $this->getCustomerCollection()
$customers = $this->getCustomerCollection();
$customers
->addAttributeToSelect('firstname')
->addAttributeToSelect('lastname')
->addAttributeToSelect('email');
Expand All @@ -142,14 +146,14 @@ protected function execute(InputInterface $input, OutputInterface $output)
$ranges = array();
$ranges[0] = $this->dialog->askAndValidate(
$this->output,
$this->dialog->getQuestion('Range start Id', '1'),
$this->getQuestion('Range start Id', '1'),
array($this, 'validateInt'),
false,
'1'
);
$ranges[1] = $this->dialog->askAndValidate(
$this->output,
$this->dialog->getQuestion('Range end Id', '1'),
$this->getQuestion('Range end Id', '1'),
array($this, 'validateInt'),
false,
'1'
Expand Down Expand Up @@ -184,7 +188,7 @@ protected function shouldRemove()
if (!$shouldRemove) {
$shouldRemove = $this->dialog->askConfirmation(
$this->output,
$this->dialog->getQuestion('Are you sure?', 'n'),
$this->getQuestion('Are you sure?', 'n'),
false
);
}
Expand All @@ -199,10 +203,12 @@ protected function shouldRemove()
*/
protected function getCustomer($id)
{
// Get customer
/** @var \Mage_Customer_Model_Customer $customer */
$customer = $this->getCustomerModel()->load($id);
if (!$customer->getId()) {
$website = $this->getHelperSet()->get('parameter')->askWebsite($this->input, $this->output);
/** @var $parameterHelper ParameterHelper */
$parameterHelper = $this->getHelperSet()->get('parameter');
$website = $parameterHelper->askWebsite($this->input, $this->output);
$customer = $this->getCustomerModel()
->setWebsiteId($website->getId())
->loadByEmail($id);
Expand Down Expand Up @@ -263,4 +269,24 @@ public function validateInt($answer)

return $answer;
}

/**
* @param string $message
* @param string $default [optional]
*
* @return string
*/
private function getQuestion($message, $default = null)
{
$params = array($message);
$pattern = '%s: ';

if (null !== $default) {
$params[] = $default;
$pattern .= '[%s] ';

}

return vsprintf($pattern, $params);
}
}

0 comments on commit 7e76268

Please sign in to comment.