<?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)) {
if(!str_contains($objectData->getpath(),"/SAP/Approved")) {
$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)) {
$productId = $objectData->getId();
$dbConnection = \Pimcore\Db::get();
$workflowInfo = $dbConnection->fetchRow("
SELECT *
FROM `element_workflow_state`
WHERE `cid` = '$productId'
LIMIT 1
");
if(!empty($workflowInfo['place'])) {
if($workflowInfo['place'] != 'sap_action') {
$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();
$division = trim($objectData->getdivision());
if (empty($objectData->getmrp_controller())) {
$mrpcnt = array(
'AT' => 'ATT',
'BI' => 'BIS',
'CF' => 'CON',
'CH' => 'CHO',
'DF' => 'DFS',
'DM' => 'DMS',
'DP' => 'DPS',
'IC' => 'COF',
'JU' => 'JUC',
'ND' => 'NDL',
'RG' => 'COF',
'RI' => 'RIC',
'RT' => 'RTE',
'SA' => 'SAL',
'SI' => 'SPI',
'SN' => 'PAS',
'SX' => 'SNX',
'DB' => 'MFO',
'BR' => 'ATT',
'SS' => 'ATT',
'OL' => 'ATT',
'PA' => 'ATT',
'MX' => 'ATT',
'ZF' => 'FFD',
'CP' => 'ATT',
'PU' => 'ATT',
'BC' => 'ATT',
'YB' => 'BIS'
);
if (!empty($division)) {
if (array_key_exists($division,$mrpcnt)) {
$objectData->setmrp_controller($mrpcnt[$division]);
}
}
} else if (empty($objectData->getmaterial_type())) {
$mt = array(
'AT' => 'FFGD',
'BI' => 'FFGD',
'CF' => 'FFGD',
'CH' => 'FFGD',
'DF' => 'FDFG',
'DM' => 'FDFG',
'DP' => 'FDFG',
'GU' => 'FFGD',
'IC' => 'FFGD',
'JU' => 'FFGD',
'ND' => 'FFGD',
'RG' => 'FFGD',
'RI' => 'FFGD',
'RT' => 'FFGD',
'SA' => 'FFGD',
'SI' => 'FFGD',
'SN' => 'FFGD',
'SX' => 'FFGD',
'DB' => 'FFGD',
'BR' => 'FFGD',
'SS' => 'FFGD',
'OL' => 'FFGD',
'PA' => 'FFGD',
'MX' => 'FFGD',
'ZF' => 'FFGD',
'CP' => 'FFGD',
'PU' => 'FFGD',
'BC' => 'FFGD',
'YB' => 'FFGD'
);
if (!empty($division)) {
if (array_key_exists($division,$mt)) {
$objectData->setmaterial_type($mt[$division]);
}
}
} else if(empty($objectData->getuom())) {
$uom = array(
'AT' => 'KG',
'BI' => 'KG',
'CF' => 'KG',
'CH' => 'EA',
'DF' => 'KG',
'DM' => 'L',
'DP' => 'KG',
'IC' => 'KG',
'JU' => 'L',
'ND' => 'KG',
'RG' => 'EA',
'RI' => 'KG',
'RT' => 'EA',
'SA' => 'KG',
'SI' => 'KG',
'SN' => 'KG',
'SX' => 'KG',
'DB' => 'L',
'BR' => 'KG',
'SS' => 'KG',
'OL' => 'L',
'PA' => 'KG',
'MX' => 'KG',
'ZF' => 'KG',
'CP' => 'KG',
'PU' => 'KG',
'BC' => 'KG',
'YB' => 'KG'
);
if (!empty($division)) {
if (array_key_exists($division,$uom)) {
$objectData->setuom($uom[$division]);
}
}
}
$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 {
}
}
}
}
}
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->getClassName() == 'ProductByName') {
$dbConnection = \Pimcore\Db::get();
$objectId = $objectData->getid();
$wfInfo = $dbConnection->fetchAssociative("
SELECT * FROM `element_workflow_state`
WHERE cid = '$objectId'
");
$wfplace = $wfInfo['place'];
$products = $objectData->getproducts();
foreach ($products as $key => $value) {
$productId = $value->getId();
$dbConnection->query("
UPDATE `element_workflow_state`
SET `place` = '$wfplace'
WHERE `cid` = '$productId'
");
}
}
} 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);
}
}
}