<?php
namespace App\EventListener;
use Pimcore\Model\Asset;
use Pimcore\Event\Model\ElementEventInterface;
use Pimcore\Event\Model\DataObjectEvent;
use Pimcore\Event\Model\AssetEvent;
use Pimcore\Event\Model\DocumentEvent;
use Pimcore\Event\Model\VersionEvent;
use \Pimcore\Model\DataObject;
use Symfony\Component\EventDispatcher\GenericEvent;
use Pimcore\Model\Element\ValidationException;
class DefaultValueProvider extends \Pimcore\Bundle\AdminBundle\Controller\AdminController {
public function onPostAdd(ElementEventInterface $e) {
if ($e instanceof DataObjectEvent) {
$objectData = $e->getObject();
if($objectData->gettype() == 'object'){
if($objectData->getClassName() == 'ProductCategory') {
// $categoryAddUrl = "https://integration-5ojmyuq-23txbi6folvo6.ap-3.magentosite.cloud/rest/V1/bridge/catalog/category/add";
$categoryAddUrl = "http://local.itc.com/rest/V1/bridge/catalog/category/addToData";
$data = array(
"category_id" => $objectData->getId(),
"store_view_id" => "0"
);
$productSyncKey = "qzgg1vbpgwk6pjctbv6ku2a65906ocpb";
// $productSyncKey = "ah2zed4d9rcqgxc4ds16bh6wssdm8xni";
$data_json = json_encode(array("data" => $data));
$curl = curl_init(); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_json);
curl_setopt($curl, CURLOPT_URL, $categoryAddUrl);
curl_setopt($curl, CURLOPT_HTTPHEADER, array( 'Authorization: Bearer ' . $productSyncKey, 'Accept: application/json', 'Content-Type:application/json', ));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
$result = curl_exec($curl);
$resultArray = json_decode($result);
curl_close($curl);
}
if($objectData->getClassName() == 'Products') {
// $productSyncKey = "enlwq7pkg6t2od9blt93coomo7g6p9p0"; // Live Integration
$productSyncKey = "qzgg1vbpgwk6pjctbv6ku2a65906ocpb"; // Local
// $productUpdatedUrl = 'https://integration-5ojmyuq-23txbi6folvo6.ap-3.magentosite.cloud/rest/V1/bridge/catalog/product/add';
$productUpdatedUrl = 'http://local.itc.com/rest/V1/bridge/catalog/product/add';
// echo $productUpdatedUrl.'<br>';
// echo $productSyncKey;
$data = array( "product_id" => $objectData->getId(),"store_view_id" => "0", );
$data_json = json_encode(array("data" => $data));
$curl = curl_init();
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_json);
curl_setopt($curl, CURLOPT_URL, $productUpdatedUrl);
curl_setopt($curl, CURLOPT_HTTPHEADER, array( 'Authorization: Bearer ' . $productSyncKey, 'Accept: application/json', 'Content-Type:application/json', ));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
$result = curl_exec($curl);
$resultArray = json_decode($result);
curl_close($curl);
// echo '<pre>'; print_r($resultArray); exit;
}
$productClasses = array(
'Products' => 'SAP',
'ProductITCStore' => 'ITC Store',
'ProductAmazon' => 'Amazon',
'ProductBigBasket' => 'Big Basket',
'ProductBlinkit' => 'Blinkit',
'ProductFlipkart' => 'Flipkart',
'ProductJioMart' => 'JioMart',
'ProductSwiggy' => 'Swiggy',
'ProductZepto' => 'Zepto'
);
if (array_key_exists($objectData->getClassName(),$productClasses)) {
$folder = DataObject\Service::createFolderByPath('/Products/'.$productClasses[$objectData->getClassName()]);
$folderId = $folder->getId();
$objectData->seto_parentId($folderId);
$objectData->save();
}
if($objectData->getClassName() == 'Products') {
// Create Objects
// $productObj = DataObject\Products::getById($objectData->getId());
// $this->createMarketPlaceProduct($productObj);
}
} else if($objectData->gettype() == 'folder'){
}
}
}
public function createMarketPlaceProduct($productObj) {
$key = $productObj->getkey();
$newObject = new DataObject\ProductAmazon();
$newObject->setKey(\Pimcore\Model\Element\Service::getValidKey('A_'.$key, 'object'));
$newObject->setstoreProduct($productObj);
$newObject->seto_parentId('1');
$newObject->save(["versionNote" => "Product created from SAP"]);
$newObject = new DataObject\ProductBigBasket();
$newObject->setKey(\Pimcore\Model\Element\Service::getValidKey('BB_'.$key, 'object'));
$newObject->seto_parentId('1');
$newObject->setstoreProduct($productObj);
$newObject->save(["versionNote" => "Product created from SAP"]);
$newObject = new DataObject\ProductBlinkit();
$newObject->setKey(\Pimcore\Model\Element\Service::getValidKey('BI_'.$key, 'object'));
$newObject->seto_parentId('1');
$newObject->setstoreProduct($productObj);
$newObject->save(["versionNote" => "Product created from SAP"]);
$newObject = new DataObject\ProductFlipkart();
$newObject->setKey(\Pimcore\Model\Element\Service::getValidKey('FK_'.$key, 'object'));
$newObject->seto_parentId('1');
$newObject->setstoreProduct($productObj);
$newObject->save(["versionNote" => "Product created from SAP"]);
$newObject = new DataObject\ProductJioMart();
$newObject->setKey(\Pimcore\Model\Element\Service::getValidKey('JM_'.$key, 'object'));
$newObject->seto_parentId('1');
$newObject->setstoreProduct($productObj);
$newObject->save(["versionNote" => "Product created from SAP"]);
$newObject = new DataObject\ProductSwiggy();
$newObject->setKey(\Pimcore\Model\Element\Service::getValidKey('SG_'.$key, 'object'));
$newObject->seto_parentId('1');
$newObject->setstoreProduct($productObj);
$newObject->save(["versionNote" => "Product created from SAP"]);
$newObject = new DataObject\ProductZepto();
$newObject->setKey(\Pimcore\Model\Element\Service::getValidKey('Zepto_'.$key, 'object'));
$newObject->seto_parentId('1');
$newObject->setstoreProduct($productObj);
$newObject->save(["versionNote" => "Product created from SAP"]);
$newObject = new DataObject\ProductITCStore();
$newObject->setKey(\Pimcore\Model\Element\Service::getValidKey('Store_'.$key, 'object'));
$newObject->seto_parentId('1');
$newObject->setstoreProduct($productObj);
$newObject->save(["versionNote" => "Product created from SAP"]);
}
function validateAlphanumeric($str) {
if (strlen($str) != 40) {
return false;
}
// Check the first 5 characters are numeric
if (!preg_match('/^\d{5}/', $str)) {
return false;
}
// Check the sixth character is a space
if ($str[5] != ' ') {
return false;
}
// Check the remaining 34 characters are alphabetic
if (!preg_match('/^[a-zA-Z]{34}$/', substr($str, 6))) {
return false;
}
// String passed all tests, so it's valid
return true;
}
function validateAlphabetsOnly($string) {
// The regular expression /^[A-Za-z]+$/ matches one or more alphabetical characters from A to Z (both uppercase and lowercase)
if (preg_match('/^[A-Za-z]+$/', $string)) {
return true; // The string only contains alphabetic characters
} else {
return false; // The string contains non-alphabetic characters
}
}
public function onPreUpdate(ElementEventInterface $e) {
if ($e instanceof DataObjectEvent) {
$objectData = $e->getObject();
if($objectData->gettype() == 'object'){
$productClasses = array(
'Products' => 'SAP',
'ProductITCStore' => 'ITC Store',
'ProductAmazon' => 'Amazon',
'ProductBigBasket' => 'Big Basket',
'ProductBlinkit' => 'Blinkit',
'ProductFlipkart' => 'Flipkart',
'ProductJioMart' => 'JioMart',
'ProductSwiggy' => 'Swiggy',
'ProductZepto' => 'Zepto'
);
if (array_key_exists($objectData->getClassName(),$productClasses)) {
$folder = DataObject\Service::createFolderByPath('/Products/'.$productClasses[$objectData->getClassName()]);
$folderId = $folder->getId();
$objectData->seto_parentId($folderId);
}
if($objectData->getClassName() == 'ProductFlipkart') {
$manufacturing_address = trim($objectData->getmanufacturing_address());
$packer_details = trim($objectData->getpacker_details());
$errors = array();
if(strlen($manufacturing_address) > 120) {
$errors[] = '<b>Manufacturing address : </b> should be 120 characters only';
}
if(strlen($packer_details) > 120) {
$errors[] = '<b>Packer details : </b> should be 120 characters only';
}
if(count($errors)) {
$message = implode('<br/> ',$errors);
throw new \Pimcore\Model\Element\ValidationException($message);
}
}
if($objectData->getClassName() == 'Products') {
$objectId = $objectData->getId();
// Swiggy
// $sku_name = $objectData->getsku_name();
// $weight = $objectData->getweight();
// $swiggyProduct = new DataObject\ProductSwiggy\Listing();
// $swiggyProduct->setCondition(" storeProduct__id = '$objectId' ");
// if(!empty($swiggyProduct->getData()) && count($swiggyProduct->getData())) {
// $swiggyProduct->getData()[0]->setsku_name($sku_name);
// $swiggyProduct->getData()[0]->setweight($weight);
// $swiggyProduct->getData()[0]->setPublished(true);
// $swiggyProduct->getData()[0]->save();
// }
// Zepto
// $hsn_code = $objectData->gethsncode();
// $zeptoProduct = new DataObject\ProductZepto\Listing();
// $zeptoProduct->setCondition(" storeProduct__id = '$objectId' ");
// if(!empty($zeptoProduct->getData()) && count($zeptoProduct->getData())) {
// $zeptoProduct->getData()[0]->sethsn_code($hsn_code);
// $zeptoProduct->getData()[0]->setPublished(true);
// $zeptoProduct->getData()[0]->save();
// }
// JioMart
// $hsn_code = $objectData->gethsncode();
// $how_to_use = $objectData->gethow_to_use();
// $product_type = $objectData->getproduct_type();
// $ingredients = $objectData->getingredients();
// $size = $objectData->getsize();
// $food_type = $objectData->getfood_type();
// $jioMartProduct = new DataObject\ProductJioMart\Listing();
// $jioMartProduct->setCondition(" storeProduct__id = '$objectId' ");
// if(!empty($jioMartProduct->getData()) && count($jioMartProduct->getData())) {
// $jioMartProduct->getData()[0]->sethsn_code($hsn_code);
// $jioMartProduct->getData()[0]->sethow_to_use($how_to_use);
// $jioMartProduct->getData()[0]->setproduct_type($product_type);
// $jioMartProduct->getData()[0]->setingredients($ingredients);
// $jioMartProduct->getData()[0]->setsize($size);
// $jioMartProduct->getData()[0]->setfood_type($food_type);
// $jioMartProduct->getData()[0]->setPublished(true);
// $jioMartProduct->getData()[0]->save();
// }
// Big Basket
// $how_to_use = $objectData->gethow_to_use();
// $product_description = $objectData->getproduct_description();
// $hsn_code = $objectData->gethsncode();
// $bbProduct = new DataObject\ProductBigBasket\Listing();
// $bbProduct->setCondition(" storeProduct__id = '$objectId' ");
// if(!empty($bbProduct->getData()) && count($bbProduct->getData())) {
// $bbProduct->getData()[0]->sethow_to_use($how_to_use);
// $bbProduct->getData()[0]->setproduct_description($product_description);
// $bbProduct->getData()[0]->sethsn_code($hsn_code);
// $bbProduct->getData()[0]->setPublished(true);
// $bbProduct->getData()[0]->save();
// }
//Blinkit
// $key_features = $objectData->getkey_features();
// $product_type = $objectData->getproduct_type();
// $price = $objectData->getprice();
// $ingredients = $objectData->getingredients();
// $blinkitProduct = new DataObject\ProductBlinkit\Listing();
// $blinkitProduct->setCondition(" storeProduct__id = '$objectId' ");
// if(!empty($blinkitProduct->getData()) && count($blinkitProduct->getData())) {
// $blinkitProduct->getData()[0]->setkey_features($key_features);
// $blinkitProduct->getData()[0]->setproduct_type($product_type);
// $blinkitProduct->getData()[0]->setprice($price);
// $blinkitProduct->getData()[0]->setingredients($ingredients);
// $blinkitProduct->getData()[0]->setPublished(true);
// $blinkitProduct->getData()[0]->save();
// }
//Amazon
// $size = $objectData->getsize();
// $product_type = $objectData->getproduct_type();
// $amazonProduct = new DataObject\ProductAmazon\Listing();
// $amazonProduct->setCondition(" storeProduct__id = '$objectId' ");
// if(!empty($amazonProduct->getData()) && count($amazonProduct->getData())) {
// $amazonProduct->getData()[0]->setproduct_type($size);
// $amazonProduct->getData()[0]->setsize($product_type);
// $amazonProduct->getData()[0]->setPublished(true);
// $amazonProduct->getData()[0]->save();
// }
$errors = array();
// if(!$this->validateAlphanumeric($objectData->getsku())) {
// $errors[] = '<b>SKU : </b> Please enter valid sku code';
// }
// if(!$this->validateAlphabetsOnly($objectData->getname())) {
// $errors[] = '<b>Product name : </b> Please enter valid product name';
// }
// if(!empty($objectData->getprice())) {
// if (!preg_match('/^[0-9\s]+$/', $objectData->getprice())) {
// // The input contains only numeric characters and spaces
// $errors[] = '<b>Price : </b> Only numeric value is allowed';
// }
// }
if(!empty($objectData->getpack_weight()) && !empty($objectData->getno_packs())) {
$objectData->setcfcweight($objectData->getpack_weight() * $objectData->getno_packs());
}
// if(!empty($objectData->getmaterial_number()) && !$this->validateAlphabetsOnly($objectData->getmaterial_number())) {
// $errors[] = '<b>SAP SKU Code : </b> Please enter valid SAP sku code (Alphanumeric)';
// }
if(!empty($objectData->gettax_category()) && !$this->validateAlphabetsOnly($objectData->gettax_category())) {
$errors[] = '<b>Tax category : </b> Please enter valid tax category';
}
if(!empty($objectData->getbatch_management()) && !$this->validateAlphabetsOnly($objectData->getbatch_management())) {
$errors[] = '<b>Batch management : </b> Please enter valid batch management';
}
// if(empty($objectData->getdivision())) {
// $errors[] = '<b>Division : </b> Please select a division';
// }
if(count($errors)) {
$message = implode('<br/> ',$errors);
throw new \Pimcore\Model\Element\ValidationException($message);
} else {
$productId = $objectData->getId();
$dbConnection = \Pimcore\Db::get();
$workflowInfo = $dbConnection->fetchRow("
SELECT *
FROM `element_workflow_state`
WHERE `cid` = '$productId'
LIMIT 1
");
if(!empty($workflowInfo['place'])) {
$previousWorkflowPlace = $objectData->getpreviousWorkflowPlace();
}
}
}
}
}
}
public function onPostUpdate(ElementEventInterface $e) {
if ($e instanceof DataObjectEvent) {
$objectData = $e->getObject();
if($objectData->gettype() == 'object'){
if($objectData->getClassName() == 'ProductCategory') {
// $categoryAddUrl = "https://integration-5ojmyuq-23txbi6folvo6.ap-3.magentosite.cloud/rest/V1/bridge/catalog/category/add";
$categoryAddUrl = "http://local.itc.com/rest/V1/bridge/catalog/category/add";
$data = array(
"category_id" => $objectData->getId(),
"store_view_id" => "0"
);
$productSyncKey = "qzgg1vbpgwk6pjctbv6ku2a65906ocpb";
// $productSyncKey = "ah2zed4d9rcqgxc4ds16bh6wssdm8xni";
$data_json = json_encode(array("data" => $data));
$curl = curl_init(); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_json);
curl_setopt($curl, CURLOPT_URL, $categoryAddUrl);
curl_setopt($curl, CURLOPT_HTTPHEADER, array( 'Authorization: Bearer ' . $productSyncKey, 'Accept: application/json', 'Content-Type:application/json', ));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
$result = curl_exec($curl);
$resultArray = json_decode($result);
curl_close($curl);
}
if($objectData->getClassName() == 'Products') {
$productId = $objectData->getId();
$dbConnection = \Pimcore\Db::get();
$workflowInfo = $dbConnection->fetchRow("
SELECT *
FROM `element_workflow_state`
WHERE `cid` = '$productId'
LIMIT 1
");
if(!empty($workflowInfo['place']) && $workflowInfo['place'] == 'approved') {
// $productSyncKey = "enlwq7pkg6t2od9blt93coomo7g6p9p0"; // Live Integration
$productSyncKey = "qzgg1vbpgwk6pjctbv6ku2a65906ocpb"; // Local
// $productUpdatedUrl = 'https://integration-5ojmyuq-23txbi6folvo6.ap-3.magentosite.cloud/rest/V1/bridge/catalog/product/add';
$productUpdatedUrl = 'http://local.itc.com/rest/V1/bridge/catalog/product/add';
// echo $productUpdatedUrl.'<br>';
// echo $productSyncKey;
$data = array( "product_id" => $productId,"store_view_id" => "0", );
$data_json = json_encode(array("data" => $data));
$curl = curl_init();
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_json);
curl_setopt($curl, CURLOPT_URL, $productUpdatedUrl);
curl_setopt($curl, CURLOPT_HTTPHEADER, array( 'Authorization: Bearer ' . $productSyncKey, 'Accept: application/json', 'Content-Type:application/json', ));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
$result = curl_exec($curl);
$resultArray = json_decode($result);
curl_close($curl);
// echo '<pre>'; print_r($resultArray); exit;
}
}
} else if($objectData->gettype() == 'folder'){
}
}
}
public function onAssetPostAdd(\Pimcore\Event\Model\AssetEvent $event) {
$data = false;
if($event instanceof AssetEvent) {
$data = $event->getAsset();
} else if ($event instanceof DocumentEvent) {
$data = $event->getDocument();
} else if ($event instanceof DataObjectEvent) {
$data = $event->getObject();
}
if ($data) {
$id = $data->getId();
// $productSyncKey = "enlwq7pkg6t2od9blt93coomo7g6p9p0"; // Live Integration
$productSyncKey = "qzgg1vbpgwk6pjctbv6ku2a65906ocpb"; // Local
// $productUpdatedUrl = 'https://integration-5ojmyuq-23txbi6folvo6.ap-3.magentosite.cloud/rest/V1/bridge/catalog/product/add';
$productUpdatedUrl = 'http://local.itc.com/rest/V1/bridge/asset/add';
// echo $productUpdatedUrl.'<br>';
// echo $productSyncKey;
$data = array( "asset_id" => $id,"store_view_id" => "0", );
$data_json = json_encode(array("data" => $data));
$curl = curl_init();
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_json);
curl_setopt($curl, CURLOPT_URL, $productUpdatedUrl);
curl_setopt($curl, CURLOPT_HTTPHEADER, array( 'Authorization: Bearer ' . $productSyncKey, 'Accept: application/json', 'Content-Type:application/json', ));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
$result = curl_exec($curl);
$resultArray = json_decode($result);
curl_close($curl);
}
}
}