summaryrefslogtreecommitdiff
path: root/bucky2/lib/Bucky.pm
blob: 181c1ae14c2ee2cc807c8b8bf80b7d501f7da0d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package Bucky;

use strict 'vars';

use Data::Dumper;
use Common;

use localbucky qw("config");

use Bucky::DB;
use Bucky::Keyword;
use Bucky::Search;
use Bucky::Session;
use Bucky::Thread;
# use Bucky::Comment;
# use Bucky::File;
use Bucky::SVN;

our $TYPE_Bucky			= "bucky";
our $TYPE_Keyword		= "keyword";
our $TYPE_Thread		= "thread";
our $TYPE_Comment		= "comment";
our $TYPE_File			= "file";

our $VALID_TYPES =
	{
	$TYPE_Bucky => 1,
	$TYPE_Keyword => 1,
	$TYPE_Thread => 1,
	$TYPE_Comment => 1,
	$TYPE_File => 1,
	};
sub valid_types
	{ return $VALID_TYPES; }
sub is_valid_type
	{
	my ($self, $type) = @_;
#	print "TYPE $type IS VALID? >>> " . $self->valid_types->{$type} . "\n";
	return $self->valid_types->{$type};
	}
sub bucky_data_path
	{ return "/var/www/vhosts/carbonpictures.com" }
sub new
	{
	my ($class, $self) = @_;
	$self ||= {};
	bless $self, $class;
	return $self;
	}
sub type
	{ return $TYPE_Bucky; }
sub inherit
	{
	my ($self, $parent) = @_;
	if ($parent && ref($parent) =~ /Bucky/)
		{
		$self->db($parent);
		$self->bucky($parent);
		}
	}
sub bucky
	{
	my ($self, $parent) = @_;
	return $self->{_bucky} if $self->{_bucky};
	if ($parent && ref($parent) eq "Bucky")
		{ $self->{_bucky} = $parent; }
	elsif ($parent && ref($parent) =~ /Bucky/)
		{ $self->{_bucky} = $parent->bucky; }
	elsif ($self && ref($self) eq "Bucky")
		{ $self->{_bucky} = $self; }
	else
		{ $self->{_bucky} = new Bucky; }
	return $self->{_bucky};
	}
sub db
	{
	my ($self, $parent) = @_;
	return $self->{_db} if $self->{_db};
	if ($parent && ref($parent) =~ /Bucky/)
		{ $self->{_db} = $parent->bucky->db; }
	else
		{ $self->{_db} = new Bucky::DB; }
	return $self->{_db};
	}
sub keywords
	{
	}
sub keyword ($)
	{ return shift->entity( $TYPE_Keyword, @_ ); }
sub thread ($)
	{ return shift->entity( $TYPE_Thread, @_ ); }
sub comment ($)
	{ return shift->entity( $TYPE_Comment, @_ ); }
sub file ($)
	{ return shift->entity( $TYPE_File, @_ ); }
# return unless my $criteria = $self->check_criteria($which);
sub check_criteria
	{
	my ($self, $type, $which) = @_;
	my $accessor = "_" . $type;
	if ($self->can($accessor))
		{ $which ||= $self->$accessor; }
	return undef unless $type && $which;
	my $criteria = {};
	if ( $self->is_number($which) )
		{ $criteria->{id} = $which; }
	elsif ( length($which) )
		{ $criteria->{keyword} = $which; }
	return scalar keys %$criteria ? $criteria : undef;
	}
# my $keyword = $self->entity( $TYPE_Keyword, $which );
sub entity
	{
	my ($self, $type, $which) = @_;
	return unless my $criteria = $self->check_criteria($type, $which);
	my $entity_list = $self->db->select($type, $criteria);
	foreach my $entity (@$entity_list)
		{
		# TODO: privacy check?
		return $self->condone( $entity, $type );
		}
	return undef;
	}
# my $threads = $keyword->threads;
# my $files = $thread->files;
# my $children = $keyword->children;
# join " ", map { $_->type }, $keyword->children;
sub comments_by_id
	{
	my ($self, $comments_to_get) = @_;
	return $self->db->select_by_id("comment", $comments_to_get);
	}
sub files_by_id
	{
	my ($self, $files_to_get) = @_;
	return $self->db->select_by_id("file", $files_to_get);
	}
sub threads
	{
	my ($self) = @_;
	return {};
	}
sub family
	{
	my ($self, $which) = @_;
	my $type = $self->type || return;
	my $entity_list = $self->db->select("family", { $type => $self->id });
	}
sub condone
	{
	my ($self, $ref, $type) = @_;
	if ($type !~ /Bucky/ && $self->is_valid_type($type))
		{ $type = "Bucky::" . ucfirst($type); }
	bless $ref, $type;
	$ref->inherit($self);
	return $ref;
	}
1;