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