From 4eec17a45d7e0d1ae8f378ee7b46713ff1760a78 Mon Sep 17 00:00:00 2001 From: Daniel Berthereau Date: Sat, 7 Dec 2013 08:25:27 +0100 Subject: [PATCH 1/2] Internalized functions in plugin class. --- SocialBookmarkingPlugin.php | 143 ++++++++++++++++-- helpers/SocialBookmarkingFunctions.php | 104 ------------- plugin.ini | 2 +- .../social-bookmarking-config-form.php | 24 +-- 4 files changed, 135 insertions(+), 138 deletions(-) delete mode 100644 helpers/SocialBookmarkingFunctions.php rename config_form.php => views/admin/plugins/social-bookmarking-config-form.php (70%) diff --git a/SocialBookmarkingPlugin.php b/SocialBookmarkingPlugin.php index 2e5d6f3..d275d03 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. */ @@ -47,7 +44,7 @@ class SocialBookmarkingPlugin extends Omeka_Plugin_AbstractPlugin */ 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 +64,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 +83,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,22 +111,24 @@ public function hookConfigForm() public function hookConfig($args) { $post = $args['post']; - unset($post['install_plugin']); 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_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() @@ -125,7 +139,7 @@ public function hookPublicItemsShow() $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 $this->_toolbar($url, $title, $description); } } @@ -137,7 +151,104 @@ public function hookPublicCollectionsShow() $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 $this->_toolbar($url, $title, $description); + } + } + + protected function _get_service_settings() + { + $serviceSettings = unserialize(get_option(SocialBookmarkingPlugin::SERVICE_SETTINGS_OPTION)); + ksort($serviceSettings); + return $serviceSettings; + } + + protected function _set_service_settings($serviceSettings) + { + set_option(SocialBookmarkingPlugin::SERVICE_SETTINGS_OPTION, serialize($serviceSettings)); + } + + protected function _get_default_service_settings() + { + $services = $this->_get_services(); + $serviceSettings = array(); + $defaultEnabledServiceCodes = array( + 'facebook', + 'twitter', + 'linkedin', + 'pinterest', + 'email', + 'google', + 'orkut', + 'delicious', + 'digg', + 'stumbleupon', + 'yahoobkm' + ); + foreach($services as $serviceCode => $serviceInfo) { + $serviceSettings[$serviceCode] = in_array($serviceCode, $defaultEnabledServiceCodes); + } + return $serviceSettings; + } + + protected function _get_services_xml() + { + static $xml = null; + if (!$xml) { + $file = file_get_contents(SocialBookmarkingPlugin::ADDTHIS_SERVICES_URL); + $xml = new SimpleXMLElement($file); + } + return $xml; + } + + 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; + } + + protected function _get_service($serviceCode) + { + $services = $this->_get_services(); + if (array_key_exists($serviceCode, $services)) { + return $services[$serviceCode]; + } + return null; + } + + protected function _toolbar($url, $title, $description='') + { + $html = ''; + $html .= ''; + $html .= '
'; + $services = $this->_get_services(); + $serviceSettings = $this->_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/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..3cc7550 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.0.2" 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 70% rename from config_form.php rename to views/admin/plugins/social-bookmarking-config-form.php index 8e9e163..d473cae 100644 --- a/config_form.php +++ b/views/admin/plugins/social-bookmarking-config-form.php @@ -33,24 +33,14 @@

-
-
From 10e7f0432565787357a1e36d537d6bcdbbd19ec3 Mon Sep 17 00:00:00 2001 From: Daniel Berthereau Date: Sat, 7 Dec 2013 09:34:29 +0100 Subject: [PATCH 2/2] Added a hook to insert addthis toolbar inside header of any page. --- SocialBookmarkingPlugin.php | 161 ++++++++++++------ plugin.ini | 2 +- .../social-bookmarking-config-form.php | 15 ++ views/public/social-bookmarking-toolbar.php | 16 ++ 4 files changed, 144 insertions(+), 50 deletions(-) create mode 100644 views/public/social-bookmarking-toolbar.php diff --git a/SocialBookmarkingPlugin.php b/SocialBookmarkingPlugin.php index d275d03..4761cbe 100644 --- a/SocialBookmarkingPlugin.php +++ b/SocialBookmarkingPlugin.php @@ -13,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'; @@ -26,6 +27,7 @@ class SocialBookmarkingPlugin extends Omeka_Plugin_AbstractPlugin 'initialize', 'config_form', 'config', + 'public_header', 'public_items_show', 'public_collections_show' ); @@ -35,10 +37,28 @@ 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. */ @@ -112,9 +132,11 @@ public function hookConfig($args) { $post = $args['post']; + 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]); 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]); @@ -131,30 +153,92 @@ public function hookConfig($args) $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 $this->_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 $this->_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)); @@ -162,44 +246,30 @@ protected function _get_service_settings() 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(); - $defaultEnabledServiceCodes = array( - 'facebook', - 'twitter', - 'linkedin', - 'pinterest', - 'email', - 'google', - 'orkut', - 'delicious', - 'digg', - 'stumbleupon', - 'yahoobkm' - ); foreach($services as $serviceCode => $serviceInfo) { - $serviceSettings[$serviceCode] = in_array($serviceCode, $defaultEnabledServiceCodes); + $serviceSettings[$serviceCode] = in_array($serviceCode, $this->_defaultEnabledServiceCodes); } return $serviceSettings; } - protected function _get_services_xml() - { - static $xml = null; - if (!$xml) { - $file = file_get_contents(SocialBookmarkingPlugin::ADDTHIS_SERVICES_URL); - $xml = new SimpleXMLElement($file); - } - return $xml; - } - + /** + * Gets current services from AddThis. + */ protected function _get_services() { static $services = null; @@ -220,6 +290,9 @@ protected function _get_services() return $services; } + /** + * Gets one current service from AddThis. + */ protected function _get_service($serviceCode) { $services = $this->_get_services(); @@ -229,26 +302,16 @@ protected function _get_service($serviceCode) return null; } - protected function _toolbar($url, $title, $description='') + /** + * Gets list of services from AddThis. + */ + protected function _get_services_xml() { - $html = ''; - $html .= ''; - $html .= '
'; - $services = $this->_get_services(); - $serviceSettings = $this->_get_service_settings(); - $booleanFilter = new Omeka_Filter_Boolean; - foreach ($serviceSettings as $serviceCode => $value) { - if ($booleanFilter->filter($value) && array_key_exists($serviceCode, $services)) { - $html .= ''; - } + static $xml = null; + if (!$xml) { + $file = file_get_contents(SocialBookmarkingPlugin::ADDTHIS_SERVICES_URL); + $xml = new SimpleXMLElement($file); } - $html .= ''; - //$html .= ''; - $html .= '
'; - $html .= ''; - $html .= ''; - - return $html; + return $xml; } } diff --git a/plugin.ini b/plugin.ini index 3cc7550..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.2" +version="2.1" license="GPLv3" support_link="http://omeka.org/forums/forum/plugins" omeka_minimum_version="2.0" diff --git a/views/admin/plugins/social-bookmarking-config-form.php b/views/admin/plugins/social-bookmarking-config-form.php index d473cae..91d10d0 100644 --- a/views/admin/plugins/social-bookmarking-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'); ?> 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)) : ?> + + + + +
+ +