public function processRecord($record, $columnMap, &$results, $preview = false) { // Get Current Object $objID = parent::processRecord($record, $columnMap, $results, $preview); $object = DataObject::get_by_id($this->objectClass, $objID); $this->extend("onBeforeProcess", $object, $record, $columnMap, $results, $preview); // Loop through all fields and setup associations foreach ($record as $key => $value) { // Find any categories (denoted by a 'CategoryXX' column) if (strpos($key, 'Category') !== false) { $category = CatalogueCategory::get()->filter("Title", $value)->first(); if ($category) { $object->Categories()->add($category); } } // Find any Images (denoted by a 'ImageXX' column) if (strpos($key, 'Image') !== false && $key != "Images") { $image = Image::get()->filter("Name", $value)->first(); if ($image) { $object->Images()->add($image); } } // Find any related products (denoted by a 'RelatedXX' column) if (strpos($key, 'Related') !== false && $key != "RelatedProducts") { $product = Product::get()->filter("StockID", $value)->first(); if ($product) { $object->RelatedProducts()->add($product); } } } $this->extend("onAfterProcess", $object, $record, $columnMap, $results, $preview); $object->destroy(); unset($object); return $objID; }
/** * Returns a fixed navigation menu of the given level. * @return SS_List */ public function CategoryMenu($level = 1) { if ($level == 1) { $result = CatalogueCategory::get()->filter(array("ParentID" => 0)); } else { $parent = $this->data(); $stack = array($parent); if ($parent) { while ($parent = $parent->Parent) { array_unshift($stack, $parent); } } if (isset($stack[$level - 2])) { $result = $stack[$level - 2]->Children(); } } $visible = array(); if (isset($result)) { foreach ($result as $item) { if ($item->canView()) { $visible[] = $item; } } } return new ArrayList($visible); }
public function run($request) { $products = 0; $categories = 0; // First load all products $items = CatalogueProduct::get(); foreach ($items as $item) { // Just write product, on before write should deal with the rest $item->write(); $products++; } // Then all categories $items = CatalogueCategory::get(); foreach ($items as $item) { // Just write category, on before write should deal with the rest $item->write(); $categories++; } echo "Wrote {$products} products and {$categories} categories.\n"; }
/** * Check catalogue URL's before we get to the CMS (if it exists) * * @param SS_HTTPRequest $request * @param DataModel|null $model * @return SS_HTTPResponse */ public function handleRequest(SS_HTTPRequest $request, DataModel $model) { $this->request = $request; $this->setDataModel($model); $this->pushCurrent(); // Create a response just in case init() decides to redirect $this->response = new SS_HTTPResponse(); $this->init(); // If we had a redirection or something, halt processing. if ($this->response->isFinished()) { $this->popCurrent(); return $this->response; } // If DB is not present, build if (!DB::isActive() || !ClassInfo::hasTable('CatalogueProduct') || !ClassInfo::hasTable('CatalogueCategory')) { return $this->response->redirect(Director::absoluteBaseURL() . 'dev/build?returnURL=' . (isset($_GET['url']) ? urlencode($_GET['url']) : null)); } $urlsegment = $request->param('URLSegment'); $this->extend('onBeforeInit'); $this->init(); $this->extend('onAfterInit'); // Find link, regardless of current locale settings if (class_exists('Translatable')) { Translatable::disable_locale_filter(); } $filter = array('URLSegment' => $urlsegment, 'Disabled' => 0); if ($object = CatalogueProduct::get()->filter($filter)->first()) { $controller = $this->controller_for($object); } elseif ($object = CatalogueCategory::get()->filter($filter)->first()) { $controller = $this->controller_for($object); } elseif (class_exists('ModelAsController')) { // If CMS installed $controller = ModelAsController::create(); } else { $controller = Controller::create(); } if (class_exists('Translatable')) { Translatable::enable_locale_filter(); } $result = $controller->handleRequest($request, $model); $this->popCurrent(); return $result; }
public function requireDefaultRecords() { parent::requireDefaultRecords(); // Alter any existing recods that might have the wrong classname foreach (CatalogueCategory::get()->filter("ClassName", "CatalogueCategory") as $category) { $category->ClassName = "Category"; $category->write(); } }
/** * Gets a list of all Categories, either top level (default) or * from a sub level * * @param Parent the ID of a parent cetegory * @return SS_List */ public function Categories($ParentID = 0) { return CatalogueCategory::get()->filter(array("ParentID" => $ParentID, "Disabled" => 0)); }