/**
  * appends sql to given select statement
  *
  * @param Tinebase_Backend_Sql_Filter_GroupSelect $_select
  * @param Tinebase_Backend_Sql_Abstract $_backend
  */
 public function appendFilterSql($_select, $_backend)
 {
     $db = $_backend->getAdapter();
     $value = $this->_value ? 1 : 0;
     if ($value) {
         // nothing to do -> show all lists!
         if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
             Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Query all lists.');
         }
     } else {
         if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) {
             Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' Only query visible lists.');
         }
         $_select->join(array('groupvisibility' => $db->table_prefix . 'groups'), $db->quoteIdentifier('groupvisibility.list_id') . ' = ' . $db->quoteIdentifier('addressbook_lists.id'), array());
         $_select->where($db->quoteIdentifier('groupvisibility.visibility') . ' = ?', 'displayed');
     }
 }
 /**
  * appends container_acl sql 
  * 
  * @param  Zend_Db_Select    $_select
  * @param  String            $_accountId
  * @param  Array|String      $_grant
  * @param  String            $_aclTableName
  * @param  bool              $_andGrants
  * @return void
  */
 public static function addGrantsSql($_select, $_accountId, $_grant, $_aclTableName = 'container_acl', $_andGrants = FALSE, $joinCallBack = NULL)
 {
     $accountId = $_accountId instanceof Tinebase_Record_Abstract ? $_accountId->getId() : $_accountId;
     $db = $_select->getAdapter();
     $grants = is_array($_grant) ? $_grant : array($_grant);
     $groupMemberships = Tinebase_Group::getInstance()->getGroupMemberships($accountId);
     $quotedActId = $db->quoteIdentifier("{$_aclTableName}.account_id");
     $quotedActType = $db->quoteIdentifier("{$_aclTableName}.account_type");
     $accountSelect = new Tinebase_Backend_Sql_Filter_GroupSelect($_select);
     $accountSelect->orWhere("{$quotedActId} = ? AND {$quotedActType} = " . $db->quote(Tinebase_Acl_Rights::ACCOUNT_TYPE_USER), $accountId)->orWhere("{$quotedActId} IN (?) AND {$quotedActType} = " . $db->quote(Tinebase_Acl_Rights::ACCOUNT_TYPE_GROUP), empty($groupMemberships) ? ' ' : $groupMemberships);
     if (!Tinebase_Config::getInstance()->get(Tinebase_Config::ANYONE_ACCOUNT_DISABLED)) {
         $accountSelect->orWhere("{$quotedActType} = ?", Tinebase_Acl_Rights::ACCOUNT_TYPE_ANYONE);
     }
     $accountSelect->appendWhere(Zend_Db_Select::SQL_AND);
     // we only need to filter, if the filter does not contain %
     if (!in_array('*', $grants)) {
         // @todo fetch wildcard from specific db adapter
         $grants = str_replace('*', '%', $grants);
         $quotedGrant = $db->quoteIdentifier($_aclTableName . '.account_grant');
         $iteration = 0;
         $grantsSelect = new Tinebase_Backend_Sql_Filter_GroupSelect($_select);
         foreach ($grants as $grant) {
             if ($_andGrants) {
                 if ($iteration > 0) {
                     $callbackIdentifier = call_user_func($joinCallBack, $_select, $iteration);
                     $grantsSelect->where($db->quoteIdentifier($callbackIdentifier . '.account_grant') . ' LIKE ?', $grant);
                 } else {
                     $grantsSelect->where($quotedGrant . ' LIKE ?', $grant);
                 }
                 ++$iteration;
             } else {
                 $grantsSelect->orWhere($quotedGrant . ' LIKE ?', $grant);
             }
         }
         // admin grant includes all other grants
         if (!in_array(Tinebase_Model_Grants::GRANT_ADMIN, $grants)) {
             $grantsSelect->orWhere($quotedGrant . ' LIKE ?', Tinebase_Model_Grants::GRANT_ADMIN);
         }
         $grantsSelect->appendWhere(Zend_Db_Select::SQL_AND);
     }
 }