Skip to content
This repository was archived by the owner on Sep 10, 2021. It is now read-only.

Commit c6f4239

Browse files
author
Charles Marion
committed
ENH: added community invitation and a way to leave a community
1 parent 3a34e13 commit c6f4239

20 files changed

+438
-32
lines changed

core/AppController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@ public function preDispatch()
118118
{
119119
$this->view->recentItems = unserialize($cookieData);
120120
$check = $this->_getParam('checkRecentItem');
121-
// check if recent items exit (every 2 minutes)
122-
if(isset($check) || !isset($user->Dao->lastAction) || strtotime($user->Dao->lastAction) < strtotime("-2 minute"))
121+
// check if recent items exit (every 5 minutes)
122+
if(isset($check) || !isset($user->Dao->lastAction) || strtotime($user->Dao->lastAction) < strtotime("-5 minute"))
123123
{
124124
$modelLoad = new MIDAS_ModelLoader();
125125
$itemModel = $modelLoad->loadModel('Item');

core/constant/feed.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
define("MIDAS_FEED_CREATE_COMMUNITY", 0);
33
define("MIDAS_FEED_DELETE_COMMUNITY", 1);
44
define("MIDAS_FEED_UPDATE_COMMUNITY", 2);
5+
define("MIDAS_FEED_COMMUNITY_INVITATION", 3);
56

67
define("MIDAS_FEED_CREATE_USER", 10);
78

core/controllers/CommunityController.php

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/** Community Controller*/
33
class CommunityController extends AppController
44
{
5-
public $_models = array('Community', 'Folder', 'Group', 'Folderpolicygroup', 'Group', 'User', 'Feed', "Feedpolicygroup", "Feedpolicyuser", 'Item');
5+
public $_models = array('Community', 'Folder', 'Group', 'Folderpolicygroup', 'Group', 'User', 'Feed', "Feedpolicygroup", "Feedpolicyuser", 'Item', 'CommunityInvitation');
66
public $_daos = array('Community', 'Folder', 'Group', 'Folderpolicygroup', 'Group', 'User');
77
public $_components = array('Sortdao', 'Date');
88
public $_forms = array('Community');
@@ -243,12 +243,26 @@ function viewAction()
243243
throw new Zend_Exception("This community doesn't exist or you don't have the permissions.");
244244
}
245245
$joinCommunity = $this->_getParam('joinCommunity');
246+
$leaveCommunity = $this->_getParam('leaveCommunity');
246247
$canJoin = $communityDao->getCanJoin() == MIDAS_COMMUNITY_CAN_JOIN;
248+
249+
$this->view->isInvited = $this->CommunityInvitation->isInvited($communityDao, $this->userSession->Dao);
247250
$this->view->canJoin = $canJoin;
248-
if($canJoin && $this->userSession->Dao != null && isset($joinCommunity) && $communityDao->getPrivacy() == MIDAS_COMMUNITY_PUBLIC)
251+
if($this->userSession->Dao != null && isset($joinCommunity) && ($canJoin || $this->view->isInvited))
249252
{
250253
$member_group = $communityDao->getMemberGroup();
251254
$this->Group->addUser($member_group, $this->userSession->Dao);
255+
if($this->view->isInvited)
256+
{
257+
$this->CommunityInvitation->removeInvitation($communityDao, $this->userSession->Dao);
258+
}
259+
}
260+
261+
if($this->userSession->Dao != null && isset($leaveCommunity))
262+
{
263+
$member_group = $communityDao->getMemberGroup();
264+
$this->Group->removeUser($member_group, $this->userSession->Dao);
265+
$this->_redirect('/');
252266
}
253267

254268
$this->Community->incrementViewCount($communityDao);
@@ -278,6 +292,7 @@ function viewAction()
278292
$this->view->isModerator = $this->Community->policyCheck($communityDao, $this->userSession->Dao, MIDAS_POLICY_WRITE);
279293
$this->view->isAdmin = $this->Community->policyCheck($communityDao, $this->userSession->Dao, MIDAS_POLICY_ADMIN);
280294
$this->view->json['community'] = $communityDao->toArray();
295+
$this->view->json['community']['sendInvitation'] = $this->t('Send invitation');
281296

282297
if($this->view->isMember)
283298
{
@@ -310,6 +325,47 @@ function deleteAction()
310325
$this->_redirect('/');
311326
}//end delete
312327

328+
/** Invit user to a community*/
329+
function invitationAction()
330+
{
331+
$this->_helper->layout->disableLayout();
332+
333+
$communityId = $this->_getParam("communityId");
334+
if(!isset($communityId) || (!is_numeric($communityId) && strlen($communityId) != 32)) // This is tricky! and for Cassandra for now
335+
{
336+
throw new Zend_Exception("Community ID should be a number");
337+
}
338+
$communityDao = $this->Community->load($communityId);
339+
if($communityDao === false || !$this->Community->policyCheck($communityDao, $this->userSession->Dao, MIDAS_POLICY_WRITE))
340+
{
341+
throw new Zend_Exception("This community doesn't exist or you don't have the permissions.");
342+
}
343+
344+
if($this->_request->isPost())
345+
{
346+
$this->_helper->viewRenderer->setNoRender();
347+
$sendInvitation = $this->_getParam('sendInvitation');
348+
if(isset($sendInvitation))
349+
{
350+
$userId = $this->_getParam('userId');
351+
$userDao = $this->User->load($userId);
352+
if($userDao == false)
353+
{
354+
throw new Zend_Exception("Unable to find user.");
355+
}
356+
$invitation = $this->CommunityInvitation->createInvitation($communityDao, $this->userSession->Dao, $userDao);
357+
if($invitation == false)
358+
{
359+
echo JsonComponent::encode(array(false, $this->t('Error')));
360+
}
361+
else
362+
{
363+
echo JsonComponent::encode(array(true, $userDao->getFullName().' '.$this->t('has been invited')));
364+
}
365+
}
366+
}
367+
}//end delete
368+
313369
/** create a community (ajax)*/
314370
function createAction()
315371
{

core/controllers/SearchController.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,9 @@ public function liveAction()
7373

7474
$search = $this->getRequest()->getParam('term');
7575
$shareSearch = $this->getRequest()->getParam('shareSearch'); //return user group and communities
76+
$userSearch = $this->getRequest()->getParam('userSearch');
7677

77-
if(isset ($shareSearch))
78+
if(isset($shareSearch))
7879
{
7980
$ItemsDao = array();
8081
$FoldersDao = array();
@@ -105,6 +106,15 @@ public function liveAction()
105106
// Search for the users
106107
$UsersDao = $this->User->getUsersFromSearch($search, $this->userSession->Dao);
107108
}
109+
elseif(isset($userSearch))
110+
{
111+
$ItemsDao = array();
112+
$FoldersDao = array();
113+
$CommunitiesDao = array();
114+
$GroupsDao = array();
115+
// Search for the users
116+
$UsersDao = $this->User->getUsersFromSearch($search, $this->userSession->Dao);
117+
}
108118
else
109119
{
110120
// Search for the items

core/database/upgrade/3.0.11.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
class Upgrade_3_0_11 extends MIDASUpgrade
4+
{
5+
public function preUpgrade()
6+
{
7+
8+
}
9+
10+
public function mysql()
11+
{
12+
$sql = "
13+
CREATE TABLE IF NOT EXISTS `communityinvitation` (
14+
`communityinvitation_id` bigint(20) NOT NULL AUTO_INCREMENT,
15+
`community_id` bigint(20),
16+
`user_id` bigint(20),
17+
PRIMARY KEY (`communityinvitation_id`)
18+
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=10 ;
19+
";
20+
$this->db->query($sql);
21+
}
22+
23+
24+
public function pgsql()
25+
{
26+
$sql = "
27+
CREATE TABLE communityinvitation (
28+
communityinvitation_id serial PRIMARY KEY,
29+
community_id bigint,
30+
user_id bigint,
31+
)
32+
; ";
33+
$this->db->query($sql);
34+
}
35+
36+
public function postUpgrade()
37+
{
38+
39+
}
40+
}
41+
?>
42+
43+
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
/** ItemRevisionModelBase*/
3+
class CommunityInvitationModelBase extends AppModel
4+
{
5+
/** Constructor */
6+
public function __construct()
7+
{
8+
parent::__construct();
9+
$this->_name = 'communityinvitation';
10+
$this->_daoName = 'CommunityInvitationDao';
11+
$this->_key = 'communityinvitation_id';
12+
13+
$this->_mainData = array(
14+
'communityinvitation_id' => array('type' => MIDAS_DATA),
15+
'community_id' => array('type' => MIDAS_DATA),
16+
'user_id' => array('type' => MIDAS_DATA),
17+
'community' => array('type' => MIDAS_MANY_TO_ONE, 'model' => 'Community', 'parent_column' => 'community_id', 'child_column' => 'community_id'),
18+
'user' => array('type' => MIDAS_MANY_TO_ONE, 'model' => 'User', 'parent_column' => 'user_id', 'child_column' => 'user_id'),
19+
);
20+
$this->initialize(); // required
21+
} // end __construct()
22+
23+
/** create invitation */
24+
public function createInvitation($communityDao, $userDao, $invitedUserDao)
25+
{
26+
$invitations = $invitedUserDao->getInvitations();
27+
foreach($invitations as $invitation)
28+
{
29+
if($invitation->getCommunityId() == $communityDao->getKey())
30+
{
31+
return;
32+
}
33+
}
34+
35+
Zend_Loader::loadClass('CommunityInvitationDao', BASE_PATH.'/core/models/dao');
36+
$invitationDao = new CommunityInvitationDao();
37+
$invitationDao->setCommunityId($communityDao->getKey());
38+
$invitationDao->setUserId($invitedUserDao->getKey());
39+
$this->save($invitationDao);
40+
41+
$modelLoad = new MIDAS_ModelLoader();
42+
$feedModel = $modelLoad->loadModel('Feed');
43+
$feedpolicyuserModel = $modelLoad->loadModel('Feedpolicyuser');
44+
45+
$feed = $feedModel->createFeed($userDao, MIDAS_FEED_COMMUNITY_INVITATION, $invitationDao, $communityDao);
46+
$feedpolicyuserModel->createPolicy($invitedUserDao, $feed, MIDAS_POLICY_ADMIN);
47+
return $invitationDao;
48+
}
49+
50+
/** is user invited */
51+
public function isInvited($communityDao, $userDao)
52+
{
53+
if($userDao == null)
54+
{
55+
return false;
56+
}
57+
$invitations = $userDao->getInvitations();
58+
foreach($invitations as $invitation)
59+
{
60+
if($invitation->getCommunityId() == $communityDao->getKey())
61+
{
62+
return true;
63+
}
64+
}
65+
return false;
66+
}
67+
68+
/** remove invitation */
69+
public function removeInvitation($communityDao, $userDao)
70+
{
71+
if($userDao == null)
72+
{
73+
return;
74+
}
75+
$invitations = $userDao->getInvitations();
76+
foreach($invitations as $invitation)
77+
{
78+
if($invitation->getCommunityId() == $communityDao->getKey())
79+
{
80+
$modelLoad = new MIDAS_ModelLoader();
81+
$feedModel = $modelLoad->loadModel('Feed');
82+
$feeds = $feedModel->getFeedByResourceAndType(array(MIDAS_FEED_COMMUNITY_INVITATION), $invitation);
83+
foreach($feeds as $feed)
84+
{
85+
$feedModel->delete($feed);
86+
}
87+
$this->delete($invitation);
88+
return true;
89+
}
90+
}
91+
return;
92+
}
93+
} // end class ItemRevisionModelBase

core/models/base/CommunityModelBase.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public function __construct()
2727
'private_folder' => array('type' => MIDAS_MANY_TO_ONE, 'model' => 'Folder', 'parent_column' => 'privatefolder_id', 'child_column' => 'folder_id'),
2828
'admin_group' => array('type' => MIDAS_MANY_TO_ONE, 'model' => 'Group', 'parent_column' => 'admingroup_id', 'child_column' => 'group_id'),
2929
'moderator_group' => array('type' => MIDAS_MANY_TO_ONE, 'model' => 'Group', 'parent_column' => 'moderatorgroup_id', 'child_column' => 'group_id'),
30+
'invitations' => array('type' => MIDAS_ONE_TO_MANY, 'model' => 'CommunityInvitation', 'parent_column' => 'community_id', 'child_column' => 'community_id'),
3031
'member_group' => array('type' => MIDAS_MANY_TO_ONE, 'model' => 'Group', 'parent_column' => 'membergroup_id', 'child_column' => 'group_id'),
3132
'feeds' => array('type' => MIDAS_MANY_TO_MANY, 'model' => 'Feed', 'table' => 'feed2community', 'parent_column' => 'community_id', 'child_column' => 'feed_id'),
3233
);
@@ -197,6 +198,14 @@ function delete($communityDao)
197198
{
198199
$uuModel->delete($uudao);
199200
}
201+
202+
$ciModel = $modelLoad->loadModel('CommunityInvitation');
203+
$invitations = $communityDao->getInvitations();
204+
foreach($invitations as $invitation)
205+
{
206+
$ciModel->delete($invitation);
207+
}
208+
200209
parent::delete($communityDao);
201210
unset($communityDao->community_id);
202211
$communityDao->saved = false;
@@ -256,6 +265,15 @@ function policyCheck($communityDao, $userDao = null, $policy = 0)
256265
return true;
257266
}
258267
}
268+
269+
$invitations = $userDao->getInvitations();
270+
foreach($invitations as $invitation)
271+
{
272+
if($invitation->getCommunityId() == $communityDao->getKey())
273+
{
274+
return true;
275+
}
276+
}
259277
return false;
260278
}
261279
break;

core/models/base/FeedModelBase.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,13 @@ function createFeed($userDao, $type, $ressource, $communityDao = null)
7373
}
7474
$feed->setRessource($ressource->getKey());
7575
break;
76+
case MIDAS_FEED_COMMUNITY_INVITATION:
77+
if(!$ressource instanceof CommunityInvitationDao)
78+
{
79+
throw new Zend_Exception("Error parameter ressource, type:".$type);
80+
}
81+
$feed->setRessource($ressource->getKey());
82+
break;
7683
case MIDAS_FEED_CREATE_FOLDER:
7784
if(!$ressource instanceof FolderDao)
7885
{

core/models/base/UserModelBase.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public function __construct()
2727
'public_folder' => array('type' => MIDAS_MANY_TO_ONE, 'model' => 'Folder', 'parent_column' => 'publicfolder_id', 'child_column' => 'folder_id'),
2828
'private_folder' => array('type' => MIDAS_MANY_TO_ONE, 'model' => 'Folder', 'parent_column' => 'privatefolder_id', 'child_column' => 'folder_id'),
2929
'groups' => array('type' => MIDAS_MANY_TO_MANY, 'model' => 'Group', 'table' => 'user2group', 'parent_column' => 'user_id', 'child_column' => 'group_id'),
30+
'invitations' => array('type' => MIDAS_ONE_TO_MANY, 'model' => 'CommunityInvitation', 'parent_column' => 'user_id', 'child_column' => 'user_id'),
3031
'folderpolicyuser' => array('type' => MIDAS_ONE_TO_MANY, 'model' => 'Folderpolicyuser', 'parent_column' => 'user_id', 'child_column' => 'user_id'),
3132
'feeds' => array('type' => MIDAS_ONE_TO_MANY, 'model' => 'Feed', 'parent_column' => 'user_id', 'child_column' => 'user_id'),
3233
);
@@ -58,6 +59,14 @@ public function delete($dao)
5859
{
5960
$uuModel->delete($uudao);
6061
}
62+
63+
$ciModel = $modelLoad->loadModel('CommunityInvitation');
64+
$invitations = $dao->getInvitations();
65+
foreach($invitations as $invitation)
66+
{
67+
$ciModel->delete($invitation);
68+
}
69+
6170
parent::delete($dao);
6271
}// delete
6372

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
/**
3+
* \class CommunityInvitationDao
4+
* \brief DAO Assetstore
5+
*/
6+
class CommunityInvitationDao extends AppDao
7+
{
8+
public $_model = 'CommunityInvitation';
9+
10+
11+
} // end class
12+
?>

0 commit comments

Comments
 (0)