Revision 526 (by ahitrov, 2015/11/25 16:12:34) multimedia_multi - multifile field type

<%ARGS>

	$object	=> undef
	$SETS	=> undef

</%ARGS>
<%INIT>

    use Contenido::File;
    use vars qw($keeper $request );

    $request->{local_codepage} = 'KOI8';
    $request->{local_codepage} = 'WIN'	if Convert::Cyrillic::cstocs('WIN', 'KOI8', $SETS->{control_charset}) eq '��������';
    $request->{local_codepage} = 'UTF8'	if Convert::Cyrillic::cstocs('UTF8', 'KOI8', $SETS->{control_charset}) eq '��������';

    warn "Contenido Debug: ����� ��� �������������� ������ � ��������� ".$request->{local_codepage}."\n"        if ($state->debug());

    my @properties = $object->structure();
    for (0..$#properties)
    {
	my $prop = $properties[$_];
	my $name = $prop->{attr};
	my $type = $prop->{type};

	next	if ($name eq 'sections');
	next	if ($name eq 'id');

	if ($m->comp_exists("/contenido/components/outputs/$type.msn")) {
		$object->{$name} = $m->comp("/contenido/components/outputs/$type.msn", SETS => $SETS, name => $name, object => $object );

	} elsif ( $prop->{type} eq 'image' ) {
            # --------------------------------------------------------------------------------------
            # ���������� �����, �������� ������� ��������...

            if ($SETS->{'_delete_image.'.$name} == 1)
            {
		my $IMAGE = $object->get_image( $name );
		$object->_delete_image( $IMAGE );
		$object->$name('');
            } else {
                my $IMAGE = $object->get_image( $name );
		$IMAGE = ref $IMAGE ? $IMAGE : {};
		$IMAGE = $m->comp('/contenido/components/set_image.msn', default=>$IMAGE, field=>$name, SETS=>$SETS, object=>$object, prop => $prop);
		if ( exists $IMAGE->{filename} ) {
			local $Data::Dumper::Indent = 0;
			$object->$name( Data::Dumper::Dumper($IMAGE) );
                }
            }

	} elsif ($prop->{type} =~ /^images/) {
            # --------------------------------------------------------------------------------------
            # � ��� ��� ��� ������� - ����������� ����� �� ���� ��������...

            my $IMAGES = $object->get_image( $name );
	    if (ref($IMAGES) ne 'HASH') {
		$IMAGES = {};
		$IMAGES->{maxnumber} = 0;
	    };

            # ������ ���� ����������� ��� ��������� - � ����� ��� ��, ��� ��� �����...
            foreach my $arg (keys(%{ $SETS }))
            {
		# ��������� ��� �� �����, ��������...
		my ($partname,undef) = split(/\./,$arg);
		$partname =~ /^(.+)_(\d+)$/;
		my ($localname,$number) = ($1, $2);
		if ( ($localname eq $prop->{attr}) && ($number > 0) && ( $arg eq $localname.'_'.$number )) {
                    # ���! ����� �������!

                    my $partname = $localname.'_'.$number;

                    if ($SETS->{'_delete_image.'.$partname} == 1) {
			$object->_delete_image( $IMAGES->{'image_'.$number} );
			delete $IMAGES->{'image_'.$number};
		    } else {
			my $IMAGE = undef;
			if (exists($IMAGES->{'image_'.$number})) {
				$IMAGE = $IMAGES->{'image_'.$number};
			};
                        $IMAGE = $m->comp('/contenido/components/set_image.msn', default=>$IMAGE, field=>$partname, SETS=>$SETS, object=>$object, prop => $prop);
			if (exists($IMAGE->{filename})) {
				local $Data::Dumper::Indent = 0;
				$IMAGE->{number} = $number;
				$IMAGES->{'image_'.$number} = $IMAGE;
				$IMAGES->{maxnumber} = $number		if ($number > $IMAGES->{maxnumber});
			}
		    }
		}
	    }

	    if ( scalar(keys(%{ $IMAGES })) > 0) {
		my $maxnumber = 0;
		for ( 1..100 ) {
			$maxnumber = $_		if exists $IMAGES->{"image_$_"} && $_ > $maxnumber;
		}
		$IMAGES->{maxnumber} = $maxnumber;
		local $Data::Dumper::Indent = 0;
		$object->$name( Data::Dumper::Dumper($IMAGES) );
	    }

	} elsif ( $prop->{type} eq 'multimedia' ) {
            # --------------------------------------------------------------------------------------
            # ���������� ������ ������ �����������

            if ($SETS->{'_delete_multi.'.$name} == 1)
            {
                my $BINARY = eval ('my '.$object->{ $name });
                Contenido::File::remove($BINARY->{"filename"});
                $object->{$name} = '';
            } else {
                my $MULTI = undef;
                if (length($object->{ $name }) > 8)
                {
                    $MULTI = eval ('my '.$object->{ $name });
                    if (ref($MULTI) ne 'HASH') {  $MULTI = {} };
                }
                $MULTI = $m->comp('/contenido/components/set_multi.msn', default=>$MULTI, field=>$name, SETS=>$SETS, object=>$object);
                if (exists($MULTI->{filename}))
                {
                    local $Data::Dumper::Indent = 0;
                    $object->{$name} = Data::Dumper::Dumper($MULTI);
                }
            }

	} elsif ( $prop->{type} eq 'multimedia_new' ) {
            # --------------------------------------------------------------------------------------
            # ���������� ������ ������ �����������

            if ($SETS->{'_delete_multi.'.$name} == 1)
            {
                my $BINARY = eval ('my '.$object->{ $name });
                Contenido::File::remove($BINARY->{"filename"});
                $object->{$name} = '';
            } else {
                my $MULTI = undef;
                if (length($object->{ $name }) > 8)
                {
                    $MULTI = eval ('my '.$object->{ $name });
                    if (ref($MULTI) ne 'HASH') {  $MULTI = {} };
                }
                $MULTI = $m->comp('/contenido/components/set_multimedia_new.msn', default=>$MULTI, field=>$name, SETS=>$SETS, object=>$object, prop => $prop);
                if (exists($MULTI->{filename}))
                {
                    local $Data::Dumper::Indent = 0;
                    $object->{$name} = Data::Dumper::Dumper($MULTI);
                }
            }

        } elsif ($prop->{type} eq 'multimedia_multi') {
	    # --------------------------------------------------------------------------------------
	    # � ��� ��� ��� ������� - ����������� ����� �� ���� ������...

	    my $FILES = $object->get_image( $name );
	    if ( ref($FILES) ne 'HASH' ) {
		$FILES = {};
		$FILES->{maxnumber} = 0;
	    };

	    # ������ ���� ����������� ��� ��������� - � ����� ��� ��, ��� ��� �����...
	    foreach my $arg (keys(%{ $SETS }))
	    {
		# ��������� ��� �� �����, ��������...
		my ($partname,undef) = split(/\./,$arg);
		$partname =~ /^(.+)_(\d+)$/;
		my ($localname,$number) = ($1, $2);
		if ( ($localname eq $prop->{attr}) && ($number > 0) && ( $arg eq $localname.'_'.$number )) {
		    # ���! ����� �������!

		    my $partname = $localname.'_'.$number;

		    if ($SETS->{'_delete_multi.'.$partname} == 1) {
			$object->_delete_binary( $FILES->{'file_'.$number} );
			delete $FILES->{'file_'.$number};
		    } else {
			my $MULTI = undef;
			if (exists($FILES->{'file_'.$number})) {
				$MULTI = $FILES->{'file_'.$number};
			};
			$MULTI = $m->comp('/contenido/components/set_multimedia_new.msn', default=>$MULTI, field=>$partname, SETS=>$SETS, object=>$object, prop => $prop);
			if (exists($MULTI->{filename})) {
				$MULTI->{number} = $number;
				$FILES->{'file_'.$number} = $MULTI;
				$FILES->{maxnumber} = $number		if ($number > $FILES->{maxnumber});
			}
		    }
		}
	    }

	    if ( scalar(keys(%{ $FILES })) > 0) {
		my $maxnumber = 0;
		for ( 1..100 ) {
			$maxnumber = $_		if exists $FILES->{"file_$_"} && $_ > $maxnumber;
		}
		$FILES->{maxnumber} = $maxnumber;
		$object->$name( $object->_serialize($FILES) );
	    }

	} elsif ( $prop->{type} eq 'audio' ) {
            # --------------------------------------------------------------------------------------
            # ���������� ������ ������ �����������

            if ($SETS->{'_delete_multi.'.$name} == 1)
            {
                my $VAR1 = undef;
                eval "\$object->{$name}";
                my $BINARY = $VAR1;
                if (ref $BINARY) {$BINARY->remove}
                $object->{$name} = '';
            } else {
                my $MULTI = undef;
                if (length($object->$name) > 8)
                {
                    my $VAR1 = undef;
                    eval "\$object->{$name}";
                    $MULTI = $VAR1;
                    unless (ref $MULTI) {$MULTI = undef};
                }
                $MULTI = $m->comp('/contenido/components/set_audio.msn', default=>$MULTI, field=>$name, SETS=>$SETS, object=>$object, prop => $prop);
                if (exists($MULTI->{filename}))
                {
                    local $Data::Dumper::Indent = 0;
                    $object->{$name} = Data::Dumper::Dumper($MULTI);
                }
            }

	} else {
		$object->{$name} = $m->comp('/contenido/components/filter.msn', str => $SETS->{$name} );
        }

    }

    # �� ���� - �� �������� � �������, ��� ��� ������ �� ���� ����������!    
    # O-��! �-��! �-��!
    return 1;

</%INIT>