Overview

Packages

  • Folks
  • None

Classes

  • Folks
  • Folks_Activity_Form
  • Folks_Api
  • Folks_Application
  • Folks_Block_Activities
  • Folks_Block_Friends
  • Folks_Block_Know
  • Folks_Block_New
  • Folks_Block_Random
  • Folks_Block_Recent
  • Folks_Driver
  • Folks_Driver_sql
  • Folks_Friends
  • Folks_Friends_application
  • Folks_Friends_facebook
  • Folks_Friends_prefs
  • Folks_Friends_shared
  • Folks_Friends_sql
  • Folks_Login_Form
  • Folks_Notification
  • Folks_Notification_facebook
  • Folks_Notification_letter
  • Folks_Notification_mail
  • Folks_Notification_tickets
  • Folks_Search_Form
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * Folks Notification Class.
  4:  *
  5:  * Copyright Obala d.o.o. (www.obala.si)
  6:  *
  7:  * See the enclosed file COPYING for license information (GPL). If you
  8:  * did not receive this file, see http://www.horde.org/licenses/gpl.
  9:  *
 10:  * @author  Duck <duck@obala.net>
 11:  * @package Folks
 12:  */
 13: class Folks_Notification_facebook extends Folks_Notification {
 14: 
 15:     /**
 16:      * FB object
 17:      */
 18:     private $_fb;
 19: 
 20:     /**
 21:      * FB connection parameters
 22:      */
 23:     private $_fbp;
 24: 
 25:     /**
 26:      * Returns method human name
 27:      */
 28:     public function getName()
 29:     {
 30:         return _("Facebook");
 31:     }
 32: 
 33:     /**
 34:      * Checks if a driver is available for a certain notification type
 35:      *
 36:      * @param string $type Notification type
 37:      *
 38:      * @return boolean
 39:      */
 40:     public function isAvailable($type)
 41:     {
 42:         // Check FB installation
 43:         if (!$GLOBALS['conf']['facebook']['enabled']) {
 44:             return false;
 45:         }
 46: 
 47:         // Chacke FB user config
 48:         $fbp = unserialize($GLOBALS['prefs']->getValue('facebook'));
 49:         if (!$fbp || empty($fbp['uid'])) {
 50:             return false;
 51:         }
 52: 
 53:         return true;
 54:     }
 55: 
 56:     /**
 57:      * Notify user
 58:      *
 59:      * @param mixed  $user        User or array of users to send notification to
 60:      * @param string $subject     Subject of message
 61:      * @param string $body        Body of message
 62:      * @param array  $attachments Attached files
 63:      *
 64:      * @return true on succes, PEAR_Error on failure
 65:      */
 66:     public function notify($user, $subject, $body, $attachments = array())
 67:     {
 68:         if (!$this->_loadFB()) {
 69:             return $this->_fb;
 70:         }
 71: 
 72:         try {
 73:             $message = $this->_formatBody($subject, $body);
 74:             $result = $this->_fb->notifications->send(array($this->_fbp['uid']), $message, 'user_to_user');
 75:         } catch (Horde_Service_Facebook_Exception $e) {
 76:             return PEAR::raiseError($e->getMessage(), $e->getCode());
 77:         }
 78: 
 79:         return $result;
 80:     }
 81: 
 82:     /**
 83:      * Notify user
 84:      *
 85:      * @param mixed  $user        User or array of users to send notification to
 86:      * @param string $subject     Subject of message
 87:      * @param string $body        Body of message
 88:      * @param array  $attachments Attached files
 89:      *
 90:      * @return true on succes, PEAR_Error on failure
 91:      */
 92:     public function notifyFriends($user, $subject, $body, $attachments = array())
 93:     {
 94:         if (!$this->_loadFB()) {
 95:             return $this->_fb;
 96:         }
 97: 
 98:         try {
 99:             $friends = $this->_fb->friends->get(null, $this->_fbp['uid']);
100:         } catch (Horde_Service_Facebook_Exception $e) {
101:             return PEAR::raiseError($e->getMessage(), $e->getCode());
102:         }
103: 
104:         try {
105:             $message = $this->_formatBody($subject, $body);
106:             $result = $this->_fb->notifications->send($friends, $message, 'user_to_user');
107:         } catch (Horde_Service_Facebook_Exception $e) {
108:             return PEAR::raiseError($e->getMessage(), $e->getCode());
109:         }
110: 
111:         return $result;
112:     }
113: 
114:     /**
115:      * Load FB content
116:      */
117:     private function _loadFB()
118:     {
119:         if ($this->_fb) {
120:             return true;
121:         }
122: 
123:         // Check FB installation
124:         if (!$GLOBALS['conf']['facebook']['enabled']) {
125:             $this->_fb = PEAR::raiseError(_("No Facebook integration exists."));
126:             return false;
127:         }
128: 
129:         // Check FB user config
130:         $this->_fbp = unserialize($GLOBALS['prefs']->getValue('facebook'));
131:         if (!$this->_fbp || empty($this->_fbp['uid'])) {
132:             $this->_fb = PEAR::raiseError(sprintf(_("Could not find authorization for %s to interact with your Facebook account."), $GLOBALS['registry']->get('name', 'horde')));
133:             return false;
134:         }
135: 
136:         // Create FB Object
137:         try {
138:             $this->_fb = $GLOBALS['injector']->getInstance('Horde_Service_Facebook');
139:         } catch (Horde_Exception $e) {
140:             $error = PEAR::raiseError($e->getMessage(), $e->getCode());
141:             Horde::logMessage($error, 'ERR');
142: 
143:             return $error;
144:         }
145: 
146:         // Set Auth user
147:         $this->_fb->auth->setUser($this->_fbp['uid'], $this->_fbp['sid'], 0);
148: 
149:         return true;
150:     }
151: 
152:     /**
153:      * Format notification content
154:      *
155:      * @param string $subject     Subject of message
156:      * @param string $body        Body of message
157:      *
158:      * @return string Formatted message
159:      */
160:     private function _formatBody($subject, $body)
161:     {
162:         return '<b>' . $subject . ':</b> '
163:                 . $GLOBALS['injector']->getInstance('Horde_Core_Factory_TextFilter')->filter($body, 'text2html', array('parselevel' => Horde_Text_Filter_Text2html::MICRO_LINKURL));
164:     }
165: }
166: 
API documentation generated by ApiGen