Ejemplo n.º 1
0
	/**
	 * 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;
				}
			}
		}
	}
	/**
	 * set group user status
	 *
	 * @param int       $status
	 * @param int       $id
	 * @param UserTable $user
	 */
	private function statusUser( $status, $id, $user )
	{
		global $_CB_framework;

		$row						=	new \CB\Plugin\GroupJive\Table\UserTable();

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

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

		if ( $row->get( 'id' ) ) {
			if ( ! CBGroupJive::canAccessGroup( $row->group(), $user ) ) {
				cbRedirect( $returnUrl, CBTxt::T( 'Group does not exist.' ), 'error' );
			} elseif ( $row->get( 'user_id' ) == $row->group()->get( 'user_id' ) ) {
				cbRedirect( $returnUrl, CBTxt::T( 'You can not promote or demote the group owner.' ), 'error' );
			} elseif ( ! CBGroupJive::isModerator( $user->get( 'id' ) ) ) {
				if ( $user->get( 'id' ) == $row->get( 'user_id' ) ) {
					cbRedirect( $returnUrl, CBTxt::T( 'You can not promote or demote your self.' ), 'error' );
				} elseif ( $user->get( 'id' ) != $row->group()->get( 'user_id' ) ) {
					$userStatus		=	CBGroupJive::getGroupStatus( $user, $row->group() );

					if ( ( $row->get( 'status' ) > $userStatus ) || ( ( $userStatus <= 1 ) && in_array( $status, array( -1, 1 ) ) ) || ( ( $userStatus <= 2 ) && ( $status == 2 ) ) || in_array( $status, array( 0, 3, 4 ) ) ) {
						cbRedirect( $returnUrl, CBTxt::T( 'You do not have sufficient permissions to promote or demote this user.' ), 'error' );
					}
				}
			}
		} else {
			cbRedirect( $returnUrl, CBTxt::T( 'User does not exist.' ), 'error' );
		}

		$currentStatus				=	(int) $row->get( 'status' );

		$row->set( 'status', (int) $status );

		if ( $row->getError() || ( ! $row->store() ) ) {
			cbRedirect( $returnUrl, CBTxt::T( 'GROUP_USER_STATUS_FAILED_TO_SAVE', 'User status failed to saved. Error: [error]', array( '[error]' => $row->getError() ) ), 'error' );
		}

		if ( $status && ( $currentStatus == 0 ) ) {
			if ( $row->get( 'user_id' ) != $user->get( 'id' ) ) {
				CBGroupJive::sendNotification( 4, $user, (int) $row->get( 'user_id' ), CBTxt::T( 'Group join request accepted' ), CBTxt::T( 'Your join request to group [group] has been accepted!' ), $row->group() );
			}

			CBGroupJive::sendNotifications( 'user_join', CBTxt::T( 'User joined a mutual group' ), CBTxt::T( '[user] has joined the group [group]!' ), $row->group(), (int) $row->get( 'user_id' ), null, array( $user->get( 'id' ) ) );
		}

		cbRedirect( $returnUrl, CBTxt::T( 'User status saved successfully!' ) );
	}
Ejemplo n.º 3
0
	/**
	 * @param cbautoactionsActionTable $trigger
	 * @param UserTable $user
	 */
	public function execute( $trigger, $user )
	{
		global $_CB_database;

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

			return;
		}

		foreach ( $trigger->getParams()->subTree( 'groupjive' ) as $row ) {
			/** @var ParamsInterface $row */
			switch( (int) $row->get( 'mode', 1, GetterInterface::INT ) ) {
				case 3:
					$owner							=	$row->get( 'owner', null, GetterInterface::STRING );

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

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

						continue;
					}

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

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

						continue;
					}

					$category						=	new CategoryTable();

					$category->load( array( 'name' => $name ) );

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

						continue;
					}

					$category->set( 'published', 1 );
					$category->set( 'user_id', $owner );
					$category->set( 'name', $name );
					$category->set( 'description', $trigger->getSubstituteString( $row->get( 'description', null, GetterInterface::STRING ) ) );
					$category->set( 'access', 1 );
					$category->set( 'create_access', 0 );
					$category->set( 'types', $row->get( 'types', '1|*|2|*|3', GetterInterface::STRING ) );
					$category->set( 'ordering', 1 );

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

							continue;
						}
					}
					break;
				case 2:
					$owner							=	$row->get( 'owner', null, GetterInterface::STRING );

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

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

						continue;
					}

					$categoryId						=	(int) $row->get( 'category', -1, GetterInterface::INT );

					$category						=	new CategoryTable();

					if ( $categoryId == -1 ) {
						$name						=	$trigger->getSubstituteString( $row->get( 'category_name', null, GetterInterface::STRING ) );

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

							continue;
						}

						$category->load( array( 'name' => $name ) );

						if ( ! $category->get( 'id' ) ) {
							$category->set( 'published', 1 );
							$category->set( 'user_id', $owner );
							$category->set( 'name', $name );
							$category->set( 'description', $trigger->getSubstituteString( $row->get( 'category_description', null, GetterInterface::STRING ) ) );
							$category->set( 'access', 1 );
							$category->set( 'create_access', 0 );
							$category->set( 'types', $row->get( 'category_types', '1|*|2|*|3', GetterInterface::STRING ) );
							$category->set( 'ordering', 1 );

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

								continue;
							}
						}
					} else {
						$category->load( (int) $categoryId );
					}

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

						continue;
					}

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

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

						continue;
					}

					$group							=	new GroupTable();
					$join							=	false;

					if ( $row->get( 'unique', 1, GetterInterface::BOOLEAN ) ) {
						$group->load( array( 'category' => (int) $category->get( 'id' ), 'user_id' => (int) $owner, 'name' => $name ) );
					} else {
						$group->load( array( 'category' => (int) $category->get( 'id' ), 'name' => $name ) );

						if ( $row->get( 'autojoin', 1, GetterInterface::BOOLEAN ) ) {
							$join					=	true;
						}
					}

					if ( ! $group->get( 'id' ) ) {
						$group->set( 'published', 1 );
						$group->set( 'category', (int) $category->get( 'id' ) );
						$group->set( 'user_id', $owner );
						$group->set( 'name', $name );
						$group->set( 'description', $trigger->getSubstituteString( $row->get( 'description', null, GetterInterface::STRING ) ) );
						$group->set( 'types', (int) $row->get( 'type', 1, GetterInterface::INT ) );
						$group->set( 'ordering', 1 );

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

							continue;
						}
					} elseif ( $join ) {
						$groupUser					=	new \CB\Plugin\GroupJive\Table\UserTable( $_CB_database );

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

						if ( ! $groupUser->get( 'id' ) ) {
							$groupUser->set( 'user_id', (int) $user->get( 'id' ) );
							$groupUser->set( 'group', (int) $group->get( 'id' ) );
							$groupUser->set( 'status', (int) $row->get( 'group_status', 1, GetterInterface::INT ) );

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

								continue;
							}
						}
					}
					break;
				case 4:
					$groups							=	$row->get( 'groups', null, GetterInterface::STRING );

					if ( $groups ) {
						$groups						=	explode( '|*|', $groups );

						cbArrayToInts( $groups );

						foreach ( $groups as $groupId ) {
							$group					=	new GroupTable();

							$group->load( (int) $groupId );

							if ( $group->get( 'id' ) ) {
								$groupUser			=	new \CB\Plugin\GroupJive\Table\UserTable( $_CB_database );

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

								if ( $groupUser->get( 'id' ) && ( $groupUser->get( 'status' ) != 4 ) ) {
									$groupUser->delete();
								}
							}
						}
					}
					break;
				case 1:
				default:
					$groups							=	$row->get( 'groups', null, GetterInterface::STRING );

					if ( $groups ) {
						$groups						=	explode( '|*|', $groups );

						cbArrayToInts( $groups );

						foreach ( $groups as $groupId ) {
							$group					=	new GroupTable();

							$group->load( (int) $groupId );

							if ( $group->get( 'id' ) ) {
								$groupUser			=	new \CB\Plugin\GroupJive\Table\UserTable( $_CB_database );

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

								if ( ! $groupUser->get( 'id' ) ) {
									$groupUser->set( 'user_id', (int) $user->get( 'id' ) );
									$groupUser->set( 'group', (int) $group->get( 'id' ) );
									$groupUser->set( 'status', (int) $row->get( 'status', 1, GetterInterface::INT ) );

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

										continue;
									}
								}
							}
						}
					}
					break;
			}
		}
	}