/**
  * add foreign table joins
  * 
  * @param Zend_Db_Select $_select
  * @param array|string $_cols columns to get, * per default
  * 
  * @todo joining the same table twice with same name but different "on"'s is not possible currently
  */
 protected function _addForeignTableJoins(Zend_Db_Select $_select, $_cols, $_groupBy = NULL)
 {
     if (!empty($this->_foreignTables)) {
         $groupBy = $_groupBy !== NULL ? $_groupBy : $this->_tableName . '.' . $this->_identifier;
         $_select->group($groupBy);
         $cols = (array) $_cols;
         foreach ($this->_foreignTables as $foreignColumn => $join) {
             // only join if field is in cols
             if (in_array('*', $cols) || (isset($cols[$foreignColumn]) || array_key_exists($foreignColumn, $cols))) {
                 if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) {
                     Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' foreign column: ' . $foreignColumn);
                 }
                 $selectArray = isset($join['select']) || array_key_exists('select', $join) ? $join['select'] : ((isset($join['field']) || array_key_exists('field', $join)) && (!(isset($join['singleValue']) || array_key_exists('singleValue', $join)) || !$join['singleValue']) ? array($foreignColumn => $this->_dbCommand->getAggregate($join['table'] . '.' . $join['field'])) : array($foreignColumn => $join['table'] . '.id'));
                 $joinId = isset($join['joinId']) ? $join['joinId'] : $this->_identifier;
                 // avoid duplicate columns => will be added again in the next few lines of code
                 $this->_removeColFromSelect($_select, $foreignColumn);
                 $from = $_select->getPart(Zend_Db_Select::FROM);
                 if (!isset($from[$join['table']])) {
                     $_select->joinLeft(array($join['table'] => $this->_tablePrefix . $join['table']), $this->_db->quoteIdentifier($this->_tableName . '.' . $joinId) . ' = ' . $this->_db->quoteIdentifier($join['table'] . '.' . $join['joinOn']), $selectArray);
                 } else {
                     // join is defined already => just add the column
                     $_select->columns($selectArray, $join['table']);
                 }
             }
         }
     }
 }
 /**
  * returns all contexts of a given tag
  *
  * @param  string $_tagId
  * @return array  array of application ids
  */
 public function getContexts($_tagId)
 {
     $select = $this->_db->select()->from(array('tags_context' => SQL_TABLE_PREFIX . 'tags_context'), array('application_id' => $this->_dbCommand->getAggregate('application_id')))->where($this->_db->quoteInto($this->_db->quoteIdentifier('tag_id') . ' = ?', $_tagId))->group('tag_id');
     Tinebase_Backend_Sql_Abstract::traitGroup($select);
     $apps = $this->_db->fetchOne($select);
     if ($apps === '0') {
         $apps = 'any';
     }
     if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
         Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' got tag contexts: ' . $apps);
     }
     return explode(',', $apps);
 }