Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Webkul_Mpmassuploadaddons_Model_Mpmassupload extends Mage_Core_Model_Abstract
- {
- /**
- * importSimpleProducts Used for importing simple product
- * @param Int $profileId Profile id that is currenly in execution
- * @param $rowCount $rowCount CSV row count
- * @var Object $collection Mass upload profiler collection
- * @var Array $result Conatines the return data
- * @var Array $data Conatines product attribute values
- * @var Object $csvFile Containes CSV file name
- * @var $imgDir Containes Image directory name
- * @var String $targetPath Profiler files target path
- * @var Object $fp File Object
- * @var Int $status Conatines product approval config status
- * @var Object $exist_productid Catalog_Model_Product Object
- * @var Int $is_in_stock Stock status
- * @var Object $profile Profiler model object
- * @var Array $wholedata Containes product atrribute values
- * @var Int $proid Catalog Product id
- * @var Object $marketplaceProduct Marketplace Product model object
- * @var Array $images Product images name
- * @return Array $result Conatines the return data
- */
- public function importSimpleProducts($profileId,$rowCount) {
- $helper = Mage::helper("mpmassuploadaddons");
- $collection = Mage::getModel('mpmassuploadaddons/profilesession')->getCollection()
- ->addFieldToFilter('session_id',array('eq' => $profileId));
- $result = array();
- $data = array();
- $csvFile = "";
- $imgDir = "";
- if (count($collection) > 0) {
- foreach ($collection as $value) {
- $csvFile = $value->getCsvFile();
- $imgDir = $value->getImageFile();
- }
- $targetPath = Mage::getBaseDir('media')."/marketplace/massuploaded/".$profileId;
- $csvPath = $targetPath.'/'.$csvFile;
- $fp = fopen($csvPath, 'r');
- while(!feof($fp) ){
- $data[] = fgetcsv($fp, 1024);
- }
- fclose($fp);
- $info = $data[$rowCount];
- $wholedata = array(
- 'category' => $info[0],
- 'name' => $info[1],
- 'description' => $info[2],
- 'short_description' => $info[3],
- 'sku' => $info[4],
- 'price' => $info[5],
- 'tax_class_id' => $info[9],
- 'is_in_stock' => $info[10],
- 'stock' => $info[11],
- 'weight' => $info[12],
- 'images' => $info[13]
- );
- if(isset($info[6])){
- $specialdata=array(
- 'special_price' => $info[6],
- 'special_from_date' => $info[7],
- 'special_to_date' => $info[8]
- );
- $wholedata=array_merge($wholedata,$specialdata);
- }
- if(isset($info[16])){
- $systemattribute=$data[0][16];
- $othersystemattribute=array(
- $systemattribute => $info[16]
- );
- $wholedata=array_merge($wholedata,$othersystemattribute);
- }
- list($wholedata, $errors) = $this->validatePost($wholedata);
- if(empty($errors)) {
- $customAttributeData = array();
- $mediaTypeAttributedata = array();
- if(isset($data[0][14])) {
- $customAttributeCodes = explode('(', $data[0][14]);
- if($customAttributeCodes[0] != "") {
- list($customAttributeData, $errors) = $this->validateCustomAttributeValues($customAttributeCodes[0],$info[14]);
- $mediaTypeAttributedata = $this->getMediaImageAttributesData($customAttributeCodes[0],$info[14]);
- }
- // echo "<pre>";print_r($customAttributeData);die;
- }
- if(empty($errors)) {
- $profile = Mage::getModel('mpmassuploadaddons/profilesession')->load($profileId);
- $additionalInfo = array(
- 'type_id' => 'simple',
- 'userid' => $profile->getSellerId(),
- 'wstoreids' => Mage::app()->getStore()->getStoreId(),
- 'attribute_set_id' => $profile->getAttributesetId()
- );
- $wholedata = array_merge($wholedata,$additionalInfo);
- Mage::getSingleton('core/session')->setAttributeSet($profile->getAttributesetId());
- $proid = Mage::getModel('marketplace/product')->saveSimpleNewProduct($wholedata);
- $objprod = Mage::getModel('catalog/product')->load($proid);
- $websites = array();
- foreach(Mage::app()->getWebsites(true) as $website) {
- $websites[] = $website->getId();
- }
- $objprod->setWebsiteIds($websites)->save();
- $marketplaceProduct = Mage::getModel('marketplace/product')->getCollection()
- ->addFieldToFilter("mageproductid",array("eq" => $proid))
- ->getFirstItem();
- $marketplaceProduct->setUserid($profile->getSellerId())->save();
- if($info[13] != "") {
- $images = array_reverse(explode(',',$info[13]));
- foreach ($images as $image) {
- $objprod = Mage::getModel('catalog/product')->load($proid);
- $imgp = Mage::getBaseDir('media')."/marketplace/massuploaded/".$profileId."/".$imgDir."/";
- $checkimg = glob($imgp . $image);
- if(count($checkimg) != 0) {
- $filepath = $imgp.$image;
- $objprod->addImageToMediaGallery($filepath, array ('image','small_image','thumbnail'), true, false);
- Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
- $objprod->save();
- }
- }
- }
- try {
- Mage::dispatchEvent('mp_add_customattribute_mass', array('product_id' => $proid,'customattribute' => $customAttributeData));
- if(Mage::getModel('customattribute/customattribute')) {
- if(count($mediaTypeAttributedata)) {
- foreach ($mediaTypeAttributedata as $imageType => $image) {
- if($image != "") {
- $objprod = Mage::getModel('catalog/product')->load($proid);
- $imgp = Mage::getBaseDir('media')."/marketplace/massuploaded/".$profileId."/".$imgDir."/";
- $checkimg = glob($imgp . $image);
- if(count($checkimg) != 0) {
- $filepath = $imgp.$image;
- $objprod->addImageToMediaGallery($filepath, array ($imageType), true, false);
- Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
- $objprod->save();
- }
- }
- }
- }
- }
- } catch(Exception $e) {
- }
- if($info[15] != "") {
- $wholedata['id'] = $proid;
- $data = json_decode($info[15],true);
- $i = 0;
- foreach($data as $k => $d) {
- foreach ($d as $key => $value) {
- if($key == 'values') {
- $wholedata['selectoptions'][$i] = $d[$key];
- } else {
- $wholedata['options'][$i][$key] = $value;
- $wholedata['options'][$i]['customoptindex'] = $i;
- }
- }
- $i++;
- }
- Mage::dispatchEvent('mp_customoption_setdata', $wholedata);
- }
- $result['savedRows'] = 1;
- $result['success'] = $helper->__("successfully saved.");
- } else {
- $result['savedRows'] = 0;
- $result['errors'] = current($errors);
- }
- } else {
- $result['savedRows'] = 0;
- $result['errors'] = current($errors);
- }
- }
- return $result;
- }
- /**
- * importSimpleProducts Used for importing simple product
- * @param Int $profileId Profile id that is currenly in execution
- * @param $rowCount $rowCount CSV row count
- * @var Object $collection Mass upload profiler collection
- * @var Array $result Conatines the return data
- * @var Array $data Conatines product attribute values
- * @var Object $csvFile Containes CSV file name
- * @var $imgDir Containes Image directory name
- * @var String $targetPath Profiler files target path
- * @var Object $fp File Object
- * @var Int $status Conatines product approval config status
- * @var Object $exist_productid Catalog_Model_Product Object
- * @var Int $is_in_stock Stock status
- * @var Object $profile Profiler model object
- * @var Array $wholedata Containes product atrribute values
- * @var Int $proid Catalog Product id
- * @var Object $marketplaceProduct Marketplace Product model object
- * @var Array $images Product images name
- * @var String $ext Link extension
- * @var String $pathnamelink Uploaded link files target path
- * @var String $newfile Catalog Product downloadable link files path
- * @var Object $linkModel Product Downloadabel link model
- * @return Array $result Conatines the return data
- */
- public function importDownloadableProducts($profileId,$rowCount) {
- $helper = Mage::helper("mpmassuploadaddons");
- $collection = Mage::getModel('mpmassuploadaddons/profilesession')->getCollection()
- ->addFieldToFilter('session_id',array('eq' => $profileId));
- $result = array();
- $csvFile = "";
- $imgDir = "";
- $linkDir = "";
- if (count($collection) > 0) {
- foreach ($collection as $value) {
- $csvFile = $value->getCsvFile();
- $imgDir = $value->getImageFile();
- $linkDir = $value->getLinkFile();
- }
- $targetPath = Mage::getBaseDir('media')."/marketplace/massuploaded/".$profileId;
- $csvPath = $targetPath.'/'.$csvFile;
- $fp = fopen($csvPath, 'r');
- while(!feof($fp) ) {
- $data[] = fgetcsv($fp, 1024);
- }
- fclose($fp);
- $info = $data[$rowCount];
- $wholedata = array(
- 'category' => $info[0],
- 'name' => $info[1],
- 'description' => $info[2],
- 'short_description' => $info[3],
- 'sku' => $info[4],
- 'price' => $info[5],
- 'tax_class_id' => $info[9],
- 'is_in_stock' => $info[10],
- 'stock' => $info[11],
- 'images' => $info[12],
- 'links_title' => $info[13],
- 'links_purchased_separately' => $info[14],
- 'links' => $info[15]
- );
- if(isset($info[6])){
- $specialdata=array(
- 'special_price' => $info[6],
- 'special_from_date' => $info[7],
- 'special_to_date' => $info[8]
- );
- $wholedata=array_merge($wholedata,$specialdata);
- }
- if(isset($info[18])){
- $systemattribute=$data[0][18];
- $othersystemattribute=array(
- $systemattribute => $info[18]
- );
- $wholedata=array_merge($wholedata,$othersystemattribute);
- }
- $wholedata = array(
- 'category' => $info[0],
- 'name' => $info[1],
- 'description' => $info[2],
- 'short_description' => $info[3],
- 'sku' => $info[4],
- 'price' => $info[5],
- 'tax_class_id' => $info[9],
- 'is_in_stock' => $info[10],
- 'stock' => $info[11],
- 'weight' => $info[12],
- 'images' => $info[13]
- );
- list($wholedata, $errors) = $this->validatePost($wholedata);
- if(empty($errors)) {
- $links = explode(',',$info[15]);
- foreach ($links as $link) {
- $lngp = Mage::getBaseDir('media')."/marketplace/massuploaded/".$profileId."/".$linkDir."/";
- $checkimg = glob($lngp . $link);
- if(count($checkimg) == 0) {
- $errors[] = Mage::helper('mpmassuploadaddons')->__('Skip import row, link file "%s" does not exist for field "link_file"',$link);
- }
- }
- }
- if(empty($errors)) {
- $customAttributeData = array();
- $mediaTypeAttributedata = array();
- if(isset($data[0][16])) {
- $customAttributeCodes = explode('(', $data[0][16]);
- if($customAttributeCodes[0] != "") {
- list($customAttributeData, $errors) = $this->validateCustomAttributeValues($customAttributeCodes[0],$info[16]);
- $mediaTypeAttributedata = $this->getMediaImageAttributesData($customAttributeCodes[0],$info[16]);
- }
- }
- if(empty($errors)) {
- $profile = Mage::getModel('mpmassuploadaddons/profilesession')->load($profileId);
- $additionalInfo = array(
- 'type_id' => 'downloadable',
- 'userid' => $profile->getSellerId(),
- 'wstoreids' => Mage::app()->getStore()->getStoreId(),
- 'attribute_set_id' => $profile->getAttributesetId(),
- 'producttypecustom' => 'customproductdownloadable',
- );
- $wholedata = array_merge($wholedata,$additionalInfo);
- $proid = Mage::getModel('marketplace/product')->saveDownloadableNewProduct($wholedata);
- $objprod = Mage::getModel('catalog/product')->load($proid);
- $websites = array();
- foreach(Mage::app()->getWebsites(true) as $website) {
- $websites[] = $website->getId();
- }
- $objprod->setWebsiteIds($websites)->save();
- $marketplaceProduct = Mage::getModel('marketplace/product')->getCollection()
- ->addFieldToFilter("mageproductid",array("eq" => $proid))
- ->getFirstItem();
- $marketplaceProduct->setUserid($profile->getSellerId())->save();
- if($info[12] != "") {
- $images = array_reverse(explode(',',$info[12]));
- foreach ($images as $image) {
- $objprod = Mage::getModel('catalog/product')->load($proid);
- $imgp = Mage::getBaseDir('media')."/marketplace/massuploaded/".$profileId."/".$imgDir."/";
- $checkimg = glob($imgp . $image);
- if(count($checkimg) != 0) {
- $filepath = $imgp.$image;
- $objprod->addImageToMediaGallery($filepath, array ('image','small_image','thumbnail'), true, false);
- Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
- $objprod->save();
- }
- }
- }
- if($info[15]!="") {
- $links = explode(',',$info[15]);
- foreach ($links as $link) {
- $ext = explode('.',$link);
- $pathnamelink = Mage::getBaseDir('media')."/marketplace/massuploaded/".$profileId."/".$linkDir."/".$link;
- $linkModel = Mage::getModel('downloadable/link');
- $filename1 = "UploadedFile-".$proid;
- $time = time().$filename1.".".$ext[1];
- $newfile = Mage::getBaseDir('media').'/downloadable/files/links/'.$time;
- copy($pathnamelink, $newfile);
- $linkModel->setLinkFile("/".$time);
- $linkModel->setLinkType("file");
- $linkModel->setProductId($proid);
- $web_id = Mage::app()->getStore(Mage_Catalog_Model_Abstract::DEFAULT_STORE_ID)->getWebsiteId().",".Mage::app()->getStore()->getId();
- $linkModel->setWebsiteIds(array($web_id));
- $linkModel->setStoreId(0);
- $linkModel->setTitle("Image-".$proid);
- $linkModel->setNumberOfDownloads(0);
- $linkModel->save();
- break;
- }
- }
- try {
- Mage::dispatchEvent('mp_add_customattribute_mass', array('product_id' => $proid,'customattribute' => $customAttributeData));
- if(Mage::getModel('customattribute/customattribute')) {
- if(count($mediaTypeAttributedata)) {
- foreach ($mediaTypeAttributedata as $imageType => $image) {
- if($image != "") {
- $objprod = Mage::getModel('catalog/product')->load($proid);
- $imgp = Mage::getBaseDir('media')."/marketplace/massuploaded/".$profileId."/".$imgDir."/";
- $checkimg = glob($imgp . $image);
- if(count($checkimg) != 0) {
- $filepath = $imgp.$image;
- $objprod->addImageToMediaGallery($filepath, array ($imageType), true, false);
- Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
- $objprod->save();
- }
- }
- }
- }
- }
- } catch(Exception $e) {
- }
- if(isset($info[17]) && $info[17] != "") {
- $wholedata['id'] = $proid;
- $data = json_decode($info[17],true);
- $i=0;
- foreach($data as $k => $d){
- foreach ($d as $key => $value) {
- if($key == 'values') {
- $wholedata['selectoptions'][$i] = $d[$key];
- } else {
- $wholedata['options'][$i][$key] = $value;
- $wholedata['options'][$i]['customoptindex'] = $i;
- }
- }
- $i++;
- }
- Mage::dispatchEvent('mp_customoption_setdata', $wholedata);
- }
- $result['savedRows'] = 1;
- $result['success'] = $helper->__("successfully saved.");
- } else {
- $result['savedRows'] = 0;
- $result['errors'] = current($errors);
- }
- } else {
- $result['savedRows'] = 0;
- $result['errors'] = current($errors);
- }
- }
- return $result;
- }
- /**
- * importSimpleProducts Used for importing simple product
- * @param Int $profileId Profile id that is currenly in execution
- * @param $rowCount $rowCount CSV row count
- * @var Object $collection Mass upload profiler collection
- * @var Array $result Conatines the return data
- * @var Array $data Conatines product attribute values
- * @var Object $csvFile Containes CSV file name
- * @var $imgDir Containes Image directory name
- * @var String $targetPath Profiler files target path
- * @var Object $fp File Object
- * @var Int $status Conatines product approval config status
- * @var Object $exist_productid Catalog_Model_Product Object
- * @var Int $is_in_stock Stock status
- * @var Object $profile Profiler model object
- * @var Array $wholedata Containes product atrribute values
- * @var Int $proid Catalog Product id
- * @var Object $marketplaceProduct Marketplace Product model object
- * @var Array $images Product images name
- * @return Array $result Conatines the return data
- */
- public function importVirtualProducts($profileId,$rowCount) {
- $helper = Mage::helper("mpmassuploadaddons");
- $collection = Mage::getModel('mpmassuploadaddons/profilesession')->getCollection()
- ->addFieldToFilter('session_id',array('eq' => $profileId));
- $result = array();
- $csvFile = "";
- $imgDir = "";
- if (count($collection) > 0) {
- foreach ($collection as $value) {
- $csvFile = $value->getCsvFile();
- $imgDir = $value->getImageFile();
- }
- $targetPath = Mage::getBaseDir('media')."/marketplace/massuploaded/".$profileId;
- $csvPath = $targetPath.'/'.$csvFile;
- $fp = fopen($csvPath, 'r');
- while(!feof($fp) ){
- $data[] = fgetcsv($fp, 1024);
- }
- fclose($fp);
- $info = $data[$rowCount];
- $wholedata = array(
- 'category' => $info[0],
- 'name' => $info[1],
- 'description' => $info[2],
- 'short_description' => $info[3],
- 'sku' => $info[4],
- 'price' => $info[5],
- 'tax_class_id' => $info[9],
- 'is_in_stock' => $info[10],
- 'stock' => $info[11],
- 'images' => $info[12]
- );
- if(isset($info[6])){
- $specialdata=array(
- 'special_price' => $info[6],
- 'special_from_date' => $info[7],
- 'special_to_date' => $info[8]
- );
- $wholedata=array_merge($wholedata,$specialdata);
- }
- list($wholedata, $errors) = $this->validatePost($wholedata);
- if(empty($errors)) {
- $customAttributeData = array();
- $mediaTypeAttributedata = array();
- if(isset($data[0][13])) {
- $customAttributeCodes = explode('(', $data[0][13]);
- if($customAttributeCodes[0] != "") {
- list($customAttributeData, $errors) = $this->validateCustomAttributeValues($customAttributeCodes[0],$info[13]);
- $mediaTypeAttributedata = $this->getMediaImageAttributesData($customAttributeCodes[0],$info[13]);
- }
- }
- if(empty($errors)) {
- $profile = Mage::getModel('mpmassuploadaddons/profilesession')->load($profileId);
- $additionalInfo = array(
- 'type_id' => 'virtual',
- 'userid' => $profile->getSellerId(),
- 'wstoreids' => Mage::app()->getStore()->getStoreId(),
- 'attribute_set_id' => $profile->getAttributesetId(),
- 'producttypecustom' => 'customproductvirtual',
- );
- $wholedata = array_merge($wholedata,$additionalInfo);
- Mage::getSingleton('core/session')->setAttributeSet($profile->getAttributesetId());
- $proid = Mage::getModel('marketplace/product')->saveVirtualNewProduct($wholedata);
- $objprod = Mage::getModel('catalog/product')->load($proid);
- $websites = array();
- foreach(Mage::app()->getWebsites(true) as $website) {
- $websites[] = $website->getId();
- }
- $objprod->setWebsiteIds($websites)->save();
- $marketplaceProduct = Mage::getModel('marketplace/product')->getCollection()
- ->addFieldToFilter("mageproductid",array("eq" => $proid))
- ->getFirstItem();
- $marketplaceProduct->setUserid($profile->getSellerId())->save();
- if($info[12] != "") {
- $images = array_reverse(explode(',',$info[12]));
- foreach ($images as $image) {
- $objprod = Mage::getModel('catalog/product')->load($proid);
- $imgp = Mage::getBaseDir('media')."/marketplace/massuploaded/".$profileId."/".$imgDir."/";
- $checkimg = glob($imgp . $image);
- if(count($checkimg) != 0) {
- $filepath = $imgp.$image;
- $objprod->addImageToMediaGallery($filepath, array ('image','small_image','thumbnail'), true, false);
- Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
- $objprod->save();
- }
- }
- }
- try {
- Mage::dispatchEvent('mp_add_customattribute_mass', array('product_id' => $proid,'customattribute' => $customAttributeData));
- if(Mage::getModel('customattribute/customattribute')) {
- if(count($mediaTypeAttributedata)) {
- foreach ($mediaTypeAttributedata as $imageType => $image) {
- if($image != "") {
- $objprod = Mage::getModel('catalog/product')->load($proid);
- $imgp = Mage::getBaseDir('media')."/marketplace/massuploaded/".$profileId."/".$imgDir."/";
- $checkimg = glob($imgp . $image);
- if(count($checkimg) != 0) {
- $filepath = $imgp.$image;
- $objprod->addImageToMediaGallery($filepath, array ($imageType), true, false);
- Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
- $objprod->save();
- }
- }
- }
- }
- }
- } catch(Exception $e) {
- }
- if(isset($info[14]) && $info[14] != ""){
- $wholedata['id'] = $proid;
- $data = json_decode($info[14],true);
- $i = 0;
- foreach($data as $k => $d) {
- foreach ($d as $key => $value) {
- if($key == 'values') {
- $wholedata['selectoptions'][$i]=$d[$key];
- } else {
- $wholedata['options'][$i][$key] = $value;
- $wholedata['options'][$i]['customoptindex'] = $i;
- }
- }
- $i++;
- }
- Mage::dispatchEvent('mp_customoption_setdata', $wholedata);
- }
- $result['savedRows'] = 1;
- $result['success'] = $helper->__("successfully saved.");
- } else {
- $result['savedRows'] = 0;
- $result['errors'] = current($errors);
- }
- } else {
- $result['savedRows'] = 0;
- $result['errors'] = current($errors);
- }
- }
- return $result;
- }
- /**
- * importConfigurableProducts Used for importing simple product
- * @param Int $profileId Profile id that is currenly in execution
- * @param $rowCount $rowCount CSV row count
- * @var Object $collection Mass upload profiler collection
- * @var Array $result Conatines the return data
- * @var Array $data Conatines product attribute values
- * @var Object $csvFile Containes CSV file name
- * @var $imgDir Containes Image directory name
- * @var String $targetPath Profiler files target path
- * @var Object $fp File Object
- * @var Int $status Conatines product approval config status
- * @var Object $exist_productid Catalog_Model_Product Object
- * @var Int $is_in_stock Stock status
- * @var Object $profile Profiler model object
- * @var Array $wholedata Containes product atrribute values
- * @var Array $associateWholedata Associated products data
- * @var Array $additionalInfo Configurable product additional information other then csv
- * @var Int $proid Catalog Product id
- * @var Int $totalProcessedRows Total number of processed row
- * @var Object $marketplaceProduct Marketplace Product model object
- * @var Array $images Product images name
- * @return Array $result Conatines the return data
- */
- public function importConfigurableProducts($profileId,$rowCount) {
- $helper = Mage::helper("mpmassuploadaddons");
- $collection = Mage::getModel('mpmassuploadaddons/profilesession')->getCollection()
- ->addFieldToFilter('session_id',array('eq' => $profileId));
- $result = array();
- $data = array();
- $csvFile = "";
- $imgDir = "";
- if (count($collection) > 0) {
- foreach ($collection as $value) {
- $csvFile = $value->getCsvFile();
- $imgDir = $value->getImageFile();
- }
- $targetPath = Mage::getBaseDir('media')."/marketplace/massuploaded/".$profileId;
- $csvPath = $targetPath.'/'.$csvFile;
- $fp = fopen($csvPath, 'r');
- while(!feof($fp) ){
- $data[] = fgetcsv($fp, 1024);
- }
- fclose($fp);
- $info = $data[$rowCount];
- $totalProcessedRows = $rowCount;
- $attributeOptionCheck = array();
- $associatedSkuCheck = array();
- $associatedSkuCheck[] = $info[5];
- $errors = array();
- for ($i = $totalProcessedRows + 1; $i <= sizeof($data); $i++) {
- if(strtolower($data[$i][0]) != "configurable" && $data[$i][0] != "") {
- $attributeOptionCheck[] = $data[$i][10];
- $associatedSkuCheck[] = $data[$i][5];
- $totalProcessedRows++;
- } else {
- if(count($attributeOptionCheck) != count(array_unique($attributeOptionCheck)))
- $errors[] = Mage::helper('mpmassuploadaddons')->__('Skip import row, product with duplicate attribute combination');
- elseif(count($associatedSkuCheck) != count(array_unique($associatedSkuCheck)))
- $errors[] = Mage::helper('mpmassuploadaddons')->__('Skip import row,associated products with duplicate skus');
- break;
- }
- }
- if(empty($errors)) {
- $wholedata = array(
- 'category' => $info[1],
- 'name' => $info[2],
- 'description' => $info[3],
- 'short_description' => $info[4],
- 'sku' => $info[5],
- 'price' => $info[6],
- 'tax_class_id' => $info[10],
- 'is_in_stock' => $info[11],
- '_super_attribute_code' => $info[12],
- 'images' => $info[17]
- );
- // $wholedata = array(
- // 'category' => $info[0],
- // 'name' => $info[1],
- // 'description' => $info[2],
- // 'short_description' => $info[3],
- // 'sku' => $info[4],
- // 'price' => $info[5],
- // 'tax_class_id' => $info[9],
- // 'is_in_stock' => $info[10],
- // 'stock' => $info[11],
- // 'weight' => $info[12],
- // 'images' => $info[13]
- // );
- if(isset($info[6])){
- $specialdata=array(
- 'special_price' => $info[7],
- 'special_from_date' => $info[8],
- 'special_to_date' => $info[9]
- );
- $wholedata=array_merge($wholedata,$specialdata);
- }
- if(isset($info[20])){
- $systemattribute=$data[0][20];
- $othersystemattribute=array(
- $systemattribute => $info[20]
- );
- $wholedata=array_merge($wholedata,$othersystemattribute);
- }
- list($wholedata, $errors) = $this->validatePost($wholedata);
- if(empty($errors)) {
- $customAttributeData = array();
- $mediaTypeAttributedata = array();
- if(isset($data[0][18])) {
- $customAttributeCodes = explode('(', $data[0][18]);
- if($customAttributeCodes[0] != "") {
- list($customAttributeData, $errors) = $this->validateCustomAttributeValues($customAttributeCodes[0],$info[18]);
- $mediaTypeAttributedata = $this->getMediaImageAttributesData($customAttributeCodes[0],$info[18]);
- }
- }
- if(empty($errors)) {
- $j = $rowCount;
- for ($i = $j + 1; $i <= sizeof($data); $i++) {
- if(strtolower($data[$i][0]) != "configurable" && $data[$i][0] != "") {
- $associateWholedata = array(
- 'type_id' => $data[$i][0],
- 'name' => $data[$i][2],
- 'sku' => $data[$i][5],
- '_super_attribute_code' => $data[$i][12],
- '_super_attribute_option' => $data[$i][13],
- '_super_attribute_price_corr' => $data[$i][14],
- '_super_attribute_qty' => $data[$i][15],
- '_super_attribute_weight' => $data[$i][16]
- );
- list($associateWholedata, $errors) = $this->validatePost($associateWholedata);
- if(!empty($errors))
- break;
- } else {
- break;
- }
- }
- if(empty($errors)) {
- $profile = Mage::getModel('mpmassuploadaddons/profilesession')->load($profileId);
- $additionalInfo = array(
- 'type_id' => 'configurable',
- 'userid' => $profile->getSellerId(),
- 'wstoreids' => Mage::app()->getStore()->getStoreId(),
- 'attribute_set_id' => $profile->getAttributesetId()
- );
- $wholedata = array_merge($wholedata,$additionalInfo);
- $proid = $this->saveConfigNewProduct($wholedata);
- $objprod = Mage::getModel('catalog/product')->load($proid);
- $websites = array();
- foreach(Mage::app()->getWebsites(true) as $website) {
- $websites[] = $website->getId();
- }
- $objprod->setWebsiteIds($websites)->save();
- $marketplaceProduct = Mage::getModel('marketplace/product')->getCollection()
- ->addFieldToFilter("mageproductid",array("eq" => $proid))
- ->getFirstItem();
- $marketplaceProduct->setUserid($profile->getSellerId())->save();
- $totalProcessedRows = $rowCount;
- $count = 1;
- for ($i = $totalProcessedRows + 1; $i <= sizeof($data); $i++) {
- if(strtolower($data[$i][0]) != "configurable" && $data[$i][0] != "") {
- $associateWholedata = array(
- 'type_id' => 'simple',
- 'name' => $data[$i][2],
- 'sku' => $data[$i][5],
- 'stock' => $data[$i][15],
- 'weight' => $data[$i][16],
- 'status' => 1,
- 'visibility' => 1,
- 'mainid' => $proid
- );
- $configProduct = Mage::getModel("catalog/product")->load($proid);
- $associatedPrice = $configProduct->getPrice();
- $configattr = Mage::getModel('catalog/product_type_configurable')->getConfigurableAttributesAsArray($configProduct);
- foreach (explode(',', $data[$i][12]) as $key => $attribute) {
- $optionIds = explode(',', $data[$i][13]);
- $optionId = $this->getAttributeOptionIdbyOptionText($attribute , $optionIds[$key]);
- $optionPrice = explode(',', $data[$i][14]);
- $associatedPrice = $associatedPrice + $optionPrice[$key];
- foreach ($configattr as $value) {
- if($value['attribute_code'] == $attribute) {
- $optionString = $attribute."|price|".$value['id']."|".$optionId;
- }
- }
- $associateWholedata[$attribute] = $optionId;
- $associateWholedata[$optionString] = $optionPrice[$key];
- $associateWholedata['price'] = $associatedPrice;
- }
- $this->quickcreate($associateWholedata);
- $totalProcessedRows++;
- $count++;
- } else {
- break;
- }
- }
- if(isset($info[17]) && $info[17] != "") {
- $images = array_reverse(explode(',',$info[17]));
- foreach ($images as $image) {
- $objprod = Mage::getModel('catalog/product')->load($proid);
- $imgp = Mage::getBaseDir('media')."/marketplace/massuploaded/".$profileId."/".$imgDir."/";
- $checkimg = glob($imgp . $image);
- if(count($checkimg) != 0) {
- $filepath = $imgp.$image;
- $objprod->addImageToMediaGallery($filepath, array ('image','small_image','thumbnail'), true, false);
- Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
- $objprod->save();
- }
- }
- }
- try {
- Mage::dispatchEvent('mp_add_customattribute_mass', array('product_id' => $proid,'customattribute' => $customAttributeData));
- if(Mage::getModel('customattribute/customattribute')) {
- if(count($mediaTypeAttributedata)) {
- foreach ($mediaTypeAttributedata as $imageType => $image) {
- if($image != "") {
- $objprod = Mage::getModel('catalog/product')->load($proid);
- $imgp = Mage::getBaseDir('media')."/marketplace/massuploaded/".$profileId."/".$imgDir."/";
- $checkimg = glob($imgp . $image);
- if(count($checkimg) != 0) {
- $filepath = $imgp.$image;
- $objprod->addImageToMediaGallery($filepath, array ($imageType), true, false);
- Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
- $objprod->save();
- }
- }
- }
- }
- }
- } catch(Exception $e) {
- }
- if(isset($info[19]) && $info[19] != "") {
- $wholedata['id'] = $proid;
- $data = json_decode($info[19],true);
- $i = 0;
- foreach($data as $k => $d) {
- foreach ($d as $key => $value) {
- if($key == 'values') {
- $wholedata['selectoptions'][$i] = $d[$key];
- } else {
- $wholedata['options'][$i][$key] = $value;
- $wholedata['options'][$i]['customoptindex'] = $i;
- }
- }
- $i++;
- }
- Mage::dispatchEvent('mp_customoption_setdata', $wholedata);
- }
- $result['savedRows'] = $count;
- $result['rowsCount'] = $totalProcessedRows;
- $result['success'] = $helper->__("successfully saved.");
- } else {
- $result['savedRows'] = 0;
- $result['rowsCount'] = $totalProcessedRows;
- $result['errors'] = current($errors);
- }
- } else {
- $result['savedRows'] = 0;
- $result['rowsCount'] = $totalProcessedRows;
- $result['errors'] = current($errors);
- }
- } else {
- $result['savedRows'] = 0;
- $result['rowsCount'] = $totalProcessedRows;
- $result['errors'] = current($errors);
- }
- } else {
- $result['savedRows'] = 0;
- $result['rowsCount'] = $totalProcessedRows;
- $result['errors'] = current($errors);
- }
- }
- return $result;
- }
- /**
- * saveConfigNewProduct This function is used for saving configurable product
- * @param Array $wholedata Configurable product information
- * @var String $attr Configurable product super attribute
- * @var Int $status Product approval status
- * @var Array $stores Store ids
- * @var Object $magentoProductModel Catalog product model
- * @var String $baseCurrencyCode Containes base currency code
- * @var String $currentCurrencyCode Containes current currency code
- * @var Array $allowedCurrencies Allowed currency codes
- * @var Array $rates Containes currency rates
- * @var Float $price Product price
- * @var Int $attributeId Super Attribute id
- * @var Array $asspro Containes associated prodcuts details
- * @var Object $super_attribute Attibute model
- * @var Int $lastId Last saved id
- * @var Object $stockStatus Catalog inventory model object
- * @var Int $vendorId Vendor id
- * @var Object $collection1 Marketplace product collection object
- */
- public function saveConfigNewProduct($wholedata) {
- $attr = explode(',',$wholedata['_super_attribute_code']);
- if(isset($wholedata['wstoreids'])) {
- $stores = $wholedata['wstoreids'];
- } else {
- $stores = Mage::app()->getStore()->getStoreId();
- }
- $status = Mage::getStoreConfig('marketplace/marketplace_options/product_approval')? 2:1;
- $magentoProductModel = Mage::getModel('catalog/product');
- $magentoProductModel->setTypeId(Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE);
- $magentoProductModel->setData($wholedata);
- $baseCurrencyCode = Mage::app()->getStore()->getBaseCurrencyCode();
- $currentCurrencyCode = Mage::app()->getStore()->getCurrentCurrencyCode();
- $allowedCurrencies = Mage::getModel('directory/currency')->getConfigAllowCurrencies();
- $rates = Mage::getModel('directory/currency')->getCurrencyRates($baseCurrencyCode, array_values($allowedCurrencies));
- if(!$rates[$currentCurrencyCode]){
- $rates[$currentCurrencyCode] = 1;
- }
- $price = $wholedata['price'] / $rates[$currentCurrencyCode];
- $magentoProductModel->setPrice($price);
- $magentoProductModel->setStoresIds(array($stores));
- $storeId = Mage::app()->getStore()->getId();
- $magentoProductModel->setWebsiteIds(array(Mage::getModel('core/store')->load( $storeId )->getWebsiteId()));
- $attributeId = Mage::getResourceModel('eav/entity_attribute')->getIdByCode('catalog_product',$attr[0]);
- $magentoProductModel->getTypeInstance()->setUsedProductAttributeIds(array($attributeId));
- foreach($attr as $attrCode) {
- $super_attribute = Mage::getModel('eav/entity_attribute')->loadByCode('catalog_product',$attrCode);
- $configurableAtt = Mage::getModel('catalog/product_type_configurable_attribute')->setProductAttribute($super_attribute);
- $newAttributes[] = array(
- 'id' => $configurableAtt->getId(),
- 'label' => $configurableAtt->getLabel(),
- 'position' => $super_attribute->getPosition(),
- 'values' => $configurableAtt->getPrices() ? $configProduct->getPrices() : array(),
- 'attribute_id' => $super_attribute->getId(),
- 'attribute_code' => $super_attribute->getAttributeCode(),
- 'frontend_label' => $super_attribute->getFrontend()->getLabel(),
- );
- }
- $magentoProductModel->setConfigurableAttributesData($newAttributes);
- $saved = $magentoProductModel->save();
- $magentoProductModel = Mage::getModel('catalog/product')->load($saved->getId());
- $magentoProductModel->setCategoryIds(Array($wholedata['category']));
- $magentoProductModel->setStatus($status);
- $baseCurrencyCode = Mage::app()->getStore()->getBaseCurrencyCode();
- $currentCurrencyCode = Mage::app()->getStore()->getCurrentCurrencyCode();
- $allowedCurrencies = Mage::getModel('directory/currency')->getConfigAllowCurrencies();
- $rates = Mage::getModel('directory/currency')->getCurrencyRates($baseCurrencyCode, array_values($allowedCurrencies));
- if(!$rates[$currentCurrencyCode]) {
- $rates[$currentCurrencyCode] = 1;
- }
- $price = $wholedata['price'] / $rates[$currentCurrencyCode];
- $magentoProductModel->setPrice($price);
- $magentoProductModel->setStockData(array(
- 'use_config_manage_stock' => 1,
- 'is_in_stock' => $wholedata['is_in_stock'],
- 'is_salable' => 1,
- ));
- $saved = $magentoProductModel->save();
- $lastId = $saved->getId();
- $stockStatus = Mage::getModel('cataloginventory/stock_status');
- $stockStatus->assignProduct($magentoProductModel);
- $stockStatus->saveProductStatus($magentoProductModel->getId(), 1);
- $vendorId = Mage::getSingleton('customer/session')->getCustomer()->getId();
- $collection1 = Mage::getModel('marketplace/product');
- $collection1->setmageproductid($lastId);
- $collection1->setuserid($vendorId);
- $collection1->setstatus($status);
- $collection1->save();
- $wholedata['id'] = $lastId;
- Mage::dispatchEvent('mp_customoption_setdata', $wholedata);
- $app = Mage::app('admin');
- umask(0);
- return $lastId;
- }
- /**
- * quickcreate Quick Create associated products
- * @param Array $wholedata Assocaited product details
- * @var Int $price Assocaited product price
- * @var Object $product Catalog product model
- * @var Array $childIds Configurable product's assocaited product details
- * @var Array $data Configurable product's assocaited product details
- * @var Object $configproducts Configurable product object
- * @var Object $magentoProductModel Catalog Product model object
- * @var String $baseCurrencyCode Containes base currency code
- * @var String $currentCurrencyCode Containes current currency code
- * @var Array $allowedCurrencies Allowed currency codes
- * @var Array $rates Containes currency rates
- * @var Float $price Product price
- * @var Array $configattr Cofigurable super attribute
- * @var Array $supattr Cofigurable super attribute
- */
- public function quickcreate($wholedata) {
- $price = 0;
- $product = Mage::getModel('catalog/product')->load($wholedata['mainid']);
- $childIds = Mage::getModel('catalog/product_type_configurable')->getChildrenIds($wholedata['mainid']);
- foreach($childIds[0] as $val) {
- $data[$val] = array();
- }
- $configproducts = Mage::getModel('catalog/product')->load($wholedata['mainid']);
- $magentoProductModel = Mage::getModel('catalog/product');
- $magentoProductModel->setData($wholedata);
- $magentoProductModel->setName($wholedata['name']);
- $magentoProductModel->setDescription($product->getDescription());
- $magentoProductModel->setShortDescription($product->getShortDescription());
- $magentoProductModel->setSku($wholedata['sku']);
- $baseCurrencyCode = Mage::app()->getStore()->getBaseCurrencyCode();
- $currentCurrencyCode = Mage::app()->getStore()->getCurrentCurrencyCode();
- $allowedCurrencies = Mage::getModel('directory/currency')->getConfigAllowCurrencies();
- $rates = Mage::getModel('directory/currency')->getCurrencyRates($baseCurrencyCode, array_values($allowedCurrencies));
- if(!$rates[$currentCurrencyCode]) {
- $rates[$currentCurrencyCode] = 1;
- }
- $price = $wholedata['price'] / $rates[$currentCurrencyCode];
- $magentoProductModel->setPrice($price);
- $magentoProductModel->setCategoryIds($product->getCategoryIds());
- $storeId = Mage::app()->getStore()->getId();
- $magentoProductModel->setWebsiteIds(array(Mage::getModel('core/store')->load( $storeId )->getWebsiteId()));
- $magentoProductModel->setTypeId('simple');
- $magentoProductModel->setAttributeSetId($configproducts->getAttributeSetId());
- $magentoProductModel->setTaxClassId("None");
- $magentoProductModel->setStockData(array(
- 'is_in_stock' => 1,
- 'qty' => $wholedata['stock']
- )
- );
- $saved = $magentoProductModel->save();
- $lastid = $saved->getId();
- $objprod = Mage::getModel('catalog/product')->load($lastid);
- $websites = array();
- foreach(Mage::app()->getWebsites(true) as $website) {
- $websites[] = $website->getId();
- }
- $objprod->setWebsiteIds($websites)->save();
- $data[$lastid] = array();
- $product->setConfigurableProductsData($data);
- $product->setCanSaveConfigurableAttributes(true);
- $product->save();
- $configattr = Mage::getModel('catalog/product_type_configurable')->getConfigurableAttributesAsArray($configproducts);
- foreach ($wholedata as $key => $value) {
- if(strpos($key,'|') !== false){
- $supattr = explode('|', $key);
- for($i = 0; $i < count($configattr); $i++) {
- if($supattr[2] == $configattr[$i]['id'] ){
- for ($j = 0; $j < count($configattr[$i]['values']); $j++) {
- if($configattr[$i]['values'][$j]['value_index'] == $supattr[3]) {
- $currentCurrencyCode = Mage::app()->getStore()->getCurrentCurrencyCode();
- $allowedCurrencies = Mage::getModel('directory/currency')->getConfigAllowCurrencies();
- $rates = Mage::getModel('directory/currency')->getCurrencyRates($baseCurrencyCode, array_values($allowedCurrencies));
- $price = $value / $rates[$currentCurrencyCode];
- $configattr[$i]['values'][$j]['pricing_value'] = $price;
- $configattr[$i]['values'][$j]['can_edit_price'] = 1;
- $configattr[$i]['values'][$j]['can_read_price'] = 1;
- }
- }
- }
- }
- }
- }
- $configproducts->setConfigurableAttributesData($configattr);
- $configproducts->save();
- $app = Mage::app('admin');
- umask(0);
- return 0;
- }
- /**
- * validatePost This function validate impoert product information
- * @param Array $wholedata Containes product information
- * @var Array $errors Containes validattion failed error
- * @var Array $data Containes validate product information
- * @var String $code Product attribute code
- * @var $value Contains attribute values
- * @var Array $catagoryIds Contains product categories ids
- * @var String $existProductId Contains product sku
- * @var Int $optionId Contains Option id of tax class id attribute
- * @var Object $productModel Catalog product model object
- * @var Object $attribute Eav Attribute model object
- * @return Array Returns error and validated data
- */
- private function validatePost($wholedata) {
- $errors = array();
- $data = array();
- foreach( $wholedata as $code => $value){
- switch ($code) :
- case 'type_id':
- if(strtolower(trim($value)) != 'simple')
- $errors[] = Mage::helper('mpmassuploadaddons')->__('Skip import row, is not valid value "%s" for field "type_id"',$value);
- else
- $data[$code] = strtolower($value);
- break;
- case 'category':
- if(trim($value) == '') {
- $errors[] = Mage::helper('mpmassuploadaddons')->__('Skip import row, is not valid value "%s" for field "category"',$value);
- } else {
- $catagoryIds = array_unique($this->getCategoryIdsByCategoryText($value));
- if(count($catagoryIds) == count(explode(',', $value)))
- $data[$code] = $catagoryIds;
- else
- $errors[] = Mage::helper('mpmassuploadaddons')->__('Skip import row, is not valid value "%s" for field "category"',$value);
- }
- break;
- case 'name':
- if(trim($value) == '')
- $errors[] = Mage::helper('mpmassuploadaddons')->__('Skip import row, is not valid value "%s" for field "name"',$value);
- else
- $data[$code] = $value;
- break;
- case 'description':
- if(trim($value) == '')
- $errors[] = Mage::helper('mpmassuploadaddons')->__('Skip import row, is not valid value "%s" for field "description"',$value);
- else
- $data[$code] = $value;
- break;
- case 'short_description':
- if(trim($value) == '')
- $errors[] = Mage::helper('mpmassuploadaddons')->__('Skip import row, is not valid value "%s" for field "short_description"',$value);
- else
- $data[$code] = $value;
- break;
- case 'sku':
- if(trim($value) == '') {
- $errors[] = Mage::helper('mpmassuploadaddons')->__('Skip import row, is not valid value "%s" for field "sku"',$value);
- } else {
- $existProductId = Mage::getModel('catalog/product')->getIdBySku(trim($value));
- if($existProductId)
- $errors[] = Mage::helper('mpmassuploadaddons')->__('Skip import row, sku "%s" already exist',$value);
- else
- $data[$code] = $value;
- }
- break;
- case 'price':
- if(trim($value) == '')
- $errors[] = Mage::helper('mpmassuploadaddons')->__('Skip import row, is not valid value "%s" for field "price"',$value);
- elseif(!preg_match("/^([0-9])+?[0-9.]*$/",$value))
- $errors[] = Mage::helper('mpmassuploadaddons')->__('Skip import row, is not valid value "%s" for field "price"',$value);
- else
- $data[$code] = $value;
- break;
- case 'special_price':
- if(trim($value) == '')
- $errors[] = Mage::helper('mpmassuploadaddons')->__('Skip import row, is not valid value "%s" for field "price"',$value);
- elseif(!preg_match("/^([0-9])+?[0-9.]*$/",$value))
- $errors[] = Mage::helper('mpmassuploadaddons')->__('Skip import row, is not valid value "%s" for field "special_price"',$value);
- else
- $data[$code] = $value;
- break;
- case 'special_from_date':
- if(trim($value) == '')
- $errors[] = Mage::helper('mpmassuploadaddons')->__('Skip import row, is not valid value "%s" for field "price"',$value);
- // elseif(!preg_match("/^([0-9])+?[0-9.]*$/",$value))
- // $errors[] = Mage::helper('mpmassuploadaddons')->__('Skip import row, is not valid value "%s" for field "special_price"',$value);
- else
- $data[$code] = $value;
- break;
- case 'special_to_date':
- if(trim($value) == '')
- $errors[] = Mage::helper('mpmassuploadaddons')->__('Skip import row, is not valid value "%s" for field "price"',$value);
- // elseif(!preg_match("/^([0-9])+?[0-9.]*$/",$value))
- // $errors[] = Mage::helper('mpmassuploadaddons')->__('Skip import row, is not valid value "%s" for field "special_price"',$value);
- else
- $data[$code] = $value;
- break;
- case 'tax_class_id':
- if(trim($value) == '') {
- $errors[] = Mage::helper('mpmassuploadaddons')->__('Skip import row, is not valid value "%s" for field "tax_class_id"',$value);
- } elseif(trim($value) == 'None') {
- $data[$code] = 0;
- } else {
- $optionId = $this->getAttributeOptionIdbyOptionText("tax_class_id",trim($value));
- if($optionId == "")
- $errors[] = Mage::helper('mpmassuploadaddons')->__('Skip import row, is not valid value "%s" for field "tax_class_id"',$value);
- else
- $data[$code] = $optionId;
- }
- break;
- case 'is_in_stock':
- if(trim($value) == '') {
- $errors[] = Mage::helper('mpmassuploadaddons')->__('Skip import row, is not valid value "%s" for field "is_in_stock"',$value);
- } else {
- if(strtolower(trim($value)) == "in stock")
- $data[$code] = 1;
- elseif(strtolower(trim($value)) == "out of stock")
- $data[$code] = 0;
- else
- $errors[] = Mage::helper('mpmassuploadaddons')->__('Skip import row, is not valid value "%s" for field "is_in_stock"',$value);
- }
- break;
- case 'stock':
- if(trim($value) == '')
- $errors[] = Mage::helper('mpmassuploadaddons')->__('Skip import row, is not valid value "%s" for field "stock"',$value);
- elseif(!preg_match("/^([0-9])+?[0-9.]*$/",$value))
- $errors[] = Mage::helper('marketplace')->__('Skip import row, is not valid value "%s" for field "stock"',$value);
- else
- $data[$code] = $value;
- break;
- case 'weight':
- if(trim($value) == '')
- $errors[] = Mage::helper('mpmassuploadaddons')->__('Skip import row, is not valid value "%s" for field "weight"',$value);
- elseif(!preg_match("/^([0-9])+?[0-9.]*$/",$value))
- $errors[] = Mage::helper('mpmassuploadaddons')->__('Skip import row, is not valid value "%s" for field "weight"',$value);
- else
- $data[$code] = $value;
- break;
- case 'cod_available':
- if(trim($value) == '')
- $errors[] = Mage::helper('mpmassuploadaddons')->__('Skip import row, is not valid value "%s" for field "price"',$value);
- elseif(!preg_match("/^([0-9])+?[0-9.]*$/",$value))
- $errors[] = Mage::helper('mpmassuploadaddons')->__('Skip import row, is not valid value "%s" for field "price"',$value);
- else
- $data[$code] = $value;
- break;
- case 'images':
- $validImageExtensions = array("jpg","jpeg","png","gif");
- $flag = 0;
- foreach (explode(',', $value) as $img) {
- if($img != "") {
- $extension = explode('.', $img);
- $limit = sizeof($extension) - 1;
- if(!in_array($extension[$limit], $validImageExtensions))
- $flag = 1;
- }
- }
- if($flag)
- $errors[] = Mage::helper('mpmassuploadaddons')->__('Skip import row, is not valid extension with value "%s" for field "images"',$value);
- else
- $data[$code] = $value;
- break;
- case 'links_title':
- $data[$code] = $value;
- break;
- case 'links_purchased_separately':
- if(trim($value) == '') {
- $errors[] = Mage::helper('mpmassuploadaddons')->__('Skip import row, is not valid value "%s" for field "links_purchased_separately"',$value);
- } else {
- if(strtolower(trim($value)) == "yes")
- $data[$code] = 1;
- elseif(strtolower(trim($value)) == "no")
- $data[$code] = 0;
- else
- $errors[] = Mage::helper('mpmassuploadaddons')->__('Skip import row, is not valid value "%s" for field "links_purchased_separately"',$value);
- }
- break;
- case 'links':
- $validLinkExtensions = explode(',',Mage::getStoreConfig('marketplace/marketplace_options/downloadfiletype'));
- $flag = 0;
- if($value != "") {
- $extension = explode('.', $value);
- $limit = sizeof($extension) - 1;
- if(!in_array($extension[$limit], $validLinkExtensions))
- $flag = 1;
- } else {
- $flag = 1;
- }
- if($flag)
- $errors[] = Mage::helper('mpmassuploadaddons')->__('Skip import row, is not allowed extension with value "%s" for field "links"',$value);
- else
- $data[$code] = $value;
- break;
- case '_super_attribute_code':
- $flag = 0;
- foreach (explode(',',$value) as $superAttributeCode) {
- $productModel = Mage::getModel('catalog/product');
- $attribute = $productModel->getResource()->getAttribute($superAttributeCode);
- if($attribute) {
- if(!$attribute->getIsConfigurable())
- $flag = 1;
- } else {
- $flag = 1;
- }
- }
- if($flag == 1)
- $errors[] = Mage::helper('mpmassuploadaddons')->__('Skip import row, is not valid value "%s" for field "_super_attribute_code"',$value);
- else
- $data[$code] = $value;
- break;
- case '_super_attribute_option':
- $attributeOptions = explode(',', $value);
- $flag = 0;
- foreach (explode(',', $wholedata['_super_attribute_code']) as $key => $attributeCode) {
- $optionId = $this->getAttributeOptionIdbyOptionText($attributeCode,$attributeOptions[$key]);
- if($optionId == "")
- $flag = 1;
- }
- if($flag == 1)
- $errors[] = Mage::helper('mpmassuploadaddons')->__('Skip import row, is not valid value "%s" for field "_super_attribute_option"',$value);
- else
- $data['_super_attribute_option'] = $value;
- break;
- case '_super_attribute_price_corr':
- $attributeOptionPrice = explode(',', $value);
- $flag = 0;
- foreach (explode(',', $wholedata['_super_attribute_code']) as $key => $attributeCode) {
- if(trim($attributeOptionPrice[$key]) == '')
- $flag = 1;
- elseif(!preg_match("/^([0-9])+?[0-9.]*$/",$attributeOptionPrice[$key]))
- $flag = 1;
- }
- if($flag == 1)
- $errors[] = Mage::helper('mpmassuploadaddons')->__('Skip import row, is not valid value "%s" for field "_super_attribute_price_corr"',$value);
- else
- $data['_super_attribute_price_corr'] = $value;
- break;
- case '_super_attribute_qty':
- if(trim($value) == '')
- $errors[] = Mage::helper('mpmassuploadaddons')->__('Skip import row, is not valid value "%s" for field "_super_attribute_qty"',$value);
- elseif(!preg_match("/^([0-9])+?[0-9.]*$/",$value))
- $errors[] = Mage::helper('marketplace')->__('Skip import row, is not valid value "%s" for field "_super_attribute_qty"',$value);
- else
- $data['stock'] = $value;
- break;
- case '_super_attribute_weight':
- if(trim($value) == '')
- $errors[] = Mage::helper('mpmassuploadaddons')->__('Skip import row, is not valid value "%s" for field "_super_attribute_weight"',$value);
- elseif(!preg_match("/^([0-9])+?[0-9.]*$/",$value))
- $errors[] = Mage::helper('mpmassuploadaddons')->__('Skip import row, is not valid value "%s" for field "_super_attribute_weight"',$value);
- else
- $data['weight'] = $value;
- break;
- endswitch;
- }
- return array($data, $errors);
- }
- /**
- * validateCustomAttributeValues This function validate the csutom attribute option values
- * @param String $attributeCodes Contains attribute code
- * @param String $attributeValues Contains attribute values
- * @var Array $errors Conatines validation failed errors
- * @var Array $data Validate attribute values
- * @var Array $attributeCodes Contains attribute code
- * @var Array $attributeValues Contains attribute values
- */
- private function validateCustomAttributeValues($attributeCodes , $attributeValues) {
- $errors = array();
- $errorFlag = 0;
- $validationRequiredFlag = Mage::getStoreConfig('marketplace/mpmassuploadaddons/validationrequired');
- $data = array();
- $attributeCodes = explode(',', $attributeCodes);
- $attributeValues = explode('::', $attributeValues);
- $productModel = Mage::getModel('catalog/product');
- foreach ($attributeCodes as $key => $attributeCode) {
- $attribute = $productModel->getResource()->getAttribute($attributeCode);
- if(isset($attributeValues[$key]))
- $attributeValue = $attributeValues[$key];
- else
- $attributeValue = "";
- if($attribute) {
- $errorFlag = 0;
- if($attribute->getIsRequired() && $attributeValue == "") {
- $errorFlag = 1;
- } else {
- if($attribute->getFrontendInput() == "select") {
- $optionId = $this->getAttributeOptionIdbyOptionText($attributeCode , $attributeValue);
- if($optionId != "") {
- $data[$attributeCode] = $optionId;
- } else {
- $errorFlag = 1;
- }
- } elseif($attribute->getFrontendInput() == "boolean") {
- if(strtolower($attributeValue) == "yes")
- $data[$attributeCode] = 1;
- elseif(strtolower($attributeValue) == "no")
- $data[$attributeCode] = 0;
- else
- $errorFlag = 1;
- } elseif($attribute->getFrontendInput() == "media_image") {
- $validImageExtensions = array("jpg","jpeg","png","gif");
- $flag = 0;
- if($attributeValue != "") {
- $extension = explode('.', $attributeValue);
- $limit = sizeof($extension) - 1;
- if(!in_array($extension[$limit], $validImageExtensions))
- $errorFlag = 1;
- else
- $data[$attributeCode] = $attributeValue;
- } else {
- $errorFlag = 1;
- }
- } elseif($attribute->getFrontendInput() == "date") {
- if(strptime($attributeValue, '%m/%d/%G') !== false)
- $data[$attributeCode] = $attributeValue;
- else
- $errorFlag = 1;
- } elseif($attribute->getFrontendInput() == "price") {
- if(is_numeric($attributeValue) && $attributeValue >= 0)
- $data[$attributeCode] = $attributeValue;
- else
- $errorFlag = 1;
- } elseif($attribute->getFrontendInput() == "multiselect") {
- $optionId = array();
- foreach (explode('|',$attributeValue) as $text) {
- $optionId[] = $attribute->getSource()->getOptionId(trim($text));
- }
- if(count(array_filter($optionId)) == count(explode('|',$attributeValue)))
- $data[$attributeCode] = $optionId;
- } else {
- $attributeValidationClass = $attribute->getFrontendClass();
- if($attributeValidationClass != "") {
- switch ($attributeValidationClass) :
- case 'validate-number':
- if(!preg_match("/^([0-9])+?[0-9.]*$/",$attributeValue))
- $errorFlag = 1;
- else
- $data[$attributeCode] = $attributeValue;
- break;
- case 'validate-digits':
- if(!preg_match("/^([0-9])+?[0-9.]*$/",$attributeValue))
- $errorFlag = 1;
- else
- $data[$attributeCode] = $attributeValue;
- break;
- case 'validate-email':
- if(!preg_match("/^[_a-z0-9-+]+(\.[_a-z0-9-+]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/",$attributeValue))
- $errorFlag = 1;
- else
- $data[$attributeCode] = $attributeValue;
- break;
- case 'validate-url':
- if(!preg_match("/^(http|https)?:\/\/[a-zA-Z0-9-\.]+\.[a-z]{2,4}/",$attributeValue))
- $errorFlag = 1;
- else
- $data[$attributeCode] = $attributeValue;
- break;
- case 'validate-alpha':
- if(!preg_match("/^[a-zA-Z]+$/",$attributeValue))
- $errorFlag = 1;
- else
- $data[$attributeCode] = $attributeValue;
- break;
- case 'validate-alphanum':
- if(!preg_match("/^[a-zA-Z0-9]+$/",$attributeValue))
- $errorFlag = 1;
- else
- $data[$attributeCode] = $attributeValue;
- break;
- default:
- $data[$attributeCode] = $attributeValue;
- break;
- endswitch;
- } else {
- $data[$attributeCode] = $attributeValue;
- }
- }
- }
- if($errorFlag && $validationRequiredFlag)
- $errors[] = Mage::helper('mpmassuploadaddons')->__('Skip import row, is not valid value "%s" for field "%s"',$attributeValue,$attributeCode);
- } else {
- if($validationRequiredFlag)
- $errors[] = Mage::helper('mpmassuploadaddons')->__('Skip import row, attribute "%s" does not exist',$attributeCode);
- }
- }
- return array($data, $errors);
- }
- /**
- * getAttributeOptionIdbyOptionText This returns
- * @param String $attributeCode Conatines Attribute code
- * @param String $optionText Conatines Attribute text
- * @var Object $productModel Catalog product model object
- * @var Object $attribute Eav Attribute model object
- * @var Int $optionId Containes Attribute option id corrosponding to option text
- * @var String $attributeValidationClass Attribute Validation class
- */
- public function getAttributeOptionIdbyOptionText($attributeCode,$optionText) {
- if($optionText == "")
- return $optionText;
- $productModel = Mage::getModel('catalog/product');
- $attribute = $productModel->getResource()->getAttribute($attributeCode);
- if($attribute) {
- $optionId = $attribute->getSource()->getOptionId(trim($optionText));
- return $optionId;
- } else {
- return "";
- }
- }
- /**
- * getMediaImageAttributesData This function seperate image type attribute details from csv custom attribute column
- * @param String $attributeCodes Comma seperated custom attribute codes
- * @param String $attributeValues Comma seperated custom attribute codes
- * @var Object $productModel Catalog product model object
- * @var Sting $attributeValue Containes attribute values
- * @return Array $data Array of image type attribute values
- */
- public function getMediaImageAttributesData($attributeCodes , $attributeValues) {
- $attributeCodes = explode(',', $attributeCodes);
- $attributeValues = explode(',', $attributeValues);
- $productModel = Mage::getModel('catalog/product');
- $data = array();
- foreach ($attributeCodes as $key => $attributeCode) {
- if(isset($attributeValues[$key]))
- $attributeValue = $attributeValues[$key];
- else
- $attributeValue = "";
- $attribute = $productModel->getResource()->getAttribute($attributeCode);
- if($attribute) {
- if($attribute->getFrontendInput() == "media_image") {
- $data[$attributeCode] = trim($attributeValue);
- }
- }
- }
- return $data;
- }
- /**
- * getCategoryIdsByCategoryText This function is used for getting categories ids from categories text
- * @param String $categoryText Contains catgories text
- * @var Object $categoryCollection Catalog category collection object
- * @return Array $catagoryIds Conatines category corrosponding to category text
- */
- public function getCategoryIdsByCategoryText($categoryText) {
- $catagories = array_filter(explode(',', $categoryText));
- $catagoryIds = array();
- foreach ($catagories as $category) {
- $categoryCollection = Mage::getResourceModel('catalog/category_collection')
- ->addAttributeToSelect('entity_id')
- ->addFieldToFilter('name', $category);
- foreach ($categoryCollection as $value)
- $catagoryIds[] = $value->getId();
- }
- return $catagoryIds;
- }
- /**
- * getAllAttributesCodes Return comma seperated attribute codes for mass upload
- * @var Array $allowedSets marketplace allowed attribute sets
- * @var Array $attributeCodes Containes attribute set codes
- * @var Object $customAttrubutes marketplace custom attribute model object
- * @var Array $temp Containes attributes of 1 attribute set at a time
- * @var Object $attribute Eav attribute model object
- * @return JSON Attribute codes
- */
- public function getAllAttributesCodes() {
- $allowedSets = explode(',',Mage::getStoreConfig('marketplace/marketplace_options/attributesetid'));
- $attributeCodes = array();
- foreach ($allowedSets as $attributeSetId) {
- $customAttrubutes = Mage::getModel('customattribute/customattribute')->getFrontShowAttributes($attributeSetId);
- $attributeCodes[$attributeSetId] = array();
- $temp = array();
- foreach($customAttrubutes as $customAttrubute) {
- $attribute = Mage::getModel('catalog/resource_eav_attribute')->load($customAttrubute['attribute_id']);
- array_push($temp, $attribute['attribute_code']);
- }
- $attributeCodes[$attributeSetId] = implode(",",$temp);
- }
- return json_encode($attributeCodes);
- }
- /**
- * getConfigurableSuperAttributes This function is used for getting the configurable attribute codes
- * @var Array $allowedSets Contains allowed attribute sets ids for Marketplace Vendor
- * @var String $attributeCodes Contains configurable attribute codes corrosponding to attribute set
- * @var Array $attributes Contains Attribute ids
- * @return JSON $attributeCodes Containes configurable attributes
- */
- public function getConfigurableSuperAttributes() {
- $allowedSets = explode(',',Mage::getStoreConfig('marketplace/marketplace_options/attributesetid'));
- $attributeCodes = array();
- foreach ($allowedSets as $attributeSetId) {
- $attributes = Mage::getModel('catalog/product_attribute_api')->items($attributeSetId);
- $attributeCodes[$attributeSetId] = array();
- $temp = array();
- foreach($attributes as $attribute) {
- $data = Mage::getModel('catalog/resource_eav_attribute')->load($attribute['attribute_id']);
- if($data['frontend_input'] == 'select' && $data['is_user_defined'] == 1 && $data['is_configurable'] == 1&& $data['is_visible'] == 1 && $data['is_global'] == 1) {
- array_push($temp, $data['attribute_code']);
- }
- }
- $attributeCodes[$attributeSetId] = implode(",",$temp);
- }
- return json_encode($attributeCodes);
- }
- /**
- * getCsvProductType This function check the csv file product type
- * @param Array $data CSV file header data
- * @return String Return product type
- */
- public function getCsvProductType($data) {
- if(in_array('weight', $data)) {
- return "simple";
- } else {
- if(in_array('link_file', $data)) {
- return "downloadable";
- } elseif(in_array('_super_attribute_code', $data)) {
- return "configurable";
- } else {
- return "virtual";
- }
- }
- return "";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement