public function doAddUser($data, $allow)
{
$userDao = self::factoryModel('user');
$optsDao = self::factoryModel('opts');
//检查这个id是否存在
if (false == ($event = $this->where('id =' . $data['id'])->find())) {
return -1;
}
if ($data['action'] === 'joinIn') {
//自动获取已填写的联系方式
$contact_fields = M('user_set')->where('module=\'contact\'')->findAll();
$contact_info = M('user_profile')->getField('data', "uid={$data['uid']} AND module='contact'");
$contact_info = unserialize($contact_info);
$need_fields = array('手机', 'QQ', 'MSN');
foreach ($contact_fields as $field) {
if (in_array($field['fieldname'], $need_fields) && !empty($contact_info[$field['fieldkey']])) {
$contacts .= $field['fieldname'] . ':' . $contact_info[$field['fieldkey']] . ' ';
}
}
}
//检查仅好友参与选项
$opts = $optsDao->where('id=' . $event['optsId'])->find();
$opt = unserialize($opts['opts']);
$role = $this->checkMember($event['uid'], $opt, $data['uid']);
//检查是否已经加入或者关注
if ($userDao->hasUser($data['uid'], $data['id'], $data['action'])) {
return -2;
}
//关注的话,再检查是否已经加入
if ($data['action'] == 'attention' && $userDao->hasUser($data['uid'], $data['id'], 'joinIn')) {
return -2;
}
$map = $data;
$map['eventId'] = $data['id'];
unset($map['id']);
$map['cTime'] = time();
$map['contact'] = $contacts;
switch ($data['action']) {
case 'attention':
if (false === $opts['canAtt']) {
return -2;
}
if ($userDao->add($map)) {
$this->setInc('attentionCount', 'id=' . $map['eventId']);
return 1;
} else {
return 0;
}
break;
case 'joinIn':
if (false === $role['canJoin']) {
return -2;
}
if (!$role['follow']) {
return 0;
}
$map['status'] = $opt['allow'] ? 0 : 1;
//如果有关注的情况,直接更新为参与
$temp_map['uid'] = $map['uid'];
$temp_map['action'] = 'attention';
$temp_map['eventId'] = $map['eventId'];
if ($id = $userDao->where($temp_map)->getField('id')) {
if ($res = $userDao->where("id={$id}")->save($map)) {
$this->setDec('attentionCount', 'id=' . $map['eventId']);
}
} else {
$res = $userDao->add($map);
}
if ($res) {
if ($map['status']) {
$this->setInc('joinCount', 'id=' . $map['eventId']);
$this->setDec('limitCount', 'id=' . $map['eventId']);
model('Credit')->setUserCredit($map['uid'], 'join_event');
}
return 1;
} else {
return 0;
}
break;
default:
return -3;
}
}