1<?php
2/**
3 * Block for displaying the current user's Facebook stream, with the ability to
4 * filter it using the same Facebook filters available on facebook.com.  Also
5 * provides ability to update the current user's status.
6 *
7 * Copyright 2009-2017 Horde LLC (http://www.horde.org/)
8 *
9 * @author  Michael J. Rubinsky <mrubinsk@horde.org>
10 * @package Horde
11 */
12class Horde_Block_FbStream extends Horde_Core_Block
13{
14    /**
15     * @var Horde_Service_Facebook
16     */
17    protected $_facebook;
18
19    /**
20     * Cache the uid/sid
21     *
22     * @var string
23     */
24    protected $_fbp = array();
25
26    /**
27     */
28    public function __construct($app, $params = array())
29    {
30        try {
31            $this->_facebook = $GLOBALS['injector']
32                ->getInstance('Horde_Service_Facebook');
33        } catch (Horde_Exception $e) {
34            $this->enabled = false;
35            return;
36        }
37        parent::__construct($app, $params);
38        $this->_name = _("My Facebook Stream");
39        $this->_fbp = unserialize($GLOBALS['prefs']->getValue('facebook'));
40
41    }
42
43    /**
44     */
45    protected function _params()
46    {
47        $filters = array();
48        if (!empty($this->_fbp['sid'])) {
49            try {
50                $stream_filters = $this->_facebook->streams->getFilters($this->_fbp['uid']);
51                foreach ($stream_filters as $filter) {
52                    $filters[$filter['filter_key']] = $filter['name'];
53                }
54            } catch (Horde_Service_Facebook_Exception $e) {
55            }
56        }
57
58        return array(
59            'filter' => array(
60                'type' => 'enum',
61                'name' => _("Filter"),
62                'default' => 'nf',
63                'values' => $filters
64            ),
65            'count' => array(
66                'type' => 'int',
67                'name' => _("Maximum number of entries to display"),
68                'default' => '20'
69            ),
70            'height' => array(
71                 'name' => _("Height of stream content (width automatically adjusts to block)"),
72                 'type' => 'int',
73                 'default' => 250
74            ),
75        );
76    }
77
78    /**
79     */
80    protected function _title()
81    {
82        return Horde::externalUrl('http://facebook.com', true) . $this->getName() . '</a>';
83    }
84
85    /**
86     * The content to go in this block.
87     *
88     * @return string   The content.
89     */
90    protected function _content()
91    {
92        global $page_output;
93
94        $instance = hash('md5', mt_rand());
95        $endpoint = Horde::url('services/facebook/', true);
96        $html = '';
97
98        // Init facebook driver, exit early if no prefs exist
99        $facebook = $this->_facebook;
100        if (!($facebook->auth->getSessionKey())) {
101            return sprintf(
102                _("You are not connected to your Facebook account. You should check your Facebook settings in your %s."),
103                $GLOBALS['registry']->getServiceLink('prefs', 'horde')->add('group', 'facebook')->link() . _("preferences") . '</a>'
104            );
105        }
106
107        // Add the client javascript / initialize it
108        $page_output->addThemeStylesheet('facebook.css');
109        $page_output->addScriptFile('facebookclient.js');
110        $script = <<<EOT
111            var Horde = window.Horde || {};
112            Horde['{$instance}_facebook'] = new Horde_Facebook({
113               spinner: '{$instance}_loading',
114               endpoint: '{$endpoint}',
115               content: '{$instance}_fbcontent',
116               status: '{$instance}_currentStatus',
117               getmore: '{$instance}_getmore',
118               'input': '{$instance}_newStatus',
119               'button': '{$instance}_button',
120               instance: '{$instance}',
121               'filter': '{$this->_params['filter']}',
122               'count': '{$this->_params['count']}'
123            });
124EOT;
125        $page_output->addInlineScript($script);
126
127        // Start building the block UI.
128        $html .= '<div style="padding: 8px 8px 0 8px">';
129
130        try {
131            $html .= Horde_Themes_Image::tag(
132                'loading.gif',
133                array(
134                    'attr' => array(
135                        'id' => $instance. '_loading',
136                        'style' => 'display:none;'
137                    )
138                )
139            );
140        } catch (Horde_Service_Facebook_Exception $e) {
141            $prefs = $GLOBALS['registry']->getServiceLink('prefs');
142            $html .= sprintf(_("There was an error making the request: %s"), $e->getMessage());
143            $html .= sprintf(_("You can also check your Facebook settings in your %s."), $prefs->add('group', 'facebook')->link() . _("preferences") . '</a>');
144            return $html;
145        }
146        $html .= '</div>'; // Close the node that wraps the status
147
148       // Build the stream feed.
149        $html .= '<br /><div id="' . $instance . '_fbcontent" style="height:' . (empty($this->_params['height']) ? 300 : $this->_params['height']) . 'px;overflow-y:auto;overflow-x:hidden;"></div><br />';
150        $html .= '<div class="hordeSmGetmore"><input type="button" id="' . $instance . '_getmore" class="horde-default"  value="' . _("Get More") . '"></div>';
151
152        $html .= '</div>';
153
154        return $html;
155    }
156
157}
158