Esempio n. 1
0
	/**
	 * Returns an options array of available user tags with optional activity specific
	 *
	 * @param null|int|ActivityTable $activityId
	 * @param null|int               $userId
	 * @return array
	 */
	static public function loadTagOptions( $activityId = null, $userId = null )
	{
		global $_CB_database;

		/** @var ActivityTable[] $cache */
		static $cache							=	array();

		if ( $activityId && ( $userId === null ) ) {
			if ( $activityId instanceof ActivityTable ) {
				$activity						=	$activityId;
				$activityId						=	(int) $activity->get( 'id' );
			} else {
				if ( ! isset( $cache[$activityId] ) ) {
					$activity					=	new ActivityTable();

					$activity->load( (int) $activityId );

					$cache[$activityId]			=	$activity;
				}

				$activity						=	$cache[$activityId];
			}

			$userId								=	(int) $activity->get( 'user_id' );
		} elseif ( $userId === null ) {
			$userId								=	Application::MyUser()->getUserId();
		}

		static $connections						=	array();
		static $custom							=	array();
		static $options							=	array();

		if ( ! isset( $options[$userId][$activityId] ) ) {
			if ( ! isset( $connections[$userId] ) ) {
				$connectionOptions				=	array();

				if ( Application::Config()->get( 'allowConnections' ) ) {
					$cbConnection				=	new \cbConnection( $userId );

					foreach( $cbConnection->getConnectedToMe( $userId ) as $connection ) {
						$connectionOptions[]	=	\moscomprofilerHTML::makeOption( (string) $connection->id, getNameFormat( $connection->name, $connection->username, Application::Config()->get( 'name_format', 3 ) ) );
					}
				}

				$connections[$userId]			=	$connectionOptions;
			}

			if ( ! isset( $custom[$activityId] ) ) {
				$activityOptions				=	array();

				if ( $activityId ) {
					$exclude					=	array();

					foreach ( $connections[$userId] as $connection ) {
						$exclude[]				=	$connection->value;
					}

					$query						=	'SELECT *'
												.	"\n FROM " . $_CB_database->NameQuote( '#__comprofiler_plugin_activity_tags' )
												.	"\n WHERE " . $_CB_database->NameQuote( 'type' ) . " = " . $_CB_database->Quote( 'activity' )
												.	"\n AND " . $_CB_database->NameQuote( 'item' ) . " = " . (int) $activityId
												.	"\n ORDER BY " . $_CB_database->NameQuote( 'date' ) . " ASC";
					$_CB_database->setQuery( $query );
					$tags						=	$_CB_database->loadObjectList( null, '\CB\Plugin\Activity\Table\TagTable', array( $_CB_database ) );

					/** @var TagTable[] $tags */
					foreach ( $tags as $tag ) {
						if ( ! in_array( $tag->get( 'user' ), $exclude ) ) {
							$activityOptions[]	=	\moscomprofilerHTML::makeOption( (string) $tag->get( 'user' ), $tag->get( 'user' ) );
						}
					}
				}

				$custom[$activityId]			=	$activityOptions;
			}

			$options[$userId][$activityId]		=	array_merge( $connections[$userId], $custom[$activityId] );
		}

		return $options[$userId][$activityId];
	}
	/**
	 * Hides activity
	 *
	 * @param int       $id
	 * @param Activity  $stream
	 * @param UserTable $user
	 * @param UserTable $viewer
	 * @param bool      $silent
	 */
	private function hideActivity( $id, $stream, $user, $viewer, $silent = false )
	{
		$item		=	new ActivityTable();

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

		if ( ( ! $item->get( 'id' ) ) || ( ! $viewer->get( 'id' ) ) || ( $viewer->get( 'id' ) == $item->get( 'user_id' ) ) ) {
			header( 'HTTP/1.0 404 Not Found' );
			exit();
		}

		$row		=	new HiddenTable();

		$row->load( array( 'user_id' => (int) $viewer->get( 'id' ), 'type' => 'activity', 'item' => (int) $item->get( 'id' ) ) );

		if ( $row->get( 'id' ) ) {
			header( 'HTTP/1.0 200 OK' );
			exit();
		}

		$row->set( 'user_id', (int) $viewer->get( 'id' ) );
		$row->set( 'type', 'activity' );
		$row->set( 'item', (int) $item->get( 'id' ) );

		if ( ! $row->check() ) {
			header( 'HTTP/1.0 401 Unauthorized' );
			exit();
		}

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

		if ( ! $silent ) {
			$unhide		=	'<a href="' . $stream->endpoint( 'unhide', array( 'id' => (int) $item->get( 'id' ) ) ) . '" class="activityContainerUnhide streamItemAction streamItemNoticeRevert">' . CBTxt::T( 'Unhide' ) . '</a>';

			echo CBTxt::T( 'ACTIVITY_HIDDEN_UNHIDE', 'This activity has been hidden. [unhide]', array( '[unhide]' => $unhide ) );
		}

		header( 'HTTP/1.0 200 OK' );
		exit();
	}
Esempio n. 3
0
	/**
	 * @param cbautoactionsActionTable $trigger
	 * @param UserTable $user
	 * @return null|string
	 */
	public function execute( $trigger, $user )
	{
		global $_CB_framework, $_CB_database;

		if ( ! $this->installed() ) {
			if ( $trigger->getParams()->get( 'debug', false, GetterInterface::BOOLEAN ) ) {
				var_dump( CBTxt::T( 'AUTO_ACTION_ACTIVITY_NOT_INSTALLED', ':: Action [action] :: CB Activity is not installed', array( '[action]' => (int) $trigger->get( 'id' ) ) ) );
			}

			return null;
		}

		$return									=	null;

		foreach ( $trigger->getParams()->subTree( 'activity' ) as $row ) {
			/** @var ParamsInterface $row */
			$mode								=	$row->get( 'mode', 'activity', GetterInterface::STRING );
			$method								=	$row->get( 'method', 'create', GetterInterface::STRING );
			$owner								=	$row->get( 'owner', null, GetterInterface::STRING );

			if ( ! $owner ) {
				$owner							=	(int) $user->get( 'id' );
			} else {
				$owner							=	(int) $trigger->getSubstituteString( $owner );
			}

			$type								=	$trigger->getSubstituteString( $row->get( 'type', null, GetterInterface::STRING ) );
			$subtype							=	$trigger->getSubstituteString( $row->get( 'subtype', null, GetterInterface::STRING ) );
			$item								=	$trigger->getSubstituteString( $row->get( 'item', null, GetterInterface::STRING ) );
			$parent								=	$trigger->getSubstituteString( $row->get( 'parent', null, GetterInterface::STRING ) );

			if ( $mode == 'stream' ) {
				if ( $owner ) {
					$streamUser					=	CBuser::getUserDataInstance( (int) $owner );
				} else {
					$streamUser					=	CBuser::getMyUserDataInstance();
				}

				$source							=	$trigger->getSubstituteString( $row->get( 'source', null, GetterInterface::STRING ) );
				$direction						=	(int) $row->get( 'direction', 0, GetterInterface::INT );

				if ( $row->get( 'stream', 'activity', GetterInterface::STRING ) == 'comments' ) {
					$object						=	new Comments( $source, $streamUser, $direction );

					CBActivity::loadStreamDefaults( $object, $row->subTree( 'comments_stream' ), 'comments_' );
				} else {
					$object						=	new Activity( $source, $streamUser, $direction );

					CBActivity::loadStreamDefaults( $object, $row->subTree( 'activity_stream' ), 'activity_' );
				}

				if ( $type ) {
					$object->set( 'type', $type );
				}

				if ( $subtype ) {
					$object->set( 'subtype', $subtype );
				}

				if ( $item ) {
					$object->set( 'item', $item );
				}

				if ( $parent ) {
					$object->set( 'parent', $parent );
				}

				if ( $row->get( 'output', 'echo', GetterInterface::STRING ) == 'echo' ) {
					echo $object->stream( false );
				} else {
					$return						.=	$object->stream( false );
				}
			} elseif ( $method == 'delete' ) {
				$where							=	array();

				if ( $owner ) {
					$where[]					=	$_CB_database->NameQuote( 'user_id' ) . ' = ' . (int) $owner;
				}

				if ( $type ) {
					$where[]					=	$_CB_database->NameQuote( 'type' ) . ( strpos( $type, '%' ) !== false ? ' LIKE ' : ' = ' ) . $_CB_database->Quote( $type );
				}

				if ( $item ) {
					$where[]					=	$_CB_database->NameQuote( 'item' ) . ' = ' . $_CB_database->Quote( $item );
				}

				if ( $mode != 'hidden' ) {
					if ( $subtype ) {
						$where[]				=	$_CB_database->NameQuote( 'subtype' ) . ( strpos( $type, '%' ) !== false ? ' LIKE ' : ' = ' ) . $_CB_database->Quote( $subtype );
					}

					if ( $parent ) {
						$where[]				=	$_CB_database->NameQuote( 'parent' ) . ' = ' . $_CB_database->Quote( $parent );
					}
				}

				switch ( $mode ) {
					case 'hidden':
						$table					=	'#__comprofiler_plugin_activity_hidden';
						$class					=	'\CB\Plugin\Activity\Table\HiddenTable';
						break;
					case 'tag':
						$table					=	'#__comprofiler_plugin_activity_tags';
						$class					=	'\CB\Plugin\Activity\Table\TagTable';
						break;
					case 'comment':
						$table					=	'#__comprofiler_plugin_activity_comments';
						$class					=	'\CB\Plugin\Activity\Table\CommentTable';
						break;
					case 'activity':
					default:
						$table					=	'#__comprofiler_plugin_activity';
						$class					=	'\CB\Plugin\Activity\Table\ActivityTable';
						break;
				}

				$query							=	'SELECT *'
												.	"\n FROM " . $_CB_database->NameQuote( $table )
												.	( $where ? "\n WHERE " . implode( "\n AND ", $where ) : null );
				$_CB_database->setQuery( $query );
				$objects						=	$_CB_database->loadObjectList( null, $class, array( $_CB_database ) );

				/** @var TableInterface[] $objects */
				foreach ( $objects as $object ) {
					$object->delete();
				}
			} else {
				if ( ! $owner ) {
					if ( $trigger->getParams()->get( 'debug', false, GetterInterface::BOOLEAN ) ) {
						var_dump( CBTxt::T( 'AUTO_ACTION_ACTIVITY_NO_OWNER', ':: Action [action] :: CB Activity skipped due to missing owner', array( '[action]' => (int) $trigger->get( 'id' ) ) ) );
					}

					continue;
				}

				if ( ! $type ) {
					if ( $trigger->getParams()->get( 'debug', false, GetterInterface::BOOLEAN ) ) {
						var_dump( CBTxt::T( 'AUTO_ACTION_ACTIVITY_NO_TYPE', ':: Action [action] :: CB Activity skipped due to missing type', array( '[action]' => (int) $trigger->get( 'id' ) ) ) );
					}

					continue;
				}

				switch ( $mode ) {
					case 'hidden':
						$object					=	new HiddenTable( $_CB_database );
						break;
					case 'tag':
						$object					=	new TagTable( $_CB_database );
						break;
					case 'comment':
						$object					=	new CommentTable( $_CB_database );
						break;
					case 'activity':
					default:
						$object					=	new ActivityTable( $_CB_database );
						break;
				}

				if ( $item ) {
					$load						=	array( 'user_id' => $owner, 'type' => $type, 'item' => $item );

					if ( $mode != 'hidden' ) {
						if ( $subtype ) {
							$load['subtype']	=	$subtype;
						}

						if ( $parent ) {
							$load['parent']		=	$parent;
						}
					}

					$object->load( $load );
				}

				$object->set( 'user_id', $owner );

				if ( $type ) {
					$object->set( 'type', $type );
				}

				if ( $mode != 'hidden' ) {
					if ( $subtype ) {
						$object->set( 'subtype', $subtype );
					}

					if ( $parent ) {
						$object->set( 'parent', $parent );
					}
				}

				if ( $item ) {
					$object->set( 'item', $item );
				}

				if ( $mode == 'activity' ) {
					$title						=	$trigger->getSubstituteString( $row->get( 'title', null, GetterInterface::RAW ) );

					if ( $title ) {
						$object->set( 'title', $title );
					}

					$date						=	$trigger->getSubstituteString( $row->get( 'date', null, GetterInterface::STRING ) );

					if ( $date ) {
						$object->set( 'date', $_CB_framework->getUTCDate( 'Y-m-d H:i:s', $date ) );
					}

					$action						=	$row->subTree( 'action' );
					$actionId					=	$action->get( 'id', null, GetterInterface::INT );

					if ( $actionId ) {
						$actionMessage			=	$trigger->getSubstituteString( $action->get( 'message', null, GetterInterface::STRING ) );

						if ( $actionMessage ) {
							$newAction			=	array(	'id'		=>	$actionId,
															'message'	=>	$actionMessage,
															'emote'		=>	$action->get( 'emote', '', GetterInterface::STRING )
														);

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

					$location					=	$row->subTree( 'location' );
					$locationId					=	$location->get( 'id', null, GetterInterface::INT );

					if ( $locationId ) {
						$locationPlace			=	$trigger->getSubstituteString( $location->get( 'place', null, GetterInterface::STRING ) );

						if ( $locationPlace ) {
							$newLocation		=	array(	'id'		=>	$locationId,
															'place'		=>	$locationPlace,
															'address'	=>	$trigger->getSubstituteString( $location->get( 'address', null, GetterInterface::STRING ) )
														);

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

					$newLinks					=	array();

					foreach ( $row->subTree( 'links' ) as $link ) {
						/** @var ParamsInterface $link */
						$linkType				=	$trigger->getSubstituteString( $link->get( 'type', null, GetterInterface::STRING ) );
						$linkUrl				=	$trigger->getSubstituteString( $link->get( 'url', null, GetterInterface::STRING ) );

						if ( ( ! $linkType ) || ( ! $linkUrl ) ) {
							continue;
						}

						$linkMedia				=	$link->subTree( 'media' );

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

					if ( $newLinks ) {
						$object->params()->set( 'links', $newLinks );
					}

					$comments					=	$row->subTree( 'comments' );

					$object->params()->set( 'comments', array(	'display'	=>	(int) $comments->get( 'display', 1, GetterInterface::INT ),
																'source'	=>	(int) $comments->get( 'source', 1, GetterInterface::INT )
															));

					$tags						=	$row->subTree( 'tags' );

					$object->params()->set( 'tags', array(	'display'	=>	(int) $tags->get( 'display', 1, GetterInterface::INT ),
															'source'	=>	(int) $tags->get( 'source', 1, GetterInterface::INT )
														));

					$object->set( 'params', $object->params()->asJson() );
				} elseif ( $mode == 'comment' ) {
					$tags						=	$row->subTree( 'replies' );

					$object->params()->set( 'replies', array( 'display' => (int) $tags->get( 'display', 1, GetterInterface::INT ) ));

					$object->set( 'params', $object->params()->asJson() );
				}

				if ( in_array( $mode, array( 'activity', 'comment' ) ) ) {
					$message					=	$trigger->getSubstituteString( $row->get( 'message', null, GetterInterface::RAW ), false );

					if ( $message ) {
						$object->set( 'message', $message );
					}
				}

				if ( ! $object->store() ) {
					if ( $trigger->getParams()->get( 'debug', false, GetterInterface::BOOLEAN ) ) {
						var_dump( CBTxt::T( 'AUTO_ACTION_ACTIVITY_CREATE_FAILED', ':: Action [action] :: CB Activity failed to save. Error: [error]', array( '[action]' => (int) $trigger->get( 'id' ), '[error]' => $object->getError() ) ) );
					}

					continue;
				}
			}
		}

		return $return;
	}