Skip to content

Commit

Permalink
[+] BO : Send e-mail when import is finished
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxime Biloé authored and xGouley committed Jan 8, 2016
1 parent d692fae commit 6820ae0
Show file tree
Hide file tree
Showing 5 changed files with 195 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -284,12 +284,22 @@
</label>
</div>
</div>
<!--
{*if empty($files_to_import)*}
<div class="alert alert-info">{l s='You must upload a file in order to proceed to the next step'}</div>
{*if !count($files_to_import)*}
<p>{l s='There is no CSV file available. Please upload one using the \'Upload\' button above.'}</p>
-->
<div class="form-group">
<label for="sendemail" class="control-label col-lg-4">
<span data-toggle="tooltip" class="label-tooltip" data-original-title="{l s='Sends an email to let you know your import is complete. It can be useful when handling large files, as the import may take some time.'}">
{l s='Send notification email'}</label>
</span>
<div class="col-lg-8">
<label class="switch-light prestashop-switch fixed-width-lg">
<input id="sendemail" name="sendemail" type="checkbox" checked="checked" />
<span>
<span>{l s='Yes'}</span>
<span>{l s='No'}</span>
</span>
<a class="slide-button btn"></a>
</label>
</div>
</div>
<div class="panel-footer">
<button type="submit" name="submitImportFile" id="submitImportFile" class="btn btn-default pull-right" >
<i class="process-icon-next"></i> <span>{l s='Next step'}</span>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
<div class="form-group">
<label class="control-label col-lg-3" for="newImportMatchs">{l s='Save your data matching configuration'}</label>
<div class="col-lg-7">
<input type="text" name="newImportMatchs" id="newImportMatchs" />
<input type="text" name="newImportMatchs" id="newImportMatchs" />
</div>
<div class="col-lg-2">
<a id="saveImportMatchs" class="btn btn-default" href="#"><i class="icon-save"></i> {l s='Save'}</a>
Expand All @@ -99,6 +99,7 @@
<input type="hidden" name="regenerate" value="{$fields_value.regenerate}" />
<input type="hidden" name="entity" value="{$fields_value.entity}" />
<input type="hidden" name="iso_lang" value="{$fields_value.iso_lang}" />
<input type="hidden" name="sendemail" value="{$fields_value.sendemail}" />
{if $fields_value.truncate}
<input type="hidden" name="truncate" value="1" />
{/if}
Expand Down
88 changes: 56 additions & 32 deletions controllers/admin/AdminImportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,7 @@ public function renderView()
'truncate' => Tools::getValue('truncate'),
'forceIDs' => Tools::getValue('forceIDs'),
'regenerate' => Tools::getValue('regenerate'),
'sendemail' => Tools::getValue('sendemail'),
'match_ref' => Tools::getValue('match_ref'),
'separator' => $this->separator,
'multiple_value_separator' => $this->multiple_value_separator
Expand Down Expand Up @@ -1162,7 +1163,7 @@ public function categoryImport($offset = false, $limit = false)
$line = $this->utf8EncodeArray($line);
}
$info = AdminImportController::getMaskedRow($line);

$this->categoryImportOne(
$info,
$default_language_id,
Expand All @@ -1177,7 +1178,7 @@ public function categoryImport($offset = false, $limit = false)
/* Import has finished, we can regenerate the categories nested tree */
Category::regenerateEntireNtree();
$this->closeCsvFile($handle);

return $line_count;
}

Expand Down Expand Up @@ -1365,7 +1366,7 @@ public function productImport($offset = false, $limit = false)
$this->receiveTab();
$handle = $this->openCsvFile($offset);
if (!$handle) return false;

$default_language_id = (int)Configuration::get('PS_LANG_DEFAULT');
$id_lang = Language::getIdByIso(Tools::getValue('iso_lang'));
if (!Validate::isUnsignedId($id_lang)) {
Expand All @@ -1388,7 +1389,7 @@ public function productImport($offset = false, $limit = false)
$line = $this->utf8EncodeArray($line);
}
$info = AdminImportController::getMaskedRow($line);

$this->productImportOne(
$info,
$default_language_id,
Expand All @@ -1404,7 +1405,7 @@ public function productImport($offset = false, $limit = false)
Module::processDeferedFuncCall();
Module::processDeferedClearCache();
Tag::updateTagCount();

return $line_count;
}

Expand Down Expand Up @@ -2068,7 +2069,7 @@ public function attributeImport($offset = false, $limit = false)
$this->receiveTab();
$handle = $this->openCsvFile($offset);
if (!$handle) return false;

AdminImportController::setLocale();

$convert = Tools::getValue('convert');
Expand All @@ -2087,7 +2088,7 @@ public function attributeImport($offset = false, $limit = false)
}
$info = AdminImportController::getMaskedRow($line);
$info = array_map('trim', $info);

$this->attributeImportOne(
$info,
$default_language,
Expand All @@ -2098,7 +2099,7 @@ public function attributeImport($offset = false, $limit = false)
);
}
$this->closeCsvFile($handle);

return $line_count;
}

Expand Down Expand Up @@ -2506,7 +2507,7 @@ public function customerImport($offset = false, $limit = false)
$this->receiveTab();
$handle = $this->openCsvFile($offset);
if (!$handle) return false;

$default_language_id = (int)Configuration::get('PS_LANG_DEFAULT');
$id_lang = Language::getIdByIso(Tools::getValue('iso_lang'));
if (!Validate::isUnsignedId($id_lang)) {
Expand Down Expand Up @@ -2535,7 +2536,7 @@ public function customerImport($offset = false, $limit = false)
);
}
$this->closeCsvFile($handle);

return $line_count;
}

Expand Down Expand Up @@ -2757,7 +2758,7 @@ public function addressImport($offset = false, $limit = false)
$default_language_id = (int)Configuration::get('PS_LANG_DEFAULT');
$handle = $this->openCsvFile($offset);
if (!$handle) return false;

AdminImportController::setLocale();

$convert = Tools::getValue('convert');
Expand All @@ -2777,10 +2778,10 @@ public function addressImport($offset = false, $limit = false)
);
}
$this->closeCsvFile($handle);

return $line_count;
}

protected function addressImportOne($info, $force_ids)
{
AdminImportController::setDefaultValues($info);
Expand Down Expand Up @@ -2956,7 +2957,7 @@ public function manufacturerImport($offset = false, $limit = false)
$this->receiveTab();
$handle = $this->openCsvFile($offset);
if (!$handle) return false;

AdminImportController::setLocale();

$shop_is_feature_active = Shop::isFeatureActive();
Expand All @@ -2980,7 +2981,7 @@ public function manufacturerImport($offset = false, $limit = false)
);
}
$this->closeCsvFile($handle);

return $line_count;
}

Expand Down Expand Up @@ -3058,7 +3059,7 @@ public function supplierImport($offset = false, $limit = false)
$this->receiveTab();
$handle = $this->openCsvFile($offset);
if (!$handle) return false;

AdminImportController::setLocale();

$shop_is_feature_active = Shop::isFeatureActive();
Expand All @@ -3074,7 +3075,7 @@ public function supplierImport($offset = false, $limit = false)
}
$info = AdminImportController::getMaskedRow($line);


$this->supplierImportOne(
$info,
$shop_is_feature_active,
Expand All @@ -3083,10 +3084,10 @@ public function supplierImport($offset = false, $limit = false)
);
}
$this->closeCsvFile($handle);

return $line_count;
}

protected function supplierImportOne($info, $shop_is_feature_active, $regenerate, $force_ids)
{
AdminImportController::setDefaultValues($info);
Expand Down Expand Up @@ -3159,7 +3160,7 @@ public function aliasImport($offset = false, $limit = false)
$this->receiveTab();
$handle = $this->openCsvFile($offset);
if (!$handle) return false;

AdminImportController::setLocale();

$convert = Tools::getValue('convert');
Expand All @@ -3179,10 +3180,10 @@ public function aliasImport($offset = false, $limit = false)
);
}
$this->closeCsvFile($handle);

return $line_count;
}

protected function aliasImportOne($info, $force_ids)
{
AdminImportController::setDefaultValues($info);
Expand Down Expand Up @@ -3232,7 +3233,7 @@ public function supplyOrdersImport($offset = false, $limit = false)
$this->receiveTab();
$handle = $this->openCsvFile($offset);
if (!$handle) return false;

AdminImportController::setLocale();

$convert = Tools::getValue('convert');
Expand All @@ -3255,7 +3256,7 @@ public function supplyOrdersImport($offset = false, $limit = false)
}
// closes
$this->closeCsvFile($handle);

return $line_count;
}

Expand Down Expand Up @@ -3358,7 +3359,7 @@ public function supplyOrdersDetailsImport($offset = false, $limit = false)
$this->receiveTab();
$handle = $this->openCsvFile($offset);
if (!$handle) return false;

AdminImportController::setLocale();

$products = array(); // FIXME: cross foreach! must fill between each call with precedent data
Expand Down Expand Up @@ -3386,7 +3387,7 @@ public function supplyOrdersDetailsImport($offset = false, $limit = false)
}
// closes
$this->closeCsvFile($handle);

return $line_count;
}

Expand Down Expand Up @@ -3712,7 +3713,7 @@ public function postProcess()
Db::getInstance()->enableCache();
return parent::postProcess();
}

public function importByGroups($offset = false, $limit = false, &$results = null)
{
// Check if the CSV file exist
Expand Down Expand Up @@ -3868,20 +3869,43 @@ public static function getPath($file = '')
return (defined('_PS_HOST_MODE_') ? _PS_ROOT_DIR_ : _PS_ADMIN_DIR_).DIRECTORY_SEPARATOR.'import'
.DIRECTORY_SEPARATOR.$file;
}

public function ajaxProcessImport()
{
$offset = (int)Tools::getValue('offset');
$limit = (int)Tools::getValue('limit');

sleep(2);

$results = array();
$this->importByGroups($offset, $limit, $results);

// TODO : envoi de mail ssi $results['isFinished'] == 1


if ((bool)$results['isFinished'] && (bool)Tools::getValue('sendemail')) {
$templateVars = array(
'{firstname}' => $this->context->employee->firstname,
'{lastname}' => $this->context->employee->lastname,
'{filename}' => Tools::getValue('csv')
);
@Mail::Send(
(int)$this->context->employee->id_lang,
'import',
Mail::l('Import complete', (int)$this->context->employee->id_lang),
$templateVars,
$this->context->employee->email,
$this->context->employee->firstname.' '.$this->context->employee->lastname,
null,
null,
null,
null,
_PS_MAIL_DIR_,
true,
(int)$this->context->shop->id
);
}

die(json_encode($results));
}

public function initModal()
{
parent::initModal();
Expand Down
Loading

0 comments on commit 6820ae0

Please sign in to comment.