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 487 ahitrov my @funct_default_fields = ("class=>'$class'", "keeper=>\$keeper", "__light=>\$light");
153 3 ahitrov@rambler.ru 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 218 ahitrov if ( exists $_->{db_field} && $_->{db_field} ) {
463 $value = undef if (defined($value) and $value eq '') and (lc($_->{db_type}) eq 'float' or lc($_->{db_type} eq 'integer'));
464 $value = undef if lc $_->{db_type} eq 'integer[]' && ref $value ne 'ARRAY';
465 $value = undef if lc $_->{db_type} eq 'integer_ref[]' && ref $value ne 'ARRAY';
466 }
467 3 ahitrov@rambler.ru
468 #���������� readonly ���� � ��������� ��� ���� id
469 if ($self->id() and $_->{readonly}) {
470
471 #��� ���� � ���� � ��������
472 } elsif (!$_->{db_field}) {
473
474 #��������� default ���� ��� ���� � ����� ���� ��� ��� �������� � ����
475 } elsif (defined($_->{default}) and ($_->{auto} or !defined($value))) {
476 push @default_fields, $_->{db_field};
477 push @default_values, $_->{default};
478 push @default_pairs, "$_->{db_field}=$_->{default}";
479
480 #���������� auto ��� default
481 } elsif ($_->{auto}) {
482
483 #��������� �������� �����
484 } elsif (defined($value)) {
485 push @fields, $_->{db_field};
486 if ($_->{db_type} eq 'integer[]') {
487 push @values, '{'.join(',', grep { $_ } @$value).'}';
488 } elsif ($_->{db_type} eq 'integer_ref[]') {
489 push @values, '{'.join(',', grep { $_ } @$value).'}';
490 } else {
491 #some special work for bytea column type
492 push @binary_fields, scalar(@fields) if ($_->{db_type} eq 'bytea');
493 if ($state->db_encode_data) {
494 push @values, Encode::decode($state->db_encode_data, $value, Encode::FB_HTMLCREF);
495 } else {
496 push @values, $value;
497 }
498 }
499
500 #undef to NULL or empty array
501 } else {
502 push @default_fields, $_->{db_field};
503 push @default_values, 'NULL';
504 push @default_pairs, "$_->{db_field}=NULL";
505 }
506 }
507
508 #���� ����������� toast �� ���������� �� 1 sql ������ � extra ����
509 487 ahitrov if (($self->keeper->store_method() eq 'toast') and $self->class_table->have_extra and !$self->{__light}) {
510 3 ahitrov@rambler.ru push @fields, 'data';
511 push @values, $self->_create_extra_dump();
512 }
513
514
515 my $values_string = '';
516 my $mode = 'update';
517 if ($self->id()) {
518 if (@fields) {
519 $values_string = join(' = ?, ', @fields).' = ?';
520 $values_string .= ', '.join(', ', @default_pairs) if (@default_pairs);
521 #��� ������� �������� �������� ������ �� @default_pairs
522 } else {
523 $values_string = join(', ', @default_pairs) if (@default_pairs);
524 }
525 my $sql='UPDATE '.$self->class_table->db_table.' SET '.$values_string." WHERE ".$self->class_table()->id_field()." = ?";
526
527 my $sth=$self->keeper->TSQL->prepare_cached($sql, {}, 1) || return $self->t_abort();
528 #settin special escape for bytea column type!!!
529 foreach (@binary_fields) {
530 $sth->bind_param($_, undef, {pg_type => DBD::Pg::PG_BYTEA});
531 }
532 unless ($sth->execute(@values, $self->{id})) {
533 $log->error("DBI execute error on $sql\n".Data::Dumper::Dumper(\@values));
534 $sth->finish();
535 return $self->t_abort();
536 }
537 $sth->finish();
538
539 487 ahitrov if (($self->keeper->store_method() ne 'toast') and $self->class_table->have_extra and !$self->{__light}) {
540 3 ahitrov@rambler.ru $self->store_extras(mode => $mode) || return $self->t_abort();
541 }
542
543 } else {
544 $mode = 'insert';
545 if (@fields) {
546 $values_string = '?, 'x(scalar (@fields)-1).'?';
547 $values_string .= ', '.join(', ', @default_values) if (@default_values);
548 #��� ������� �������� �������� ������ �� @default_pairs
549 } else {
550 $values_string = join(', ', @default_values) if (@default_values);
551 }
552 my $sql='INSERT INTO '.$self->class_table->db_table.' ('.join(', ', (@fields, @default_fields)).') VALUES ('.$values_string.')';
553
554 my $sth=$self->keeper->TSQL->prepare_cached($sql, {}, 1) || return $self->t_abort();
555 #settin special escape for bytea column type!!!
556 foreach (@binary_fields) {
557 $sth->bind_param($_, undef, {pg_type => DBD::Pg::PG_BYTEA});
558 }
559 unless ($sth->execute(@values)) {
560 $log->error("DBI execute error on $sql\n".Data::Dumper::Dumper(\@values));
561 $sth->finish();
562 return $self->t_abort();
563 }
564 $sth->finish();
565
566 487 ahitrov my $id = $self->keeper->TSQL->selectrow_array("SELECT currval('".$self->class_table->db_id_sequence()."')");
567 3 ahitrov@rambler.ru $self->id($id);
568 return $self->t_abort("��������� �������� �������� �������������") if (! defined($self->{id}) || ($self->{id} <= 0));
569
570 487 ahitrov if (($self->keeper->store_method() ne 'toast') and $self->class_table->have_extra and !$self->{__light}) {
571 3 ahitrov@rambler.ru $self->store_extras(mode => $mode) || return $self->t_abort();
572 }
573
574 }
575
576 $self->post_store(mode => $mode);
577
578 $self->keeper->t_finish();
579
580 $self->post_finish_store();
581
582 $self->drop_cache($mode) if ($self->keeper->state()->memcached_enable());
583
584 return 1;
585 }
586
587 # ----------------------------------------------------------------------------
588 # ����� delete() ��� �������� ������� �� ���� ������.
589 #
590 # ������ �������������:
591 # $document->delete()
592 # ----------------------------------------------------------------------------
593 sub delete {
594 my $self = shift;
595 my (%opts) = @_;
596 do { $log->error("����� delete() ����� �������� ������ � ��������, �� �� �������"); die } unless ref($self);
597
598 return undef if ($self->keeper->state->readonly());
599
600 unless ($self->pre_delete()) {
601 $log->error("pre_delete call failed!");
602 return undef;
603 }
604
605 my $keeper = $self->keeper;
606 do { $log->error("� ������� ��������� �� ���������� ������ �� ���� ������"); die } unless ref($keeper);
607
608 if ( exists $opts{attachments} && $opts{attachments} ) {
609 my @props = $self->structure();
610 if ( @props ) {
611 @props = grep { $_->{type} =~ /^(image|images|multimedia_new)$/ } @props;
612 foreach my $prop ( @props ) {
613 my $att = $self->get_image($prop->{attr});
614 if ( $prop->{type} eq 'image' ) {
615 if ( ref $att && exists $att->{filename} && $att->{filename} ) {
616 Contenido::File::remove( $att->{filename} );
617 }
618 56 ahitrov@rambler.ru if ( exists $att->{mini} && ref $att->{mini} eq 'HASH' ) {
619 Contenido::File::remove( $att->{mini}{filename} ) if exists $att->{mini}{filename};
620 foreach my $val ( values %{ $att->{mini} } ) {
621 if ( ref $val && exists $val->{filename} && $val->{filename} && ($val->{filename} ne $att->{mini}{filename}) ) {
622 Contenido::File::remove( $val->{filename} );
623 3 ahitrov@rambler.ru }
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 56 ahitrov@rambler.ru if ( exists $img->{mini} && ref $img->{mini} eq 'HASH' ) {
635 Contenido::File::remove( $img->{mini}{filename} ) if exists $img->{mini}{filename};
636 foreach my $val ( values %{ $img->{mini} } ) {
637 if ( ref $val && exists $val->{filename} && $val->{filename} && ($val->{filename} ne $img->{mini}{filename}) ) {
638 Contenido::File::remove( $val->{filename} );
639 3 ahitrov@rambler.ru }
640 }
641 }
642 }
643 } elsif ( $prop->{type} eq 'multimedia_new' ) {
644 if ( ref $att && exists $att->{filename} && $att->{filename} ) {
645 Contenido::File::remove( $att->{filename} );
646 }
647 }
648 }
649 }
650 }
651 do { $log->warning("����� ������ delete() ��� �������� �������������� ��� ��������"); return undef }
652 56 ahitrov@rambler.ru unless ($self->{id});
653 3 ahitrov@rambler.ru $keeper->t_connect() || do { $keeper->error(); return undef; };
654 $keeper->TSQL->do("DELETE FROM ".$self->class_table->db_table." WHERE id = ?", {}, $self->id) || return $self->t_abort();
655
656 # �������� ������ ����� ��������� � ������� �����������...
657 my %document_links;
658 if ( $keeper->state->{available_links} && ref $keeper->state->{available_links} eq 'ARRAY' ) {
659 foreach my $classlink ( @{ $keeper->state->{available_links} } ) {
660 my $sources = $classlink->available_sources;
661 if ( ref $sources eq 'ARRAY' && @$sources ) {
662 $document_links{$classlink->class_table->db_table}{source} = 1 if grep { $self->class eq $_ } @$sources;
663 }
664 my $dests = $classlink->available_destinations;
665 if ( ref $dests eq 'ARRAY' && @$dests ) {
666 46 ahitrov@rambler.ru $document_links{$classlink->class_table->db_table}{dest} = 1 if grep { $self->class eq $_ } @$dests;
667 3 ahitrov@rambler.ru }
668 }
669 foreach my $tablename ( keys %document_links ) {
670 my (@wheres, @values);
671 if ( exists $document_links{$tablename}{source} ) {
672 push @wheres, "(source_id = ? AND source_class = ?)";
673 push @values, ( $self->id, $self->class );
674 }
675 if ( exists $document_links{$tablename}{dest} ) {
676 push @wheres, "(dest_id = ? AND dest_class = ?)";
677 push @values, ( $self->id, $self->class );
678 }
679 my $request = "DELETE FROM $tablename WHERE ".join (' OR ', @wheres);
680 warn "DELETE LINKS. Request: [$request]\n" if $DEBUG;
681 warn "Values: [".join(', ', @values)."]\n" if $DEBUG;
682 $keeper->TSQL->do($request, {}, @values) || return $self->t_abort();
683 }
684 } else {
685 $keeper->TSQL->do("DELETE FROM links WHERE source_id = ? AND source_class = ? ", {}, $self->id, $self->class) || return $self->t_abort();
686 $keeper->TSQL->do("DELETE FROM links WHERE dest_id = ? AND dest_class = ? ", {}, $self->id, $self->class) || return $self->t_abort();
687 }
688 $keeper->t_finish();
689
690 $self->post_delete();
691
692 $self->drop_cache('delete') if ($keeper->state()->memcached_enable());
693
694 return 1;
695 }
696
697 # ----------------------------------------------------------------------------
698 # ����� links() ���������� ������ �������� ���� Contenido::Link
699 #
700 # ������ �������������:
701 # $document->links([�����])
702 # ----------------------------------------------------------------------------
703 sub links {
704 my ($self, $lclass, $direction, %opts) = @_;
705 do { $log->error("����� ->links() ����� �������� ������ � ��������, �� �� �������"); die } unless ref($self);
706
707 do { $log->error("� ������� ��������� �� ���������� ������ �� ���� ������"); die } unless ref($self->keeper);
708
709 do { $log->warning("����� ������ ->links() ��� �������� �������������� ���������-���������"); return () } unless ($self->id() > 0);
710
711 my $check = defined $direction ? 'dest_id' : 'source_id';
712
713 $opts{$check} = $self->id();
714
715 if (defined($lclass) && (length($lclass) > 0)) {
716 56 ahitrov@rambler.ru $opts{class} = $lclass;
717 3 ahitrov@rambler.ru }
718
719 my @links = $self->keeper->get_links(%opts);
720
721 $self->{links} = \@links;
722 return @links;
723 }
724
725
726 sub linked_to {
727 my ($self, $lclass) = @_;
728 $self->links($lclass, 1);
729 }
730
731
732 # ----------------------------------------------------------------------------
733 # ��������� �����. ������ ���� ����������� ����� �����...
734 # � �������� source_id ��������� ������������� �������, � �������� $dest_id -
735 # ��������.
736 #
737 # ������ �������������:
738 # $document->set_link($lclass, $dest_id)
739 #
740 # �������� �� ������������ - � ��������� ����� ���� ��������� ����������
741 # ������.
742 # ----------------------------------------------------------------------------
743 sub set_link {
744 my ($self, $lclass, $dest_id, $dest_class, @opts) = @_;
745 do { $log->error("����� ->set_link() ������ � ������������ ���-��� ����������"); die } if @opts % 2;
746 do { $log->error("����� ->set_link() ����� �������� ������ � ��������, �� �� �������"); die } unless ref($self);
747 my %opts = @opts;
748
749 return undef if ($self->keeper->state->readonly());
750
751 do { $log->warning("����� ������ ->set_link() ��� �������� �������������� ���������-���������"); return undef } unless ($self->id() > 0);
752 do { $log->warning("����� ������ ->set_link() ��� �������� �������������� ���������-����"); return undef } unless ($dest_id >= 0);
753 114 ahitrov do { $log->warning("����� ������ ->set_link() ��� �������� ������ �����"); } unless defined($lclass) && length($lclass);
754 3 ahitrov@rambler.ru
755 # ������� ������ �����...
756 my $link = $lclass->new($self->keeper);
757
758 $link->dest_id($dest_id);
759 $link->dest_class($dest_class);
760
761 $link->status(1);
762
763 $link->source_id($self->id());
764 $link->source_class($self->class());
765
766 while (my ($k,$v) = each %opts) {
767 56 ahitrov@rambler.ru $link->{$k} = $v;
768 3 ahitrov@rambler.ru }
769
770 if ($link->store())
771 {
772 56 ahitrov@rambler.ru return $link->id;
773 3 ahitrov@rambler.ru } else {
774 56 ahitrov@rambler.ru return undef;
775 3 ahitrov@rambler.ru }
776 }
777
778 # -------------------------------------------------------------------
779 # ���������� ������ � ������������� ���.
780 #
781 sub prepare_for_cache {
782 my $self = shift;
783
784 do { $log->error("����� ->prepare_for_cache() ����� �������� ������ � ��������, �� �� �������"); die } unless ref($self);
785
786 my $hash = {};
787
788 foreach ( $self->structure() ) {
789 56 ahitrov@rambler.ru $hash->{$_->{attr}} = $self->{$_->{attr}} if defined $self->{$_->{attr}};
790 3 ahitrov@rambler.ru }
791 bless $hash, $self->class();
792 return $hash;
793 }
794
795 # -------------------------------------------------------------------
796 # ��������������� ����������� ������ �� �������������� ����.
797 # ��� ��� ���� ������������ � ����������� ������.
798 # -------------------------------------------------------------------
799 sub recover_from_cache {
800 my $self = shift;
801 my $keeper = shift;
802
803 do { $log->error("����� ->recover_from_cache() ����� �������� ������ � ��������, �� �� �������"); die } unless ref($self);
804 do { $log->error("� ������� ��������� �� ���������� ������ �� ���� ������"); die } unless ref($keeper);
805
806 #�� ����� ��� bless ���������... 100% ���� �� ������ � ��������� ����� �� �� ���� ����� ����� ������
807 $self->init();
808 $self->keeper($keeper);
809
810 return 1;
811 }
812
813 # -------------------------------------------------------------------
814 # ���������� ���:
815 # {��������1 => [���1, ���2, ...], ��������2 => [���1, ���2, ...], ...}
816 # �.�. ��� ������� �������� �������� ������ ���� ������ � ����,
817 # ������� ���� �������.
818 # ��������� ������� ��������: insert, update, delete
819 # ��� ����� ������� ������ ������ ���� ����� ������ ���� �������������
820 # � ������ ������ �������
821 #
822 sub dependencies {
823 my ($self, $mode) = @_;
824
825 my @keys = ($self->get_object_key,);
826 my $object_unique_key = $self->get_object_unique_key;
827 push @keys, $object_unique_key if defined $object_unique_key;
828
829 return
830 ($mode eq 'delete') || ($mode eq 'insert') || ($mode eq 'update')
831 ? \@keys
832 : [];
833 }
834
835 # -------------------------------------------------------------------
836 # ������� �� ���� �����, �������� ��� ������ dependencies().
837 # ������ ������:
838 # $group->drop_cache('update');
839 #
840 sub drop_cache {
841 my $self = shift;
842 my $mode = shift;
843
844 do { $log->error("����� ->drop_cache() ����� �������� ������ � ��������, �� �� �������"); die } unless ref($self);
845
846 my $keeper = $self->keeper;
847 do { $log->error("� ������� ��������� �� ���������� ������ �� ���� ������"); die } unless ref($keeper);
848
849 my $dependencies = $self->dependencies($mode, @_);
850
851 my @not_deleted = ();
852 if ( defined($dependencies) && (ref($dependencies) eq 'ARRAY') ) {
853 for (@$dependencies) {
854 my $res = $self->keeper->MEMD ? $self->keeper->MEMD->delete($_) : undef;
855 push @not_deleted, $_ unless $res;
856 $keeper->MEMD->delete($_) if ($keeper->MEMD);
857 }
858 }
859 return @not_deleted;
860 }
861
862
863 sub keeper {
864 my $self = shift;
865 my $project_keeper = shift;
866
867 do { $log->error("����� keeper() ����� �������� ������ � ��������, �� �� �������"); die } unless ref($self);
868
869 if ($project_keeper && ref $project_keeper) {
870 $self->{keeper} = $project_keeper;
871 return $project_keeper;
872 }
873 return $self->{keeper} && ref $self->{keeper} ? $self->{keeper} : $keeper;
874 }
875
876 #������ ������� ��� init_from_db ����� ������������������� ����� ���� ����
877 sub init_from_db {
878 my $self = shift;
879
880 my $class = ref($self) || $self;
881
882 #������ �� ����������� �������� ���� class_init �� �����������
883 if (defined($_[-1]) and ($_[-1] eq 'RECURSIVE CALL FLAG!')) {
884 do { $log->error("$class cannot be initialized (->class_init dont work) (recursive call) !!!"); die };
885 }
886
887 #���� ���� ����� �� �������� ������� ��� ��� �� ������������������ �� ����������� �������������������
888 #������ ������������� ������ init_from_db ��������� �� ref �� �����
889 if ($class and $class->isa('Contenido::Object')) {
890 no strict 'refs';
891 if (${$class.'::class_init_done'}) {
892 do { $log->error("$class already initialized but DONT HAVE init_from_db method!!!"); die };
893 } else {
894 if ($self->class_init()) {
895 return $self->init_from_db(@_, 'RECURSIVE CALL FLAG!');
896 } else {
897 do { $log->error("$class cannot be initialized (->class_init dont work) !!!"); die };
898 }
899 }
900 } else {
901 do { $log->error("$class cannot be initialized (not Contenido::Object child class) !!!"); die };
902 }
903 }
904
905 # ----------------------------------------------------------------------------
906 # ��� ����� AUTOLOAD. ����� ������� ��� ���������/������ �����...
907 # ������ 1.0
908 # ������ �� ���������� ����������� ����� ������� ���� ����
909 # ----------------------------------------------------------------------------
910 sub AUTOLOAD {
911 my $self = shift;
912 my $attribute = $AUTOLOAD;
913
914 $log->info("$self calling AUTOLOAD method: $attribute") if ($DEBUG_CORE);
915
916 $attribute=~s/^.*:://;
917
918 my $class = ref($self);
919 unless ($class and $class->isa('Contenido::Object')) {
920
921 my $mason_comp = ref($HTML::Mason::Commands::m) ? $HTML::Mason::Commands::m->current_comp() : undef;
922 my $mason_file = ref($mason_comp) ? $mason_comp->path : undef;
923 my ($package, $filename, $line) = caller;
924
925 $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));
926 if (wantarray) { return (); } else { return undef; }
927 }
928
929 #�������� ���� ���� �� �� ����� �������� ������������� ������ ���� �� �� ����� �� ������ ���� ������ ��� ��������������������� ������
930 {
931 no strict 'refs';
932 unless (${$class.'::class_init_done'}) {
933 my ($package, $filename, $line) = caller;
934 $log->error("AUTOLOAD called method '$attribute' for not initialised class ($class) from '$package/$filename/$line'");
935 if (wantarray) { return (); } else { return undef; }
936 }
937 }
938
939 if (! exists($self->{attributes}->{$attribute})) {
940
941 my $mason_comp = ref($HTML::Mason::Commands::m) ? $HTML::Mason::Commands::m->current_comp() : undef;
942 my $mason_file = ref($mason_comp) ? $mason_comp->path : undef;
943 my ($package, $filename, $line) = caller;
944
945 $log->error(ref($self)."): ����� ������, ��� �������� �� ���������� ��������������� ��������: ->$attribute() called from $package/$filename/$line ".($mason_file ? "called from $mason_file" : '')."\n".Data::Dumper::Dumper($self));
946 if (wantarray) { return (); } else { return undef; }
947 #special work with ARRAY types
948 } elsif ($self->{attributes}->{$attribute} eq 'ARRAY') {
949 my $funct = "
950 use Contenido::Globals;
951 my \$self = shift;
952 unless (ref(\$self->{$attribute}) eq 'ARRAY') {
953 my (\$package, \$filename, \$line) = caller;
954 \$log->error(\"Wrong structure in field $attribute called from \$package/\$filename/\$line \\n\".Data::Dumper::Dumper(\$self)) if (\$self->{$attribute});;
955 \$self->{$attribute} = [];
956 }
957 \$self->{$attribute} = [\@_] if (\@_);
958 return \@{\$self->{$attribute}};";
959
960 if (create_method($class, $attribute, $funct)) {
961 return $self->$attribute(@_);
962 } else {
963 $log->error("Cannot create method $attribute for class $self->{class}");
964 #fallback to old autoload method if create method fail
965 unless (ref($self->{$attribute}) eq 'ARRAY') {
966 my ($package, $filename, $line) = caller;
967 $log->error("Wrong structure in field $attribute called from $package/$filename/$line \n".Data::Dumper::Dumper($self));
968 $self->{$attribute} = [];
969 }
970 $self->{$attribute} = [@_] if (@_);
971 return @{$self->{$attribute}};
972 }
973 #todo: �������� ������ � images ���������� ����� ��� ����� ������
974 } else {
975 #todo: ��������� ������� �����
976 my $funct = "
977 my \$self = shift;
978 \$self->{$attribute} = shift if (\@_);
979 return \$self->{$attribute};";
980
981 if (create_method($class, $attribute, $funct)) {
982 return $self->$attribute(@_);
983 } else {
984 $log->error("Cannot create method $attribute for class $self->{class}");
985 #fallback to old autoload method if create method fail
986 $self->{$attribute} = shift if (@_);
987 return $self->{$attribute};
988 }
989 }
990 }
991
992 sub eval_dump {
993 no strict 'vars';
994 return {} unless ${$_[0]};
995 return eval ${$_[0]};
996 }
997
998 sub create_method {
999 my ($class, $sub_name, $code) = @_;
1000
1001 unless ($class and $sub_name and $code) {
1002 $log->error("Wrong call create_method $class/$sub_name/$code");
1003 return 0;
1004 }
1005
1006 my $string = "package $class;\n\nsub $sub_name {\n$code\n}\n\n1;";
1007 eval $string;
1008
1009 if ($@) {
1010 $log->error("Cannot create method $sub_name for class $class because $@ (method code:\n$string\n)");
1011 return 0;
1012 } else {
1013 $log->info("Method '$sub_name' for class '$class' (method code:\n$string\n) created ok") if ($DEBUG_CORE);
1014 return 1;
1015 }
1016 }
1017
1018 ######################################## ONLY FOR INTERNAL USE!!!! #################################################
1019 #todo �������� �������� ��� ���� ������� ������ �� ��������� ��� � ��� 1 table � �� 5 ������
1020 sub _get_table {
1021 my ($self, %opts) = @_;
1022
1023 my $class_table;
1024
1025 my $table=$opts{table};
1026 my $class=$opts{class} || ref $self || $self;
1027
1028 #������ ������� � %opts
1029 if ($table and $table->can('new')) {
1030 $class_table=$table;
1031 #����� ������� �� ������
1032 } elsif ($class and !ref($class)) {
1033 unless ($class->can('class_table')) {
1034 $log->error("$class cannot class_table");
1035 return undef;
1036 }
1037 $class_table=$class->class_table();
1038 #����� ������� �� ������� ������ � ������
1039 } elsif ($class and ref($class) eq 'ARRAY' and @$class) {
1040 unless ($class->[0]->can('class_table')) {
1041 $log->error("$class->[0] cannot class_table");
1042 return undef;
1043 }
1044 $class_table=$class->[0]->class_table();
1045 #����� �������������
1046 } else {
1047 $class_table='SQL::DocumentTable';
1048 }
1049
1050 if ($class_table->can('new')) {
1051 return $class_table->new();
1052 } else {
1053 $log->error("$class_table cannot new!!!!");
1054 return undef;
1055 }
1056 }
1057
1058 #######################################################################################################################
1059 ########## OLD CODE FOR COMPATIBILITY #################################################################################
1060 #######################################################################################################################
1061 sub structure {
1062 my $self = shift;
1063 my $class = ref($self);
1064 {
1065 no strict 'refs';
1066 return @${$class.'::structure'};
1067 }
1068 }
1069
1070
1071 # ��������� ��� �������� �������������...
1072 sub get_image {
1073 my $self = shift;
1074 $self->get_data(shift);
1075 }
1076
1077 sub raw_restore {
1078 my $self = shift;
1079 do { $log->error("����� restore() ����� �������� ������ � ��������, �� �� �������"); die } unless ref $self;
1080 do { $log->warning("����� ������ Contenido\:\:Object\:\:raw_restore() ��� �������� �������������� ��� ������"); return undef } unless $self->id;
1081 $self->restore_extras();
1082 }
1083
1084 sub init {
1085 my $self = shift;
1086 my $class = ref($self) || $self;
1087 $self->class_init();
1088 {
1089 no strict 'refs';
1090 $self->{attributes} = ${$class.'::attributes'};
1091 }
1092 return 1;
1093 }
1094
1095 sub get_file_name {
1096 my $self = shift;
1097
1098 do { $log->error("����� get_file_name ����� �������� ������ � ��������, �� �� �������"); die } unless ref $self;
1099
1100 my @date;
1101 my $time = time;
1102
1103 if ($self->{"dtime"} and $self->{"dtime"} =~ /^(\d{4})-(\d{2})-(\d{2})/) {
1104 @date = ($1, $2, $3);
1105 } else {
1106 @date = (localtime $time)[5, 4, 3]; $date[0] += 1900; $date[1] += 1;
1107 }
1108
1109 my $component_class = lc((reverse split "::", ref $self)[0]);
1110 my $component_date = sprintf "%04d/%02d/%02d", @date;
1111 my $component_time_rand = sprintf "%010d_%05d", $time, int rand 99999;
1112
1113 return join "/", $component_class, $component_date, $component_time_rand;
1114 }
1115
1116 sub get {
1117 my ( $self, %opts ) = @_;
1118 my $class = ref $self || $self;
1119 my $local_keeper = (ref($self) and ref($self->keeper)) ? $self->keeper : $keeper;
1120 delete $opts{class};
1121 return $keeper->get_documents( class => $class, %opts );
1122 }
1123
1124 sub contenido_is_available {
1125 return 1;
1126 }
1127
1128 sub contenido_status_style {
1129 return;
1130 }
1131
1132 sub memcached_expire {
1133 return $_[0]->keeper->state->memcached_object_expire;
1134 }
1135
1136 114 ahitrov # ----------------------------------------------------------------------------
1137 127 ahitrov # ����� _store_image() ��������� �������, ����������� � ���� image ��� images
1138 114 ahitrov #
1139 # ������ �������������:
1140 127 ahitrov # $document->_store_image( INPUT, attr => 'fieldname' )
1141 364 ahitrov # $document->_store_image( INPUT, prop => $prophash )
1142 114 ahitrov # ----------------------------------------------------------------------------
1143 sub _store_image {
1144 my $self = shift;
1145 do { $log->error("����� delete() ����� �������� ������ � ��������, �� �� �������"); die } unless ref($self);
1146
1147 my $input = shift;
1148 my (%opts) = @_;
1149
1150 364 ahitrov return Contenido::File::store_image( $input, object => $self, %opts );
1151 114 ahitrov }
1152
1153 127 ahitrov # ----------------------------------------------------------------------------
1154 # ����� _delete_image() ������� �����, ��������� � ����� image ��� images.
1155 # �������� ��� ����-�����
1156 #
1157 # ������ �������������:
1158 # $document->_store_image( $image_attr_structure )
1159 # ----------------------------------------------------------------------------
1160 114 ahitrov sub _delete_image {
1161 my $self = shift;
1162 my $IMAGE = shift;
1163
1164 return Contenido::File::remove_image( $IMAGE );
1165 }
1166
1167 127 ahitrov # ----------------------------------------------------------------------------
1168 # ����� _store_binary() ��������� ������������ �������� ����, ����������� � ���� multimedia ��� multimedia_new
1169 #
1170 # ������ �������������:
1171 # $document->_store_binary( INPUT, attr => 'fieldname' )
1172 # ----------------------------------------------------------------------------
1173 sub _store_binary {
1174 my $self = shift;
1175 do { $log->error("����� delete() ����� �������� ������ � ��������, �� �� �������"); die } unless ref($self);
1176
1177 my $input = shift;
1178 my (%opts) = @_;
1179
1180 return Contenido::File::store_binary( $input, object => $self, attr => $opts{attr} );
1181 }
1182
1183 # ----------------------------------------------------------------------------
1184 # ����� _delete_binary() ������� �����, ��������� � ����� multimedia ��� multimedia_new.
1185 # �� �������� ������ ����-�����
1186 #
1187 # ������ �������������:
1188 # $document->_delete_binary( $binary_attr_structure )
1189 # ----------------------------------------------------------------------------
1190 sub _delete_binary {
1191 my $self = shift;
1192 my $BINARY = shift;
1193
1194 return Contenido::File::remove_binary( $BINARY );
1195 }
1196
1197 3 ahitrov@rambler.ru 1;
1198