Skip to content

Commit

Permalink
Merge pull request #11 from magmodules/1.2.2
Browse files Browse the repository at this point in the history
1.2.2
  • Loading branch information
Marvin-Magmodules authored Feb 5, 2018
2 parents f0583b8 + a4f3b23 commit 8a48d65
Show file tree
Hide file tree
Showing 8 changed files with 179 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,11 @@ protected function _prepareColumns()
);

$this->addColumn(
'call_result', array(
'header' => Mage::helper('channableapi')->__('Call Result'),
'width' => '50px',
'index' => 'call_result',
'status', array(
'header' => Mage::helper('channableapi')->__('Status'),
'width' => '150px',
'index' => 'status',
'renderer' => 'channableapi/adminhtml_items_renderer_status',
)
);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
/**
* Magmodules.eu - http://www.magmodules.eu
*
* 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:
* http://opensource.org/licenses/osl-3.0.php
* 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 [email protected] so we can send you a copy immediately.
*
* @category Magmodules
* @package Magmodules_Channableapi
* @author Magmodules <[email protected]>
* @copyright Copyright (c) 2018 (http://www.magmodules.eu)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*
*
*/

class Magmodules_Channableapi_Block_Adminhtml_Items_Renderer_Status
extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{

/**
* @param Varien_Object $row
*
* @return string with html link to order view
*/
public function render(Varien_Object $row)
{
$status = $row->getData('status');
$callResult = $row->getData('call_result');
$attempts = $row->getData('attempts');

$html = $status;
$message = '';

if ($status == 'Not Found' && $attempts > 1) {
$message .= __('Removed item from que after %1 attempts.', $attempts) . '<br/>';
}

if (!empty($callResult)) {
$message .= '<i>API: "' . $callResult . '"</i><br/>';
if ($attempts > 1) {
$message .= __('Attempts: %s', $attempts);
}
}

if (!empty($message)) {
$html .= ' <a href="#" class="chanable-tooltip" >(?)<span>' . $message . '</span></a>';
}

return $html;
}

}
66 changes: 49 additions & 17 deletions app/code/community/Magmodules/Channableapi/Model/Items.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,14 @@ public function reformatFeedData($productData, $storeId, $type)

if (isset($productData['price'])) {
if (!empty($productData['price'])) {
$data['price'] = number_format(preg_replace('/([^0-9\.,])/i', '', $productData['price']), 2);
$priceClean = preg_replace('/([^0-9\.,])/i', '', $productData['price']);
$data['price'] = number_format($priceClean, 2, '.', '');
} else {
$data['price'] = '0.00';
}
if (!empty($productData['special_price'])) {
$data['discount_price'] = number_format(preg_replace('/([^0-9\.,])/i', '', $productData['special_price']), 2);
$discountPriceClean = preg_replace('/([^0-9\.,])/i', '', $productData['special_price']);
$data['discount_price'] = number_format($discountPriceClean, 2, '.', '');
} else {
$data['discount_price'] = '';
}
Expand Down Expand Up @@ -145,23 +147,29 @@ public function runUpdate($storeId, $items = '')
{
$config = $this->getConfig($storeId);
if (empty($items)) {
$items = $this->getCollection()
$itemCollection = $this->getCollection()
->addFieldToFilter('store_id', $storeId)
->addFieldToFilter('needs_update', 1)
->setOrder('updated_at', 'ASC')
->setPageSize($config['limit'])
->setCurPage(1)
->addFieldToFilter('needs_update', 1)
->load();

if (is_object($itemCollection) && $itemCollection->count()) {
$items = $itemCollection;
}
}

if ($items->count()) {
if (!empty($items)) {
$postData = $this->getProductData($items, $storeId, $config);
$postResult = $this->postData($postData, $config);
$this->updateData($postResult);

return $postResult;
} else {
$date = date("Y-m-d H:i:s", Mage::getModel('core/date')->timestamp(time()));
$postResult = array('status' => 'success', 'qty' => 0, 'updates' => 0, 'date' => $date, 'store_id' => $config['store_id']);
}

return false;
return $postResult;
}

/**
Expand Down Expand Up @@ -331,13 +339,14 @@ public function postData($postData, $config)

$results['result'] = json_decode($result, true);
$results['qty'] = count($postData);
$results['updates'] = count($postData);
$results['date'] = date("Y-m-d H:i:s", Mage::getModel('core/date')->timestamp(time()));
$results['store_id'] = $config['store_id'];
$results['webhook'] = $config['webhook'];
$results['post_data'] = $postData;

if ($config['debug']) {
Mage::getSingleton('adminhtml/session')->addNotice('DEBUG: [' . json_encode($postData) . ']');
Mage::getSingleton('adminhtml/session')->addNotice('DEBUG: [' . json_encode($post) . ']');
}

if (!empty($config['log'])) {
Expand All @@ -364,7 +373,7 @@ public function postData($postData, $config)
*/
public function getNonApiFields()
{
return array('item_id', 'availability', 'parent_id', 'updated_at', 'title');
return array('item_id', 'availability', 'parent_id', 'updated_at', 'title', 'store_id');
}

/**
Expand All @@ -376,25 +385,48 @@ public function updateData($postResult)
$postData = $postResult['post_data'];
$items = isset($itemsResult['content']) ? $itemsResult['content'] : array();
$status = isset($postResult['status']) ? $postResult['status'] : '';

if ($status == 'success') {
foreach ($items as $item) {
$key = array_search($item['id'], array_column($postData, 'id'));
$key = array_search($item['id'], array_column($postData, 'product_id'));
$postData[$key]['call_result'] = $item['message'];
$postData[$key]['status'] = ucfirst($item['status']);
$postData[$key]['needs_update'] = ($item['status'] == 'success') ? 0 : 1;
$postData[$key]['last_call'] = Mage::getModel('core/date')->gmtDate('Y-m-d H:i:s');

if ($item['status'] == 'error') {
$oldStatus = $this->load($postData[$key]['item_id'])->getStatus();
if ($oldStatus == 'Error') {
$postData[$key]['status'] = 'Not Found';
$postData[$key]['needs_update'] = 0;
$postData[$key]['updated_at'] = Mage::getModel('core/date')->gmtDate('Y-m-d H:i:s');
$postData[$key]['attempts'] = 1;
if ($postData[$key]['needs_update']) {
$oldItem = $this->load($postData[$key]['item_id']);
if ($oldItem->getStatus() == 'Error') {
$postData[$key]['attempts'] = $oldItem->getAttempts() + 1;
if ($postData[$key]['attempts'] > 3) {
$postData[$key]['status'] = 'Not Found';
$postData[$key]['needs_update'] = 0;
}
}
}
}
}

foreach ($postData as $key => $data) {
if(!isset($data['status'])) {
$data['call_result'] = 'No result and/or error in post data';
$data['status'] = 'Error';
$data['needs_update'] = 1;
$data['last_call'] = Mage::getModel('core/date')->gmtDate('Y-m-d H:i:s');
$data['updated_at'] = Mage::getModel('core/date')->gmtDate('Y-m-d H:i:s');
$data['attempts'] = 1;
if ($data['needs_update']) {
$oldItem = $this->load($data['item_id']);
if ($oldItem->getStatus() == 'Error') {
$data['attempts'] = $oldItem->getAttempts() + 1;
if ($data['attempts'] > 3) {
$data['status'] = 'Not Found';
$data['needs_update'] = 0;
}
}
}
}
try {
$this->setData($data)->save();
} catch (\Exception $e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function shopitemApi()
$appEmulation = Mage::getSingleton('core/app_emulation');
$initialEnvironmentInfo = $appEmulation->startEnvironmentEmulation($storeId);
if ($result = Mage::getModel('channableapi/items')->runUpdate($storeId)) {
if ($result['status'] == 'SUCCESS') {
if ($result['status'] == 'success') {
$html = $result['status'] . ' - Updates: ' . $result['updates'] . '<br/>';
$html .= '<small>Date: ' . $result['date'] . ' - ';
$html .= 'Time: ' . number_format((microtime(true) - $timeStart), 4) . '</small>';
Expand Down Expand Up @@ -172,6 +172,8 @@ public function cleanItems()
Mage::getSingleton('core/resource')->getConnection('core_read')->delete($debug, $where);
}
} catch (Exception $e) {
/** @var Magmodules_Channableapi_Model_Items $model */
$model = Mage::getModel('channableapi/items');
$model->addTolog('cleanItems', $e->getMessage(), 2);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,18 @@ public function updateItemAction()
if (!empty($itemId)) {
$item = Mage::getModel('channableapi/items')->load($itemId);
$result = Mage::getModel('channableapi/items')->runUpdate($item->getStoreId(), array($item));
if ($result['status'] == 'SUCCESS') {
Mage::getSingleton('adminhtml/session')->addSuccess(
Mage::helper('channableapi')->__('Item updated')
);
if ($result['status'] == 'success') {
if (!empty($result['result']['content'][0]['message'])) {
Mage::getSingleton('adminhtml/session')->addSuccess(
Mage::helper('channableapi')->__(
'Item updated, result: %s',
$result['result']['content'][0]['message'])
);
} else {
Mage::getSingleton('adminhtml/session')->addSuccess(
Mage::helper('channableapi')->__('Item updated')
);
}
} else {
Mage::getSingleton('adminhtml/session')->addError(
Mage::helper('channableapi')->__('Could not update item')
Expand Down
2 changes: 1 addition & 1 deletion app/code/community/Magmodules/Channableapi/etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<config>
<modules>
<Magmodules_Channableapi>
<version>1.2.1</version>
<version>1.2.2</version>
</Magmodules_Channableapi>
</modules>
<global>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/**
* Magmodules.eu - http://www.magmodules.eu
*
* 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:
* http://opensource.org/licenses/osl-3.0.php
* 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 [email protected] so we can send you a copy immediately.
*
* @category Magmodules
* @package Magmodules_Channableapi
* @author Magmodules <[email protected]>
* @copyright Copyright (c) 2018 (http://www.magmodules.eu)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/

/** @var $installer Mage_Catalog_Model_Resource_Setup */
$installer = $this;
$installer->startSetup();

try {
$installer->run("ALTER TABLE {$this->getTable('channable_items')} ADD `attempts` smallint(5) DEFAULT 0 AFTER `last_call`;");
$installer->run("ALTER TABLE {$this->getTable('channable_items')} ADD `status` varchar(255) NOT NULL AFTER `last_call`;");

} catch (Exception $e) {
Mage::log($e->getMessage());
}

$installer->endSetup();
17 changes: 17 additions & 0 deletions skin/adminhtml/default/default/magmodules/channableapi/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,21 @@
.channableapi-notice .more {
float: right;
margin-right: 10px;
}

a.chanable-tooltip span {
display: none;
padding: 2px 3px;
margin-left: 8px;
width: 550px;
}

a.chanable-tooltip:hover span {
display: block;
position: absolute;
background: #FFFFFF;
border: 1px solid #CCCCCC;
color: #6C6C6C;
width: auto;
margin-right: 20px;
}

0 comments on commit 8a48d65

Please sign in to comment.