Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
firstred committed May 3, 2018
1 parent 8c39b1a commit c4738e6
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 39 deletions.
4 changes: 0 additions & 4 deletions classes/MailChimpCart.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,6 @@ public static function getCarts($idShops = null, $offset = 0, $limit = 0, $remai

$defaultCurrency = Currency::getDefaultCurrency();
$defaultCurrencyCode = $defaultCurrency->iso_code;
$mailChimpShop = MailChimpShop::getByShopId($idShops);
if (!Validate::isLoadedObject($mailChimpShop)) {
return false;
}
foreach ($results as &$cart) {
$cartObject = new Cart($cart['id_cart']);

Expand Down
8 changes: 4 additions & 4 deletions classes/MailChimpOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ class MailChimpOrder extends ObjectModel
'table' => 'mailchimp_order',
'primary' => 'id_mailchimp_order',
'fields' => [
'id_order' => ['type' => self::TYPE_INT, 'validate' => 'isInt', 'required' => true, 'db_type' => 'INT(11) UNSIGNED'],
'last_synced' => ['type' => self::TYPE_DATE, 'validate' => 'isDate', 'required' => true, 'default' => '1970-01-01 00:00:00', 'db_type' => 'DATETIME' ],
'id_order' => ['type' => self::TYPE_INT, 'validate' => 'isInt', 'required' => true, 'db_type' => 'INT(11) UNSIGNED'],
'last_synced' => ['type' => self::TYPE_DATE, 'validate' => 'isDate', 'required' => true, 'default' => '1970-01-01 00:00:00', 'db_type' => 'DATETIME' ],
],
];
// @codingStandardsIgnoreStart
Expand Down Expand Up @@ -96,7 +96,7 @@ public static function getOrders($idShops = null, $offset = 0, $limit = 0, $rema
if ($count) {
$sql->select('COUNT(*)');
} else {
$sql->select('o.`id_order`, o.`date_add`, o.`date_upd`, c.*, mo.`last_synced`, mt.`mc_tc`, mt.`mc_cid`, mc.`landing_site`');
$sql->select('o.`id_order`, o.`date_add`, o.`date_upd`, c.*, mo.`last_synced`, mt.`mc_tc`, mt.`mc_cid`, mt.`landing_site`');
$sql->select('cu.`id_customer`, cu.`email`, cu.`firstname`, cu.`lastname`, cu.`birthday`, cu.`newsletter`');
$sql->select('l.`language_code`, o.`id_shop`');
}
Expand All @@ -109,7 +109,7 @@ public static function getOrders($idShops = null, $offset = 0, $limit = 0, $rema
$sql->where('o.`id_order` IN (SELECT `id_order` FROM `'._DB_PREFIX_.'order_detail`)');
$sql->where('o.`current_state` IN ('.implode(',', array_map('intval', MailChimp::getValidOrderStatuses())).')');
$sql->where('o.`date_add` > \''.pSQL(MailChimp::getOrderDateCutoff()).'\'');
$sql->leftJoin(bqSQL(self::$definition['table']), 'mo', 'mo.`id_order` = o.`id_order`');
$sql->leftJoin(bqSQL(static::$definition['table']), 'mo', 'mo.`id_order` = o.`id_order`');
if ($remaining) {
$sql->where('mo.`last_synced` IS NULL OR (mo.`last_synced` < o.`date_upd` AND mo.`last_synced` > \'2000-01-01 00:00:00\')');
}
Expand Down
15 changes: 10 additions & 5 deletions classes/MailChimpProduct.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ class MailChimpProduct extends ObjectModel
'table' => 'mailchimp_product',
'primary' => 'id_mailchimp_product',
'fields' => [
'id_product' => ['type' => self::TYPE_INT, 'validate' => 'isInt', 'required' => true, 'db_type' => 'INT(11) UNSIGNED' ],
'id_shop' => ['type' => self::TYPE_INT, 'validate' => 'isInt', 'required' => true, 'db_type' => 'INT(11) UNSIGNED' ],
'last_synced' => ['type' => self::TYPE_DATE, 'validate' => 'isDate', 'required' => true, 'default' => '1970-01-01 00:00:00', 'db_type' => 'DATETIME' ],
'id_product' => ['type' => self::TYPE_INT, 'validate' => 'isInt', 'required' => true, 'db_type' => 'INT(11) UNSIGNED' ],
'id_shop' => ['type' => self::TYPE_INT, 'validate' => 'isInt', 'required' => true, 'db_type' => 'INT(11) UNSIGNED' ],
'last_synced' => ['type' => self::TYPE_DATE, 'validate' => 'isDate', 'required' => true, 'default' => '1970-01-01 00:00:00', 'db_type' => 'DATETIME' ],
],
];
// @codingStandardsIgnoreStart
Expand All @@ -74,7 +74,7 @@ class MailChimpProduct extends ObjectModel
* @param bool $remaining
* @param bool $count
*
* @return array|false|int
* @return array|int
*
* @since 1.1.0
* @throws PrestaShopException
Expand Down Expand Up @@ -114,7 +114,12 @@ public static function getProducts($idShops = null, $offset = 0, $limit = 0, $re
return (int) Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($sql);
}

return (array) Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql);
$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql);
if (!is_array($result)) {
$result = [];
}

return $result;
} catch (PrestaShopException $e) {
Context::getContext()->controller->errors[] = Translate::getModuleTranslation('mailchimp', 'Unable to count products', 'mailchimp');

Expand Down
27 changes: 12 additions & 15 deletions classes/MailChimpShop.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ public static function getShops($active = false)
*
* @param int $idShop
*
* @return MailChimpShop|false
* @return MailChimpShop
*
* @throws Adapter_Exception
* @throws PrestaShopDatabaseException
* @throws PrestaShopException
Expand All @@ -117,17 +118,15 @@ public static function getByShopId($idShop)
$sql = new DbQuery();
$sql->select('s.`'.bqSQL(Shop::$definition['primary']).'`, ms.*');
$sql->from('shop', 's');
$sql->leftJoin(bqSQL(self::$definition['table']), 'ms', 's.`'.bqSQL(Shop::$definition['primary']).'` = ms.`'.bqSQL(Shop::$definition['primary']).'`');
$sql->leftJoin(bqSQL(static::$definition['table']), 'ms', 's.`'.bqSQL(Shop::$definition['primary']).'` = ms.`'.bqSQL(Shop::$definition['primary']).'`');
$sql->where('s.`id_shop` = '.(int) $idShop);

$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($sql);
if (!$result) {
return false;
$mcs = new static();
if (is_array($result)) {
$mcs->hydrate($result);
}

$mcs = new self();
$mcs->hydrate($result);

return $mcs;
}

Expand All @@ -148,7 +147,7 @@ public static function getByShopIds($idShops)
$sql = new DbQuery();
$sql->select('s.`'.bqSQL(Shop::$definition['primary']).'`, ms.*');
$sql->from('shop', 's');
$sql->innerJoin(bqSQL(self::$definition['table']), 'ms', 's.`'.bqSQL(Shop::$definition['primary']).'` = ms.`'.bqSQL(Shop::$definition['primary']).'`');
$sql->innerJoin(bqSQL(static::$definition['table']), 'ms', 's.`'.bqSQL(Shop::$definition['primary']).'` = ms.`'.bqSQL(Shop::$definition['primary']).'`');
$sql->where('s.`'.bqSQL(Shop::$definition['primary']).'` IN ('.implode(',', array_map('intval', $idShops)).')');

$results = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql);
Expand All @@ -171,7 +170,7 @@ public static function getByShopIds($idShops)
*
* @param string $idList
*
* @return MailChimpShop|false
* @return MailChimpShop
*
* @throws Adapter_Exception
* @throws PrestaShopDatabaseException
Expand All @@ -182,17 +181,15 @@ public static function getByListId($idList)
$sql = new DbQuery();
$sql->select('s.`'.bqSQL(Shop::$definition['primary']).'`, ms.*');
$sql->from('shop', 's');
$sql->leftJoin(bqSQL(self::$definition['table']), 'ms', 's.`'.bqSQL(Shop::$definition['primary']).'` = ms.`'.bqSQL(Shop::$definition['primary']).'`');
$sql->leftJoin(bqSQL(static::$definition['table']), 'ms', 's.`'.bqSQL(Shop::$definition['primary']).'` = ms.`'.bqSQL(Shop::$definition['primary']).'`');
$sql->where('s.`id_list` = \''.pSQL($idList).'\'');

$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($sql);
if (!$result) {
return false;
$mcs = new static();
if (is_array($result)) {
$mcs->hydrate($result);
}

$mcs = new self();
$mcs->hydrate($result);

return $mcs;
}
}
18 changes: 7 additions & 11 deletions mailchimp.php
Original file line number Diff line number Diff line change
Expand Up @@ -986,9 +986,9 @@ public function displayAjaxExportAllCarts()
$count = (int) Tools::getValue('count');

if ($exportRemaining) {
$success = $this->exportProducts(0, $idShops, $exportRemaining);
$success = $this->exportCarts(0, $idShops, $exportRemaining);
} else {
$success = $this->exportProducts(($count - 1) * static::EXPORT_CHUNK_SIZE, $idShops, $exportRemaining);
$success = $this->exportCarts(($count - 1) * static::EXPORT_CHUNK_SIZE, $idShops, $exportRemaining);
}

die(json_encode([
Expand Down Expand Up @@ -1030,9 +1030,9 @@ public function displayAjaxExportAllOrders()
}

if ($exportRemaining) {
$success = $this->exportProducts(0, $idShops, $exportRemaining);
$success = $this->exportOrders(0, $idShops, $exportRemaining);
} else {
$success = $this->exportProducts(($count - 1) * static::EXPORT_CHUNK_SIZE, $idShops, $exportRemaining);
$success = $this->exportOrders(($count - 1) * static::EXPORT_CHUNK_SIZE, $idShops, $exportRemaining);
}
die(json_encode([
'success' => $success,
Expand Down Expand Up @@ -1461,7 +1461,7 @@ protected function registerWebhook($idList, $url = null)
}

/**
* @param array $list
* @param MailChimpSubscriber[] $list
*
* @return bool
*
Expand All @@ -1477,12 +1477,12 @@ protected function importList($list)
if (!$client) {
return false;
}
$promises = call_user_func(function () use ($list, $client) {
$mailChimpShop = MailChimpShop::getByShopId(Context::getContext()->shop->id);
$promises = call_user_func(function () use ($list, $client, $mailChimpShop) {
for ($i = 0; $i < count($list); $i++) {
/** @var MailChimpSubscriber $subscriber */
$subscriber = $list[$i];
$hash = md5(mb_strtolower($subscriber->getEmail()));
$mailChimpShop = MailChimpShop::getByShopId(Context::getContext()->shop->id);
yield $client->putAsync(
"lists/{$mailChimpShop->list_id}/members/{$hash}",
[
Expand Down Expand Up @@ -3990,11 +3990,9 @@ public static function signalSyncStart($idShops)
}
});

static::signalSyncStart($idShops);
(new EachPromise($promises, [
'concurrency' => static::API_CONCURRENCY,
]))->promise()->wait();
static::signalSyncStop($idShops);
}

/**
Expand Down Expand Up @@ -4026,11 +4024,9 @@ public static function signalSyncStop($idShops)
}
});

static::signalSyncStart($idShops);
(new EachPromise($promises, [
'concurrency' => static::API_CONCURRENCY,
]))->promise()->wait();
static::signalSyncStop($idShops);
}

/**
Expand Down

0 comments on commit c4738e6

Please sign in to comment.