public function get_annotation($json, $callback) { $data = json_to_array($json); $annotation_id = $data["annotation_id"]; //test_msg($annotation_id); $annotation = new Annotation($annotation_id); $data = array('annotation' => $annotation->export_data()); $this->_display_jsonp($data, $callback); }
function list_annotation($json, $callback = NULL) { $enable_profiler = FALSE; if ($enable_profiler === TRUE) { $this->output->enable_profiler(TRUE); } if (is_string($json)) { $data = json_to_object($json); } else { $data = $json; } $user = get_context_user(); $url = $this->url; $search = new Search_annotation_collection(); $search_id = null; if (isset($data->limit)) { $search_id = new Search_annotation_id_collection(); } // 1 [ topic id ] if (isset($data->topic_id) && isset($data->target_topic) && $data->target_topic === TRUE) { $annotation = new Annotation($data->topic_id); $output_data = array('annotation_collection' => array($annotation->export_data()), 'totally_loaded' => true); return $this->_display_jsonp($output_data, $callback); } else { if (isset($data->topic_id) && isset($data->target_topic) && $data->target_topic === FALSE) { $search->set_target_topic_id($data->topic_id); if (isset($search_id)) { $search_id->set_target_topic_id($data->topic_id); } } } // 2 [ scope ] //test_msg('2 [ scope ]', is_null($data->scope)); //如果沒有設定範圍,則直接回傳空值 //if (is_null($data->scope)) //{ // //return $this->_create_null_list($callback); //} if (isset($data->scope)) { $scope_coll_data = $data->scope; //test_msg('2 [ scope ] 2', is_null($data->scope)); $scope_coll = $this->annotation_scope_collection->import_webpage_search_data($url, $scope_coll_data); //test_msg($scope_coll->export_json()); $search->set_overlap_scope($scope_coll); if (isset($search_id)) { $search_id->set_overlap_scope($scope_coll); } //test_msg('2 [ scope ] 3', is_null($data->scope)); } // 3 [ target like ] // 4 [ target my ] //test_msg('3 [ target like ] 4 [ target my ]'); if ((isset($data->target_like) or isset($data->target_my)) && is_null($user)) { return $this->_create_null_list($callback); } if (isset($data->target_like)) { $search->set_target_like($data->target_like, $user); if (isset($search_id)) { $search_id->set_target_like($data->target_like, $user); } } if (isset($data->target_my)) { if ($data->target_my === TRUE) { $search->set_target_user($user); if (isset($search_id)) { $search_id->set_target_user($user); } } else { $search->set_exclude_user($user); if (isset($search_id)) { $search_id->set_exclude_user($user); } } } // 5 [ target_topic ] if (isset($data->target_topic)) { $search->set_target_topic($data->target_topic); if (isset($search_id)) { $search_id->set_target_topic($data->target_topic); } } // 6 [ order by ] // 6 [ is_desc] //test_msg('6 [ order by ]', isset($data->order_by)); //test_msg('6 [ order by ] direction', $data->direction); $order_id = 1; $default_direction = TRUE; if (isset($data->order_by)) { if ($data->order_by == 'update') { $order_id = 6; $default_direction = TRUE; } else { if ($data->order_by == 'create') { $order_id = 7; $default_direction = TRUE; } else { // 依照分數排序 $order_id = 1; $default_direction = TRUE; } } } if (isset($data->direction) && $data->direction == 'asc') { $default_direction = FALSE; } /* if (isset($data->order_by) === FALSE OR $data->order_by != 'update') { $order_id = 6; $default_is_desc = TRUE; } */ //test_msg('6 [ order by ] add_oder', array($order_id, $default_is_desc)); $search->add_order($order_id, $default_direction); // if (isset($search_id)) { // $search_id->add_order ($order_id, $default_direction); // } // 7 [ offset ] //test_msg('7 [ offset ]', isset($data->offset)); if (isset($data->offset)) { $search->set_offset($data->offset); // if (isset($search_id)) { // $search_id->set_offset($data->offset); // } } // 8 [ limit ] //test_msg('8 [ limit ]', array(isset($data->limit),$data->limit)); if (isset($data->limit)) { $search->set_limit($data->limit); //$search_id在此不作設限 } $search->set_target_webpage(get_context_webpage()); //輸出 $totally_loaded = TRUE; if (isset($search_id)) { $totally_loaded = FALSE; } //不作limit的情況下讀完,表示完全讀取 if (isset($search_id) && ($search->length() == $search_id->length() || $search->length() == 0)) { $totally_loaded = TRUE; } $annotation_collection = array(); //test_msg('Search Length', $search->length()); foreach ($search as $search_annotation) { $annotation_data = $search_annotation->export_webpage_data($url); if (isset($data->target_topic) && $data->target_topic === TRUE) { $search_data = json_to_object('{}'); $search_data->target_topic = FALSE; $search_data->topic_id = $search_annotation->get_id(); if (isset($data->respond_limit)) { $search_data->limit = $data->respond_limit; } //$search_data->is_like = NULL; $search_data->order_by = 'create'; if (isset($data->respond_direction)) { $search_data->direction = $data->respond_direction; } else { //$search_data->direction = "asc"; } $search_data->show_total_count = TRUE; $search_result = $this->list_annotation($search_data); //test_msg($search_result); if (count($search_result['annotation_collection']) > 0) { $annotation_data['respond_list'] = $search_result; } } array_push($annotation_collection, $annotation_data); } $output_data = array('annotation_collection' => $annotation_collection, 'totally_loaded' => $totally_loaded); if (isset($data->show_total_count) && $data->show_total_count === TRUE) { if (count($annotation_collection) === 0) { $output_data['total_count'] = 0; } else { if (isset($search_id)) { $output_data['total_count'] = $search_id->length(); } else { $output_data['total_count'] = count($annotation_collection); } } } //log區 $array_data = NULL; if (is_string($json)) { $array_data = json_to_array($json); } else { $array_data = (array) $json; } $user_id = NULL; if (isset($user)) { $user_id = $user->get_id(); } $action = 12; if (isset($data->topic_id) && isset($data->target_topic) && $data->target_topic === FALSE && isset($data->limit) == FALSE) { $action = 16; } $do_log = TRUE; if (isset($data->limit) && $data->limit == 5) { $do_log = FALSE; } if (isset($data->target_my)) { if ($data->target_my == FALSE) { $do_log = FALSE; } } else { if ($user_id == NULL) { $action = 17; if (isset($data->topic_id) && isset($data->target_topic) && $data->target_topic === FALSE && isset($data->limit) == FALSE) { $action = 18; } } } if ($do_log) { kals_log($this->db, $action, array('memo' => $array_data, 'user_id' => $user_id)); } context_complete(); if ($enable_profiler != TRUE) { return $this->_display_jsonp($output_data, $callback); } }