diff --git a/SocialBookmarkingPlugin.php b/SocialBookmarkingPlugin.php index 2e5d6f3..4761cbe 100644 --- a/SocialBookmarkingPlugin.php +++ b/SocialBookmarkingPlugin.php @@ -6,9 +6,6 @@ * @license http://www.gnu.org/licenses/gpl-3.0.txt GNU GPLv3 */ - -require_once dirname(__FILE__) . '/helpers/SocialBookmarkingFunctions.php'; - /** * Social Bookmarking plugin. */ @@ -16,6 +13,7 @@ class SocialBookmarkingPlugin extends Omeka_Plugin_AbstractPlugin { const ADDTHIS_SERVICES_URL = 'http://cache.addthiscdn.com/services/v1/sharing.en.xml'; const SERVICE_SETTINGS_OPTION = 'social_bookmarking_services'; + const ADD_TO_HEADER_OPTION = 'social_bookmarking_add_to_header'; const ADD_TO_OMEKA_ITEMS_OPTION = 'social_bookmarking_add_to_omeka_items'; const ADD_TO_OMEKA_COLLECTIONS_OPTION = 'social_bookmarking_add_to_omeka_collections'; @@ -29,6 +27,7 @@ class SocialBookmarkingPlugin extends Omeka_Plugin_AbstractPlugin 'initialize', 'config_form', 'config', + 'public_header', 'public_items_show', 'public_collections_show' ); @@ -38,16 +37,34 @@ class SocialBookmarkingPlugin extends Omeka_Plugin_AbstractPlugin */ protected $_options = array( 'social_bookmarking_services' => '', + 'social_bookmarking_add_to_header' => '1', 'social_bookmarking_add_to_omeka_items' => '1', 'social_bookmarking_add_to_omeka_collections' => '1', ); + /** + * @var array Default services. + */ + protected $_defaultEnabledServiceCodes = array( + 'facebook', + 'twitter', + 'linkedin', + 'pinterest', + 'email', + 'google', + 'orkut', + 'delicious', + 'digg', + 'stumbleupon', + 'yahoobkm' + ); + /** * Install the plugin. */ public function hookInstall() { - $this->_options['social_bookmarking_services'] = serialize(social_bookmarking_get_default_service_settings()); + $this->_options['social_bookmarking_services'] = serialize($this->_get_default_service_settings()); $this->_installOptions(); } @@ -67,14 +84,14 @@ public function hookUninstall() public function hookUpgrade($args) { $booleanFilter = new Omeka_Filter_Boolean; - $newServiceSettings = social_bookmarking_get_default_service_settings(); - $oldServiceSettings = social_bookmarking_get_service_settings(); + $newServiceSettings = $this->_get_default_service_settings(); + $oldServiceSettings = $this->_get_service_settings(); foreach($newServiceSettings as $serviceCode => $value) { if (array_key_exists($serviceCode, $oldServiceSettings)) { $newServiceSettings[$serviceCode] = $booleanFilter->filter($oldServiceSettings[$serviceCode]); } } - social_bookmarking_set_service_settings($newServiceSettings); + $this->_set_service_settings($newServiceSettings); } /** @@ -86,11 +103,26 @@ public function hookInitialize() } /** - * Display the plugin config form. + * Render the config form. */ public function hookConfigForm() { - require dirname(__FILE__) . '/config_form.php'; + // Set form defaults. + $services = $this->_get_services(); + $serviceSettings = $this->_get_service_settings(); + $setServices = array(); + foreach($services as $serviceCode => $serviceInfo) { + $setServices[$serviceCode] = array_key_exists($serviceCode, $serviceSettings) + ? $serviceSettings[$serviceCode] + : false; + } + + echo get_view()->partial( + 'plugins/social-bookmarking-config-form.php', + array( + 'services' => $services, + 'setServices' => $setServices, + )); } /** @@ -99,45 +131,187 @@ public function hookConfigForm() public function hookConfig($args) { $post = $args['post']; - unset($post['install_plugin']); + set_option(SocialBookmarkingPlugin::ADD_TO_HEADER_OPTION, $post[SocialBookmarkingPlugin::ADD_TO_HEADER_OPTION]); set_option(SocialBookmarkingPlugin::ADD_TO_OMEKA_ITEMS_OPTION, $post[SocialBookmarkingPlugin::ADD_TO_OMEKA_ITEMS_OPTION]); - unset($post[SocialBookmarkingPlugin::ADD_TO_OMEKA_ITEMS_OPTION]); - set_option(SocialBookmarkingPlugin::ADD_TO_OMEKA_COLLECTIONS_OPTION, $post[SocialBookmarkingPlugin::ADD_TO_OMEKA_COLLECTIONS_OPTION]); + + unset($post[SocialBookmarkingPlugin::ADD_TO_HEADER_OPTION]); + unset($post[SocialBookmarkingPlugin::ADD_TO_OMEKA_ITEMS_OPTION]); unset($post[SocialBookmarkingPlugin::ADD_TO_OMEKA_COLLECTIONS_OPTION]); - $serviceSettings = social_bookmarking_get_service_settings(); + $serviceSettings = $this->_get_service_settings(); $booleanFilter = new Omeka_Filter_Boolean; foreach($post as $key => $value) { if (array_key_exists($key, $serviceSettings)) { $serviceSettings[$key] = $booleanFilter->filter($value); } + else { + $serviceSettings[$key] = false; + } } - social_bookmarking_set_service_settings($serviceSettings); + $this->_set_service_settings($serviceSettings); } - public function hookPublicItemsShow() + /** + * Hook for public header. + */ + public function hookPublicHeader($args) + { + if (get_option(SocialBookmarkingPlugin::ADD_TO_HEADER_OPTION) == '1') { + $view = $args['view']; + $vars = $view->getVars(); + $request = Zend_Controller_Front::getInstance()->getRequest(); + $params = $request->getParams(); + + // We need absolute urls and getRequestUri() doesn't return domain. + $url = WEB_ROOT . $request->getPathInfo(); + if ($params['action'] == 'show' && in_array($params['controller'], array( + 'collections', + 'items', + 'files', + ))) { + $recordType = $view->singularize($params['controller']); + $record = get_current_record($recordType); + $title = isset($vars['title']) + ? $vars['title'] + : strip_formatting(metadata($record, array('Dublin Core', 'Title'))); + $description = strip_formatting(metadata($record, array('Dublin Core', 'Description'))); + } + else { + $title= isset($vars['title']) ? $vars['title'] : get_option('site_title'); + $description = ''; + } + echo $view->partial('social-bookmarking-toolbar.php', array( + 'url' => $url, + 'title' => $title, + 'description' => $description, + 'services' => $this->_get_services(), + 'serviceSettings' => $this->_get_service_settings(), + )); + } + } + + /** + * Hook for public items show view. + */ + public function hookPublicItemsShow($args) { if (get_option(SocialBookmarkingPlugin::ADD_TO_OMEKA_ITEMS_OPTION) == '1') { - $item = get_current_record('item'); + $view = $args['view']; + $item = $args['item']; $url = record_url($item, 'show', true); $title = strip_formatting(metadata($item, array('Dublin Core', 'Title'))); $description = strip_formatting(metadata($item, array('Dublin Core', 'Description'))); echo '

' . __('Social Bookmarking') . '

'; - echo social_bookmarking_toolbar($url, $title, $description); + echo $view->partial('social-bookmarking-toolbar.php', array( + 'url' => $url, + 'title' => $title, + 'description' => $description, + 'services' => $this->_get_services(), + 'serviceSettings' => $this->_get_service_settings(), + )); } } - public function hookPublicCollectionsShow() + /** + * Hook for public collections show view. + */ + public function hookPublicCollectionsShow($args) { if (get_option(SocialBookmarkingPlugin::ADD_TO_OMEKA_COLLECTIONS_OPTION) == '1') { - $collection = get_current_record('collection'); + $view = $args['view']; + $collection = $args['collection']; $url = record_url($collection, 'show', true); $title = strip_formatting(metadata($collection, array('Dublin Core', 'Title'))); $description = strip_formatting(metadata($collection, array('Dublin Core', 'Description'))); echo '

' . __('Social Bookmarking') . '

'; - echo social_bookmarking_toolbar($url, $title, $description); + echo $view->partial('social-bookmarking-toolbar.php', array( + 'url' => $url, + 'title' => $title, + 'description' => $description, + 'services' => $this->_get_services(), + 'serviceSettings' => $this->_get_service_settings(), + )); + } + } + + /** + * Gets the service settings from the database. + */ + protected function _get_service_settings() + { + $serviceSettings = unserialize(get_option(SocialBookmarkingPlugin::SERVICE_SETTINGS_OPTION)); + ksort($serviceSettings); + return $serviceSettings; + } + + /** + * Saves the service settings in the database. + */ + protected function _set_service_settings($serviceSettings) + { + set_option(SocialBookmarkingPlugin::SERVICE_SETTINGS_OPTION, serialize($serviceSettings)); + } + + /** + * Sets default service settings. + */ + protected function _get_default_service_settings() + { + $services = $this->_get_services(); + $serviceSettings = array(); + foreach($services as $serviceCode => $serviceInfo) { + $serviceSettings[$serviceCode] = in_array($serviceCode, $this->_defaultEnabledServiceCodes); + } + return $serviceSettings; + } + + /** + * Gets current services from AddThis. + */ + protected function _get_services() + { + static $services = null; + $booleanFilter = new Omeka_Filter_Boolean; + if (!$services) { + $xml = $this->_get_services_xml(); + $services = array(); + foreach ($xml->data->services->service as $service) { + $serviceCode = (string)$service->code; + $services[$serviceCode] = array( + 'code' => $serviceCode, + 'name' => (string)$service->name, + 'icon' => (string)$service->icon, + 'script_only' => $booleanFilter->filter((string)$service->script_only), + ); + } + } + return $services; + } + + /** + * Gets one current service from AddThis. + */ + protected function _get_service($serviceCode) + { + $services = $this->_get_services(); + if (array_key_exists($serviceCode, $services)) { + return $services[$serviceCode]; + } + return null; + } + + /** + * Gets list of services from AddThis. + */ + protected function _get_services_xml() + { + static $xml = null; + if (!$xml) { + $file = file_get_contents(SocialBookmarkingPlugin::ADDTHIS_SERVICES_URL); + $xml = new SimpleXMLElement($file); } + return $xml; } } diff --git a/helpers/SocialBookmarkingFunctions.php b/helpers/SocialBookmarkingFunctions.php deleted file mode 100644 index b9a0c42..0000000 --- a/helpers/SocialBookmarkingFunctions.php +++ /dev/null @@ -1,104 +0,0 @@ - $serviceInfo) { - $serviceSettings[$serviceCode] = in_array($serviceCode, $defaultEnabledServiceCodes); - } - return $serviceSettings; -} - -function social_bookmarking_get_services_xml() -{ - static $xml = null; - if (!$xml) { - $file = file_get_contents(SocialBookmarkingPlugin::ADDTHIS_SERVICES_URL); - $xml = new SimpleXMLElement($file); - } - return $xml; -} - -function social_bookmarking_get_services() -{ - static $services = null; - $booleanFilter = new Omeka_Filter_Boolean; - if (!$services) { - $xml = social_bookmarking_get_services_xml(); - $services = array(); - foreach ($xml->data->services->service as $service) { - $serviceCode = (string)$service->code; - $services[$serviceCode] = array( - 'code' => $serviceCode, - 'name' => (string)$service->name, - 'icon' => (string)$service->icon, - 'script_only' => $booleanFilter->filter((string)$service->script_only), - ); - } - } - return $services; -} - -function social_bookmarking_get_service($serviceCode) -{ - $services = social_bookmarking_get_services(); - if (array_key_exists($serviceCode, $services)) { - return $services[$serviceCode]; - } - return null; -} - -function social_bookmarking_toolbar($url, $title, $description='') -{ - $html = ''; - $html .= ''; - $html .= '
'; - $services = social_bookmarking_get_services(); - $serviceSettings = social_bookmarking_get_service_settings(); - $booleanFilter = new Omeka_Filter_Boolean; - foreach ($serviceSettings as $serviceCode => $value) { - if ($booleanFilter->filter($value) && array_key_exists($serviceCode, $services)) { - $html .= ''; - } - } - $html .= ''; - //$html .= ''; - $html .= '
'; - $html .= ''; - $html .= ''; - - return $html; -} diff --git a/plugin.ini b/plugin.ini index 9b8a335..d09eb05 100755 --- a/plugin.ini +++ b/plugin.ini @@ -3,7 +3,7 @@ name="Social Bookmarking" author="Roy Rosenzweig Center for History and New Media" description="Uses AddThis to insert a customizable list of social bookmarking sites on each item page." link="http://omeka.org/codex/Plugins/SocialBookmarking_2.0" -version="2.0.1" +version="2.1" license="GPLv3" support_link="http://omeka.org/forums/forum/plugins" omeka_minimum_version="2.0" diff --git a/config_form.php b/views/admin/plugins/social-bookmarking-config-form.php similarity index 61% rename from config_form.php rename to views/admin/plugins/social-bookmarking-config-form.php index 8e9e163..91d10d0 100644 --- a/config_form.php +++ b/views/admin/plugins/social-bookmarking-config-form.php @@ -1,4 +1,19 @@
+
+
+ formLabel(SocialBookmarkingPlugin::ADD_TO_HEADER_OPTION, 'Add to Header'); ?> +
+
+ formCheckbox( + SocialBookmarkingPlugin::ADD_TO_HEADER_OPTION, + true, + array('checked' => (boolean) get_option(SocialBookmarkingPlugin::ADD_TO_HEADER_OPTION))); ?> +

+
+
+
formLabel(SocialBookmarkingPlugin::ADD_TO_OMEKA_ITEMS_OPTION, 'Add to Items'); ?> @@ -33,24 +48,14 @@

-
-
    - $serviceInfo): - if (array_key_exists($serviceCode, $serviceSettings)) { - $value = $serviceSettings[$serviceCode]; - } else { - $value = false; - } - ?> -
  • - formCheckbox($serviceCode, true, array('checked'=>(boolean)$value)); ?> - -
  • - +
      + $serviceInfo): ?> +
    • + formCheckbox($serviceCode, true, array('checked' => (boolean) $setServices[$serviceCode])); ?> + +
    • +
diff --git a/views/public/social-bookmarking-toolbar.php b/views/public/social-bookmarking-toolbar.php new file mode 100644 index 0000000..9df6a3e --- /dev/null +++ b/views/public/social-bookmarking-toolbar.php @@ -0,0 +1,16 @@ + +
+ $value) : + if ($booleanFilter->filter($value) && array_key_exists($serviceCode, $services)) : ?> + + + + +
+ +