public function index() { $route_data = Myroute::instance()->get(); if ($_POST) { $site_next_flow = site::site_next_flow($this->current_flow); $submit_target = intval($this->input->post('submit_target')); if (Myroute::instance()->edit($_POST)) { //判断添加成功去向 switch ($submit_target) { case 2: remind::set(Kohana::lang('o_global.update_success'), $site_next_flow['url'], 'success'); default: remind::set(Kohana::lang('o_global.update_success'), 'site/route', 'success'); } } else { remind::set(Kohana::lang('o_global.update_error'), 'site/route'); } } $this->template->content = new View("site/route_edit"); $this->template->content->is_modify = 0; if ($this->manager_is_admin == 1) { $this->template->content->is_modify = 1; } $this->template->content->data = $route_data; $this->template->content->site_id = $this->site_id; }
/** * 获得商品前台链接 * * @param int 商品id * @param String 商品链接 */ public static function permalink($product) { static $routes = array(); static $categorys = array(); if (!is_array($product)) { $product = ProductService::get_instance()->get($product); } $routes = Myroute::instance()->get(); $domain = Mysite::instance()->get('domain'); $route_type = $routes['type']; $product_suffix = $routes['product_suffix']; $product_route = $routes['product']; if ($route_type == 2 or $route_type == 3) { if (!isset($categorys[$product['category_id']])) { $categorys[$product['category_id']] = CategoryService::get_instance()->get($product['category_id']); } } $permalink = ''; $product['uri_name'] = isset($product['uri_name']) ? $product['uri_name'] : $product['id']; switch ($route_type) { case 0: //$permalink = $product_route.'/'.$product['id'].$product_suffix; $permalink = $product_route . $product['id']; break; case 1: $permalink = $product_route . '/' . urlencode($product['uri_name']) . $product_suffix; break; case 2: $permalink = urlencode($categorys[$product['category_id']]['uri_name']) . '/' . urlencode($product['uri_name']) . $product_suffix; break; case 3: $permalink = urlencode($categorys[$product['category_id']]['uri_name']) . '/' . urlencode($product['uri_name']) . $product_suffix; break; case 4: $permalink = $product_route . '/' . urlencode($product['uri_name']) . $product_suffix; break; } return Kohana::config('config.front_protocol') . $domain . '/' . $permalink; }
/** * 获取前台分类链接 * * @param int id 分类id * @return string 分类链接 */ public static function permalink($id, $absolute = true) { $category_service = CategoryService::get_instance(); $category = $category_service->get($id); $route = Myroute::instance()->get(); $route_type = $route['type']; $category_route = $route['category']; $category_suffix = $route['category_suffix']; $domain = Mysite::instance()->get('domain'); if ($route_type == 0) { // 0: none get category and product with id $category_permalink = $category_route . '/' . $category['id']; } if ($route_type == 1) { // 1: get product with {product}/permalink $category_permalink = $category_route . '/' . urlencode($category['uri_name']); } if ($route_type == 2 || $route_type == 4) { // 2: get category and product with {category_permalink} and {category+permalink}/{product_permalink} $category_permalink = urlencode($category['uri_name']); } if ($route_type == 3) { // 3: get category and prdouct with {category_permalink1}/.../{category_permalinkn} and {category_permalink1}/.../{category_permalinkn}/{product_permalink} $parents = $category_service->get_parents_by_category_id($id); $category_permalink = ''; foreach ($parents as $category) { if ($category == end($parents)) { $category_permalink .= urlencode($category['uri_name']); } else { $category_permalink .= urlencode($category['uri_name']) . '/'; } } } if ($absolute) { return 'http://' . $domain . '/' . $category_permalink . $category_suffix; } else { return $category_permalink . $category_suffix; } }
/** * init site data * * @param int $site_id * @param int $theme_id * @return boolean */ public static function init($site_id, $theme_id = 1) { $type = 0; //doc default info //Mydoc::instance()->init($site_id); site::doc_init($site_id); //faq default info Myfaq::instance()->init($site_id); //seo default info Myseo::instance()->init($site_id); //route default info Myroute::instance()->init($site_id); //menu default info Mysite_menu::instance()->init($site_id); //theme default info Mytheme::instance($theme_id)->init($site_id); //mail default info Mymail::instance()->init($site_id); //product default info //Mydata_import::instance()->import($site_id); }
public function edit() { role::check('product_category_edit'); $return_struct = array('status' => 0, 'code' => 501, 'msg' => 'Not Implemented', 'content' => array()); try { //* 初始化返回数据 */ $return_data = array('action' => 'post'); //* 收集请求数据 ==根据业务逻辑定制== */ $request_data = $this->input->get(); //数据验证 if (!isset($request_data['id']) || !is_numeric($request_data['id'])) { throw new MyRuntimeException(Kohana::lang('o_global.input_error'), 404); } $category_service = CategoryService::get_instance(); $category = $category_service->get($request_data['id']); //返回数据 //$category['pic_url'] = AttributeService::get_attach_url($category['pic_attach_id'], 't'); $category['pic_url'] = AttService::get_instance($this->img_dir_name)->get_img_url($category['pic_attach_id']); $return_data['data'] = $category; //uri_name处理 $route = Myroute::instance()->get(); $route_type = $route['type']; $category_route = $route['category']; $category_suffix = $route['category_suffix']; if ($route_type == 0) { // 0: none get category and product with id $category_permalink = $category_route . '/' . $category['id']; } else { if ($route_type == 1) { // 1: get product with {product}/permalink $category_permalink = $category_route . '/'; } else { if ($route_type == 2 || $route_type == 4) { // 2: get category and product with {category_permalink} and {category+permalink}/{product_permalink} $category_permalink = ''; } else { if ($route_type == 3) { // 3: get category and prdouct with {category_permalink1}/.../{category_permalinkn} and {category_permalink1}/.../{category_permalinkn}/{product_permalink} $parents = $category_service->get_parents_by_category_id($category['id']); $category_permalink = ''; $i = 1; foreach ($parents as $val) { if ($i != 1) { $category_permalink = urlencode($val['uri_name']) . '/' . $category_permalink; } $i++; } } } } } //当前站点分类 $categories = $category_service->get_categories(); $child_ids = $category_service->get_childrens_by_category_id($category['id']); $parent_data = $category_service->get_parents_by_category_id($category['id']); $parent_is_show = isset($parent_data[1]) ? $parent_data[1]['is_show'] : 1; //去掉子分类和自己 foreach ($categories as $key => $val) { if (in_array($val['id'], $child_ids) || $val['id'] == $category['id']) { unset($categories[$key]); } } $str = '<option value={$id} {$selected}>{$spacer}{$title}</option>'; //$return_data['category_list'] = $category_service->get_tree_by_site_id($request_data['site_id'], '<option value={$id} {$selected}>{$spacer}{$title}</option>',$category['pid']); $return_data['category_list'] = tree::get_tree($categories, $str, 0, $category['pid']); //当前站点类型 $classify = ClassifyService::get_instance()->get_classifies(); $return_data['classify_list'] = ''; foreach ($classify as $val) { $selected = $category['classify_id'] == $val['id'] ? 'selected' : ''; $return_data['classify_list'] .= '<option value=' . $val['id'] . ' ' . $selected . '>--' . $val['name'] . '--</option>'; } //* 补充&修改返回结构体 */ $return_struct['status'] = 1; $return_struct['code'] = 200; $return_struct['msg'] = ''; $return_struct['content'] = $return_data; //* 请求类型 */ if ($this->is_ajax_request()) { // ajax 请求 // json 输出 $this->template->content = $return_struct; } else { // html 输出 //* 模板输出 */ $content = new View($this->package . '/' . $this->class_name . '/' . __FUNCTION__); //* 变量绑定 */ $this->template->title = Kohana::config('site.name'); $this->template->content = $content; //* 请求结构数据绑定 */ $this->template->content->request_data = $request_data; //* 返回结构体绑定 */ $this->template->content->return_struct = $return_struct; //:: 当前应用专用数据 $this->template->content->has_child = count($child_ids); $this->template->content->parent_is_show = $parent_is_show; $this->template->content->route_type = $route_type; $this->template->content->category_suffix = $category_suffix; $this->template->content->category_permalink = $category_permalink; } // end of request type determine } catch (MyRuntimeException $ex) { $this->_ex(&$ex, $return_struct, $request_data); } }
public function build() { $return_struct = array('status' => 0, 'code' => 501, 'msg' => 'Not Implemented', 'content' => array()); try { //$profiler = new Profiler; //* 初始化返回数据 */ $return_data = array(); //* 收集请求数据 ==根据业务逻辑定制== */ $request_data = $this->input->post(); //* 实现功能后屏蔽此异常抛出 */ //throw new MyRuntimeException('Not Implemented',501); //权限验证 if ($this->site_id == 0) { throw new MyRuntimeException(Kohana::lang('o_global.select_site'), 400); } role::check('default', $this->site_id, 0); // 调用底层服务 $sitemap_service = SitemapService::get_instance(); //业务逻辑 $xmlContent = ''; $xmlContent .= '<?xml version="1.0" encoding="UTF-8"?>'; $xmlContent .= '<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'; //添加首页 if (!empty($request_data['index']) && is_numeric($request_data['index'])) { $priority = number_format($request_data['index'], 1); } else { throw new MyRuntimeException(Kohana::lang('o_global.bad_request'), 404); } $site_domain = Mysite::instance($this->site_id)->get('domain'); $xmlContent .= sitemap::Render('http://' . $site_domain, 0, 'always', $priority); //添加分类页面 $categories = $sitemap_service->get_category_page_by_site_id($this->site_id); if (!empty($request_data['category']) && is_numeric($request_data['category'])) { $priority = number_format($request_data['category'], 1); } else { throw new MyRuntimeException(Kohana::lang('o_global.bad_request'), 404); } if (!empty($categories)) { foreach ($categories as $category) { if (empty($request_data['exclude_category']) || !empty($request_data['exclude_category']) && !in_array($category['id'], $request_data['exclude_category'])) { $xmlContent .= sitemap::Render(category::permalink($category['id']), 0, 'weekly', $priority); } } } //添加商品页面 if (!empty($request_data['product']) && is_numeric($request_data['product'])) { $priority = number_format($request_data['product'], 1); } else { throw new MyRuntimeException(Kohana::lang('o_global.bad_request'), 404); } if (isset($request_data['on_sale'])) { $on_sale = intval($request_data['on_sale']); } else { throw new MyRuntimeException(Kohana::lang('o_global.bad_request'), 404); } $products = $sitemap_service->get_product_page_by_site_id($this->site_id, $on_sale); if (!empty($request_data['exclude_product'])) { if (preg_match('/^([a-zA-Z0-9_]+,)*[a-zA-Z0-9_]+$/i', $request_data['exclude_product'])) { $request_data['exclude_product'] = explode(',', $request_data['exclude_product']); } else { throw new MyRuntimeException(Kohana::lang('o_site.product_id_format_check'), 404); } } if (!empty($products)) { foreach ($products as $product) { if (empty($request_data['exclude_product'])) { $xmlContent .= sitemap::Render(product::permalink($product['id']), $product['update_timestamp'], 'weekly', $priority); } elseif (!empty($request_data['exclude_product']) && !in_array($product['sku'], $request_data['exclude_product'])) { $xmlContent .= sitemap::Render(product::permalink($product['id']), $product['update_timestamp'], 'weekly', $priority); } } } //添加促销页 if (!empty($request_data['promotion']) && is_numeric($request_data['promotion'])) { $priority = number_format($request_data['promotion'], 1); } else { throw new MyRuntimeException(Kohana::lang('o_global.bad_request'), 404); } $promotions = $sitemap_service->get_promotion_page_by_site_id($this->site_id); if (!empty($promotions)) { $route = Myroute::instance()->get(); $action = $route['promotion']; foreach ($promotions as $promotion) { $xmlContent .= sitemap::Render('http://' . $site_domain . '/' . $action . '/' . $promotion['id'], time(), 'weekly', $priority); } } //添加文案页 if (!empty($request_data['doc']) && is_numeric($request_data['doc'])) { $priority = number_format($request_data['doc'], 1); } else { throw new MyRuntimeException(Kohana::lang('o_global.bad_request'), 404); } $docs = $sitemap_service->get_doc_page_by_site_id($this->site_id); if (!empty($docs)) { foreach ($docs as $doc) { $lastmod = strtotime($doc['updated']); $xmlContent .= sitemap::Render('http://' . $site_domain . '/' . $doc['permalink'], $lastmod, 'weekly', $priority); } } $xmlContent .= '</urlset>'; $data['sitemap'] = $xmlContent; if (!Mysite_detail::instance()->update_by_site_id($this->site_id, $data)) { throw new MyRuntimeException(Kohana::lang('o_site.sitemap_error_handle'), 500); } //* 补充&修改返回结构体 */ $return_struct['status'] = 1; $return_struct['code'] = 200; $return_struct['msg'] = '操作成功'; $return_struct['content'] = $return_data; $return_struct['action'] = array('type' => 'location', 'url' => url::base() . 'site/sitemap'); //* 请求类型 */ if ($this->is_ajax_request()) { // ajax 请求 // json 输出 $this->template->content = $return_struct; } else { // html 输出 $this->template->return_struct = $return_struct; $content = new View('info'); $this->template->content = $content; //* 请求结构数据绑定 */ $this->template->content->request_data = $request_data; //* 返回结构体绑定 */ $this->template->content->return_struct = $return_struct; } // end of request type determine } catch (MyRuntimeException $ex) { $return_struct['status'] = 0; $return_struct['code'] = $ex->getCode(); $return_struct['msg'] = $ex->getMessage(); //TODO 异常处理 //throw $ex; if ($this->is_ajax_request()) { $this->template->content = $return_struct; } else { $this->template->return_struct = $return_struct; $content = new View('info'); $this->template->content = $content; //* 请求结构数据绑定 */ $this->template->content->request_data = $request_data; //* 返回结构体绑定 */ $this->template->content->return_struct = $return_struct; } } }
/** * get site's route config * * @return Array */ public function route() { // set route config $this->data['route'] = Myroute::instance($this->data['id'])->route(); return $this->data['route']; }
public static function parse($current_uri) { $current_uri = trim($current_uri); if (empty($current_uri)) { return FALSE; } $site_id = Mysite::instance()->id(); // 尝试解析 doc $doc_uris = Mydoc::get_uris($site_id); if (isset($doc_uris[$current_uri])) { return '/doc/view/' . $current_uri; } $route_type = Myroute::instance()->type(); $category_suffix = Myroute::instance()->get_action('category_suffix'); $category_key = Myroute::instance()->get_action('category'); $categorys = CategoryService::get_instance()->get_uri_name(); // 尝试解析 category $arguments = explode('/', $current_uri); switch ($route_type) { case 0: // www.example.com/category/1.html/page/2 if ($arguments[0] === $category_key and isset($arguments[1])) { if (!empty($category_suffix)) { $arguments[1] = substr($arguments[1], 0, strlen($arguments[1]) - strlen($category_suffix)); } if (preg_match('/^\\d+$/', $arguments[1])) { $result = 'category/index/' . $arguments[1]; for ($i = 2; isset($arguments[$i]); $i++) { $result .= '/' . $arguments[$i]; } return $result; } } break; case 1: // www.example.com/category/xxxxx.html/page/2 if ($arguments[0] === $category_key and isset($arguments[1])) { if (!empty($category_suffix)) { $arguments[1] = substr($arguments[1], 0, strlen($arguments[1]) - strlen($category_suffix)); } if (isset($categorys[$arguments[1]])) { $result = 'category/index/' . $arguments[1]; for ($i = 2; isset($arguments[$i]); $i++) { $result .= '/' . $arguments[$i]; } return $result; } } break; case 2: // www.example.com/xxxxx.html/page/2 if (count($arguments) == 1 or count($arguments) > 2) { if (!empty($category_suffix)) { $arguments[0] = substr($arguments[0], 0, strlen($arguments[0]) - strlen($category_suffix)); } if (isset($categorys[$arguments[0]])) { $result = 'category/index/' . $arguments[0]; for ($i = 1; isset($arguments[$i]); $i++) { $result .= '/' . $arguments[$i]; } return $result; } } break; case 3: // www.example.com/xxxxx/ooooo.html/page/2 for ($i = 0; isset($arguments[$i]); $i++) { if (!isset($categorys[$arguments[$i]])) { break; } } if ($i == 0) { break; } if (empty($category_suffix)) { $i--; } $count = count($arguments) - $i; if ($count == 1 or $count > 2) { if (!empty($category_suffix)) { $arguments[$i] = substr($arguments[$i], 0, strlen($arguments[$i]) - strlen($category_suffix)); } $result = 'category/index/' . $arguments[$i]; for ($i = $i + 1; isset($arguments[$i]); $i++) { $result .= '/' . $arguments[$i]; } return $result; } break; case 4: // www.example.com/xxxxx.html/page/2 if (count($arguments) == 1 or count($arguments) > 2) { if (!empty($category_suffix)) { $arguments[0] = substr($arguments[0], 0, strlen($arguments[0]) - strlen($category_suffix)); } if (isset($categorys[$arguments[0]])) { $result = 'category/index/' . $arguments[0]; for ($i = 1; isset($arguments[$i]); $i++) { $result .= '/' . $arguments[$i]; } return $result; } } break; } $product_suffix = Myroute::instance()->get_action('product_suffix'); $product_key = Myroute::instance()->get_action('product'); // 尝试解析 product $arguments = explode('/', $current_uri); switch ($route_type) { case 0: // www.example.com/product/220.html if ($arguments[0] === $product_key and isset($arguments[1])) { if (!empty($product_suffix)) { $arguments[1] = substr($arguments[1], 0, strlen($arguments[1]) - strlen($product_suffix)); } if (preg_match('/^\\d+$/', $arguments[1])) { return 'product/get/' . $arguments[1]; } } break; case 1: // www.example.com/product/yyyyy.html if ($arguments[0] === $product_key and isset($arguments[1])) { if (!empty($product_suffix)) { $arguments[1] = substr($arguments[1], 0, strlen($arguments[1]) - strlen($product_suffix)); } return 'product/get/' . $arguments[1]; } break; case 2: // www.example.com/xxxxx/yyyyy.html if (isset($categorys[$arguments[0]]) and isset($arguments[1])) { if (!empty($product_suffix)) { $arguments[1] = substr($arguments[1], 0, strlen($arguments[1]) - strlen($product_suffix)); } return 'product/get/' . $arguments[1]; } break; case 3: // www.example.com/xxxxx/ooooo/yyyyy.html for ($i = 0; isset($arguments[$i]); $i++) { if (!isset($categorys[$arguments[$i]])) { break; } } if ($i > 0 and isset($arguments[$i])) { if (!empty($product_suffix)) { $arguments[$i] = substr($arguments[$i], 0, strlen($arguments[$i]) - strlen($product_suffix)); } return 'product/get/' . $arguments[$i]; } break; case 4: // www.example.com/product/yyyyy.html if ($arguments[0] === $product_key and isset($arguments[1])) { if (!empty($product_suffix)) { $arguments[1] = substr($arguments[1], 0, strlen($arguments[1]) - strlen($product_suffix)); } return 'product/get/' . $arguments[1]; } break; } return FALSE; }