* [Friends] Members can now apply to be added to the contact list.
[pivip.git] / project / modules / friends / Module.php
blob2f6600a6776597829632cf3e75f80612e0c10341
1 <?php
3 /**
4 * Pivip
5 * Copyright (C) 2008 Vincent Tunru
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
20 * @license http://www.fsf.org/licensing/licenses/info/GPLv2.html GPL v.2
21 * @category PivipModulesDefault
22 * @package Module_Friends
23 * @copyright (C) 2008 Vincent Tunru
24 * @author Vincent Tunru <email@vincentt.org>
27 /**
28 * Manage the Friends module
30 * @see /library/Pivip/Module/Abstract.php
32 class Friends_Module extends Pivip_Module_Abstract
34 /**
35 * Checks whether the Friends module is installed
37 * The Friends module is installed if the database tables have been created.
39 * @return boolean Whether Friends is already installed.
41 public static function isInstalled()
43 if(!file_exists(CODE_PATH
44 . 'application/models/generated/BaseFriend.php'))
46 return false;
48 try
50 $friendsTable = Doctrine::getTable('Friend');
51 } catch(Exception $e) {
52 return false;
54 return true;
57 /**
58 * @return boolean False
60 public static function needsConfiguring()
62 return false;
65 /**
66 * Creates the table in the database and perform the first authentication
68 * @throws Pivip_Install_Exception
69 * @return boolean Whether the installation succeeded
71 public function install()
73 if($this->isInstalled())
75 return;
77 $nextRequest = new Zend_Controller_Request_Simple('index',
78 'install',
79 'friends');
80 $this->_pushStack($nextRequest);
83 /**
84 * Remove the table from the database
86 * @throws Pivip_Install_Exception
87 * @return boolean Whether uninstallation succeeded
89 public function uninstall()
93 /**
94 * @return array A list of the blocks this module provides for normal pages,
95 * in the format:
96 * array('section' => array('Link to add this block'
97 => 'Block name'))
99 public static function getPageBlocks($location)
101 if(!self::isInstalled())
103 return array();
105 $translate = Zend_Registry::get('Zend_Translate');
106 $route = Zend_Controller_Front::getInstance()->getRouter()
107 ->getRoute('Friends_AddList');
108 $blocks = array();
109 $sections = array('sitecontext');
110 foreach($sections as $section)
112 $blocks[$section] = array($route->assemble(array('section' => $section,
113 'location'
114 => $location))
115 => $translate->_('Contact list'));
117 return $blocks;
121 * Load the generic cache for use by this module
123 * @return Zend_Cache
125 public static function loadCache()
127 $frontendOptions = array('automatic_serialization' => true);
128 $cacheConfig = Zend_Registry::get('cacheConfig');
129 $cache = Zend_Cache::factory('Core',
130 $cacheConfig->cache->backend,
131 $frontendOptions,
132 $cacheConfig->backendOptions->toArray());
133 return $cache;
137 * Load the module
139 * @todo Allow the admins to edit the ACL.
141 public function bootstrap()
143 // Only load if installed
144 if(!$this->isInstalled())
146 return;
148 // Define the Access Control List
149 $acl = Zend_Registry::get('acl');
150 $acl->add(new Zend_Acl_Resource('friendlist'))
151 ->allow('guest', 'friendlist', 'apply');
152 Zend_Registry::set('acl', $acl);
153 // Define the routes
154 $router = Zend_Controller_Front::getInstance()->getRouter();
155 $options = array('module' => 'friends', 'controller' => 'list',
156 'action' => 'add', 'location' => '');
157 $route = new Zend_Controller_Router_Route(
158 'contactlist/add/:section/:location', $options);
159 $router->addRoute('Friends_AddList', $route);
160 $options = array('module' => 'friends', 'controller' => 'list',
161 'action' => 'edit');
162 $route = new Zend_Controller_Router_Route_Static(
163 'contactlist/edit', $options);
164 $router->addRoute('Friends_EditList', $route);
165 $options = array('module' => 'friends', 'controller' => 'friend',
166 'action' => 'add');
167 $route = new Zend_Controller_Router_Route('contacts/apply',
168 $options);
169 $router->addRoute('Friends_ApplyAsFriend', $route);
170 $options = array('module' => 'friends', 'controller' => 'friend',
171 'action' => 'add', 'friend' => '');
172 $route = new Zend_Controller_Router_Route('contacts/add/:friend',
173 $options);
174 $router->addRoute('Friends_AddFriend', $route);
175 $options = array('module' => 'friends', 'controller' => 'friend',
176 'action' => 'edit');
177 $route = new Zend_Controller_Router_Route('contacts/edit/:friend',
178 $options);
179 $router->addRoute('Friends_EditFriend', $route);
180 $options = array('module' => 'friends', 'controller' => 'friend',
181 'action' => 'delete');
182 $route = new Zend_Controller_Router_Route('contacts/delete/:friend',
183 $options);
184 $router->addRoute('Friends_DeleteFriend', $route);
185 $options = array('module' => 'friends', 'controller' => 'friend',
186 'action' => 'rest');
187 $route = new Zend_Controller_Router_Route('contacts/rest/*', $options);
188 $router->addRoute('Friends_Rest', $route);