/** * join group safely * * @param int $id * @param UserTable $user */ private function joinGroup( $id, $user ) { global $_CB_framework, $_CB_database, $_PLUGINS; $group = CBGroupJive::getGroup( $id ); $isModerator = CBGroupJive::isModerator( $user->get( 'id' ) ); if ( ! $user->get( 'id' ) ) { $returnUrl = $_CB_framework->pluginClassUrl( $this->element, false, array( 'action' => 'groups', 'func' => 'join', 'id' => (int) $group->get( 'id' ) ) ); cbRedirect( $_CB_framework->viewUrl( 'login', false, array( 'return' => base64_encode( $returnUrl ) ) ), CBTxt::T( 'Please Login or Register to join groups.' ) ); } else { $returnUrl = $_CB_framework->pluginClassUrl( $this->element, false, array( 'action' => 'groups', 'func' => 'show', 'id' => (int) $group->get( 'id' ) ) ); if ( ! CBGroupJive::canAccessGroup( $group, $user ) ) { CBGroupJive::returnRedirect( $returnUrl, CBTxt::T( 'Group does not exist.' ), 'error' ); } } $row = new \CB\Plugin\GroupJive\Table\UserTable(); $row->load( array( 'user_id' => (int) $user->get( 'id' ), 'group' => (int) $group->get( 'id' ) ) ); if ( $row->get( 'id' ) ) { switch ( (int) $row->get( 'status' ) ) { case -1: CBGroupJive::returnRedirect( $returnUrl, CBTxt::T( 'You have been banned from this group.' ), 'error' ); break; case 0: CBGroupJive::returnRedirect( $returnUrl, CBTxt::T( 'Your group join request is currently pending approval.' ) ); break; default: CBGroupJive::returnRedirect( $returnUrl, CBTxt::T( 'You have already joined this group.' ), 'error' ); break; } } $query = 'SELECT *' . "\n FROM " . $_CB_database->NameQuote( '#__groupjive_invites' ) . "\n WHERE " . $_CB_database->NameQuote( 'group' ) . " = " . (int) $group->get( 'id' ) . "\n AND " . $_CB_database->NameQuote( 'accepted' ) . ' = ' . $_CB_database->Quote( '0000-00-00 00:00:00' ) . "\n AND ( ( " . $_CB_database->NameQuote( 'email' ) . ' = ' . $_CB_database->Quote( $user->get( 'email' ) ) . ' AND ' . $_CB_database->NameQuote( 'email' ) . ' != "" )' . ' OR ( ' . $_CB_database->NameQuote( 'user' ) . ' = ' . (int) $user->get( 'id' ) . ' AND ' . $_CB_database->NameQuote( 'user' ) . ' > 0 ) )'; $_CB_database->setQuery( $query ); $invites = $_CB_database->loadObjectList( null, '\CB\Plugin\GroupJive\Table\InviteTable', array( $_CB_database ) ); if ( $row->get( 'type' ) == 3 ) { if ( ! $invites ) { CBGroupJive::returnRedirect( $returnUrl, CBTxt::T( 'You have not been invited to join this group.' ), 'error' ); } } $row->set( 'user_id', (int) $user->get( 'id' ) ); $row->set( 'group', (int) $group->get( 'id' ) ); $row->set( 'status', ( $isModerator ? 1 : ( $group->get( 'type' ) == 2 ? 0 : 1 ) ) ); $_PLUGINS->trigger( 'gj_onBeforeJoinGroup', array( &$row, $group, $user ) ); if ( $row->getError() || ( ! $row->check() ) ) { CBGroupJive::returnRedirect( $returnUrl, CBTxt::T( 'GROUP_JOIN_FAILED', 'Group join failed. Error: [error]', array( '[error]' => $row->getError() ) ), 'error' ); } if ( $row->getError() || ( ! $row->store() ) ) { CBGroupJive::returnRedirect( $returnUrl, CBTxt::T( 'GROUP_JOIN_FAILED', 'Group join failed. Error: [error]', array( '[error]' => $row->getError() ) ), 'error' ); } $notified = array( $row->get( 'user_id' ) ); /** @var InviteTable[] $invites */ foreach ( $invites as $invite ) { if ( $invite->accept() && ( ! in_array( $invite->get( 'user_id' ), $notified ) ) ) { CBGroupJive::sendNotifications( 'invite_accept', CBTxt::T( 'Group invite accepted' ), CBTxt::T( 'Your group [group] invite to [user] has been accepted!' ), $group, $user, (int) $invite->get( 'user_id' ), $notified ); $notified[] = $invite->get( 'user_id' ); } } if ( $row->get( 'status' ) == 0 ) { CBGroupJive::sendNotifications( 'user_approve', CBTxt::T( 'User group join request awaiting approval' ), CBTxt::T( '[user] has joined the group [group] and is awaiting approval!' ), $group, (int) $row->get( 'user_id' ), null, $notified, 2 ); } else { CBGroupJive::sendNotifications( 'user_join', CBTxt::T( 'User joined a mutual group' ), CBTxt::T( '[user] has joined the group [group]!' ), $group, (int) $row->get( 'user_id' ), null, $notified ); } $_PLUGINS->trigger( 'gj_onAfterJoinGroup', array( $row, $group, $user ) ); if ( $row->get( 'status' ) == 0 ) { CBGroupJive::returnRedirect( $returnUrl, CBTxt::T( 'Your group join request is currently pending approval!' ) ); } else { CBGroupJive::returnRedirect( $returnUrl, CBTxt::T( 'Group joined successfully!' ) ); } }
/** * Mutator: * Prepares field data commit * Override * * @param FieldTable $field * @param UserTable $user RETURNED populated: touch only variables related to saving this field (also when not validating for showing re-edit) * @param array $postdata Typically $_POST (but not necessarily), filtering required. * @param string $reason 'edit' for save user edit, 'register' for save registration */ public function commitFieldDataSave( &$field, &$user, &$postdata, $reason ) { if ( ! in_array( $reason, array( 'register', 'edit' ) ) ) { return; } $value = $this->getValue( $field, $user, $postdata ); if ( $value ) { $groups = explode( '|*|', $value ); cbArrayToInts( $groups ); foreach ( $groups as $groupId ) { $row = new \CB\Plugin\GroupJive\Table\UserTable(); $row->load( array( 'user_id' => (int) $user->get( 'id' ), 'group' => (int) $groupId ) ); if ( $row->get( 'id' ) ) { continue; } $row->set( 'user_id', (int) $user->get( 'id' ) ); $row->set( 'group', (int) $groupId ); $row->set( 'status', 1 ); if ( $row->getError() || ( ! $row->check() ) ) { $this->_setValidationError( $field, $user, $reason, $row->getError() ); break; } if ( $row->getError() || ( ! $row->store() ) ) { $this->_setValidationError( $field, $user, $reason, $row->getError() ); break; } } } }