Line # Revision Author
1 3 ahitrov@rambler.ru package Contenido::Object;
2
3 # ----------------------------------------------------------------------------
4 # ����� ������.
5 # ������������ ����� ��� ���� �����. ��� �� �� ���������� ���, �� �������
6 # ������������� ��� ������������.
7 #
8 # �� ���� �� ����� ����������� ��������� ������ � ����������� ������,
9 # ��������� ������ � ��������� ������� � ��� �����...
10 # ----------------------------------------------------------------------------
11
12 use strict;
13 use warnings;
14 use locale;
15
16 use vars qw($VERSION $AUTOLOAD);
17 $VERSION = '4.1';
18
19 use Utils;
20 use Contenido::Globals;
21 114 ahitrov use Contenido::File;
22 3 ahitrov@rambler.ru use Data::Dumper;
23
24 use DBD::Pg;
25 use Encode;
26
27 use SQL::ProtoTable;
28
29 # required properties ������ ������� �� ������� �������
30 sub required_properties {
31 my $self=shift;
32 my $class = ref($self) || $self;
33 if ($class->can('class_table')) {
34 return $self->class_table->required_properties();
35 } else {
36 $log->error("$class cannot method class_table");
37 return ();
38 }
39 }
40
41 sub extra_properties {
42 return ();
43 }
44
45 sub post_init {
46 return;
47 }
48
49 sub pre_store {
50 return 1;
51 }
52
53 sub post_store {
54 return 1;
55 }
56
57 sub post_finish_store {
58 return 1;
59 }
60
61 sub pre_delete {
62 return 1;
63 }
64
65 sub post_delete {
66 return 1;
67 }
68
69 sub pre_abort {
70 return 1;
71 }
72
73 sub t_abort {
74 my $self = shift;
75 $self->pre_abort();
76 return $self->keeper->t_abort();
77 }
78
79 sub new {
80 $log->error("Method 'new' cannot be called for class Contenido::Object");
81 die;
82 }
83
84 sub class_table {
85 $log->error("Method 'class_table' cannot be called for Contenido::Object");
86 die;
87 }
88
89 #��������� ������ DESTROY ����� ��� ������ � AUTOLOAD �� ��������
90 sub DESTROY {}
91
92 #��������� ����� � ���� �� $object ��� �� $class/$id
93 #can be overloaded in class
94 sub get_object_key {
95 my $self = shift;
96 return $self->class_table->_get_object_key($self, @_);
97 }
98
99 #��������� ����� � ���� �� $object ��� �� $class/$unique
100 #can be overloaded in class
101 sub get_object_unique_key {
102 my $self = shift;
103 return $self->class_table->_get_object_unique_key($self, @_);
104 }
105
106 #�������� ��� ���������� �� ��������... ����������� 1 ��� �� ������ �����
107 #??? �������� ����� ������� ������ ������� � ��������� �� ���� �����
108 sub class_init {
109 my $self = shift;
110 my $class = ref($self) || $self;
111
112 {
113 no strict 'refs';
114 return 1 if (${$class.'::class_init_done'});
115 use strict;
116 }
117
118 #�������������� ��������� �������
119 if ($class->can('class_table')) {
120 eval { SQL::ProtoTable->table_init($class->class_table) };
121 do { $log->error("Cannot initialise class $class!"); die } if ($@);
122 }
123
124 #��������� ������������ ������ (todo)
125 #$self->class_validate();
126
127 my $funct;
128
129 #������ ������ ������� �������������� ������ �� ����
130 my $funct_begin = "
131 my (\$class, \$row, \$keeper, \$light) = \@_;
132 ";
133
134 my $funct_start_obj =' return bless({';
135 my $funct_end_obj = "}, '$class');
136 ";
137 my $funct_begin_if_light = '
138 if ($light) {';
139 my $funct_elsif_light = '
140 } else {';
141 my $funct_endif_light = '
142 }';
143
144 my $func_start_encode = '';
145 my $func_end_encode = '';
146
147 if ($state->db_encode_data) {
148 $func_start_encode = 'Encode::encode("'.$state->db_encode_data.'", ';
149 $func_end_encode = ', Encode::FB_HTMLCREF)';
150 }
151
152 my @funct_default_fields = ("class=>'$class'", "keeper=>\$keeper");
153 my @funct_exra_fields = ();
154
155 #�� ���� ������� ���� ������� ���������� ��� ������������� ������
156 my (%props, %attributes, @extra_fields, %virtual_fields, @structure);
157
158 my $pos = 0;
159 #������������������ reload: required_properties ����� ���� ���������� ����� add_properties ������� ����� ���� ����� �������� ����� extra_properties
160
161 foreach my $prop ($self->required_properties()) {
162 my $attr = $prop->{attr};
163 unless ($attr) {
164 $log->error("$class have wrong data in required_properties (no attr for field)");
165 next;
166 }
167 unless ($prop->{db_type} || $prop->{virtual}) {
168 $log->warning("$class with class table: ".$self->class_table()." property '$attr' missing db_type in table descriptor... can be incompatible with future versions!");
169 }
170
171 $props{$attr} = $prop;
172
173 push @structure, $prop;
174
175 #������ � ������� ���� ��������... ���� ���� ��� ��� ����� ������ ����� ���� �� �����... ������ ����� ���
176 next if ($attr eq 'class');
177
178 #���� ������� ���� � DB ����� ���� ��������� ������ � required_properties
179 if (exists($prop->{db_field}) and $prop->{db_field}) {
180 $pos++;
181 #$DBD::Pg versions since 2.0.0 do it automatically
182 if ($DBD::Pg::VERSION=~/^1\./ and $prop->{db_type} and (($prop->{db_type} eq 'integer[]') or ($prop->{db_type} eq 'integer_ref[]'))) {
183 push @funct_default_fields, "$attr=>[(\$row->[$pos] and \$row->[$pos]=~/^{(\\d+(?:,\\d+)*)}\$/) ? split(/,/, \$1) : ()]";
184 } else {
185 push @funct_default_fields, "$attr=>$func_start_encode\$row->[$pos]$func_end_encode";
186 }
187 }
188
189 if ($prop->{db_type} and ($prop->{db_type} eq 'integer[]')) {
190 $attributes{$attr} = 'ARRAY';
191 } elsif($prop->{db_type} and ($prop->{db_type} eq 'integer_ref[]')) {
192 $attributes{$attr} = 'ARRAY_REF';
193 } else {
194 $attributes{$attr} = 'SCALAR';
195 }
196 }
197 push @funct_default_fields, "attributes=>\$${class}::attributes";
198
199 my $have_extra = $self->class_table->have_extra;
200 if ($have_extra) {
201 my @ap = $self->add_properties() if $self->can('add_properties');
202 #������������������ reload: required_properties ����� ���� ���������� ����� add_properties ������� ����� ���� ����� �������� ����� extra_properties
203 foreach my $prop (@ap, $self->extra_properties()) {
204 my $attr = $prop->{attr};
205 if (exists($props{$attr})) {
206 #reload code
207 $log->info("Reloaded property $attr for class $class") if ($DEBUG);
208 while ( my ($field, $value) = each(%$prop)) {
209 $props{$attr}->{$field} = $value;
210 }
211 } else {
212 $props{$attr} = $prop;
213 #���� ��� ��� �� overload �� ��� ����� extra ����
214 push @extra_fields, $attr;
215 push @structure, $prop;
216 $attributes{$attr} = 'SCALAR';
217 if ($prop->{virtual}) {
218 #���������� ��� ��� ������ ����������� �������
219 $virtual_fields{$attr} = 1;
220 } else {
221 #�������������� �� dump ��� ����� ����������� �������
222 push @funct_exra_fields, "$attr=>$func_start_encode\$dump->{$attr}$func_end_encode";
223 }
224 }
225 }
226 }
227
228 $attributes{class} = 'SCALAR';
229
230 #���� � ������� ���� extra_attributes ���� �� ������� restore_extras ���� �� ������ light
231 #������� have_extra � ������� �� ����� � ������������� ������� extra_fields
232 if (@extra_fields) {
233 # --------------------------------------------------------------------------------------------
234 # ������ �� ������ ����� � ���� ������
235 # --------------------------------------------------------------------------------------------
236 my $funct_eval_dump .= '
237 my $dump = Contenido::Object::eval_dump(\\$row->[-1]);
238 ';
239 $funct = $funct_begin.$funct_begin_if_light.$funct_start_obj.join(', ', @funct_default_fields).$funct_end_obj.$funct_elsif_light.$funct_eval_dump.$funct_start_obj.join(', ', (@funct_default_fields, @funct_exra_fields)).$funct_end_obj.$funct_endif_light;
240 } else {
241 $funct = $funct_begin.$funct_start_obj.join(', ', @funct_default_fields).$funct_end_obj;
242 }
243
244 create_method($class, 'init_from_db', $funct);
245
246 {
247 no strict 'refs';
248 ${$class.'::structure'} = \@structure;
249 ${$class.'::attributes'} = \%attributes;
250 ${$class.'::extra_fields'} = \@extra_fields;
251 ${$class.'::virtual_fields'} = \%virtual_fields;
252 ${$class.'::class_init_done'} = 1;
253 }
254 return 1;
255 }
256
257 # -------------------------------------------------------------------------------------------
258 # ��������� ������� �������� ������� � ����������� �� ���������� �������...
259 # -------------------------------------------------------------------------------------------
260 sub store_extras {
261 my $self = shift;
262 my %opts = @_;
263 do {$log->error("����� store_extras() ����� �������� ������ � ��������, �� �� �������\n"); die } unless ref($self);
264
265 do { $log->error("� ������� �� ���������� ������ �� ���� ������"); die } unless ref($self->keeper);
266 do { $log->error("�� ����� ����� ���������� ������ (insert/update)"); die } if (($opts{mode} ne 'insert') && ($opts{mode} ne 'update'));
267 do { $log->error("�� ����� ������������� ������� (� ������ ���� ����� � ������������ �������)"); die } unless($self->id());
268
269 if ($self->keeper->store_method() eq 'sqldump') {
270 my $extra_table=$self->class_table->extra_table;
271 do { $log->error("No extra table for class $self->{class}"); die } unless ($extra_table);
272 if ($opts{mode} eq 'insert') {
273 $self->keeper->TSQL->do("INSERT INTO $extra_table (id, data) VALUES (?, ?)", {}, $self->id(), $self->_create_extra_dump()) || $self->t_abort();
274 } else {
275 $self->keeper->TSQL->do("UPDATE $extra_table SET data=? WHERE id=?", {}, $self->_create_extra_dump(), $self->id()) || $self->t_abort();
276 }
277
278 } elsif ($self->keeper->store_method() eq 'toast') {
279 my $table = $self->class_table->db_table;
280 do { $log->error("There no db_table for class $self->{class}"); die } unless ($table);
281 $self->keeper->TSQL->do("UPDATE $table SET data=? WHERE id=?", {}, $self->_create_extra_dump(), $self->id()) || $self->t_abort();
282
283 } else {
284 $log->error("����� ���������� �������� ����� �������! ��������� �������� - TOAST, SQLDUMP");
285 die;
286 }
287
288 return 1;
289 }
290
291
292 sub _create_extra_dump {
293 my $self = shift;
294
295 do { $log->error("����� _create_extra_dump ����� �������� ������ � ��������, �� �� �������"); die } unless ref($self);
296
297 my $class_table = $self->class_table;
298 return undef unless ($class_table and $class_table->have_extra);
299
300 my $extra_fields = [];
301 my $virtual_fields = {};
302 {
303 no strict 'refs';
304 local $Data::Dumper::Indent = 0;
305 #���������� virtual attributes
306 #�� � ���� ��� ��� ������ ������ �� ���� ����� ��� ���������
307 $extra_fields = ${$self->{class}.'::extra_fields'};
308 $virtual_fields = ${$self->{class}.'::virtual_fields'};
309 #���� ������������ ��� extra ���� ����� ��� ��� ��������� � virtual_fields ������
310 if ($state->db_encode_data) {
311 return Data::Dumper::Dumper({map { $_=> Encode::decode($state->db_encode_data, $self->{$_}, Encode::FB_HTMLCREF) } grep { !$virtual_fields->{$_} && (defined $self->{$_}) } @$extra_fields});
312 } else {
313 return Data::Dumper::Dumper({map { $_=>$self->{$_} } grep { !$virtual_fields->{$_} && (defined $self->{$_}) } @$extra_fields});
314 }
315 }
316 }
317
318 # -------------------------------------------------------------------------------------------
319 # ��������� ������� �������� ������� � ����������� �� ���������� �������...
320 # -------------------------------------------------------------------------------------------
321 sub restore_extras {
322 my ($self, $row) = @_;
323
324 do { $log->error("����� restore_extras() ����� �������� ������ � ��������, �� �� �������"); die } unless ref($self);
325
326 my $extra_fields;
327 {
328 no strict 'refs';
329 $extra_fields = ${$self->{class}.'::extra_fields'};
330 }
331
332 if (@$extra_fields) {
333 if (($Contenido::Globals::store_method eq 'toast') or ($Contenido::Globals::store_method eq 'sqldump')) {
334 # --------------------------------------------------------------------------------------------
335 # ������ �� ������ ����� � ���� ������
336 # --------------------------------------------------------------------------------------------
337 my $dump_ = eval_dump(\$row->[-1]);
338 if ($dump_) {
339 foreach (@$extra_fields) {
340 $self->{$_} = $dump_->{$_};
341 }
342 }
343 } else {
344 $log->error("����� ���������� �������� ����� �������! ��������� �������� - TOAST, SQLDUMP, SINGLE, DUMP");
345 die;
346 }
347 }
348 }
349
350 # ----------------------------------------------------------------------------
351 # �������� ��� �� ����-����� �� ��������
352 # ������:
353 # my $pics_hashe = $doc->get_data('images');
354 # ----------------------------------------------------------------------------
355 sub get_data {
356 my $self = shift;
357 my $attr = shift;
358 my $data = eval_dump(\$self->{$attr});
359 return ($data || {});
360 }
361
362 # ----------------------------------------------------------------------------
363 # �������� �������� �� ������� �� �� ��������
364 # ���������� ������ ���� Contenido::Image
365 #
366 # ������:
367 # my $pic = $doc->get_pic('top_image');
368 #
369 # ----------------------------------------------------------------------------
370 sub get_pic {
371 my $self = shift;
372 my $attr = shift;
373
374 Contenido::Image->new (
375 img => $self->get_data($attr),
376 attr => $attr,
377 );
378 }
379
380 # ----------------------------------------------------------------------------
381 # �������� �������� �� ������� �� �������� �� ���������
382 # ���������� ������ �������� ���� Contenido::Image
383 #
384 # ������:
385 # my @pics = $doc->get_pics('images', {
386 # order => 'reverse', # ������� ���������� �� ������ ('reverse' ,'asis', �� ��������� - 'direct')
387 # keys => [3..12, 1..2], # �������� ������
388 # });
389 #
390 # ----------------------------------------------------------------------------
391 sub get_pics {
392 my $self = shift;
393 my $attr = shift;
394 my %args = ref $_[0] ? %{$_[0]} : @_;
395 my $pics = $self->get_data($attr);
396
397 # �������
398 $args{order} ||= 'direct';
399
400 # �������� ������� ������ ��� ��������...
401 my @keys = ref $args{keys} ne 'ARRAY' ? grep {s/\D+//, /^\d+$/} keys %{$pics} : @{$args{keys}};
402
403 my $prefix = 'image_'; # � ���� ��, ���� ���: my $prefix = $attr.'_';
404
405 map { Contenido::Image->new (
406 img => $pics->{$prefix.$_},
407 attr => $prefix.$_,
408 group => $attr,
409 key => $_,
410 )} sort { $args{order} eq 'asis' ? 1 : $args{order} ne 'reverse' ? $a <=> $b : $b <=> $a } @keys;
411 }
412
413 sub _get_sql {
414 my ($self,%opts)=@_;
415
416 #������ ������ ������� �� ������� ��������
417 my $table = $self->_get_table(%opts);
418 unless ($table) {
419 $log->error("�� ���� �������� ������� �������");
420 return;
421 }
422
423 my ($query, $binds) = $table->generate_sql(%opts);
424 my @binds = ();
425
426 if ($state->db_encode_data) {
427 foreach my $i (0..$#{$binds}) {
428 $binds->[$i] = Encode::decode($state->db_encode_data, $binds->[$i], Encode::FB_HTMLCREF);
429 }
430 }
431
432 return $query, $binds;
433 }
434
435 # ������ �������������:
436 # $document->store()
437 #
438 # ���� $id ����� �� �� �������, ��� ���� ������ � ���� ����. ����
439 # �� �����, �� �� �������, ��� ����� ������� � ���� ��� � ������� ���.
440 # ----------------------------------------------------------------------------
441 sub store {
442 my $self = shift;
443 do { $log->error("����� store() ����� �������� ������ � ��������, �� �� �������"); die } unless ref($self);
444
445 do { $log->error("� ������� ��������� �� ���������� ������ �� ���� ������"); die } unless ref($self->keeper);
446
447 return undef if ($self->keeper->state->readonly());
448
449 $self->keeper->t_connect() || do { $self->keeper->error(); return undef; };
450 $self->{status} ||= 0; # �������� ������� �� ��������� = 0
451
452 unless ($self->pre_store()) {
453 $log->notice("pre_store call failed!");
454 return undef;
455 }
456
457 my (@fields, @values, @default_pairs, @default_fields, @default_values, @binary_fields);
458
459 foreach ($self->required_properties()) {
460
461 my $value = $self->{$_->{attr}};
462 $value = undef if (defined($value) and $value eq '') and (lc($_->{db_type}) eq 'float' or lc($_->{db_type} eq 'integer'));
463 $value = undef if lc $_->{db_type} eq 'integer[]' && ref $value ne 'ARRAY';
464 $value = undef if lc $_->{db_type} eq 'integer_ref[]' && ref $value ne 'ARRAY';
465
466 #���������� readonly ���� � ��������� ��� ���� id
467 if ($self->id() and $_->{readonly}) {
468
469 #��� ���� � ���� � ��������
470 } elsif (!$_->{db_field}) {
471
472 #��������� default ���� ��� ���� � ����� ���� ��� ��� �������� � ����
473 } elsif (defined($_->{default}) and ($_->{auto} or !defined($value))) {
474 push @default_fields, $_->{db_field};
475 push @default_values, $_->{default};
476 push @default_pairs, "$_->{db_field}=$_->{default}";
477
478 #���������� auto ��� default
479 } elsif ($_->{auto}) {
480
481 #��������� �������� �����
482 } elsif (defined($value)) {
483 push @fields, $_->{db_field};
484 if ($_->{db_type} eq 'integer[]') {
485 push @values, '{'.join(',', grep { $_ } @$value).'}';
486 } elsif ($_->{db_type} eq 'integer_ref[]') {
487 push @values, '{'.join(',', grep { $_ } @$value).'}';
488 } else {
489 #some special work for bytea column type
490 push @binary_fields, scalar(@fields) if ($_->{db_type} eq 'bytea');
491 if ($state->db_encode_data) {
492 push @values, Encode::decode($state->db_encode_data, $value, Encode::FB_HTMLCREF);
493 } else {
494 push @values, $value;
495 }
496 }
497
498 #undef to NULL or empty array
499 } else {
500 push @default_fields, $_->{db_field};
501 push @default_values, 'NULL';
502 push @default_pairs, "$_->{db_field}=NULL";
503 }
504 }
505
506 #���� ����������� toast �� ���������� �� 1 sql ������ � extra ����
507 if (($self->keeper->store_method() eq 'toast') and $self->class_table->have_extra) {
508 push @fields, 'data';
509 push @values, $self->_create_extra_dump();
510 }
511
512
513 my $values_string = '';
514 my $mode = 'update';
515 if ($self->id()) {
516 if (@fields) {
517 $values_string = join(' = ?, ', @fields).' = ?';
518 $values_string .= ', '.join(', ', @default_pairs) if (@default_pairs);
519 #��� ������� �������� �������� ������ �� @default_pairs
520 } else {
521 $values_string = join(', ', @default_pairs) if (@default_pairs);
522 }
523 my $sql='UPDATE '.$self->class_table->db_table.' SET '.$values_string." WHERE ".$self->class_table()->id_field()." = ?";
524
525 my $sth=$self->keeper->TSQL->prepare_cached($sql, {}, 1) || return $self->t_abort();
526 #settin special escape for bytea column type!!!
527 foreach (@binary_fields) {
528 $sth->bind_param($_, undef, {pg_type => DBD::Pg::PG_BYTEA});
529 }
530 unless ($sth->execute(@values, $self->{id})) {
531 $log->error("DBI execute error on $sql\n".Data::Dumper::Dumper(\@values));
532 $sth->finish();
533 return $self->t_abort();
534 }
535 $sth->finish();
536
537 if (($self->keeper->store_method() ne 'toast') and $self->class_table->have_extra) {
538 $self->store_extras(mode => $mode) || return $self->t_abort();
539 }
540
541 } else {
542 $mode = 'insert';
543 if (@fields) {
544 $values_string = '?, 'x(scalar (@fields)-1).'?';
545 $values_string .= ', '.join(', ', @default_values) if (@default_values);
546 #��� ������� �������� �������� ������ �� @default_pairs
547 } else {
548 $values_string = join(', ', @default_values) if (@default_values);
549 }
550 my $sql='INSERT INTO '.$self->class_table->db_table.' ('.join(', ', (@fields, @default_fields)).') VALUES ('.$values_string.')';
551
552 my $sth=$self->keeper->TSQL->prepare_cached($sql, {}, 1) || return $self->t_abort();
553 #settin special escape for bytea column type!!!
554 foreach (@binary_fields) {
555 $sth->bind_param($_, undef, {pg_type => DBD::Pg::PG_BYTEA});
556 }
557 unless ($sth->execute(@values)) {
558 $log->error("DBI execute error on $sql\n".Data::Dumper::Dumper(\@values));
559 $sth->finish();
560 return $self->t_abort();
561 }
562 $sth->finish();
563
564 my $id = $self->keeper->TSQL->selectrow_array("SELECT currval('documents_id_seq')");
565 $self->id($id);
566 return $self->t_abort("��������� �������� �������� �������������") if (! defined($self->{id}) || ($self->{id} <= 0));
567
568 if (($self->keeper->store_method() ne 'toast') and $self->class_table->have_extra) {
569 $self->store_extras(mode => $mode) || return $self->t_abort();
570 }
571
572 }
573
574 $self->post_store(mode => $mode);
575
576 $self->keeper->t_finish();
577
578 $self->post_finish_store();
579
580 $self->drop_cache($mode) if ($self->keeper->state()->memcached_enable());
581
582 return 1;
583 }
584
585 # ----------------------------------------------------------------------------
586 # ����� delete() ��� �������� ������� �� ���� ������.
587 #
588 # ������ �������������:
589 # $document->delete()
590 # ----------------------------------------------------------------------------
591 sub delete {
592 my $self = shift;
593 my (%opts) = @_;
594 do { $log->error("����� delete() ����� �������� ������ � ��������, �� �� �������"); die } unless ref($self);
595
596 return undef if ($self->keeper->state->readonly());
597
598 unless ($self->pre_delete()) {
599 $log->error("pre_delete call failed!");
600 return undef;
601 }
602
603 my $keeper = $self->keeper;
604 do { $log->error("� ������� ��������� �� ���������� ������ �� ���� ������"); die } unless ref($keeper);
605
606 if ( exists $opts{attachments} && $opts{attachments} ) {
607 my @props = $self->structure();
608 if ( @props ) {
609 @props = grep { $_->{type} =~ /^(image|images|multimedia_new)$/ } @props;
610 foreach my $prop ( @props ) {
611 my $att = $self->get_image($prop->{attr});
612 if ( $prop->{type} eq 'image' ) {
613 if ( ref $att && exists $att->{filename} && $att->{filename} ) {
614 Contenido::File::remove( $att->{filename} );
615 }
616 56 ahitrov@rambler.ru if ( exists $att->{mini} && ref $att->{mini} eq 'HASH' ) {
617 Contenido::File::remove( $att->{mini}{filename} ) if exists $att->{mini}{filename};
618 foreach my $val ( values %{ $att->{mini} } ) {
619 if ( ref $val && exists $val->{filename} && $val->{filename} && ($val->{filename} ne $att->{mini}{filename}) ) {
620 Contenido::File::remove( $val->{filename} );
621 3 ahitrov@rambler.ru }
622 }
623 }
624
625 } elsif ( $prop->{type} eq 'images' ) {
626 for ( 1..100 ) {
627 next unless exists $att->{"image_$_"};
628 my $img = $att->{"image_$_"};
629 if ( ref $img && exists $img->{filename} && $img->{filename} ) {
630 Contenido::File::remove( $img->{filename} );
631 }
632 56 ahitrov@rambler.ru if ( exists $img->{mini} && ref $img->{mini} eq 'HASH' ) {
633 Contenido::File::remove( $img->{mini}{filename} ) if exists $img->{mini}{filename};
634 foreach my $val ( values %{ $img->{mini} } ) {
635 if ( ref $val && exists $val->{filename} && $val->{filename} && ($val->{filename} ne $img->{mini}{filename}) ) {
636 Contenido::File::remove( $val->{filename} );
637 3 ahitrov@rambler.ru }
638 }
639 }
640 }
641 } elsif ( $prop->{type} eq 'multimedia_new' ) {
642 if ( ref $att && exists $att->{filename} && $att->{filename} ) {
643 Contenido::File::remove( $att->{filename} );
644 }
645 }
646 }
647 }
648 }
649 do { $log->warning("����� ������ delete() ��� �������� �������������� ��� ��������"); return undef }
650 56 ahitrov@rambler.ru unless ($self->{id});
651 3 ahitrov@rambler.ru $keeper->t_connect() || do { $keeper->error(); return undef; };
652 $keeper->TSQL->do("DELETE FROM ".$self->class_table->db_table." WHERE id = ?", {}, $self->id) || return $self->t_abort();
653
654 # �������� ������ ����� ��������� � ������� �����������...
655 my %document_links;
656 if ( $keeper->state->{available_links} && ref $keeper->state->{available_links} eq 'ARRAY' ) {
657 foreach my $classlink ( @{ $keeper->state->{available_links} } ) {
658 my $sources = $classlink->available_sources;
659 if ( ref $sources eq 'ARRAY' && @$sources ) {
660 $document_links{$classlink->class_table->db_table}{source} = 1 if grep { $self->class eq $_ } @$sources;
661 }
662 my $dests = $classlink->available_destinations;
663 if ( ref $dests eq 'ARRAY' && @$dests ) {
664 46 ahitrov@rambler.ru $document_links{$classlink->class_table->db_table}{dest} = 1 if grep { $self->class eq $_ } @$dests;
665 3 ahitrov@rambler.ru }
666 }
667 foreach my $tablename ( keys %document_links ) {
668 my (@wheres, @values);
669 if ( exists $document_links{$tablename}{source} ) {
670 push @wheres, "(source_id = ? AND source_class = ?)";
671 push @values, ( $self->id, $self->class );
672 }
673 if ( exists $document_links{$tablename}{dest} ) {
674 push @wheres, "(dest_id = ? AND dest_class = ?)";
675 push @values, ( $self->id, $self->class );
676 }
677 my $request = "DELETE FROM $tablename WHERE ".join (' OR ', @wheres);
678 warn "DELETE LINKS. Request: [$request]\n" if $DEBUG;
679 warn "Values: [".join(', ', @values)."]\n" if $DEBUG;
680 $keeper->TSQL->do($request, {}, @values) || return $self->t_abort();
681 }
682 } else {
683 $keeper->TSQL->do("DELETE FROM links WHERE source_id = ? AND source_class = ? ", {}, $self->id, $self->class) || return $self->t_abort();
684 $keeper->TSQL->do("DELETE FROM links WHERE dest_id = ? AND dest_class = ? ", {}, $self->id, $self->class) || return $self->t_abort();
685 }
686 $keeper->t_finish();
687
688 $self->post_delete();
689
690 $self->drop_cache('delete') if ($keeper->state()->memcached_enable());
691
692 return 1;
693 }
694
695 # ----------------------------------------------------------------------------
696 # ����� links() ���������� ������ �������� ���� Contenido::Link
697 #
698 # ������ �������������:
699 # $document->links([�����])
700 # ----------------------------------------------------------------------------
701 sub links {
702 my ($self, $lclass, $direction, %opts) = @_;
703 do { $log->error("����� ->links() ����� �������� ������ � ��������, �� �� �������"); die } unless ref($self);
704
705 do { $log->error("� ������� ��������� �� ���������� ������ �� ���� ������"); die } unless ref($self->keeper);
706
707 do { $log->warning("����� ������ ->links() ��� �������� �������������� ���������-���������"); return () } unless ($self->id() > 0);
708
709 my $check = defined $direction ? 'dest_id' : 'source_id';
710
711 $opts{$check} = $self->id();
712
713 if (defined($lclass) && (length($lclass) > 0)) {
714 56 ahitrov@rambler.ru $opts{class} = $lclass;
715 3 ahitrov@rambler.ru }
716
717 my @links = $self->keeper->get_links(%opts);
718
719 $self->{links} = \@links;
720 return @links;
721 }
722
723
724 sub linked_to {
725 my ($self, $lclass) = @_;
726 $self->links($lclass, 1);
727 }
728
729
730 # ----------------------------------------------------------------------------
731 # ��������� �����. ������ ���� ����������� ����� �����...
732 # � �������� source_id ��������� ������������� �������, � �������� $dest_id -
733 # ��������.
734 #
735 # ������ �������������:
736 # $document->set_link($lclass, $dest_id)
737 #
738 # �������� �� ������������ - � ��������� ����� ���� ��������� ����������
739 # ������.
740 # ----------------------------------------------------------------------------
741 sub set_link {
742 my ($self, $lclass, $dest_id, $dest_class, @opts) = @_;
743 do { $log->error("����� ->set_link() ������ � ������������ ���-��� ����������"); die } if @opts % 2;
744 do { $log->error("����� ->set_link() ����� �������� ������ � ��������, �� �� �������"); die } unless ref($self);
745 my %opts = @opts;
746
747 return undef if ($self->keeper->state->readonly());
748
749 do { $log->warning("����� ������ ->set_link() ��� �������� �������������� ���������-���������"); return undef } unless ($self->id() > 0);
750 do { $log->warning("����� ������ ->set_link() ��� �������� �������������� ���������-����"); return undef } unless ($dest_id >= 0);
751 114 ahitrov do { $log->warning("����� ������ ->set_link() ��� �������� ������ �����"); } unless defined($lclass) && length($lclass);
752 3 ahitrov@rambler.ru
753 # ������� ������ �����...
754 my $link = $lclass->new($self->keeper);
755
756 $link->dest_id($dest_id);
757 $link->dest_class($dest_class);
758
759 $link->status(1);
760
761 $link->source_id($self->id());
762 $link->source_class($self->class());
763
764 while (my ($k,$v) = each %opts) {
765 56 ahitrov@rambler.ru $link->{$k} = $v;
766 3 ahitrov@rambler.ru }
767
768 if ($link->store())
769 {
770 56 ahitrov@rambler.ru return $link->id;
771 3 ahitrov@rambler.ru } else {
772 56 ahitrov@rambler.ru return undef;
773 3 ahitrov@rambler.ru }
774 }
775
776 # -------------------------------------------------------------------
777 # ���������� ������ � ������������� ���.
778 #
779 sub prepare_for_cache {
780 my $self = shift;
781
782 do { $log->error("����� ->prepare_for_cache() ����� �������� ������ � ��������, �� �� �������"); die } unless ref($self);
783
784 my $hash = {};
785
786 foreach ( $self->structure() ) {
787 56 ahitrov@rambler.ru $hash->{$_->{attr}} = $self->{$_->{attr}} if defined $self->{$_->{attr}};
788 3 ahitrov@rambler.ru }
789 bless $hash, $self->class();
790 return $hash;
791 }
792
793 # -------------------------------------------------------------------
794 # ��������������� ����������� ������ �� �������������� ����.
795 # ��� ��� ���� ������������ � ����������� ������.
796 # -------------------------------------------------------------------
797 sub recover_from_cache {
798 my $self = shift;
799 my $keeper = shift;
800
801 do { $log->error("����� ->recover_from_cache() ����� �������� ������ � ��������, �� �� �������"); die } unless ref($self);
802 do { $log->error("� ������� ��������� �� ���������� ������ �� ���� ������"); die } unless ref($keeper);
803
804 #�� ����� ��� bless ���������... 100% ���� �� ������ � ��������� ����� �� �� ���� ����� ����� ������
805 $self->init();
806 $self->keeper($keeper);
807
808 return 1;
809 }
810
811 # -------------------------------------------------------------------
812 # ���������� ���:
813 # {��������1 => [���1, ���2, ...], ��������2 => [���1, ���2, ...], ...}
814 # �.�. ��� ������� �������� �������� ������ ���� ������ � ����,
815 # ������� ���� �������.
816 # ��������� ������� ��������: insert, update, delete
817 # ��� ����� ������� ������ ������ ���� ����� ������ ���� �������������
818 # � ������ ������ �������
819 #
820 sub dependencies {
821 my ($self, $mode) = @_;
822
823 my @keys = ($self->get_object_key,);
824 my $object_unique_key = $self->get_object_unique_key;
825 push @keys, $object_unique_key if defined $object_unique_key;
826
827 return
828 ($mode eq 'delete') || ($mode eq 'insert') || ($mode eq 'update')
829 ? \@keys
830 : [];
831 }
832
833 # -------------------------------------------------------------------
834 # ������� �� ���� �����, �������� ��� ������ dependencies().
835 # ������ ������:
836 # $group->drop_cache('update');
837 #
838 sub drop_cache {
839 my $self = shift;
840 my $mode = shift;
841
842 do { $log->error("����� ->drop_cache() ����� �������� ������ � ��������, �� �� �������"); die } unless ref($self);
843
844 my $keeper = $self->keeper;
845 do { $log->error("� ������� ��������� �� ���������� ������ �� ���� ������"); die } unless ref($keeper);
846
847 my $dependencies = $self->dependencies($mode, @_);
848
849 my @not_deleted = ();
850 if ( defined($dependencies) && (ref($dependencies) eq 'ARRAY') ) {
851 for (@$dependencies) {
852 my $res = $self->keeper->MEMD ? $self->keeper->MEMD->delete($_) : undef;
853 push @not_deleted, $_ unless $res;
854 $keeper->MEMD->delete($_) if ($keeper->MEMD);
855 }
856 }
857 return @not_deleted;
858 }
859
860
861 sub keeper {
862 my $self = shift;
863 my $project_keeper = shift;
864
865 do { $log->error("����� keeper() ����� �������� ������ � ��������, �� �� �������"); die } unless ref($self);
866
867 if ($project_keeper && ref $project_keeper) {
868 $self->{keeper} = $project_keeper;
869 return $project_keeper;
870 }
871 return $self->{keeper} && ref $self->{keeper} ? $self->{keeper} : $keeper;
872 }
873
874 #������ ������� ��� init_from_db ����� ������������������� ����� ���� ����
875 sub init_from_db {
876 my $self = shift;
877
878 my $class = ref($self) || $self;
879
880 #������ �� ����������� �������� ���� class_init �� �����������
881 if (defined($_[-1]) and ($_[-1] eq 'RECURSIVE CALL FLAG!')) {
882 do { $log->error("$class cannot be initialized (->class_init dont work) (recursive call) !!!"); die };
883 }
884
885 #���� ���� ����� �� �������� ������� ��� ��� �� ������������������ �� ����������� �������������������
886 #������ ������������� ������ init_from_db ��������� �� ref �� �����
887 if ($class and $class->isa('Contenido::Object')) {
888 no strict 'refs';
889 if (${$class.'::class_init_done'}) {
890 do { $log->error("$class already initialized but DONT HAVE init_from_db method!!!"); die };
891 } else {
892 if ($self->class_init()) {
893 return $self->init_from_db(@_, 'RECURSIVE CALL FLAG!');
894 } else {
895 do { $log->error("$class cannot be initialized (->class_init dont work) !!!"); die };
896 }
897 }
898 } else {
899 do { $log->error("$class cannot be initialized (not Contenido::Object child class) !!!"); die };
900 }
901 }
902
903 # ----------------------------------------------------------------------------
904 # ��� ����� AUTOLOAD. ����� ������� ��� ���������/������ �����...
905 # ������ 1.0
906 # ������ �� ���������� ����������� ����� ������� ���� ����
907 # ----------------------------------------------------------------------------
908 sub AUTOLOAD {
909 my $self = shift;
910 my $attribute = $AUTOLOAD;
911
912 $log->info("$self calling AUTOLOAD method: $attribute") if ($DEBUG_CORE);
913
914 $attribute=~s/^.*:://;
915
916 my $class = ref($self);
917 unless ($class and $class->isa('Contenido::Object')) {
918
919 my $mason_comp = ref($HTML::Mason::Commands::m) ? $HTML::Mason::Commands::m->current_comp() : undef;
920 my $mason_file = ref($mason_comp) ? $mason_comp->path : undef;
921 my ($package, $filename, $line) = caller;
922
923 $log->warning("Wrong AUTOLOAD call with self='$self'/class='$class' and method '$attribute' called from '$package/$filename/$line' ".($mason_file ? "called from $mason_file" : '')."\n".Data::Dumper::Dumper($self));
924 if (wantarray) { return (); } else { return undef; }
925 }
926
927 #�������� ���� ���� �� �� ����� �������� ������������� ������ ���� �� �� ����� �� ������ ���� ������ ��� ��������������������� ������
928 {
929 no strict 'refs';
930 unless (${$class.'::class_init_done'}) {
931 my ($package, $filename, $line) = caller;
932 $log->error("AUTOLOAD called method '$attribute' for not initialised class ($class) from '$package/$filename/$line'");
933 if (wantarray) { return (); } else { return undef; }
934 }
935 }
936
937 if (! exists($self->{attributes}->{$attribute})) {
938
939 my $mason_comp = ref($HTML::Mason::Commands::m) ? $HTML::Mason::Commands::m->current_comp() : undef;
940 my $mason_file = ref($mason_comp) ? $mason_comp->path : undef;
941 my ($package, $filename, $line) = caller;
942
943 $log->error(ref($self)."): ����� ������, ��� �������� �� ���������� ��������������� ��������: ->$attribute() called from $package/$filename/$line ".($mason_file ? "called from $mason_file" : '')."\n".Data::Dumper::Dumper($self));
944 if (wantarray) { return (); } else { return undef; }
945 #special work with ARRAY types
946 } elsif ($self->{attributes}->{$attribute} eq 'ARRAY') {
947 my $funct = "
948 use Contenido::Globals;
949 my \$self = shift;
950 unless (ref(\$self->{$attribute}) eq 'ARRAY') {
951 my (\$package, \$filename, \$line) = caller;
952 \$log->error(\"Wrong structure in field $attribute called from \$package/\$filename/\$line \\n\".Data::Dumper::Dumper(\$self)) if (\$self->{$attribute});;
953 \$self->{$attribute} = [];
954 }
955 \$self->{$attribute} = [\@_] if (\@_);
956 return \@{\$self->{$attribute}};";
957
958 if (create_method($class, $attribute, $funct)) {
959 return $self->$attribute(@_);
960 } else {
961 $log->error("Cannot create method $attribute for class $self->{class}");
962 #fallback to old autoload method if create method fail
963 unless (ref($self->{$attribute}) eq 'ARRAY') {
964 my ($package, $filename, $line) = caller;
965 $log->error("Wrong structure in field $attribute called from $package/$filename/$line \n".Data::Dumper::Dumper($self));
966 $self->{$attribute} = [];
967 }
968 $self->{$attribute} = [@_] if (@_);
969 return @{$self->{$attribute}};
970 }
971 #todo: �������� ������ � images ���������� ����� ��� ����� ������
972 } else {
973 #todo: ��������� ������� �����
974 my $funct = "
975 my \$self = shift;
976 \$self->{$attribute} = shift if (\@_);
977 return \$self->{$attribute};";
978
979 if (create_method($class, $attribute, $funct)) {
980 return $self->$attribute(@_);
981 } else {
982 $log->error("Cannot create method $attribute for class $self->{class}");
983 #fallback to old autoload method if create method fail
984 $self->{$attribute} = shift if (@_);
985 return $self->{$attribute};
986 }
987 }
988 }
989
990 sub eval_dump {
991 no strict 'vars';
992 return {} unless ${$_[0]};
993 return eval ${$_[0]};
994 }
995
996 sub create_method {
997 my ($class, $sub_name, $code) = @_;
998
999 unless ($class and $sub_name and $code) {
1000 $log->error("Wrong call create_method $class/$sub_name/$code");
1001 return 0;
1002 }
1003
1004 my $string = "package $class;\n\nsub $sub_name {\n$code\n}\n\n1;";
1005 eval $string;
1006
1007 if ($@) {
1008 $log->error("Cannot create method $sub_name for class $class because $@ (method code:\n$string\n)");
1009 return 0;
1010 } else {
1011 $log->info("Method '$sub_name' for class '$class' (method code:\n$string\n) created ok") if ($DEBUG_CORE);
1012 return 1;
1013 }
1014 }
1015
1016 ######################################## ONLY FOR INTERNAL USE!!!! #################################################
1017 #todo �������� �������� ��� ���� ������� ������ �� ��������� ��� � ��� 1 table � �� 5 ������
1018 sub _get_table {
1019 my ($self, %opts) = @_;
1020
1021 my $class_table;
1022
1023 my $table=$opts{table};
1024 my $class=$opts{class} || ref $self || $self;
1025
1026 #������ ������� � %opts
1027 if ($table and $table->can('new')) {
1028 $class_table=$table;
1029 #����� ������� �� ������
1030 } elsif ($class and !ref($class)) {
1031 unless ($class->can('class_table')) {
1032 $log->error("$class cannot class_table");
1033 return undef;
1034 }
1035 $class_table=$class->class_table();
1036 #����� ������� �� ������� ������ � ������
1037 } elsif ($class and ref($class) eq 'ARRAY' and @$class) {
1038 unless ($class->[0]->can('class_table')) {
1039 $log->error("$class->[0] cannot class_table");
1040 return undef;
1041 }
1042 $class_table=$class->[0]->class_table();
1043 #����� �������������
1044 } else {
1045 $class_table='SQL::DocumentTable';
1046 }
1047
1048 if ($class_table->can('new')) {
1049 return $class_table->new();
1050 } else {
1051 $log->error("$class_table cannot new!!!!");
1052 return undef;
1053 }
1054 }
1055
1056 #######################################################################################################################
1057 ########## OLD CODE FOR COMPATIBILITY #################################################################################
1058 #######################################################################################################################
1059 sub structure {
1060 my $self = shift;
1061 my $class = ref($self);
1062 {
1063 no strict 'refs';
1064 return @${$class.'::structure'};
1065 }
1066 }
1067
1068
1069 # ��������� ��� �������� �������������...
1070 sub get_image {
1071 my $self = shift;
1072 $self->get_data(shift);
1073 }
1074
1075 sub raw_restore {
1076 my $self = shift;
1077 do { $log->error("����� restore() ����� �������� ������ � ��������, �� �� �������"); die } unless ref $self;
1078 do { $log->warning("����� ������ Contenido\:\:Object\:\:raw_restore() ��� �������� �������������� ��� ������"); return undef } unless $self->id;
1079 $self->restore_extras();
1080 }
1081
1082 sub init {
1083 my $self = shift;
1084 my $class = ref($self) || $self;
1085 $self->class_init();
1086 {
1087 no strict 'refs';
1088 $self->{attributes} = ${$class.'::attributes'};
1089 }
1090 return 1;
1091 }
1092
1093 sub get_file_name {
1094 my $self = shift;
1095
1096 do { $log->error("����� get_file_name ����� �������� ������ � ��������, �� �� �������"); die } unless ref $self;
1097
1098 my @date;
1099 my $time = time;
1100
1101 if ($self->{"dtime"} and $self->{"dtime"} =~ /^(\d{4})-(\d{2})-(\d{2})/) {
1102 @date = ($1, $2, $3);
1103 } else {
1104 @date = (localtime $time)[5, 4, 3]; $date[0] += 1900; $date[1] += 1;
1105 }
1106
1107 my $component_class = lc((reverse split "::", ref $self)[0]);
1108 my $component_date = sprintf "%04d/%02d/%02d", @date;
1109 my $component_time_rand = sprintf "%010d_%05d", $time, int rand 99999;
1110
1111 return join "/", $component_class, $component_date, $component_time_rand;
1112 }
1113
1114 sub get {
1115 my ( $self, %opts ) = @_;
1116 my $class = ref $self || $self;
1117 my $local_keeper = (ref($self) and ref($self->keeper)) ? $self->keeper : $keeper;
1118 delete $opts{class};
1119 return $keeper->get_documents( class => $class, %opts );
1120 }
1121
1122 sub contenido_is_available {
1123 return 1;
1124 }
1125
1126 sub contenido_status_style {
1127 return;
1128 }
1129
1130 sub memcached_expire {
1131 return $_[0]->keeper->state->memcached_object_expire;
1132 }
1133
1134 114 ahitrov # ----------------------------------------------------------------------------
1135 127 ahitrov # ����� _store_image() ��������� �������, ����������� � ���� image ��� images
1136 114 ahitrov #
1137 # ������ �������������:
1138 127 ahitrov # $document->_store_image( INPUT, attr => 'fieldname' )
1139 114 ahitrov # ----------------------------------------------------------------------------
1140 sub _store_image {
1141 my $self = shift;
1142 do { $log->error("����� delete() ����� �������� ������ � ��������, �� �� �������"); die } unless ref($self);
1143
1144 my $input = shift;
1145 my (%opts) = @_;
1146
1147 return Contenido::File::store_image( $input, object => $self, attr => $opts{attr} );
1148 }
1149
1150 127 ahitrov # ----------------------------------------------------------------------------
1151 # ����� _delete_image() ������� �����, ��������� � ����� image ��� images.
1152 # �������� ��� ����-�����
1153 #
1154 # ������ �������������:
1155 # $document->_store_image( $image_attr_structure )
1156 # ----------------------------------------------------------------------------
1157 114 ahitrov sub _delete_image {
1158 my $self = shift;
1159 my $IMAGE = shift;
1160
1161 return Contenido::File::remove_image( $IMAGE );
1162 }
1163
1164 127 ahitrov # ----------------------------------------------------------------------------
1165 # ����� _store_binary() ��������� ������������ �������� ����, ����������� � ���� multimedia ��� multimedia_new
1166 #
1167 # ������ �������������:
1168 # $document->_store_binary( INPUT, attr => 'fieldname' )
1169 # ----------------------------------------------------------------------------
1170 sub _store_binary {
1171 my $self = shift;
1172 do { $log->error("����� delete() ����� �������� ������ � ��������, �� �� �������"); die } unless ref($self);
1173
1174 my $input = shift;
1175 my (%opts) = @_;
1176
1177 return Contenido::File::store_binary( $input, object => $self, attr => $opts{attr} );
1178 }
1179
1180 # ----------------------------------------------------------------------------
1181 # ����� _delete_binary() ������� �����, ��������� � ����� multimedia ��� multimedia_new.
1182 # �� �������� ������ ����-�����
1183 #
1184 # ������ �������������:
1185 # $document->_delete_binary( $binary_attr_structure )
1186 # ----------------------------------------------------------------------------
1187 sub _delete_binary {
1188 my $self = shift;
1189 my $BINARY = shift;
1190
1191 return Contenido::File::remove_binary( $BINARY );
1192 }
1193
1194 3 ahitrov@rambler.ru 1;
1195