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 {
%	if ( $document->postman_id == $user->id ) {
<& "/contenido/components/obj_list_js.msn", object => $document &>
<& "/contenido/webshop/components/order_form.msn",
	context => 'post',
	object  => $document,
	proto   => 'documents',
	sect_id => $owner->id,
	clone   => $clone,
	filter_params   => \%filter_params,
&>
%	} else {
%		my $profile = $keeper->get_user_by_id( $document->postman_id );
<div style="padding:10px;">
<b style="font-size:110%">Доставка ЗАКАЗА</b>
<p style="color:red"><b>Внимание!</b> Доставку заказа осуществляет <% $profile->name %></p>
<p><a href="./?ost=3">&lt;&lt; Вернуться в список доставки</a></p>
</div>
%	}
% }

</body>
</html>
<%ARGS>
	$p		=> 1
	$class		=> 'webshop::Order'
	$sect_id	=> undef
	$s_alias	=> undef
	$id		=> undef
	$delete		=> undef
	$save		=> undef
	$clone		=> undef
	$activate	=> undef
	$deactivate	=> 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);
		if ( $clone && exists $document->{'attributes'}->{'dtime'} ) {
			$document->dtime(undef);
		}
	} 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 clone s_alias activate deactivate 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->postman_id ) {
		$document->postman_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=3".($return_params ? '&'.$return_params : ''));
        }

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

		if ( $ARGS{_save_and_deliver} ) {
			$document->status(4);
			$document->postman_id( $user->id );
			$document->etime( $now->ymd('-').' '.$now->hms );
		} elsif ( $ARGS{_ret_manager} ) {
			$document->status(1);
		} elsif ( $ARGS{_ret_facility} ) {
			$document->status(2);
		} elsif ( $ARGS{_save_and_leave} ) {
		}

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

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

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

</%INIT>