/** * 设置商品的关联商品 * * @param array $product * @param array $relation_ids * @return boolean */ public static function set(&$product) { ORM::factory('product_relation')->where('product_id', $product['id'])->delete_all(); if (isset($product['pdt_relation_ids']) && count($product['pdt_relation_ids']) > 0) { $relations = ProductService::get_instance()->index(array('where' => array('id' => $product['pdt_relation_ids']))); foreach ($relations as $relation) { if ($relation['id'] != $product['id']) { Product_relationService::get_instance()->add(array('product_id' => $product['id'], 'relation_product_id' => $relation['id'])); } } } return TRUE; }
public function delete_all() { $return_struct = array('status' => 0, 'code' => 501, 'msg' => 'Not Implemented.', 'content' => array()); try { //$profiler = new Profiler; //* 初始化返回数据 */ $return_data = array(); //* 收集请求数据 ==根据业务逻辑定制== */ $request_data = $this->input->get(); //* 实现功能后屏蔽此异常抛出 */ //throw new MyRuntimeException('Not Implemented',501); //* 权限验证,数据验证,逻辑验证 ==根据业务逻辑定制== */ $site_ids = role::get_site_ids(); if (empty($site_ids)) { throw new MyRuntimeException(Kohana::lang('o_global.access_denied'), 403); } if (empty($request_data['product_id']) or empty($request_data['relation_ids'])) { throw new MyRuntimeException(Kohana::lang('o_global.bad_request'), 400); } //* 权限验证 ==根据业务逻辑定制== */ $product_service = ProductService::get_instance(); $product_relation_service = Product_relationService::get_instance(); $product = $product_service->get($request_data['product_id']); if (!in_array($product['site_id'], $site_ids)) { throw new MyRuntimeException(Kohana::lang('o_global.access_denied'), 403); } $relation_ids = array(); $query_struct = array('where' => array('product_id' => $product, 'relation_product_id' => explode('-', $request_data['relation_ids']))); foreach (Product_relationService::get_instance()->query_assoc($query_struct) as $relation) { $relation_ids[$relation['relation_product_id']] = TRUE; } ORM::factory('product_relation')->where('product_id', $request_data['product_id'])->in('relation_product_id', array_keys($relation_ids))->delete_all(); ProductService::get_instance()->set($product['id'], array('update_timestamp' => time())); //* 补充&修改返回结构体 */ $return_struct['status'] = 1; $return_struct['code'] = 200; $return_struct['msg'] = ''; $return_struct['content'] = $relation_ids; //* 请求类型 */ if ($this->is_ajax_request()) { // ajax 请求 // json 输出 $this->template->content = $return_struct; } else { throw new MyRuntimeException(Kohana::lang('o_global.access_denied'), 404); // html 输出 $this->template = new View('layout/empty_html'); //$this->template->manager_data = $this->manager; //* 模板输出 */ //$this->template->return_struct = $return_struct; $content = new View($this->package_name . '/' . $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->title = Kohana::config('site.name'); } // 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 { //$return_struct['action']['type']= 'close'; // 当前应用为弹出窗口所以定义失败后续动作为关闭窗口 $this->template = new View('layout/default_html'); $this->template->return_struct = $return_struct; $content = new View('info2'); $this->template->content = $content; //* 请求结构数据绑定 */ $this->template->content->request_data = $request_data; //* 返回结构体绑定 */ $this->template->content->return_struct = $return_struct; } } }