From a1f08342c8b8502197159a9fd78e48e6cead4ea0 Mon Sep 17 00:00:00 2001 From: Cash Costello Date: Sat, 31 Jul 2010 19:22:47 +0000 Subject: improved the upgrade system and moved albums to use an ordered list for images --- actions/admin/upgrade.php | 46 +++++++++++++++ actions/upgrade.php | 39 ------------- actions/upload.php | 4 ++ lib/album.php | 97 +++++++++++++++++++++++++++++-- start.php | 1 + upgrades/2009082901.php | 29 +++++++++ upgrades/2010073101.php | 22 +++++++ version.php | 8 +++ views/default/object/album.php | 25 ++------ views/default/object/image.php | 30 +++------- views/default/tidypics/admin/settings.php | 3 +- views/default/tidypics/admin/upgrade.php | 30 ++++++++++ views/default/tidypics/forms/settings.php | 6 -- 13 files changed, 247 insertions(+), 93 deletions(-) create mode 100644 actions/admin/upgrade.php delete mode 100644 actions/upgrade.php create mode 100644 upgrades/2009082901.php create mode 100644 upgrades/2010073101.php create mode 100644 version.php create mode 100644 views/default/tidypics/admin/upgrade.php diff --git a/actions/admin/upgrade.php b/actions/admin/upgrade.php new file mode 100644 index 000000000..9c4aef1cf --- /dev/null +++ b/actions/admin/upgrade.php @@ -0,0 +1,46 @@ +pluginspath}tidypics/version.php"; + +$local_version = get_plugin_setting('version', 'tidypics'); + +if ($version <= $local_version) { + register_error('No upgrade required'); + forward($_SERVER['HTTP_REFERER']); +} + +$base_dir = $CONFIG->pluginspath . 'tidypics/upgrades'; + +// taken from engine/lib/version.php +if ($handle = opendir($base_dir)) { + $upgrades = array(); + + while ($updatefile = readdir($handle)) { + // Look for upgrades and add to upgrades list + if (!is_dir("$base_dir/$updatefile")) { + if (preg_match('/^([0-9]{10})\.(php)$/', $updatefile, $matches)) { + $plugin_version = (int) $matches[1]; + if ($plugin_version > $local_version) { + $upgrades[] = "$base_dir/$updatefile"; + } + } + } + } + + // Sort and execute + asort($upgrades); + + if (sizeof($upgrades) > 0) { + foreach ($upgrades as $upgrade) { + include($upgrade); + } + } +} + +set_plugin_setting('version', $version, 'tidypics'); + +system_message("Tidypics has been upgraded"); + +forward($_SERVER['HTTP_REFERER']); diff --git a/actions/upgrade.php b/actions/upgrade.php deleted file mode 100644 index 596b90a22..000000000 --- a/actions/upgrade.php +++ /dev/null @@ -1,39 +0,0 @@ -dbprefix . 'entity_subtypes'; - $result = update_data("UPDATE {$table} set class='TidypicsImage' where id={$id}"); - if (!result) { - register_error(elgg_echo('tidypics:upgrade:failed')); - forward($_SERVER['HTTP_REFERER']); - } -} - -// add album class -$id = get_subtype_id("object", "album"); -if ($id != 0) { - $table = $CONFIG->dbprefix . 'entity_subtypes'; - $result = update_data("UPDATE {$table} set class='TidypicsAlbum' where id={$id}"); - if (!result) { - register_error(elgg_echo('tidypics:upgrade:failed')); - forward($_SERVER['HTTP_REFERER']); - } -} - -system_message(elgg_echo('tidypics:upgrade:success')); - -forward($_SERVER['HTTP_REFERER']); diff --git a/actions/upload.php b/actions/upload.php index b1eb4efe1..88b000706 100644 --- a/actions/upload.php +++ b/actions/upload.php @@ -244,6 +244,10 @@ if (count($uploaded_images) && $img_river_view == "1") { // update image repo size create_metadata($album->container_guid, "image_repo_size", $image_repo_size, 'integer', $album->container_guid); +if (count($uploaded_images) > 0) { + $album->prependImageList($uploaded_images); +} + // plugins can register to be told when a Tidypics album has had images added trigger_elgg_event('upload', 'tp_album', $album); diff --git a/lib/album.php b/lib/album.php index 1eaabd3ba..87bb715f4 100644 --- a/lib/album.php +++ b/lib/album.php @@ -17,12 +17,61 @@ class TidypicsAlbum extends ElggObject { parent::__construct($guid); } + /** + * Get an array of image objects + * + * @param int $limit + * @param int $offset + * @return array + */ + public function getImages($limit, $offset=0) { + $imageList = $this->getImageList(); + if ($offset > count($imageList)) { + return array(); + } + + $imageList = array_slice($imageList, $offset, $limit); + + $images = array(); + foreach ($imageList as $guid) { + $images[] = get_entity($guid); + } + return $images; + } + + /** + * View a list of images + * + * @param int $limit + * @param int $offset + * @return string + */ + public function viewImages($limit, $offset=0) { + $images = $this->getImages($limit, $offset); + if (count($images) == 0) { + return ''; + } + + $count = $this->getSize(); + + return elgg_view_entity_list($images, $count, $offset, $limit, FALSE, FALSE, TRUE); + } + + /** + * Get the number of photos in the album + * + * @return int + */ + public function getSize() { + return count($this->getImageList()); + } + /** * Returns an order list of image guids * * @return array */ - public function getOrderedImageList() { + public function getImageList() { $listString = $this->orderedImages; if (!$listString) { return array(); @@ -36,7 +85,7 @@ class TidypicsAlbum extends ElggObject { * * @param array $list An indexed array of image guids */ - public function setOrderedImageList($list) { + public function setImageList($list) { $listString = serialize($list); $this->orderedImages = $listString; } @@ -46,9 +95,47 @@ class TidypicsAlbum extends ElggObject { * * @param array $list An indexed array of image guids */ - public function prependOrderedImageList($list) { - $currentList = $this->getOrderedImageList(); + public function prependImageList($list) { + $currentList = $this->getImageList(); $list = array_merge($list, $currentList); - $this->setOrderedImageList($list); + $this->setImageList($list); + } + + /** + * Get the GUID of the image before the current one + * + * @param int $currentGuid + * @return int + */ + public function getPreviousImageGuid($currentGuid) { + $imageList = $this->getImageList(); + $key = array_search($currentGuid, $imageList); + if ($key === FALSE) { + return 0; + } + $key--; + if ($key < 0) { + return 0; + } + return $imageList[$key]; + } + + /** + * Get the GUID of the image after the current one + * + * @param int $currentGuid + * @return int + */ + public function getNextImageGuid($currentGuid) { + $imageList = $this->getImageList(); + $key = array_search($currentGuid, $imageList); + if ($key === FALSE) { + return 0; + } + $key++; + if ($key >= count($imageList)) { + return 0; + } + return $imageList[$key]; } } diff --git a/start.php b/start.php index 8589be9fb..52d56d77a 100644 --- a/start.php +++ b/start.php @@ -461,3 +461,4 @@ register_action("tidypics/edit_multi", false, $CONFIG->pluginspath. "tidypics/ac register_action("tidypics/addtag", true, $CONFIG->pluginspath . "tidypics/actions/addtag.php"); register_action("tidypics/deletetag", true, $CONFIG->pluginspath . "tidypics/actions/deletetag.php"); register_action("tidypics/flickrSetup", true, $CONFIG->pluginspath . "tidypics/actions/flickrSetup.php"); +register_action("tidypics/admin/upgrade", true, $CONFIG->pluginspath . "tidypics/actions/admin/upgrade.php", true); diff --git a/upgrades/2009082901.php b/upgrades/2009082901.php new file mode 100644 index 000000000..dfbdf79f4 --- /dev/null +++ b/upgrades/2009082901.php @@ -0,0 +1,29 @@ +dbprefix . 'entity_subtypes'; + $result = update_data("UPDATE {$table} set class='TidypicsImage' where id={$id}"); + if (!$result) { + register_error('unable to update tidypics image class'); + } +} + +// add album class +$id = get_subtype_id("object", "album"); +if ($id != 0) { + $table = $CONFIG->dbprefix . 'entity_subtypes'; + $result = update_data("UPDATE {$table} set class='TidypicsAlbum' where id={$id}"); + if (!$result) { + register_error('unable to update tidypics album class'); + } +} diff --git a/upgrades/2010073101.php b/upgrades/2010073101.php new file mode 100644 index 000000000..20fd33144 --- /dev/null +++ b/upgrades/2010073101.php @@ -0,0 +1,22 @@ +dbprefix}entities WHERE subtype = $album_subtype_id"); +while ($guid_obj = mysql_fetch_object($album_guids)) { + $DB_QUERY_CACHE = $DB_PROFILE = $ENTITY_CACHE = array(); + + $album = get_entity($guid_obj->guid); + $images = get_entities("object", "image", $album->guid, '', 9999); + $image_list = array(); + foreach ($images as $image) { + $image_list[] = $image->guid; + } + + $album->prependImageList($image_list); +} + diff --git a/version.php b/version.php new file mode 100644 index 000000000..557ea5d1c --- /dev/null +++ b/version.php @@ -0,0 +1,8 @@ +' . autop($desc) . ''; - $images = get_entities("object", "image", $album_guid, '', 999); - - //build array for back | next links - $_SESSION['image_sort'] = array(); - - if (is_array($images)) { - foreach ($images as $image) { - array_push($_SESSION['image_sort'], $image->guid); - } - - // display the simple image views. Uses 'object/image' view - echo list_entities("object", "image", $album_guid, 24, false); - - $num_images = count($images); - } else { - echo '
' . elgg_echo('image:none') . '
'; - $num_images = 0; - } + $offset = (int)get_input('offset', 0); + echo $album->viewImages(8, $offset); + // echo '
' . elgg_echo('image:none') . '
'; + // $num_images = 0; + //} ?>
@@ -137,7 +124,7 @@ if (get_context() == "search") { } ?> name; ?>
-
+ getSize(); ?>
container_guid); + $back_guid = $album->getPreviousImageGuid($image->guid); + $next_guid = $album->getNextImageGuid($image->guid); - $current = array_search($image_guid, $_SESSION['image_sort']); - - if (!$current) { // means we are no longer using the correct album array - - //rebuild the array - $count = get_entities("object","image", $album->guid, '', 999); - $_SESSION['image_sort'] = array(); - - foreach ($count as $img) { - array_push($_SESSION['image_sort'], $img->guid); - } - - if ($_SESSION['image_sort']) { - $current = array_search($image_guid, $_SESSION['image_sort']); - } - } - - if ($current != 0) { - $back = '« ' . elgg_echo('image:back') . ''; + if ($back_guid != 0) { + $text = elgg_echo('image:back'); + $back = "« $text"; } - if (sizeof($_SESSION['image_sort']) > $current + 1) { - $next = '' . elgg_echo('image:next') . ' »'; + if ($next_guid != 0) { + $text = elgg_echo('image:next'); + $next = "$text »"; } ?> diff --git a/views/default/tidypics/admin/settings.php b/views/default/tidypics/admin/settings.php index 60c34b64e..6dace1830 100644 --- a/views/default/tidypics/admin/settings.php +++ b/views/default/tidypics/admin/settings.php @@ -7,8 +7,7 @@ echo elgg_view('output/longtext', array('value' => elgg_echo("tidypics:admin:ins wwwroot . 'mod/tidypics/pages/server_analysis.php'; +$url = "{$vars['url']}mod/tidypics/pages/server_analysis.php"; $text = elgg_echo('tidypics:settings:server:analysis'); echo "$text"; diff --git a/views/default/tidypics/admin/upgrade.php b/views/default/tidypics/admin/upgrade.php new file mode 100644 index 000000000..5a21aa136 --- /dev/null +++ b/views/default/tidypics/admin/upgrade.php @@ -0,0 +1,30 @@ +pluginspath}tidypics/version.php"; + +$upgrade_url = "{$vars['url']}action/tidypics/admin/upgrade"; +//$upgrade_url = elgg_add_action_tokens_to_url($upgrade_url); + +// determine whether an upgrade is required +$local_version = get_plugin_setting('version', 'tidypics'); +if ($local_version === FALSE) { + // no version set so either new install or really old one + if (!get_subtype_class('object', 'image') || !get_subtype_class('object', 'album')) { + $local_version = 0; + } else { + set_plugin_setting('version', $local_version, 'tidypics'); + $local_version = $version; + } +} elseif ($local_version == '1.62') { + $local_version = 2010010101; + set_plugin_setting('version', $local_version, 'tidypics'); +} +if ($local_version == $version) { + // no upgrade required + return TRUE; +} + +echo elgg_view('output/url', array( 'text' => 'Upgrade', + 'href' => $upgrade_url, + 'is_action' => TRUE)); +echo '
'; \ No newline at end of file diff --git a/views/default/tidypics/forms/settings.php b/views/default/tidypics/forms/settings.php index e7d05adcc..c102b044e 100644 --- a/views/default/tidypics/forms/settings.php +++ b/views/default/tidypics/forms/settings.php @@ -10,12 +10,6 @@ $action = $vars['url'] . 'action/tidypics/settings'; $plugin = find_plugin_settings('tidypics'); -// bootstrap the plugin version here for now -if (!$plugin->version) { - set_plugin_setting('version', 1.62, 'tidypics'); -} - - // Main settings $form_body = '

' . elgg_echo('tidypics:settings:heading:main') . '

'; -- cgit v1.2.3