Example #1
0
 /**
  * Imports objects based on a specified CSV file in $_GET['FileName']
  */
 function import()
 {
     $FileName = $_GET['FileName'];
     $FileName = $_SERVER['DOCUMENT_ROOT'] . substr($_SERVER['PHP_SELF'], 0, strlen($_SERVER['PHP_SELF']) - 18) . "/assets/" . $FileName;
     if (file_exists($FileName)) {
         $handle = fopen($FileName, 'r');
         if ($handle) {
             while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
                 $num = count($data);
                 $row++;
                 if ($row == 1) {
                     for ($c = 0; $c < $num; $c++) {
                         $ColumnHeaders[] = str_replace(' ', '', $data[$c]);
                         // Have to add code here to remove unsafe chars..
                     }
                 } else {
                     $Product = new Product();
                     for ($c = 0; $c < $num; $c++) {
                         $Product->{$ColumnHeaders}[$c] = trim($data[$c]);
                     }
                     $MainCategory = DataObject::get("ProductGroup", "URLSegment LIKE '" . $Product->generateURLSegment($Product->Category) . "'");
                     if (!$MainCategory) {
                         // if we can't find a main category, create all three sub
                         // categories, as they must be unique.
                         $ProductGroup = new ProductGroup();
                         $ProductGroup->Title = $Product->Category;
                         print_r("<ul><li>Created : {$ProductGroup->Title}</li>");
                         $ProductGroup->ParentID = 1;
                         $index = $ProductGroup->write();
                         $ProductGroup->flushCache();
                         if ($Product->SubCategory) {
                             $ChildProductGroup = new ProductGroup();
                             $ChildProductGroup->Title = $Product->SubCategory;
                             print_r("<ul><li>Created : {$ChildProductGroup->Title}</li>");
                             $ChildProductGroup->ClassName = "ProductGroup";
                             $ChildProductGroup->ParentID = $index;
                             $index = $ChildProductGroup->write();
                             $ChildProductGroup->flushCache();
                         }
                         if ($Product->SubCategory2) {
                             $NestedProductGroup = new ProductGroup();
                             $NestedProductGroup->Title = $Product->SubCategory2;
                             print_r("<ul><li>Created : {$NestedProductGroup->Title}</li>");
                             $NestedProductGroup->ClassName = "ProductGroup";
                             $NestedProductGroup->ParentID = $index;
                             $index = $NestedProductGroup->write();
                             $NestedProductGroup->flushCache();
                         }
                     } else {
                         // We've  found a main category. check if theres a second...
                         print_r("<ul><li>USING : {$MainCategory->Title}</li>");
                         $index = $MainCategory->ID;
                         $SubCategory = DataObject::get_one("ProductGroup", "URLSegment LIKE '" . $Product->generateURLSegment($Product->SubCategory) . "'");
                         if (!$SubCategory && $Product->SubCategory) {
                             $ChildProductGroup = new ProductGroup();
                             $ChildProductGroup->Title = $Product->SubCategory;
                             print_r("<ul><li>Created : {$ChildProductGroup->Title}</li>");
                             $ChildProductGroup->ClassName = "ProductGroup";
                             $ChildProductGroup->ParentID = $index;
                             $index = $ChildProductGroup->write();
                             $ChildProductGroup->flushCache();
                             if ($Product->SubCategory2) {
                                 $NestedProductGroup = new ProductGroup();
                                 $NestedProductGroup->Title = $Product->SubCategory2;
                                 print_r("<ul><li>{$NestedProductGroup->Title}</li>");
                                 $NestedProductGroup->ClassName = "ProductGroup";
                                 $NestedProductGroup->ParentID = $index;
                                 $index = $NestedProductGroup->write();
                                 $NestedProductGroup->flushCache();
                                 $index = $SubCategory2->ID;
                             }
                         } else {
                             if ($Product->SubCategory) {
                                 print_r("<ul><li>USING : {$SubCategory->Title}</li>");
                                 $index = $SubCategory->ID;
                                 $SubCategory2 = DataObject::get_one("ProductGroup", "URLSegment LIKE '" . $Product->generateURLSegment($Product->SubCategory2) . "'");
                                 if ($Product->SubCategory2) {
                                     $NestedProductGroup = new ProductGroup();
                                     $NestedProductGroup->Title = $Product->SubCategory2;
                                     print_r("<ul><li>{$NestedProductGroup->Title}</li>");
                                     $NestedProductGroup->ClassName = "ProductGroup";
                                     $NestedProductGroup->ParentID = $index;
                                     $index = $NestedProductGroup->write();
                                     $NestedProductGroup->flushCache();
                                     $index = $SubCategory2->ID;
                                 }
                             }
                         }
                     }
                     $MatchedProduct = DataObject::get_one("Product", "URLSegment LIKE '" . $Product->generateURLSegment($Product->Title) . "'");
                     if ($MatchedProduct) {
                         // create the new parents / assign many many
                         $MatchedProduct->ParentID = $index;
                         // create the new product
                         $MatchedProduct->write();
                         $MatchedProduct->flushCache();
                         print_r(" <h4>UPDATED</h4></ul></ul></ul><br/><br/>");
                     } else {
                         // save the new product
                         $Product->ParentID = $index;
                         $Product->write();
                         $Product->flushCache();
                         print_r(" <h4>New Product {$product->Title}</h4></ul></ul></ul><br/><br/>");
                     }
                 }
             }
             fclose($handle);
         } else {
             print_r("<h1>Error: Could not open file.</h1>");
         }
     } else {
         print_r("<h1>Error: Could not open file.</h1>");
     }
 }