/**
	 * send group message
	 *
	 * @param int       $id
	 * @param UserTable $user
	 */
	private function sendMessage( $id, $user )
	{
		global $_CB_framework, $_CB_database;

		$row					=	CBGroupJive::getGroup( $id );
		$returnUrl				=	$_CB_framework->pluginClassUrl( $this->element, false, array( 'action' => 'groups', 'func' => 'show', 'id' => (int) $row->get( 'id' ) ) );

		if ( CBGroupJive::canAccessGroup( $row, $user ) ) {
			if ( ! CBGroupJive::isModerator( $user->get( 'id' ) ) ) {
				if ( ! $this->params->get( 'groups_message', 0 ) ) {
					cbRedirect( $returnUrl, CBTxt::T( 'You do not have access to messaging in this group.' ), 'error' );
				} elseif ( ( $row->get( 'published' ) == -1 ) || ( CBGroupJive::getGroupStatus( $user, $row ) < 3 ) ) {
					cbRedirect( $returnUrl, CBTxt::T( 'You do not have sufficient permissions to messaging in this group.' ), 'error' );
				} elseif ( $row->params()->get( 'messaged' ) ) {
					$seconds	=	(int) $this->params->get( 'groups_message_delay', 60 );

					if ( $seconds ) {
						$diff	=	Application::Date( 'now', 'UTC' )->diff( $row->get( 'messaged' ) );

						if ( ( $diff === false ) || ( $diff->s < $seconds ) ) {
							cbRedirect( $returnUrl, CBTxt::T( 'You can not send a message to this group at this time. Please wait awhile and try again.' ), 'error' );
						}
					}
				}
			}
		} else {
			CBGroupJive::returnRedirect( $returnUrl, CBTxt::T( 'Group does not exist.' ), 'error' );
		}

		$message				=	$this->input( 'post/message', null, GetterInterface::STRING );

		if ( ! $message ) {
			$_CB_framework->enqueueMessage( CBTxt::T( 'GROUP_MESSAGE_FAILED_TO_SEND', 'Group message failed to send! Error: [error]', array( '[error]' => CBTxt::T( 'Message not specified!' ) ) ), 'error' );

			$this->showGroupMessage( $id, $user );
			return;
		}

		$query					=	'SELECT cb.*, j.*'
								.	"\n FROM " . $_CB_database->NameQuote( '#__groupjive_users' ) . " AS u"
								.	"\n LEFT JOIN " . $_CB_database->NameQuote( '#__comprofiler' ) . " AS cb"
								.	' ON cb.' . $_CB_database->NameQuote( 'id' ) . ' = u.' . $_CB_database->NameQuote( 'user_id' )
								.	"\n LEFT JOIN " . $_CB_database->NameQuote( '#__users' ) . " AS j"
								.	' ON j.' . $_CB_database->NameQuote( 'id' ) . ' = cb.' . $_CB_database->NameQuote( 'id' )
								.	"\n WHERE u." . $_CB_database->NameQuote( 'user_id' ) . " != " . (int) $user->get( 'id' )
								.	"\n AND u." . $_CB_database->NameQuote( 'group' ) . " = " . (int) $row->get( 'id' )
								.	"\n AND cb." . $_CB_database->NameQuote( 'approved' ) . " = 1"
								.	"\n AND cb." . $_CB_database->NameQuote( 'confirmed' ) . " = 1"
								.	"\n AND j." . $_CB_database->NameQuote( 'block' ) . " = 0"
								.	"\n AND u." . $_CB_database->NameQuote( 'status' ) . " > 0";
		$_CB_database->setQuery( $query );
		$users					=	$_CB_database->loadObjectList( null, '\CB\Database\Table\UserTable', array( $_CB_database ) );

		if ( ! $users ) {
			CBGroupJive::returnRedirect( $returnUrl, CBTxt::T( 'This group has no users to message.' ) );
		} else {
			foreach ( $users as $usr ) {
				CBGroupJive::sendNotification( $this->params->get( 'groups_message_type', 2 ), $user, $usr, CBTxt::T( 'Group message' ), CBTxt::T( 'GROUP_MESSAGE', 'Group [group] has sent the following message.<p>[message]</p>', array( '[message]' => $message ) ), $row );
			}
		}

		$row->params()->set( 'messaged', Application::Database()->getUtcDateTime() );

		$row->set( 'params', $row->params()->asJson() );

		if ( $row->getError() || ( ! $row->store() ) ) {
			$_CB_framework->enqueueMessage( CBTxt::T( 'GROUP_FAILED_TO_SAVE', 'Group failed to save! Error: [error]', array( '[error]' => $row->getError() ) ), 'error' );

			$this->showGroupMessage( $id, $user );
			return;
		}

		CBGroupJive::returnRedirect( $returnUrl, CBTxt::T( 'Group messaged successfully!' ) );
	}