diff --git a/contrib/artists/main.php b/contrib/artists/main.php index ca6ad4fa..5c5e17b0 100644 --- a/contrib/artists/main.php +++ b/contrib/artists/main.php @@ -1,1024 +1,1215 @@ - - * Alpha - * License: GPLv2 - * Description: Simple artists extension - * Documentation: - * - */ -class AuthorSetEvent extends Event { - var $image, $user, $author; - - public function AuthorSetEvent(Image $image, User $user, $author) - { - $this->image = $image; - $this->user = $user; - $this->author = $author; - } -} - -class Artists implements Extension { - var $theme; - - public function receive_event(Event $event) - { - global $user; - - if(is_null($this->theme)) $this->theme = get_theme_object($this); - - if ($event instanceof ImageInfoSetEvent) - if (isset($_POST["tag_edit__author"])) - send_event(new AuthorSetEvent($event->image, $user, $_POST["tag_edit__author"])); - - if ($event instanceof AuthorSetEvent) - $this->update_author($event); - - if($event instanceof InitExtEvent) - $this->try_install(); - - if ($event instanceof ImageInfoBoxBuildingEvent) - $this->add_author_field_to_image($event); - - if ($event instanceof PageRequestEvent) - $this->handle_commands($event); - } - - public function try_install() { - global $config, $database; - - if ($config->get_int("ext_artists_version") < 1) - { - $database->create_table("artists", - "id SCORE_AIPK - , user_id INTEGER NOT NULL - , name VARCHAR(255) NOT NULL - , created DATETIME NOT NULL - , updated DATETIME NOT NULL - , notes TEXT - , INDEX(id) - "); - $database->create_table("artist_members", - "id SCORE_AIPK - , artist_id INTEGER NOT NULL - , user_id INTEGER NOT NULL - , name VARCHAR(255) NOT NULL - , created DATETIME NOT NULL - , updated DATETIME NOT NULL - , INDEX (id) - , FOREIGN KEY (artist_id) REFERENCES artists (id) ON UPDATE CASCADE ON DELETE CASCADE - "); - $database->create_table("artist_alias", - "id SCORE_AIPK - , artist_id INTEGER NOT NULL - , user_id INTEGER NOT NULL - , created DATETIME - , updated DATETIME - , alias VARCHAR(255) - , INDEX (id) - , FOREIGN KEY (artist_id) REFERENCES artists (id) ON UPDATE CASCADE ON DELETE CASCADE - "); - $database->create_table("artist_urls", - "id SCORE_AIPK - , artist_id INTEGER NOT NULL - , user_id INTEGER NOT NULL - , created DATETIME NOT NULL - , updated DATETIME NOT NULL - , url VARCHAR(1000) NOT NULL - , INDEX (id) - , FOREIGN KEY (artist_id) REFERENCES artists (id) ON UPDATE CASCADE ON DELETE CASCADE - "); - $database->execute("ALTER TABLE images ADD COLUMN author VARCHAR(255) NULL", array()); - - $config->set_int("artistsPerPage", 20); - $config->set_int("ext_artists_version", 1); - - log_info("artists", "extension installed"); - } - } - - public function update_author($event) - { - global $database; - - $author = strtolower($event->author); - if (strlen($author) == 0 || strpos($author, " ")) - return; - - $paddedAuthor = str_replace(" ", "_", $author); - - $artistID = NULL; - if ($this->artist_exists($author)) - $artistID = $this->get_artist_id($author); - - if (is_null($artistID) && $this->alias_exists_by_name($paddedAuthor)) - $artistID = $this->get_artistID_by_aliasName($paddedAuthor); - - if (is_null($artistID) && $this->member_exists_by_name($paddedAuthor)) - $artistID = $this->get_artistID_by_memberName($paddedAuthor); - - if (is_null($artistID) && $this->url_exists_by_url($author)) - $artistID = $this->get_artistID_by_url($author); - - if (!is_null($artistID)) - $artistName = $this->get_artistName_by_artistID($artistID); - else - { - $this->save_new_artist($author, ""); - $artistName = $author; - } - - $database->execute("UPDATE images SET author = ? WHERE id = ?", array($artistName, $event->image->id)); - } - public function handle_commands($event) - { - global $config, $page, $user; - - if($event->page_matches("artist")) - { - switch($event->get_arg(0)) - { - //*************ARTIST SECTION************** - case "list": - { - $this->get_listing($page, $event); - $this->theme->sidebar_options("neutral"); - break; - } - case "new": - { - if(!$user->is_anonymous()){ - $this->theme->new_artist_composer(); - }else{ - $errMessage = "You must be registered and logged in to create a new artist."; - $this->theme->display_error($page, "Error", $errMessage); - } - break; - } - case "new_artist": - { - $page->set_mode("redirect"); - $page->set_redirect(make_link("artist/new")); - break; - } - case "create": - { - if(!$user->is_anonymous()) - { - $newArtistID = $this->add_artist(); - if ($newArtistID == -1) - { - $errMessage = "Error when entering artist data."; - $this->theme->display_error($page, "Error", $errMessage); - } - else - { - $page->set_mode("redirect"); - $page->set_redirect(make_link("artist/view/".$newArtistID)); - } - } - else - { - $errMessage = "You must be registered and logged in to create a new artist."; - $this->theme->display_error($page, "Error", $errMessage); - } - break; - } - - case "view": - { - $artistID = $event->get_arg(1); - $artist = $this->get_artist($artistID); - $aliases = $this->get_alias($artist['id']); - $members = $this->get_members($artist['id']); - $urls = $this->get_urls($artist['id']); - - $userIsLogged = !$user->is_anonymous(); - $userIsAdmin = $user->is_admin(); - - $images = Image::find_images(0, 4, Tag::explode($artist['name'])); - - $this->theme->show_artist($artist, $aliases, $members, $urls, $images, $userIsLogged, $userIsAdmin); - if ($userIsLogged) - { - //$this->theme->show_new_alias_composer($artistID); - //$this->theme->show_new_member_composer($artistID); - //$this->theme->show_new_url_composer($artistID); - } - - $this->theme->sidebar_options("editor", $artistID, $userIsAdmin); - - break; - } - - case "edit": - { - $artistID = $event->get_arg(1); - $artist = $this->get_artist($artistID); - $aliases = $this->get_alias($artistID); - $members = $this->get_members($artistID); - $urls = $this->get_urls($artistID); - - - - if(!$user->is_anonymous()){ - $this->theme->show_artist_editor($artist, $aliases, $members, $urls); - - $userIsAdmin = $user->is_admin(); - $this->theme->sidebar_options("editor", $artistID, $userIsAdmin); - }else{ - $errMessage = "You must be registered and logged in to edit an artist."; - $this->theme->display_error($page, "Error", $errMessage); - } - break; - } - case "edit_artist": - { - $artistID = $_POST['artist_id']; - $page->set_mode("redirect"); - $page->set_redirect(make_link("artist/edit/".$artistID)); - break; - } - case "edited": - { - $artistID = int_escape($_POST['id']); - $this->update_artist(); - $page->set_mode("redirect"); - $page->set_redirect(make_link("artist/view/".$artistID)); - break; - } - case "nuke_artist": - { - $artistID = $_POST['artist_id']; - $page->set_mode("redirect"); - $page->set_redirect(make_link("artist/nuke/".$artistID)); - break; - } - case "nuke": - { - $artistID = $event->get_arg(1); - $this->delete_artist($artistID); // this will delete the artist, it's alias, it's urls and it's members - $page->set_mode("redirect"); - $page->set_redirect(make_link("artist/list")); - break; - } - case "add_alias": - { - $artistID = $_POST['artist_id']; - $this->theme->show_new_alias_composer($artistID); - break; - } - case "add_member": - { - $artistID = $_POST['artist_id']; - $this->theme->show_new_member_composer($artistID); - break; - } - case "add_url": - { - $artistID = $_POST['artist_id']; - $this->theme->show_new_url_composer($artistID); - break; - } - //***********ALIAS SECTION *********************** - case "alias": - { - switch ($event->get_arg(1)) - { - case "add": - { - $artistID = $_POST['artistID']; - $this->add_alias(); - $page->set_mode("redirect"); - $page->set_redirect(make_link("artist/view/".$artistID)); - break; - } - case "delete": - { - $aliasID = $event->get_arg(2); - $artistID = $this->get_artistID_by_aliasID($aliasID); - $this->delete_alias($aliasID); - $page->set_mode("redirect"); - $page->set_redirect(make_link("artist/view/".$artistID)); - break; - } - case "edit": - { - $aliasID = int_escape($event->get_arg(2)); - $alias = $this->get_alias_by_id($aliasID); - $this->theme->show_alias_editor($alias); - break; - } - case "edited": - { - $this->update_alias(); - $aliasID = int_escape($_POST['aliasID']); - $artistID = $this->get_artistID_by_aliasID($aliasID); - $page->set_mode("redirect"); - $page->set_redirect(make_link("artist/view/".$artistID)); - break; - } - } - break; // case: alias - } - - //**************** URLS SECTION ********************** - case "url": - { - switch ($event->get_arg(1)) - { - case "add": - { - $artistID = $_POST['artistID']; - $this->add_urls(); - $page->set_mode("redirect"); - $page->set_redirect(make_link("artist/view/".$artistID)); - break; - } - case "delete": - { - $urlID = $event->get_arg(2); - $artistID = $this->get_artistID_by_urlID($urlID); - $this->delete_url($urlID); - $page->set_mode("redirect"); - $page->set_redirect(make_link("artist/view/".$artistID)); - break; - } - case "edit": - { - $urlID = int_escape($event->get_arg(2)); - $url = $this->get_url_by_id($urlID); - $this->theme->show_url_editor($url); - break; - } - case "edited": - { - $this->update_url(); - $urlID = int_escape($_POST['urlID']); - $artistID = $this->get_artistID_by_urlID($urlID); - $page->set_mode("redirect"); - $page->set_redirect(make_link("artist/view/".$artistID)); - break; - } - } - break; // case: urls - } - //******************* MEMBERS SECTION ********************* - case "member": - { - switch ($event->get_arg(1)) - { - case "add": - { - $artistID = $_POST['artistID']; - $this->add_members(); - $page->set_mode("redirect"); - $page->set_redirect(make_link("artist/view/".$artistID)); - break; - } - case "delete": - { - $memberID = int_escape($event->get_arg(2)); - $artistID = $this->get_artistID_by_memberID($memberID); - $this->delete_member($memberID); - $page->set_mode("redirect"); - $page->set_redirect(make_link("artist/view/".$artistID)); - break; - } - case "edit": - { - $memberID = int_escape($event->get_arg(2)); - $member = $this->get_member_by_id($memberID); - $this->theme->show_member_editor($member); - break; - } - case "edited": - { - $this->update_member(); - $memberID = int_escape($_POST['memberID']); - $artistID = $this->get_artistID_by_memberID($memberID); - $page->set_mode("redirect"); - $page->set_redirect(make_link("artist/view/".$artistID)); - break; - } - } - break; //case: members - } - } - } - } - - public function add_author_field_to_image($event) - { - $artistName = $this->get_artistName_by_imageID($event->image->id); - - $event->add_part($this->theme->get_author_editor_html($artistName), 42); - } - - private function get_artistName_by_imageID($imageID) - { - global $database; - - $result = $database->get_row("SELECT author FROM images WHERE id = ?", array($imageID)); - return $result['author']; - } - - private function url_exists_by_url($url) - { - global $database; - - $result = $database->db->GetOne("SELECT COUNT(1) FROM artist_urls WHERE url = ?", array($url)); - return ($result != 0); - } - - private function member_exists_by_name($member) - { - global $database; - - $result = $database->db->GetOne("SELECT COUNT(1) FROM artist_members WHERE name = ?", array($member)); - return ($result != 0); - } - - private function alias_exists_by_name($alias) - { - global $database; - - $result = $database->db->GetOne("SELECT COUNT(1) FROM artist_alias WHERE alias = ?", array($alias)); - return ($result != 0); - } - - private function alias_exists($artistID, $alias){ - global $database; - - $result = $database->db->GetOne("SELECT COUNT(1) FROM artist_alias WHERE artist_id = ? AND alias = ?", array($artistID, $alias)); - return ($result != 0); - } - - private function get_artistID_by_url($url) - { - global $database; - $result = $database->get_row("SELECT artist_id FROM artist_urls WHERE url = ?", array($url)); - return $result['artist_id']; - } - - private function get_artistID_by_memberName($member) - { - global $database; - $result = $database->get_row("SELECT artist_id FROM artist_members WHERE name = ?", array($member)); - return $result['artist_id']; - } - private function get_artistName_by_artistID($artistID) - { - global $database; - $result = $database->get_row("SELECT name FROM artists WHERE id = ?", array($artistID)); - return $result['name']; - } - - private function get_artistID_by_aliasID($aliasID) - { - global $database; - $result = $database->get_row("SELECT artist_id FROM artist_alias WHERE id = ?", array($aliasID)); - return $result['artist_id']; - } - - private function get_artistID_by_memberID($memberID) - { - global $database; - $result = $database->get_row("SELECT artist_id FROM artist_members WHERE id = ?", array($memberID)); - return $result['artist_id']; - } - - private function get_artistID_by_urlID($urlID) - { - global $database; - $result = $database->get_row("SELECT artist_id FROM artist_urls WHERE id = ?", array($urlID)); - return $result['artist_id']; - } - - private function delete_alias($aliasID) - { - global $database; - $database->execute("DELETE FROM artist_alias WHERE id = ?", array($aliasID)); - } - - private function delete_url($urlID) - { - global $database; - $database->execute("DELETE FROM artist_urls WHERE id = ?", array($urlID)); - } - - private function delete_member($memberID) - { - global $database; - $database->execute("DELETE FROM artist_members WHERE id = ?", array($memberID)); - } - - - private function get_alias_by_id($aliasID) - { - global $database; - return $database->get_row("SELECT * FROM artist_alias WHERE id = ?", array($aliasID)); - } - - private function get_url_by_id($urlID) - { - global $database; - return $database->get_row("SELECT * FROM artist_urls WHERE id = ?", array($urlID)); - } - - private function get_member_by_id($memberID) - { - global $database; - return $database->get_row("SELECT * FROM artist_members WHERE id = ?", array($memberID)); - } - - private function update_artist() - { - global $user; - $artistID = $_POST['id']; - $name = strtolower($_POST['name']); - $notes = $_POST['notes']; - $userID = $user->id; - - $aliasesAsString = $_POST["aliases"]; - $aliasesIDsAsString = $_POST["aliasesIDs"]; - - $membersAsString = $_POST["members"]; - $membersIDsAsString = $_POST["membersIDs"]; - - $urlsAsString = $_POST["urls"]; - $urlsIDsAsString = $_POST["urlsIDs"]; - - if (is_null($artistID) || !is_numeric($artistID)) - return; - - if (is_null($name) || strlen($name) == 0 || strpos($name, " ")) - return; - - if (is_null($aliasesAsString) || is_null($aliasesIDsAsString)) - return; - - if (is_null($membersAsString) || is_null($membersIDsAsString)) - return; - - if (is_null($urlsAsString) || is_null($urlsIDsAsString)) - return; - - if (strlen($notes) == 0) - $notes = NULL; - - global $database; - $database->execute("UPDATE artists SET name = ?, notes = ?, updated = now(), user_id = ? WHERE id = ? " - , array($name, $notes, $userID, $artistID)); - - // ALIAS MATCHING SECTION - $i = 0; - $aliasesAsArray = explode(" ", $aliasesAsString); - $aliasesIDsAsArray = explode(" ", $aliasesIDsAsString); - while ($i < count($aliasesAsArray)) - { - // if an alias was updated - if ($i < count($aliasesIDsAsArray)) - // save it - $this->save_existing_alias($aliasesIDsAsArray[$i], $aliasesAsArray[$i], $userID); - else - // if we already updated all, save new ones - $this->save_new_alias($artistID, $aliasesAsArray[$i], $userID); - - $i++; - } - // if we have more ids than alias, then some alias have been deleted -- delete them from db - while ($i < count($aliasesIDsAsArray)) - $this->delete_alias($aliasesIDsAsArray[$i++]); - - // MEMBERS MATCHING SECTION - $i = 0; - $membersAsArray = explode(" ", $membersAsString); - $membersIDsAsArray = explode(" ", $membersIDsAsString); - while ($i < count($membersAsArray)) - { - // if a member was updated - if ($i < count($membersIDsAsArray)) - //save it - $this->save_existing_member($membersIDsAsArray[$i], $membersAsArray[$i], $userID); - else - // if we already updated all, save new ones - $this->save_new_member($artistID, $membersAsArray[$i], "", $userID); - - $i++; - } - // if we have more ids than members, then some members have been deleted -- delete them from db - while ($i < count($membersIDsAsArray)) - $this->delete_member($membersIDsAsArray[$i++]); - - // URLS MATCHING SECTION - $i = 0; - $urlsAsArray = explode("\n", $urlsAsString); - $urlsIDsAsArray = explode(" ", $urlsIDsAsString); - while ($i < count($urlsAsArray)) - { - // if an URL was updated - if ($i < count($urlsIDsAsArray)) - // save it - $this->save_existing_url($urlsIDsAsArray[$i], $urlsAsArray[$i], $userID); - else - $this->save_new_url($artistID, $urlsAsArray[$i], $userID); - - $i++; - } - // if we have more ids than urls, then some urls have been deleted -- delete them from db - while ($i < count($urlsIDsAsArray)) - $this->delete_url($urlsIDsAsArray[$i++]); - } - - private function update_alias() - { - $aliasID = $_POST['aliasID']; - $alias = strtolower($_POST['alias']); - - if (is_null($aliasID) || !is_numeric($aliasID)) - return; - - if (is_null($alias) || strlen($alias) == 0) - return; - - global $user; - $this->save_existing_alias($aliasID, $alias, $user->id); - } - - private function save_existing_alias($aliasID, $alias, $userID) - { - global $database; - $database->execute("UPDATE artist_alias SET alias = ?, updated = now(), user_id = ? WHERE id = ? " - , array($alias, $userID, $aliasID)); - } - - private function update_url() - { - $urlID = $_POST['urlID']; - $url = $_POST['url']; - - if (is_null($urlID) || !is_numeric($urlID)) - return; - - if (is_null($url) || strlen($url) == 0) - return; - - global $user; - $this->save_existing_url($urlID, $url, $user->id); - } - - private function save_existing_url($urlID, $url, $userID) - { - global $database; - $database->execute("UPDATE artist_urls SET url = ?, updated = now(), user_id = ? WHERE id = ?" - , array($url, $userID, $urlID)); - } - - private function update_member() - { - $memberID = $_POST['memberID']; - $memberName = strtolower($_POST['name']); - - if (is_null($memberID) || !is_numeric($memberID)) - return; - - if (is_null($memberName) || strlen($memberName) == 0) - return; - - global $user; - $this->save_existing_member($memberID, $memberName, $user->id); - } - - private function save_existing_member($memberID, $memberName, $userID) - { - global $database; - - $database->execute("UPDATE artist_members SET name = ?, updated = now(), user_id = ? WHERE id = ?" - , array($memberName, $userID, $memberID)); - } - - /* - * HERE WE ADD AN ARTIST - */ - private function add_artist(){ - global $user; - - $name = html_escape(strtolower($_POST["name"])); - if (is_null($name) || (strlen($name) == 0) || strpos($name, " ")) - return -1; - - $notes = html_escape(ucfirst($_POST["notes"])); - if (strlen($notes) == 0) - $notes = NULL; - - $aliases = strtolower($_POST["aliases"]); - $members = strtolower($_POST["members"]); - $urls = $_POST["urls"]; - $userID = $user->id; - - $artistID = ""; - - //// WE CHECK IF THE ARTIST ALREADY EXISTS ON DATABASE; IF NOT WE CREATE - if(!$this->artist_exists($name)) - { - $artistID = $this->save_new_artist($name, $notes); - log_info("artists", "Artist {$artistID} created by {$user->name}"); - } - else - $artistID = $this->get_artist_id($name); - - if (strlen($aliases) > 0) - { - $aliasArray = explode(" ", $aliases); - foreach($aliasArray as $alias) - if (!$this->alias_exists($artistID, $alias)) - $this->save_new_alias($artistID, $alias, $userID); - } - - if (strlen($members) > 0) - { - $membersArray = explode(" ", $members); - foreach ($membersArray as $member) - if (!$this->member_exists($artistID, $member)) - $this->save_new_member($artistID, $member, "", $userID); - } - - if (strlen($urls)) - { - $urlsArray = explode("\n", $urls); - foreach ($urlsArray as $url) - if (!$this->url_exists($artistID, $url)) - $this->save_new_url($artistID, $url, $userID); - } - return $artistID; - } - - private function save_new_artist($name, $notes) - { - global $database, $user; - - $database->execute(" - INSERT INTO artists - (user_id, name, notes, created, updated) - VALUES - (?, ?, ?, now(), now())", - array($user->id, $name, $notes)); - - $result = $database->get_row("SELECT LAST_INSERT_ID() AS artistID", array()); - - return $result["artistID"]; - } - - /* - * HERE WE CHECK IF ARTIST EXIST - */ - private function artist_exists($name){ - global $database; - - $result = $database->db->GetOne("SELECT COUNT(1) FROM artists WHERE name = ?", array($name)); - return ($result != 0); - } - - /* - * HERE WE GET THE INFO OF THE ARTIST - */ - private function get_artist($artistID){ - global $database; - return $database->get_row("SELECT * FROM artists WHERE id = ?", array($artistID)); - } - - private function get_members($artistID) - { - global $database; - return $database->get_all("SELECT * FROM artist_members WHERE artist_id = ?", array($artistID)); - } - private function get_urls($artistID) - { - global $database; - return $database->get_all("SELECT id, url FROM artist_urls WHERE artist_id = ?", array($artistID)); - } - - /* - * HERE WE GET THE ID OF THE ARTIST - */ - private function get_artist_id($name){ - global $database; - $artistID = $database->get_row("SELECT id FROM artists WHERE name = ?", array($name)); - return $artistID['id']; - } - - private function get_artistID_by_aliasName($alias) - { - global $database; - - $artistID = $database->get_row("SELECT artist_id FROM artist_alias WHERE alias = ?", array($alias)); - return $artistID["artist_id"]; - } - - - /* - * HERE WE DELETE THE ARTIST - */ - private function delete_artist($artistID) - { - global $database; - $database->execute("DELETE FROM artists WHERE id = ? ", array($artistID)); - } - - - - /* - * HERE WE GET THE LIST OF ALL ARTIST WITH PAGINATION - */ - private function get_listing(Page $page, $event) - { - $pageNumber = $event->get_arg(1); - if(is_null($pageNumber) || !is_numeric($pageNumber)) - $pageNumber = 0; - else if ($pageNumber <= 0) - $pageNumber = 0; - else - $pageNumber--; - - global $config, $database; - $artistsPerPage = $config->get_int("artistsPerPage"); - - $listing = $database->get_all( - " - ( - SELECT a.id, a.user_id, a.name, u.name AS user_name, COALESCE(t.count, 0) AS posts - , 'artist' as type, a.id AS artist_id, a.name AS artist_name, a.updated - FROM artists AS a - INNER JOIN users AS u - ON a.user_id = u.id - LEFT OUTER JOIN tags AS t - ON a.name = t.tag - GROUP BY a.id, a.user_id, a.name, u.name - ORDER BY a.updated DESC - ) - - UNION - - ( - SELECT aa.id, aa.user_id, aa.alias AS name, u.name AS user_name, COALESCE(t.count, 0) AS posts - , 'alias' as type, a.id AS artist_id, a.name AS artist_name, aa.updated - FROM artist_alias AS aa - INNER JOIN users AS u - ON aa.user_id = u.id - INNER JOIN artists AS a - ON aa.artist_id = a.id - LEFT OUTER JOIN tags AS t - ON aa.alias = t.tag - GROUP BY aa.id, a.user_id, aa.alias, u.name, a.id, a.name - ORDER BY aa.updated DESC - ) - - UNION - - ( - SELECT m.id, m.user_id, m.name AS name, u.name AS user_name, COALESCE(t.count, 0) AS posts - , 'member' AS type, a.id AS artist_id, a.name AS artist_name, m.updated - FROM artist_members AS m - INNER JOIN users AS u - ON m.user_id = u.id - INNER JOIN artists AS a - ON m.artist_id = a.id - LEFT OUTER JOIN tags AS t - ON m.name = t.tag - GROUP BY m.id, m.user_id, m.name, u.name, a.id, a.name - ORDER BY m.updated DESC - ) - ORDER BY updated DESC - LIMIT ?, ? - ", array($pageNumber * $artistsPerPage, $artistsPerPage)); - - $count = $database->db->GetOne( - "SELECT COUNT(1) - FROM artists AS a - LEFT OUTER JOIN artist_members AS am - ON a.id = am.artist_id - LEFT OUTER JOIN artist_alias AS aa - ON a.id = aa.artist_id - "); - - $totalPages = ceil ($count / $artistsPerPage); - - $this->theme->list_artists($listing, $pageNumber + 1, $totalPages); - } - - /* - * HERE WE ADD AN ALIAS - */ - private function add_urls() - { - global $user; - $artistID = $_POST["artistID"]; - $urls = $_POST["urls"]; - $userID = $user->id; - - if (is_null($artistID) || !is_numeric($artistID)) - return; - - if (is_null($urls) || strlen($urls) == 0) - return; - - $urlArray = explode("\n", $urls); - - foreach ($urlArray as $url) - if (!$this->url_exists($artistID, $url)) - $this->save_new_url($artistID, $url, $userID); - } - - private function save_new_url($artistID, $url, $userID) - { - global $database; - $database->execute("INSERT INTO artist_urls (artist_id, created, updated, url, user_id) VALUES (?, now(), now(), ?, ?)" - , array($artistID, $url, $userID)); - } - - private function add_alias() - { - global $user; - $artistID = $_POST["artistID"]; - $aliases = strtolower($_POST["aliases"]); - $userID = $user->id; - - if (is_null($artistID) || !is_numeric($artistID)) - return; - - if (is_null($aliases) || trim($aliases) == "") - return; - - $aliasArray = explode(" ", $aliases); - global $database; - foreach ($aliasArray as $alias) - if (!$this->alias_exists($artistID, $alias)) - $this->save_new_alias($artistID, $alias, $userID); - } - - private function save_new_alias($artistID, $alias, $userID) - { - global $database; - $database->execute("INSERT INTO artist_alias (artist_id, created, updated, alias, user_id) VALUES (?, now(), now(), ?, ?)" - , array($artistID, $alias, $userID)); - } - - private function add_members() - { - global $user; - $artistID = $_POST["artistID"]; - $members = strtolower($_POST["members"]); - $userID = $user->id; - - if (is_null($artistID) || !is_numeric($artistID)) - return; - - if (is_null($members) || trim($members) == "") - return; - - $memberArray = explode(" ", $members); - foreach ($memberArray as $member) - if (!$this->member_exists($artistID, $member)) - $this->save_new_member($artistID, $member, $userID); - } - - private function save_new_member($artistID, $member, $userID) - { - global $database; - $database->execute("INSERT INTO artist_members (artist_id, name, created, updated, user_id) VALUES (?, ?, now(), now(), ?)" - , array($artistID, $member, $userID)); - } - - private function member_exists($artistID, $member) - { - global $database; - - $result = $database->db->GetOne("SELECT COUNT(1) FROM artist_members WHERE artist_id = ? AND name = ?", array($artistID, $member)); - return ($result != 0); - } - - private function url_exists($artistID, $url) - { - global $database; - - $result = $database->db->GetOne("SELECT COUNT(1) FROM artist_urls WHERE artist_id = ? AND url = ?", array($artistID, $url)); - return ($result != 0); - } - - /* - * HERE WE GET THE INFO OF THE ALIAS - */ - private function get_alias($artistID){ - global $database; - - return $database->get_all("SELECT id AS alias_id, alias AS alias_name ". - "FROM artist_alias ". - "WHERE artist_id = ? ". - "ORDER BY alias ASC" - , array($artistID)); - } -} -add_event_listener(new Artists()); -?> + + * Alpha + * License: GPLv2 + * Description: Simple artists extension + * Documentation: + * + */ +class AuthorSetEvent extends Event { + var $image, $user, $author; + + public function AuthorSetEvent(Image $image, User $user, $author) + { + $this->image = $image; + $this->user = $user; + $this->author = $author; + } +} + +class Artists implements Extension { + var $theme; + + public function receive_event(Event $event) + { + global $user; + + if(is_null($this->theme)) $this->theme = get_theme_object($this); + + if ($event instanceof ImageInfoSetEvent) + if (isset($_POST["tag_edit__author"])) + send_event(new AuthorSetEvent($event->image, $user, $_POST["tag_edit__author"])); + + if ($event instanceof AuthorSetEvent) + $this->update_author($event); + + if($event instanceof InitExtEvent) + $this->try_install(); + + if ($event instanceof ImageInfoBoxBuildingEvent) + $this->add_author_field_to_image($event); + + if ($event instanceof PageRequestEvent) + $this->handle_commands($event); + } + + public function try_install() { + global $config, $database; + + if ($config->get_int("ext_artists_version") < 1) + { + $database->create_table("artists", + "id SCORE_AIPK + , user_id INTEGER NOT NULL + , name VARCHAR(255) NOT NULL + , created DATETIME NOT NULL + , updated DATETIME NOT NULL + , notes TEXT + , INDEX(id) + "); + $database->create_table("artist_members", + "id SCORE_AIPK + , artist_id INTEGER NOT NULL + , user_id INTEGER NOT NULL + , name VARCHAR(255) NOT NULL + , created DATETIME NOT NULL + , updated DATETIME NOT NULL + , INDEX (id) + , FOREIGN KEY (artist_id) REFERENCES artists (id) ON UPDATE CASCADE ON DELETE CASCADE + "); + $database->create_table("artist_alias", + "id SCORE_AIPK + , artist_id INTEGER NOT NULL + , user_id INTEGER NOT NULL + , created DATETIME + , updated DATETIME + , alias VARCHAR(255) + , INDEX (id) + , FOREIGN KEY (artist_id) REFERENCES artists (id) ON UPDATE CASCADE ON DELETE CASCADE + "); + $database->create_table("artist_urls", + "id SCORE_AIPK + , artist_id INTEGER NOT NULL + , user_id INTEGER NOT NULL + , created DATETIME NOT NULL + , updated DATETIME NOT NULL + , url VARCHAR(1000) NOT NULL + , INDEX (id) + , FOREIGN KEY (artist_id) REFERENCES artists (id) ON UPDATE CASCADE ON DELETE CASCADE + "); + $database->execute("ALTER TABLE images ADD COLUMN author VARCHAR(255) NULL", array()); + + $config->set_int("artistsPerPage", 20); + $config->set_int("ext_artists_version", 1); + + log_info("artists", "extension installed"); + } + } + + public function update_author($event) + { + global $database; + + $author = strtolower($event->author); + if (strlen($author) == 0 || strpos($author, " ")) + return; + + $paddedAuthor = str_replace(" ", "_", $author); + + $artistID = NULL; + if ($this->artist_exists($author)) + $artistID = $this->get_artist_id($author); + + if (is_null($artistID) && $this->alias_exists_by_name($paddedAuthor)) + $artistID = $this->get_artistID_by_aliasName($paddedAuthor); + + if (is_null($artistID) && $this->member_exists_by_name($paddedAuthor)) + $artistID = $this->get_artistID_by_memberName($paddedAuthor); + + if (is_null($artistID) && $this->url_exists_by_url($author)) + $artistID = $this->get_artistID_by_url($author); + + if (!is_null($artistID)) + $artistName = $this->get_artistName_by_artistID($artistID); + else + { + $this->save_new_artist($author, ""); + $artistName = $author; + } + + $database->execute("UPDATE images SET author = ? WHERE id = ?" + , array( + mysql_real_escape_string($artistName) + , $event->image->id + )); + } + public function handle_commands($event) + { + global $config, $page, $user; + + if($event->page_matches("artist")) + { + switch($event->get_arg(0)) + { + //*************ARTIST SECTION************** + case "list": + { + $this->get_listing($page, $event); + $this->theme->sidebar_options("neutral"); + break; + } + case "new": + { + if(!$user->is_anonymous()){ + $this->theme->new_artist_composer(); + }else{ + $errMessage = "You must be registered and logged in to create a new artist."; + $this->theme->display_error($page, "Error", $errMessage); + } + break; + } + case "new_artist": + { + $page->set_mode("redirect"); + $page->set_redirect(make_link("artist/new")); + break; + } + case "create": + { + if(!$user->is_anonymous()) + { + $newArtistID = $this->add_artist(); + if ($newArtistID == -1) + { + $errMessage = "Error when entering artist data."; + $this->theme->display_error($page, "Error", $errMessage); + } + else + { + $page->set_mode("redirect"); + $page->set_redirect(make_link("artist/view/".$newArtistID)); + } + } + else + { + $errMessage = "You must be registered and logged in to create a new artist."; + $this->theme->display_error($page, "Error", $errMessage); + } + break; + } + + case "view": + { + $artistID = $event->get_arg(1); + $artist = $this->get_artist($artistID); + $aliases = $this->get_alias($artist['id']); + $members = $this->get_members($artist['id']); + $urls = $this->get_urls($artist['id']); + + $userIsLogged = !$user->is_anonymous(); + $userIsAdmin = $user->is_admin(); + + $images = Image::find_images(0, 4, Tag::explode($artist['name'])); + + $this->theme->show_artist($artist, $aliases, $members, $urls, $images, $userIsLogged, $userIsAdmin); + if ($userIsLogged) + { + //$this->theme->show_new_alias_composer($artistID); + //$this->theme->show_new_member_composer($artistID); + //$this->theme->show_new_url_composer($artistID); + } + + $this->theme->sidebar_options("editor", $artistID, $userIsAdmin); + + break; + } + + case "edit": + { + $artistID = $event->get_arg(1); + $artist = $this->get_artist($artistID); + $aliases = $this->get_alias($artistID); + $members = $this->get_members($artistID); + $urls = $this->get_urls($artistID); + + if(!$user->is_anonymous()){ + $this->theme->show_artist_editor($artist, $aliases, $members, $urls); + + $userIsAdmin = $user->is_admin(); + $this->theme->sidebar_options("editor", $artistID, $userIsAdmin); + }else{ + $errMessage = "You must be registered and logged in to edit an artist."; + $this->theme->display_error($page, "Error", $errMessage); + } + break; + } + case "edit_artist": + { + $artistID = $_POST['artist_id']; + $page->set_mode("redirect"); + $page->set_redirect(make_link("artist/edit/".$artistID)); + break; + } + case "edited": + { + $artistID = int_escape($_POST['id']); + $this->update_artist(); + $page->set_mode("redirect"); + $page->set_redirect(make_link("artist/view/".$artistID)); + break; + } + case "nuke_artist": + { + $artistID = $_POST['artist_id']; + $page->set_mode("redirect"); + $page->set_redirect(make_link("artist/nuke/".$artistID)); + break; + } + case "nuke": + { + $artistID = $event->get_arg(1); + $this->delete_artist($artistID); // this will delete the artist, it's alias, it's urls and it's members + $page->set_mode("redirect"); + $page->set_redirect(make_link("artist/list")); + break; + } + case "add_alias": + { + $artistID = $_POST['artist_id']; + $this->theme->show_new_alias_composer($artistID); + break; + } + case "add_member": + { + $artistID = $_POST['artist_id']; + $this->theme->show_new_member_composer($artistID); + break; + } + case "add_url": + { + $artistID = $_POST['artist_id']; + $this->theme->show_new_url_composer($artistID); + break; + } + //***********ALIAS SECTION *********************** + case "alias": + { + switch ($event->get_arg(1)) + { + case "add": + { + $artistID = $_POST['artistID']; + $this->add_alias(); + $page->set_mode("redirect"); + $page->set_redirect(make_link("artist/view/".$artistID)); + break; + } + case "delete": + { + $aliasID = $event->get_arg(2); + $artistID = $this->get_artistID_by_aliasID($aliasID); + $this->delete_alias($aliasID); + $page->set_mode("redirect"); + $page->set_redirect(make_link("artist/view/".$artistID)); + break; + } + case "edit": + { + $aliasID = int_escape($event->get_arg(2)); + $alias = $this->get_alias_by_id($aliasID); + $this->theme->show_alias_editor($alias); + break; + } + case "edited": + { + $this->update_alias(); + $aliasID = int_escape($_POST['aliasID']); + $artistID = $this->get_artistID_by_aliasID($aliasID); + $page->set_mode("redirect"); + $page->set_redirect(make_link("artist/view/".$artistID)); + break; + } + } + break; // case: alias + } + + //**************** URLS SECTION ********************** + case "url": + { + switch ($event->get_arg(1)) + { + case "add": + { + $artistID = $_POST['artistID']; + $this->add_urls(); + $page->set_mode("redirect"); + $page->set_redirect(make_link("artist/view/".$artistID)); + break; + } + case "delete": + { + $urlID = $event->get_arg(2); + $artistID = $this->get_artistID_by_urlID($urlID); + $this->delete_url($urlID); + $page->set_mode("redirect"); + $page->set_redirect(make_link("artist/view/".$artistID)); + break; + } + case "edit": + { + $urlID = int_escape($event->get_arg(2)); + $url = $this->get_url_by_id($urlID); + $this->theme->show_url_editor($url); + break; + } + case "edited": + { + $this->update_url(); + $urlID = int_escape($_POST['urlID']); + $artistID = $this->get_artistID_by_urlID($urlID); + $page->set_mode("redirect"); + $page->set_redirect(make_link("artist/view/".$artistID)); + break; + } + } + break; // case: urls + } + //******************* MEMBERS SECTION ********************* + case "member": + { + switch ($event->get_arg(1)) + { + case "add": + { + $artistID = $_POST['artistID']; + $this->add_members(); + $page->set_mode("redirect"); + $page->set_redirect(make_link("artist/view/".$artistID)); + break; + } + case "delete": + { + $memberID = int_escape($event->get_arg(2)); + $artistID = $this->get_artistID_by_memberID($memberID); + $this->delete_member($memberID); + $page->set_mode("redirect"); + $page->set_redirect(make_link("artist/view/".$artistID)); + break; + } + case "edit": + { + $memberID = int_escape($event->get_arg(2)); + $member = $this->get_member_by_id($memberID); + $this->theme->show_member_editor($member); + break; + } + case "edited": + { + $this->update_member(); + $memberID = int_escape($_POST['memberID']); + $artistID = $this->get_artistID_by_memberID($memberID); + $page->set_mode("redirect"); + $page->set_redirect(make_link("artist/view/".$artistID)); + break; + } + } + break; //case: members + } + } + } + } + + public function add_author_field_to_image($event) + { + $artistName = $this->get_artistName_by_imageID($event->image->id); + + $event->add_part($this->theme->get_author_editor_html($artistName), 42); + } + + private function get_artistName_by_imageID($imageID) + { + if(!is_numeric($imageID)) return null; + + global $database; + + $result = $database->get_row("SELECT author FROM images WHERE id = ?", array($imageID)); + return stripslashes($result['author']); + } + + private function url_exists_by_url($url) + { + global $database; + + $result = $database->db->GetOne("SELECT COUNT(1) FROM artist_urls WHERE url = ?", array(mysql_real_escape_string($url))); + return ($result != 0); + } + + private function member_exists_by_name($member) + { + global $database; + + $result = $database->db->GetOne("SELECT COUNT(1) FROM artist_members WHERE name = ?", array(mysql_real_escape_string($member))); + return ($result != 0); + } + + private function alias_exists_by_name($alias) + { + global $database; + + $result = $database->db->GetOne("SELECT COUNT(1) FROM artist_alias WHERE alias = ?", array(mysql_real_escape_string($alias))); + return ($result != 0); + } + + private function alias_exists($artistID, $alias){ + if (!is_numeric($artistID)) return; + + global $database; + + $result = $database->db->GetOne("SELECT COUNT(1) FROM artist_alias WHERE artist_id = ? AND alias = ?", array( + $artistID + , mysql_real_escape_string($alias) + )); + return ($result != 0); + } + + private function get_artistID_by_url($url) + { + global $database; + $result = $database->get_row("SELECT artist_id FROM artist_urls WHERE url = ?", array(mysql_real_escape_string($url))); + return $result['artist_id']; + } + + private function get_artistID_by_memberName($member) + { + global $database; + $result = $database->get_row("SELECT artist_id FROM artist_members WHERE name = ?", array(mysql_real_escape_string($member))); + return $result['artist_id']; + } + private function get_artistName_by_artistID($artistID) + { + if (!is_numeric($artistID)) return; + + global $database; + $result = $database->get_row("SELECT name FROM artists WHERE id = ?", array($artistID)); + return stripslashes($result['name']); + } + + private function get_artistID_by_aliasID($aliasID) + { + if (!is_numeric($aliasID)) return; + + global $database; + $result = $database->get_row("SELECT artist_id FROM artist_alias WHERE id = ?", array($aliasID)); + return $result['artist_id']; + } + + private function get_artistID_by_memberID($memberID) + { + if (!is_numeric($memberID)) return; + + global $database; + $result = $database->get_row("SELECT artist_id FROM artist_members WHERE id = ?", array($memberID)); + return $result['artist_id']; + } + + private function get_artistID_by_urlID($urlID) + { + if (!is_numeric($urlID)) return; + + global $database; + $result = $database->get_row("SELECT artist_id FROM artist_urls WHERE id = ?", array($urlID)); + return $result['artist_id']; + } + + private function delete_alias($aliasID) + { + if (!is_numeric($aliasID)) return; + + global $database; + $database->execute("DELETE FROM artist_alias WHERE id = ?", array($aliasID)); + } + + private function delete_url($urlID) + { + if (!is_numeric($urlID)) return; + + global $database; + $database->execute("DELETE FROM artist_urls WHERE id = ?", array($urlID)); + } + + private function delete_member($memberID) + { + if (!is_numeric($memberID)) return; + + global $database; + $database->execute("DELETE FROM artist_members WHERE id = ?", array($memberID)); + } + + + private function get_alias_by_id($aliasID) + { + if (!is_numeric($aliasID)) return; + + global $database; + $result = $database->get_row("SELECT * FROM artist_alias WHERE id = ?", array($aliasID)); + + $result["alias"] = stripslashes($result["alias"]); + + return $result; + } + + private function get_url_by_id($urlID) + { + if (!is_numeric($urlID)) return; + + global $database; + $result = $database->get_row("SELECT * FROM artist_urls WHERE id = ?", array($urlID)); + + $result["url"] = stripslashes($result["url"]); + + return $result; + } + + private function get_member_by_id($memberID) + { + if (!is_numeric($memberID)) return; + + global $database; + $result = $database->get_row("SELECT * FROM artist_members WHERE id = ?", array($memberID)); + + $result["name"] = stripslashes($result["name"]); + + return $result; + } + + private function update_artist() + { + global $user; + $artistID = $_POST['id']; + $name = strtolower($_POST['name']); + $notes = $_POST['notes']; + $userID = $user->id; + + $aliasesAsString = trim($_POST["aliases"]); + if (strlen($aliasesAsString) == 0) $aliasesAsString = NULL; + $aliasesIDsAsString = trim($_POST["aliasesIDs"]); + if (strlen($aliasesIDsAsString) == 0) $aliasesIDsAsString = NULL; + + $membersAsString = trim($_POST["members"]); + if (strlen($membersAsString) == 0) $membersAsString = NULL; + $membersIDsAsString = trim($_POST["membersIDs"]); + if (strlen($membersIDsAsString) == 0) $membersIDsAsString = NULL; + + $urlsAsString = trim($_POST["urls"]); + if (strlen($urlsAsString) == 0) $urlsAsString = NULL; + $urlsIDsAsString = trim($_POST["urlsIDs"]); + if (strlen($urlsIDsAsString) == 0) $urlsIDsAsString = NULL; + + if (is_null($artistID) || !is_numeric($artistID)) + return; + + if (is_null($userID) || !is_numeric($userID)) + return; + + if (is_null($name) || strlen($name) == 0 || strpos($name, " ")) + return; + + //if (is_null($aliasesAsString) || is_null($aliasesIDsAsString)) + // return; + + //if (is_null($membersAsString) || is_null($membersIDsAsString)) + // return; + + //if (is_null($urlsAsString) || is_null($urlsIDsAsString)) + // return; + + if (strlen($notes) == 0) + $notes = NULL; + + global $database; + $database->execute("UPDATE artists SET name = ?, notes = ?, updated = now(), user_id = ? WHERE id = ? " + , array( + mysql_real_escape_string($name) + , mysql_real_escape_string($notes) + , $userID + , $artistID + )); + + // ALIAS MATCHING SECTION + $i = 0; + $aliasesAsArray = is_null($aliasesAsString) ? array() : explode(" ", $aliasesAsString); + $aliasesIDsAsArray = is_null($aliasesIDsAsString) ? array() : explode(" ", $aliasesIDsAsString); + while ($i < count($aliasesAsArray)) + { + // if an alias was updated + if ($i < count($aliasesIDsAsArray)) + // save it + $this->save_existing_alias($aliasesIDsAsArray[$i], $aliasesAsArray[$i], $userID); + else + // if we already updated all, save new ones + $this->save_new_alias($artistID, $aliasesAsArray[$i], $userID); + + $i++; + } + // if we have more ids than alias, then some alias have been deleted -- delete them from db + while ($i < count($aliasesIDsAsArray)) + $this->delete_alias($aliasesIDsAsArray[$i++]); + + // MEMBERS MATCHING SECTION + $i = 0; + $membersAsArray = is_null($membersAsString) ? array() : explode(" ", $membersAsString); + $membersIDsAsArray = is_null($membersIDsAsString) ? array() : explode(" ", $membersIDsAsString); + while ($i < count($membersAsArray)) + { + // if a member was updated + if ($i < count($membersIDsAsArray)) + //save it + $this->save_existing_member($membersIDsAsArray[$i], $membersAsArray[$i], $userID); + else + // if we already updated all, save new ones + $this->save_new_member($artistID, $membersAsArray[$i], "", $userID); + + $i++; + } + // if we have more ids than members, then some members have been deleted -- delete them from db + while ($i < count($membersIDsAsArray)) + $this->delete_member($membersIDsAsArray[$i++]); + + // URLS MATCHING SECTION + $i = 0; + $urlsAsString = str_replace("\r\n", "\n", $urlsAsString); + $urlsAsString = str_replace("\n\r", "\n", $urlsAsString); + $urlsAsArray = is_null($urlsAsString) ? array() : explode("\n", $urlsAsString); + $urlsIDsAsArray = is_null($urlsIDsAsString) ? array() : explode(" ", $urlsIDsAsString); + while ($i < count($urlsAsArray)) + { + // if an URL was updated + if ($i < count($urlsIDsAsArray)) + { + // save it + $this->save_existing_url($urlsIDsAsArray[$i], $urlsAsArray[$i], $userID); + } + else + { + $this->save_new_url($artistID, $urlsAsArray[$i], $userID); + } + + $i++; + } + + // if we have more ids than urls, then some urls have been deleted -- delete them from db + while ($i < count($urlsIDsAsArray)) + $this->delete_url($urlsIDsAsArray[$i++]); + } + + private function update_alias() + { + $aliasID = $_POST['aliasID']; + $alias = strtolower($_POST['alias']); + + if (is_null($aliasID) || !is_numeric($aliasID)) + return; + + if (is_null($alias) || strlen($alias) == 0) + return; + + global $user; + $this->save_existing_alias($aliasID, $alias, $user->id); + } + + private function save_existing_alias($aliasID, $alias, $userID) + { + if (!is_numeric($userID)) return; + if (!is_numeric($aliasID)) return; + + global $database; + $database->execute("UPDATE artist_alias SET alias = ?, updated = now(), user_id = ? WHERE id = ? " + , array( + mysql_real_escape_string($alias) + , $userID + , $aliasID + )); + } + + private function update_url() + { + $urlID = $_POST['urlID']; + $url = $_POST['url']; + + if (is_null($urlID) || !is_numeric($urlID)) + return; + + if (is_null($url) || strlen($url) == 0) + return; + + global $user; + $this->save_existing_url($urlID, $url, $user->id); + } + + private function save_existing_url($urlID, $url, $userID) + { + if (!is_numeric($userID)) return; + if (!is_numeric($urlID)) return; + + global $database; + $database->execute("UPDATE artist_urls SET url = ?, updated = now(), user_id = ? WHERE id = ?" + , array( + mysql_real_escape_string($url) + , $userID + , $urlID + )); + } + + private function update_member() + { + $memberID = $_POST['memberID']; + $memberName = strtolower($_POST['name']); + + if (is_null($memberID) || !is_numeric($memberID)) + return; + + if (is_null($memberName) || strlen($memberName) == 0) + return; + + global $user; + $this->save_existing_member($memberID, $memberName, $user->id); + } + + private function save_existing_member($memberID, $memberName, $userID) + { + if (!is_numeric($memberID)) return; + if (!is_numeric($userID)) return; + + global $database; + + $database->execute("UPDATE artist_members SET name = ?, updated = now(), user_id = ? WHERE id = ?" + , array( + mysql_real_escape_string($memberName) + , $userID + , $memberID + )); + } + + /* + * HERE WE ADD AN ARTIST + */ + private function add_artist(){ + global $user; + + $name = html_escape(strtolower($_POST["name"])); + if (is_null($name) || (strlen($name) == 0) || strpos($name, " ")) + return -1; + + $notes = html_escape(ucfirst($_POST["notes"])); + if (strlen($notes) == 0) + $notes = NULL; + + $aliases = strtolower($_POST["aliases"]); + $members = strtolower($_POST["members"]); + $urls = $_POST["urls"]; + $userID = $user->id; + + $artistID = ""; + + //// WE CHECK IF THE ARTIST ALREADY EXISTS ON DATABASE; IF NOT WE CREATE + if(!$this->artist_exists($name)) + { + $artistID = $this->save_new_artist($name, $notes); + log_info("artists", "Artist {$artistID} created by {$user->name}"); + } + else + $artistID = $this->get_artist_id($name); + + if (strlen($aliases) > 0) + { + $aliasArray = explode(" ", $aliases); + foreach($aliasArray as $alias) + if (!$this->alias_exists($artistID, $alias)) + $this->save_new_alias($artistID, $alias, $userID); + } + + if (strlen($members) > 0) + { + $membersArray = explode(" ", $members); + foreach ($membersArray as $member) + if (!$this->member_exists($artistID, $member)) + $this->save_new_member($artistID, $member, "", $userID); + } + + if (strlen($urls)) + { + //delete double "separators" + $urls = str_replace("\r\n", "\n", $urls); + $urls = str_replace("\n\r", "\n", $urls); + + $urlsArray = explode("\n", $urls); + foreach ($urlsArray as $url) + if (!$this->url_exists($artistID, $url)) + $this->save_new_url($artistID, $url, $userID); + } + return $artistID; + } + + private function save_new_artist($name, $notes) + { + global $database, $user; + + $database->execute(" + INSERT INTO artists + (user_id, name, notes, created, updated) + VALUES + (?, ?, ?, now(), now())", + array( + $user->id + , mysql_real_escape_string($name) + , mysql_real_escape_string($notes) + )); + + $result = $database->get_row("SELECT LAST_INSERT_ID() AS artistID", array()); + + return $result["artistID"]; + } + + /* + * HERE WE CHECK IF ARTIST EXIST + */ + private function artist_exists($name){ + global $database; + + $result = $database->db->GetOne("SELECT COUNT(1) FROM artists WHERE name = ?" + , array( + mysql_real_escape_string($name) + )); + return ($result != 0); + } + + /* + * HERE WE GET THE INFO OF THE ARTIST + */ + private function get_artist($artistID){ + if (!is_numeric($artistID)) return; + + global $database; + $result = $database->get_row("SELECT * FROM artists WHERE id = ?", + array( + $artistID + )); + + $result["name"] = stripslashes($result["name"]); + $result["notes"] = stripslashes($result["notes"]); + + return $result; + } + + private function get_members($artistID) + { + if (!is_numeric($artistID)) return; + + global $database; + $result = $database->get_all("SELECT * FROM artist_members WHERE artist_id = ?" + , array( + $artistID + )); + + for ($i = 0 ; $i < count($result) ; $i++) + { + $result[$i]["name"] = stripslashes($result[$i]["name"]); + } + + return $result; + } + private function get_urls($artistID) + { + if (!is_numeric($artistID)) return; + + global $database; + $result = $database->get_all("SELECT id, url FROM artist_urls WHERE artist_id = ?" + , array( + $artistID + )); + + for ($i = 0 ; $i < count($result) ; $i++) + { + $result[$i]["url"] = stripslashes($result[$i]["url"]); + } + + + return $result; + } + + /* + * HERE WE GET THE ID OF THE ARTIST + */ + private function get_artist_id($name){ + global $database; + $artistID = $database->get_row("SELECT id FROM artists WHERE name = ?" + , array( + mysql_real_escape_string($name) + )); + return $artistID['id']; + } + + private function get_artistID_by_aliasName($alias) + { + global $database; + + $artistID = $database->get_row("SELECT artist_id FROM artist_alias WHERE alias = ?" + , array( + mysql_real_escape_string($alias) + )); + return $artistID["artist_id"]; + } + + + /* + * HERE WE DELETE THE ARTIST + */ + private function delete_artist($artistID) + { + if (!is_numeric($artistID)) return; + + global $database; + $database->execute("DELETE FROM artists WHERE id = ? " + , array( + $artistID + )); + } + + + + /* + * HERE WE GET THE LIST OF ALL ARTIST WITH PAGINATION + */ + private function get_listing(Page $page, $event) + { + $pageNumber = $event->get_arg(1); + if(is_null($pageNumber) || !is_numeric($pageNumber)) + $pageNumber = 0; + else if ($pageNumber <= 0) + $pageNumber = 0; + else + $pageNumber--; + + global $config, $database; + $artistsPerPage = $config->get_int("artistsPerPage"); + + $listing = $database->get_all( + " + ( + SELECT a.id, a.user_id, a.name, u.name AS user_name, COALESCE(t.count, 0) AS posts + , 'artist' as type, a.id AS artist_id, a.name AS artist_name, a.updated + FROM artists AS a + INNER JOIN users AS u + ON a.user_id = u.id + LEFT OUTER JOIN tags AS t + ON a.name = t.tag + GROUP BY a.id, a.user_id, a.name, u.name + ORDER BY a.updated DESC + ) + + UNION + + ( + SELECT aa.id, aa.user_id, aa.alias AS name, u.name AS user_name, COALESCE(t.count, 0) AS posts + , 'alias' as type, a.id AS artist_id, a.name AS artist_name, aa.updated + FROM artist_alias AS aa + INNER JOIN users AS u + ON aa.user_id = u.id + INNER JOIN artists AS a + ON aa.artist_id = a.id + LEFT OUTER JOIN tags AS t + ON aa.alias = t.tag + GROUP BY aa.id, a.user_id, aa.alias, u.name, a.id, a.name + ORDER BY aa.updated DESC + ) + + UNION + + ( + SELECT m.id, m.user_id, m.name AS name, u.name AS user_name, COALESCE(t.count, 0) AS posts + , 'member' AS type, a.id AS artist_id, a.name AS artist_name, m.updated + FROM artist_members AS m + INNER JOIN users AS u + ON m.user_id = u.id + INNER JOIN artists AS a + ON m.artist_id = a.id + LEFT OUTER JOIN tags AS t + ON m.name = t.tag + GROUP BY m.id, m.user_id, m.name, u.name, a.id, a.name + ORDER BY m.updated DESC + ) + ORDER BY updated DESC + LIMIT ?, ? + ", array( + $pageNumber * $artistsPerPage + , $artistsPerPage + )); + + for ($i = 0 ; $i < count($listing) ; $i++) + { + $listing[$i]["name"] = stripslashes($listing[$i]["name"]); + $listing[$i]["user_name"] = stripslashes($listing[$i]["user_name"]); + $listing[$i]["artist_name"] = stripslashes($listing[$i]["artist_name"]); + } + + $count = $database->db->GetOne( + "SELECT COUNT(1) + FROM artists AS a + LEFT OUTER JOIN artist_members AS am + ON a.id = am.artist_id + LEFT OUTER JOIN artist_alias AS aa + ON a.id = aa.artist_id + "); + + $totalPages = ceil ($count / $artistsPerPage); + + $this->theme->list_artists($listing, $pageNumber + 1, $totalPages); + } + + /* + * HERE WE ADD AN ALIAS + */ + private function add_urls() + { + global $user; + $artistID = $_POST["artistID"]; + $urls = $_POST["urls"]; + $userID = $user->id; + + if (is_null($artistID) || !is_numeric($artistID)) + return; + + if (is_null($urls) || strlen($urls) == 0) + return; + + $urlArray = explode("\n", $urls); + + foreach ($urlArray as $url) + if (!$this->url_exists($artistID, $url)) + $this->save_new_url($artistID, $url, $userID); + } + + private function save_new_url($artistID, $url, $userID) + { + if (!is_numeric($artistID)) return; + if (!is_numeric($userID)) return; + + global $database; + $database->execute("INSERT INTO artist_urls (artist_id, created, updated, url, user_id) VALUES (?, now(), now(), ?, ?)" + , array( + $artistID + , mysql_real_escape_string($url) + , $userID + )); + } + + private function add_alias() + { + global $user; + $artistID = $_POST["artistID"]; + $aliases = strtolower($_POST["aliases"]); + $userID = $user->id; + + if (is_null($artistID) || !is_numeric($artistID)) + return; + + if (is_null($aliases) || trim($aliases) == "") + return; + + $aliasArray = explode(" ", $aliases); + global $database; + foreach ($aliasArray as $alias) + if (!$this->alias_exists($artistID, $alias)) + $this->save_new_alias($artistID, $alias, $userID); + } + + private function save_new_alias($artistID, $alias, $userID) + { + if (!is_numeric($artistID)) return; + if (!is_numeric($userID)) return; + + global $database; + $database->execute("INSERT INTO artist_alias (artist_id, created, updated, alias, user_id) VALUES (?, now(), now(), ?, ?)" + , array( + $artistID + , mysql_real_escape_string($alias) + , $userID + )); + } + + private function add_members() + { + global $user; + $artistID = $_POST["artistID"]; + $members = strtolower($_POST["members"]); + $userID = $user->id; + + if (is_null($artistID) || !is_numeric($artistID)) + return; + + if (is_null($members) || trim($members) == "") + return; + + $memberArray = explode(" ", $members); + foreach ($memberArray as $member) + if (!$this->member_exists($artistID, $member)) + $this->save_new_member($artistID, $member, $userID); + } + + private function save_new_member($artistID, $member, $userID) + { + if (!is_numeric($artistID)) return; + if (!is_numeric($userID)) return; + + global $database; + $database->execute("INSERT INTO artist_members (artist_id, name, created, updated, user_id) VALUES (?, ?, now(), now(), ?)" + , array( + $artistID + , mysql_real_escape_string($member) + , $userID + )); + } + + private function member_exists($artistID, $member) + { + if (!is_numeric($artistID)) return; + + global $database; + + $result = $database->db->GetOne("SELECT COUNT(1) FROM artist_members WHERE artist_id = ? AND name = ?" + , array( + $artistID + , mysql_real_escape_string($member) + )); + return ($result != 0); + } + + private function url_exists($artistID, $url) + { + if (!is_numeric($artistID)) return; + + global $database; + + $result = $database->db->GetOne("SELECT COUNT(1) FROM artist_urls WHERE artist_id = ? AND url = ?" + , array( + $artistID + , mysql_real_escape_string($url) + )); + return ($result != 0); + } + + /* + * HERE WE GET THE INFO OF THE ALIAS + */ + private function get_alias($artistID){ + if (!is_numeric($artistID)) return; + + global $database; + + $result = $database->get_all("SELECT id AS alias_id, alias AS alias_name ". + "FROM artist_alias ". + "WHERE artist_id = ? ". + "ORDER BY alias ASC" + , array($artistID)); + + for ($i = 0 ; $i < count($result) ; $i++) + { + $result[$i]["alias_name"] = stripslashes($result[$i]["alias_name"]); + } + return $result; + } +} +add_event_listener(new Artists()); +?> diff --git a/contrib/notes/main.php b/contrib/notes/main.php index 97fde17d..30cfd582 100644 --- a/contrib/notes/main.php +++ b/contrib/notes/main.php @@ -1,610 +1,610 @@ - - * License: GPLv2 - * Description: Annotate images - * Documentation: - */ - -class Notes extends SimpleExtension { - public function onInitExt($event) { - global $config, $database; - - // shortcut to latest - if ($config->get_int("ext_notes_version") < 1) { - $database->Execute("ALTER TABLE images ADD COLUMN notes INTEGER NOT NULL DEFAULT 0"); - $database->create_table("notes", " - id SCORE_AIPK, - enable INTEGER NOT NULL, - image_id INTEGER NOT NULL, - user_id INTEGER NOT NULL, - user_ip CHAR(15) NOT NULL, - date DATETIME NOT NULL, - x1 INTEGER NOT NULL, - y1 INTEGER NOT NULL, - height INTEGER NOT NULL, - width INTEGER NOT NULL, - note TEXT NOT NULL, - INDEX (image_id), - FOREIGN KEY (image_id) REFERENCES images(id) ON DELETE CASCADE - "); - - $database->create_table("note_request", " - id SCORE_AIPK, - image_id INTEGER NOT NULL, - user_id INTEGER NOT NULL, - date DATETIME NOT NULL, - INDEX (image_id), - FOREIGN KEY (image_id) REFERENCES images(id) ON DELETE CASCADE - "); - - $database->create_table("note_histories", " - id SCORE_AIPK, - note_enable INTEGER NOT NULL, - note_id INTEGER NOT NULL, - review_id INTEGER NOT NULL, - image_id INTEGER NOT NULL, - user_id INTEGER NOT NULL, - user_ip CHAR(15) NOT NULL, - date DATETIME NOT NULL, - x1 INTEGER NOT NULL, - y1 INTEGER NOT NULL, - height INTEGER NOT NULL, - width INTEGER NOT NULL, - note TEXT NOT NULL, - INDEX (image_id), - FOREIGN KEY (note_id) REFERENCES notes(id) ON DELETE CASCADE - "); - - $config->set_int("notesNotesPerPage", 20); - $config->set_int("notesRequestsPerPage", 20); - $config->set_int("notesHistoriesPerPage", 20); - - $config->set_int("ext_notes_version", 1); - log_info("notes", "extension installed"); - } - } - - - - public function onPageRequest($event) { - global $page, $user; - if($event->page_matches("note")) { - - switch($event->get_arg(0)) { - case "list": //index - { - $this->get_notes_list($event); // This should show images like post/list but i don't know how do that. - break; - } - case "requests": // The same as post/list but only for note_request table. - { - $this->get_notes_requests($event); // This should shouw images like post/list but i don't know how do that. - break; - } - case "search": - { - if(!$user->is_anonymous()) - $this->theme->search_notes_page($page); - break; - } - case "updated": //Thinking how biuld this function. - { - $this->get_histories($event); - break; - } - case "history": //Thinking how biuld this function. - { - $this->get_history($event); - break; - } - case "revert": - { - $noteID = $event->get_arg(1); - $reviewID = $event->get_arg(2); - if(!$user->is_anonymous()){ - $this->revert_history($noteID, $reviewID); - } - - $page->set_mode("redirect"); - $page->set_redirect(make_link("note/updated")); - break; - } - case "add_note": - { - if(!$user->is_anonymous()) - $this->add_new_note(); - - $page->set_mode("redirect"); - $page->set_redirect(make_link("post/view/".$_POST["image_id"])); - break; - } - case "add_request": - { - if(!$user->is_anonymous()) - $this->add_note_request(); - - $page->set_mode("redirect"); - $page->set_redirect(make_link("post/view/".$_POST["image_id"])); - break; - } - case "nuke_notes": - { - if($user->is_admin()) - $this->nuke_notes(); - - $page->set_mode("redirect"); - $page->set_redirect(make_link("post/view/".$_POST["image_id"])); - break; - } - case "nuke_requests": - { - if($user->is_admin()) - $this->nuke_requests(); - - $page->set_mode("redirect"); - $page->set_redirect(make_link("post/view/".$_POST["image_id"])); - break; - } - case "edit_note": - { - if (!$user->is_anonymous()) { - $this->update_note(); - $page->set_mode("redirect"); - $page->set_redirect(make_link("post/view/".$_POST["image_id"])); - } - break; - } - case "delete_note": - { - if ($user->is_admin()) { - $this->delete_note(); - $page->set_mode("redirect"); - $page->set_redirect(make_link("post/view/".$_POST["image_id"])); - } - break; - } - default: - { - $page->set_mode("redirect"); - $page->set_redirect(make_link("note/list")); - break; - } - } - } - } - - - /* - * HERE WE LOAD THE NOTES IN THE IMAGE - */ - public function onDisplayingImage($event) { - global $page, $user; - - //display form on image event - $notes = $this->get_notes($event->image->id); - $this->theme->display_note_system($page, $event->image->id, $notes, $user->is_admin()); - } - - - /* - * HERE WE ADD THE BUTTONS ON SIDEBAR - */ - public function onImageAdminBlockBuilding($event) { - global $user; - if(!$user->is_anonymous()) { - $event->add_part($this->theme->note_button($event->image->id)); - $event->add_part($this->theme->request_button($event->image->id)); - if($user->is_admin()) { - $event->add_part($this->theme->nuke_notes_button($event->image->id)); - $event->add_part($this->theme->nuke_requests_button($event->image->id)); - } - } - } - - - /* - * HERE WE ADD QUERYLETS TO ADD SEARCH SYSTEM - */ - public function onSearchTermParse($event) { - $matches = array(); - if(preg_match("/note=(.*)/i", $event->term, $matches)) { - $notes = int_escape($matches[1]); - $event->add_querylet(new Querylet("images.id IN (SELECT image_id FROM notes WHERE note = $notes)")); - } - else if(preg_match("/notes(<|>|<=|>=|=)(\d+)/", $event->term, $matches)) { - $cmp = $matches[1]; - $notes = $matches[2]; - $event->add_querylet(new Querylet("images.id IN (SELECT id FROM images WHERE notes $cmp $notes)")); - } - else if(preg_match("/notes_by=(.*)/i", $event->term, $matches)) { - global $database; - $user = User::by_name($matches[1]); - if(!is_null($user)) { - $user_id = $user->id; - } - else { - $user_id = -1; - } - - $event->add_querylet(new Querylet("images.id IN (SELECT image_id FROM notes WHERE user_id = $user_id)")); - } - else if(preg_match("/notes_by_userno=([0-9]+)/i", $event->term, $matches)) { - $user_id = int_escape($matches[1]); - $event->add_querylet(new Querylet("images.id IN (SELECT image_id FROM notes WHERE user_id = $user_id)")); - } - } - - - /* - * HERE WE GET ALL NOTES FOR DISPLAYED IMAGE - */ - private function get_notes($imageID) { - global $database; - - return $database->get_all( - "SELECT * ". - "FROM notes ". - "WHERE enable = ? AND image_id = ? ". - "ORDER BY date ASC" - , array('1', $imageID)); - } - - - /* - * HERE WE ADD A NOTE TO DATABASE - */ - private function add_new_note() { - global $database, $user; - - $imageID = int_escape($_POST["image_id"]); - $user_id = $user->id; - $noteX1 = int_escape($_POST["note_x1"]); - $noteY1 = int_escape($_POST["note_y1"]); - $noteHeight = int_escape($_POST["note_height"]); - $noteWidth = int_escape($_POST["note_width"]); - $noteText = html_escape($_POST["note_text"]); - - $database->execute(" - INSERT INTO notes - (enable, image_id, user_id, user_ip, date, x1, y1, height, width, note) - VALUES - (?, ?, ?, ?, now(), ?, ?, ?, ?, ?)", - array(1, $imageID, $user_id, $_SERVER['REMOTE_ADDR'], $noteX1, $noteY1, $noteHeight, $noteWidth, $noteText)); - - $result = $database->get_row("SELECT LAST_INSERT_ID() AS noteID", array()); - $noteID = $result["noteID"]; - - log_info("notes", "Note added {$noteID} by {$user->name}"); - - $database->Execute("UPDATE images SET notes=(SELECT COUNT(*) FROM notes WHERE image_id=?) WHERE id=?", array($imageID, $imageID)); - - $this->add_history(1, $noteID, $imageID, $noteX1, $noteY1, $noteHeight, $noteWidth, $noteText); - } - - - - /* - * HERE WE ADD A REQUEST TO DATABASE - */ - private function add_note_request() { - global $database, $user; - - $image_id = int_escape($_POST["image_id"]); - $user_id = $user->id; - - $database->execute(" - INSERT INTO note_request - (image_id, user_id, date) - VALUES - (?, ?, now())", - array($image_id, $user_id)); - - $result = $database->get_row("SELECT LAST_INSERT_ID() AS requestID", array()); - - log_info("notes", "Note requested {$result["requestID"]} by {$user->name}"); - } - - - - /* - * HERE WE EDIT THE NOTE - */ - private function update_note() - { - $imageID = $_POST["image_id"]; - $noteID = $_POST["note_id"]; - $noteX1 = $_POST["note_x1"]; - $noteY1 = $_POST["note_y1"]; - $noteHeight = $_POST["note_height"]; - $noteWidth = $_POST["note_width"]; - $noteText = $_POST["note_text"]; - - // validate parameters - if(is_null($imageID) || !is_numeric($imageID)) - return; - - if(is_null($noteID) || !is_numeric($noteID)) - return; - - if(is_null($noteX1) || !is_numeric($noteX1)) - return; - - if(is_null($noteY1) || !is_numeric($noteY1)) - return; - - if(is_null($noteHeight) || !is_numeric($noteHeight)) - return; - - if(is_null($noteWidth) || !is_numeric($noteWidth)) - return; - - if(is_null($noteText) || strlen($noteText) == 0) - return; - - global $database; - $database->execute("UPDATE notes ". - "SET x1 = ?, ". - "y1 = ?, ". - "height = ?, ". - "width = ?,". - "note = ? ". - "WHERE image_id = ? AND id = ?", array($noteX1, $noteY1, $noteHeight, $noteWidth, $noteText, $imageID, $noteID)); - - $this->add_history(1, $noteID, $imageID, $noteX1, $noteY1, $noteHeight, $noteWidth, $noteText); - } - - - - /* - * HERE WE DELETE THE NOTE - */ - private function delete_note() - { - $imageID = $_POST["image_id"]; - $noteID = $_POST["note_id"]; - - // validate parameters - if(is_null($imageID) || !is_numeric($imageID)) - return; - - if(is_null($noteID) || !is_numeric($noteID)) - return; - - global $database; - - $database->execute("UPDATE notes ". - "SET enable = ? ". - "WHERE image_id = ? AND id = ?", array(0, $imageID, $noteID)); - - log_info("notes", "Note deleted {$noteID} by {$user->name}"); - } - - - - /* - * HERE WE DELETE ALL NOTES FROM IMAGE - */ - private function nuke_notes() { - global $database; - $image_id = int_escape($_POST["image_id"]); - $database->execute("DELETE FROM notes WHERE image_id = ?", array($image_id)); - log_info("notes", "Notes deleted from {$image_id} by {$user->name}"); - } - - - - /* - * HERE WE DELETE ALL REQUESTS FOR IMAGE - */ - private function nuke_requests() { - global $database; - $image_id = int_escape($_POST["image_id"]); - - $database->execute("DELETE FROM note_request WHERE image_id = ?", array($image_id)); - - log_info("notes", "Requests deleted from {$image_id} by {$user->name}"); - } - - - - /* - * HERE WE ALL IMAGES THAT HAVE NOTES - */ - private function get_notes_list($event) { - $pageNumber = $event->get_arg(1); - if(is_null($pageNumber) || !is_numeric($pageNumber)) - $pageNumber = 0; - else if ($pageNumber <= 0) - $pageNumber = 0; - else - $pageNumber--; - - global $config; - - $notesPerPage = $config->get_int('notesNotesPerPage'); - - - //$result = $database->get_all("SELECT * FROM pool_images WHERE pool_id=?", array($poolID)); - global $database; - $get_notes = " - SELECT DISTINCT image_id ". - "FROM notes ". - "WHERE enable = ? ". - "ORDER BY date DESC LIMIT ?, ?"; - - $result = $database->Execute($get_notes, array(1, $pageNumber * $notesPerPage, $notesPerPage)); - - $totalPages = ceil($database->db->GetOne("SELECT COUNT(*) FROM notes") / $notesPerPage); - - $images = array(); - while(!$result->EOF) { - $image = Image::by_id($result->fields["image_id"]); - $images[] = array($image); - $result->MoveNext(); - } - - $this->theme->display_note_list($images, $pageNumber + 1, $totalPages); - } - - - - /* - * HERE WE GET ALL NOTE REQUESTS - */ - private function get_notes_requests($event) { - $pageNumber = $event->get_arg(1); - if(is_null($pageNumber) || !is_numeric($pageNumber)) - $pageNumber = 0; - else if ($pageNumber <= 0) - $pageNumber = 0; - else - $pageNumber--; - - global $config; - - $requestsPerPage = $config->get_int('notesRequestsPerPage'); - - - //$result = $database->get_all("SELECT * FROM pool_images WHERE pool_id=?", array($poolID)); - global $database; - $get_requests = " - SELECT DISTINCT image_id ". - "FROM note_request ". - "ORDER BY date DESC LIMIT ?, ?"; - - $result = $database->Execute($get_requests, array($pageNumber * $requestsPerPage, $requestsPerPage)); - - $totalPages = ceil($database->db->GetOne("SELECT COUNT(*) FROM note_request") / $requestsPerPage); - - $images = array(); - while(!$result->EOF) { - $image = Image::by_id($result->fields["image_id"]); - $images[] = array($image); - $result->MoveNext(); - } - - $this->theme->display_note_requests($images, $pageNumber + 1, $totalPages); - } - - - - /* - * HERE WE ADD HISTORY TO TRACK THE CHANGES OF THE NOTES FOR THE IMAGES. - */ - private function add_history($noteEnable, $noteID, $imageID, $noteX1, $noteY1, $noteHeight, $noteWidth, $noteText){ - global $user, $database; - - $userID = $user->id; - - $reviewID = $database->db->GetOne("SELECT COUNT(*) FROM note_histories WHERE note_id = ?", array($noteID)); - $reviewID = $reviewID + 1; - - $database->execute(" - INSERT INTO note_histories - (note_enable, note_id, review_id, image_id, user_id, user_ip, date, x1, y1, height, width, note) - VALUES - (?, ?, ?, ?, ?, ?, now(), ?, ?, ?, ?, ?)", - array($noteEnable, $noteID, $reviewID, $imageID, $userID, $_SERVER['REMOTE_ADDR'], $noteX1, $noteY1, $noteHeight, $noteWidth, $noteText)); - } - - - - /* - * HERE WE GET ALL HISTORIES. - */ - private function get_histories($event){ - $pageNumber = $event->get_arg(1); - if(is_null($pageNumber) || !is_numeric($pageNumber)) - $pageNumber = 0; - else if ($pageNumber <= 0) - $pageNumber = 0; - else - $pageNumber--; - - global $config; - - $histiriesPerPage = $config->get_int('notesHistoriesPerPage'); - - //ORDER BY IMAGE & DATE - global $database; - $histories = $database->get_all("SELECT h.note_id, h.review_id, h.image_id, h.date, h.note, u.name AS user_name ". - "FROM note_histories AS h ". - "INNER JOIN users AS u ". - "ON u.id = h.user_id ". - "ORDER BY date DESC LIMIT ?, ?", - array($pageNumber * $histiriesPerPage, $histiriesPerPage)); - - $totalPages = ceil($database->db->GetOne("SELECT COUNT(*) FROM note_histories") / $histiriesPerPage); - - $this->theme->display_histories($histories, $pageNumber + 1, $totalPages); - } - - - - /* - * HERE WE THE HISTORY FOR A SPECIFIC NOTE. - */ - private function get_history($event){ - $noteID = $event->get_arg(1); - $pageNumber = $event->get_arg(2); - if(is_null($pageNumber) || !is_numeric($pageNumber)) - $pageNumber = 0; - else if ($pageNumber <= 0) - $pageNumber = 0; - else - $pageNumber--; - - global $config; - - $histiriesPerPage = $config->get_int('notesHistoriesPerPage'); - - global $database; - $histories = $database->get_all("SELECT h.note_id, h.review_id, h.image_id, h.date, h.note, u.name AS user_name ". - "FROM note_histories AS h ". - "INNER JOIN users AS u ". - "ON u.id = h.user_id ". - "WHERE note_id = ? ". - "ORDER BY date DESC LIMIT ?, ?", - array($noteID, $pageNumber * $histiriesPerPage, $histiriesPerPage)); - - $totalPages = ceil($database->db->GetOne("SELECT COUNT(*) FROM note_histories WHERE note_id = ?", array($noteID)) / $histiriesPerPage); - - $this->theme->display_history($histories, $pageNumber + 1, $totalPages); - } - - - - /* - * HERE GO BACK IN HISTORY AND SET THE OLD NOTE. IF WAS REMOVED WE READD IT. - */ - private function revert_history($noteID, $reviewID){ - global $user, $database; - - $history = $database->get_row("SELECT * FROM note_histories WHERE note_id = ? AND review_id = ?",array($noteID, $reviewID)); - - $noteEnable = $history['note_enable']; - $noteID = $history['note_id']; - $imageID = $history['image_id']; - $userID = $user->id; - $noteX1 = $history['x1']; - $noteY1 = $history['y1']; - $noteHeight = $history['height']; - $noteWidth = $history['width']; - $noteText = $history['note']; - - $database->execute("UPDATE notes ". - "SET enable = ?, ". - "x1 = ?, ". - "y1 = ?, ". - "height = ?, ". - "width = ?,". - "note = ? ". - "WHERE image_id = ? AND id = ?", array(1, $noteX1, $noteY1, $noteHeight, $noteWidth, $noteText, $imageID, $noteID)); - - $this->add_history($noteEnable, $noteID, $imageID, $noteX1, $noteY1, $noteHeight, $noteWidth, $noteText); - - } -} -?> + + * License: GPLv2 + * Description: Annotate images + * Documentation: + */ + +class Notes extends SimpleExtension { + public function onInitExt($event) { + global $config, $database; + + // shortcut to latest + if ($config->get_int("ext_notes_version") < 1) { + $database->Execute("ALTER TABLE images ADD COLUMN notes INTEGER NOT NULL DEFAULT 0"); + $database->create_table("notes", " + id SCORE_AIPK, + enable INTEGER NOT NULL, + image_id INTEGER NOT NULL, + user_id INTEGER NOT NULL, + user_ip CHAR(15) NOT NULL, + date DATETIME NOT NULL, + x1 INTEGER NOT NULL, + y1 INTEGER NOT NULL, + height INTEGER NOT NULL, + width INTEGER NOT NULL, + note TEXT NOT NULL, + INDEX (image_id), + FOREIGN KEY (image_id) REFERENCES images(id) ON DELETE CASCADE + "); + + $database->create_table("note_request", " + id SCORE_AIPK, + image_id INTEGER NOT NULL, + user_id INTEGER NOT NULL, + date DATETIME NOT NULL, + INDEX (image_id), + FOREIGN KEY (image_id) REFERENCES images(id) ON DELETE CASCADE + "); + + $database->create_table("note_histories", " + id SCORE_AIPK, + note_enable INTEGER NOT NULL, + note_id INTEGER NOT NULL, + review_id INTEGER NOT NULL, + image_id INTEGER NOT NULL, + user_id INTEGER NOT NULL, + user_ip CHAR(15) NOT NULL, + date DATETIME NOT NULL, + x1 INTEGER NOT NULL, + y1 INTEGER NOT NULL, + height INTEGER NOT NULL, + width INTEGER NOT NULL, + note TEXT NOT NULL, + INDEX (image_id), + FOREIGN KEY (note_id) REFERENCES notes(id) ON DELETE CASCADE + "); + + $config->set_int("notesNotesPerPage", 20); + $config->set_int("notesRequestsPerPage", 20); + $config->set_int("notesHistoriesPerPage", 20); + + $config->set_int("ext_notes_version", 1); + log_info("notes", "extension installed"); + } + } + + + + public function onPageRequest($event) { + global $page, $user; + if($event->page_matches("note")) { + + switch($event->get_arg(0)) { + case "list": //index + { + $this->get_notes_list($event); // This should show images like post/list but i don't know how do that. + break; + } + case "requests": // The same as post/list but only for note_request table. + { + $this->get_notes_requests($event); // This should shouw images like post/list but i don't know how do that. + break; + } + case "search": + { + if(!$user->is_anonymous()) + $this->theme->search_notes_page($page); + break; + } + case "updated": //Thinking how biuld this function. + { + $this->get_histories($event); + break; + } + case "history": //Thinking how biuld this function. + { + $this->get_history($event); + break; + } + case "revert": + { + $noteID = $event->get_arg(1); + $reviewID = $event->get_arg(2); + if(!$user->is_anonymous()){ + $this->revert_history($noteID, $reviewID); + } + + $page->set_mode("redirect"); + $page->set_redirect(make_link("note/updated")); + break; + } + case "add_note": + { + if(!$user->is_anonymous()) + $this->add_new_note(); + + $page->set_mode("redirect"); + $page->set_redirect(make_link("post/view/".$_POST["image_id"])); + break; + } + case "add_request": + { + if(!$user->is_anonymous()) + $this->add_note_request(); + + $page->set_mode("redirect"); + $page->set_redirect(make_link("post/view/".$_POST["image_id"])); + break; + } + case "nuke_notes": + { + if($user->is_admin()) + $this->nuke_notes(); + + $page->set_mode("redirect"); + $page->set_redirect(make_link("post/view/".$_POST["image_id"])); + break; + } + case "nuke_requests": + { + if($user->is_admin()) + $this->nuke_requests(); + + $page->set_mode("redirect"); + $page->set_redirect(make_link("post/view/".$_POST["image_id"])); + break; + } + case "edit_note": + { + if (!$user->is_anonymous()) { + $this->update_note(); + $page->set_mode("redirect"); + $page->set_redirect(make_link("post/view/".$_POST["image_id"])); + } + break; + } + case "delete_note": + { + if ($user->is_admin()) { + $this->delete_note(); + $page->set_mode("redirect"); + $page->set_redirect(make_link("post/view/".$_POST["image_id"])); + } + break; + } + default: + { + $page->set_mode("redirect"); + $page->set_redirect(make_link("note/list")); + break; + } + } + } + } + + + /* + * HERE WE LOAD THE NOTES IN THE IMAGE + */ + public function onDisplayingImage($event) { + global $page, $user; + + //display form on image event + $notes = $this->get_notes($event->image->id); + $this->theme->display_note_system($page, $event->image->id, $notes, $user->is_admin()); + } + + + /* + * HERE WE ADD THE BUTTONS ON SIDEBAR + */ + public function onImageAdminBlockBuilding($event) { + global $user; + if(!$user->is_anonymous()) { + $event->add_part($this->theme->note_button($event->image->id)); + $event->add_part($this->theme->request_button($event->image->id)); + if($user->is_admin()) { + $event->add_part($this->theme->nuke_notes_button($event->image->id)); + $event->add_part($this->theme->nuke_requests_button($event->image->id)); + } + } + } + + + /* + * HERE WE ADD QUERYLETS TO ADD SEARCH SYSTEM + */ + public function onSearchTermParse($event) { + $matches = array(); + if(preg_match("/note=(.*)/i", $event->term, $matches)) { + $notes = int_escape($matches[1]); + $event->add_querylet(new Querylet("images.id IN (SELECT image_id FROM notes WHERE note = $notes)")); + } + else if(preg_match("/notes(<|>|<=|>=|=)(\d+)/", $event->term, $matches)) { + $cmp = $matches[1]; + $notes = $matches[2]; + $event->add_querylet(new Querylet("images.id IN (SELECT id FROM images WHERE notes $cmp $notes)")); + } + else if(preg_match("/notes_by=(.*)/i", $event->term, $matches)) { + global $database; + $user = User::by_name($matches[1]); + if(!is_null($user)) { + $user_id = $user->id; + } + else { + $user_id = -1; + } + + $event->add_querylet(new Querylet("images.id IN (SELECT image_id FROM notes WHERE user_id = $user_id)")); + } + else if(preg_match("/notes_by_userno=([0-9]+)/i", $event->term, $matches)) { + $user_id = int_escape($matches[1]); + $event->add_querylet(new Querylet("images.id IN (SELECT image_id FROM notes WHERE user_id = $user_id)")); + } + } + + + /* + * HERE WE GET ALL NOTES FOR DISPLAYED IMAGE + */ + private function get_notes($imageID) { + global $database; + + return $database->get_all( + "SELECT * ". + "FROM notes ". + "WHERE enable = ? AND image_id = ? ". + "ORDER BY date ASC" + , array('1', $imageID)); + } + + + /* + * HERE WE ADD A NOTE TO DATABASE + */ + private function add_new_note() { + global $database, $user; + + $imageID = int_escape($_POST["image_id"]); + $user_id = $user->id; + $noteX1 = int_escape($_POST["note_x1"]); + $noteY1 = int_escape($_POST["note_y1"]); + $noteHeight = int_escape($_POST["note_height"]); + $noteWidth = int_escape($_POST["note_width"]); + $noteText = mysql_real_escape_string(html_escape($_POST["note_text"])); + + $database->execute(" + INSERT INTO notes + (enable, image_id, user_id, user_ip, date, x1, y1, height, width, note) + VALUES + (?, ?, ?, ?, now(), ?, ?, ?, ?, ?)", + array(1, $imageID, $user_id, $_SERVER['REMOTE_ADDR'], $noteX1, $noteY1, $noteHeight, $noteWidth, $noteText)); + + $result = $database->get_row("SELECT LAST_INSERT_ID() AS noteID", array()); + $noteID = $result["noteID"]; + + log_info("notes", "Note added {$noteID} by {$user->name}"); + + $database->Execute("UPDATE images SET notes=(SELECT COUNT(*) FROM notes WHERE image_id=?) WHERE id=?", array($imageID, $imageID)); + + $this->add_history(1, $noteID, $imageID, $noteX1, $noteY1, $noteHeight, $noteWidth, $noteText); + } + + + + /* + * HERE WE ADD A REQUEST TO DATABASE + */ + private function add_note_request() { + global $database, $user; + + $image_id = int_escape($_POST["image_id"]); + $user_id = $user->id; + + $database->execute(" + INSERT INTO note_request + (image_id, user_id, date) + VALUES + (?, ?, now())", + array($image_id, $user_id)); + + $result = $database->get_row("SELECT LAST_INSERT_ID() AS requestID", array()); + + log_info("notes", "Note requested {$result["requestID"]} by {$user->name}"); + } + + + + /* + * HERE WE EDIT THE NOTE + */ + private function update_note() + { + $imageID = int_escape($_POST["image_id"]); + $noteID = int_escape($_POST["note_id"]); + $noteX1 = int_escape($_POST["note_x1"]); + $noteY1 = int_escape($_POST["note_y1"]); + $noteHeight = int_escape($_POST["note_height"]); + $noteWidth = int_escape($_POST["note_width"]); + $noteText = mysql_real_escape_string(html_escape($_POST["note_text"])); + + // validate parameters + if(is_null($imageID) || !is_numeric($imageID)) + return; + + if(is_null($noteID) || !is_numeric($noteID)) + return; + + if(is_null($noteX1) || !is_numeric($noteX1)) + return; + + if(is_null($noteY1) || !is_numeric($noteY1)) + return; + + if(is_null($noteHeight) || !is_numeric($noteHeight)) + return; + + if(is_null($noteWidth) || !is_numeric($noteWidth)) + return; + + if(is_null($noteText) || strlen($noteText) == 0) + return; + + global $database; + $database->execute("UPDATE notes ". + "SET x1 = ?, ". + "y1 = ?, ". + "height = ?, ". + "width = ?,". + "note = ? ". + "WHERE image_id = ? AND id = ?", array($noteX1, $noteY1, $noteHeight, $noteWidth, $noteText, $imageID, $noteID)); + + $this->add_history(1, $noteID, $imageID, $noteX1, $noteY1, $noteHeight, $noteWidth, $noteText); + } + + + + /* + * HERE WE DELETE THE NOTE + */ + private function delete_note() + { + $imageID = int_escape($_POST["image_id"]); + $noteID = int_escape($_POST["note_id"]); + + // validate parameters + if(is_null($imageID) || !is_numeric($imageID)) + return; + + if(is_null($noteID) || !is_numeric($noteID)) + return; + + global $database; + + $database->execute("UPDATE notes ". + "SET enable = ? ". + "WHERE image_id = ? AND id = ?", array(0, $imageID, $noteID)); + + log_info("notes", "Note deleted {$noteID} by {$user->name}"); + } + + + + /* + * HERE WE DELETE ALL NOTES FROM IMAGE + */ + private function nuke_notes() { + global $database; + $image_id = int_escape($_POST["image_id"]); + $database->execute("DELETE FROM notes WHERE image_id = ?", array($image_id)); + log_info("notes", "Notes deleted from {$image_id} by {$user->name}"); + } + + + + /* + * HERE WE DELETE ALL REQUESTS FOR IMAGE + */ + private function nuke_requests() { + global $database; + $image_id = int_escape($_POST["image_id"]); + + $database->execute("DELETE FROM note_request WHERE image_id = ?", array($image_id)); + + log_info("notes", "Requests deleted from {$image_id} by {$user->name}"); + } + + + + /* + * HERE WE ALL IMAGES THAT HAVE NOTES + */ + private function get_notes_list($event) { + $pageNumber = $event->get_arg(1); + if(is_null($pageNumber) || !is_numeric($pageNumber)) + $pageNumber = 0; + else if ($pageNumber <= 0) + $pageNumber = 0; + else + $pageNumber--; + + global $config; + + $notesPerPage = $config->get_int('notesNotesPerPage'); + + + //$result = $database->get_all("SELECT * FROM pool_images WHERE pool_id=?", array($poolID)); + global $database; + $get_notes = " + SELECT DISTINCT image_id ". + "FROM notes ". + "WHERE enable = ? ". + "ORDER BY date DESC LIMIT ?, ?"; + + $result = $database->Execute($get_notes, array(1, $pageNumber * $notesPerPage, $notesPerPage)); + + $totalPages = ceil($database->db->GetOne("SELECT COUNT(*) FROM notes") / $notesPerPage); + + $images = array(); + while(!$result->EOF) { + $image = Image::by_id($result->fields["image_id"]); + $images[] = array($image); + $result->MoveNext(); + } + + $this->theme->display_note_list($images, $pageNumber + 1, $totalPages); + } + + + + /* + * HERE WE GET ALL NOTE REQUESTS + */ + private function get_notes_requests($event) { + $pageNumber = $event->get_arg(1); + if(is_null($pageNumber) || !is_numeric($pageNumber)) + $pageNumber = 0; + else if ($pageNumber <= 0) + $pageNumber = 0; + else + $pageNumber--; + + global $config; + + $requestsPerPage = $config->get_int('notesRequestsPerPage'); + + + //$result = $database->get_all("SELECT * FROM pool_images WHERE pool_id=?", array($poolID)); + global $database; + $get_requests = " + SELECT DISTINCT image_id ". + "FROM note_request ". + "ORDER BY date DESC LIMIT ?, ?"; + + $result = $database->Execute($get_requests, array($pageNumber * $requestsPerPage, $requestsPerPage)); + + $totalPages = ceil($database->db->GetOne("SELECT COUNT(*) FROM note_request") / $requestsPerPage); + + $images = array(); + while(!$result->EOF) { + $image = Image::by_id($result->fields["image_id"]); + $images[] = array($image); + $result->MoveNext(); + } + + $this->theme->display_note_requests($images, $pageNumber + 1, $totalPages); + } + + + + /* + * HERE WE ADD HISTORY TO TRACK THE CHANGES OF THE NOTES FOR THE IMAGES. + */ + private function add_history($noteEnable, $noteID, $imageID, $noteX1, $noteY1, $noteHeight, $noteWidth, $noteText){ + global $user, $database; + + $userID = $user->id; + + $reviewID = $database->db->GetOne("SELECT COUNT(*) FROM note_histories WHERE note_id = ?", array($noteID)); + $reviewID = $reviewID + 1; + + $database->execute(" + INSERT INTO note_histories + (note_enable, note_id, review_id, image_id, user_id, user_ip, date, x1, y1, height, width, note) + VALUES + (?, ?, ?, ?, ?, ?, now(), ?, ?, ?, ?, ?)", + array($noteEnable, $noteID, $reviewID, $imageID, $userID, $_SERVER['REMOTE_ADDR'], $noteX1, $noteY1, $noteHeight, $noteWidth, $noteText)); + } + + + + /* + * HERE WE GET ALL HISTORIES. + */ + private function get_histories($event){ + $pageNumber = $event->get_arg(1); + if(is_null($pageNumber) || !is_numeric($pageNumber)) + $pageNumber = 0; + else if ($pageNumber <= 0) + $pageNumber = 0; + else + $pageNumber--; + + global $config; + + $histiriesPerPage = $config->get_int('notesHistoriesPerPage'); + + //ORDER BY IMAGE & DATE + global $database; + $histories = $database->get_all("SELECT h.note_id, h.review_id, h.image_id, h.date, h.note, u.name AS user_name ". + "FROM note_histories AS h ". + "INNER JOIN users AS u ". + "ON u.id = h.user_id ". + "ORDER BY date DESC LIMIT ?, ?", + array($pageNumber * $histiriesPerPage, $histiriesPerPage)); + + $totalPages = ceil($database->db->GetOne("SELECT COUNT(*) FROM note_histories") / $histiriesPerPage); + + $this->theme->display_histories($histories, $pageNumber + 1, $totalPages); + } + + + + /* + * HERE WE THE HISTORY FOR A SPECIFIC NOTE. + */ + private function get_history($event){ + $noteID = $event->get_arg(1); + $pageNumber = $event->get_arg(2); + if(is_null($pageNumber) || !is_numeric($pageNumber)) + $pageNumber = 0; + else if ($pageNumber <= 0) + $pageNumber = 0; + else + $pageNumber--; + + global $config; + + $histiriesPerPage = $config->get_int('notesHistoriesPerPage'); + + global $database; + $histories = $database->get_all("SELECT h.note_id, h.review_id, h.image_id, h.date, h.note, u.name AS user_name ". + "FROM note_histories AS h ". + "INNER JOIN users AS u ". + "ON u.id = h.user_id ". + "WHERE note_id = ? ". + "ORDER BY date DESC LIMIT ?, ?", + array($noteID, $pageNumber * $histiriesPerPage, $histiriesPerPage)); + + $totalPages = ceil($database->db->GetOne("SELECT COUNT(*) FROM note_histories WHERE note_id = ?", array($noteID)) / $histiriesPerPage); + + $this->theme->display_history($histories, $pageNumber + 1, $totalPages); + } + + + + /* + * HERE GO BACK IN HISTORY AND SET THE OLD NOTE. IF WAS REMOVED WE READD IT. + */ + private function revert_history($noteID, $reviewID){ + global $user, $database; + + $history = $database->get_row("SELECT * FROM note_histories WHERE note_id = ? AND review_id = ?",array($noteID, $reviewID)); + + $noteEnable = $history['note_enable']; + $noteID = $history['note_id']; + $imageID = $history['image_id']; + $userID = $user->id; + $noteX1 = $history['x1']; + $noteY1 = $history['y1']; + $noteHeight = $history['height']; + $noteWidth = $history['width']; + $noteText = $history['note']; + + $database->execute("UPDATE notes ". + "SET enable = ?, ". + "x1 = ?, ". + "y1 = ?, ". + "height = ?, ". + "width = ?,". + "note = ? ". + "WHERE image_id = ? AND id = ?", array(1, $noteX1, $noteY1, $noteHeight, $noteWidth, $noteText, $imageID, $noteID)); + + $this->add_history($noteEnable, $noteID, $imageID, $noteX1, $noteY1, $noteHeight, $noteWidth, $noteText); + + } +} +?> diff --git a/contrib/pools/main.php b/contrib/pools/main.php index fb18bcb7..1719fcaa 100644 --- a/contrib/pools/main.php +++ b/contrib/pools/main.php @@ -1,732 +1,741 @@ - - * License: GPLv2 - * Description: Simple pools extension - * Documentation: - */ - -class PoolCreationException extends SCoreException { -} - -class Pools extends SimpleExtension { - public function onInitExt($event) { - global $config, $database; - - if ($config->get_int("ext_pools_version") < 1) { - $database->create_table("pools", " - id SCORE_AIPK, - user_id INTEGER NOT NULL, - public SCORE_BOOL NOT NULL DEFAULT SCORE_BOOL_N, - title VARCHAR(255) NOT NULL, - description TEXT, - date DATETIME NOT NULL, - posts INTEGER NOT NULL DEFAULT 0, - INDEX (id) - "); - $database->create_table("pool_images", " - pool_id INTEGER NOT NULL, - image_id INTEGER NOT NULL, - image_order INTEGER NOT NULL DEFAULT 0 - "); - $database->create_table("pool_history", " - id SCORE_AIPK, - pool_id INTEGER NOT NULL, - user_id INTEGER NOT NULL, - action INTEGER NOT NULL, - images TEXT, - count INTEGER NOT NULL DEFAULT 0, - date DATETIME NOT NULL, - INDEX (id) - "); - - $config->set_int("ext_pools_version", 1); - - $config->set_int("poolsMaxImportResults", 1000); - $config->set_int("poolsImagesPerPage", 20); - $config->set_int("poolsListsPerPage", 20); - $config->set_int("poolsUpdatedPerPage", 20); - $config->set_bool("poolsInfoOnViewImage", "N"); - - log_info("pools", "extension installed"); - } - } - - public function onSetupBuilding(SetupBuildingEvent $event) { - $sb = new SetupBlock("Pools"); - $sb->add_int_option("poolsMaxImportResults", "Max results on import: "); - $sb->add_int_option("poolsImagesPerPage", "
Images per page: "); - $sb->add_int_option("poolsListsPerPage", "
Index list items per page: "); - $sb->add_int_option("poolsUpdatedPerPage", "
Updated list items per page: "); - $sb->add_bool_option("poolsInfoOnViewImage", "
Show pool info on image: "); - $event->panel->add_block($sb); - } - - public function onPageRequest($event) { - global $config, $page, $user; - - if($event->page_matches("pool")) { - switch($event->get_arg(0)) { - case "list": //index - { - $this->list_pools($page, $event); - break; - } - case "new": // Show form - { - if(!$user->is_anonymous()) { - $this->theme->new_pool_composer($page); - } else { - $errMessage = "You must be registered and logged in to create a new pool."; - $this->theme->display_error($errMessage); - } - break; - } - case "create": // ADD _POST - { - try { - if($user->is_anonymous()) { - throw new PoolCreationException("You must be registered and logged in to add a image."); - } - - $newPoolID = $this->add_pool(); - $page->set_mode("redirect"); - $page->set_redirect(make_link("pool/view/".$newPoolID)); - } - catch(PoolCreationException $ex) { - $this->theme->display_error($ex->getMessage()); - } - break; - } - case "view": - { - $poolID = int_escape($event->get_arg(1)); - $this->get_posts($event, $poolID); - break; - } - case "updated": - { - $this->get_history($event); - break; - } - case "revert": - { - if(!$user->is_anonymous()) { - $historyID = int_escape($event->get_arg(1)); - - $this->revert_history($historyID); - - $page->set_mode("redirect"); - $page->set_redirect(make_link("pool/updated")); - } - break; - } - case "edit": - { - $poolID = int_escape($event->get_arg(1)); - $pools = $this->get_pool($poolID); - - foreach($pools as $pool) { - //if the pool is public and user is logged OR if the user is admin OR the user is the owner - if(($pool['public'] == "Y" && !$user->is_anonymous()) || $user->is_admin() || $user->id == $pool['user_id']) { - $this->theme->edit_pool($page, $this->get_pool($poolID), $this->edit_posts($poolID)); - } else { - $page->set_mode("redirect"); - $page->set_redirect(make_link("pool/view/".$poolID)); - } - } - break; - } - case "edit_pool": - { - $poolID = int_escape($_POST["pool_id"]); - $page->set_mode("redirect"); - $page->set_redirect(make_link("pool/edit/".$poolID)); - break; - } - case "order": - { - $poolID = int_escape($event->get_arg(1)); - $pools = $this->get_pool($poolID); - - foreach($pools as $pool) { - //if the pool is public and user is logged OR if the user is admin - if(($pool['public'] == "Y" && !$user->is_anonymous()) || $user->is_admin() || $user->id == $pool['user_id']) { - $this->theme->edit_order($page, $this->get_pool($poolID), $this->edit_order($poolID)); - } else { - $page->set_mode("redirect"); - $page->set_redirect(make_link("pool/view/".$poolID)); - } - } - break; - } - case "edit_order": - { - $poolID = int_escape($_POST["pool_id"]); - $page->set_mode("redirect"); - $page->set_redirect(make_link("pool/order/".$poolID)); - break; - } - case "import": - { - $pool_id = int_escape($_POST["pool_id"]); - $pool = $this->get_single_pool($pool_id); - - if(($pool['public'] == "Y" && !$user->is_anonymous()) || $user->is_admin() || $user->id == $pool['user_id']) { - $this->import_posts(); - } else { - $this->theme->display_error("Permssion denied."); - } - break; - } - case "add_posts": - { - $pool_id = int_escape($_POST["pool_id"]); - $pool = $this->get_single_pool($pool_id); - - if(($pool['public'] == "Y" && !$user->is_anonymous()) || $user->is_admin() || $user->id == $pool['user_id']) { - $this->add_posts(); - $page->set_mode("redirect"); - $page->set_redirect(make_link("pool/view/".$pool_id)); - } else { - $this->theme->display_error("Permssion denied."); - } - break; - } - case "order_posts": - { - $pool_id = int_escape($_POST["pool_id"]); - $pool = $this->get_single_pool($pool_id); - - if(($pool['public'] == "Y" && !$user->is_anonymous()) || $user->is_admin() || $user->id == $pool['user_id']) { - $this->order_posts(); - $page->set_mode("redirect"); - $page->set_redirect(make_link("pool/view/".$pool_id)); - } else { - $this->theme->display_error("Permssion denied."); - } - break; - } - case "remove_posts": - { - $pool_id = int_escape($_POST["pool_id"]); - $pool = $this->get_single_pool($pool_id); - - if(($pool['public'] == "Y" && !$user->is_anonymous()) || $user->is_admin() || $user->id == $pool['user_id']) { - $this->remove_posts(); - $page->set_mode("redirect"); - $page->set_redirect(make_link("pool/view/".$pool_id )); - } else { - $this->theme->display_error("Permssion denied."); - } - - break; - } - case "nuke": - { - $pool_id = int_escape($event->get_arg(1)); - $pool = $this->get_single_pool($pool_id); - - // only admins and owners may do this - if($user->is_admin() || $user->id == $pool['user_id']) { - $this->nuke_pool($pool_id); - $page->set_mode("redirect"); - $page->set_redirect(make_link("pool/list")); - } else { - $this->theme->display_error("Permssion denied."); - } - break; - } - case "nuke_pool": - { - $poolID = int_escape($_POST["pool_id"]); - $page->set_mode("redirect"); - $page->set_redirect(make_link("pool/nuke/".$poolID)); - break; - } - default: - { - $page->set_mode("redirect"); - $page->set_redirect(make_link("pool/list")); - break; - } - } - } - } - - - - /* - * HERE WE GET THE POOLS WHERE THE IMAGE APPEARS WHEN THE IMAGE IS DISPLAYED - */ - public function onDisplayingImage($event) { - global $page; - $imageID = $event->image->id; - $poolsIDs = $this->get_pool_id($imageID); - - $linksPools = ""; - foreach ($poolsIDs as $poolID) { - $pools = $this->get_pool($poolID['pool_id']); - foreach ($pools as $pool) { - $linksPools .= "".$pool['title'].", "; - } - } - $linksPools = substr($linksPools, 0, -2); - $linksPools = $linksPools." "; - $this->theme->pool_info($linksPools); - } - - - - /* - * HERE WE GET THE LIST OF POOLS - */ - private function list_pools(Page $page, $event) { - global $config, $database; - - $pageNumber = int_escape($event->get_arg(1)); - if(is_null($pageNumber) || !is_numeric($pageNumber)) - $pageNumber = 0; - else if ($pageNumber <= 0) - $pageNumber = 0; - else - $pageNumber--; - - $poolsPerPage = $config->get_int("poolsListsPerPage"); - - $pools = $database->get_all( - "SELECT p.id, p.user_id, p.public, p.title, p.description, p.posts, u.name as user_name ". - "FROM pools AS p ". - "INNER JOIN users AS u ". - "ON p.user_id = u.id ". - "ORDER BY p.date DESC ". - "LIMIT ?, ?" - , array($pageNumber * $poolsPerPage, $poolsPerPage) - ); - - $totalPages = ceil($database->db->GetOne("SELECT COUNT(*) FROM pools") / $poolsPerPage); - - $this->theme->list_pools($page, $pools, $pageNumber + 1, $totalPages); - } - - - - /* - * HERE WE CREATE A NEW POOL - */ - private function add_pool() { - global $user, $database; - - $public = html_escape($_POST["public"]); - $title = html_escape(trim($_POST["title"])); - $description = html_escape(trim($_POST["description"])); - - if($public == "") { - $public = "N"; - } - - if(empty($title)) throw new PoolCreationException("Pool must have a title"); - - $database->execute(" - INSERT INTO pools - (user_id, public, title, description, date) - VALUES - (?, ?, ?, ?, now())", - array($user->id, $public, $title, $description)); - - $result = $database->get_row("SELECT LAST_INSERT_ID() AS poolID"); - - log_info("pools", "Pool {$result["poolID"]} created by {$user->name}"); - - return $result["poolID"]; - } - - private function get_pool($poolID) { - global $database; - $poolID = int_escape($poolID); - return $database->get_all("SELECT * FROM pools WHERE id=?", array($poolID)); - } - - private function get_single_pool($poolID) { - global $database; - $poolID = int_escape($poolID); - return $database->get_row("SELECT * FROM pools WHERE id=?", array($poolID)); - } - - /* - * HERE WE GET THE ID OF THE POOL FROM AN IMAGE - */ - private function get_pool_id($imageID) { - global $database; - $imageID = int_escape($imageID); - return $database->get_all("SELECT pool_id FROM pool_images WHERE image_id=?", array($imageID)); - } - - - - /* - * HERE WE GET THE IMAGES FROM THE TAG ON IMPORT - */ - private function import_posts() { - global $page, $config, $database; - - $pool_id = int_escape($_POST["pool_id"]); - $pool_tag = html_escape($_POST["pool_tag"]);; - - $poolsMaxResults = $config->get_int("poolsMaxImportResults", 1000); - - $images = Image::find_images(0, $poolsMaxResults, Tag::explode($pool_tag)); - $this->theme->pool_result($page, $images, $pool_id); - } - - - - /* - * HERE WE ADD CHECKED IMAGES FROM POOL AND UPDATE THE HISTORY - */ - private function add_posts() { //ADD CHECKED POSTS - global $database; - - $poolID = int_escape($_POST['pool_id']); - $images = ""; - - foreach ($_POST['check'] as $imageID) { - if(!$this->check_post($poolID, $imageID)) { - $database->execute(" - INSERT INTO pool_images - (pool_id, image_id) - VALUES - (?, ?)", - array($poolID, $imageID)); - - $images .= " ".$imageID; - } - } - - if(!strlen($images) == 0) { - $count = $database->db->GetOne("SELECT COUNT(*) FROM pool_images WHERE pool_id=?", array($poolID)); - $this->add_history($poolID, 1, $images, $count); - } - - $database->Execute("UPDATE pools SET posts=(SELECT COUNT(*) FROM pool_images WHERE pool_id=?) WHERE id=?", array($poolID, $poolID)); - return $poolID; - } - - private function order_posts() { //ORDER POSTS - global $database; - - $poolID = int_escape($_POST['pool_id']); - - foreach ($_POST['imgs'] as $data) { - list ($imageORDER, $imageID) = $data; - - $imageID = int_escape($imageID); - $database->Execute("UPDATE pool_images SET image_order=? WHERE pool_id=? AND image_id=?", array($imageORDER, $poolID, $imageID)); - } - - return $poolID; - } - - - - /* - * HERE WE REMOVE CHECKED IMAGES FROM POOL AND UPDATE THE HISTORY - */ - private function remove_posts() { - global $database; - - $poolID = int_escape($_POST['pool_id']); - $images = ""; - - foreach ($_POST['check'] as $imageID) { - $database->execute("DELETE FROM pool_images WHERE pool_id=? AND image_id=?", array($poolID, $imageID)); - $images .= " ".$imageID; - } - - $count = $database->db->GetOne("SELECT COUNT(*) FROM pool_images WHERE pool_id=?", array($poolID)); - $this->add_history($poolID, 0, $images, $count); - return $poolID; - } - - - - /* - * HERE WE CHECK IF THE POST IS ALREADY ON POOL - * USED IN add_posts() - */ - private function check_post($poolID, $imageID) { - global $database; - $result = $database->db->GetOne("SELECT COUNT(*) FROM pool_images WHERE pool_id=? AND image_id=?", array($poolID, $imageID)); - if($result == 0) { - return FALSE; - } else { - return TRUE; - } - } - - - - /* - * HERE WE GET ALL IMAGES FOR THE POOL - */ - private function get_posts($event, $poolID) { - global $config, $user, $database; - - $pageNumber = int_esscape($event->get_arg(2)); - if(is_null($pageNumber) || !is_numeric($pageNumber)) - $pageNumber = 0; - else if ($pageNumber <= 0) - $pageNumber = 0; - else - $pageNumber--; - - $poolID = int_escape($poolID); - - $imagesPerPage = $config->get_int("poolsImagesPerPage"); - - // WE CHECK IF THE EXTENSION RATING IS INSTALLED, WICH VERSION AND IF IT WORKS TO SHOW/HIDE SAFE, QUESTIONABLE, EXPLICIT AND UNRATED IMAGES FROM USER - if(class_exists("Ratings") && $config->get_int("ext_ratings2_version") < 3) { - $result = $database->get_all("SELECT image_id ". - "FROM pool_images ". - "WHERE pool_id=? ". - "ORDER BY image_order ASC ". - "LIMIT ?, ?" - , array($poolID, $pageNumber * $imagesPerPage, $imagesPerPage)); - $totalPages = ceil($database->db->GetOne("SELECT COUNT(*) FROM pool_images WHERE pool_id=?",array($poolID)) / $imagesPerPage); - } - - if(class_exists("Ratings") && $config->get_int("ext_ratings2_version") >= 3) { - $rating = Ratings::privs_to_sql(Ratings::get_user_privs($user)); - - $result = $database->get_all("SELECT p.image_id - FROM pool_images AS p - INNER JOIN images AS i - ON i.id = p.image_id - WHERE p.pool_id = ? - AND i.rating IN ($rating) - ORDER BY p.image_order ASC - LIMIT ?, ?", - array($poolID, $pageNumber * $imagesPerPage, $imagesPerPage)); - - $totalPages = ceil($database->db->GetOne("SELECT COUNT(*) ". - "FROM pool_images AS p ". - "INNER JOIN images AS i ". - "ON i.id = p.image_id ". - "WHERE pool_id=? ". - "AND i.rating IN ($rating) " - ,array($poolID)) / $imagesPerPage); - } - - $images = array(); - foreach ($result as $singleResult) { - $image = Image::by_id($singleResult["image_id"]); - $images[] = array($image); - } - - $pool = $this->get_pool($poolID); - $this->theme->view_pool($pool, $images, $pageNumber + 1, $totalPages); - } - - - /* - * WE GET THE ORDER OF THE IMAGES - */ - private function edit_posts($poolID) { - global $database; - $poolID = int_escape($poolID); - - $result = $database->Execute("SELECT image_id FROM pool_images WHERE pool_id=? ORDER BY image_order ASC", array($poolID)); - - $images = array(); - while(!$result->EOF) { - $image = Image::by_id($result->fields["image_id"]); - $images[] = array($image); - $result->MoveNext(); - } - - return $images; - } - - - - /* - * WE GET THE ORDER OF THE IMAGES BUT HERE WE SEND KEYS ADDED IN ARRAY TO GET THE ORDER IN THE INPUT VALUE - */ - private function edit_order($poolID) { - global $database; - $poolID = int_escape($poolID); - - $result = $database->Execute("SELECT image_id FROM pool_images WHERE pool_id=? ORDER BY image_order ASC", array($poolID)); - - $images = array(); - while(!$result->EOF) { - $image = $database->get_row("SELECT * FROM images AS i ". - "INNER JOIN pool_images AS p ". - "ON i.id = p.image_id ". - "WHERE pool_id=? AND i.id=?" - , array($poolID, $result->fields["image_id"])); - $image = ($image ? new Image($image) : null); - $images[] = array($image); - $result->MoveNext(); - } - // Original code - // - // $images = array(); - // while(!$result->EOF) { - // $image = Image::by_id($result->fields["image_id"]); - // $images[] = array($image); - // $result->MoveNext(); - // } - return $images; - } - - - - /* - * HERE WE NUKE ENTIRE POOL. WE REMOVE POOLS AND POSTS FROM REMOVED POOL AND HISTORIES ENTRIES FROM REMOVED POOL - */ - private function nuke_pool($poolID) { - global $user, $database; - - if($user->is_admin()) { - $database->execute("DELETE FROM pool_history WHERE pool_id = ?", array($poolID)); - $database->execute("DELETE FROM pool_images WHERE pool_id = ?", array($poolID)); - $database->execute("DELETE FROM pools WHERE id = ?", array($poolID)); - } elseif(!$user->is_anonymous()) { - // WE CHECK IF THE USER IS THE OWNER OF THE POOL IF NOT HE CAN'T DO ANYTHING - $database->execute("DELETE FROM pool_history WHERE pool_id = ?", array($poolID)); - $database->execute("DELETE FROM pool_images WHERE pool_id = ?", array($poolID)); - $database->execute("DELETE FROM pools WHERE id = ? AND user_id = ?", array($poolID, $user->id)); - } - } - - - - /* - * HERE WE ADD A HISTORY ENTRY - * FOR $action 1 (one) MEANS ADDED, 0 (zero) MEANS REMOVED - */ - private function add_history($poolID, $action, $images, $count) { - global $user, $database; - $user_id = $user->id; - $database->execute(" - INSERT INTO pool_history - (pool_id, user_id, action, images, count, date) - VALUES - (?, ?, ?, ?, ?, now())", - array($poolID, $user_id, $action, $images, $count)); - - } - - - - /* - * HERE WE GET THE HISTORY LIST - */ - private function get_history($event) { - global $config, $database; - - $pageNumber = $event->get_arg(1); - if(is_null($pageNumber) || !is_numeric($pageNumber)) - $pageNumber = 0; - else if ($pageNumber <= 0) - $pageNumber = 0; - else - $pageNumber--; - - - $historiesPerPage = $config->get_int("poolsUpdatedPerPage"); - - $history = $database->get_all( - "SELECT h.id, h.pool_id, h.user_id, h.action, h.images, h.count, h.date, u.name as user_name, p.title as title ". - "FROM pool_history AS h ". - "INNER JOIN pools AS p ". - "ON p.id = h.pool_id ". - "INNER JOIN users AS u ". - "ON h.user_id = u.id ". - "ORDER BY h.date DESC ". - "LIMIT ?, ?" - , array($pageNumber * $historiesPerPage, $historiesPerPage)); - - $totalPages = ceil($database->db->GetOne("SELECT COUNT(*) FROM pool_history") / $historiesPerPage); - - $this->theme->show_history($history, $pageNumber + 1, $totalPages); - } - - - - /* - * HERE GO BACK IN HISTORY AND ADD OR REMOVE POSTS TO POOL - */ - private function revert_history($historyID) { - global $database; - $status = $database->get_all("SELECT * FROM pool_history WHERE id=?", array($historyID)); - - foreach ($status as $entry) { - $images = trim($entry['images']); - $images = explode(" ", $images); - $poolID = $entry['pool_id']; - $imageArray = ""; - - if ($entry['action'] == 0) { - // READD ENTRIES - foreach ($images as $image) { - $imageID = $image; - $this->add_post($poolID, $imageID); - - $imageArray .= " ".$imageID; - $newAction = 1; - } - } elseif ($entry['action'] == 1) { - // DELETE ENTRIES - foreach ($images as $image) { - $imageID = $image; - $this->delete_post($poolID, $imageID); - - $imageArray .= " ".$imageID; - $newAction = 0; - } - } - - $count = $database->db->GetOne("SELECT COUNT(*) FROM pool_images WHERE pool_id=?", array($poolID)); - $this->add_history($poolID, $newAction, $imageArray, $count); - } - } - - - - /* - * HERE WE ADD A SIMPLE POST FROM POOL - * USED WITH FOREACH IN revert_history() - */ - private function add_post($poolID, $imageID) { - global $database; - - if(!$this->check_post($poolID, $imageID)) { - $database->execute(" - INSERT INTO pool_images - (pool_id, image_id) - VALUES - (?, ?)", - array($poolID, $imageID)); - } - - $database->execute("UPDATE pools SET posts=(SELECT COUNT(*) FROM pool_images WHERE pool_id=?) WHERE id=?", array($poolID, $poolID)); - } - - - - /* - * HERE WE REMOVE A SIMPLE POST FROM POOL - * USED WITH FOREACH IN revert_history() - */ - private function delete_post($poolID, $imageID) { - global $database; - - $database->execute("DELETE FROM pool_images WHERE pool_id = ? AND image_id = ?", array($poolID, $imageID)); - $database->execute("UPDATE pools SET posts=(SELECT COUNT(*) FROM pool_images WHERE pool_id=?) WHERE id=?", array($poolID, $poolID)); - } - -} -?> + + * License: GPLv2 + * Description: Simple pools extension + * Documentation: + */ + +class Pools extends SimpleExtension { + + public function onInitExt($event) { + global $config, $database; + + if ($config->get_int("ext_pools_version") < 1){ + $database->create_table("pools", " + id SCORE_AIPK, + user_id INTEGER NOT NULL, + public SCORE_BOOL NOT NULL DEFAULT SCORE_BOOL_N, + title VARCHAR(255) NOT NULL, + description TEXT, + date DATETIME NOT NULL, + posts INTEGER NOT NULL DEFAULT 0, + INDEX (id) + "); + $database->create_table("pool_images", " + pool_id INTEGER NOT NULL, + image_id INTEGER NOT NULL, + image_order INTEGER NOT NULL DEFAULT 0 + "); + $database->create_table("pool_history", " + id SCORE_AIPK, + pool_id INTEGER NOT NULL, + user_id INTEGER NOT NULL, + action INTEGER NOT NULL, + images TEXT, + count INTEGER NOT NULL DEFAULT 0, + date DATETIME NOT NULL, + INDEX (id) + "); + + $config->set_int("ext_pools_version", 1); + + $config->set_int("poolsMaxImportResults", 1000); + $config->set_int("poolsImagesPerPage", 20); + $config->set_int("poolsListsPerPage", 20); + $config->set_int("poolsUpdatedPerPage", 20); + $config->set_bool("poolsInfoOnViewImage", "N"); + + log_info("pools", "extension installed"); + } + } + + public function onSetupBuilding(SetupBuildingEvent $event) { + $sb = new SetupBlock("Pools"); + $sb->add_int_option("poolsMaxImportResults", "Max results on import: "); + $sb->add_int_option("poolsImagesPerPage", "
Images per page: "); + $sb->add_int_option("poolsListsPerPage", "
Index list items per page: "); + $sb->add_int_option("poolsUpdatedPerPage", "
Updated list items per page: "); + $sb->add_bool_option("poolsInfoOnViewImage", "
Show pool info on image: "); + $event->panel->add_block($sb); + } + + public function onPageRequest($event) { + global $config, $page, $user; + + if($event->page_matches("pool")) { + switch($event->get_arg(0)) { + case "list": //index + { + $this->list_pools($page, $event); + break; + } + case "new": // Show form + { + if(!$user->is_anonymous()){ + $this->theme->new_pool_composer($page); + } else { + $errMessage = "You must be registered and logged in to create a new pool."; + $this->theme->display_error($errMessage); + } + break; + } + case "create": // ADD _POST + { + if(!$user->is_anonymous()){ + $newPoolID = $this->add_pool(); + $page->set_mode("redirect"); + $page->set_redirect(make_link("pool/view/".$newPoolID."")); + } else { + $this->theme->display_error("You must be registered and logged in to add a image."); + } + break; + } + case "view": + { + $poolID = $event->get_arg(1); + $this->get_posts($event, $poolID); + break; + } + case "updated": + { + $this->get_history($event); + break; + } + case "revert": + { + if(!$user->is_anonymous()) { + $historyID = $event->get_arg(1); + + $this->revert_history($historyID); + + $page->set_mode("redirect"); + $page->set_redirect(make_link("pool/updated")); + } + break; + } + case "edit": + { + $poolID = $event->get_arg(1); + $pools = $this->get_pool($poolID); + + foreach($pools as $pool) { + //if the pool is public and user is logged OR if the user is admin OR the user is the owner + if(($pool['public'] == "Y" && !$user->is_anonymous()) || $user->is_admin() || $user->id == $pool['user_id']) { + $this->theme->edit_pool($page, $this->get_pool($poolID), $this->edit_posts($poolID)); + } else { + $page->set_mode("redirect"); + $page->set_redirect(make_link("pool/view/".$poolID."")); + } + } + break; + } + case "edit_pool": + { + $poolID = int_escape($_POST["pool_id"]); + $page->set_mode("redirect"); + $page->set_redirect(make_link("pool/edit/".$poolID."")); + break; + } + case "order": + { + $poolID = $event->get_arg(1); + $pools = $this->get_pool($poolID); + + foreach($pools as $pool) { + //if the pool is public and user is logged OR if the user is admin + if(($pool['public'] == "Y" && !$user->is_anonymous()) || $user->is_admin() || $user->id == $pool['user_id']) { + $this->theme->edit_order($page, $this->get_pool($poolID), $this->edit_order($poolID)); + } else { + $page->set_mode("redirect"); + $page->set_redirect(make_link("pool/view/".$poolID."")); + } + } + break; + } + case "edit_order": + { + $poolID = int_escape($_POST["pool_id"]); + $page->set_mode("redirect"); + $page->set_redirect(make_link("pool/order/".$poolID."")); + break; + } + case "import": + { + $pool_id = int_escape($_POST["pool_id"]); + $pool = $this->get_single_pool($pool_id); + + if(($pool['public'] == "Y" && !$user->is_anonymous()) || $user->is_admin() || $user->id == $pool['user_id']) { + $this->import_posts(); + } else { + $this->theme->display_error("Permssion denied."); + } + break; + } + case "add_posts": + { + $pool_id = int_escape($_POST["pool_id"]); + $pool = $this->get_single_pool($pool_id); + + if(($pool['public'] == "Y" && !$user->is_anonymous()) || $user->is_admin() || $user->id == $pool['user_id']) { + $this->add_posts(); + $page->set_mode("redirect"); + $page->set_redirect(make_link("pool/view/".$pool_id)); + } else { + $this->theme->display_error("Permssion denied."); + } + break; + } + case "order_posts": + { + $pool_id = int_escape($_POST["pool_id"]); + $pool = $this->get_single_pool($pool_id); + + if(($pool['public'] == "Y" && !$user->is_anonymous()) || $user->is_admin() || $user->id == $pool['user_id']) { + $this->order_posts(); + $page->set_mode("redirect"); + $page->set_redirect(make_link("pool/view/".$pool_id)); + } else { + $this->theme->display_error("Permssion denied."); + } + break; + } + case "remove_posts": + { + $pool_id = int_escape($_POST["pool_id"]); + $pool = $this->get_single_pool($pool_id); + + if(($pool['public'] == "Y" && !$user->is_anonymous()) || $user->is_admin() || $user->id == $pool['user_id']) { + $this->remove_posts(); + $page->set_mode("redirect"); + $page->set_redirect(make_link("pool/view/".$pool_id )); + } else { + $this->theme->display_error("Permssion denied."); + } + + break; + } + case "nuke": + { + $pool_id = $event->get_arg(1); + $pool = $this->get_single_pool($pool_id); + + // only admins and owners may do this + if($user->is_admin() || $user->id == $pool['user_id']) { + $this->nuke_pool($pool_id); + $page->set_mode("redirect"); + $page->set_redirect(make_link("pool/list")); + } else { + $this->theme->display_error("Permssion denied."); + } + break; + } + case "nuke_pool": + { + $poolID = int_escape($_POST["pool_id"]); + $page->set_mode("redirect"); + $page->set_redirect(make_link("pool/nuke/".$poolID)); + break; + } + default: + { + $page->set_mode("redirect"); + $page->set_redirect(make_link("pool/list")); + break; + } + } + } + } + + + + /* + * HERE WE GET THE POOLS WHERE THE IMAGE APPEARS WHEN THE IMAGE IS DISPLAYED + */ + public function onDisplayingImage($event) { + global $page; + $imageID = $event->image->id; + $poolsIDs = $this->get_pool_id($imageID); + + $linksPools = ""; + foreach ($poolsIDs as $poolID){ + $pools = $this->get_pool($poolID['pool_id']); + foreach ($pools as $pool){ + $linksPools .= "".$pool['title'].", "; + } + } + $linksPools = substr($linksPools, 0, -2); + $linksPools = $linksPools." "; + $this->theme->pool_info($linksPools); + } + + + + /* + * HERE WE GET THE LIST OF POOLS + */ + private function list_pools(Page $page, $event){ + global $config, $database; + + $pageNumber = $event->get_arg(1); + if(is_null($pageNumber) || !is_numeric($pageNumber)) + $pageNumber = 0; + else if ($pageNumber <= 0) + $pageNumber = 0; + else + $pageNumber--; + + $poolsPerPage = $config->get_int("poolsListsPerPage"); + + $pools = $database->get_all( + "SELECT p.id, p.user_id, p.public, p.title, p.description, p.posts, u.name as user_name ". + "FROM pools AS p ". + "INNER JOIN users AS u ". + "ON p.user_id = u.id ". + "ORDER BY p.date DESC ". + "LIMIT ?, ?" + , array($pageNumber * $poolsPerPage, $poolsPerPage) + ); + + $totalPages = ceil($database->db->GetOne("SELECT COUNT(*) FROM pools") / $poolsPerPage); + + $this->theme->list_pools($page, $pools, $pageNumber + 1, $totalPages); + } + + + + /* + * HERE WE CREATE A NEW POOL + */ + private function add_pool(){ + global $user, $database; + + $public = html_escape($_POST["public"]); + $title = mysql_real_escape_string(html_escape($_POST["title"])); + $description = mysql_real_escape_string(html_escape($_POST["description"])); + + if($public <> "Y"){ + $public = "N"; + } + + $database->execute(" + INSERT INTO pools + (user_id, public, title, description, date) + VALUES + (?, ?, ?, ?, now())", + array($user->id, $public, $title, $description)); + + $result = $database->get_row("SELECT LAST_INSERT_ID() AS poolID", array()); + + log_info("pools", "Pool {$result["poolID"]} created by {$user->name}"); + + return $result["poolID"]; + } + + private function get_pool($poolID){ + global $database; + $poolID = int_escape($poolID); + return $database->get_all("SELECT * FROM pools WHERE id=?", array($poolID)); + } + + private function get_single_pool($poolID) + { + global $database; + $poolID = int_escape($poolID); + return $database->get_row("SELECT * FROM pools WHERE id=?", array($poolID)); + } + + /* + * HERE WE GET THE ID OF THE POOL FROM AN IMAGE + */ + private function get_pool_id($imageID){ + global $database; + $imageID = int_escape($imageID); + return $database->get_all("SELECT pool_id FROM pool_images WHERE image_id =?", array($imageID)); + } + + + + /* + * HERE WE GET THE IMAGES FROM THE TAG ON IMPORT + */ + private function import_posts(){ + global $page, $config, $database; + + $pool_id = int_escape($_POST["pool_id"]); + $pool_tag = mysql_real_escape_string(html_escape($_POST["pool_tag"])); + + $poolsMaxResults = $config->get_int("poolsMaxImportResults", 1000); + + $images = $images = Image::find_images(0, $poolsMaxResults, Tag::explode($pool_tag)); + $this->theme->pool_result($page, $images, $pool_id); + } + + + + /* + * HERE WE ADD CHECKED IMAGES FROM POOL AND UPDATE THE HISTORY + */ + private function add_posts(){ //ADD CHECKED POSTS + global $database; + + $poolID = int_escape($_POST['pool_id']); + $images = ""; + + foreach ($_POST['check'] as $imageID){ + + if(!$this->check_post($poolID, $imageID)){ + $database->execute(" + INSERT INTO pool_images + (pool_id, image_id) + VALUES + (?, ?)", + array($poolID, $imageID)); + + $images .= " ".$imageID; + } + + } + + if(!strlen($images) == 0){ + $count = $database->db->GetOne("SELECT COUNT(*) FROM pool_images WHERE pool_id=?", array($poolID)); + $this->add_history($poolID, 1, $images, $count); + } + + $database->Execute("UPDATE pools SET posts=(SELECT COUNT(*) FROM pool_images WHERE pool_id=?) WHERE id=?", array($poolID, $poolID)); + return $poolID; + } + + private function order_posts(){ //ORDER POSTS + global $database; + + $poolID = int_escape($_POST['pool_id']); + + foreach ($_POST['imgs'] as $data) { + list ($imageORDER, $imageID) = $data; + + $imageID = int_escape($imageID); + $database->Execute("UPDATE pool_images SET image_order = ? WHERE pool_id = ? AND image_id = ?", array($imageORDER, $poolID, $imageID)); + } + + return $poolID; + } + + + + /* + * HERE WE REMOVE CHECKED IMAGES FROM POOL AND UPDATE THE HISTORY + */ + private function remove_posts(){ + global $database; + + $poolID = int_escape($_POST['pool_id']); + $images = ""; + + foreach ($_POST['check'] as $imageID){ + $database->execute("DELETE FROM pool_images WHERE pool_id = ? AND image_id = ?", array($poolID, $imageID)); + + $images .= " ".$imageID; + } + + $count = $database->db->GetOne("SELECT COUNT(*) FROM pool_images WHERE pool_id=?", array($poolID)); + $this->add_history($poolID, 0, $images, $count); + return $poolID; + } + + + + /* + * HERE WE CHECK IF THE POST IS ALREADY ON POOL + * USED IN add_posts() + */ + private function check_post($poolID, $imageID){ + global $database; + $result = $database->db->GetOne("SELECT COUNT(*) FROM pool_images WHERE pool_id=? AND image_id=?", array($poolID, $imageID)); + if($result == 0) { + return FALSE; + } else { + return TRUE; + } + } + + + + /* + * HERE WE GET ALL IMAGES FOR THE POOL + */ + private function get_posts($event, $poolID){ + global $config, $user, $database; + + $pageNumber = $event->get_arg(2); + if(is_null($pageNumber) || !is_numeric($pageNumber)) + $pageNumber = 0; + else if ($pageNumber <= 0) + $pageNumber = 0; + else + $pageNumber--; + + $poolID = int_escape($poolID); + + $imagesPerPage = $config->get_int("poolsImagesPerPage"); + + // WE CHECK IF THE EXTENSION RATING IS INSTALLED, WICH VERSION AND IF IT WORKS TO SHOW/HIDE SAFE, QUESTIONABLE, EXPLICIT AND UNRATED IMAGES FROM USER + if($config->get_int("ext_ratings2_version") < 3) { + $result = $database->get_all("SELECT image_id ". + "FROM pool_images ". + "WHERE pool_id=? ". + "ORDER BY image_order ASC ". + "LIMIT ?, ?" + , array($poolID, $pageNumber * $imagesPerPage, $imagesPerPage)); + $totalPages = ceil($database->db->GetOne("SELECT COUNT(*) FROM pool_images WHERE pool_id=?",array($poolID)) / $imagesPerPage); + } + + if($config->get_int("ext_ratings2_version") >= 3) { + if($user->is_anonymous()) { + $sqes = $config->get_string("ext_rating_anon_privs"); + } + else if($user->is_admin()) { + $sqes = $config->get_string("ext_rating_admin_privs"); + } + else { + $sqes = $config->get_string("ext_rating_user_privs"); + } + $arr = array(); + for($i=0; $iget_all("SELECT p.image_id ". + "FROM pool_images AS p ". + "INNER JOIN images AS i ". + "ON i.id = p.image_id ". + "WHERE p.pool_id = ? ". + "AND i.rating IN ($rating) ". + "ORDER BY p.image_order ASC ". + "LIMIT ?, ?" + , array($poolID, $pageNumber * $imagesPerPage, $imagesPerPage)); + + $totalPages = ceil($database->db->GetOne("SELECT COUNT(*) ". + "FROM pool_images AS p ". + "INNER JOIN images AS i ". + "ON i.id = p.image_id ". + "WHERE pool_id=? ". + "AND i.rating IN ($rating) " + ,array($poolID)) / $imagesPerPage); + } + + $images = array(); + foreach ($result as $singleResult) { + $image = Image::by_id($singleResult["image_id"]); + $images[] = array($image); + } + + $pool = $this->get_pool($poolID); + $this->theme->view_pool($pool, $images, $pageNumber + 1, $totalPages); + } + + + /* + * WE GET THE ORDER OF THE IMAGES + */ + private function edit_posts($poolID){ + global $database; + $poolID = int_escape($poolID); + + $result = $database->Execute("SELECT image_id FROM pool_images WHERE pool_id=? ORDER BY image_order ASC", array($poolID)); + + $images = array(); + while(!$result->EOF) { + $image = Image::by_id($result->fields["image_id"]); + $images[] = array($image); + $result->MoveNext(); + } + + return $images; + } + + + + /* + * WE GET THE ORDER OF THE IMAGES BUT HERE WE SEND KEYS ADDED IN ARRAY TO GET THE ORDER IN THE INPUT VALUE + */ + private function edit_order($poolID){ + global $database; + $poolID = int_escape($poolID); + + $result = $database->Execute("SELECT image_id FROM pool_images WHERE pool_id=? ORDER BY image_order ASC", array($poolID)); + + $images = array(); + while(!$result->EOF) { + $image = $database->get_row("SELECT * FROM images AS i ". + "INNER JOIN pool_images AS p ". + "ON i.id = p.image_id ". + "WHERE pool_id=? AND i.id=?" + , array($poolID, $result->fields["image_id"])); + $image = ($image ? new Image($image) : null); + $images[] = array($image); + $result->MoveNext(); + } +// Original code +// +// $images = array(); +// while(!$result->EOF) { +// $image = Image::by_id($result->fields["image_id"]); +// $images[] = array($image); +// $result->MoveNext(); +// } + return $images; + } + + + + /* + * HERE WE NUKE ENTIRE POOL. WE REMOVE POOLS AND POSTS FROM REMOVED POOL AND HISTORIES ENTRIES FROM REMOVED POOL + */ + private function nuke_pool($poolID){ + global $user, $database; + + if($user->is_admin()){ + $database->execute("DELETE FROM pool_history WHERE pool_id = ?", array($poolID)); + $database->execute("DELETE FROM pool_images WHERE pool_id = ?", array($poolID)); + $database->execute("DELETE FROM pools WHERE id = ?", array($poolID)); + } elseif(!$user->is_anonymous()){ + // WE CHECK IF THE USER IS THE OWNER OF THE POOL IF NOT HE CAN'T DO ANYTHING + $database->execute("DELETE FROM pool_history WHERE pool_id = ?", array($poolID)); + $database->execute("DELETE FROM pool_images WHERE pool_id = ?", array($poolID)); + $database->execute("DELETE FROM pools WHERE id = ? AND user_id = ?", array($poolID, $user->id)); + } + } + + + + /* + * HERE WE ADD A HISTORY ENTRY + * FOR $action 1 (one) MEANS ADDED, 0 (zero) MEANS REMOVED + */ + private function add_history($poolID, $action, $images, $count){ + global $user, $database; + $user_id = $user->id; + $database->execute(" + INSERT INTO pool_history + (pool_id, user_id, action, images, count, date) + VALUES + (?, ?, ?, ?, ?, now())", + array($poolID, $user_id, $action, $images, $count)); + + } + + + + /* + * HERE WE GET THE HISTORY LIST + */ + private function get_history($event){ + global $config, $database; + + $pageNumber = $event->get_arg(1); + if(is_null($pageNumber) || !is_numeric($pageNumber)) + $pageNumber = 0; + else if ($pageNumber <= 0) + $pageNumber = 0; + else + $pageNumber--; + + + $historiesPerPage = $config->get_int("poolsUpdatedPerPage"); + + $history = $database->get_all( + "SELECT h.id, h.pool_id, h.user_id, h.action, h.images, h.count, h.date, u.name as user_name, p.title as title ". + "FROM pool_history AS h ". + "INNER JOIN pools AS p ". + "ON p.id = h.pool_id ". + "INNER JOIN users AS u ". + "ON h.user_id = u.id ". + "ORDER BY h.date DESC ". + "LIMIT ?, ?" + , array($pageNumber * $historiesPerPage, $historiesPerPage)); + + $totalPages = ceil($database->db->GetOne("SELECT COUNT(*) FROM pool_history") / $historiesPerPage); + + $this->theme->show_history($history, $pageNumber + 1, $totalPages); + } + + + + /* + * HERE GO BACK IN HISTORY AND ADD OR REMOVE POSTS TO POOL + */ + private function revert_history($historyID){ + global $database; + $status = $database->get_all("SELECT * FROM pool_history WHERE id=?", array($historyID)); + + foreach ($status as $entry) + { + $images = trim($entry['images']); + $images = explode(" ", $images); + $poolID = $entry['pool_id']; + $imageArray = ""; + + if ($entry['action'] == 0) { + // READD ENTRIES + foreach ($images as $image) { + $imageID = $image; + $this->add_post($poolID, $imageID); + + $imageArray .= " ".$imageID; + $newAction = 1; + } + } elseif ($entry['action'] == 1) { + // DELETE ENTRIES + foreach ($images as $image) { + $imageID = $image; + $this->delete_post($poolID, $imageID); + + $imageArray .= " ".$imageID; + $newAction = 0; + } + } + + $count = $database->db->GetOne("SELECT COUNT(*) FROM pool_images WHERE pool_id=?", array($poolID)); + $this->add_history($poolID, $newAction, $imageArray, $count); + } + } + + + + /* + * HERE WE ADD A SIMPLE POST FROM POOL + * USED WITH FOREACH IN revert_history() + */ + private function add_post($poolID, $imageID){ + global $database; + + if(!$this->check_post($poolID, $imageID)){ + $database->execute(" + INSERT INTO pool_images + (pool_id, image_id) + VALUES + (?, ?)", + array($poolID, $imageID)); + } + + $database->execute("UPDATE pools SET posts=(SELECT COUNT(*) FROM pool_images WHERE pool_id=?) WHERE id=?", array($poolID, $poolID)); + } + + + + /* + * HERE WE REMOVE A SIMPLE POST FROM POOL + * USED WITH FOREACH IN revert_history() + */ + private function delete_post($poolID, $imageID){ + global $database; + + $database->execute("DELETE FROM pool_images WHERE pool_id = ? AND image_id = ?", array($poolID, $imageID)); + $database->execute("UPDATE pools SET posts=(SELECT COUNT(*) FROM pool_images WHERE pool_id=?) WHERE id=?", array($poolID, $poolID)); + } + +} +?> diff --git a/contrib/pools/theme.php b/contrib/pools/theme.php index 4d84382e..550ac405 100644 --- a/contrib/pools/theme.php +++ b/contrib/pools/theme.php @@ -1,476 +1,488 @@ -get_bool("poolsInfoOnViewImage")) { - if($linksPools <> " ") { - $page->add_block(new Block("Pool Info", $editor, "main", 1)); - } - } - } - - - - /* - * HERE WE SHOWS THE LIST OF POOLS - */ - public function list_pools(Page $page, $pools, $pageNumber, $totalPages) { - global $user; - - $html = ''. - "". - "". - "". - "". - ""; - - if($user->is_admin()) { - $html .= ""; - } - $html .= ""; - - $n = 0; - foreach ($pools as $pool) { - $oe = ($n++ % 2 == 0) ? "even" : "odd"; - - $pool_link = ''.$pool['title'].""; - $user_link = ''.$pool['user_name'].""; - $del_link = 'Delete'; - - if($pool['public'] == "Y") { - $public = "Yes"; - } elseif($pool['public'] == "N") { - $public = "No"; - } - - $html .= "". - "". - "". - "". - ""; - - if($user->is_admin()) { - $html .= ""; - } - - $html .= ""; - - } - - if(!$user->is_anonymous()) { - $html .= ""; - } - $html .= "
NameCreatorPostsPublicAction
".$pool_link."".$user_link."".$pool['posts']."".$public."".$del_link."
Create New
"; - - $blockTitle = "Pools"; - $page->set_title(html_escape($blockTitle)); - $page->set_heading(html_escape($blockTitle)); - $page->add_block(new Block($blockTitle, $html, "main", 10)); - - $this->display_paginator($page, "pool/list", null, $pageNumber, $totalPages); - } - - - - /* - * HERE WE DISPLAY THE NEW POOL COMPOSER - */ - public function new_pool_composer(Page $page) { - $html = "
- - - - - -
Title:
Public?
Description:
- "; - - $blockTitle = "Create Pool"; - $page->set_title(html_escape($blockTitle)); - $page->set_heading(html_escape($blockTitle)); - $page->add_block(new Block($blockTitle, $html, "main", 10)); - } - - - - /* - * HERE WE DISPLAY THE POOL WITH TITLE DESCRIPTION AND IMAGES WITH PAGINATION - */ - public function view_pool($pools, $images, $pageNumber, $totalPages) { - global $user, $page; - - $pool_info = "". - "". - "". - "". - ""; - - $n = 0; - foreach ($pools as $pool) { - $oe = ($n++ % 2 == 0) ? "even" : "odd"; - - $pool_info .= "". - "". - "". - ""; - - // this will make disasters if more than one pool comes in the parameter - if($pool['public'] == "Y" || $user->is_admin()) {// IF THE POOL IS PUBLIC OR IS ADMIN SHOW EDIT PANEL - if(!$user->is_anonymous()) {// IF THE USER IS REGISTERED AND LOGGED IN SHOW EDIT PANEL - $this->sidebar_options($page, $pool); - } - } - $this->display_paginator($page, "pool/view/".$pool['id'], null, $pageNumber, $totalPages); - } - - $pool_info .= "
TitleDescription
".$pool['title']."".$pool['description']."
"; - - if(count($pools) == 1) { - $page->set_title("Pool: ".html_escape($pool['title'])); - $page->set_heading(html_escape($pool['title'])); - $page->add_block(new Block("Viewing Pool", $pool_info, "main", 10)); - } - else { - $page->set_title("Viewing Pool"); - $page->set_heading("Viewing Pool"); - $page->add_block(new Block("Viewing Pool", $pool_info, "main", 10)); - } - - $pool_images = ''; - foreach($images as $pair) { - $image = $pair[0]; - - $thumb_html = $this->build_thumb_html($image); - - $pool_images .= ''. - ''.$thumb_html.''. - ''; - } - - //$pool_images .= print_r($images); - $page->add_block(new Block("Viewing Posts", $pool_images, "main", 30)); - } - - - - /* - * HERE WE DISPLAY THE POOL OPTIONS ON SIDEBAR BUT WE HIDE REMOVE OPTION IF THE USER IS NOT THE OWNER OR ADMIN - */ - public function sidebar_options(Page $page, $pool) { - global $user; - - $editor = " - - - -
- -
- - -
- -
- - -
- "; - - if($user->id == $pool['user_id'] || $user->is_admin()) { - $editor .= " - - -
- - -
- "; - } - $page->add_block(new Block("Manage Pool", $editor, "left", 10)); - } - - - - /* - * HERE WE DISPLAY THE RESULT OF THE SEARCH ON IMPORT - */ - public function pool_result(Page $page, $images, $pool_id) { - - $pool_images = " - - - - - "; - - $pool_images .= "
"; - - foreach($images as $image) { - - $thumb_html = $this->build_thumb_html($image); - - $pool_images .= ''. - ''.$thumb_html.''. - '
'. - ''. - '
'; - } - $pool_images .= "
". - "". - "". - "
"; - - $page->add_block(new Block("Import", $pool_images, "main", 10)); - - $editor = " - - - "; - - $page->add_block(new Block("Manage Pool", $editor, "left", 10)); - } - - - - /* - * HERE WE DISPLAY THE POOL ORDERER - * WE LIST ALL IMAGES ON POOL WITHOUT PAGINATION AND WITH A TEXT INPUT TO SET A NUMBER AND CHANGE THE ORDER - */ - public function edit_order(Page $page, $pools, $images) { - global $user; - - $pool_info = "". - "". - "". - "". - ""; - - $n = 0; - - foreach ($pools as $pool) { - $oe = ($n++ % 2 == 0) ? "even" : "odd"; - - $pool_info .= "". - "". - "". - ""; - - } - $pool_info .= "
TitleDescription
".$pool['title']."".$pool['description']."
"; - - $page->set_title("Sorting Pool"); - $page->set_heading("Sorting Pool"); - $page->add_block(new Block("Sorting Pool", $pool_info, "main", 10)); - - $pool_images = "
"; - $n = 0; - foreach($images as $pair) { - $image = $pair[0]; - $thumb_html = $this->build_thumb_html($image); - $pool_images .= ''. - ''.$thumb_html.''; - $pool_images .= '
'. - ''; - $n = $n+1; - $pool_images .= '
'; - } - - $pool_images .= "
". - "". - "". - "
"; - - $page->add_block(new Block("Sorting Posts", $pool_images, "main", 30)); - } - - - - /* - * HERE WE DISPLAY THE POOL EDITOR - * WE LIST ALL IMAGES ON POOL WITHOUT PAGINATION AND WITH A CHECKBOX TO SELECT WHICH IMAGE WE WANT REMOVE - */ - public function edit_pool(Page $page, $pools, $images) { - global $user; - - $pool_info = "". - "". - "". - "". - ""; - - $n = 0; - - foreach ($pools as $pool) { - $oe = ($n++ % 2 == 0) ? "even" : "odd"; - - $pool_info .= "". - "". - "". - ""; - - } - $pool_info .= "
TitleDescription
".$pool['title']."".$pool['description']."
"; - - $page->set_title("Editing Pool"); - $page->set_heading("Editing Pool"); - $page->add_block(new Block("Editing Pool", $pool_info, "main", 10)); - - - $pool_images = " - - "; - - $pool_images .= "
"; - - foreach($images as $pair) { - $image = $pair[0]; - $thumb_html = $this->build_thumb_html($image); - $pool_images .= ''. - ''.$thumb_html.''; - $pool_images .= '
'; - $pool_images .= '
'; - } - - $pool_images .= "
". - "". - "". - "
"; - - $page->add_block(new Block("Editing Posts", $pool_images, "main", 30)); - - $editor = " - - "; - - $page->add_block(new Block("Manage Pool", $editor, "left", 10)); - } - - - - /* - * HERE WE DISPLAY THE HISTORY LIST - */ - public function show_history($histories, $pageNumber, $totalPages) { - global $page; - $html = "". - "". - "". - "". - "". - "". - "". - "". - ""; - - $n = 0; - foreach ($histories as $history) { - $oe = ($n++ % 2 == 0) ? "even" : "odd"; - - $pool_link = "".$history['title'].""; - $user_link = "".$history['user_name'].""; - $revert_link = "Revert"; - - if ($history['action'] == 1) { - $prefix = "+"; - } elseif ($history['action'] == 0) { - $prefix = "-"; - } - - $images = trim($history['images']); - $images = explode(" ", $images); - - $image_link = ""; - foreach ($images as $image) { - $image_link .= "".$prefix.$image." "; - } - - $html .= "". - "". - "". - "". - "". - "". - "". - ""; - } - - $html .= "
PoolPost CountChangesUpdaterDateAction
".$pool_link."".$history['count']."".$image_link."".$user_link."".$history['date']."".$revert_link."
"; - - $page->set_title("Recent Changes"); - $page->set_heading("Recent Changes"); - $page->add_block(new Block("Recent Changes", $html, "main", 10)); - - $this->display_paginator($page, "pool/updated", null, $pageNumber, $totalPages); - } - - - - /* - * HERE WE DISPLAY THE ERROR - */ - public function display_error($errMessage) { - global $page; - - $page->set_title("Error"); - $page->set_heading("Error"); - $page->add_block(new Block("Error", $errMessage, "main", 10)); - } - -} -?> +get_bool("poolsInfoOnViewImage")){ + if($linksPools <> " "){ + $page->add_block(new Block("Pool Info", $editor, "main", 1)); + } + } + } + + + + /* + * HERE WE SHOWS THE LIST OF POOLS + */ + public function list_pools(Page $page, $pools, $pageNumber, $totalPages) + { + global $user; + + $html = ''. + "". + "". + "". + "". + ""; + + if($user->is_admin()){ + $html .= ""; + } + $html .= ""; + + $n = 0; + foreach ($pools as $pool) + { + $oe = ($n++ % 2 == 0) ? "even" : "odd"; + + $pool_link = ''.$pool['title'].""; + $user_link = ''.$pool['user_name'].""; + $del_link = 'Delete'; + + if($pool['public'] == "Y"){ + $public = "Yes"; + } elseif($pool['public'] == "N"){ + $public = "No"; + } + + $html .= "". + "". + "". + "". + ""; + + if($user->is_admin()){ + $html .= ""; + } + + $html .= ""; + + } + + $html .= "
NameCreatorPostsPublicAction
".$pool_link."".$user_link."".$pool['posts']."".$public."".$del_link."
"; + + $blockTitle = "Pools"; + $page->set_title(html_escape($blockTitle)); + $page->set_heading(html_escape($blockTitle)); + $page->add_block(new Block($blockTitle, $html, "main", 10)); + + $this->display_paginator($page, "pool/list", null, $pageNumber, $totalPages); + } + + + + /* + * HERE WE DISPLAY THE NEW POOL COMPOSER + */ + public function new_pool_composer(Page $page) + { + $html = "
+ + + + + +
Title:
Public?
Description:
+ "; + + $blockTitle = "Create Pool"; + $page->set_title(html_escape($blockTitle)); + $page->set_heading(html_escape($blockTitle)); + $page->add_block(new Block($blockTitle, $html, "main", 10)); + } + + + + /* + * HERE WE DISPLAY THE POOL WITH TITLE DESCRIPTION AND IMAGES WITH PAGINATION + */ + public function view_pool($pools, $images, $pageNumber, $totalPages) + { + global $user, $page; + + $pool_info = "". + "". + "". + "". + ""; + + $n = 0; + foreach ($pools as $pool) + { + $oe = ($n++ % 2 == 0) ? "even" : "odd"; + + $pool_info .= "". + "". + "". + ""; + + // this will make disasters if more than one pool comes in the parameter + if($pool['public'] == "Y" || $user->is_admin()){// IF THE POOL IS PUBLIC OR IS ADMIN SHOW EDIT PANEL + if(!$user->is_anonymous()){// IF THE USER IS REGISTERED AND LOGGED IN SHOW EDIT PANEL + $this->sidebar_options($page, $pool); + } + } + $this->display_paginator($page, "pool/view/".$pool['id']."", null, $pageNumber, $totalPages); + } + + $pool_info .= "
TitleDescription
".$pool['title']."".$pool['description']."
"; + + $page->set_title("Viewing Pool"); + $page->set_heading("Viewing Pool"); + $page->add_block(new Block("Viewing Pool", $pool_info, "main", 10)); + + $pool_images = ''; + foreach($images as $pair) { + $image = $pair[0]; + + $thumb_html = $this->build_thumb_html($image); + + $pool_images .= ''. + ''.$thumb_html.''. + ''; + } + + //$pool_images .= print_r($images); + $page->add_block(new Block("Viewing Posts", $pool_images, "main", 30)); + } + + + + /* + * HERE WE DISPLAY THE POOL OPTIONS ON SIDEBAR BUT WE HIDE REMOVE OPTION IF THE USER IS NOT THE OWNER OR ADMIN + */ + public function sidebar_options(Page $page, $pool){ + global $user; + + $editor = " + + + +
+ +
+ + +
+ +
+ + +
+ "; + + if($user->id == $pool['user_id'] || $user->is_admin()){ + $editor .= " + + +
+ + +
+ "; + } + $page->add_block(new Block("Manage Pool", $editor, "left", 10)); + } + + + + /* + * HERE WE DISPLAY THE RESULT OF THE SEARCH ON IMPORT + */ + public function pool_result(Page $page, $images, $pool_id){ + + $pool_images = " + + + + + "; + + $pool_images .= "
"; + + foreach($images as $image) { + + $thumb_html = $this->build_thumb_html($image); + + $pool_images .= ''. + ''.$thumb_html.''. + '
'. + ''. + '
'; + } + $pool_images .= "
". + "". + "". + "
"; + + $page->add_block(new Block("Import", $pool_images, "main", 10)); + + $editor = " + + + "; + + $page->add_block(new Block("Manage Pool", $editor, "left", 10)); + } + + + + /* + * HERE WE DISPLAY THE POOL ORDERER + * WE LIST ALL IMAGES ON POOL WITHOUT PAGINATION AND WITH A TEXT INPUT TO SET A NUMBER AND CHANGE THE ORDER + */ + public function edit_order(Page $page, $pools, $images) + { + global $user; + + $pool_info = "". + "". + "". + "". + ""; + + $n = 0; + + foreach ($pools as $pool) + { + $oe = ($n++ % 2 == 0) ? "even" : "odd"; + + $pool_info .= "". + "". + "". + ""; + + } + $pool_info .= "
TitleDescription
".$pool['title']."".$pool['description']."
"; + + $page->set_title("Sorting Pool"); + $page->set_heading("Sorting Pool"); + $page->add_block(new Block("Sorting Pool", $pool_info, "main", 10)); + + $pool_images = "
"; + $n = 0; + foreach($images as $pair) { + $image = $pair[0]; + + $thumb_html = $this->build_thumb_html($image); + + $pool_images .= ''. + ''.$thumb_html.''; + + + $pool_images .= '
'. + ''; + $n = $n+1; + + $pool_images .= '
'; + } + + $pool_images .= "
". + "". + "". + "
"; + + $page->add_block(new Block("Sorting Posts", $pool_images, "main", 30)); + } + + + + /* + * HERE WE DISPLAY THE POOL EDITOR + * WE LIST ALL IMAGES ON POOL WITHOUT PAGINATION AND WITH A CHECKBOX TO SELECT WHICH IMAGE WE WANT REMOVE + */ + public function edit_pool(Page $page, $pools, $images) + { + global $user; + + $pool_info = "". + "". + "". + "". + ""; + + $n = 0; + + foreach ($pools as $pool) + { + $oe = ($n++ % 2 == 0) ? "even" : "odd"; + + $pool_info .= "". + "". + "". + ""; + + } + $pool_info .= "
TitleDescription
".$pool['title']."".$pool['description']."
"; + + $page->set_title("Editing Pool"); + $page->set_heading("Editing Pool"); + $page->add_block(new Block("Editing Pool", $pool_info, "main", 10)); + + + $pool_images = " + + + "; + + $pool_images .= "
"; + + foreach($images as $pair) { + $image = $pair[0]; + + $thumb_html = $this->build_thumb_html($image); + + $pool_images .= ''. + ''.$thumb_html.''; + + + $pool_images .= '
'; + + + $pool_images .= '
'; + } + + $pool_images .= "
". + "". + "". + "
"; + + $page->add_block(new Block("Editing Posts", $pool_images, "main", 30)); + + $editor = " + + "; + + $page->add_block(new Block("Manage Pool", $editor, "left", 10)); + } + + + + /* + * HERE WE DISPLAY THE HISTORY LIST + */ + public function show_history($histories, $pageNumber, $totalPages){ + global $page; + $html = "". + "". + "". + "". + "". + "". + "". + "". + ""; + + $n = 0; + foreach ($histories as $history) + { + $oe = ($n++ % 2 == 0) ? "even" : "odd"; + + $pool_link = "".$history['title'].""; + $user_link = "".$history['user_name'].""; + $revert_link = "Revert"; + + if ($history['action'] == 1) { + $prefix = "+"; + } elseif ($history['action'] == 0) { + $prefix = "-"; + } + + $images = trim($history['images']); + $images = explode(" ", $images); + + $image_link = ""; + foreach ($images as $image) { + $image_link .= "".$prefix.$image." "; + } + + $html .= "". + "". + "". + "". + "". + "". + "". + ""; + } + + $html .= "
PoolPost CountChangesUpdaterDateAction
".$pool_link."".$history['count']."".$image_link."".$user_link."".$history['date']."".$revert_link."
"; + + $page->set_title("Recent Changes"); + $page->set_heading("Recent Changes"); + $page->add_block(new Block("Recent Changes", $html, "main", 10)); + + $this->display_paginator($page, "pool/updated", null, $pageNumber, $totalPages); + } + + + + /* + * HERE WE DISPLAY THE ERROR + */ + public function display_error($errMessage){ + global $page; + + $page->set_title("Error"); + $page->set_heading("Error"); + $page->add_block(new Block("Error", $errMessage, "main", 10)); + } + +} +?> \ No newline at end of file