/** * Recursively builds the $arrCategory array as returned by * {@link ShopCategories::getTreeArray()}. * * See {@link buildTreeArray()} for details. * @param boolean $flagFull If true, the full tree is built, * only the parts visible for * $selected_id otherwise. * Defaults to false. * @param boolean $active Only return ShopCategories * with active == true if true. * Defaults to true. * @param boolean $flagVirtual If true, also returns the virtual * content of ShopCategories marked * as virtual. Defaults to false. * @param integer $selected_id The optional selected ShopCategory * ID. If set and greater than zero, * only the ShopCategories needed * to display the Shop page are * returned. * @param integer $parent_id The optional root ShopCategories ID. * Defaults to 0 (zero). * @param integer $maxlevel The optional maximum nesting level. * 0 (zero) means all. * Defaults to 0 (zero). * @param integer $level The optional nesting level, * initially 0. * Defaults to 0 (zero). * @return boolean True on success, false otherwise. * @static * @author Reto Kohli <*****@*****.**> */ static function buildTreeArrayRecursive($flagFull = false, $active = true, $flagVirtual = true, $selected_id = 0, $parent_id = 0, $maxlevel = 0, $level = 0) { // Get the ShopCategories's children $arrCategory = ShopCategories::getChildCategoriesById($parent_id, $active, $flagVirtual); // Has there been an error? if ($arrCategory === false) { return false; } foreach ($arrCategory as $objCategory) { $id = $objCategory->id(); // $index = count(self::$arrCategory); // self::$arrCategory[$index] = array( self::$arrCategory[$id] = array('id' => $id, 'name' => $objCategory->name(), 'description' => $objCategory->description(), 'parent_id' => $objCategory->parent_id(), 'ord' => $objCategory->ord(), 'active' => $objCategory->active(), 'picture' => $objCategory->picture(), 'flags' => $objCategory->flags(), 'virtual' => $objCategory->virtual(), 'level' => $level); // self::$arrCategoryIndex[$id] = $index; // Get the grandchildren if // - the maximum depth has not been exceeded and // - the full list has been requested, or the current ShopCategory // is an ancestor of the selected one or the selected itself. if (($maxlevel == 0 || $level < $maxlevel) && ($flagFull || in_array($id, self::$arrTrail)) && (!$objCategory->virtual() || $flagVirtual)) { self::buildTreeArrayRecursive($flagFull, $active, $flagVirtual, $selected_id, $id, $maxlevel, $level + 1); } } return true; }
/** * Set up the subcategories block in the current shop page. * @param integer $parent_id The optional parent ShopCategory ID, * defaults to 0 (zero). * @return boolean True on success, false otherwise * @global array */ static function showCategories($parent_id = 0) { if ($parent_id) { $objCategory = ShopCategory::getById($parent_id); // If we can't get this ShopCategory, it most probably does // not exist. if (!$objCategory) { // Retry using the root. $parent_id = 0; } else { // Show the parent ShopCategory's image, if available $imageName = $objCategory->picture(); if ($imageName) { self::$objTemplate->setVariable(array('SHOP_CATEGORY_CURRENT_IMAGE' => \Cx\Core\Core\Controller\Cx::instanciate()->getWebsiteImagesShopWebPath() . '/' . $imageName, 'SHOP_CATEGORY_CURRENT_IMAGE_ALT' => $objCategory->name())); } } } // Get all active child categories with parent ID $parent_id $arrShopCategory = ShopCategories::getChildCategoriesById($parent_id, true); if (!is_array($arrShopCategory)) { return false; } $cell = 0; $arrDefaultImageSize = false; // For all child categories do... foreach ($arrShopCategory as $objCategory) { $id = $objCategory->id(); $catName = $objCategory->name(); $imageName = $objCategory->picture(); $thumbnailPath = self::$defaultImage; $description = $objCategory->description(); $description = nl2br(htmlentities($description, ENT_QUOTES, CONTREXX_CHARSET)); $description = preg_replace('/[\\n\\r]/', '', $description); if (empty($arrDefaultImageSize)) { //\DBG::log("Shop::showCategories(): ".\Cx\Core\Core\Controller\Cx::instanciate()->getWebsitePath() . self::$defaultImage); $arrDefaultImageSize = getimagesize(\Cx\Core\Core\Controller\Cx::instanciate()->getWebsitePath() . self::$defaultImage); self::scaleImageSizeToThumbnail($arrDefaultImageSize); } $arrSize = $arrDefaultImageSize; if (!$imageName) { // Look for a picture in the Products. $imageName = Products::getPictureByCategoryId($id); } if (!$imageName) { // Look for a picture in the subcategories and their Products. $imageName = ShopCategories::getPictureById($id); } if ($imageName) { $thumb_name = \ImageManager::getThumbnailFilename($imageName); if (file_exists(\Cx\Core\Core\Controller\Cx::instanciate()->getWebsiteImagesShopPath() . '/' . $thumb_name)) { // Image found! Use that instead of the default. $thumbnailPath = \Cx\Core\Core\Controller\Cx::instanciate()->getWebsiteImagesShopWebPath() . '/' . $thumb_name; $arrSize = getimagesize(\Cx\Core\Core\Controller\Cx::instanciate()->getWebsitePath() . $thumbnailPath); self::scaleImageSizeToThumbnail($arrSize); } } if ($imageName) { self::$objTemplate->setVariable('SHOP_CATEGORY_IMAGE', contrexx_raw2encodedUrl(\Cx\Core\Core\Controller\Cx::instanciate()->getWebsiteImagesShopWebPath() . '/' . $imageName)); } self::$objTemplate->setVariable(array('SHOP_CATEGORY_ID' => $id, 'SHOP_CATEGORY_NAME' => htmlentities($catName, ENT_QUOTES, CONTREXX_CHARSET), 'SHOP_CATEGORY_THUMBNAIL' => contrexx_raw2encodedUrl($thumbnailPath), 'SHOP_CATEGORY_THUMBNAIL_SIZE' => $arrSize[3], 'SHOP_CATEGORY_DESCRIPTION' => $description)); // Add flag images for flagged ShopCategories $strImage = ''; $arrVirtual = ShopCategories::getVirtualCategoryNameArray(); foreach ($arrVirtual as $strFlag) { if ($objCategory->testFlag($strFlag)) { $strImage .= '<img src="images/content/' . $strFlag . '.jpg" alt="' . $strFlag . '" />'; } } if ($strImage) { self::$objTemplate->setVariable('SHOP_CATEGORY_FLAG_IMAGE', $strImage); } if (self::$objTemplate->blockExists('subCategories')) { self::$objTemplate->parse('subCategories'); if (++$cell % 4 == 0) { self::$objTemplate->parse('subCategoriesRow'); } } } return true; }
/** * Returns an array of this ShopCategory's children from the database. * @param boolean $active Only return ShopCategories with * active==1 if true. Defaults to false. * @return mixed An array of ShopCategory objects * on success, false otherwise * @author Reto Kohli <*****@*****.**> */ function getChildCategories($active = false) { if ($this->id <= 0) { return false; } return ShopCategories::getChildCategoriesById($this->id, $active); }
/** * Delete Products from the ShopCategory given by its ID. * * If deleting one of the Products fails, aborts and returns false * immediately without trying to delete the remaining Products. * Deleting the ShopCategory after this method failed will most * likely result in Product bodies in the database! * @param integer $category_id The ShopCategory ID * @param boolean $flagDeleteImages Delete images, if true * @param boolean $recursive Delete Products from * subcategories, if true * @return boolean True on success, null on noop, * false otherwise * @static * @author Reto Kohli <*****@*****.**> */ static function deleteByShopCategory($category_id, $flagDeleteImages = false, $recursive = false) { // Verify that the Category still exists $objShopCategory = ShopCategory::getById($category_id); if (!$objShopCategory) { //\DBG::log("Products::deleteByShopCategory($category_id, $flagDeleteImages): Info: Category ID $category_id does not exist"); return null; } $arrProductId = Products::getIdArrayByShopCategory($category_id); if (!is_array($arrProductId)) { //\DBG::log("Products::deleteByShopCategory($category_id, $flagDeleteImages): Failed to get Product IDs in that Category"); return false; } // Look whether this is within a virtual ShopCategory $virtualContainer = ''; $parent_id = $category_id; do { $objShopCategory = ShopCategory::getById($parent_id); if (!$objShopCategory) { //\DBG::log("Products::deleteByShopCategory($category_id, $flagDeleteImages): Failed to get parent Category"); return false; } if ($objShopCategory->virtual()) { // The name of any virtual ShopCategory is used to mark // Products within $virtualContainer = $objShopCategory->name(); break; } $parent_id = $objShopCategory->parent_id(); } while ($parent_id != 0); // Remove the Products in one way or another foreach ($arrProductId as $product_id) { $objProduct = Product::getById($product_id); if (!$objProduct) { //\DBG::log("Products::deleteByShopCategory($category_id, $flagDeleteImages): Failed to get Product IDs $product_id"); return false; } if ($virtualContainer != '' && $objProduct->flags() != '') { // Virtual ShopCategories and their content depends on // the Product objects' flags. foreach ($arrProductId as $objProduct) { $objProduct->removeFlag($virtualContainer); if (!Products::changeFlagsByProductCode($objProduct->code(), $objProduct->flags())) { //\DBG::log("Products::deleteByShopCategory($category_id, $flagDeleteImages): Failed to update Product flags for ID ".$objProduct->id()); return false; } } } else { // Normal, non-virtual ShopCategory. // Remove Products having the same Product code. // Don't delete Products having more than one Category assigned. // Instead, remove them from the chosen Category only. $arrCategoryId = array_flip(preg_split('/\\s*,\\s*/', $objProduct->category_id(), null, PREG_SPLIT_NO_EMPTY)); if (count($arrCategoryId) > 1) { unset($arrCategoryId[$category_id]); $objProduct->category_id(join(',', array_keys($arrCategoryId))); if (!$objProduct->store()) { return false; } } else { // if (!Products::deleteByCode( // $objProduct->getCode(), // $flagDeleteImages) // ) return false; if (!$objProduct->delete()) { return false; } } } } if ($recursive) { $arrCategoryId = ShopCategories::getChildCategoriesById($category_id); foreach ($arrCategoryId as $category_id) { if (!self::deleteByShopCategory($category_id, $flagDeleteImages, $recursive)) { \DBG::log("ERROR: Failed to delete Products in Category ID {$category_id}"); return false; } } } return true; }
/** * Edit a pricelist * @global ADOConnection $objDatabase * @global array $_ARRAYLANG * @return boolean True on success, false otherwise */ static function view_pricelist_edit() { global $_ARRAYLANG; $list_id = null; $objList = PriceList::getFromPost(); if ($objList) { $result = $objList->store(); if ($result) { if (isset($_REQUEST['list_id'])) { unset($_REQUEST['list_id']); } //die("Showing lists"); return self::view_pricelists(); } } $list_id = isset($_GET['list_id']) ? $_GET['list_id'] : null; $objList = PriceList::getById($list_id); if (!$objList) { $objList = new PriceList(null); } $list_id = $objList->id(); self::$objTemplate->loadTemplateFile("module_shop_pricelist_details.html"); self::$objTemplate->setGlobalVariable($_ARRAYLANG); self::$objTemplate->setVariable(array('SHOP_PRICELIST_EDIT' => $_ARRAYLANG[$list_id ? 'TXT_SHOP_PRICELIST_EDIT' : 'TXT_SHOP_PRICELIST_ADD'], 'SHOP_PRICELIST_ID' => $list_id, 'SHOP_PRICELIST_LINK_PDF' => $list_id ? PriceList::getUrl($list_id) : '', 'SHOP_PRICELIST_NAME' => $objList->name(), 'SHOP_PRICELIST_LANGUAGE_MENUOPTIONS' => \Html::getOptions(\FWLanguage::getNameArray(), $objList->lang_id()), 'SHOP_PRICELIST_BORDER_CHECKED' => $objList->border() ? \Html::ATTRIBUTE_CHECKED : '', 'SHOP_PRICELIST_HEADER_CHECKED' => $objList->header() ? \Html::ATTRIBUTE_CHECKED : '', 'SHOP_PRICELIST_HEADER_LEFT' => $objList->header_left(), 'SHOP_PRICELIST_HEADER_RIGHT' => $objList->header_right(), 'SHOP_PRICELIST_FOOTER_CHECKED' => $objList->footer() ? \Html::ATTRIBUTE_CHECKED : '', 'SHOP_PRICELIST_FOOTER_LEFT' => $objList->footer_left(), 'SHOP_PRICELIST_FOOTER_RIGHT' => $objList->footer_right())); $category_ids = $objList->category_ids(); $category_all = false; if (empty($category_ids) || $category_ids == '*') { $category_all = true; self::$objTemplate->setVariable('SHOP_PRICELIST_CATEGORY_ALL_CHECKED', \Html::ATTRIBUTE_CHECKED); } $arrCategories = ShopCategories::getChildCategoriesById(0, false); if (empty($arrCategories)) { Message::warning($_ARRAYLANG['TXT_SHOP_WARNING_NO_CATEGORIES']); } $i = 0; foreach ($arrCategories as $objCategory) { $category_id = $objCategory->id(); $selected = $category_all || preg_match('/(?:^|,)\\s*' . $category_id . '\\s*(?:,|$)/', $category_ids); //DBG::log("Category ID $category_id, ".($selected ? "selected" : "NOT")); self::$objTemplate->setVariable(array('SHOP_CATEGORY_ID' => $category_id, 'SHOP_CATEGORY_NAME' => $objCategory->name(), 'SHOP_CATEGORY_DISABLED' => $category_all ? \Html::ATTRIBUTE_DISABLED : '', 'SHOP_CATEGORY_CHECKED' => $selected ? \Html::ATTRIBUTE_CHECKED : '', 'SHOP_CATEGORY_ROWCLASS' => 'row' . (++$i % 2 + 1))); self::$objTemplate->parse('shop_category'); } return true; }