Merge pull request #378 from DakuTree/patch

Post Relationships ext + TagTermParseEvent
This commit is contained in:
jgen 2014-02-21 13:32:33 -05:00
commit e8cba99111
14 changed files with 310 additions and 125 deletions

View File

@ -44,7 +44,6 @@ class BaseThemelet {
$h_thumb_link = $image->get_thumb_link();
$h_tip = html_escape($image->get_tooltip());
$h_tags = strtolower($image->get_tag_list());
$base = get_base_href();
$ext = strtolower($image->ext);
// If the file doesn't support thumbnail generation, show it at max size.
@ -55,7 +54,13 @@ class BaseThemelet {
$tsize = get_thumbnail_size($image->width, $image->height);
}
return "<a href='$h_view_link' class='thumb shm-thumb shm-thumb-link' data-tags='$h_tags' data-post-id='$i_id'>".
$custom_classes = "";
if(class_exists("Relationships")){
if($image->parent_id !== NULL){ $custom_classes .= "shm-thumb-has_parent "; }
if($image->has_children == TRUE){ $custom_classes .= "shm-thumb-has_child "; }
}
return "<a href='$h_view_link' class='thumb shm-thumb shm-thumb-link {$custom_classes}' data-tags='$h_tags' data-post-id='$i_id'>".
"<img id='thumb_$i_id' title='$h_tip' alt='$h_tip' height='{$tsize[1]}' width='{$tsize[0]}' src='$h_thumb_link'>".
"</a>\n";
}

View File

@ -479,15 +479,9 @@ class Image {
$this->delete_tags_from_image();
// insert each new tags
foreach($tags as $tag) {
if(preg_match("/^source[=|:](.*)$/i", $tag, $matches)) {
$this->set_source($matches[1]);
continue;
}
if(preg_match("/^pool[=|:](.*)$/i", $tag, $matches)) {
if(class_exists("Pools")) {
$pls = new Pools();
$pls->add_post_from_tag($matches[1], $this->id);
}
$ttpe = new TagTermParseEvent($tag, $this->id);
send_event($ttpe);
if($ttpe->is_metatag()) {
continue;
}

View File

@ -141,6 +141,11 @@
* <li>pool=(PoolID, any, none) -- search for images in a pool by PoolID.
* <li>pool_by_name=PoolName -- search for images in a pool by PoolName. underscores are replaced with spaces
* </ul>
* <li>Post Relationships
* <ul>
* <li>parent=(parentID, any, none) -- search for images by parentID / if they have, do not have a parent
* <li>child=(any, none) -- search for images which have, or do not have children
* </ul>
* </ul>
*/

View File

@ -256,6 +256,20 @@ class NumericScore extends Extension {
}
}
public function onTagTermParse(TagTermParseEvent $event) {
$matches = array();
if(preg_match("/^vote[=|:](up|down|remove)$/", $event->term, $matches)) {
global $user;
$score = ($matches[1] == "up" ? 1 : ($matches[1] == "down" ? -1 : 0));
if(!$user->is_anonymous()) {
send_event(new NumericScoreSetEvent($event->id, $user, $score));
}
}
if(!empty($matches)) $event->metatag = true;
}
private function install() {
global $database;
global $config;

View File

@ -315,21 +315,26 @@ class Pools extends Extension {
}
}
public function add_post_from_tag(/*str*/ $poolTag, /*int*/ $imageID){
$poolTag = str_replace("_", " ", $poolTag);
//First check if pool tag is a title
if(ctype_digit($poolTag)){
//If string only contains numeric characters, assume it is $poolID
if($this->get_single_pool($poolTag)){ //Make sure pool exists
$this->add_post($poolTag, $imageID);
public function onTagTermParse(TagTermParseEvent $event) {
$matches = array();
if(preg_match("/^pool[=|:](.*)$/i", $event->term, $matches)) {
global $user;
$poolTag = (string) str_replace("_", " ", $matches[1]);
$pool = null;
if(ctype_digit($poolTag)){ //If only digits, assume PoolID
$pool = $this->get_single_pool($poolTag);
}else{ //assume PoolTitle
$pool = $this->get_single_pool_from_title($poolTag);
}
}else{
//If string doesn't contain only numeric characters, check to see if tag is title.
$pool = $this->get_single_pool_from_title($poolTag);
if($pool){
$this->add_post($pool['id'], $imageID);
if($pool ? $this->have_permission($user, $pool) : FALSE){
$this->add_post($pool['id'], $event->id, true);
}
}
if(!empty($matches)) $event->metatag = true;
}
/* ------------------------------------------------- */
@ -832,9 +837,9 @@ class Pools extends Extension {
/*
* HERE WE ADD A SIMPLE POST FROM POOL
* USED WITH FOREACH IN revert_history()
* USED WITH FOREACH IN revert_history() & onTagTermParse()
*/
private function add_post(/*int*/ $poolID, /*int*/ $imageID) {
private function add_post(/*int*/ $poolID, /*int*/ $imageID, $history=false) {
global $database;
if(!$this->check_post($poolID, $imageID)) {
@ -845,19 +850,29 @@ class Pools extends Extension {
}
$database->execute("UPDATE pools SET posts=(SELECT COUNT(*) FROM pool_images WHERE pool_id=:pid) WHERE id=:pid", array("pid"=>$poolID));
if($history){
$count = $database->get_one("SELECT COUNT(*) FROM pool_images WHERE pool_id=:pid", array("pid"=>$poolID));
$this->add_history($poolID, 1, $imageID, $count);
}
}
/*
* HERE WE REMOVE A SIMPLE POST FROM POOL
* USED WITH FOREACH IN revert_history()
* USED WITH FOREACH IN revert_history() & onTagTermParse()
*/
private function delete_post(/*int*/ $poolID, /*int*/ $imageID) {
private function delete_post(/*int*/ $poolID, /*int*/ $imageID, $history=false) {
global $database;
$database->execute("DELETE FROM pool_images WHERE pool_id = :pid AND image_id = :iid", array("pid"=>$poolID, "iid"=>$imageID));
$database->execute("UPDATE pools SET posts=(SELECT COUNT(*) FROM pool_images WHERE pool_id=:pid) WHERE id=:pid", array("pid"=>$poolID));
if($history){
$count = $database->get_one("SELECT COUNT(*) FROM pool_images WHERE pool_id=:pid", array("pid"=>$poolID));
$this->add_history($poolID, 0, $imageID, $count);
}
}
}

View File

@ -412,8 +412,15 @@ class PoolsTheme extends Themelet {
$html .= "</tbody></table>";
$nav_html = '
<a href="'.make_link().'">Index</a>
<br><a href="'.make_link("pool/new").'">Create Pool</a>
<br><a href="'.make_link("pool/updated").'">Pool Changes</a>
';
$page->set_title("Recent Changes");
$page->set_heading("Recent Changes");
$page->add_block(new Block("Navigation", $nav_html, "left", 10));
$page->add_block(new Block("Recent Changes", $html, "main", 10));
$this->display_paginator($page, "pool/updated", null, $pageNumber, $totalPages);

View File

@ -0,0 +1,123 @@
<?php
/**
* Name: Post Relationships
* Author: Angus Johnston <admin@codeanimu.net>
* License: GPLv2
* Description: Allow posts to have relationships (parent/child).
*/
class Relationships extends Extension {
public function onInitExt(InitExtEvent $event) {
global $config, $database;
// Create the database tables
if ($config->get_int("ext_relationships_version") < 1){
$database->Execute("ALTER TABLE images ADD parent_id INT NULL, ADD INDEX (parent_id);");
$database->Execute("ALTER TABLE images ADD has_children BOOL DEFAULT FALSE NOT NULL;");
$config->set_int("ext_relationships_version", 1);
log_info("relationships", "extension installed");
}
}
public function onImageInfoSet(ImageInfoSetEvent $event) {
global $user;
if(isset($_POST['tag_edit__tags']) ? !preg_match('/parent[=|:]/', $_POST["tag_edit__tags"]) : TRUE) { //Ignore tag_edit__parent if tags contain parent metatag
if (isset($_POST["tag_edit__parent"]) ? ctype_digit($_POST["tag_edit__parent"]) : FALSE) {
$this->set_parent($event->image->id, (int) $_POST["tag_edit__parent"]);
}else{
$this->remove_parent($event->image->id);
}
}
}
public function onDisplayingImage(DisplayingImageEvent $event) {
$this->theme->relationship_info($event->image);
}
public function onSearchTermParse(SearchTermParseEvent $event) {
$matches = array();
if(preg_match("/^parent[=|:]([0-9]+|any|none)$/", $event->term, $matches)) {
$parentID = $matches[1];
if(preg_match("/^(any|none)$/", $parentID)){
$not = ($parentID == "any" ? "NOT" : "");
$event->add_querylet(new Querylet("images.parent_id IS $not NULL"));
}else{
$event->add_querylet(new Querylet("images.parent_id = :pid", array("pid"=>$parentID)));
}
}
else if(preg_match("/^child[=|:](any|none)$/", $event->term, $matches)) {
$not = ($matches[1] == "any" ? "=" : "!=");
$event->add_querylet(new Querylet("images.has_children $not TRUE"));
}
}
public function onTagTermParse(TagTermParseEvent $event) {
$matches = array();
if(preg_match("/^parent[=|:]([0-9]+|none)$/", $event->term, $matches)) {
$parentID = $matches[1];
if($parentID == "none" || $parentID == "0"){
$this->remove_parent($event->id);
}else{
$this->set_parent($event->id, $parentID);
}
}
else if(preg_match("/^child[=|:]([0-9]+)$/", $event->term, $matches)) {
$childID = $matches[1];
$this->set_child($event->id, $childID);
}
if(!empty($matches)) $event->metatag = true;
}
public function onImageInfoBoxBuilding(ImageInfoBoxBuildingEvent $event) {
$event->add_part($this->theme->get_parent_editor_html($event->image), 45);
}
public function onImageDeletion(ImageDeletionEvent $event) {
global $database;
if($event->image->has_children){
$database->execute("UPDATE images SET parent_id = NULL WHERE parent_id = :iid", array("iid"=>$event->image->id));
}
if($event->image->parent_id !== NULL){
$database->execute("UPDATE images SET has_children = (SELECT * FROM (SELECT CASE WHEN (COUNT(*) - 1) > 0 THEN 1 ELSE 0 END FROM images WHERE parent_id = :pid) AS sub)
WHERE id = :pid", array("pid"=>$event->image->parent_id));
}
}
private function set_parent(/*int*/ $imageID, /*int*/ $parentID){
global $database;
if($database->get_row("SELECT 1 FROM images WHERE id = :pid", array("pid"=>$parentID))){
$database->execute("UPDATE images SET parent_id = :pid WHERE id = :iid", array("pid"=>$parentID, "iid"=>$imageID));
$database->execute("UPDATE images SET has_children = TRUE WHERE id = :pid", array("pid"=>$parentID));
}
}
private function set_child(/*int*/ $parentID, /*int*/ $childID){
global $database;
if($database->get_row("SELECT 1 FROM images WHERE id = :cid", array("cid"=>$childID))){
$database->execute("UPDATE images SET parent_id = :pid WHERE id = :cid", array("cid"=>$childID, "pid"=>$parentID));
$database->execute("UPDATE images SET has_children = TRUE WHERE id = :pid", array("pid"=>$parentID));
}
}
private function remove_parent(/*int*/ $imageID){
global $database;
$parentID = $database->get_one("SELECT parent_id FROM images WHERE id = :iid", array("iid"=>$imageID));
if($parentID){
$database->execute("UPDATE images SET parent_id = NULL WHERE id = :iid", array("iid"=>$imageID));
$database->execute("UPDATE images SET has_children = (SELECT * FROM (SELECT CASE WHEN COUNT(*) > 0 THEN 1 ELSE 0 END FROM images WHERE parent_id = :pid) AS sub)
WHERE id = :pid", array("pid"=>$parentID));
}
}
}
?>

View File

@ -0,0 +1,15 @@
.thumb IMG {
border-width: 2px !important;
}
.shm-thumb-has_child img {
border-color: lime !important;
}
.shm-thumb-has_parent img {
border-color: #cc0 !important;
}
.shm-thumb-has_child.shm-thumb-has_parent img {
border-color: lime #cc0 #cc0 lime !important;
}

View File

@ -0,0 +1,45 @@
<?php
class RelationshipsTheme extends Themelet {
public function relationship_info($image) {
global $page, $database;
if($image->parent_id !== NULL){
$a = "<a href='".make_link("post/view/".$image->parent_id)."'>parent post</a>";
$page->add_block(new Block(null, "This post belongs to a $a.", "main", 5));
}
if($image->has_children == TRUE){
$ids = $database->get_col("SELECT id FROM images WHERE parent_id = :iid", array("iid"=>$image->id));
$html = "This post has <a href='".make_link('post/list/parent='.$image->id.'/1')."'>".(count($ids) > 1 ? "child posts" : "a child post")."</a>";
$html .= " (post ";
foreach($ids as $id){
$html .= "#<a href='".make_link('post/view/'.$id)."'>{$id}</a>, ";
}
$html = rtrim($html, ", ").").";
$page->add_block(new Block(null, $html, "main", 6));
}
}
public function get_parent_editor_html(Image $image) {
global $user;
$h_parent_id = $image->parent_id;
$s_parent_id = $h_parent_id ?: "None.";
$html = "<tr>\n".
" <th>Parent</th>\n".
" <td>\n".
(!$user->is_anonymous() ?
" <span class='view' style='overflow: hidden; white-space: nowrap;'>{$s_parent_id}</span>\n".
" <input class='edit' type='text' name='tag_edit__parent' type='number' value='{$h_parent_id}'>\n"
:
$s_parent_id
).
" <td>\n".
"</tr>\n";
return $html;
}
}
?>

View File

@ -3,6 +3,34 @@
* Name: Tag Editor
* Author: Shish
* Description: Allow images to have tags assigned to them
* Documentation:
* Here is a list of the tagging metatags available out of the box;
* Shimmie extensions may provide other metatags:
* <ul>
* <li>source=(*, none) eg -- using this metatag will ignore anything set in the "Source" box
* <ul>
* <li>source=http://example.com -- set source to http://example.com
* <li>source=none -- set source to NULL
* </ul>
* </ul>
* <p>Metatags can be followed by ":" rather than "=" if you prefer.
* <br />I.E: "source:http://example.com", "source=http://example.com" etc.
* <p>Some tagging metatags provided by extensions:
* <ul>
* <li>Numeric Score
* <ul>
* <li>vote=(up, down, remove) -- vote, or remove your vote on an image
* </ul>
* <li>Pools
* <ul>
* <li>pool=(PoolID, PoolTitle) -- add post to pool (if exists)
* </ul>
* <li>Post Relationships
* <ul>
* <li>parent=(parentID, none) -- set parent ID of current image
* <li>child=(childID) -- set parent ID of child image to current image ID
* </ul>
* </ul>
*/
/*
@ -72,6 +100,25 @@ class LockSetEvent extends Event {
}
}
/*
* TagTermParseEvent:
* Signal that a tag term needs parsing
*/
class TagTermParseEvent extends Event {
var $term = null;
var $id = null;
var $metatag = false;
public function TagTermParseEvent($term, $id) {
$this->term = $term;
$this->id = $id;
}
public function is_metatag() {
return $this->metatag;
}
}
class TagEdit extends Extension {
public function onPageRequest(PageRequestEvent $event) {
global $user, $page;
@ -112,7 +159,9 @@ class TagEdit extends Extension {
send_event(new TagSetEvent($event->image, $_POST['tag_edit__tags']));
}
if($this->can_source($event->image) && isset($_POST['tag_edit__source'])) {
send_event(new SourceSetEvent($event->image, $_POST['tag_edit__source']));
if(isset($_POST['tag_edit__tags']) ? !preg_match('/source[=|:]/', $_POST["tag_edit__tags"]) : TRUE){
send_event(new SourceSetEvent($event->image, $_POST['tag_edit__source']));
}
}
if($user->can("edit_image_lock")) {
$locked = isset($_POST['tag_edit__locked']) && $_POST['tag_edit__locked']=="on";
@ -169,6 +218,16 @@ class TagEdit extends Extension {
$event->add_part($this->theme->get_lock_editor_html($event->image), 42);
}
public function onTagTermParse(TagTermParseEvent $event) {
$matches = array();
if(preg_match("/^source[=|:](.*)$/i", $event->term, $matches)) {
$source = ($matches[1] !== "none" ? $matches[1] : null);
send_event(new SourceSetEvent(Image::by_id($event->id), $source));
}
if(!empty($matches)) $event->metatag = true;
}
private function can_tag(Image $image) {
global $config, $user;

View File

@ -1,26 +1,5 @@
<?php
class Themelet extends BaseThemelet {
public function build_thumb_html(Image $image) {
global $config;
$h_view_link = make_link("post/view/{$image->id}");
$h_thumb_link = $image->get_thumb_link();
$h_tip = html_escape($image->get_tooltip());
$i_id = int_escape($image->id);
$h_tags = strtolower($image->get_tag_list());
// If file is flash or svg then sets thumbnail to max size.
if($image->ext == 'swf' || $image->ext == 'svg') {
$tsize = get_thumbnail_size($config->get_int('thumb_width'), $config->get_int('thumb_height'));
}
else{
$tsize = get_thumbnail_size($image->width, $image->height);
}
return "<a href='$h_view_link' class='shm-thumb shm-thumb-link' data-tags='$h_tags' data-post-id='$i_id'><img title='$h_tip' alt='$h_tip' ".
"width='{$tsize[0]}' height='{$tsize[1]}' src='$h_thumb_link' /></a>";
}
public function display_paginator(Page $page, $base, $query, $page_number, $total_pages) {
if($total_pages == 0) $total_pages = 1;
$body = $this->build_paginator($page_number, $total_pages, $base, $query);

View File

@ -1,26 +1,5 @@
<?php
class Themelet extends BaseThemelet {
public function build_thumb_html(Image $image) {
global $config;
$h_view_link = make_link("post/view/{$image->id}");
$h_thumb_link = $image->get_thumb_link();
$h_tip = html_escape($image->get_tooltip());
$i_id = int_escape($image->id);
$h_tags = strtolower($image->get_tag_list());
// If file is flash or svg then sets thumbnail to max size.
if($image->ext == 'swf' || $image->ext == 'svg') {
$tsize = get_thumbnail_size($config->get_int('thumb_width'), $config->get_int('thumb_height'));
}
else{
$tsize = get_thumbnail_size($image->width, $image->height);
}
return "<a href='$h_view_link' class='shm-thumb shm-thumb-link' data-tags='$h_tags' data-post-id='$i_id'><img title='$h_tip' alt='$h_tip' ".
"width='{$tsize[0]}' height='{$tsize[1]}' src='$h_thumb_link' /></a>";
}
public function display_paginator(Page $page, $base, $query, $page_number, $total_pages) {
if($total_pages == 0) $total_pages = 1;
$body = $this->build_paginator($page_number, $total_pages, $base, $query);

View File

@ -1,30 +1,5 @@
<?php
class Themelet extends BaseThemelet {
/**
* Generic thumbnail code; returns HTML rather than adding
* a block since thumbs tend to go inside blocks...
*/
public function build_thumb_html(Image $image) {
global $config;
$h_view_link = make_link("post/view/{$image->id}");
$h_thumb_link = $image->get_thumb_link();
$h_tip = html_escape($image->get_tooltip());
$i_id = int_escape($image->id);
$h_tags = strtolower($image->get_tag_list());
// If file is flash or svg then sets thumbnail to max size.
if($image->ext == 'swf' || $image->ext == 'svg') {
$tsize = get_thumbnail_size($config->get_int('thumb_width'), $config->get_int('thumb_height'));
}
else {
$tsize = get_thumbnail_size($image->width, $image->height);
}
return "<a href='$h_view_link' class='thumb shm-thumb shm-thumb-link' data-tags='$h_tags' data-post-id='$i_id'><img title='$h_tip' alt='$h_tip' ".
"width='{$tsize[0]}' height='{$tsize[1]}' src='$h_thumb_link' /></a>";
}
/**
* Add a generic paginator
*/

View File

@ -1,34 +1,5 @@
<?php
class Themelet extends BaseThemelet {
/**
* Generic thumbnail code; returns HTML rather than adding
* a block since thumbs tend to go inside blocks...
*/
public function build_thumb_html(Image $image) {
global $config;
$i_id = (int) $image->id;
$h_view_link = make_link('post/view/'.$i_id);
$h_thumb_link = $image->get_thumb_link();
$h_tip = html_escape($image->get_tooltip());
$h_tags = strtolower($image->get_tag_list());
$base = get_base_href();
// If file is flash or svg then sets thumbnail to max size.
if($image->ext === 'swf' || $image->ext === 'svg'){
$tsize = get_thumbnail_size($config->get_int('thumb_width'), $config->get_int('thumb_height'));
}
else{
$tsize = get_thumbnail_size($image->width, $image->height);
}
return '<center class="shm-thumb" data-tags="'.$h_tags.'" data-post-id="'.$i_id.'"><div class="thumbblock">'.
'<a href="'.$h_view_link.'" class="thumb shm-thumb-link">'.
'<img id="thumb_'.$i_id.'" title="'.$h_tip.'" alt="'.$h_tip.'" height="'.$tsize[1].'" width="'.$tsize[0].'" class="lazy" data-original="'.$h_thumb_link.'" src="'.$base.'/lib/static/grey.gif">'.
'<noscript><img id="thumb_'.$i_id.'" title="'.$h_tip.'" alt="'.$h_tip.'" height="'.$tsize[1].'" width="'.$tsize[0].'" src="'.$h_thumb_link.'"></noscript>'.
"</a></div></center>\n";
}
/**
* Put something in a rounded rectangle box; specific to the default theme
*/
@ -40,7 +11,6 @@ class Themelet extends BaseThemelet {
";
}
/**
* Add a generic paginator
*/