package Newslash::Model::Tags;
use Newslash::Model::Base -base;
+
+sub set_tag {
+ my $self = shift;
+ my $params = {@_};
+
+ my $globj_id = $params->{globj_id};
+ my $uid = $params->{uid} || 1;
+ my $name = $params->{name};
+ my $private = $params->{private} ? "yes" : "no";
+ my $active = defined $params->{active} ? $params->{active} : 1;
+ my $tagname_id = $params->{tagname_id};
+
+ if (!$globj_id) {
+ return;
+ }
+
+ if ($name) {
+ my $tagname = $self->select(tagname => $name);
+ if ($tagname) {
+ $tagname_id = $tagname->{tagnameid};
+ }
+ else {
+ $tagname_id = $self->create(tagname => $name);
+ }
+ }
+
+ if (!$tagname_id) {
+ return;
+ }
+
+ my $inactivated = $active ? "NULL" : "NOW()";
+ my $sql = <<"EOSQL";
+INSERT INTO tags
+ (tagnameid, globjid, uid, created_at, inactivated, private, is_active)
+ VALUES
+ (?, ?, ?, NOW(), $inactivated, ?, ?)
+EOSQL
+
+ my $dbh = $self->connect_db;
+ my $rs = $dbh->do($sql, undef, $tagname_id, $globj_id, $uid, $private, $active);
+ if (!$rs) {
+ $dbh->disconnect;
+ return;
+ }
+ my $id = $dbh->last_insert_id(undef, undef, undef, undef);
+ $dbh->disconnect;
+ return $id;
+}
+
+sub create {
+ my $self = shift;
+ my $params = {@_};
+
+ if ($params->{tagname}) {
+ my $sql = "INSERT INTO tagnames (tagname) VALUES (?)";
+ my $dbh = $self->connect_db;
+ my $rs = $dbh->do($sql, undef, $params->{tagname});
+ if (!$rs) {
+ $dbh->disconnect;
+ return;
+ }
+ my $id = $dbh->last_insert_id(undef, undef, undef, undef);
+ $dbh->disconnect;
+ return $id;
+ }
+ return;
+}
+
+sub select {
+ my $self = shift;
+ my $params = {@_};
+
+ if ($params->{tagname}) {
+ my $sql = "SELECT * FROM tagnames WHERE tagname = ?";
+ my $dbh = $self->connect_db;
+ my $sth = $dbh->prepare($sql);
+ $sth->execute($params->{tagname});
+ my $rs = $sth->fetchall_arrayref({});
+ $dbh->disconnect;
+ if (@$rs) {
+ return $rs->[0];
+ }
+ return;
+ }
+}
+
+
+# =================== Legacy API ====================
+
+
+
my $tags_tagname_regex = "^!?[a-z一-龠ぁ-んァ-ヴー][a-z0-9一-龠ぁ-んァ-ヴー/・]{0,63}\$";
sub tagnameSyntaxOK {
my($self, $tagname) = @_;
return 0 unless defined($tagname) && length($tagname) > 0;
- my $constants = getCurrentStatic();
+ #my $constants = getCurrentStatic();
#my $regex = $constants->{tags_tagname_regex};
my $regex = $tags_tagname_regex;
return($tagname =~ /$regex/);