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_Friends:: defines an API for implementing storage backends for
  4:  * Folks.
  5:  *
  6:  * Copyright 2007-2012 Horde LLC (http://www.horde.org/)
  7:  *
  8:  * See the enclosed file COPYING for license information (GPL). If you
  9:  * did not receive this file, see http://www.horde.org/licenses/gpl.
 10:  *
 11:  * @author Duck <duck@obala.net>
 12:  * @package Folks
 13:  */
 14: class Folks_Friends_sql extends Folks_Friends {
 15: 
 16:     /**
 17:      * Handle for the current database connection.
 18:      *
 19:      * @var DB
 20:      */
 21:     private $_db;
 22: 
 23:     /**
 24:      * Handle for the current database connection, used for writing. Defaults
 25:      * to the same handle as $_db if a separate write database is not required.
 26:      *
 27:      * @var DB
 28:      */
 29:     private $_write_db;
 30: 
 31:     /**
 32:      * Constructs a new SQL storage object.
 33:      *
 34:      * @param array $params  A hash containing connection parameters.
 35:      */
 36:     protected function __construct($params = array())
 37:     {
 38:         parent::__construct($params);
 39: 
 40:         $this->_params = $params;
 41:         $this->_connect();
 42:     }
 43: 
 44:     /**
 45:      * Get user blacklist
 46:      *
 47:      * @return array of users blacklist
 48:      */
 49:     protected function _getBlacklist()
 50:     {
 51:         $query = 'SELECT friend_uid FROM ' . $this->_params['blacklist']
 52:                 . ' WHERE user_uid = ? '
 53:                 . ' ORDER BY friend_uid ASC';
 54: 
 55:         return $this->_db->getCol($query, 0, array($this->_user));
 56:     }
 57: 
 58:     /**
 59:      * Add user to a blacklist list
 60:      *
 61:      * @param string $user   Usersame
 62:      */
 63:     protected function _addBlacklisted($user)
 64:     {
 65:         $query = 'INSERT INTO ' . $this->_params['blacklist']
 66:                         . ' (user_uid, friend_uid) VALUES (?, ?)';
 67: 
 68:         return $this->_write_db->query($query, array($this->_user, $user));
 69:     }
 70: 
 71:     /**
 72:      * Remove user from blacklist list
 73:      *
 74:      * @param string $user   Usersame
 75:      */
 76:     protected function _removeBlacklisted($user)
 77:     {
 78:         $query = 'DELETE FROM ' . $this->_params['blacklist']
 79:                     . ' WHERE user_uid = ? AND friend_uid = ?';
 80: 
 81:         return $this->_write_db->query($query, array($this->_user, $user));
 82:     }
 83: 
 84:     /**
 85:      * Add user to a friend list
 86:      *
 87:      * @param string $friend   Friend's usersame
 88:      */
 89:     protected function _addFriend($friend)
 90:     {
 91:         $approve = $this->needsApproval($friend) ? 1 : 0;
 92: 
 93:         $query = 'INSERT INTO ' . $this->_params['friends']
 94:                 . ' (user_uid, friend_uid, friend_ask) VALUES (?, ?, ?)';
 95: 
 96:         return $this->_write_db->query($query, array($this->_user, $friend, $approve));
 97:     }
 98: 
 99:     /**
100:      * Approve our friend to add us to his userlist
101:      *
102:      * @param string $friend  Friedn username
103:      */
104:     protected function _approveFriend($friend)
105:     {
106:         $query = 'UPDATE ' . $this->_params['friends']
107:                 . ' SET friend_ask = ? WHERE user_uid = ? AND friend_uid = ?';
108: 
109:         $result = $this->_write_db->query($query, array(0, $friend, $this->_user));
110:         if ($result instanceof PEAR_Error) {
111:             return $result;
112:         }
113: 
114:         // Add user even to firend's friend list
115:         $query = 'REPLACE INTO ' . $this->_params['friends']
116:                 . ' (user_uid, friend_uid, friend_ask) VALUES (?, ?, ?)';
117: 
118:         return $this->_write_db->query($query, array($this->_user, $friend, 0));
119:     }
120: 
121:     /**
122:      * Remove user from a fiend list
123:      *
124:      * @param string $friend   Friend's usersame
125:      */
126:     protected function _removeFriend($friend)
127:     {
128:         $query = 'DELETE FROM ' . $this->_params['friends']
129:                     . ' WHERE user_uid = ? AND friend_uid = ?';
130: 
131:         return $this->_write_db->query($query, array($this->_user, $friend));
132:     }
133: 
134:     /**
135:      * Get user friends
136:      *
137:      * @return array of user's friends
138:      */
139:     protected function _getFriends()
140:     {
141:         $query = 'SELECT friend_uid FROM ' . $this->_params['friends']
142:                 . ' WHERE user_uid = ? and friend_ask = ?'
143:                 . ' ORDER BY friend_uid ASC';
144: 
145:         return $this->_db->getCol($query, 0, array($this->_user, 0));
146:     }
147: 
148:     /**
149:      * Get friends that does not confirm the current user yet
150:      */
151:     public function waitingApprovalFrom()
152:     {
153:         $query = 'SELECT friend_uid FROM ' . $this->_params['friends']
154:                 . ' WHERE user_uid = ? AND friend_ask = ?'
155:                 . ' ORDER BY friend_uid ASC';
156: 
157:         return $this->_db->getCol($query, 0, array($this->_user, 1));
158:     }
159: 
160:     /**
161:      * Get friends that does not confirm the current user yet
162:      */
163:     public function waitingApprovalFor()
164:     {
165:         $query = 'SELECT user_uid FROM ' . $this->_params['friends']
166:                 . ' WHERE friend_uid = ? AND friend_ask = ?'
167:                 . ' ORDER BY user_uid ASC';
168: 
169:         return $this->_db->getCol($query, 0, array($this->_user, 1));
170:     }
171: 
172:     /**
173:      * Get users who have you on friendlist
174:      *
175:      * @return array users
176:      */
177:     public function friendOf()
178:     {
179:         $query = 'SELECT user_uid FROM ' . $this->_params['friends']
180:                 . ' WHERE friend_uid = ? AND friend_ask = ?'
181:                 . ' ORDER BY friend_uid ASC';
182: 
183:         return $this->_db->getCol($query, 0, array($this->_user, 0));
184:     }
185: 
186:     /**
187:      * Get user groups
188:      */
189:     protected function _getGroups()
190:     {
191:         return array(_("Friends"));
192:     }
193: 
194:     /**
195:      * Attempts to open a persistent connection to the SQL server.
196:      *
197:      * @return boolean  True on success.
198:      * @throws Horde_Exception
199:      */
200:     protected function _connect()
201:     {
202:         $this->_params = array_merge(array(
203:             'blacklist' => 'folks_blacklist',
204:             'friends' => 'folks_friends'
205:         ), $this->_params);
206: 
207:         $this->_db = $GLOBALS['injector']->getInstance('Horde_Core_Factory_DbPear')->create('read', 'folks', 'storage');
208:         $this->_write_db = $GLOBALS['injector']->getInstance('Horde_Core_Factory_DbPear')->create('rw', 'folks', 'storage');
209: 
210:         return true;
211:     }
212: }
213: 
API documentation generated by ApiGen