Skip to content

Commit

Permalink
[BF] Fix contact list by role when cardinality of the set is zero - f…
Browse files Browse the repository at this point in the history
…ixes #142
  • Loading branch information
barryo committed Apr 16, 2014
1 parent 614736a commit 9c0670d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 17 deletions.
32 changes: 18 additions & 14 deletions application/Repositories/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,35 @@ class Contact extends EntityRepository
{
/**
* Gets role names array for contacts.
*
*
* Function gets arrays of role names for contacts by given contacts
* id list. Return sturcture:
* $array = [
* $array = [
* contact_id0 => [ name0, name1, ..],
* contact_id1 => [ name0, name1, ..],
* ...
* ...
* ];
*
* @param array $ids Contacts ID list to get roles names
* @return array
*/
public function getRolesByIds( $ids )
{
if( !count( $ids ) )
return [];

$qb = $this->getEntityManager()->createQueryBuilder()
->select( 'c.id as contact_id, cg.name as name' )
->from( '\\Entities\\Contact', 'c' )
->leftJoin( 'c.Groups', 'cg' );

$qb->add( 'where', $qb->expr()->in( 'c.id', '?1' ) )
->andWhere( 'cg.type = ?2' )
->setParameter( 1, $ids )
->setParameter( 2, \Entities\ContactGroup::TYPE_ROLE );

$data = [];

foreach( $qb->getQuery()->getResult() as $row )
$data[ $row['contact_id'] ][] = $row['name'];

Expand All @@ -47,13 +51,13 @@ public function getRolesByIds( $ids )

/**
* Gets group types and names array for contacts.
*
*
* Function gets arrays of group types and names for contacts by given
* contacts id list. Return sturcture:
* $array = [
* $array = [
* contact_id0 => [ [ name => name0, type => type0 ], [ name => name1, type => type1 ], ..],
* contact_id1 => [ [ name => name0, type => type0 ], [ name => name1, type => type1 ], ..],
* ...
* ...
* ];
*
* @param array $ids Contacts ID list to get roles names
Expand All @@ -65,7 +69,7 @@ public function getGroupsByIds( $ids )
->select( 'c.id as contact_id, cg.name as name, cg.type as type' )
->from( '\\Entities\\Contact', 'c' )
->leftJoin( 'c.Groups', 'cg' );

$qb->add( 'where', $qb->expr()->in( 'c.id', '?1' ) )
->andWhere( 'cg.type <> ?2' )
->setParameter( 1, $ids )
Expand All @@ -80,19 +84,19 @@ public function getGroupsByIds( $ids )



/**
/**
* Find contacts by username
*
* Will support a username starts / ends with as it uses LIKE
*
*
* @param string $username The username to search for
* @return \Entities\Contact[] Matching contacts
*/
public function findByUsername( $username )
{
return $this->getEntityManager()->createQuery(
"SELECT c
FROM \\Entities\\Contact c
LEFT JOIN c.User u
Expand All @@ -102,17 +106,17 @@ public function findByUsername( $username )
->getResult();
}

/**
/**
* Find contacts by contact / user email
*
*
* @param string $email The email to search for
* @return \Entities\Contact[] Matching contacts
*/
public function findByEmail( $email )
{
return $this->getEntityManager()->createQuery(
"SELECT c
FROM \\Entities\\Contact c
LEFT JOIN c.User u
Expand Down
7 changes: 4 additions & 3 deletions application/controllers/ContactController.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ protected function listGetData( $id = null )
return $data;

$data = $this->setRolesAndGroups( $data, $id );

return $data;
}

Expand All @@ -196,6 +196,7 @@ private function setRolesAndGroups( $data, $id )
$ids[] = $row['id'];

$roles = $this->getD2R( '\\Entities\\Contact' )->getRolesByIds( $ids );

if( $id !== null )
$groups = $this->getD2R( '\\Entities\\Contact' )->getGroupsByIds( $ids );

Expand Down Expand Up @@ -764,7 +765,7 @@ private function _processUser( $form, $contact, $isEdit )
if( $form->getValue( "privs" ) == \Entities\User::AUTH_SUPERUSER )
$user->setPreference( 'customer-notes.read_upto', time() );
}

$this->getD2EM()->persist( $user );
$this->_feParams->userStatus = "created";
}
Expand All @@ -785,7 +786,7 @@ private function _processUser( $form, $contact, $isEdit )
OSS_Auth_Password::hash( $form->getValue( "password" ), $this->_options['resources']['auth']['oss'] )
);
}

$user->setUsername( $form->getValue( "username" ) );
$user->setPrivs( $form->getValue( "privs" ) );
}
Expand Down

0 comments on commit 9c0670d

Please sign in to comment.