/**
	 * save notifications
	 *
	 * @param int       $id
	 * @param UserTable $user
	 */
	private function saveNotifications( $id, $user )
	{
		global $_CB_framework;

		$row				=	new NotificationTable();

		$isModerator		=	CBGroupJive::isModerator( $user->get( 'id' ) );

		$group				=	CBGroupJive::getGroup( $id );

		$row->load( array( 'user_id' => (int) $user->get( 'id' ), 'group' => (int) $group->get( 'id' ) ) );

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

		if ( ! CBGroupJive::canAccessGroup( $group, $user ) ) {
			cbRedirect( $returnUrl, CBTxt::T( 'Group does not exist.' ), 'error' );
		} elseif ( ! $this->params->get( 'notifications', 1 ) ) {
			cbRedirect( $returnUrl, CBTxt::T( 'You do not have access to notifications in this group.' ), 'error' );
		} elseif ( $row->get( 'id' ) && ( $user->get( 'id' ) != $row->get( 'user_id' ) ) ) {
			cbRedirect( $returnUrl, CBTxt::T( 'You do not have sufficient permissions to edit this users notifications.' ), 'error' );
		} elseif ( ! $isModerator ) {
			if ( ! CBGroupJive::canCreateGroupContent( $user, $group ) ) {
				cbRedirect( $returnUrl, CBTxt::T( 'You do not have sufficient permissions to notifications in this group.' ), 'error' );
			}
		}

		$row->set( 'user_id', $row->get( 'user_id', (int) $user->get( 'id' ) ) );
		$row->set( 'group', $row->get( 'group', (int) $group->get( 'id' ) ) );

		foreach ( $this->getInput()->subTree( 'params' ) as $k => $v ) {
			if ( is_array( $v ) || is_object( $v ) ) {
				continue;
			}

			$k				=	Get::clean( $k, GetterInterface::COMMAND );

			if ( $k ) {
				if ( is_numeric( $v ) ) {
					$v		=	(int) $this->input( 'post/params.' . $k, null, GetterInterface::INT );
				} else {
					$v		=	$this->input( 'post/params.' . $k, null, GetterInterface::STRING );
				}

				$row->params()->set( $k, $v );
			}
		}

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

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

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

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

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

		cbRedirect( $returnUrl, CBTxt::T( 'Notifications saved successfully!' ) );
	}
Example #2
0
	/**
	 * prepare frontend tab render
	 *
	 * @param TabTable  $tab
	 * @param UserTable $user
	 * @param int       $ui
	 * @return null|string
	 */
	public function getDisplayTab( $tab, $user, $ui )
	{
		global $_CB_framework, $_CB_database;

		if ( ! ( $tab->params instanceof ParamsInterface ) ) {
			$tab->params		=	new Registry( $tab->params );
		}

		$viewer					=	CBuser::getMyUserDataInstance();
		$isModerator			=	CBGroupJive::isModerator( $viewer->get( 'id' ) );
		$isOwner				=	( $viewer->get( 'id' ) == $user->get( 'id' ) );

		CBGroupJive::getTemplate( 'tab' );

		$limit					=	(int) $tab->params->get( 'tab_limit', 30 );
		$limitstart				=	$_CB_framework->getUserStateFromRequest( 'gj_tab_limitstart{com_comprofiler}', 'gj_tab_limitstart' );
		$search					=	$_CB_framework->getUserStateFromRequest( 'gj_tab_search{com_comprofiler}', 'gj_tab_search' );
		$where					=	null;

		if ( $search && $tab->params->get( 'tab_search', 1 ) ) {
			$where				.=	"\n AND ( g." . $_CB_database->NameQuote( 'name' ) . " LIKE " . $_CB_database->Quote( '%' . $_CB_database->getEscaped( $search, true ) . '%', false )
								.	" OR g." . $_CB_database->NameQuote( 'description' ) . " LIKE " . $_CB_database->Quote( '%' . $_CB_database->getEscaped( $search, true ) . '%', false ) . " )";
		}

		$searching				=	( $where ? true : false );

		$query					=	'SELECT COUNT(*)'
								.	"\n FROM " . $_CB_database->NameQuote( '#__groupjive_groups' ) . " AS g";

		if ( ! $isModerator ) {
			$query				.=	"\n LEFT JOIN " . $_CB_database->NameQuote( '#__groupjive_categories' ) . " AS c"
								.	' ON c.' . $_CB_database->NameQuote( 'id' ) . ' = g.' . $_CB_database->NameQuote( 'category' );
		}

		$query					.=	"\n LEFT JOIN " . $_CB_database->NameQuote( '#__groupjive_users' ) . " AS u"
								.	' ON u.' . $_CB_database->NameQuote( 'group' ) . ' = g.' . $_CB_database->NameQuote( 'id' )
								.	' AND u.' . $_CB_database->NameQuote( 'user_id' ) . ' = ' . (int) $user->get( 'id' )
								.	"\n LEFT JOIN " . $_CB_database->NameQuote( '#__groupjive_invites' ) . " AS i"
								.	' ON i.' . $_CB_database->NameQuote( 'group' ) . ' = g.' . $_CB_database->NameQuote( 'id' )
								.	' AND i.' . $_CB_database->NameQuote( 'accepted' ) . ' = ' . $_CB_database->Quote( '0000-00-00 00:00:00' )
								.	' AND ( ( i.' . $_CB_database->NameQuote( 'email' ) . ' = ' . $_CB_database->Quote( $user->get( 'email' ) )
								.	' AND i.' . $_CB_database->NameQuote( 'email' ) . ' != "" )'
								.	' OR ( i.' . $_CB_database->NameQuote( 'user' ) . ' = ' . (int) $user->get( 'id' )
								.	' AND i.' . $_CB_database->NameQuote( 'user' ) . ' > 0 ) )';

		if ( $isOwner ) {
			$query				.=	"\n WHERE ( g." . $_CB_database->NameQuote( 'user_id' ) . " = " . (int) $user->get( 'id' );

			if ( ! $isModerator ) {
				$query			.=		' OR ( g.' . $_CB_database->NameQuote( 'published' ) . ' = 1'
								.		' AND ( u.' . $_CB_database->NameQuote( 'status' ) . ' IN ( 0, 1, 2, 3 )'
								.		' OR i.' . $_CB_database->NameQuote( 'id' ) . ' IS NOT NULL ) ) )';
			} else {
				$query			.=		' OR u.' . $_CB_database->NameQuote( 'status' ) . ' IN ( 0, 1, 2, 3 )'
								.		' OR i.' . $_CB_database->NameQuote( 'id' ) . ' IS NOT NULL )';
			}
		} else {
			$query				.=	"\n LEFT JOIN " . $_CB_database->NameQuote( '#__groupjive_users' ) . " AS mu"
								.	' ON mu.' . $_CB_database->NameQuote( 'group' ) . ' = g.' . $_CB_database->NameQuote( 'id' )
								.	' AND mu.' . $_CB_database->NameQuote( 'user_id' ) . ' = ' . (int) $viewer->get( 'id' )
								.	"\n LEFT JOIN " . $_CB_database->NameQuote( '#__groupjive_invites' ) . " AS mi"
								.	' ON mi.' . $_CB_database->NameQuote( 'group' ) . ' = g.' . $_CB_database->NameQuote( 'id' )
								.	' AND mi.' . $_CB_database->NameQuote( 'accepted' ) . ' = ' . $_CB_database->Quote( '0000-00-00 00:00:00' )
								.	' AND ( ( mi.' . $_CB_database->NameQuote( 'email' ) . ' = ' . $_CB_database->Quote( $viewer->get( 'email' ) )
								.	' AND mi.' . $_CB_database->NameQuote( 'email' ) . ' != "" )'
								.	' OR ( mi.' . $_CB_database->NameQuote( 'user' ) . ' = ' . (int) $viewer->get( 'id' )
								.	' AND mi.' . $_CB_database->NameQuote( 'user' ) . ' > 0 ) )'
								.	"\n WHERE ( g." . $_CB_database->NameQuote( 'user_id' ) . " = " . (int) $user->get( 'id' );

			if ( ! $isModerator ) {
				$query			.=		' OR ( g.' . $_CB_database->NameQuote( 'published' ) . ' = 1'
								.		' AND u.' . $_CB_database->NameQuote( 'status' ) . ' IN ( 1, 2, 3 ) ) )'
								.	"\n AND ( g." . $_CB_database->NameQuote( 'user_id' ) . " = " . (int) $viewer->get( 'id' )
								.		' OR ( g.' . $_CB_database->NameQuote( 'published' ) . ' = 1'
								.		' AND ( g.' . $_CB_database->NameQuote( 'type' ) . ' IN ( 1, 2 )'
								.		' OR mu.' . $_CB_database->NameQuote( 'status' ) . ' IN ( 0, 1, 2, 3 )'
								.		' OR mi.' . $_CB_database->NameQuote( 'id' ) . ' IS NOT NULL ) ) )';
			} else {
				$query			.=		' OR u.' . $_CB_database->NameQuote( 'status' ) . ' IN ( 1, 2, 3 ) )';
			}
		}

		if ( ! $isModerator ) {
			$query				.=	"\n AND ( ( c." . $_CB_database->NameQuote( 'published' ) . " = 1"
								.		' AND c.' . $_CB_database->NameQuote( 'access' ) . ' IN ' . $_CB_database->safeArrayOfIntegers( CBGroupJive::getAccess( (int) $user->get( 'id' ) ) )
								.		' AND c.' . $_CB_database->NameQuote( 'access' ) . ' IN ' . $_CB_database->safeArrayOfIntegers( CBGroupJive::getAccess( (int) $viewer->get( 'id' ) ) ) . ' )'
								.		( $this->params->get( 'groups_uncategorized', 1 ) ? ' OR g.' . $_CB_database->NameQuote( 'category' ) . ' = 0 )' : ' )' );
		}

		$query					.=	$where;
		$_CB_database->setQuery( $query );
		$total					=	(int) $_CB_database->loadResult();

		if ( ( ! $total ) && ( ! $searching ) && ( ( ! $isOwner ) || ( $isOwner && ( ! CBGroupJive::canCreateGroup( $user ) ) ) ) && ( ! Application::Config()->get( 'showEmptyTabs', 1 ) ) ) {
			return null;
		}

		$pageNav				=	new cbPageNav( $total, $limitstart, $limit );

		$pageNav->setInputNamePrefix( 'gj_tab_' );

		switch( (int) $tab->params->get( 'tab_orderby', 1 ) ) {
			case 2:
				$orderBy		=	'g.' . $_CB_database->NameQuote( 'ordering' ) . ' DESC';
				break;
			case 3:
				$orderBy		=	'g.' . $_CB_database->NameQuote( 'date' ) . ' ASC';
				break;
			case 4:
				$orderBy		=	'g.' . $_CB_database->NameQuote( 'date' ) . ' DESC';
				break;
			case 5:
				$orderBy		=	'g.' . $_CB_database->NameQuote( 'name' ) . ' ASC';
				break;
			case 6:
				$orderBy		=	'g.' . $_CB_database->NameQuote( 'name' ) . ' DESC';
				break;
			case 7:
				$orderBy		=	$_CB_database->NameQuote( '_users' ) . ' ASC';
				break;
			case 8:
				$orderBy		=	$_CB_database->NameQuote( '_users' ) . ' DESC';
				break;
			case 1:
			default:
				$orderBy		=	'g.' . $_CB_database->NameQuote( 'ordering' ) . ' ASC';
				break;
		}

		$users					=	'SELECT COUNT(*)'
								.	"\n FROM " . $_CB_database->NameQuote( '#__groupjive_users' ) . " AS uc"
								.	"\n LEFT JOIN " . $_CB_database->NameQuote( '#__comprofiler' ) . " AS uccb"
								.	' ON uccb.' . $_CB_database->NameQuote( 'id' ) . ' = uc.' . $_CB_database->NameQuote( 'user_id' )
								.	"\n LEFT JOIN " . $_CB_database->NameQuote( '#__users' ) . " AS ucj"
								.	' ON ucj.' . $_CB_database->NameQuote( 'id' ) . ' = uccb.' . $_CB_database->NameQuote( 'id' )
								.	"\n WHERE uc." . $_CB_database->NameQuote( 'group' ) . " = g." . $_CB_database->NameQuote( 'id' )
								.	"\n AND uccb." . $_CB_database->NameQuote( 'approved' ) . " = 1"
								.	"\n AND uccb." . $_CB_database->NameQuote( 'confirmed' ) . " = 1"
								.	"\n AND ucj." . $_CB_database->NameQuote( 'block' ) . " = 0";

		if ( ! $isModerator ) {
			$users				.=	"\n AND ( g." . $_CB_database->NameQuote( 'user_id' ) . " = " . (int) $viewer->get( 'id' )
								.		( ! $isOwner ? ' OR mu.' . $_CB_database->NameQuote( 'status' ) . ' >= 2' : null )
								.		' OR uc.' . $_CB_database->NameQuote( 'status' ) . ' >= 1 )';
		}

		if ( ! $this->params->get( 'groups_users_owner', 1 ) ) {
			$users				.=	"\n AND uc." . $_CB_database->NameQuote( 'status' ) . " != 4";
		}

		$query					=	'SELECT g.*'
								.	', c.' . $_CB_database->NameQuote( 'name' ) . ' AS _category_name';

		if ( $isOwner ) {
			$query				.=	', u.' . $_CB_database->NameQuote( 'status' ) . ' AS _user_status'
								.	', i.' . $_CB_database->NameQuote( 'id' ) . ' AS _invite_id';
		} else {
			$query				.=	', mu.' . $_CB_database->NameQuote( 'status' ) . ' AS _user_status'
								.	', mi.' . $_CB_database->NameQuote( 'id' ) . ' AS _invite_id';
		}

		$query					.=	', ( ' . $users . ' ) AS _users'
								.	"\n FROM " . $_CB_database->NameQuote( '#__groupjive_groups' ) . " AS g"
								.	"\n LEFT JOIN " . $_CB_database->NameQuote( '#__groupjive_categories' ) . " AS c"
								.	' ON c.' . $_CB_database->NameQuote( 'id' ) . ' = g.' . $_CB_database->NameQuote( 'category' )
								.	"\n LEFT JOIN " . $_CB_database->NameQuote( '#__groupjive_users' ) . " AS u"
								.	' ON u.' . $_CB_database->NameQuote( 'user_id' ) . ' = ' . (int) $user->get( 'id' )
								.	' AND u.' . $_CB_database->NameQuote( 'group' ) . ' = g.' . $_CB_database->NameQuote( 'id' )
								.	"\n LEFT JOIN " . $_CB_database->NameQuote( '#__groupjive_invites' ) . " AS i"
								.	' ON i.' . $_CB_database->NameQuote( 'group' ) . ' = g.' . $_CB_database->NameQuote( 'id' )
								.	' AND i.' . $_CB_database->NameQuote( 'accepted' ) . ' = ' . $_CB_database->Quote( '0000-00-00 00:00:00' )
								.	' AND ( ( i.' . $_CB_database->NameQuote( 'email' ) . ' = ' . $_CB_database->Quote( $user->get( 'email' ) )
								.	' AND i.' . $_CB_database->NameQuote( 'email' ) . ' != "" )'
								.	' OR ( i.' . $_CB_database->NameQuote( 'user' ) . ' = ' . (int) $user->get( 'id' )
								.	' AND i.' . $_CB_database->NameQuote( 'user' ) . ' > 0 ) )';

		if ( $isOwner ) {
			$query				.=	"\n WHERE ( g." . $_CB_database->NameQuote( 'user_id' ) . " = " . (int) $user->get( 'id' );

			if ( ! $isModerator ) {
				$query			.=		' OR ( g.' . $_CB_database->NameQuote( 'published' ) . ' = 1'
								.		' AND ( u.' . $_CB_database->NameQuote( 'status' ) . ' IN ( 0, 1, 2, 3 )'
								.		' OR i.' . $_CB_database->NameQuote( 'id' ) . ' IS NOT NULL ) ) )';
			} else {
				$query			.=		' OR u.' . $_CB_database->NameQuote( 'status' ) . ' IN ( 0, 1, 2, 3 )'
								.		' OR i.' . $_CB_database->NameQuote( 'id' ) . ' IS NOT NULL )';
			}
		} else {
			$query				.=	"\n LEFT JOIN " . $_CB_database->NameQuote( '#__groupjive_users' ) . " AS mu"
								.	' ON mu.' . $_CB_database->NameQuote( 'user_id' ) . ' = ' . (int) $viewer->get( 'id' )
								.	' AND mu.' . $_CB_database->NameQuote( 'group' ) . ' = g.' . $_CB_database->NameQuote( 'id' )
								.	"\n LEFT JOIN " . $_CB_database->NameQuote( '#__groupjive_invites' ) . " AS mi"
								.	' ON mi.' . $_CB_database->NameQuote( 'group' ) . ' = g.' . $_CB_database->NameQuote( 'id' )
								.	' AND mi.' . $_CB_database->NameQuote( 'accepted' ) . ' = ' . $_CB_database->Quote( '0000-00-00 00:00:00' )
								.	' AND ( ( mi.' . $_CB_database->NameQuote( 'email' ) . ' = ' . $_CB_database->Quote( $viewer->get( 'email' ) )
								.	' AND mi.' . $_CB_database->NameQuote( 'email' ) . ' != "" )'
								.	' OR ( mi.' . $_CB_database->NameQuote( 'user' ) . ' = ' . (int) $viewer->get( 'id' )
								.	' AND mi.' . $_CB_database->NameQuote( 'user' ) . ' > 0 ) )'
								.	"\n WHERE ( g." . $_CB_database->NameQuote( 'user_id' ) . " = " . (int) $user->get( 'id' );

			if ( ! $isModerator ) {
				$query			.=		' OR ( g.' . $_CB_database->NameQuote( 'published' ) . ' = 1'
								.		' AND u.' . $_CB_database->NameQuote( 'status' ) . ' IN ( 1, 2, 3 ) ) )'
								.	"\n AND ( g." . $_CB_database->NameQuote( 'user_id' ) . " = " . (int) $viewer->get( 'id' )
								.		' OR ( g.' . $_CB_database->NameQuote( 'published' ) . ' = 1'
								.		' AND ( g.' . $_CB_database->NameQuote( 'type' ) . ' IN ( 1, 2 )'
								.		' OR mu.' . $_CB_database->NameQuote( 'status' ) . ' IN ( 0, 1, 2, 3 )'
								.		' OR mi.' . $_CB_database->NameQuote( 'id' ) . ' IS NOT NULL ) ) )';
			} else {
				$query			.=		' OR u.' . $_CB_database->NameQuote( 'status' ) . ' IN ( 1, 2, 3 ) )';
			}
		}

		if ( ! $isModerator ) {
			$query				.=	"\n AND ( ( c." . $_CB_database->NameQuote( 'published' ) . " = 1"
								.		' AND c.' . $_CB_database->NameQuote( 'access' ) . ' IN ' . $_CB_database->safeArrayOfIntegers( CBGroupJive::getAccess( (int) $user->get( 'id' ) ) )
								.		' AND c.' . $_CB_database->NameQuote( 'access' ) . ' IN ' . $_CB_database->safeArrayOfIntegers( CBGroupJive::getAccess( (int) $viewer->get( 'id' ) ) ) . ' )'
								.		( $this->params->get( 'groups_uncategorized', 1 ) ? ' OR g.' . $_CB_database->NameQuote( 'category' ) . ' = 0 )' : ' )' );
		}

		$query					.=	$where
								.	"\n ORDER BY " . $orderBy;
		if ( $tab->params->get( 'tab_paging', 1 ) ) {
			$_CB_database->setQuery( $query, $pageNav->limitstart, $pageNav->limit );
		} else {
			$_CB_database->setQuery( $query );
		}
		$rows					=	$_CB_database->loadObjectList( null, '\CB\Plugin\GroupJive\Table\GroupTable', array( $_CB_database ) );

		$input['search']		=	'<input type="text" name="gj_tab_search" value="' . htmlspecialchars( $search ) . '" onchange="document.gjTabForm.submit();" placeholder="' . htmlspecialchars( CBTxt::T( 'Search Groups...' ) ) . '" class="form-control" />';

		CBGroupJive::getGroup( $rows );
		CBGroupJive::preFetchUsers( $rows );

		$class					=	$this->params->get( 'general_class', null );

		$return					=	'<div class="cbGroupJive' . ( $class ? ' ' . htmlspecialchars( $class ) : null ) . '">'
								.		'<div class="cbGroupJiveInner">'
								.			HTML_groupjiveTab::showTab( $rows, $pageNav, $searching, $input, $viewer, $user, $tab, $this )
								.		'</div>'
								.	'</div>';

		return $return;
	}
Example #3
0
	/**
	 * @return GroupTable
	 */
	public function group()
	{
		return CBGroupJive::getGroup( (int) $this->get( 'group' ) );
	}
	/**
	 * save wall
	 *
	 * @param int       $id
	 * @param UserTable $user
	 */
	private function saveWallEdit( $id, $user )
	{
		global $_CB_framework;

		$row					=	CBGroupJiveWall::getPost( (int) $id );
		$isModerator			=	CBGroupJive::isModerator( $user->get( 'id' ) );
		$groupId				=	$this->input( 'group', null, GetterInterface::INT );

		if ( $groupId === null ) {
			$group				=	$row->group();
		} else {
			$group				=	CBGroupJive::getGroup( $groupId );
		}

		$returnUrl				=	$_CB_framework->pluginClassUrl( $this->_gjPlugin->element, false, array( 'action' => 'groups', 'func' => 'show', 'id' => (int) $group->get( 'id' ) ) );

		if ( ! CBGroupJive::canAccessGroup( $group, $user ) ) {
			cbRedirect( $returnUrl, CBTxt::T( 'Group does not exist.' ), 'error' );
		} elseif ( ! $isModerator ) {
			if ( ( ! $row->get( 'id' ) ) && ( ! CBGroupJive::canCreateGroupContent( $user, $group, 'wall' ) ) ) {
				cbRedirect( $returnUrl, CBTxt::T( 'You do not have sufficient permissions to post in this group.' ), 'error' );
			} elseif ( $row->get( 'id' ) && ( $user->get( 'id' ) != $row->get( 'user_id' ) ) && ( CBGroupJive::getGroupStatus( $user, $group ) < 2 ) ) {
				cbRedirect( $returnUrl, CBTxt::T( 'You do not have sufficient permissions to edit this post.' ), 'error' );
			}
		}

		$replyId				=	(int) $this->input( 'reply', null, GetterInterface::INT );

		if ( $replyId ) {
			$reply				=	CBGroupJiveWall::getPost( (int) $replyId );

			if ( ! $reply->get( 'id' ) ) {
				cbRedirect( $returnUrl, CBTxt::T( 'Reply does not exist.' ), 'error' );
			}

			$row->set( 'reply', (int) $reply->get( 'id' ) );
		}

		$canModerate			=	( CBGroupJive::getGroupStatus( $user, $group ) >= 2 );

		if ( $isModerator ) {
			$row->set( 'user_id', (int) $this->input( 'post/user_id', $row->get( 'user_id', $user->get( 'id' ) ), GetterInterface::INT ) );
		} else {
			$row->set( 'user_id', (int) $row->get( 'user_id', $user->get( 'id' ) ) );
		}

		$row->set( 'published', ( $isModerator || $canModerate || ( $row->get( 'id' ) && ( $row->get( 'published' ) != -1 ) ) || ( $group->params()->get( 'wall', 1 ) != 2 ) ? (int) $this->input( 'post/published', $row->get( 'published', 1 ), GetterInterface::INT ) : -1 ) );
		$row->set( 'group', (int) $group->get( 'id' ) );

		$postLimit				=	( $isModerator ? 0 : (int) $this->params->get( 'groups_wall_character_limit', 400 ) );
		$post					=	trim( $this->input( 'post', $row->get( 'post', null, GetterInterface::HTML ), GetterInterface::HTML ) );

		// Remove duplicate spaces:
		$post					=	preg_replace( '/ {2,}/i', ' ', $post );
		// Remove duplicate tabs:
		$post					=	preg_replace( '/\t{2,}/i', "\t", $post );
		// Remove duplicate linebreaks:
		$post					=	preg_replace( '/(\r\n|\r|\n){2,}/i', '$1', $post );

		if ( $postLimit && ( cbutf8_strlen( $post ) > $postLimit ) ) {
			$post				=	cbutf8_substr( $post, 0, $postLimit );
		}

		$row->set( 'post', $post );

		$new					=	( $row->get( 'id' ) ? false : true );

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

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

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

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

		if ( $new ) {
			if ( $row->get( 'published' ) == 1 ) {
				if ( $row->reply()->get( 'id' ) ) {
					CBGroupJive::sendNotifications( 'wall_reply', CBTxt::T( 'New group post reply' ), CBTxt::T( '[user] has posted a reply on the wall in the group [group]!' ), $row->group(), (int) $row->get( 'user_id' ), (int) $row->reply()->get( 'user_id' ), array( $user->get( 'id' ) ) );
				} else {
					CBGroupJive::sendNotifications( 'wall_new', CBTxt::T( 'New group post' ), CBTxt::T( '[user] has posted on the wall in the group [group]!' ), $row->group(), (int) $row->get( 'user_id' ), null, array( $user->get( 'id' ) ) );
				}
			} elseif ( ( $row->get( 'published' ) == -1 ) && ( $row->group()->params()->get( 'wall', 1 ) == 2 ) ) {
				CBGroupJive::sendNotifications( 'wall_approve', CBTxt::T( 'New group post awaiting approval' ), CBTxt::T( '[user] has posted on the wall in the group [group] and is awaiting approval!' ), $row->group(), (int) $row->get( 'user_id' ), null, array( $user->get( 'id' ) ) );
			}

			cbRedirect( $returnUrl, CBTxt::T( 'Posted successfully!' ) );
		} else {
			cbRedirect( $returnUrl, CBTxt::T( 'Post saved successfully!' ) );
		}
	}
	/**
	 * save event
	 *
	 * @param int       $id
	 * @param UserTable $user
	 */
	private function saveEventEdit( $id, $user )
	{
		global $_CB_framework, $_PLUGINS;

		$row					=	CBGroupJiveEvents::getEvent( (int) $id );
		$isModerator			=	CBGroupJive::isModerator( $user->get( 'id' ) );
		$groupId				=	$this->input( 'group', null, GetterInterface::INT );

		if ( $groupId === null ) {
			$group				=	$row->group();
		} else {
			$group				=	CBGroupJive::getGroup( $groupId );
		}

		$returnUrl				=	$_CB_framework->pluginClassUrl( $this->_gjPlugin->element, false, array( 'action' => 'groups', 'func' => 'show', 'id' => (int) $group->get( 'id' ) ) );

		if ( ! CBGroupJive::canAccessGroup( $group, $user ) ) {
			cbRedirect( $returnUrl, CBTxt::T( 'Group does not exist.' ), 'error' );
		} elseif ( ! $isModerator ) {
			if ( ( ! $row->get( 'id' ) ) && ( ! CBGroupJive::canCreateGroupContent( $user, $group, 'events' ) ) ) {
				cbRedirect( $returnUrl, CBTxt::T( 'You do not have sufficient permissions to schedule an event in this group.' ), 'error' );
			} elseif ( $row->get( 'id' ) && ( $user->get( 'id' ) != $row->get( 'user_id' ) ) && ( CBGroupJive::getGroupStatus( $user, $group ) < 2 ) ) {
				cbRedirect( $returnUrl, CBTxt::T( 'You do not have sufficient permissions to edit this event.' ), 'error' );
			}
		}

		if ( $isModerator ) {
			$row->set( 'user_id', (int) $this->input( 'post/user_id', $row->get( 'user_id', $user->get( 'id' ) ), GetterInterface::INT ) );
		} else {
			$row->set( 'user_id', (int) $row->get( 'user_id', $user->get( 'id' ) ) );
		}

		$canModerate			=	( CBGroupJive::getGroupStatus( $user, $group ) >= 2 );

		$currentTitle			=	$row->get( 'title' );
		$currentEvent			=	$row->get( 'event' );
		$currentLocation		=	$row->get( 'location' );
		$currentAddress			=	$row->get( 'address' );
		$currentStart			=	$row->get( 'start', '0000-00-00 00:00:00' );
		$currentEnd				=	$row->get( 'end', '0000-00-00 00:00:00' );

		$newStart				=	$this->input( 'post/start', $currentStart, GetterInterface::STRING );
		$newEnd					=	$this->input( 'post/end', $currentEnd, GetterInterface::STRING );

		$row->set( 'published', ( $isModerator || $canModerate || ( $row->get( 'id' ) && ( $row->get( 'published' ) != -1 ) ) || ( $group->params()->get( 'events', 1 ) != 2 ) ? (int) $this->input( 'post/published', $row->get( 'published', 1 ), GetterInterface::INT ) : -1 ) );
		$row->set( 'group', (int) $group->get( 'id' ) );
		$row->set( 'title', $this->input( 'post/title', $currentTitle, GetterInterface::STRING ) );
		$row->set( 'event', $this->input( 'post/event', $currentEvent, GetterInterface::HTML ) );
		$row->set( 'location', $this->input( 'post/location', $currentLocation, GetterInterface::STRING ) );
		$row->set( 'address', $this->input( 'post/address', $currentAddress, GetterInterface::STRING ) );
		$row->set( 'start', ( $newStart ? $newStart : '0000-00-00 00:00:00' ) );
		$row->set( 'end', ( $newEnd ? $newEnd : '0000-00-00 00:00:00' ) );
		$row->set( 'limit', (int) $this->input( 'post/limit', $row->get( 'limit' ), GetterInterface::INT ) );

		if ( ( ! $isModerator ) && $this->params->get( 'groups_events_captcha', 0 ) ) {
			$_PLUGINS->loadPluginGroup( 'user' );

			$_PLUGINS->trigger( 'onCheckCaptchaHtmlElements', array() );

			if ( $_PLUGINS->is_errors() ) {
				$row->setError( $_PLUGINS->getErrorMSG() );
			}
		}

		$new					=	( $row->get( 'id' ) ? false : true );

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

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

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

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

		$extras					=	array(	'event_title'	=>	htmlspecialchars( $row->get( 'title' ) ),
											'event'			=>	'<a href="' . $_CB_framework->pluginClassUrl( $this->_gjPlugin->element, false, array( 'action' => 'groups', 'func' => 'show', 'id' => (int) $row->get( 'group' ), 'tab' => 'grouptabevents' ) ) . '">' . htmlspecialchars( CBTxt::T( $row->get( 'title' ) ) ) . '</a>' );

		if ( $new ) {
			if ( $row->get( 'published' ) == 1 ) {
				CBGroupJive::sendNotifications( 'event_new', CBTxt::T( 'New group event' ), CBTxt::T( '[user] has scheduled the event [event] in the group [group]!' ), $row->group(), (int) $row->get( 'user_id' ), null, array( $user->get( 'id' ) ), 1, $extras );
			} elseif ( ( $row->get( 'published' ) == -1 ) && ( $row->group()->params()->get( 'events', 1 ) == 2 ) ) {
				CBGroupJive::sendNotifications( 'event_approve', CBTxt::T( 'New group event awaiting approval' ), CBTxt::T( '[user] has scheduled the event [event] in the group [group] and is awaiting approval!' ), $row->group(), (int) $row->get( 'user_id' ), null, array( $user->get( 'id' ) ), 1, $extras );
			}

			cbRedirect( $returnUrl, CBTxt::T( 'Event scheduled successfully!' ) );
		} else {
			if ( ( $row->get( 'published' ) == 1 ) && ( ( $row->get( 'title' ) != $currentTitle ) || ( $row->get( 'event' ) != $currentEvent ) || ( $row->get( 'location' ) != $currentLocation ) || ( $row->get( 'address' ) != $currentAddress ) || ( $row->get( 'start' ) != $currentStart ) || ( $row->get( 'end' ) != $currentEnd ) ) ) {
				CBGroupJive::sendNotifications( 'event_edit', CBTxt::T( 'Group event changed' ), CBTxt::T( '[user] has changed the scheduled event [event] in the group [group]!' ), $row->group(), (int) $row->get( 'user_id' ), null, array( $user->get( 'id' ) ), 1, $extras );
			}

			cbRedirect( $returnUrl, CBTxt::T( 'Event saved successfully!' ) );
		}
	}
	/**
	 * save file
	 *
	 * @param int       $id
	 * @param UserTable $user
	 */
	private function saveFileEdit( $id, $user )
	{
		global $_CB_framework, $_PLUGINS;

		$row					=	CBGroupJiveFile::getFile( (int) $id );
		$isModerator			=	CBGroupJive::isModerator( $user->get( 'id' ) );
		$groupId				=	$this->input( 'group', null, GetterInterface::INT );

		if ( $groupId === null ) {
			$group				=	$row->group();
		} else {
			$group				=	CBGroupJive::getGroup( $groupId );
		}

		$returnUrl				=	$_CB_framework->pluginClassUrl( $this->_gjPlugin->element, false, array( 'action' => 'groups', 'func' => 'show', 'id' => (int) $group->get( 'id' ) ) );

		if ( ! CBGroupJive::canAccessGroup( $group, $user ) ) {
			cbRedirect( $returnUrl, CBTxt::T( 'Group does not exist.' ), 'error' );
		} elseif ( ! $isModerator ) {
			if ( ( ! $row->get( 'id' ) ) && ( ! CBGroupJive::canCreateGroupContent( $user, $group, 'file' ) ) ) {
				cbRedirect( $returnUrl, CBTxt::T( 'You do not have sufficient permissions to upload a file in this group.' ), 'error' );
			} elseif ( $row->get( 'id' ) && ( $user->get( 'id' ) != $row->get( 'user_id' ) ) && ( CBGroupJive::getGroupStatus( $user, $group ) < 2 ) ) {
				cbRedirect( $returnUrl, CBTxt::T( 'You do not have sufficient permissions to edit this file.' ), 'error' );
			}
		}

		if ( $isModerator ) {
			$row->set( 'user_id', (int) $this->input( 'post/user_id', $row->get( 'user_id', $user->get( 'id' ) ), GetterInterface::INT ) );
		} else {
			$row->set( 'user_id', (int) $row->get( 'user_id', $user->get( 'id' ) ) );
		}

		$canModerate			=	( CBGroupJive::getGroupStatus( $user, $group ) >= 2 );

		$row->set( 'published', ( $isModerator || $canModerate || ( $row->get( 'id' ) && ( $row->get( 'published' ) != -1 ) ) || ( $group->params()->get( 'file', 1 ) != 2 ) ? (int) $this->input( 'post/published', $row->get( 'published', 1 ), GetterInterface::INT ) : -1 ) );
		$row->set( 'group', (int) $group->get( 'id' ) );
		$row->set( 'title', $this->input( 'post/title', $row->get( 'title' ), GetterInterface::STRING ) );
		$row->set( 'description', $this->input( 'post/description', $row->get( 'description' ), GetterInterface::STRING ) );

		if ( ( ! $isModerator ) && $this->params->get( 'groups_file_captcha', 0 ) ) {
			$_PLUGINS->loadPluginGroup( 'user' );

			$_PLUGINS->trigger( 'onCheckCaptchaHtmlElements', array() );

			if ( $_PLUGINS->is_errors() ) {
				$row->setError( $_PLUGINS->getErrorMSG() );
			}
		}

		$new					=	( $row->get( 'id' ) ? false : true );

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

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

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

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

		if ( $new ) {
			$extras				=	array(	'file_title'	=>	htmlspecialchars( ( $row->get( 'title' ) ? $row->get( 'title' ) : $row->name() ) ),
											'file'			=>	'<a href="' . $_CB_framework->pluginClassUrl( $this->_gjPlugin->element, false, array( 'action' => 'groups', 'func' => 'show', 'id' => (int) $row->get( 'group' ), 'tab' => 'grouptabfile' ) ) . '">' . htmlspecialchars( ( $row->get( 'title' ) ? $row->get( 'title' ) : $row->name() ) ) . '</a>' );

			if ( $row->get( 'published' ) == 1 ) {
				CBGroupJive::sendNotifications( 'file_new', CBTxt::T( 'New group file' ), CBTxt::T( '[user] has uploaded the file [file] in the group [group]!' ), $row->group(), (int) $row->get( 'user_id' ), null, array( $user->get( 'id' ) ), 1, $extras );
			} elseif ( ( $row->get( 'published' ) == -1 ) && ( $row->group()->params()->get( 'file', 1 ) == 2 ) ) {
				CBGroupJive::sendNotifications( 'file_approve', CBTxt::T( 'New group file awaiting approval' ), CBTxt::T( '[user] has uploaded the file [file] in the group [group] and is awaiting approval!' ), $row->group(), (int) $row->get( 'user_id' ), null, array( $user->get( 'id' ) ), 1, $extras );
			}

			cbRedirect( $returnUrl, CBTxt::T( 'File uploaded successfully!' ) );
		} else {
			cbRedirect( $returnUrl, CBTxt::T( 'File saved successfully!' ) );
		}
	}
	/**
	 * save about
	 *
	 * @param int       $id
	 * @param UserTable $user
	 */
	private function saveAboutEdit( $id, $user )
	{
		global $_CB_framework;

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

		if ( CBGroupJive::canAccessGroup( $row, $user ) ) {
			if ( ! CBGroupJive::isModerator( $user->get( 'id' ) ) ) {
				if ( CBGroupJive::getGroupStatus( $user, $row ) < 3 ) {
					cbRedirect( $returnUrl, CBTxt::T( 'You do not have sufficient permissions to edit about in this group.' ), 'error' );
				}
			}
		} else {
			cbRedirect( $returnUrl, CBTxt::T( 'Group does not exist.' ), 'error' );
		}

		$row->params()->set( 'about_content', trim( $this->input( 'post/about', $row->params()->get( 'about_content' ), GetterInterface::HTML ) ) );

		$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->showAboutEdit( $id, $user );
			return;
		}

		cbRedirect( $returnUrl, CBTxt::T( 'Group about saved successfully!' ) );
	}
	/**
	 * integrates with kunena model
	 *
	 * @param string $event
	 * @param $config
	 * @param $params
	 */
	public function kunena( $event, &$config, &$params )
	{
		global $_CB_database;

		if ( ( ! $this->_forumModel ) || ( $this->_forumModel->type != 'kunena' ) ) {
			return;
		}

		if ( $event == 'loadGroups' ) {
			$groups									=	CBGroupJive::getGroupOptions();
			$options								=	array();

			foreach ( $groups as $group ) {
				$option								=	new stdClass();
				$option->id							=	( is_array( $group->value ) ? uniqid() : (int) $group->value );
				$option->parent_id					=	0;
				$option->level						=	( is_array( $group->value ) ? 0 : 1 );
				$option->name						=	$group->text;

				$options[$option->id]				=	$option;
			}

			$params['groups']						=	$options;
		} elseif ( $event == 'getAllowedForumsRead' ) {
			static $cache							=	array();

			$mydId									=	Application::MyUser()->getUserId();

			if ( ! $mydId ) {
				return;
			}

			if ( ! isset( $cache[$mydId] ) ) {
				$user								=	CBuser::getMyUserDataInstance();
				$isModerator						=	CBGroupJive::isModerator( $user->get( 'id' ) );

				$query								=	'SELECT g.*'
													.	"\n FROM " . $_CB_database->NameQuote( '#__groupjive_groups' ) . " AS g"
													.	"\n LEFT JOIN " . $_CB_database->NameQuote( '#__comprofiler' ) . " AS cb"
													.	' ON cb.' . $_CB_database->NameQuote( 'id' ) . ' = g.' . $_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 LEFT JOIN " . $_CB_database->NameQuote( '#__groupjive_users' ) . " AS u"
													.	' ON u.' . $_CB_database->NameQuote( 'user_id' ) . ' = ' . (int) $user->get( 'id' )
													.	' AND u.' . $_CB_database->NameQuote( 'group' ) . ' = g.' . $_CB_database->NameQuote( 'id' )
													.	"\n WHERE cb." . $_CB_database->NameQuote( 'approved' ) . " = 1"
													.	"\n AND cb." . $_CB_database->NameQuote( 'confirmed' ) . " = 1"
													.	"\n AND j." . $_CB_database->NameQuote( 'block' ) . " = 0";

				if ( ! $isModerator ) {
					$query							.=	"\n AND ( g." . $_CB_database->NameQuote( 'user_id' ) . " = " . (int) $user->get( 'id' )
													.		' OR ( g.' . $_CB_database->NameQuote( 'published' ) . ' = 1'
													.		' AND u.' . $_CB_database->NameQuote( 'status' ) . ' > 0 ) )';
				}

				$_CB_database->setQuery( $query );
				$groups								=	$_CB_database->loadObjectList( null, '\CB\Plugin\GroupJive\Table\GroupTable', array( $_CB_database ) );

				$allowed							=	array();

				/** @var GroupTable[] $groups */
				foreach ( $groups as $group ) {
					if ( $group->params()->get( 'forums', 1 ) ) {
						$froumId					=	(int) $group->params()->get( 'forum_id' );

						if ( $froumId && CBGroupJive::canCreateGroupContent( $user, $group, 'forums' ) ) {
							$allowed[]				=	$froumId;
						}
					}
				}

				$cache[$mydId]						=	$allowed;
			}

			if ( ! $cache[$mydId] ) {
				return;
			}

			$existingAccess							=	explode( ',', $params[1] );
			$cleanAccess							=	array_diff( $cache[$mydId], $existingAccess );
			$newAccess								=	array_merge( $existingAccess, $cleanAccess );

			cbArrayToInts( $newAccess );

			$params[1]								=	implode( ',', $newAccess );
		} elseif ( $event == 'authoriseUsers' ) {
			/** @var KunenaForumCategory $category */
			$category								=	$params['category'];
			$groupId								=	$category->get( 'access' );

			if ( ( $category->get( 'accesstype' ) != 'communitybuilder' ) || ( ! $groupId ) ) {
				return;
			}

			$users									=	$params['userids'];

			if ( ! $users ) {
				return;
			}

			static $allowed							=	array();

			if ( ! isset( $allowed[$groupId] ) ) {
				$allowed[$groupId]					=	array();

				$group								=	CBGroupJive::getGroup( $groupId );

				if ( $group->get( 'id' ) ) {
					$query							=	'SELECT u.' . $_CB_database->NameQuote( 'user_id' )
													.	"\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( 'group' ) . " = " . (int) $group->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' ) . " >= 1";
					$_CB_database->setQuery( $query );
					$allowed[$groupId]				=	$_CB_database->loadResultArray();
				}

				foreach ( $users as $userId ) {
					if ( ( ! in_array( $userId, $allowed[$groupId] ) ) && CBGroupJive::isModerator( $userId ) ) {
						$allowed[$groupId][]		=	$userId;
					}
				}

				cbArrayToInts( $allowed[$groupId] );
			}

			if ( ! $allowed[$groupId] ) {
				return;
			}

			$params['allow']						=	$allowed[$groupId];
		} elseif ( $this->params->get( 'groups_forums_back', 1 ) && ( $event == 'onStart' ) && ( $this->input( 'view', null, GetterInterface::STRING ) == 'category' ) ) {
			$categoryId								=	(int) $this->input( 'catid', 0, GetterInterface::INT );

			if ( ! $categoryId ) {
				return;
			}

			$model									=	CBGroupJiveForums::getModel();

			if ( ! $model ) {
				return;
			}

			$category								=	$model->getCategory( $categoryId );

			if ( ! $category->get( 'id' ) ) {
				return;
			}

			$category								=	$category->category();

			if ( ( $category->get( 'accesstype' ) != 'communitybuilder' ) || ( ! $category->get( 'access' ) ) ) {
				return;
			}

			$group									=	CBGroupJive::getGroup( (int) $category->get( 'access' ) );

			if ( ! $group->get( 'id' ) ) {
				return;
			}

			CBGroupJive::getTemplate( 'backlink', true, true, $this->element );

			echo HTML_groupjiveForumsBacklink::showBacklink( $group, $category, $this );
		}
	}