Revision 198 (by ahitrov, 2012/03/15 18:29:29) Simple webshop support plugin
%# vim:syn=mason
<& "/contenido/components/header.msn" &>
<& "/contenido/components/naviline.msn", sect_id => $owner->id &>

% if ($error) {
<div align="center" style="font-size:110%; color:red;">
<% $error %>
</div>
<br><br>
% }

% if (!ref($document)) {
%	if ($id) {
<div align="center" style="font-size:110%; color:red;">
Документ с идентификатором <% $id %> не найден
</div>
<br><br>
%	} elsif ($class) {
<!-- Блок с выбором нового документа для создания -->
<table width="50%" border="0"><tr><td>
<fieldset>
<legend>Выберите тип документа для создания</legend>
<& "/contenido/components/new_objects_form.msn", proto => 'documents', sect_id => $owner->id &>
</fieldset>
</td></tr></table>
%	} else {
<div align="center" style="font-size:110%; color:red;">
Неверный вызов документа!!! (отсутствуют id и class одновременно)
</div>
<br><br>
%	}

% } else {

<& "/contenido/components/obj_list_js.msn", object => $document &>
<& "/contenido/webshop/components/order_form.msn", 
	object	=> $document, 
	proto	=> 'documents', 
	sect_id => $owner->id,
	filter_params	=> \%filter_params,
&>
% }

</body>
</html>
<%ARGS>
	$p		=> 1
	$class		=> 'webshop::Order'
	$sect_id	=> undef
	$s_alias	=> undef
	$id		=> undef
	$delete		=> undef
	$save		=> undef
</%ARGS>
<%INIT>

	&abort404	unless $class;
	my $error='';
        ### !!! При добавлении переменных в ARGS их надо внести в список исключений в структуре ниже

	my $filter = $m->comp('/contenido/components/context.msn', name => 'filter');

	my $document;
	my $new;
	my $now = Contenido::DateTime->new;

	if ($id && ($id !~ /\D/) && ($id > 0)) {
		$document = $keeper->get_document_by_id($id, class=>$class);
	} elsif ( ($class) && (length($class)>0) && (! ref($document)) ) {
		$document = new $class ($keeper);
		$new = 1;
		my @properties = $document->structure();
		foreach my $prop ( @properties ) {
			my $attr = $prop->{attr};
			### !!! Если не стандартная переменная, то можем инициализировать
			if ( exists $ARGS{$attr} && ! grep { $prop->{attr} eq $_ } qw( class sect_id id delete save s_alias p s use_section alpha alpha_search search_by search ) ) {
				$document->$attr($ARGS{$attr});
			}
		}
	}
	&abort404	unless ref $document;

	my @props = $document->structure();
	my %filter_params;
	if ($ARGS{use_section} && !grep { $_->{attr} eq 'use_section' } @props ) {
		$filter_params{use_section} = $ARGS{use_section};
		$filter_params{class} = $document->class;
	}
	$filter_params{alpha} = $ARGS{alpha}			if $ARGS{alpha} && !grep { $_->{attr} eq 'alpha' } @props;
	$filter_params{alpha_search} = $ARGS{alpha_search}	if $ARGS{alpha_search} && !grep { $_->{attr} eq 'alpha_search' } @props;
	$filter_params{search_by} = $ARGS{search_by}		if $ARGS{search_by} && !grep { $_->{attr} eq 'search_by' } @props;
	$filter_params{search} = $ARGS{search}			if $ARGS{search} && !grep { $_->{attr} eq 'search' } @props;
	$filter_params{s} = $ARGS{s}				if $ARGS{s} && !grep { $_->{attr} eq 's' } @props;
	$filter_params{p} = $p					if $p > 1;
	my $return_params = join ('&', map { $_.'='.$filter_params{$_} } grep { $_ ne 's' } keys %filter_params );


	if ($s_alias) {
		$sect_id = $project->s_alias->{$s_alias};	
	}

	if ( (! $sect_id) && (ref($document)) && ($document->id) ) {
		$sect_id = $document->section();
	}
	my $owner = $keeper->get_section_by_id ($sect_id || $Contenido::Section::ROOT || 1);

	if (! ref($owner)) {
		$owner = $keeper->get_section_by_id ($Contenido::Section::ROOT || 1);
	}
	if (! ref($owner)) {
		warn "Contenido Die: Не могу найти корневую секцию\n";
		return undef;
	}

	if ( ref $document ) {
		$document->manager_id( $user->id );
		$document->store;
	}
	if (ref($document) && $document->id() && $document->section()) {
		my $document_access = $user->section_accesses($user, $owner);
		if ($document_access != 2) {
			$m->clear_buffer;
			$m->abort(403);
		}
	}

	# Удаление...
	if (defined($id) && ($id > 0) && $delete) {
		$document->status( 5 );
		$document->store;

		$m->redirect("./?ost=1".($return_params ? '&'.$return_params : ''));
	}

	# Сохранение существующего документа или создание нового...
	# Кстати, пока никак не обрабатываются связи...
	elsif ( $save == 1 )
	{
                if ($m->comp('/contenido/webshop/components/set_properties.msn', object => $document, SETS => \%ARGS) != 1)
		{
			# Ошибка, надо бы обработать...
			warn "Contenido Warning: Не могу установить значения полей!\n";
		}
		if ( $new ) {
			$document->sections( $owner->id, $filter > 0 ? ($filter) : ());
		}

		if ( $ARGS{_save_and_deliver} ) {
			$document->manager_id( $user->id );
			$document->ftime( $now->ymd('-').' '.$now->hms );
			$document->status(3);
		} elsif ( $ARGS{_save_and_complect} ) {
			$document->manager_id( $user->id );
			$document->btime( $now->ymd('-').' '.$now->hms );
			$document->status(2);
		} elsif ( $ARGS{_save_and_leave} ) {
			$document->manager_id( undef );
			$document->status(1);
		}

		unless ($document->store()) {
			# Ошибка, надо бы обработать...
			$error="Ошибка сохранения ($keeper->{last_error})";
		} else {

			if ($ARGS{_save_and_complect}) {
				$m->redirect("./?ost=2");
			} elsif ($ARGS{_save_and_deliver}) {
				$m->redirect("./?ost=3");
			} elsif ($ARGS{_save_and_leave}) {
				$m->redirect("./?ost=1".($return_params ? '&'.$return_params : ''));
			}

			$m->redirect("take_care.html?id=".$document->id.($return_params ? '&'.$return_params : ''));
		}
	}

</%INIT>