private function _find_duplicate(Group_Contact $contact) { /*1.联系人姓名相同,且有一个手机号码相同的,判断为同一个联系人, 自动进行合并处理,合并时需要判断好友关系、公司、部门、职位、生日、 昵称、头像这几个信息是否有冲突,如果没有冲突则可以合并两个联系人 */ $same_name_tel_tag = new Group_Contact(); $same_name_tel_tag->set_group_id($contact->get_group_id())->set_formatted_name($this->name_to_formatted_name($contact->get_family_name(), $contact->get_given_name()))->set_tels($contact->get_tels()); $result = $this->group_contact_mapper->find_by_tags($same_name_tel_tag); if (!empty($result)) { //冲突检测 foreach ($result as $id) { $find_contact = $this->find_by_id($contact->get_group_id(), $id); if ($find_contact->merge($contact)) { return $find_contact; } } } return FALSE; /* //2 两个联系人中有Email、IM、电话号码相同,其中一个没有姓名,在其他信息没有冲突情况下,可以合并 $same_email_tag = new Contact(); $same_email_tag->set_user_id($contact->get_user_id()) ->set_emails($contact->get_emails()); $result = $this->contact_mapper->find_by_tags($same_email_tag); if (! empty($result)) { //冲突检测 foreach ($result as $id) { $find_contact = $this->get($contact->get_user_id(), $id); if ($find_contact->merge($contact, $is_append)) { return $find_contact; } } } //3 所有信息完全相同的联系人,可以合并 $all_same_tag = new Contact(); $all_same_tag->set_user_id($contact->get_user_id()); $contact_array = $contact->to_array(); foreach ($contact_array as $key => $value) { if (! in_array($key, array('tels', 'emails', 'ims')) and ! empty($value)) { call_user_func(array($all_same_tag, 'set_' . $key), $value); } } $result = $this->contact_mapper->find_by_tags($all_same_tag); if (! empty($result)) { //冲突检测 foreach ($result as $id) { $find_contact = $this->get($contact->get_user_id(), $id); if ($contact_array == array_intersect_assoc( $find_contact->to_array(), $contact_array)) { return $find_contact; } } } return FALSE; */ }
/** * 根据群联系人对象创建群联系人 * @param Group_Contact $group_contact 群联系人对象 * @throws Exception */ public function insert($group_contact) { $query = $this->db->query("INSERT INTO `gcp_contacts` (`gid`,`uid`, `formatted_name`," . " `phonetic`, `given_name`, `middle_name`, `family_name`," . " `prefix`, `suffix`, `organization`, `department`, `note`," . " `birthday`, `title`, `nickname`, `sort`," . "`created`, `modified`, `fid`) VALUES (? , ?, " . " ? , ?, ? , ? , ? , ? , ? , ? , ?, ? , ? , ? , ? , ? , ? , ? , ? )", array($group_contact->get_group_id(), $group_contact->get_user_id(), $group_contact->get_formatted_name(), $group_contact->get_phonetic(), $group_contact->get_given_name(), $group_contact->get_middle_name(), $group_contact->get_family_name(), $group_contact->get_prefix(), $group_contact->get_suffix(), $group_contact->get_organization(), $group_contact->get_department(), $group_contact->get_note(), $group_contact->get_birthday(), $group_contact->get_title(), $group_contact->get_nickname(), $group_contact->get_sort(), $group_contact->get_modified_at(), $group_contact->get_modified_at(), $group_contact->get_momo_user_id())); $id = $query->insert_id(); if ($id) { $sqls = array_merge($this->_edit_avatar($group_contact->get_group_id(), $id, $group_contact->get_avatar())); foreach (array('emails', 'tels', 'addresses', 'ims', 'urls', 'events', 'relations') as $type) { $sqls = array_merge($sqls, $this->_add_info($group_contact->get_group_id(), $id, $type, call_user_func(array($group_contact, 'get_' . $type)))); } foreach ($sqls as $sql) { $this->db->query($sql); } $inserted = $this->find_by_id($group_contact->get_group_id(), $id); // clean up database related fields in parameter instance if (method_exists($inserted, 'set_id')) { $group_contact->set_id($inserted->get_id()); $group_contact->set_created_at($inserted->get_created_at()); $group_contact->set_modified_at($inserted->get_modified_at()); } } else { throw new Exception('DB Error: Add Group_Contact Fail'); } }