1<?php
2/**
3 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
4 * @author     Robert Nitsch <r.s.nitsch@gmail.com>
5 *
6 * Based on the first socialshareprivacy plugin by Frank Schiebel.
7 */
8
9if (!defined('DOKU_INC')) die();
10require_once(DOKU_PLUGIN.'action.php');
11
12class action_plugin_socialshareprivacy2 extends DokuWiki_Action_Plugin {
13
14    public function register(Doku_Event_Handler &$controller) {
15       $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'handle_tpl_metaheader_output');
16    }
17
18    public function handle_tpl_metaheader_output(Doku_Event &$event, $param) {
19        global $conf, $ID;
20
21        $options = array(
22            "buffer_status",
23            "delicious_status",
24            "disqus_status",
25            "facebook_status",
26            "flattr_status",
27            "gplus_status",
28            "hackernews_status",
29            "linkedin_status",
30            "mail_status",
31            "pinterest_status",
32            "reddit_status",
33            "stumbleupon_status",
34            "tumblr_status",
35            "twitter_status",
36            "xing_status",
37
38            "buffer_order",
39            "delicious_order",
40            "disqus_order",
41            "facebook_order",
42            "flattr_order",
43            "gplus_order",
44            "hackernews_order",
45            "linkedin_order",
46            "mail_order",
47            "pinterest_order",
48            "reddit_order",
49            "stumbleupon_order",
50            "tumblr_order",
51            "twitter_order",
52            "xing_order"
53        );
54
55        foreach ($options as $opt) {
56            $opt_value=$this->getConf("$opt");
57            $parts = explode("_", $opt, 2);
58            if ($parts[1] == "status" && $opt_value == "1") { $opt_value = "on"; }
59            if ($parts[1] == "status" && $opt_value == "0") { $opt_value = "off"; }
60            if ($parts[1] == "perma_option" && $opt_value == "1") { $opt_value = "on"; }
61            if ($parts[1] == "perma_option" && $opt_value == "0") { $opt_value = "off"; }
62            if ($opt_value != "") {
63                $jsopt["$parts[0]"] .= "'" . $parts[1] . "' : " . "'" .$opt_value . "',";
64            }
65        }
66
67        $path_prefix = DOKU_BASE."lib/plugins/socialshareprivacy2/SSP/";
68
69        // Output
70        $event->data["script"][] = array (
71            "type" => "text/javascript",
72            "_data" => "",
73            "src" => $path_prefix."scripts/jquery.socialshareprivacy.js"
74        );
75        $event->data["script"][] = array (
76            "type" => "text/javascript",
77            "_data" => "",
78            "src" => $path_prefix."../JC/jquery.cookie.js"
79        );
80
81        $services = array();
82        $orders = array();
83
84        foreach ($options as $opt) {
85            list($service, $setting) = explode("_", $opt, 2);
86            $value = $this->getConf($opt);
87
88            if ($setting == "status" && $value == "1") {
89                $event->data["script"][] = array (
90                    "type" => "text/javascript",
91                    "_data" => "",
92                    "src" => $path_prefix."scripts/jquery.socialshareprivacy.$service.js"
93                );
94            } else if ($setting == "order") {
95                $services[] = "'$service'";
96                $orders[] = $value;
97            }
98        }
99
100        // Sort services by order and name.
101        array_multisort($orders, SORT_ASC, $services, SORT_STRING);
102
103        $settings_order = implode($services, ', ');
104        $settings_url = wl($ID, '', true, '&');
105
106        // Service specific settings
107        $settings_flattr_uid = $this->getConf("flattr_uid");
108        $settings_twitter_via = $this->getConf("twitter_via");
109
110        /*
111        TODO:
112            For some reason, in the below javascript code, $ cannot be used directly. Maybe it is
113            overwritten by some other javascript library.
114
115            The closure is a workaround for this problem.
116        */
117        $script = <<<JS
118    (function ($) {
119        $(document).ready(function () {
120            $.fn.socialSharePrivacy.settings.description = $("div.page > :not(#dw__toc)").text().substr(0, 300);
121            $.fn.socialSharePrivacy.settings.order = [{$settings_order}];
122            $.fn.socialSharePrivacy.settings.path_prefix = "{$path_prefix}";
123            $.fn.socialSharePrivacy.settings.uri = "{$settings_url}";
124            if ($.fn.socialSharePrivacy.settings.services.flattr) {
125                $.fn.socialSharePrivacy.settings.services.flattr.uid = "$settings_flattr_uid";
126            }
127            if ($.fn.socialSharePrivacy.settings.services.twitter) {
128                $.fn.socialSharePrivacy.settings.services.twitter.via = "$settings_twitter_via";
129            }
130
131            $('.socialshareprivacy').socialSharePrivacy();
132        });
133    }(jQuery));
134JS;
135
136        $event->data["script"][] = array (
137            "type" => "text/javascript",
138            "charset" => "utf-8",
139            "_data" => $script
140        );
141    }
142}
143