diff --git a/3.1/modules/bitly/controllers/admin_bitly.php b/3.1/modules/bitly/controllers/admin_bitly.php index f4240a63..4f82f913 100644 --- a/3.1/modules/bitly/controllers/admin_bitly.php +++ b/3.1/modules/bitly/controllers/admin_bitly.php @@ -25,14 +25,14 @@ class Admin_Bitly_Controller extends Admin_Controller { */ public function index() { $form = bitly::get_configure_form(); - $valid_config = true; - + $login = module::get_var("bitly", "login"); + $api_key = module::get_var("bitly", "api_key"); + $domain = module::get_var("bitly", "domain"); + $valid_config = false; + if (request::method() == "post") { access::verify_csrf(); if ($form->validate()) { - $current_login = module::get_var("bitly", "login"); - $current_key = module::get_var("bitly", "api_key"); - $current_domain = module::get_var("bitly", "domain"); $new_login = $form->configure_bitly->login->value; $new_key = $form->configure_bitly->api_key->value; $new_domain = $form->configure_bitly->domain->value; @@ -44,24 +44,24 @@ class Admin_Bitly_Controller extends Admin_Controller { if (!bitly::check_config()) { url::redirect("admin/bitly"); } else { - if ($current_login && !$new_login) { + if ($login && !$new_login) { message::success(t("Your bit.ly login has been cleared.")); - } else if ($current_login && $new_login && $current_login != $new_login) { + } else if ($login && $new_login && $login != $new_login) { message::success(t("Your bit.ly login has been changed.")); - } else if (!$current_login && $new_login) { + } else if (!$login && $new_login) { message::success(t("Your bit.ly login has been saved.")); } - if ($current_key && !$new_key) { + if ($api_key && !$new_key) { message::success(t("Your bit.ly API key has been cleared.")); - } else if ($current_key && $new_key && $current_key != $new_key) { + } else if ($api_key && $new_key && $api_key != $new_key) { message::success(t("Your bit.ly API key has been changed.")); - } else if (!$current_key && $new_key) { + } else if (!$api_key && $new_key) { message::success(t("Your bit.ly API key has been saved.")); } - if ($current_domain && $new_domain && $current_domain != $new_domain) { + if ($domain && $new_domain && $domain != $new_domain) { message::success(t("Your preferrend bit.ly domain has been changed.")); - } else if (!$current_domain && $new_domain) { - message::success(t("Your preferred bit.ly domain has been set.")); + } else if (!$domain && $new_domain) { + message::success(t("Your preferred bit.ly domain has been saved.")); } log::success("bitly", t("bit.ly login changed to %new_login", array("new_login" => $new_login))); @@ -79,17 +79,16 @@ class Admin_Bitly_Controller extends Admin_Controller { $view->content->login = $form->configure_bitly->login->value; $view->content->api_key = $form->configure_bitly->api_key->value; $view->content->domain = $form->configure_bitly->domain->value; - $view->content->valid_config = $valid_config; $view->content->form = $form; - if ($valid_config) { - $link = ORM::factory("bitly_link")->where("item_id", "=", 1)->find(); - if ($link->loaded()) { - $view->content->g3_url = "http://" . module::get_var("bitly", "domain") . "/$link->hash"; - } else { - $view->content->g3_url = bitly::shorten_url(1); - } + $link = ORM::factory("bitly_link")->where("item_id", "=", 1)->find(); + + if ($link->loaded()) { + $view->content->g3_url = bitly::bitly_link($link->hash); + } else if ($valid_config && !empty($login) && !empty($api_key) && !empty($domain)) { + $view->content->g3_url = bitly::shorten_url(1); } + print $view; } diff --git a/3.1/modules/bitly/helpers/bitly.php b/3.1/modules/bitly/helpers/bitly.php index 2ba90670..f89093e8 100644 --- a/3.1/modules/bitly/helpers/bitly.php +++ b/3.1/modules/bitly/helpers/bitly.php @@ -18,6 +18,7 @@ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ class bitly_Core { + public static $test_mode = TEST_MODE; public static $api_host = "api.bit.ly"; @@ -207,4 +208,15 @@ class bitly_Core { } } + /** + * Build a bit.ly link for a specified hash + * @param string $hash + * @return string + */ + static function bitly_link($hash) { + if (!empty($hash)) { + return "http://" . module::get_var("bitly", "domain") . "/$hash"; + } + } + } diff --git a/3.1/modules/bitly/helpers/bitly_event.php b/3.1/modules/bitly/helpers/bitly_event.php index 29463606..899357ce 100644 --- a/3.1/modules/bitly/helpers/bitly_event.php +++ b/3.1/modules/bitly/helpers/bitly_event.php @@ -18,32 +18,37 @@ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ class bitly_event_Core { + + public static $shorten_link_text = "Shorten link with bit.ly"; + static function admin_menu($menu, $theme) { $menu->get("settings_menu") ->append(Menu::factory("link") ->id("bitly_menu") - ->label(t("Bit.ly")) + ->label(t("bit.ly")) ->url(url::site("admin/bitly"))); } static function site_menu($menu, $theme) { - if ($theme->item->owner->id == identity::active_user()->id) { - $menu->get("options_menu") - ->append(Menu::factory("link") - ->id("bitly") - ->label(t("Shorten link with bit.ly")) - ->url(url::site("bitly/shorten/{$theme->item->id}?csrf=$theme->csrf")) - ->css_id("g-bitly-link") - ->css_class("g-bitly-shorten ui-icon-link")); - } - } - - static function context_menu($menu, $theme, $item) { - if ($theme->item->owner->id == identity::active_user()->id) { + $link = ORM::factory("bitly_link")->where("item_id", "=", $theme->item->id)->find(); + if (!$link->loaded() && $theme->item->owner->id == identity::active_user()->id) { $menu->get("options_menu") ->append(Menu::factory("link") ->id("bitly") - ->label(t("Shorten link with bit.ly")) + ->label(t(self::$shorten_link_text)) + ->url(url::site("bitly/shorten/{$theme->item->id}?csrf=$theme->csrf")) + ->css_id("g-bitly-shorten") + ->css_class("g-bitly-shorten ui-icon-link")); + } + } + + static function context_menu($menu, $theme, $item) { + $link = ORM::factory("bitly_link")->where("item_id", "=", $item->id)->find(); + if (!$link->loaded() && $theme->item->owner->id == identity::active_user()->id) { + $menu->get("options_menu") + ->append(Menu::factory("link") + ->id("bitly") + ->label(t(self::$shorten_link_text)) ->url(url::site("bitly/shorten/$item->id?csrf=$theme->csrf")) ->css_class("g-bitly-shorten ui-icon-link")); } diff --git a/3.1/modules/bitly/views/admin_bitly.html.php b/3.1/modules/bitly/views/admin_bitly.html.php index da84df54..d1d6703a 100644 --- a/3.1/modules/bitly/views/admin_bitly.html.php +++ b/3.1/modules/bitly/views/admin_bitly.html.php @@ -7,7 +7,7 @@ "bitly_url" => "http://bit.ly")) ?>

- +
%g3_url", array('g3_url' => $g3_url)) ?>