Пример #1
0
	/**
	 * Mutator:
	 * Prepares field data for saving to database (safe transfer from $postdata to $user)
	 * 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 prepareFieldDataSave( &$field, &$user, &$postdata, $reason )
	{
		$hybrid								=	new cbconnectHybrid();
		$fieldName							=	$field->get( 'name' );
		$provider							=	$hybrid->getProviderFromField( $fieldName );
		$providerId							=	$hybrid->getIdFromProvider( $provider );
		$currentValue						=	$user->get( $fieldName );
		$value								=	cbGetParam( $postdata, $fieldName );

		if ( $currentValue && ( $user->get( 'id' ) == Application::MyUser()->get( 'id' ) ) ) {
			if ( is_array( $value ) ) {
				if ( isset( $value[0] ) && ( $value[0] == 1 ) ) {
					$postdata[$fieldName]	=	'';
				}
			}

			$value							=	cbGetParam( $postdata, $fieldName );

			if ( $value === '' ) {
				try {
					$adapter				=	$hybrid->getAdapter( $providerId );

					if ( $adapter ) {
						switch( $provider ) {
							case 'facebook':
								/** @noinspection PhpUndefinedMethodInspection */
								$adapter->api()->api( '/me/permissions', 'DELETE' );
								break;
						}

						$adapter->logout();
					}
				} catch ( Exception $e ) {}
			}
		}

		if ( ( ! Application::Cms()->getClientId() ) && $user->get( 'id' ) && $currentValue && ( $value !== '' ) ) {
			$postdata[$fieldName]			=	$currentValue;
		}

		parent::prepareFieldDataSave( $field, $user, $postdata, $reason );
	}