Advertisement
Guest User

code

a guest
Oct 13th, 2015
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 66.26 KB | None | 0 0
  1. <?php
  2. class Webkul_Mpmassuploadaddons_Model_Mpmassupload extends Mage_Core_Model_Abstract
  3. {
  4. /**
  5. * importSimpleProducts Used for importing simple product
  6. * @param Int $profileId Profile id that is currenly in execution
  7. * @param $rowCount $rowCount CSV row count
  8. * @var Object $collection Mass upload profiler collection
  9. * @var Array $result Conatines the return data
  10. * @var Array $data Conatines product attribute values
  11. * @var Object $csvFile Containes CSV file name
  12. * @var $imgDir Containes Image directory name
  13. * @var String $targetPath Profiler files target path
  14. * @var Object $fp File Object
  15. * @var Int $status Conatines product approval config status
  16. * @var Object $exist_productid Catalog_Model_Product Object
  17. * @var Int $is_in_stock Stock status
  18. * @var Object $profile Profiler model object
  19. * @var Array $wholedata Containes product atrribute values
  20. * @var Int $proid Catalog Product id
  21. * @var Object $marketplaceProduct Marketplace Product model object
  22. * @var Array $images Product images name
  23. * @return Array $result Conatines the return data
  24. */
  25. public function importSimpleProducts($profileId,$rowCount) {
  26. $helper = Mage::helper("mpmassuploadaddons");
  27. $collection = Mage::getModel('mpmassuploadaddons/profilesession')->getCollection()
  28. ->addFieldToFilter('session_id',array('eq' => $profileId));
  29. $result = array();
  30. $data = array();
  31. $csvFile = "";
  32. $imgDir = "";
  33. if (count($collection) > 0) {
  34. foreach ($collection as $value) {
  35. $csvFile = $value->getCsvFile();
  36. $imgDir = $value->getImageFile();
  37. }
  38. $targetPath = Mage::getBaseDir('media')."/marketplace/massuploaded/".$profileId;
  39. $csvPath = $targetPath.'/'.$csvFile;
  40. $fp = fopen($csvPath, 'r');
  41. while(!feof($fp) ){
  42. $data[] = fgetcsv($fp, 1024);
  43. }
  44.  
  45. fclose($fp);
  46. $info = $data[$rowCount];
  47. $wholedata = array(
  48. 'category' => $info[0],
  49. 'name' => $info[1],
  50. 'description' => $info[2],
  51. 'short_description' => $info[3],
  52. 'sku' => $info[4],
  53. 'price' => $info[5],
  54. 'tax_class_id' => $info[9],
  55. 'is_in_stock' => $info[10],
  56. 'stock' => $info[11],
  57. 'weight' => $info[12],
  58. 'images' => $info[13]
  59. );
  60. if(isset($info[6])){
  61. $specialdata=array(
  62. 'special_price' => $info[6],
  63. 'special_from_date' => $info[7],
  64. 'special_to_date' => $info[8]
  65. );
  66. $wholedata=array_merge($wholedata,$specialdata);
  67. }
  68. if(isset($info[16])){
  69. $systemattribute=$data[0][16];
  70. $othersystemattribute=array(
  71. $systemattribute => $info[16]
  72. );
  73. $wholedata=array_merge($wholedata,$othersystemattribute);
  74. }
  75.  
  76. list($wholedata, $errors) = $this->validatePost($wholedata);
  77.  
  78. if(empty($errors)) {
  79. $customAttributeData = array();
  80. $mediaTypeAttributedata = array();
  81. if(isset($data[0][14])) {
  82. $customAttributeCodes = explode('(', $data[0][14]);
  83.  
  84. if($customAttributeCodes[0] != "") {
  85. list($customAttributeData, $errors) = $this->validateCustomAttributeValues($customAttributeCodes[0],$info[14]);
  86. $mediaTypeAttributedata = $this->getMediaImageAttributesData($customAttributeCodes[0],$info[14]);
  87. }
  88. // echo "<pre>";print_r($customAttributeData);die;
  89. }
  90. if(empty($errors)) {
  91. $profile = Mage::getModel('mpmassuploadaddons/profilesession')->load($profileId);
  92. $additionalInfo = array(
  93. 'type_id' => 'simple',
  94. 'userid' => $profile->getSellerId(),
  95. 'wstoreids' => Mage::app()->getStore()->getStoreId(),
  96. 'attribute_set_id' => $profile->getAttributesetId()
  97. );
  98. $wholedata = array_merge($wholedata,$additionalInfo);
  99. Mage::getSingleton('core/session')->setAttributeSet($profile->getAttributesetId());
  100. $proid = Mage::getModel('marketplace/product')->saveSimpleNewProduct($wholedata);
  101. $objprod = Mage::getModel('catalog/product')->load($proid);
  102. $websites = array();
  103. foreach(Mage::app()->getWebsites(true) as $website) {
  104. $websites[] = $website->getId();
  105. }
  106. $objprod->setWebsiteIds($websites)->save();
  107. $marketplaceProduct = Mage::getModel('marketplace/product')->getCollection()
  108. ->addFieldToFilter("mageproductid",array("eq" => $proid))
  109. ->getFirstItem();
  110. $marketplaceProduct->setUserid($profile->getSellerId())->save();
  111. if($info[13] != "") {
  112. $images = array_reverse(explode(',',$info[13]));
  113. foreach ($images as $image) {
  114. $objprod = Mage::getModel('catalog/product')->load($proid);
  115. $imgp = Mage::getBaseDir('media')."/marketplace/massuploaded/".$profileId."/".$imgDir."/";
  116. $checkimg = glob($imgp . $image);
  117.  
  118. if(count($checkimg) != 0) {
  119. $filepath = $imgp.$image;
  120. $objprod->addImageToMediaGallery($filepath, array ('image','small_image','thumbnail'), true, false);
  121. Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
  122. $objprod->save();
  123. }
  124. }
  125. }
  126. try {
  127. Mage::dispatchEvent('mp_add_customattribute_mass', array('product_id' => $proid,'customattribute' => $customAttributeData));
  128. if(Mage::getModel('customattribute/customattribute')) {
  129. if(count($mediaTypeAttributedata)) {
  130. foreach ($mediaTypeAttributedata as $imageType => $image) {
  131. if($image != "") {
  132. $objprod = Mage::getModel('catalog/product')->load($proid);
  133. $imgp = Mage::getBaseDir('media')."/marketplace/massuploaded/".$profileId."/".$imgDir."/";
  134. $checkimg = glob($imgp . $image);
  135.  
  136. if(count($checkimg) != 0) {
  137. $filepath = $imgp.$image;
  138. $objprod->addImageToMediaGallery($filepath, array ($imageType), true, false);
  139. Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
  140. $objprod->save();
  141. }
  142. }
  143. }
  144. }
  145. }
  146. } catch(Exception $e) {
  147. }
  148. if($info[15] != "") {
  149. $wholedata['id'] = $proid;
  150. $data = json_decode($info[15],true);
  151. $i = 0;
  152. foreach($data as $k => $d) {
  153. foreach ($d as $key => $value) {
  154. if($key == 'values') {
  155. $wholedata['selectoptions'][$i] = $d[$key];
  156. } else {
  157. $wholedata['options'][$i][$key] = $value;
  158. $wholedata['options'][$i]['customoptindex'] = $i;
  159. }
  160. }
  161. $i++;
  162. }
  163. Mage::dispatchEvent('mp_customoption_setdata', $wholedata);
  164. }
  165. $result['savedRows'] = 1;
  166. $result['success'] = $helper->__("successfully saved.");
  167. } else {
  168. $result['savedRows'] = 0;
  169. $result['errors'] = current($errors);
  170. }
  171. } else {
  172. $result['savedRows'] = 0;
  173. $result['errors'] = current($errors);
  174. }
  175. }
  176. return $result;
  177. }
  178.  
  179. /**
  180. * importSimpleProducts Used for importing simple product
  181. * @param Int $profileId Profile id that is currenly in execution
  182. * @param $rowCount $rowCount CSV row count
  183. * @var Object $collection Mass upload profiler collection
  184. * @var Array $result Conatines the return data
  185. * @var Array $data Conatines product attribute values
  186. * @var Object $csvFile Containes CSV file name
  187. * @var $imgDir Containes Image directory name
  188. * @var String $targetPath Profiler files target path
  189. * @var Object $fp File Object
  190. * @var Int $status Conatines product approval config status
  191. * @var Object $exist_productid Catalog_Model_Product Object
  192. * @var Int $is_in_stock Stock status
  193. * @var Object $profile Profiler model object
  194. * @var Array $wholedata Containes product atrribute values
  195. * @var Int $proid Catalog Product id
  196. * @var Object $marketplaceProduct Marketplace Product model object
  197. * @var Array $images Product images name
  198. * @var String $ext Link extension
  199. * @var String $pathnamelink Uploaded link files target path
  200. * @var String $newfile Catalog Product downloadable link files path
  201. * @var Object $linkModel Product Downloadabel link model
  202. * @return Array $result Conatines the return data
  203. */
  204. public function importDownloadableProducts($profileId,$rowCount) {
  205. $helper = Mage::helper("mpmassuploadaddons");
  206. $collection = Mage::getModel('mpmassuploadaddons/profilesession')->getCollection()
  207. ->addFieldToFilter('session_id',array('eq' => $profileId));
  208. $result = array();
  209. $csvFile = "";
  210. $imgDir = "";
  211. $linkDir = "";
  212. if (count($collection) > 0) {
  213. foreach ($collection as $value) {
  214. $csvFile = $value->getCsvFile();
  215. $imgDir = $value->getImageFile();
  216. $linkDir = $value->getLinkFile();
  217. }
  218. $targetPath = Mage::getBaseDir('media')."/marketplace/massuploaded/".$profileId;
  219. $csvPath = $targetPath.'/'.$csvFile;
  220. $fp = fopen($csvPath, 'r');
  221. while(!feof($fp) ) {
  222. $data[] = fgetcsv($fp, 1024);
  223. }
  224.  
  225. fclose($fp);
  226. $info = $data[$rowCount];
  227. $wholedata = array(
  228. 'category' => $info[0],
  229. 'name' => $info[1],
  230. 'description' => $info[2],
  231. 'short_description' => $info[3],
  232. 'sku' => $info[4],
  233. 'price' => $info[5],
  234. 'tax_class_id' => $info[9],
  235. 'is_in_stock' => $info[10],
  236. 'stock' => $info[11],
  237. 'images' => $info[12],
  238. 'links_title' => $info[13],
  239. 'links_purchased_separately' => $info[14],
  240. 'links' => $info[15]
  241. );
  242. if(isset($info[6])){
  243. $specialdata=array(
  244. 'special_price' => $info[6],
  245. 'special_from_date' => $info[7],
  246. 'special_to_date' => $info[8]
  247. );
  248. $wholedata=array_merge($wholedata,$specialdata);
  249. }
  250.  
  251. if(isset($info[18])){
  252. $systemattribute=$data[0][18];
  253. $othersystemattribute=array(
  254. $systemattribute => $info[18]
  255. );
  256. $wholedata=array_merge($wholedata,$othersystemattribute);
  257. }
  258.  
  259. $wholedata = array(
  260. 'category' => $info[0],
  261. 'name' => $info[1],
  262. 'description' => $info[2],
  263. 'short_description' => $info[3],
  264. 'sku' => $info[4],
  265. 'price' => $info[5],
  266. 'tax_class_id' => $info[9],
  267. 'is_in_stock' => $info[10],
  268. 'stock' => $info[11],
  269. 'weight' => $info[12],
  270. 'images' => $info[13]
  271. );
  272.  
  273. list($wholedata, $errors) = $this->validatePost($wholedata);
  274. if(empty($errors)) {
  275. $links = explode(',',$info[15]);
  276. foreach ($links as $link) {
  277. $lngp = Mage::getBaseDir('media')."/marketplace/massuploaded/".$profileId."/".$linkDir."/";
  278. $checkimg = glob($lngp . $link);
  279. if(count($checkimg) == 0) {
  280. $errors[] = Mage::helper('mpmassuploadaddons')->__('Skip import row, link file "%s" does not exist for field "link_file"',$link);
  281. }
  282. }
  283. }
  284. if(empty($errors)) {
  285. $customAttributeData = array();
  286. $mediaTypeAttributedata = array();
  287. if(isset($data[0][16])) {
  288. $customAttributeCodes = explode('(', $data[0][16]);
  289. if($customAttributeCodes[0] != "") {
  290. list($customAttributeData, $errors) = $this->validateCustomAttributeValues($customAttributeCodes[0],$info[16]);
  291. $mediaTypeAttributedata = $this->getMediaImageAttributesData($customAttributeCodes[0],$info[16]);
  292. }
  293. }
  294. if(empty($errors)) {
  295. $profile = Mage::getModel('mpmassuploadaddons/profilesession')->load($profileId);
  296. $additionalInfo = array(
  297. 'type_id' => 'downloadable',
  298. 'userid' => $profile->getSellerId(),
  299. 'wstoreids' => Mage::app()->getStore()->getStoreId(),
  300. 'attribute_set_id' => $profile->getAttributesetId(),
  301. 'producttypecustom' => 'customproductdownloadable',
  302. );
  303. $wholedata = array_merge($wholedata,$additionalInfo);
  304. $proid = Mage::getModel('marketplace/product')->saveDownloadableNewProduct($wholedata);
  305. $objprod = Mage::getModel('catalog/product')->load($proid);
  306. $websites = array();
  307. foreach(Mage::app()->getWebsites(true) as $website) {
  308. $websites[] = $website->getId();
  309. }
  310. $objprod->setWebsiteIds($websites)->save();
  311. $marketplaceProduct = Mage::getModel('marketplace/product')->getCollection()
  312. ->addFieldToFilter("mageproductid",array("eq" => $proid))
  313. ->getFirstItem();
  314. $marketplaceProduct->setUserid($profile->getSellerId())->save();
  315. if($info[12] != "") {
  316. $images = array_reverse(explode(',',$info[12]));
  317. foreach ($images as $image) {
  318. $objprod = Mage::getModel('catalog/product')->load($proid);
  319. $imgp = Mage::getBaseDir('media')."/marketplace/massuploaded/".$profileId."/".$imgDir."/";
  320. $checkimg = glob($imgp . $image);
  321. if(count($checkimg) != 0) {
  322. $filepath = $imgp.$image;
  323. $objprod->addImageToMediaGallery($filepath, array ('image','small_image','thumbnail'), true, false);
  324. Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
  325. $objprod->save();
  326. }
  327. }
  328. }
  329. if($info[15]!="") {
  330. $links = explode(',',$info[15]);
  331. foreach ($links as $link) {
  332. $ext = explode('.',$link);
  333. $pathnamelink = Mage::getBaseDir('media')."/marketplace/massuploaded/".$profileId."/".$linkDir."/".$link;
  334. $linkModel = Mage::getModel('downloadable/link');
  335.  
  336. $filename1 = "UploadedFile-".$proid;
  337. $time = time().$filename1.".".$ext[1];
  338. $newfile = Mage::getBaseDir('media').'/downloadable/files/links/'.$time;
  339.  
  340. copy($pathnamelink, $newfile);
  341. $linkModel->setLinkFile("/".$time);
  342. $linkModel->setLinkType("file");
  343. $linkModel->setProductId($proid);
  344. $web_id = Mage::app()->getStore(Mage_Catalog_Model_Abstract::DEFAULT_STORE_ID)->getWebsiteId().",".Mage::app()->getStore()->getId();
  345. $linkModel->setWebsiteIds(array($web_id));
  346. $linkModel->setStoreId(0);
  347. $linkModel->setTitle("Image-".$proid);
  348. $linkModel->setNumberOfDownloads(0);
  349. $linkModel->save();
  350. break;
  351. }
  352. }
  353. try {
  354. Mage::dispatchEvent('mp_add_customattribute_mass', array('product_id' => $proid,'customattribute' => $customAttributeData));
  355. if(Mage::getModel('customattribute/customattribute')) {
  356. if(count($mediaTypeAttributedata)) {
  357. foreach ($mediaTypeAttributedata as $imageType => $image) {
  358. if($image != "") {
  359. $objprod = Mage::getModel('catalog/product')->load($proid);
  360. $imgp = Mage::getBaseDir('media')."/marketplace/massuploaded/".$profileId."/".$imgDir."/";
  361. $checkimg = glob($imgp . $image);
  362.  
  363. if(count($checkimg) != 0) {
  364. $filepath = $imgp.$image;
  365. $objprod->addImageToMediaGallery($filepath, array ($imageType), true, false);
  366. Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
  367. $objprod->save();
  368. }
  369. }
  370. }
  371. }
  372. }
  373. } catch(Exception $e) {
  374. }
  375. if(isset($info[17]) && $info[17] != "") {
  376. $wholedata['id'] = $proid;
  377. $data = json_decode($info[17],true);
  378. $i=0;
  379. foreach($data as $k => $d){
  380. foreach ($d as $key => $value) {
  381. if($key == 'values') {
  382. $wholedata['selectoptions'][$i] = $d[$key];
  383. } else {
  384. $wholedata['options'][$i][$key] = $value;
  385. $wholedata['options'][$i]['customoptindex'] = $i;
  386. }
  387. }
  388. $i++;
  389. }
  390. Mage::dispatchEvent('mp_customoption_setdata', $wholedata);
  391. }
  392. $result['savedRows'] = 1;
  393. $result['success'] = $helper->__("successfully saved.");
  394. } else {
  395. $result['savedRows'] = 0;
  396. $result['errors'] = current($errors);
  397. }
  398. } else {
  399. $result['savedRows'] = 0;
  400. $result['errors'] = current($errors);
  401. }
  402. }
  403. return $result;
  404. }
  405.  
  406. /**
  407. * importSimpleProducts Used for importing simple product
  408. * @param Int $profileId Profile id that is currenly in execution
  409. * @param $rowCount $rowCount CSV row count
  410. * @var Object $collection Mass upload profiler collection
  411. * @var Array $result Conatines the return data
  412. * @var Array $data Conatines product attribute values
  413. * @var Object $csvFile Containes CSV file name
  414. * @var $imgDir Containes Image directory name
  415. * @var String $targetPath Profiler files target path
  416. * @var Object $fp File Object
  417. * @var Int $status Conatines product approval config status
  418. * @var Object $exist_productid Catalog_Model_Product Object
  419. * @var Int $is_in_stock Stock status
  420. * @var Object $profile Profiler model object
  421. * @var Array $wholedata Containes product atrribute values
  422. * @var Int $proid Catalog Product id
  423. * @var Object $marketplaceProduct Marketplace Product model object
  424. * @var Array $images Product images name
  425. * @return Array $result Conatines the return data
  426. */
  427. public function importVirtualProducts($profileId,$rowCount) {
  428. $helper = Mage::helper("mpmassuploadaddons");
  429. $collection = Mage::getModel('mpmassuploadaddons/profilesession')->getCollection()
  430. ->addFieldToFilter('session_id',array('eq' => $profileId));
  431. $result = array();
  432. $csvFile = "";
  433. $imgDir = "";
  434. if (count($collection) > 0) {
  435. foreach ($collection as $value) {
  436. $csvFile = $value->getCsvFile();
  437. $imgDir = $value->getImageFile();
  438. }
  439. $targetPath = Mage::getBaseDir('media')."/marketplace/massuploaded/".$profileId;
  440. $csvPath = $targetPath.'/'.$csvFile;
  441. $fp = fopen($csvPath, 'r');
  442. while(!feof($fp) ){
  443. $data[] = fgetcsv($fp, 1024);
  444. }
  445.  
  446. fclose($fp);
  447. $info = $data[$rowCount];
  448. $wholedata = array(
  449. 'category' => $info[0],
  450. 'name' => $info[1],
  451. 'description' => $info[2],
  452. 'short_description' => $info[3],
  453. 'sku' => $info[4],
  454. 'price' => $info[5],
  455. 'tax_class_id' => $info[9],
  456. 'is_in_stock' => $info[10],
  457. 'stock' => $info[11],
  458. 'images' => $info[12]
  459. );
  460. if(isset($info[6])){
  461. $specialdata=array(
  462. 'special_price' => $info[6],
  463. 'special_from_date' => $info[7],
  464. 'special_to_date' => $info[8]
  465. );
  466. $wholedata=array_merge($wholedata,$specialdata);
  467. }
  468.  
  469. list($wholedata, $errors) = $this->validatePost($wholedata);
  470. if(empty($errors)) {
  471. $customAttributeData = array();
  472. $mediaTypeAttributedata = array();
  473. if(isset($data[0][13])) {
  474. $customAttributeCodes = explode('(', $data[0][13]);
  475. if($customAttributeCodes[0] != "") {
  476. list($customAttributeData, $errors) = $this->validateCustomAttributeValues($customAttributeCodes[0],$info[13]);
  477. $mediaTypeAttributedata = $this->getMediaImageAttributesData($customAttributeCodes[0],$info[13]);
  478. }
  479. }
  480. if(empty($errors)) {
  481. $profile = Mage::getModel('mpmassuploadaddons/profilesession')->load($profileId);
  482. $additionalInfo = array(
  483. 'type_id' => 'virtual',
  484. 'userid' => $profile->getSellerId(),
  485. 'wstoreids' => Mage::app()->getStore()->getStoreId(),
  486. 'attribute_set_id' => $profile->getAttributesetId(),
  487. 'producttypecustom' => 'customproductvirtual',
  488. );
  489. $wholedata = array_merge($wholedata,$additionalInfo);
  490. Mage::getSingleton('core/session')->setAttributeSet($profile->getAttributesetId());
  491. $proid = Mage::getModel('marketplace/product')->saveVirtualNewProduct($wholedata);
  492. $objprod = Mage::getModel('catalog/product')->load($proid);
  493. $websites = array();
  494. foreach(Mage::app()->getWebsites(true) as $website) {
  495. $websites[] = $website->getId();
  496. }
  497. $objprod->setWebsiteIds($websites)->save();
  498. $marketplaceProduct = Mage::getModel('marketplace/product')->getCollection()
  499. ->addFieldToFilter("mageproductid",array("eq" => $proid))
  500. ->getFirstItem();
  501. $marketplaceProduct->setUserid($profile->getSellerId())->save();
  502. if($info[12] != "") {
  503. $images = array_reverse(explode(',',$info[12]));
  504. foreach ($images as $image) {
  505. $objprod = Mage::getModel('catalog/product')->load($proid);
  506. $imgp = Mage::getBaseDir('media')."/marketplace/massuploaded/".$profileId."/".$imgDir."/";
  507. $checkimg = glob($imgp . $image);
  508.  
  509. if(count($checkimg) != 0) {
  510. $filepath = $imgp.$image;
  511. $objprod->addImageToMediaGallery($filepath, array ('image','small_image','thumbnail'), true, false);
  512. Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
  513. $objprod->save();
  514. }
  515. }
  516. }
  517. try {
  518. Mage::dispatchEvent('mp_add_customattribute_mass', array('product_id' => $proid,'customattribute' => $customAttributeData));
  519. if(Mage::getModel('customattribute/customattribute')) {
  520. if(count($mediaTypeAttributedata)) {
  521. foreach ($mediaTypeAttributedata as $imageType => $image) {
  522. if($image != "") {
  523. $objprod = Mage::getModel('catalog/product')->load($proid);
  524. $imgp = Mage::getBaseDir('media')."/marketplace/massuploaded/".$profileId."/".$imgDir."/";
  525. $checkimg = glob($imgp . $image);
  526.  
  527. if(count($checkimg) != 0) {
  528. $filepath = $imgp.$image;
  529. $objprod->addImageToMediaGallery($filepath, array ($imageType), true, false);
  530. Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
  531. $objprod->save();
  532. }
  533. }
  534. }
  535. }
  536. }
  537. } catch(Exception $e) {
  538. }
  539. if(isset($info[14]) && $info[14] != ""){
  540. $wholedata['id'] = $proid;
  541. $data = json_decode($info[14],true);
  542. $i = 0;
  543. foreach($data as $k => $d) {
  544. foreach ($d as $key => $value) {
  545. if($key == 'values') {
  546. $wholedata['selectoptions'][$i]=$d[$key];
  547. } else {
  548. $wholedata['options'][$i][$key] = $value;
  549. $wholedata['options'][$i]['customoptindex'] = $i;
  550. }
  551. }
  552. $i++;
  553. }
  554. Mage::dispatchEvent('mp_customoption_setdata', $wholedata);
  555. }
  556. $result['savedRows'] = 1;
  557. $result['success'] = $helper->__("successfully saved.");
  558. } else {
  559. $result['savedRows'] = 0;
  560. $result['errors'] = current($errors);
  561. }
  562. } else {
  563. $result['savedRows'] = 0;
  564. $result['errors'] = current($errors);
  565. }
  566. }
  567. return $result;
  568. }
  569.  
  570. /**
  571. * importConfigurableProducts Used for importing simple product
  572. * @param Int $profileId Profile id that is currenly in execution
  573. * @param $rowCount $rowCount CSV row count
  574. * @var Object $collection Mass upload profiler collection
  575. * @var Array $result Conatines the return data
  576. * @var Array $data Conatines product attribute values
  577. * @var Object $csvFile Containes CSV file name
  578. * @var $imgDir Containes Image directory name
  579. * @var String $targetPath Profiler files target path
  580. * @var Object $fp File Object
  581. * @var Int $status Conatines product approval config status
  582. * @var Object $exist_productid Catalog_Model_Product Object
  583. * @var Int $is_in_stock Stock status
  584. * @var Object $profile Profiler model object
  585. * @var Array $wholedata Containes product atrribute values
  586. * @var Array $associateWholedata Associated products data
  587. * @var Array $additionalInfo Configurable product additional information other then csv
  588. * @var Int $proid Catalog Product id
  589. * @var Int $totalProcessedRows Total number of processed row
  590. * @var Object $marketplaceProduct Marketplace Product model object
  591. * @var Array $images Product images name
  592. * @return Array $result Conatines the return data
  593. */
  594. public function importConfigurableProducts($profileId,$rowCount) {
  595. $helper = Mage::helper("mpmassuploadaddons");
  596. $collection = Mage::getModel('mpmassuploadaddons/profilesession')->getCollection()
  597. ->addFieldToFilter('session_id',array('eq' => $profileId));
  598. $result = array();
  599. $data = array();
  600. $csvFile = "";
  601. $imgDir = "";
  602. if (count($collection) > 0) {
  603. foreach ($collection as $value) {
  604. $csvFile = $value->getCsvFile();
  605. $imgDir = $value->getImageFile();
  606. }
  607. $targetPath = Mage::getBaseDir('media')."/marketplace/massuploaded/".$profileId;
  608. $csvPath = $targetPath.'/'.$csvFile;
  609. $fp = fopen($csvPath, 'r');
  610. while(!feof($fp) ){
  611. $data[] = fgetcsv($fp, 1024);
  612. }
  613. fclose($fp);
  614. $info = $data[$rowCount];
  615. $totalProcessedRows = $rowCount;
  616. $attributeOptionCheck = array();
  617. $associatedSkuCheck = array();
  618. $associatedSkuCheck[] = $info[5];
  619. $errors = array();
  620. for ($i = $totalProcessedRows + 1; $i <= sizeof($data); $i++) {
  621. if(strtolower($data[$i][0]) != "configurable" && $data[$i][0] != "") {
  622. $attributeOptionCheck[] = $data[$i][10];
  623. $associatedSkuCheck[] = $data[$i][5];
  624. $totalProcessedRows++;
  625. } else {
  626. if(count($attributeOptionCheck) != count(array_unique($attributeOptionCheck)))
  627. $errors[] = Mage::helper('mpmassuploadaddons')->__('Skip import row, product with duplicate attribute combination');
  628. elseif(count($associatedSkuCheck) != count(array_unique($associatedSkuCheck)))
  629. $errors[] = Mage::helper('mpmassuploadaddons')->__('Skip import row,associated products with duplicate skus');
  630. break;
  631. }
  632. }
  633. if(empty($errors)) {
  634. $wholedata = array(
  635. 'category' => $info[1],
  636. 'name' => $info[2],
  637. 'description' => $info[3],
  638. 'short_description' => $info[4],
  639. 'sku' => $info[5],
  640. 'price' => $info[6],
  641. 'tax_class_id' => $info[10],
  642. 'is_in_stock' => $info[11],
  643. '_super_attribute_code' => $info[12],
  644. 'images' => $info[17]
  645.  
  646. );
  647.  
  648. // $wholedata = array(
  649. // 'category' => $info[0],
  650. // 'name' => $info[1],
  651. // 'description' => $info[2],
  652. // 'short_description' => $info[3],
  653. // 'sku' => $info[4],
  654. // 'price' => $info[5],
  655. // 'tax_class_id' => $info[9],
  656. // 'is_in_stock' => $info[10],
  657. // 'stock' => $info[11],
  658. // 'weight' => $info[12],
  659. // 'images' => $info[13]
  660. // );
  661. if(isset($info[6])){
  662. $specialdata=array(
  663. 'special_price' => $info[7],
  664. 'special_from_date' => $info[8],
  665. 'special_to_date' => $info[9]
  666. );
  667. $wholedata=array_merge($wholedata,$specialdata);
  668. }
  669.  
  670. if(isset($info[20])){
  671. $systemattribute=$data[0][20];
  672. $othersystemattribute=array(
  673. $systemattribute => $info[20]
  674. );
  675. $wholedata=array_merge($wholedata,$othersystemattribute);
  676. }
  677.  
  678. list($wholedata, $errors) = $this->validatePost($wholedata);
  679. if(empty($errors)) {
  680. $customAttributeData = array();
  681. $mediaTypeAttributedata = array();
  682. if(isset($data[0][18])) {
  683. $customAttributeCodes = explode('(', $data[0][18]);
  684. if($customAttributeCodes[0] != "") {
  685. list($customAttributeData, $errors) = $this->validateCustomAttributeValues($customAttributeCodes[0],$info[18]);
  686. $mediaTypeAttributedata = $this->getMediaImageAttributesData($customAttributeCodes[0],$info[18]);
  687. }
  688. }
  689. if(empty($errors)) {
  690. $j = $rowCount;
  691. for ($i = $j + 1; $i <= sizeof($data); $i++) {
  692. if(strtolower($data[$i][0]) != "configurable" && $data[$i][0] != "") {
  693. $associateWholedata = array(
  694. 'type_id' => $data[$i][0],
  695. 'name' => $data[$i][2],
  696. 'sku' => $data[$i][5],
  697. '_super_attribute_code' => $data[$i][12],
  698. '_super_attribute_option' => $data[$i][13],
  699. '_super_attribute_price_corr' => $data[$i][14],
  700. '_super_attribute_qty' => $data[$i][15],
  701. '_super_attribute_weight' => $data[$i][16]
  702. );
  703. list($associateWholedata, $errors) = $this->validatePost($associateWholedata);
  704. if(!empty($errors))
  705. break;
  706. } else {
  707. break;
  708. }
  709. }
  710. if(empty($errors)) {
  711. $profile = Mage::getModel('mpmassuploadaddons/profilesession')->load($profileId);
  712. $additionalInfo = array(
  713. 'type_id' => 'configurable',
  714. 'userid' => $profile->getSellerId(),
  715. 'wstoreids' => Mage::app()->getStore()->getStoreId(),
  716. 'attribute_set_id' => $profile->getAttributesetId()
  717. );
  718. $wholedata = array_merge($wholedata,$additionalInfo);
  719. $proid = $this->saveConfigNewProduct($wholedata);
  720. $objprod = Mage::getModel('catalog/product')->load($proid);
  721. $websites = array();
  722. foreach(Mage::app()->getWebsites(true) as $website) {
  723. $websites[] = $website->getId();
  724. }
  725. $objprod->setWebsiteIds($websites)->save();
  726. $marketplaceProduct = Mage::getModel('marketplace/product')->getCollection()
  727. ->addFieldToFilter("mageproductid",array("eq" => $proid))
  728. ->getFirstItem();
  729. $marketplaceProduct->setUserid($profile->getSellerId())->save();
  730. $totalProcessedRows = $rowCount;
  731. $count = 1;
  732. for ($i = $totalProcessedRows + 1; $i <= sizeof($data); $i++) {
  733. if(strtolower($data[$i][0]) != "configurable" && $data[$i][0] != "") {
  734. $associateWholedata = array(
  735. 'type_id' => 'simple',
  736. 'name' => $data[$i][2],
  737. 'sku' => $data[$i][5],
  738. 'stock' => $data[$i][15],
  739. 'weight' => $data[$i][16],
  740. 'status' => 1,
  741. 'visibility' => 1,
  742. 'mainid' => $proid
  743. );
  744. $configProduct = Mage::getModel("catalog/product")->load($proid);
  745. $associatedPrice = $configProduct->getPrice();
  746. $configattr = Mage::getModel('catalog/product_type_configurable')->getConfigurableAttributesAsArray($configProduct);
  747. foreach (explode(',', $data[$i][12]) as $key => $attribute) {
  748. $optionIds = explode(',', $data[$i][13]);
  749. $optionId = $this->getAttributeOptionIdbyOptionText($attribute , $optionIds[$key]);
  750. $optionPrice = explode(',', $data[$i][14]);
  751. $associatedPrice = $associatedPrice + $optionPrice[$key];
  752. foreach ($configattr as $value) {
  753. if($value['attribute_code'] == $attribute) {
  754. $optionString = $attribute."|price|".$value['id']."|".$optionId;
  755. }
  756. }
  757. $associateWholedata[$attribute] = $optionId;
  758. $associateWholedata[$optionString] = $optionPrice[$key];
  759. $associateWholedata['price'] = $associatedPrice;
  760. }
  761. $this->quickcreate($associateWholedata);
  762. $totalProcessedRows++;
  763. $count++;
  764. } else {
  765. break;
  766. }
  767. }
  768. if(isset($info[17]) && $info[17] != "") {
  769. $images = array_reverse(explode(',',$info[17]));
  770. foreach ($images as $image) {
  771. $objprod = Mage::getModel('catalog/product')->load($proid);
  772. $imgp = Mage::getBaseDir('media')."/marketplace/massuploaded/".$profileId."/".$imgDir."/";
  773. $checkimg = glob($imgp . $image);
  774.  
  775. if(count($checkimg) != 0) {
  776. $filepath = $imgp.$image;
  777. $objprod->addImageToMediaGallery($filepath, array ('image','small_image','thumbnail'), true, false);
  778. Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
  779. $objprod->save();
  780. }
  781. }
  782. }
  783.  
  784. try {
  785. Mage::dispatchEvent('mp_add_customattribute_mass', array('product_id' => $proid,'customattribute' => $customAttributeData));
  786. if(Mage::getModel('customattribute/customattribute')) {
  787. if(count($mediaTypeAttributedata)) {
  788. foreach ($mediaTypeAttributedata as $imageType => $image) {
  789. if($image != "") {
  790. $objprod = Mage::getModel('catalog/product')->load($proid);
  791. $imgp = Mage::getBaseDir('media')."/marketplace/massuploaded/".$profileId."/".$imgDir."/";
  792. $checkimg = glob($imgp . $image);
  793.  
  794. if(count($checkimg) != 0) {
  795. $filepath = $imgp.$image;
  796. $objprod->addImageToMediaGallery($filepath, array ($imageType), true, false);
  797. Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
  798. $objprod->save();
  799. }
  800. }
  801. }
  802. }
  803. }
  804. } catch(Exception $e) {
  805. }
  806. if(isset($info[19]) && $info[19] != "") {
  807. $wholedata['id'] = $proid;
  808. $data = json_decode($info[19],true);
  809. $i = 0;
  810. foreach($data as $k => $d) {
  811. foreach ($d as $key => $value) {
  812. if($key == 'values') {
  813. $wholedata['selectoptions'][$i] = $d[$key];
  814. } else {
  815. $wholedata['options'][$i][$key] = $value;
  816. $wholedata['options'][$i]['customoptindex'] = $i;
  817. }
  818. }
  819. $i++;
  820. }
  821. Mage::dispatchEvent('mp_customoption_setdata', $wholedata);
  822. }
  823. $result['savedRows'] = $count;
  824. $result['rowsCount'] = $totalProcessedRows;
  825. $result['success'] = $helper->__("successfully saved.");
  826. } else {
  827. $result['savedRows'] = 0;
  828. $result['rowsCount'] = $totalProcessedRows;
  829. $result['errors'] = current($errors);
  830. }
  831. } else {
  832. $result['savedRows'] = 0;
  833. $result['rowsCount'] = $totalProcessedRows;
  834. $result['errors'] = current($errors);
  835. }
  836. } else {
  837. $result['savedRows'] = 0;
  838. $result['rowsCount'] = $totalProcessedRows;
  839. $result['errors'] = current($errors);
  840. }
  841. } else {
  842. $result['savedRows'] = 0;
  843. $result['rowsCount'] = $totalProcessedRows;
  844. $result['errors'] = current($errors);
  845. }
  846. }
  847. return $result;
  848. }
  849.  
  850. /**
  851. * saveConfigNewProduct This function is used for saving configurable product
  852. * @param Array $wholedata Configurable product information
  853. * @var String $attr Configurable product super attribute
  854. * @var Int $status Product approval status
  855. * @var Array $stores Store ids
  856. * @var Object $magentoProductModel Catalog product model
  857. * @var String $baseCurrencyCode Containes base currency code
  858. * @var String $currentCurrencyCode Containes current currency code
  859. * @var Array $allowedCurrencies Allowed currency codes
  860. * @var Array $rates Containes currency rates
  861. * @var Float $price Product price
  862. * @var Int $attributeId Super Attribute id
  863. * @var Array $asspro Containes associated prodcuts details
  864. * @var Object $super_attribute Attibute model
  865. * @var Int $lastId Last saved id
  866. * @var Object $stockStatus Catalog inventory model object
  867. * @var Int $vendorId Vendor id
  868. * @var Object $collection1 Marketplace product collection object
  869. */
  870. public function saveConfigNewProduct($wholedata) {
  871. $attr = explode(',',$wholedata['_super_attribute_code']);
  872. if(isset($wholedata['wstoreids'])) {
  873. $stores = $wholedata['wstoreids'];
  874. } else {
  875. $stores = Mage::app()->getStore()->getStoreId();
  876. }
  877. $status = Mage::getStoreConfig('marketplace/marketplace_options/product_approval')? 2:1;
  878. $magentoProductModel = Mage::getModel('catalog/product');
  879. $magentoProductModel->setTypeId(Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE);
  880. $magentoProductModel->setData($wholedata);
  881. $baseCurrencyCode = Mage::app()->getStore()->getBaseCurrencyCode();
  882. $currentCurrencyCode = Mage::app()->getStore()->getCurrentCurrencyCode();
  883. $allowedCurrencies = Mage::getModel('directory/currency')->getConfigAllowCurrencies();
  884. $rates = Mage::getModel('directory/currency')->getCurrencyRates($baseCurrencyCode, array_values($allowedCurrencies));
  885. if(!$rates[$currentCurrencyCode]){
  886. $rates[$currentCurrencyCode] = 1;
  887. }
  888.  
  889. $price = $wholedata['price'] / $rates[$currentCurrencyCode];
  890. $magentoProductModel->setPrice($price);
  891. $magentoProductModel->setStoresIds(array($stores));
  892. $storeId = Mage::app()->getStore()->getId();
  893. $magentoProductModel->setWebsiteIds(array(Mage::getModel('core/store')->load( $storeId )->getWebsiteId()));
  894.  
  895. $attributeId = Mage::getResourceModel('eav/entity_attribute')->getIdByCode('catalog_product',$attr[0]);
  896. $magentoProductModel->getTypeInstance()->setUsedProductAttributeIds(array($attributeId));
  897. foreach($attr as $attrCode) {
  898. $super_attribute = Mage::getModel('eav/entity_attribute')->loadByCode('catalog_product',$attrCode);
  899. $configurableAtt = Mage::getModel('catalog/product_type_configurable_attribute')->setProductAttribute($super_attribute);
  900. $newAttributes[] = array(
  901. 'id' => $configurableAtt->getId(),
  902. 'label' => $configurableAtt->getLabel(),
  903. 'position' => $super_attribute->getPosition(),
  904. 'values' => $configurableAtt->getPrices() ? $configProduct->getPrices() : array(),
  905. 'attribute_id' => $super_attribute->getId(),
  906. 'attribute_code' => $super_attribute->getAttributeCode(),
  907. 'frontend_label' => $super_attribute->getFrontend()->getLabel(),
  908. );
  909. }
  910. $magentoProductModel->setConfigurableAttributesData($newAttributes);
  911. $saved = $magentoProductModel->save();
  912. $magentoProductModel = Mage::getModel('catalog/product')->load($saved->getId());
  913. $magentoProductModel->setCategoryIds(Array($wholedata['category']));
  914. $magentoProductModel->setStatus($status);
  915. $baseCurrencyCode = Mage::app()->getStore()->getBaseCurrencyCode();
  916. $currentCurrencyCode = Mage::app()->getStore()->getCurrentCurrencyCode();
  917. $allowedCurrencies = Mage::getModel('directory/currency')->getConfigAllowCurrencies();
  918. $rates = Mage::getModel('directory/currency')->getCurrencyRates($baseCurrencyCode, array_values($allowedCurrencies));
  919. if(!$rates[$currentCurrencyCode]) {
  920. $rates[$currentCurrencyCode] = 1;
  921. }
  922. $price = $wholedata['price'] / $rates[$currentCurrencyCode];
  923. $magentoProductModel->setPrice($price);
  924. $magentoProductModel->setStockData(array(
  925. 'use_config_manage_stock' => 1,
  926. 'is_in_stock' => $wholedata['is_in_stock'],
  927. 'is_salable' => 1,
  928. ));
  929. $saved = $magentoProductModel->save();
  930. $lastId = $saved->getId();
  931. $stockStatus = Mage::getModel('cataloginventory/stock_status');
  932. $stockStatus->assignProduct($magentoProductModel);
  933. $stockStatus->saveProductStatus($magentoProductModel->getId(), 1);
  934. $vendorId = Mage::getSingleton('customer/session')->getCustomer()->getId();
  935. $collection1 = Mage::getModel('marketplace/product');
  936. $collection1->setmageproductid($lastId);
  937. $collection1->setuserid($vendorId);
  938. $collection1->setstatus($status);
  939. $collection1->save();
  940.  
  941. $wholedata['id'] = $lastId;
  942. Mage::dispatchEvent('mp_customoption_setdata', $wholedata);
  943.  
  944. $app = Mage::app('admin');
  945. umask(0);
  946. return $lastId;
  947.  
  948. }
  949.  
  950. /**
  951. * quickcreate Quick Create associated products
  952. * @param Array $wholedata Assocaited product details
  953. * @var Int $price Assocaited product price
  954. * @var Object $product Catalog product model
  955. * @var Array $childIds Configurable product's assocaited product details
  956. * @var Array $data Configurable product's assocaited product details
  957. * @var Object $configproducts Configurable product object
  958. * @var Object $magentoProductModel Catalog Product model object
  959. * @var String $baseCurrencyCode Containes base currency code
  960. * @var String $currentCurrencyCode Containes current currency code
  961. * @var Array $allowedCurrencies Allowed currency codes
  962. * @var Array $rates Containes currency rates
  963. * @var Float $price Product price
  964. * @var Array $configattr Cofigurable super attribute
  965. * @var Array $supattr Cofigurable super attribute
  966. */
  967. public function quickcreate($wholedata) {
  968. $price = 0;
  969. $product = Mage::getModel('catalog/product')->load($wholedata['mainid']);
  970. $childIds = Mage::getModel('catalog/product_type_configurable')->getChildrenIds($wholedata['mainid']);
  971. foreach($childIds[0] as $val) {
  972. $data[$val] = array();
  973. }
  974. $configproducts = Mage::getModel('catalog/product')->load($wholedata['mainid']);
  975. $magentoProductModel = Mage::getModel('catalog/product');
  976. $magentoProductModel->setData($wholedata);
  977. $magentoProductModel->setName($wholedata['name']);
  978. $magentoProductModel->setDescription($product->getDescription());
  979. $magentoProductModel->setShortDescription($product->getShortDescription());
  980. $magentoProductModel->setSku($wholedata['sku']);
  981. $baseCurrencyCode = Mage::app()->getStore()->getBaseCurrencyCode();
  982. $currentCurrencyCode = Mage::app()->getStore()->getCurrentCurrencyCode();
  983. $allowedCurrencies = Mage::getModel('directory/currency')->getConfigAllowCurrencies();
  984. $rates = Mage::getModel('directory/currency')->getCurrencyRates($baseCurrencyCode, array_values($allowedCurrencies));
  985. if(!$rates[$currentCurrencyCode]) {
  986. $rates[$currentCurrencyCode] = 1;
  987. }
  988. $price = $wholedata['price'] / $rates[$currentCurrencyCode];
  989. $magentoProductModel->setPrice($price);
  990. $magentoProductModel->setCategoryIds($product->getCategoryIds());
  991. $storeId = Mage::app()->getStore()->getId();
  992. $magentoProductModel->setWebsiteIds(array(Mage::getModel('core/store')->load( $storeId )->getWebsiteId()));
  993. $magentoProductModel->setTypeId('simple');
  994. $magentoProductModel->setAttributeSetId($configproducts->getAttributeSetId());
  995. $magentoProductModel->setTaxClassId("None");
  996. $magentoProductModel->setStockData(array(
  997. 'is_in_stock' => 1,
  998. 'qty' => $wholedata['stock']
  999. )
  1000. );
  1001. $saved = $magentoProductModel->save();
  1002. $lastid = $saved->getId();
  1003. $objprod = Mage::getModel('catalog/product')->load($lastid);
  1004. $websites = array();
  1005. foreach(Mage::app()->getWebsites(true) as $website) {
  1006. $websites[] = $website->getId();
  1007. }
  1008. $objprod->setWebsiteIds($websites)->save();
  1009. $data[$lastid] = array();
  1010. $product->setConfigurableProductsData($data);
  1011. $product->setCanSaveConfigurableAttributes(true);
  1012. $product->save();
  1013. $configattr = Mage::getModel('catalog/product_type_configurable')->getConfigurableAttributesAsArray($configproducts);
  1014. foreach ($wholedata as $key => $value) {
  1015. if(strpos($key,'|') !== false){
  1016. $supattr = explode('|', $key);
  1017. for($i = 0; $i < count($configattr); $i++) {
  1018. if($supattr[2] == $configattr[$i]['id'] ){
  1019. for ($j = 0; $j < count($configattr[$i]['values']); $j++) {
  1020. if($configattr[$i]['values'][$j]['value_index'] == $supattr[3]) {
  1021. $currentCurrencyCode = Mage::app()->getStore()->getCurrentCurrencyCode();
  1022. $allowedCurrencies = Mage::getModel('directory/currency')->getConfigAllowCurrencies();
  1023. $rates = Mage::getModel('directory/currency')->getCurrencyRates($baseCurrencyCode, array_values($allowedCurrencies));
  1024. $price = $value / $rates[$currentCurrencyCode];
  1025. $configattr[$i]['values'][$j]['pricing_value'] = $price;
  1026. $configattr[$i]['values'][$j]['can_edit_price'] = 1;
  1027. $configattr[$i]['values'][$j]['can_read_price'] = 1;
  1028. }
  1029. }
  1030. }
  1031. }
  1032. }
  1033. }
  1034. $configproducts->setConfigurableAttributesData($configattr);
  1035. $configproducts->save();
  1036. $app = Mage::app('admin');
  1037. umask(0);
  1038. return 0;
  1039. }
  1040.  
  1041. /**
  1042. * validatePost This function validate impoert product information
  1043. * @param Array $wholedata Containes product information
  1044. * @var Array $errors Containes validattion failed error
  1045. * @var Array $data Containes validate product information
  1046. * @var String $code Product attribute code
  1047. * @var $value Contains attribute values
  1048. * @var Array $catagoryIds Contains product categories ids
  1049. * @var String $existProductId Contains product sku
  1050. * @var Int $optionId Contains Option id of tax class id attribute
  1051. * @var Object $productModel Catalog product model object
  1052. * @var Object $attribute Eav Attribute model object
  1053. * @return Array Returns error and validated data
  1054. */
  1055. private function validatePost($wholedata) {
  1056. $errors = array();
  1057. $data = array();
  1058. foreach( $wholedata as $code => $value){
  1059. switch ($code) :
  1060. case 'type_id':
  1061. if(strtolower(trim($value)) != 'simple')
  1062. $errors[] = Mage::helper('mpmassuploadaddons')->__('Skip import row, is not valid value "%s" for field "type_id"',$value);
  1063. else
  1064. $data[$code] = strtolower($value);
  1065. break;
  1066. case 'category':
  1067. if(trim($value) == '') {
  1068. $errors[] = Mage::helper('mpmassuploadaddons')->__('Skip import row, is not valid value "%s" for field "category"',$value);
  1069. } else {
  1070. $catagoryIds = array_unique($this->getCategoryIdsByCategoryText($value));
  1071. if(count($catagoryIds) == count(explode(',', $value)))
  1072. $data[$code] = $catagoryIds;
  1073. else
  1074. $errors[] = Mage::helper('mpmassuploadaddons')->__('Skip import row, is not valid value "%s" for field "category"',$value);
  1075. }
  1076. break;
  1077. case 'name':
  1078. if(trim($value) == '')
  1079. $errors[] = Mage::helper('mpmassuploadaddons')->__('Skip import row, is not valid value "%s" for field "name"',$value);
  1080. else
  1081. $data[$code] = $value;
  1082. break;
  1083. case 'description':
  1084. if(trim($value) == '')
  1085. $errors[] = Mage::helper('mpmassuploadaddons')->__('Skip import row, is not valid value "%s" for field "description"',$value);
  1086. else
  1087. $data[$code] = $value;
  1088. break;
  1089. case 'short_description':
  1090. if(trim($value) == '')
  1091. $errors[] = Mage::helper('mpmassuploadaddons')->__('Skip import row, is not valid value "%s" for field "short_description"',$value);
  1092. else
  1093. $data[$code] = $value;
  1094. break;
  1095. case 'sku':
  1096. if(trim($value) == '') {
  1097. $errors[] = Mage::helper('mpmassuploadaddons')->__('Skip import row, is not valid value "%s" for field "sku"',$value);
  1098. } else {
  1099. $existProductId = Mage::getModel('catalog/product')->getIdBySku(trim($value));
  1100. if($existProductId)
  1101. $errors[] = Mage::helper('mpmassuploadaddons')->__('Skip import row, sku "%s" already exist',$value);
  1102. else
  1103. $data[$code] = $value;
  1104. }
  1105. break;
  1106. case 'price':
  1107. if(trim($value) == '')
  1108. $errors[] = Mage::helper('mpmassuploadaddons')->__('Skip import row, is not valid value "%s" for field "price"',$value);
  1109. elseif(!preg_match("/^([0-9])+?[0-9.]*$/",$value))
  1110. $errors[] = Mage::helper('mpmassuploadaddons')->__('Skip import row, is not valid value "%s" for field "price"',$value);
  1111. else
  1112. $data[$code] = $value;
  1113. break;
  1114. case 'special_price':
  1115. if(trim($value) == '')
  1116. $errors[] = Mage::helper('mpmassuploadaddons')->__('Skip import row, is not valid value "%s" for field "price"',$value);
  1117. elseif(!preg_match("/^([0-9])+?[0-9.]*$/",$value))
  1118. $errors[] = Mage::helper('mpmassuploadaddons')->__('Skip import row, is not valid value "%s" for field "special_price"',$value);
  1119. else
  1120. $data[$code] = $value;
  1121. break;
  1122. case 'special_from_date':
  1123. if(trim($value) == '')
  1124. $errors[] = Mage::helper('mpmassuploadaddons')->__('Skip import row, is not valid value "%s" for field "price"',$value);
  1125. // elseif(!preg_match("/^([0-9])+?[0-9.]*$/",$value))
  1126. // $errors[] = Mage::helper('mpmassuploadaddons')->__('Skip import row, is not valid value "%s" for field "special_price"',$value);
  1127. else
  1128. $data[$code] = $value;
  1129. break;
  1130. case 'special_to_date':
  1131. if(trim($value) == '')
  1132. $errors[] = Mage::helper('mpmassuploadaddons')->__('Skip import row, is not valid value "%s" for field "price"',$value);
  1133. // elseif(!preg_match("/^([0-9])+?[0-9.]*$/",$value))
  1134. // $errors[] = Mage::helper('mpmassuploadaddons')->__('Skip import row, is not valid value "%s" for field "special_price"',$value);
  1135. else
  1136. $data[$code] = $value;
  1137. break;
  1138. case 'tax_class_id':
  1139. if(trim($value) == '') {
  1140. $errors[] = Mage::helper('mpmassuploadaddons')->__('Skip import row, is not valid value "%s" for field "tax_class_id"',$value);
  1141. } elseif(trim($value) == 'None') {
  1142. $data[$code] = 0;
  1143. } else {
  1144. $optionId = $this->getAttributeOptionIdbyOptionText("tax_class_id",trim($value));
  1145. if($optionId == "")
  1146. $errors[] = Mage::helper('mpmassuploadaddons')->__('Skip import row, is not valid value "%s" for field "tax_class_id"',$value);
  1147. else
  1148. $data[$code] = $optionId;
  1149. }
  1150. break;
  1151. case 'is_in_stock':
  1152. if(trim($value) == '') {
  1153. $errors[] = Mage::helper('mpmassuploadaddons')->__('Skip import row, is not valid value "%s" for field "is_in_stock"',$value);
  1154. } else {
  1155. if(strtolower(trim($value)) == "in stock")
  1156. $data[$code] = 1;
  1157. elseif(strtolower(trim($value)) == "out of stock")
  1158. $data[$code] = 0;
  1159. else
  1160. $errors[] = Mage::helper('mpmassuploadaddons')->__('Skip import row, is not valid value "%s" for field "is_in_stock"',$value);
  1161. }
  1162. break;
  1163. case 'stock':
  1164. if(trim($value) == '')
  1165. $errors[] = Mage::helper('mpmassuploadaddons')->__('Skip import row, is not valid value "%s" for field "stock"',$value);
  1166. elseif(!preg_match("/^([0-9])+?[0-9.]*$/",$value))
  1167. $errors[] = Mage::helper('marketplace')->__('Skip import row, is not valid value "%s" for field "stock"',$value);
  1168. else
  1169. $data[$code] = $value;
  1170. break;
  1171. case 'weight':
  1172. if(trim($value) == '')
  1173. $errors[] = Mage::helper('mpmassuploadaddons')->__('Skip import row, is not valid value "%s" for field "weight"',$value);
  1174. elseif(!preg_match("/^([0-9])+?[0-9.]*$/",$value))
  1175. $errors[] = Mage::helper('mpmassuploadaddons')->__('Skip import row, is not valid value "%s" for field "weight"',$value);
  1176. else
  1177. $data[$code] = $value;
  1178. break;
  1179. case 'cod_available':
  1180. if(trim($value) == '')
  1181. $errors[] = Mage::helper('mpmassuploadaddons')->__('Skip import row, is not valid value "%s" for field "price"',$value);
  1182. elseif(!preg_match("/^([0-9])+?[0-9.]*$/",$value))
  1183. $errors[] = Mage::helper('mpmassuploadaddons')->__('Skip import row, is not valid value "%s" for field "price"',$value);
  1184. else
  1185. $data[$code] = $value;
  1186. break;
  1187. case 'images':
  1188. $validImageExtensions = array("jpg","jpeg","png","gif");
  1189. $flag = 0;
  1190. foreach (explode(',', $value) as $img) {
  1191. if($img != "") {
  1192. $extension = explode('.', $img);
  1193. $limit = sizeof($extension) - 1;
  1194. if(!in_array($extension[$limit], $validImageExtensions))
  1195. $flag = 1;
  1196. }
  1197. }
  1198. if($flag)
  1199. $errors[] = Mage::helper('mpmassuploadaddons')->__('Skip import row, is not valid extension with value "%s" for field "images"',$value);
  1200. else
  1201. $data[$code] = $value;
  1202. break;
  1203. case 'links_title':
  1204. $data[$code] = $value;
  1205. break;
  1206. case 'links_purchased_separately':
  1207. if(trim($value) == '') {
  1208. $errors[] = Mage::helper('mpmassuploadaddons')->__('Skip import row, is not valid value "%s" for field "links_purchased_separately"',$value);
  1209. } else {
  1210. if(strtolower(trim($value)) == "yes")
  1211. $data[$code] = 1;
  1212. elseif(strtolower(trim($value)) == "no")
  1213. $data[$code] = 0;
  1214. else
  1215. $errors[] = Mage::helper('mpmassuploadaddons')->__('Skip import row, is not valid value "%s" for field "links_purchased_separately"',$value);
  1216. }
  1217. break;
  1218. case 'links':
  1219. $validLinkExtensions = explode(',',Mage::getStoreConfig('marketplace/marketplace_options/downloadfiletype'));
  1220. $flag = 0;
  1221. if($value != "") {
  1222. $extension = explode('.', $value);
  1223. $limit = sizeof($extension) - 1;
  1224. if(!in_array($extension[$limit], $validLinkExtensions))
  1225. $flag = 1;
  1226. } else {
  1227. $flag = 1;
  1228. }
  1229. if($flag)
  1230. $errors[] = Mage::helper('mpmassuploadaddons')->__('Skip import row, is not allowed extension with value "%s" for field "links"',$value);
  1231. else
  1232. $data[$code] = $value;
  1233. break;
  1234. case '_super_attribute_code':
  1235. $flag = 0;
  1236. foreach (explode(',',$value) as $superAttributeCode) {
  1237. $productModel = Mage::getModel('catalog/product');
  1238. $attribute = $productModel->getResource()->getAttribute($superAttributeCode);
  1239. if($attribute) {
  1240. if(!$attribute->getIsConfigurable())
  1241. $flag = 1;
  1242. } else {
  1243. $flag = 1;
  1244. }
  1245. }
  1246. if($flag == 1)
  1247. $errors[] = Mage::helper('mpmassuploadaddons')->__('Skip import row, is not valid value "%s" for field "_super_attribute_code"',$value);
  1248. else
  1249. $data[$code] = $value;
  1250. break;
  1251. case '_super_attribute_option':
  1252. $attributeOptions = explode(',', $value);
  1253. $flag = 0;
  1254. foreach (explode(',', $wholedata['_super_attribute_code']) as $key => $attributeCode) {
  1255. $optionId = $this->getAttributeOptionIdbyOptionText($attributeCode,$attributeOptions[$key]);
  1256. if($optionId == "")
  1257. $flag = 1;
  1258. }
  1259. if($flag == 1)
  1260. $errors[] = Mage::helper('mpmassuploadaddons')->__('Skip import row, is not valid value "%s" for field "_super_attribute_option"',$value);
  1261. else
  1262. $data['_super_attribute_option'] = $value;
  1263. break;
  1264. case '_super_attribute_price_corr':
  1265. $attributeOptionPrice = explode(',', $value);
  1266. $flag = 0;
  1267. foreach (explode(',', $wholedata['_super_attribute_code']) as $key => $attributeCode) {
  1268. if(trim($attributeOptionPrice[$key]) == '')
  1269. $flag = 1;
  1270. elseif(!preg_match("/^([0-9])+?[0-9.]*$/",$attributeOptionPrice[$key]))
  1271. $flag = 1;
  1272. }
  1273. if($flag == 1)
  1274. $errors[] = Mage::helper('mpmassuploadaddons')->__('Skip import row, is not valid value "%s" for field "_super_attribute_price_corr"',$value);
  1275. else
  1276. $data['_super_attribute_price_corr'] = $value;
  1277. break;
  1278. case '_super_attribute_qty':
  1279. if(trim($value) == '')
  1280. $errors[] = Mage::helper('mpmassuploadaddons')->__('Skip import row, is not valid value "%s" for field "_super_attribute_qty"',$value);
  1281. elseif(!preg_match("/^([0-9])+?[0-9.]*$/",$value))
  1282. $errors[] = Mage::helper('marketplace')->__('Skip import row, is not valid value "%s" for field "_super_attribute_qty"',$value);
  1283. else
  1284. $data['stock'] = $value;
  1285. break;
  1286. case '_super_attribute_weight':
  1287. if(trim($value) == '')
  1288. $errors[] = Mage::helper('mpmassuploadaddons')->__('Skip import row, is not valid value "%s" for field "_super_attribute_weight"',$value);
  1289. elseif(!preg_match("/^([0-9])+?[0-9.]*$/",$value))
  1290. $errors[] = Mage::helper('mpmassuploadaddons')->__('Skip import row, is not valid value "%s" for field "_super_attribute_weight"',$value);
  1291. else
  1292. $data['weight'] = $value;
  1293. break;
  1294. endswitch;
  1295. }
  1296. return array($data, $errors);
  1297. }
  1298.  
  1299. /**
  1300. * validateCustomAttributeValues This function validate the csutom attribute option values
  1301. * @param String $attributeCodes Contains attribute code
  1302. * @param String $attributeValues Contains attribute values
  1303. * @var Array $errors Conatines validation failed errors
  1304. * @var Array $data Validate attribute values
  1305. * @var Array $attributeCodes Contains attribute code
  1306. * @var Array $attributeValues Contains attribute values
  1307. */
  1308. private function validateCustomAttributeValues($attributeCodes , $attributeValues) {
  1309. $errors = array();
  1310. $errorFlag = 0;
  1311. $validationRequiredFlag = Mage::getStoreConfig('marketplace/mpmassuploadaddons/validationrequired');
  1312. $data = array();
  1313. $attributeCodes = explode(',', $attributeCodes);
  1314. $attributeValues = explode('::', $attributeValues);
  1315. $productModel = Mage::getModel('catalog/product');
  1316. foreach ($attributeCodes as $key => $attributeCode) {
  1317. $attribute = $productModel->getResource()->getAttribute($attributeCode);
  1318. if(isset($attributeValues[$key]))
  1319. $attributeValue = $attributeValues[$key];
  1320. else
  1321. $attributeValue = "";
  1322. if($attribute) {
  1323. $errorFlag = 0;
  1324. if($attribute->getIsRequired() && $attributeValue == "") {
  1325. $errorFlag = 1;
  1326. } else {
  1327. if($attribute->getFrontendInput() == "select") {
  1328. $optionId = $this->getAttributeOptionIdbyOptionText($attributeCode , $attributeValue);
  1329. if($optionId != "") {
  1330. $data[$attributeCode] = $optionId;
  1331. } else {
  1332. $errorFlag = 1;
  1333. }
  1334. } elseif($attribute->getFrontendInput() == "boolean") {
  1335. if(strtolower($attributeValue) == "yes")
  1336. $data[$attributeCode] = 1;
  1337. elseif(strtolower($attributeValue) == "no")
  1338. $data[$attributeCode] = 0;
  1339. else
  1340. $errorFlag = 1;
  1341. } elseif($attribute->getFrontendInput() == "media_image") {
  1342. $validImageExtensions = array("jpg","jpeg","png","gif");
  1343. $flag = 0;
  1344. if($attributeValue != "") {
  1345. $extension = explode('.', $attributeValue);
  1346. $limit = sizeof($extension) - 1;
  1347. if(!in_array($extension[$limit], $validImageExtensions))
  1348. $errorFlag = 1;
  1349. else
  1350. $data[$attributeCode] = $attributeValue;
  1351. } else {
  1352. $errorFlag = 1;
  1353. }
  1354. } elseif($attribute->getFrontendInput() == "date") {
  1355. if(strptime($attributeValue, '%m/%d/%G') !== false)
  1356. $data[$attributeCode] = $attributeValue;
  1357. else
  1358. $errorFlag = 1;
  1359. } elseif($attribute->getFrontendInput() == "price") {
  1360. if(is_numeric($attributeValue) && $attributeValue >= 0)
  1361. $data[$attributeCode] = $attributeValue;
  1362. else
  1363. $errorFlag = 1;
  1364. } elseif($attribute->getFrontendInput() == "multiselect") {
  1365. $optionId = array();
  1366. foreach (explode('|',$attributeValue) as $text) {
  1367. $optionId[] = $attribute->getSource()->getOptionId(trim($text));
  1368. }
  1369. if(count(array_filter($optionId)) == count(explode('|',$attributeValue)))
  1370. $data[$attributeCode] = $optionId;
  1371. } else {
  1372. $attributeValidationClass = $attribute->getFrontendClass();
  1373. if($attributeValidationClass != "") {
  1374. switch ($attributeValidationClass) :
  1375. case 'validate-number':
  1376. if(!preg_match("/^([0-9])+?[0-9.]*$/",$attributeValue))
  1377. $errorFlag = 1;
  1378. else
  1379. $data[$attributeCode] = $attributeValue;
  1380. break;
  1381. case 'validate-digits':
  1382. if(!preg_match("/^([0-9])+?[0-9.]*$/",$attributeValue))
  1383. $errorFlag = 1;
  1384. else
  1385. $data[$attributeCode] = $attributeValue;
  1386. break;
  1387. case 'validate-email':
  1388. if(!preg_match("/^[_a-z0-9-+]+(\.[_a-z0-9-+]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/",$attributeValue))
  1389. $errorFlag = 1;
  1390. else
  1391. $data[$attributeCode] = $attributeValue;
  1392. break;
  1393. case 'validate-url':
  1394. if(!preg_match("/^(http|https)?:\/\/[a-zA-Z0-9-\.]+\.[a-z]{2,4}/",$attributeValue))
  1395. $errorFlag = 1;
  1396. else
  1397. $data[$attributeCode] = $attributeValue;
  1398. break;
  1399. case 'validate-alpha':
  1400. if(!preg_match("/^[a-zA-Z]+$/",$attributeValue))
  1401. $errorFlag = 1;
  1402. else
  1403. $data[$attributeCode] = $attributeValue;
  1404. break;
  1405. case 'validate-alphanum':
  1406. if(!preg_match("/^[a-zA-Z0-9]+$/",$attributeValue))
  1407. $errorFlag = 1;
  1408. else
  1409. $data[$attributeCode] = $attributeValue;
  1410. break;
  1411. default:
  1412. $data[$attributeCode] = $attributeValue;
  1413. break;
  1414. endswitch;
  1415. } else {
  1416. $data[$attributeCode] = $attributeValue;
  1417. }
  1418. }
  1419. }
  1420. if($errorFlag && $validationRequiredFlag)
  1421. $errors[] = Mage::helper('mpmassuploadaddons')->__('Skip import row, is not valid value "%s" for field "%s"',$attributeValue,$attributeCode);
  1422. } else {
  1423. if($validationRequiredFlag)
  1424. $errors[] = Mage::helper('mpmassuploadaddons')->__('Skip import row, attribute "%s" does not exist',$attributeCode);
  1425. }
  1426. }
  1427. return array($data, $errors);
  1428. }
  1429.  
  1430. /**
  1431. * getAttributeOptionIdbyOptionText This returns
  1432. * @param String $attributeCode Conatines Attribute code
  1433. * @param String $optionText Conatines Attribute text
  1434. * @var Object $productModel Catalog product model object
  1435. * @var Object $attribute Eav Attribute model object
  1436. * @var Int $optionId Containes Attribute option id corrosponding to option text
  1437. * @var String $attributeValidationClass Attribute Validation class
  1438. */
  1439. public function getAttributeOptionIdbyOptionText($attributeCode,$optionText) {
  1440. if($optionText == "")
  1441. return $optionText;
  1442. $productModel = Mage::getModel('catalog/product');
  1443. $attribute = $productModel->getResource()->getAttribute($attributeCode);
  1444. if($attribute) {
  1445. $optionId = $attribute->getSource()->getOptionId(trim($optionText));
  1446. return $optionId;
  1447. } else {
  1448. return "";
  1449. }
  1450. }
  1451.  
  1452. /**
  1453. * getMediaImageAttributesData This function seperate image type attribute details from csv custom attribute column
  1454. * @param String $attributeCodes Comma seperated custom attribute codes
  1455. * @param String $attributeValues Comma seperated custom attribute codes
  1456. * @var Object $productModel Catalog product model object
  1457. * @var Sting $attributeValue Containes attribute values
  1458. * @return Array $data Array of image type attribute values
  1459. */
  1460. public function getMediaImageAttributesData($attributeCodes , $attributeValues) {
  1461. $attributeCodes = explode(',', $attributeCodes);
  1462. $attributeValues = explode(',', $attributeValues);
  1463. $productModel = Mage::getModel('catalog/product');
  1464. $data = array();
  1465. foreach ($attributeCodes as $key => $attributeCode) {
  1466. if(isset($attributeValues[$key]))
  1467. $attributeValue = $attributeValues[$key];
  1468. else
  1469. $attributeValue = "";
  1470. $attribute = $productModel->getResource()->getAttribute($attributeCode);
  1471. if($attribute) {
  1472. if($attribute->getFrontendInput() == "media_image") {
  1473. $data[$attributeCode] = trim($attributeValue);
  1474. }
  1475. }
  1476. }
  1477. return $data;
  1478. }
  1479.  
  1480. /**
  1481. * getCategoryIdsByCategoryText This function is used for getting categories ids from categories text
  1482. * @param String $categoryText Contains catgories text
  1483. * @var Object $categoryCollection Catalog category collection object
  1484. * @return Array $catagoryIds Conatines category corrosponding to category text
  1485. */
  1486. public function getCategoryIdsByCategoryText($categoryText) {
  1487. $catagories = array_filter(explode(',', $categoryText));
  1488. $catagoryIds = array();
  1489. foreach ($catagories as $category) {
  1490. $categoryCollection = Mage::getResourceModel('catalog/category_collection')
  1491. ->addAttributeToSelect('entity_id')
  1492. ->addFieldToFilter('name', $category);
  1493. foreach ($categoryCollection as $value)
  1494. $catagoryIds[] = $value->getId();
  1495. }
  1496. return $catagoryIds;
  1497. }
  1498.  
  1499. /**
  1500. * getAllAttributesCodes Return comma seperated attribute codes for mass upload
  1501. * @var Array $allowedSets marketplace allowed attribute sets
  1502. * @var Array $attributeCodes Containes attribute set codes
  1503. * @var Object $customAttrubutes marketplace custom attribute model object
  1504. * @var Array $temp Containes attributes of 1 attribute set at a time
  1505. * @var Object $attribute Eav attribute model object
  1506. * @return JSON Attribute codes
  1507. */
  1508. public function getAllAttributesCodes() {
  1509. $allowedSets = explode(',',Mage::getStoreConfig('marketplace/marketplace_options/attributesetid'));
  1510. $attributeCodes = array();
  1511. foreach ($allowedSets as $attributeSetId) {
  1512. $customAttrubutes = Mage::getModel('customattribute/customattribute')->getFrontShowAttributes($attributeSetId);
  1513. $attributeCodes[$attributeSetId] = array();
  1514. $temp = array();
  1515. foreach($customAttrubutes as $customAttrubute) {
  1516. $attribute = Mage::getModel('catalog/resource_eav_attribute')->load($customAttrubute['attribute_id']);
  1517. array_push($temp, $attribute['attribute_code']);
  1518. }
  1519. $attributeCodes[$attributeSetId] = implode(",",$temp);
  1520. }
  1521. return json_encode($attributeCodes);
  1522. }
  1523.  
  1524. /**
  1525. * getConfigurableSuperAttributes This function is used for getting the configurable attribute codes
  1526. * @var Array $allowedSets Contains allowed attribute sets ids for Marketplace Vendor
  1527. * @var String $attributeCodes Contains configurable attribute codes corrosponding to attribute set
  1528. * @var Array $attributes Contains Attribute ids
  1529. * @return JSON $attributeCodes Containes configurable attributes
  1530. */
  1531. public function getConfigurableSuperAttributes() {
  1532. $allowedSets = explode(',',Mage::getStoreConfig('marketplace/marketplace_options/attributesetid'));
  1533. $attributeCodes = array();
  1534. foreach ($allowedSets as $attributeSetId) {
  1535. $attributes = Mage::getModel('catalog/product_attribute_api')->items($attributeSetId);
  1536. $attributeCodes[$attributeSetId] = array();
  1537. $temp = array();
  1538. foreach($attributes as $attribute) {
  1539. $data = Mage::getModel('catalog/resource_eav_attribute')->load($attribute['attribute_id']);
  1540. if($data['frontend_input'] == 'select' && $data['is_user_defined'] == 1 && $data['is_configurable'] == 1&& $data['is_visible'] == 1 && $data['is_global'] == 1) {
  1541. array_push($temp, $data['attribute_code']);
  1542. }
  1543. }
  1544. $attributeCodes[$attributeSetId] = implode(",",$temp);
  1545. }
  1546. return json_encode($attributeCodes);
  1547. }
  1548.  
  1549. /**
  1550. * getCsvProductType This function check the csv file product type
  1551. * @param Array $data CSV file header data
  1552. * @return String Return product type
  1553. */
  1554. public function getCsvProductType($data) {
  1555. if(in_array('weight', $data)) {
  1556. return "simple";
  1557. } else {
  1558. if(in_array('link_file', $data)) {
  1559. return "downloadable";
  1560. } elseif(in_array('_super_attribute_code', $data)) {
  1561. return "configurable";
  1562. } else {
  1563. return "virtual";
  1564. }
  1565. }
  1566. return "";
  1567. }
  1568. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement