/**
	 * 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!' ) );
		}
	}
 /**
  * CB's own UTF-8-compatible output-charset-dependent substr()
  *
  * @param  string  $str
  * @param  int     $start
  * @param  int     $length
  * @return string
  */
 function cbIsoUtf_substr($str, $start, $length = null)
 {
     global $_CB_framework;
     if ($_CB_framework->outputCharset() == 'UTF-8') {
         return cbutf8_substr($str, $start, $length);
     } else {
         if ($length === null) {
             return substr($str, $start);
         } else {
             return substr($str, $start, $length);
         }
     }
 }
	/**
	 * Saves activity
	 *
	 * @param int       $id
	 * @param Activity  $stream
	 * @param UserTable $user
	 * @param UserTable $viewer
	 */
	private function saveActivity( $id, $stream, $user, $viewer )
	{
		global $_CB_framework, $_PLUGINS;

		$cbModerator					=	CBActivity::isModerator( (int) $viewer->get( 'id' ) );

		CBActivity::getTemplate( 'activity', false, false );

		$row							=	new ActivityTable();

		$row->load( (int) $id );

		$canAccess						=	false;

		if ( ! $row->get( 'id' ) ) {
			if ( CBActivity::canCreate( $user, $viewer, $stream ) ) {
				$canAccess				=	true;
			}
		} elseif ( ( ( $row->get( 'type' ) == 'status' ) || ( $row->get( 'subtype' ) == 'status' ) ) && ( $cbModerator || ( $viewer->get( 'id' ) == $row->get( 'user_id' ) ) ) ) {
			$canAccess					=	true;
		}

		if ( ! $canAccess ) {
			header( 'HTTP/1.0 404 Not Found' );
			exit();
		}

		$messageLimit					=	( $cbModerator ? 0 : (int) $stream->get( 'message_limit', 400 ) );
		$showActions					=	(int) $stream->get( 'actions', 1 );
		$actionLimit					=	( $cbModerator ? 0 : (int) $stream->get( 'actions_message_limit', 100 ) );
		$showLocations					=	(int) $stream->get( 'locations', 1 );
		$locationLimit					=	( $cbModerator ? 0 : (int) $stream->get( 'locations_address_limit', 200 ) );
		$showLinks						=	(int) $stream->get( 'links', 1 );
		$linkLimit						=	( $cbModerator ? 0 : (int) $stream->get( 'links_link_limit', 5 ) );
		$showTags						=	(int) $stream->get( 'tags', 1 );

		$message						=	trim( $this->input( 'message', $row->get( 'message', null, GetterInterface::HTML ), GetterInterface::HTML ) );

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

		$row->set( 'user_id', $row->get( 'user_id', $viewer->get( 'id' ) ) );

		if ( $stream->get( 'type' ) && ( $stream->get( 'type' ) != 'status' ) ) {
			$row->set( 'type', $row->get( 'type', $stream->get( 'type' ) ) );
			$row->set( 'subtype', $row->get( 'subtype', 'status' ) );

			$parentDefault				=	null;
		} else {
			$row->set( 'type', $row->get( 'type', 'status' ) );

			$parentDefault				=	( $viewer->get( 'id' ) != $user->get( 'user_id' ) ? $user->get( 'user_id' ) : null );
		}

		$row->set( 'item', $row->get( 'item', $stream->get( 'item' ) ) );
		$row->set( 'parent', $row->get( 'parent', $stream->get( 'parent', $parentDefault ) ) );

		if ( $messageLimit && ( cbutf8_strlen( $message ) > $messageLimit ) ) {
			$message					=	cbutf8_substr( $message, 0, $messageLimit );
		}

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

		if ( $showActions ) {
			$action						=	$this->getInput()->subTree( 'actions' );
			$actionId					=	(int) $action->get( 'id', 0, GetterInterface::INT );
			$actionMessage				=	( $actionId ? trim( $action->get( 'message', '', GetterInterface::STRING ) ) : '' );

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

			if ( $actionLimit && ( cbutf8_strlen( $actionMessage ) > $actionLimit ) ) {
				$actionMessage			=	cbutf8_substr( $actionMessage, 0, $actionLimit );
			}

			$actionId					=	( $actionMessage ? $actionId : 0 );

			$newAction					=	array(	'id'		=>	$actionId,
													'message'	=>	( $actionId ? $actionMessage : '' ),
													'emote'		=>	( $actionId ? (int) $action->get( 'emote', 0, GetterInterface::INT ) : 0 )
												);

			$row->params()->set( 'action', $newAction );
		}

		if ( $showLocations ) {
			$location					=	$this->getInput()->subTree( 'location' );
			$locationId					=	(int) $location->get( 'id', 0, GetterInterface::INT );
			$locationPlace				=	( $locationId ? trim( $location->get( 'place', '', GetterInterface::STRING ) ) : '' );
			$locationAddress			=	( $locationId ? trim( $location->get( 'address', '', GetterInterface::STRING ) ) : '' );

			if ( $locationLimit && ( cbutf8_strlen( $locationPlace ) > $locationLimit ) ) {
				$locationPlace			=	cbutf8_substr( $locationPlace, 0, $locationLimit );
			}

			if ( $locationLimit && ( cbutf8_strlen( $locationAddress ) > $locationLimit ) ) {
				$locationAddress		=	cbutf8_substr( $locationAddress, 0, $locationLimit );
			}

			$locationId					=	( $locationPlace ? $locationId : 0 );

			$newLocation				=	array(	'id'		=>	$locationId,
													'place'		=>	( $locationId ? $locationPlace : '' ),
													'address'	=>	( $locationId ? $locationAddress : '' )
												);

			$row->params()->set( 'location', $newLocation );
		}

		if ( $showLinks ) {
			$links						=	$this->getInput()->subTree( 'links' );
			$newLinks					=	array();

			/** @var ParamsInterface[] $links */
			foreach ( $links as $i => $link ) {
				if ( $linkLimit && ( ( $i + 1 ) > $linkLimit ) ) {
					break;
				}

				$linkUrl				=	trim( $link->get( 'url', '', GetterInterface::STRING ) );

				if ( $linkUrl ) {
					$attachment			=	$stream->parser()->attachment( $linkUrl );

					if ( ! $attachment ) {
						continue;
					}

					$linkType			=	$attachment->get( 'type', '', GetterInterface::STRING );

					switch ( $linkType ) {
						case 'video':
							$linkMedia	=	$attachment->subTree( 'media' )->subTree( 'video' )->subTree( 0 );
							break;
						case 'audio':
							$linkMedia	=	$attachment->subTree( 'media' )->subTree( 'audio' )->subTree( 0 );
							break;
						case 'image':
						case 'url':
						default:
							$linkMedia	=	$attachment->subTree( 'media' )->subTree( 'image' )->subTree( 0 );
							break;
					}

					$newLinks[]			=	array(	'url'			=>	$linkUrl,
													'text'			=>	null,
													'title'			=>	trim( $link->get( 'title', $attachment->subTree( 'title' )->get( 0, '', GetterInterface::STRING ), GetterInterface::STRING ) ),
													'description'	=>	trim( $link->get( 'description', $attachment->subTree( 'description' )->get( 0, '', GetterInterface::STRING ), GetterInterface::STRING ) ),
													'media'			=>	array(	'url' => $linkMedia->get( 'url', '', GetterInterface::STRING ),
																				'mimetype' => $linkMedia->get( 'mimetype', '', GetterInterface::STRING ),
																				'extension' => $linkMedia->get( 'extension', '', GetterInterface::STRING ),
																				'custom' => ''
																			),
													'type'			=>	$linkType,
													'thumbnail'		=>	$link->get( 'thumbnail', 1, GetterInterface::INT ),
													'internal'		=>	0,
												);
				}
			}

			$row->params()->set( 'links', $newLinks );
		}

		if ( $row->get( 'id' ) ) {
			$row->params()->set( 'modified', $_CB_framework->getUTCDate() );
		}

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

		if ( $row->getError() || ( ! $row->check() ) ) {
			header( 'HTTP/1.0 500 Internal Server Error' );
			exit();
		}

		if ( $row->getError() || ( ! $row->store() ) ) {
			header( 'HTTP/1.0 500 Internal Server Error' );
			exit();
		}

		if ( $showTags ) {
			$tagsStream					=	$row->tags( $stream->source() );

			if ( $tagsStream ) {
				$tags					=	$this->input( 'tags', array(), GetterInterface::RAW );

				foreach ( $tagsStream->data() as $tag ) {
					/** @var TagTable $tag */
					if ( ! in_array( $tag->get( 'user' ), $tags ) ) {
						$tag->delete();

						$tagsStream->resetData();
					} else {
						$key			=	array_search( $tag->get( 'user' ), $tags );

						if ( $key !== false ) {
							unset( $tags[$key] );
						}
					}
				}

				foreach ( $tags as $tagUser ) {
					if ( is_numeric( $tagUser ) ) {
						$tagUser		=	(int) $tagUser;
					} else {
						$tagUser		=	Get::clean( $tagUser, GetterInterface::STRING );
					}

					$tag				=	new TagTable();

					$tag->set( 'user_id', (int) $tagsStream->user()->get( 'id' ) );
					$tag->set( 'type', $tagsStream->get( 'type', null, GetterInterface::STRING ) );
					$tag->set( 'subtype', $tagsStream->get( 'subtype', null, GetterInterface::STRING ) );
					$tag->set( 'item', $tagsStream->get( 'item', null, GetterInterface::STRING ) );
					$tag->set( 'parent', $tagsStream->get( 'parent', null, GetterInterface::STRING ) );
					$tag->set( 'user', $tagUser );

					$tag->store();

					$tagsStream->resetData();
				}
			}
		}

		$rows							=	array( &$row );

		if ( $stream->get( 'comments', 1 ) ) {
			CBActivity::preFetchComments( $rows, 'activity' );
		}

		if ( $stream->get( 'tags', 1 ) ) {
			CBActivity::preFetchTags( $rows, 'activity' );
		}

		CBActivity::preFetchUsers( $rows );

		$_PLUGINS->trigger( 'activity_onPushActivity', array( $stream, $row ) );

		echo HTML_cbactivityActivity::showActivity( $rows, $stream, 4, $user, $viewer, $this );

		header( 'HTTP/1.0 200 OK' );
		exit();
	}