/** * @param array $params array('old' => string, 'new' => string) * @see waEventHandler::execute() * @return void */ public function execute(&$params) { $model = new shopCategoryRoutesModel(); $model->updateByField(array('route' => $params['old']), array('route' => $params['new'])); $notification_model = new shopNotificationModel(); $notification_model->updateByField(array('source' => $params['old']), array('source' => $params['new'])); $followup_model = new shopFollowupModel(); $followup_model->updateByField(array('source' => $params['old']), array('source' => $params['new'])); wa('shop')->event(array('shop', 'update.route'), $params); }
/** * @return mixed * @throws waException */ protected function getCategory() { $category_model = $this->getModel(); $url_field = waRequest::param('url_type') == 1 ? 'url' : 'full_url'; if (waRequest::param('category_id')) { $category = $category_model->getById(waRequest::param('category_id')); if ($category) { $category_url = wa()->getRouteUrl('/frontend/category', array('category_url' => $category[$url_field])); if (urldecode(wa()->getConfig()->getRequestUrl(false, true)) !== $category_url) { $q = waRequest::server('QUERY_STRING'); $this->redirect($category_url . ($q ? '?' . $q : ''), 301); } } } else { $category = $category_model->getByField($url_field, waRequest::param('category_url')); if ($category && $category[$url_field] !== urldecode(waRequest::param('category_url'))) { $q = waRequest::server('QUERY_STRING'); $this->redirect(wa()->getRouteUrl('/frontend/category', array('category_url' => $category[$url_field])) . ($q ? '?' . $q : ''), 301); } } $route = wa()->getRouting()->getDomain(null, true) . '/' . wa()->getRouting()->getRoute('url'); if ($category) { $category_routes_model = new shopCategoryRoutesModel(); $routes = $category_routes_model->getRoutes($category['id']); } if (!$category || $routes && !in_array($route, $routes)) { throw new waException('Category not found', 404); } $category['subcategories'] = $category_model->getSubcategories($category, $route); $category_url = wa()->getRouteUrl('shop/frontend/category', array('category_url' => '%CATEGORY_URL%')); foreach ($category['subcategories'] as &$sc) { $sc['url'] = str_replace('%CATEGORY_URL%', waRequest::param('url_type') == 1 ? $sc['url'] : $sc['full_url'], $category_url); } unset($sc); // params $category_params_model = new shopCategoryParamsModel(); $category['params'] = $category_params_model->get($category['id']); // smarty description if ($this->getConfig()->getOption('can_use_smarty') && $category['description']) { $category['description'] = wa()->getView()->fetch('string:' . $category['description']); } return $category; }
private function getTree($parent_id = 0) { $category_model = self::getCategoryModel(); if ($parent_id) { $categories = $category_model->getTree($parent_id); } else { $categories = $category_model->getFullTree('id, left_key, right_key, parent_id, depth, name, count, type, status, include_sub_categories'); } // children_count is number of children of category foreach ($categories as &$item) { if (!isset($item['children_count'])) { $item['children_count'] = 0; } if (isset($categories[$item['parent_id']])) { $parent =& $categories[$item['parent_id']]; if (!isset($parent['children_count'])) { $parent['children_count'] = 0; } ++$parent['children_count']; unset($parent); } } unset($item); // bind storefronts (routes) $category_routes_model = new shopCategoryRoutesModel(); foreach ($category_routes_model->getRoutes(array_keys($categories)) as $category_id => $routes) { foreach ($routes as &$r) { if (substr($r, -1) === '*') { $r = substr($r, 0, -1); } if (substr($r, -1) === '/') { $r = substr($r, 0, -1); } } unset($r); $categories[$category_id]['routes'] = $routes; } // form intermediate utility data structure $stack = array(); $hierarchy = array(); foreach ($categories as $item) { $c = array('id' => $item['id'], 'total_count' => 0, 'parent_id' => $item['parent_id'], 'count' => $item['count'], 'depth' => $item['depth'], 'children' => array()); // Number of stack items $l = count($stack); // Check if we're dealing with different levels while ($l > 0 && $stack[$l - 1]['depth'] >= $item['depth']) { array_pop($stack); $l--; } // Stack is empty (we are inspecting the root) if ($l == 0) { // Assigning the root node $i = count($hierarchy); $hierarchy[$i] = $c; $stack[] =& $hierarchy[$i]; } else { // Add node to parent $i = count($stack[$l - 1]['children']); $stack[$l - 1]['children'][$i] = $c; $stack[] =& $stack[$l - 1]['children'][$i]; } } $hierarchy = array('id' => 0, 'count' => 0, 'total_count' => 0, 'children' => $hierarchy); $this->totalCount($hierarchy, $categories); return $categories; }
private function saveCategorySettings($id, &$data) { /** * @var shopCategoryModel */ $model = $this->getModel('category'); if (!$id) { if (empty($data['url'])) { $url = shopHelper::transliterate($data['name'], false); if ($url) { $data['url'] = $model->suggestUniqueUrl($url); } } if (empty($data['name'])) { $data['name'] = _w('(no-name)'); } $id = $model->add($data, $data['parent_id']); $this->logAction('category_add', $id); } else { $category = $model->getById($id); if (!$this->categorySettingsValidate($category, $data)) { return false; } if (empty($data['name'])) { $data['name'] = $category['name']; } if (empty($data['url'])) { $data['url'] = $model->suggestUniqueUrl(shopHelper::transliterate($data['name']), $id, $category['parent_id']); } unset($data['parent_id']); $data['edit_datetime'] = date('Y-m-d H:i:s'); $model->update($id, $data); $this->logAction('category_edit', $id); } if ($id) { if (waRequest::post('enable_sorting')) { $data['params']['enable_sorting'] = 1; } $category_params_model = new shopCategoryParamsModel(); $category_params_model->set($id, !empty($data['params']) ? $data['params'] : null); $category_routes_model = new shopCategoryRoutesModel(); $category_routes_model->setRoutes($id, isset($data['routes']) ? $data['routes'] : array()); $data['id'] = $id; /** * @event category_save * @param array $category * @return void */ wa()->event('category_save', $data); } return $id; }
private function getCategorySettings($id) { $category_model = new shopCategoryModel(); $category_params_model = new shopCategoryParamsModel(); $settings = $category_model->getById($id); if (!$settings) { return array(); } /** * @event backend_category_dialog * @param array $category * @return array[string][string] $return[%plugin_id%] html output for dialog */ $this->view->assign('event_dialog', wa()->event('backend_category_dialog', $settings)); $category_routes_model = new shopCategoryRoutesModel(); $settings['routes'] = $category_routes_model->getRoutes($id); $settings['frontend_urls'] = array(); foreach ($category_model->getFrontendUrls($id) as $frontend_url) { $pos = strrpos($frontend_url, $settings['url']); $settings['frontend_urls'][] = array('url' => $frontend_url, 'base' => $pos !== false ? rtrim(substr($frontend_url, 0, $pos), '/') . '/' : ''); } $settings['params'] = $category_params_model->get($id); if (isset($settings['params']['enable_sorting'])) { $settings['enable_sorting'] = 1; unset($settings['params']['enable_sorting']); } else { $settings['enable_sorting'] = 0; } $feature_model = new shopFeatureModel(); $selectable_and_boolean_features = $feature_model->select('*')->where("(selectable=1 OR type='boolean' OR type='double' OR type LIKE 'dimension\\.%' OR type LIKE 'range\\.%') AND parent_id IS NULL")->fetchAll('id'); if ($settings['type'] == shopCategoryModel::TYPE_DYNAMIC) { if ($settings['conditions']) { $settings['conditions'] = shopProductsCollection::parseConditions($settings['conditions']); } else { $settings['conditions'] = array(); } $tag_model = new shopTagModel(); $cloud = $tag_model->getCloud('name'); if (!empty($settings['conditions']['tag'][1])) { foreach ($settings['conditions']['tag'][1] as $tag_name) { $cloud[$tag_name]['checked'] = true; } } $settings['cloud'] = $cloud; // extract conditions for features foreach ($settings['conditions'] as $name => $value) { if (substr($name, -9) === '.value_id') { unset($settings['conditions'][$name]); $settings['conditions']['feature'][substr($name, 0, -9)] = $value; } } $settings['custom_conditions'] = $this->extractCustomConditions($settings['conditions']); $settings['features'] = $selectable_and_boolean_features; $settings['features'] = $feature_model->getValues($settings['features']); } $filter = $settings['filter'] !== null ? explode(',', $settings['filter']) : null; $feature_filter = array(); $features['price'] = array('id' => 'price', 'name' => 'Price'); $features += $selectable_and_boolean_features; if (!empty($filter)) { foreach ($filter as $feature_id) { $feature_id = trim($feature_id); if (isset($features[$feature_id])) { $feature_filter[$feature_id] = $features[$feature_id]; $feature_filter[$feature_id]['checked'] = true; unset($features[$feature_id]); } } } $settings['allow_filter'] = (bool) $filter; $settings['filter'] = $feature_filter + $features; return $settings; }
protected function prepareProduct(shopProduct $product) { if (waRequest::get('sku')) { $url_params = array('product_url' => $product['url']); if ($product['category_url']) { $url_params['category_url'] = $product['category_url']; } if (isset($product->skus[waRequest::get('sku')])) { $product['sku_id'] = waRequest::get('sku'); $s = $product->skus[$product['sku_id']]; if ($s['image_id'] && isset($product->images[$s['image_id']])) { $product['image_id'] = $s['image_id']; $product['ext'] = $product->images[$s['image_id']]['ext']; } } } if (!isset($product->skus[$product->sku_id])) { $product->sku_id = $product->skus ? key($product->skus) : null; } if (!$product->skus) { $product->skus = array(null => array('name' => '', 'sku' => '', 'id' => null, 'available' => false, 'count' => 0, 'price' => null, 'stock' => array())); } if ($this->getConfig()->getOption('can_use_smarty') && $product->description) { $product->description = wa()->getView()->fetch('string:' . $product->description); } if ((double) $product->compare_price <= (double) $product->price) { $product->compare_price = 0; } // check categories if ($product['categories']) { $categories = $product['categories']; $route = wa()->getRouting()->getDomain(null, true) . '/' . wa()->getRouting()->getRoute('url'); $category_routes_model = new shopCategoryRoutesModel(); $routes = $category_routes_model->getRoutes(array_keys($categories)); foreach ($categories as $c) { if (isset($routes[$c['id']]) && !in_array($route, $routes[$c['id']])) { unset($categories[$c['id']]); } } $product['categories'] = $categories; } $this->view->assign('product', $product); if ($product->sku_type == shopProductModel::SKU_TYPE_SELECTABLE) { $features_selectable = $product->features_selectable; $this->view->assign('features_selectable', $features_selectable); $product_features_model = new shopProductFeaturesModel(); $sku_features = $product_features_model->getSkuFeatures($product->id); $sku_selectable = array(); foreach ($sku_features as $sku_id => $sf) { if (!isset($product->skus[$sku_id])) { continue; } $sku_f = ""; foreach ($features_selectable as $f_id => $f) { if (isset($sf[$f_id])) { $sku_f .= $f_id . ":" . $sf[$f_id] . ";"; } } $sku = $product->skus[$sku_id]; $sku_selectable[$sku_f] = array('id' => $sku_id, 'price' => (double) shop_currency($sku['price'], $product['currency'], null, false), 'num' => 141, 'available' => $product->status && $sku['available'] && ($this->getConfig()->getGeneralSettings('ignore_stock_count') || $sku['count'] === null || $sku['count'] > 0), 'image_id' => (int) $sku['image_id']); if ($sku['compare_price']) { $sku_selectable[$sku_f]['compare_price'] = (double) shop_currency($sku['compare_price'], $product['currency'], null, false); } } $product['sku_features'] = ifset($sku_features[$product->sku_id], array()); $this->view->assign('sku_features_selectable', $sku_selectable); } }
/** * @param int $product * @param int|null $category_id * @return array */ public function getStorefrontMap($product, $category_id = null) { $storefronts_map = array(); $product_id = (int) $product; $category_product_model = new shopCategoryProductsModel(); $product_categories = $category_product_model->getByField('product_id', $product_id, 'category_id'); $product_type = $this->select('type_id')->where('id=' . $product_id)->fetchField(); if (!$product_categories || !$product_type) { return array(); } if ($category_id !== null) { if (isset($product_categories[$category_id])) { $product_categories = array($category_id => $product_categories[$category_id]); } else { return array(); } } $routing = wa()->getRouting(); $domain_routes = $routing->getByApp('shop'); $category_routes_model = new shopCategoryRoutesModel(); $category_routes = $category_routes_model->getRoutes(array_keys($product_categories)); foreach ($product_categories as $c_id => &$category) { $category['routes'] = isset($category_routes[$c_id]) ? $category_routes[$c_id] : array(); } unset($category); foreach ($product_categories as $c_id => $category) { $storefronts_map[$c_id] = array(); foreach ($domain_routes as $domain => $routes) { foreach ($routes as $r) { if (!empty($r['private'])) { continue; } if ((empty($r['type_id']) || in_array($product_type, (array) $r['type_id'])) && (!$category['routes'] || in_array($domain . '/' . $r['url'], $category['routes']))) { $routing->setRoute($r, $domain); $storefronts_map[$c_id][] = $routing->getUrl('shop/frontend', array(), true); } } } } $all_routes_count = 0; foreach ($domain_routes as $domain => $routes) { foreach ($routes as $r) { if (!empty($r['private'])) { continue; } $all_routes_count += 1; } } foreach ($storefronts_map as $c_id => &$storefronts_list) { if (count($storefronts_list) == $all_routes_count) { $storefronts_list = array(); } } unset($storefronts_list); if ($category_id !== null) { return $storefronts_map[$category_id]; } else { return $storefronts_map; } }
/** * @param string $url * @param array $get * @param waRouting $routing * @param bool $demo * @return string */ private function redirectSimpla($url, $get, $routing, $demo = false) { $redirect = null; if (substr($url, 0, 9) == 'products/') { # products/%product_slug% $url = substr($url, 9); $url_parts = explode('/', $url); if ($p = $demo ? array('url' => '%product_url%', 'category_id' => true) : $this->productModel()->getByField('url', $url_parts[0])) { $params = array('product_url' => $p['url']); if ($p['category_id']) { $c = $demo ? array('full_url' => '%category_url%') : $this->categoryModel()->getById($p['category_id']); $params['category_url'] = $c['full_url']; } $redirect = wa()->getRouteUrl('shop/frontend/product', $params); } } elseif (substr($url, 0, 8) == 'catalog/') { # catalog/%category_slug% $url = substr($url, 8); if ($demo) { $redirect_params = array('category_url' => $url); $redirect = wa()->getRouteUrl('shop/frontend/category', $redirect_params); } else { if ($c = $this->categoryModel()->getByField('full_url', rtrim($url, '/'))) { $route = $routing->getDomain(null, true) . '/' . $routing->getRoute('url'); $cat_routes_model = new shopCategoryRoutesModel(); $routes = $cat_routes_model->getRoutes($c['id']); if (!$routes || in_array($route, $routes)) { $redirect = wa()->getRootUrl(false, true) . $routing->getRootUrl() . $url; } } } } return $redirect; }
public function getFrontendUrls($id) { $category = $this->getById($id); if (!$category) { return array(); } $category_routes_model = new shopCategoryRoutesModel(); $category['routes'] = $category_routes_model->getRoutes($id); $frontend_urls = array(); $routing = wa()->getRouting(); $domain_routes = $routing->getByApp('shop'); foreach ($domain_routes as $domain => $routes) { foreach ($routes as $r) { if (!empty($r['private'])) { continue; } if (!$category['routes'] || in_array($domain . '/' . $r['url'], $category['routes'])) { $routing->setRoute($r, $domain); $frontend_urls[] = $routing->getUrl('shop/frontend/category', array('category_url' => isset($r['url_type']) && $r['url_type'] == 1 ? $category['url'] : $category['full_url']), true); } } } return $frontend_urls; }