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: require_once dirname(__FILE__) . '/sql.php';
  4: 
  5: /**
  6:  * Folks_Friends:: defines an API for implementing storage backends for
  7:  * Folks.
  8:  *
  9:  * Copyright 2007-2012 Horde LLC (http://www.horde.org/)
 10:  *
 11:  * See the enclosed file COPYING for license information (GPL). If you
 12:  * did not receive this file, see http://www.horde.org/licenses/gpl.
 13:  *
 14:  * @author Duck <duck@obala.net>
 15:  * @package Folks
 16:  */
 17: class Folks_Friends_shared extends  Folks_Friends_sql {
 18: 
 19:     /**
 20:      * Share holder
 21:      *
 22:      * @var int
 23:      */
 24:     private $_shares;
 25: 
 26:     /**
 27:      * An array of capabilities, so that the driver can report which
 28:      * operations it supports and which it doesn't.
 29:      *
 30:      * @var array
 31:      */
 32:     protected $_capabilities = array('groups_add' => true);
 33: 
 34:     /**
 35:      * Get user owning group
 36:      *
 37:      * @param integer Get group ID
 38:      *
 39:      * @param string Owner
 40:      */
 41:     public function getGroupOwner($group)
 42:     {
 43:         $GLOBALS['folks_shares'] = $GLOBALS['injector']->getInstance('Horde_Core_Factory_Share')->create();
 44: 
 45:         try {
 46:             $share = $GLOBALS['folks_shares']->getShareById($group);
 47:         } catch (Horde_Share_Exception $e) {
 48:         }
 49: 
 50:         return $share->get('owner');
 51:     }
 52: 
 53:     /**
 54:      * Get user groups
 55:      */
 56:     protected function _getGroups()
 57:     {
 58:         $GLOBALS['folks_shares'] = $GLOBALS['injector']->getInstance('Horde_Core_Factory_Share')->create();
 59:         $groups = $GLOBALS['folks_shares']->listShares($this->_user, array('perm' => Horde_Perms::READ));
 60: 
 61:         $list = array();
 62:         foreach ($groups as $group) {
 63:             $list[$group->getId()] = $group->get('name');
 64:         }
 65: 
 66:         return $list;
 67:     }
 68: 
 69:     /**
 70:      * Rename user group
 71:      *
 72:      * @param integer $group   Group ID to delete
 73:      */
 74:     public function renameGroup($group, $name)
 75:     {
 76:         if (empty($name)) {
 77:             return PEAR::raiseError(_("A group names cannot be empty"));
 78:         }
 79: 
 80:         $GLOBALS['folks_shares'] = $GLOBALS['injector']->getInstance('Horde_Core_Factory_Share')->create();
 81:         $share = $GLOBALS['folks_shares']->getShareById($group);
 82: 
 83:         // Only owners of a group can delete them
 84:         if (!$GLOBALS['registry']->getAuth() ||
 85:             ($GLOBALS['registry']->getAuth() != $share->get('owner') &&
 86:              !$GLOBALS['registry']->isAdmin(array('permission' => 'folks:admin')))) {
 87:             return PEAR::raiseError("You can rename only your own groups.");
 88:         }
 89: 
 90:         $share->set('name', $name);
 91:         $result = $share->save();
 92:         if ($result instanceof PEAR_Error) {
 93:             return $result;
 94:         }
 95: 
 96:         $this->_cache->expire('folksGroups' . $this->_user);
 97: 
 98:         return true;
 99:     }
100: 
101:     /**
102:      * Delete user group
103:      *
104:      * @param integer $group   Group ID to delete
105:      */
106:     public function removeGroup($group)
107:     {
108:         $GLOBALS['folks_shares'] = $GLOBALS['injector']->getInstance('Horde_Core_Factory_Share')->create();
109: 
110:         $share = $GLOBALS['folks_shares']->getShareById($group);
111:         if ($share instanceof PEAR_Error) {
112:             return $share;
113:         }
114: 
115:         // Only owners of a group can delete them
116:         if (!$GLOBALS['registry']->getAuth() ||
117:             ($GLOBALS['registry']->getAuth() != $share->get('owner') &&
118:              !$GLOBALS['registry']->isAdmin(array('permission' => 'folks:admin')))) {
119:             return PEAR::raiseError("You can delete only your own groups.");
120:         }
121: 
122:         $query = 'DELETE FROM ' . $this->_params['friends']
123:                     . ' WHERE user_uid = ' . $share->getShareOb()->getWriteDb()->quote($this->_user)
124:                     . ' AND group_id = ' . $share->getShareOb()->getWriteDb()->quote($share->getId());
125: 
126:         $result = $share->getShareOb()->getWriteDb()->exec($query);
127:         if ($result instanceof PEAR_Error) {
128:             return $result;
129:         }
130: 
131:         $result = $GLOBALS['folks_shares']->removeShare($share);
132:         if ($result instanceof PEAR_Error) {
133:             return $result;
134:         }
135: 
136:         $this->_cache->expire('folksGroups' . $this->_user);
137:         $this->_cache->expire('folksFriends' . $this->_user . $group);
138: 
139:         return true;
140:     }
141: 
142:     /**
143:      * Add group
144:      *
145:      * @param string $group   Group name
146:      * @throws Horde_Share_Exception
147:      */
148:     public function addGroup($name)
149:     {
150:         if (empty($name)) {
151:             return PEAR::raiseError(_("A group names cannot be empty"));
152:         }
153: 
154:         $groups = $this->getGroups();
155:         if (in_array($name, $groups)) {
156:             return PEAR::raiseError(sprintf(_("You already have a group named \"%s\"."), $name));
157:         }
158: 
159:         $GLOBALS['folks_shares'] = $GLOBALS['injector']->getInstance('Horde_Core_Factory_Share')->create();
160: 
161:         $share = $GLOBALS['folks_shares']->newShare(strval(new Horde_Support_Uuid()), $name);
162:         $result = $GLOBALS['folks_shares']->addShare($share);
163: 
164:         return $share->getId();
165:     }
166: }
167: 
API documentation generated by ApiGen