/**
  * Returns the ID of the ShopCategory with the given name and
  * parent ID, if present.
  *
  * If the ShopCategory cannot be found, a new ShopCategory
  * with the given name is inserted and its ID returned.
  * @static
  * @param   string      $catName    The ShopCategory name
  * @param   integer     $catParent  The optional parent ShopCategory ID,
  *                                  or null to ignore it (default)
  * @return  integer                 The ID of the ShopCategory,
  *                                  or 0 on failure.
  * @author  Unknown <*****@*****.**> (Original author)
  * @author  Reto Kohli <*****@*****.**> (Made static)
  */
 static function getCategoryId($catName, $catParent = null)
 {
     $objCategory = ShopCategories::getChildNamed($catName, $catParent);
     if ($objCategory) {
         return $objCategory->id();
     }
     return CsvImport::InsertNewCat($catName, $catParent);
 }
Exemple #2
0
 /**
  * Apply the flags to all Products matching the given Product code
  *
  * Any Product and ShopCategory carrying one or more of the names
  * of any ShopCategory marked as "__VIRTUAL__" is cloned and added
  * to that category.  Those having any such flags removed are deleted
  * from the respective category.  Identical copies of the same Products
  * are recognized by their "product_id" (the Product code).
  *
  * Note that in this current version, only the flags of Products are
  * tested and applied.  Products are cloned and added together with
  * their immediate parent ShopCategories (aka "Article").
  *
  * Thus, all Products within the same "Article" ShopCategory carry the
  * same flags, as does the containing ShopCategory itself.
  * @param   integer     $productCode  The Product code (*NOT* the ID).
  *                                    This must be non-empty!
  * @param   string      $strNewFlags  The new flags for the Product
  * @static
  * @author      Reto Kohli <*****@*****.**>
  */
 static function changeFlagsByProductCode($productCode, $strNewFlags)
 {
     if (empty($productCode)) {
         return false;
     }
     // Get all available flags.  These are represented by the names
     // of virtual root ShopCategories.
     $arrVirtual = ShopCategories::getVirtualCategoryNameArray();
     // Get the affected identical Products
     $arrProduct = Products::getByCustomId($productCode);
     // No way we can do anything useful without them.
     if (count($arrProduct) == 0) {
         return false;
     }
     // Get the Product flags.  As they're all the same, we'll use the
     // first one here.
     // Note that this object is used for reference only and is never stored.
     // Its database entry will be updated along the way, however.
     $_objProduct = $arrProduct[0];
     $strOldFlags = $_objProduct->getFlags();
     // Flag indicating whether the article has been cloned already
     // for all new flags set.
     $flagCloned = false;
     // Now apply the changes to all those identical Products, their parent
     // ShopCategories, and all sibling Products within them.
     foreach ($arrProduct as $objProduct) {
         // Get the containing article ShopCategory.
         $category_id = $objProduct->category_id();
         $objArticleCategory = ShopCategory::getById($category_id);
         if (!$objArticleCategory) {
             continue;
         }
         // Get parent (subgroup)
         $objSubGroupCategory = ShopCategory::getById($objArticleCategory->parent_id());
         // This should not happen!
         if (!$objSubGroupCategory) {
             continue;
         }
         $subgroupName = $objSubGroupCategory->name();
         // Get grandparent (group, root ShopCategory)
         $objRootCategory = ShopCategory::getById($objSubGroupCategory->parent_id());
         if (!$objRootCategory) {
             continue;
         }
         // Apply the new flags to all Products and Article ShopCategories.
         // Update the flags of the original Article ShopCategory first
         $objArticleCategory->flags($strNewFlags);
         $objArticleCategory->store();
         // Get all sibling Products affected by the same flags
         $arrSiblingProducts = Products::getByShopCategory($objArticleCategory->id());
         // Set the new flag set for all Products within the Article
         // ShopCategory.
         foreach ($arrSiblingProducts as $objProduct) {
             $objProduct->flags($strNewFlags);
             $objProduct->store();
         }
         // Check whether this group is affected by the changes.
         // If its name matches one of the flags, the Article and subgroup
         // may have to be removed.
         $strFlag = $objRootCategory->name();
         if (preg_match("/{$strFlag}/", $strNewFlags)) {
             // The flag is still there, don't bother.
             continue;
         }
         // Also check whether this is a virtual root ShopCategory.
         if (in_array($strFlag, $arrVirtual)) {
             // It is one of the virtual roots, and the flag is missing.
             // So the Article has to be removed from this group.
             $objArticleCategory->delete();
             $objArticleCategory = false;
             // And if the subgroup happens to contain no more
             // "Article", delete it as well.
             $arrChildren = $objSubGroupCategory->getChildrenIdArray();
             if (count($arrChildren) == 0) {
                 $objSubGroupCategory->delete();
             }
             continue;
         }
         // Here, the virtual ShopCategory groups have been processed,
         // the only ones left are the "normal" ShopCategories.
         // Clone one of the Article ShopCategories for each of the
         // new flags set.
         // Already did that?
         if ($flagCloned) {
             continue;
         }
         // Find out what flags have been added.
         foreach ($arrVirtual as $strFlag) {
             // That flag is not present in the new flag set.
             if (!preg_match("/{$strFlag}/", $strNewFlags)) {
                 continue;
             }
             // But it has been before.  The respective branch has
             // been truncated above already.
             if (preg_match("/{$strFlag}/", $strOldFlags)) {
                 continue;
             }
             // That is a new flag for which we have to clone the Article.
             // Get the affected grandparent (group, root ShopCategory)
             $objTargetRootCategory = ShopCategories::getChildNamed($strFlag, 0, false);
             if (!$objTargetRootCategory) {
                 continue;
             }
             // Check whether the subgroup exists already
             $objTargetSubGroupCategory = ShopCategories::getChildNamed($subgroupName, $objTargetRootCategory->id(), false);
             if (!$objTargetSubGroupCategory) {
                 // Nope, add the subgroup.
                 $objSubGroupCategory->makeClone();
                 $objSubGroupCategory->parent_id($objTargetRootCategory->id());
                 $objSubGroupCategory->store();
                 $objTargetSubGroupCategory = $objSubGroupCategory;
             }
             // Check whether the Article ShopCategory exists already
             $objTargetArticleCategory = ShopCategories::getChildNamed($objArticleCategory->name(), $objTargetSubGroupCategory->id(), false);
             if ($objTargetArticleCategory) {
                 // The Article Category already exists.
             } else {
                 // Nope, clone the "Article" ShopCategory and add it to the
                 // subgroup.  Note that the flags have been set already
                 // and don't need to be changed again here.
                 // Also note that the cloning process includes all content
                 // of the Article ShopCategory, but the flags will remain
                 // unchanged. That's why the flags have already been
                 // changed right at the beginning of the process.
                 $objArticleCategory->makeClone(true, true);
                 $objArticleCategory->parent_id($objTargetSubGroupCategory->id());
                 $objArticleCategory->store();
                 $objTargetArticleCategory = $objArticleCategory;
             }
         }
         // foreach $arrVirtual
     }
     // foreach $arrProduct
     // And we're done!
     return true;
 }