Exemple #1
0
 public function testgetCategory()
 {
     $c = \XLite\Core\Database::getRepo('XLite\\Model\\Category')->findOneBy(array('cleanURL' => 'fruit'));
     $p = \XLite\Core\Database::getRepo('XLite\\Model\\Product')->findOneBy(array('sku' => '00007'));
     $this->assertNotNull($p, 'check product');
     $cp = new \XLite\Model\CategoryProducts();
     $cp->setCategory($c);
     $cp->setProduct($p);
     $em = \XLite\Core\Database::getEM();
     $em->persist($cp);
     $em->flush();
     $em->clear();
     $p = \XLite\Core\Database::getRepo('XLite\\Model\\Product')->findOneBy(array('sku' => '00007'));
     $this->assertNotNull($p, 'check product #2');
     $cp = $p->getCategory(14015);
     $this->assertTrue($cp instanceof \XLite\Model\Category, 'check class');
     $this->assertEquals(14015, $cp->getCategoryId(), 'check category id');
     $cp = $p->getCategory(999999999);
     $this->assertTrue($cp instanceof \XLite\Model\Category, 'check class #2');
     $this->assertTrue(is_null($cp->getCategoryId()), 'check category is null');
     $cp = $p->getCategory();
     $this->assertTrue($cp instanceof \XLite\Model\Category, 'check class #3');
     $this->assertEquals(14015, $cp->getCategoryId(), 'check category id #3');
     $em->merge($cp);
     $em->remove($cp);
     $em->flush();
 }
Exemple #2
0
 /**
  * Import 'categories' value
  *
  * @param \XLite\Model\Product $model  Product
  * @param mixed                $value  Value
  * @param array                $column Column info
  *
  * @return void
  */
 protected function importCategoriesColumn(\XLite\Model\Product $model, $value, array $column)
 {
     $position = array();
     foreach ($model->getCategoryProducts() as $link) {
         $position[$link->getCategory()->getCategoryId()] = $link->getOrderby();
     }
     \XLite\Core\Database::getRepo('\\XLite\\Model\\CategoryProducts')->deleteInBatch($model->getCategoryProducts()->toArray());
     $model->getCategoryProducts()->clear();
     foreach (array_unique($value) as $path) {
         $category = $this->addCategoryByPath($path);
         $link = \XLite\Core\Database::getRepo('\\XLite\\Model\\CategoryProducts')->findOneBy(array('category' => $category, 'product' => $model));
         if (!$link) {
             $link = new \XLite\Model\CategoryProducts();
             $link->setProduct($model);
             $link->setCategory($category);
             if (isset($position[$category->getCategoryId()])) {
                 $link->setOrderby($position[$category->getCategoryId()]);
             }
             $model->addCategoryProducts($link);
             \XLite\Core\Database::getEM()->persist($link);
         }
     }
 }
Exemple #3
0
 /**
  * Clone entity (categories)
  *
  * @param \XLite\Model\Product $newProduct New product
  *
  * @return void
  */
 protected function cloneEntityCategories(\XLite\Model\Product $newProduct)
 {
     foreach ($this->getCategories() as $category) {
         $link = new \XLite\Model\CategoryProducts();
         $link->setProduct($newProduct);
         $link->setCategory($category);
         $newProduct->addCategoryProducts($link);
         \XLite\Core\Database::getEM()->persist($link);
     }
 }
Exemple #4
0
 /**
  * Import categories 
  * 
  * @param \XLite\Model\Product $product Product
  * @param string               $data    Data
  *  
  * @return void
  */
 protected function importCategories(\XLite\Model\Product $product, $data)
 {
     $oldLinks = array();
     foreach ($product->getCategoryProducts() as $link) {
         $oldLinks[] = $link->getId();
     }
     if ($data) {
         $root = \XLite\Core\Database::getRepo('XLite\\Model\\Category')->find(\XLite\Core\Database::getRepo('XLite\\Model\\Category')->getRootCategoryId());
         foreach (explode(';', $data) as $path) {
             $path = trim($path);
             // Detect category
             $parent = $root;
             $category = null;
             foreach (explode('/', $path) as $name) {
                 $name = trim($name);
                 $category = null;
                 foreach ($parent->getChildren() as $cat) {
                     if ($cat->getName() == $name) {
                         $category = $cat;
                         break;
                     }
                 }
                 if (!$category) {
                     $category = \XLite\Core\Database::getRepo('\\XLite\\Model\\Category')->insert(array('parent_id' => $parent->getCategoryId(), 'name' => $name));
                     $parent->addChildren($category);
                 }
                 $parent = $category;
             }
             if ($category) {
                 // Add link to category
                 $link = null;
                 foreach ($product->getCategoryProducts() as $cp) {
                     if ($cp->getCategory()->getCategoryId() == $category->getCategoryId()) {
                         $link = $cp;
                         $key = array_search($link->getId(), $oldLinks);
                         unset($oldLinks[$key]);
                         break;
                     }
                 }
                 if (!$link) {
                     $link = new \XLite\Model\CategoryProducts();
                     $link->setProduct($product);
                     $link->setCategory($category);
                     $product->addCategoryProducts($link);
                     \XLite\Core\Database::getEM()->persist($link);
                 }
             }
         }
     }
     foreach ($product->getCategoryProducts() as $link) {
         if (in_array($link->getId(), $oldLinks)) {
             $product->getCategoryProducts()->removeElement($link);
             \XLite\Core\Database::getEM()->remove($link);
         }
     }
 }