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; } } }
/** * 编辑商品分类导航 */ public function category_menu_edit($id) { $site_menu = Mysite_menu::instance($id); $temp = Mysite_menu::instance()->site_menu_queue($id); if ($_POST) { //标签过滤 tool::filter_strip_tags($_POST); /* 获取要编辑的导航的level_depth并修改其下所有的子导航的level_depth*/ $data = $_POST; $parent_id = $this->input->post('parent_id'); if ($parent_id == 0) { $data['level_depth'] = 1; } else { $parent_level_depth = Mysite_menu::instance($parent_id)->get('level_depth'); $data['level_depth'] = $parent_level_depth + 1; } if (!Mysite_menu::instance()->child_level_depth_edit($id, $data['level_depth'], $temp)) { remind::set(Kohana::lang('o_global.update_error'), 'site/menu'); } /* 获取所添加的分类的url*/ $category_id = $this->input->post('category_id'); $data['url'] = category::permalink($category_id, false); //'/category/'.$category_id; /* 标志导航类型*/ $data['memo'] = 'category'; $data['type'] = $this->MENU_TYPE_CATEGORY; $data['relation_id'] = $category_id; if ($site_menu->site_menu_edit($id, $data)) { remind::set(Kohana::lang('o_global.update_success'), 'site/menu/', 'success'); } else { remind::set(Kohana::lang('o_global.update_error'), 'site/menu'); } } $site_menu_data = $site_menu->get(); /* 得到导航列表并删除自身及自身的子目录*/ $child_ids = array(); $site_menus = Mysite_menu::instance()->site_menu_queue(); foreach ($temp as $val) { $child_ids[] = $val['id']; } foreach ($site_menus as $key => $value) { if ($value['id'] == $id || in_array($value['id'], $child_ids)) { unset($site_menus[$key]); } } /* 当前站点分类*/ $categories = CategoryService::get_instance()->get_categories(); $str = '<option value={$id} {$selected}>{$spacer}{$title_manage}</option>'; $category_list = tree::get_tree($categories, $str, 0, $site_menu_data['relation_id']); $this->template->content = new View("site/category_menu_edit"); $this->template->content->site_menus = $site_menus; $this->template->content->category_list = $category_list; $this->template->content->site_menu_data = $site_menu_data; }