private $user_id;
public function setUserId($user_id){
$this->user_id=(int)$user_id;
}
//on force le user_id avec la propriete de notre module
$oGroups->user_id=$this->user_id;
$oGroups->save();
$tGroups=model_Groups::getInstance()->findAll();
$tGroups=model_Groups::getInstance()->findListByUser( $this->user_id);
public function findListByUser($user_id){
return $this->findMany('SELECT * FROM '.$this->sTable.' WHERE user_id=?',(int)$user_id);
}
public function _friends(){
$oModuleGroups=new module_Groups();
$oModuleGroups->setUserId( _root::getAuth()->getAccount()->id );
$oViewGroups=$oModuleGroups->_index();
$this->oLayout->add('main',$oViewGroups);
}
private $user_id;
public function setUserId($user_id){
$this->user_id=(int)$user_id;
}
const STATE_PENDING=0;
const STATE_ACCEPTED=1;
const STATE_REFUSED=2;
public function findListAcceptedByUser($user_id){
return $this->findMany(
'SELECT *, Friends.id as friend_id FROM '.$this->sTable.',Users
WHERE
(Friends.state='.self::STATE_ACCEPTED.'
and Users.id=Friends.user_id2 and Friends.user_id=?)
or
(Friends.state='.self::STATE_ACCEPTED.'
and Users.id=Friends.user_id and Friends.user_id2=?)
'
,(int)$user_id
,(int)$user_id
);
}
public function findListPendingByUser($user_id){
return $this->findMany(
'SELECT * FROM '.$this->sTable.',Users
WHERE Users.id=Friends.user_id2
AND Friends.user_id=?
AND Friends.state=? '
,(int)$user_id
,self::STATE_PENDING
);
}
public function findListToValidateByUser($user_id){
return $this->findMany(
'SELECT *, Friends.id as friend_id
FROM '.$this->sTable.',Users
WHERE Users.id=Friends.user_id
AND Friends.user_id2=?
AND Friends.state=? '
,(int)$user_id
,self::STATE_PENDING
);
}
public function _list(){
$oViewContactsAccepted=$this->getList(model_Friends::getInstance()->findListAcceptedByUser($this->user_id));
$oViewContactsPending=$this->getList(model_Friends::getInstance()->findListPendingByUser($this->user_id));
$oViewContactsToValidate=$this->getList(model_Friends::getInstance()->findListToValidateByUser($this->user_id));
$oView=new _view('contacts::list');
$oView->oViewContactsAccepted=$oViewContactsAccepted;
$oView->oViewContactsPending=$oViewContactsPending;
$oView->oViewContactsToValidate=$oViewContactsToValidate;
return $oView;
}
public function getList($tContacts){
$oView=new _view('contacts::listembedded');
$oView->tContacts=$tContacts;
return $oView;
}
<?php if($this->tContacts):?>
<table>
<?php foreach($this->tContacts as $oContact):?>
<tr>
<td><?php echo $oContact->lastname ?></td>
<td><?php echo $oContact->firstname ?></td>
</tr>
<?php endforeach;?>
</table>
<?php else:?>
Aucun pour le moment
<?php endif;?>
<h1>Mes contacts</h1>
<?php echo $this->oViewContactsAccepted->show()?>
<h1>Mes demandes de contacts envoyées</h1>
<?php echo $this->oViewContactsPending->show()?>
<h1>Les demandes de contacts à valider</h1>
<?php echo $this->oViewContactsToValidate->show()?>
$oModuleContacts=new module_contacts();
$oModuleContacts->setUserId( _root::getAuth()->getAccount()->id );
$oViewContacts=$oModuleContacts->_list();
$this->oLayout->add('main',$oViewContacts);
public function findListByPattern($sPattern){
$sPattern='%'.$sPattern.'%';
return $this->findMany('SELECT * FROM '.$this->sTable.' WHERE lastname like ? or firstname like ?',$sPattern,$sPattern);
}
public function _find(){
$tUserFound=null;
if(_root::getRequest()->isPost() and _root::getParam('pattern')){
$tUserFound=model_Users::getInstance()->findListByPattern( _root::getParam('pattern') );
}
$oView=new _view('contacts::find');
$oView->tUserFound=$tUserFound;
return $oView;
}
<hr/>
<h1>Rechercher</h1>
<form action="" method="POST">
<p>Rechercher <input type="text" name="pattern" /> <input type="submit"
value="Rechercher"/></p>
</form>
<?php if(_root::getRequest()->isPost() and _root::getParam('pattern')):?>
<h1>Résultat(s) de recherche</h1>
<?php if($this->tUserFound):?>
<table>
<?php foreach($this->tUserFound as $oUserFound):?>
<tr>
<td><?php echo $oUserFound->lastname?></td>
<td><?php echo $oUserFound->firstname?></td>
</tr>
<?php endforeach;?>
</table>
<?php else:?>
<p>Aucun résultats</p>
<?php endif;?>
<?php endif;?>
$oViewFind=$oModuleContacts->_find();
$this->oLayout->add('main',$oViewFind);
public function _ask(){
if(_root::getParam('id')){
$oUserToAsk=model_Users::getInstance()->findById( _root::getParam('id' ));
if($oUserToAsk){
//si l'utilisateur existe, on cree une demande
$oNewContact=new row_Friends();
$oNewContact->user_id=_root::getAuth()->getAccount()->id;
$oNewContact->user_id2=$oUserToAsk->id;
$oNewContact->state=model_Friends::STATE_PENDING;
$oNewContact->save();
_root::redirect('mainPrivate::friends');
}
}
}
<hr/>
<h1>Rechercher</h1>
<form action="" method="POST">
<p>Rechercher <input type="text" name="pattern" /> <input type="submit"
value="Rechercher"/></p>
</form>
<?php if(_root::getRequest()->isPost() and _root::getParam('pattern')):?>
<h1>Résultat(s) de recherche</h1>
<?php if($this->tUserFound):?>
<table>
<?php foreach($this->tUserFound as $oUserFound):?>
<tr>
<td><?php echo $oUserFound->lastname?></td>
<td><?php echo $oUserFound->firstname?></td>
<td><a href="<?php echo _root::getLink('contacts::ask',
array('id'=>$oUserFound->id))
?>">demander en contact</a></td>
</tr>
<?php endforeach;?>
</table>
<?php else:?>
<p>Aucun résultats</p>
<?php endif;?>
<?php endif;?>
public function _accept(){
$this->user_id=_root::getAuth()->getAccount()->id;
$oContactAsked=model_Friends::getInstance()->findById( _root::getParam( 'id') );
if($oContactAsked and $oContactAsked->user_id2==$this->user_id){
//on check qu'on a bien trouve la demande et qu'elle nous est bien adresse
$oContactAsked->accept();
_root::redirect('mainPrivate::friends');
}
}
class row_Friends extends abstract_row{
protected $sClassModel='model_Friends';
(...)
public function accept(){
model_Friends::getInstance()->accept($this);
}
}
class model_Friends extends abstract_model{
(...)
public function accept($oContactAsked){
$oContactAsked->state=self::STATE_ACCEPTED;
$oContactAsked->save();
}
class row_Friends extends abstract_row{
protected $sClassModel='model_Friends';
public function accept(){
model_Friends::getInstance()->accept($this);
}
public function refuse(){
model_Friends::getInstance()->refuse($this);
}
(...)
}
class model_Friends extends abstract_model{
protected $sClassRow='row_Friends';
protected $sTable='Friends';
protected $sConfig='socialnetwork';
protected $tId=array('id');
const STATE_PENDING=0;
const STATE_ACCEPTED=1;
const STATE_REFUSED=2;
(...)
public function accept($oContactAsked){
$oContactAsked->state=self::STATE_ACCEPTED;
$oContactAsked->save();
}
public function refuse($oContactAsked){
$oContactAsked->state=self::STATE_REFUSED;
$oContactAsked->save();
}
}
public function _refuse(){
$this->user_id=_root::getAuth()->getAccount()->id;
$oContactAsked=model_Friends::getInstance()->findById( _root::getParam('id') );
if($oContactAsked and $oContactAsked->user_id2==$this->user_id){
//on check qu'on a bien trouve la demande et qu'elle nous est bien adresse
$oContactAsked->refuse();
_root::redirect('mainPrivate::friends');
}
}
public function _list(){
$oViewContactsAccepted=$this->getList(model_Friends::getInstance()->findListAcceptedByUser($this->user_id));
$oViewContactsPending=$this->getList(model_Friends::getInstance()->findListPendingByUser($this->user_id));
$oViewContactsToValidate=$this->getListToValidate(model_Friends::getInstance()->findListToValidateByUser($this->user_id));
$oView=new _view('contacts::list');
$oView->oViewContactsAccepted=$oViewContactsAccepted;
$oView->oViewContactsPending=$oViewContactsPending;
$oView->oViewContactsToValidate=$oViewContactsToValidate;
return $oView;
}
public function getList($tContacts){
$oView=new _view('contacts::listembedded');
$oView->tContacts=$tContacts;
return $oView;
}
public function getListToValidate($tContacts){
$oView=new _view('contacts::listembeddedToValidate');
$oView->tContacts=$tContacts;
return $oView;
}
<?php if($this->tContacts):?>
<table>
<?php foreach($this->tContacts as $oContact):?>
<tr>
<td><?php echo $oContact->lastname ?></td>
<td><?php echo $oContact->firstname ?></td>
<td>
<a href="<?php echo _root::getLink('contacts::accept',
array('id'=>$oContact->friend_id))
?>">Accepter</a>
</td>
<td>
<a href="<?php echo _root::getLink('contacts::refuse',
array('id'=>$oContact->friend_id))
?>">Refuser</a>
</td>
</tr>
<?php endforeach;?>
</table>
<?php else:?>
Aucun pour le moment
<?php endif;?>
public function _show(){
$oGroups=model_Groups::getInstance()->findById( module_Groups::getParam('id') );
//recuperation de la liste des contacts
$tContacts=model_Friends::getInstance()->findListAcceptedByUser($this->user_id);
//recuperation d'un tableau contenant les membres de ce groupe
$tIndexedMember=model_UsersGroup::getInstance()->findIndexedMemberTabByGroup( $oGroups->id);
$oView=new _view('Groups::show');
$oView->oGroups=$oGroups;
$oView->tContacts=$tContacts;
$oView->tIndexedMember=$tIndexedMember;
return $oView;
}
public function findIndexedMemberTabByGroup($id){
$tMember=$this->findMany('SELECT user_id FROM UsersGroup WHERE group_id=?',$id);
$tIndexed=array();
if($tMember){
foreach($tMember as $oMember){
$tIndexed[ $oMember->user_id ]=$oMember->user_id;
}
}
return $tIndexed;
}
<h1><?php echo $this->oGroups->name ?></h1>
<form action="" method="POST">
<?php if($this->tContacts):?>
<?php foreach($this->tContacts as $oContact):?>
<input <?php if(isset($this->tIndexedMember[ $oContact->id])):
?>checked="checked"<?php
endif;?> type="checkbox" name="tContactId[]" value="<?php echo $oContact->id?>" /> <?php echo $oContact->lastname ?> <?php echo $oContact->firstname ?><br />
<?php endforeach;?>
<?php endif;?>
<input type="submit" value="Modifier"/>
</form>
public function _show(){
$oGroups=model_Groups::getInstance()->findById( module_Groups::getParam('id') );
if(_root::getRequest()->isPost() and _root::getParam('tContactId')){
model_UsersGroup::getInstance()->updateMemberForGroupWithTab($oGroups->id,_root::getParam('tContactId'));
}
//recuperation de la liste des contacts
$tContacts=model_Friends::getInstance()->findListAcceptedByUser($this->user_id);
//recuperation d'un tableau contenant les membres de ce groupe
$tIndexedMember=model_UsersGroup::getInstance()->findIndexedMemberTabByGroup( $oGroups->id);
$oView=new _view('Groups::show');
$oView->oGroups=$oGroups;
$oView->tContacts=$tContacts;
$oView->tIndexedMember=$tIndexedMember;
return $oView;
}
public function updateMemberForGroupWithTab($group_id,$tContact){
$this->execute('DELETE FROM UsersGroup WHERE group_id=?',(int)$group_id);
if($tContact)
foreach($tContact as $user_id){
$oUsersGroup=new row_UsersGroup;
$oUsersGroup->group_id=$group_id;
$oUsersGroup->user_id=$user_id;
$oUsersGroup->save();
}
}
Lire la suite : VII Partie privée : votre fil