Exemplo n.º 1
0
 /**
  * appends sql to given select statement
  * 
  * @param  Zend_Db_Select                    $_select
  * @param  Tinebase_Backend_Sql_Abstract     $_backend
  * @throws Tinebase_Exception_NotFound
  */
 public function appendFilterSql($_select, $_backend)
 {
     $this->_options['ignoreAcl'] = TRUE;
     $this->_resolve();
     $quotedDisplayContainerIdentifier = $_backend->getAdapter()->quoteIdentifier('attendee.displaycontainer_id');
     $where = empty($this->_containerIds) ? Tinebase_Backend_Sql_Command::getFalseValue($_backend->getAdapter()) : $_select->getAdapter()->quoteInto($this->_getQuotedFieldName($_backend) . ' IN (?)', $this->_containerIds);
     $orWhere = empty($this->_containerIds) ? Tinebase_Backend_Sql_Command::getFalseValue($_backend->getAdapter()) : $_select->getAdapter()->quoteInto($quotedDisplayContainerIdentifier . ' IN (?)', $this->_containerIds);
     $_select->where($where);
     $_select->orWhere($orWhere);
 }
 /**
  * appends sql to given select statement
  *
  * @param  Zend_Db_Select                    $_select
  * @param  Tinebase_Backend_Sql_Abstract     $_backend
  */
 public function appendFilterSql($_select, $_backend)
 {
     $correlationName = Tinebase_Record_Abstract::generateUID(30);
     $db = $_backend->getAdapter();
     $_select->joinLeft(array($correlationName => $db->table_prefix . 'adb_list_m_role'), $db->quoteIdentifier($correlationName . '.contact_id') . ' = ' . $db->quoteIdentifier('addressbook.id'), array());
     $_select->where($db->quoteIdentifier($correlationName . '.list_role_id') . ' IN (?)', (array) $this->_value);
 }
 /**
  * appends sql to given select statement
  *
  * @param Zend_Db_Select                $_select
  * @param Tinebase_Backend_Sql_Abstract $_backend
  */
 public function appendFilterSql($_select, $_backend)
 {
     $db = $_backend->getAdapter();
     // prepare value
     $value = $this->_value ? 1 : 0;
     if ($value) {
         // nothing to do -> show all contacts!
         if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
             Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Query all account contacts.');
         }
     } else {
         if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
             Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Only query visible and enabled account contacts.');
         }
         if (Tinebase_Core::getUser() instanceof Tinebase_Model_FullUser) {
             $where = '/* is no user */ ' . Tinebase_Backend_Sql_Command::getIfIsNull($db, $db->quoteIdentifier('accounts.id'), 'true', 'false') . ' OR /* is user */ (' . Tinebase_Backend_Sql_Command::getIfIsNull($db, $db->quoteIdentifier('accounts.id'), 'false', 'true') . ' AND ' . $db->quoteInto($db->quoteIdentifier('accounts.status') . ' = ?', 'enabled') . " AND " . '(' . $db->quoteInto($db->quoteIdentifier('accounts.visibility') . ' = ?', 'displayed') . ' OR ' . $db->quoteInto($db->quoteIdentifier('accounts.id') . ' = ?', Tinebase_Core::getUser()->getId()) . ')' . ")";
         } else {
             $where = '/* is no user */ ' . Tinebase_Backend_Sql_Command::getIfIsNull($db, $db->quoteIdentifier('accounts.id'), 'true', 'false') . ' OR /* is user */ (' . Tinebase_Backend_Sql_Command::getIfIsNull($db, $db->quoteIdentifier('accounts.id'), 'false', 'true') . ' AND ' . $db->quoteInto($db->quoteIdentifier('accounts.status') . ' = ?', 'enabled') . " AND " . $db->quoteInto($db->quoteIdentifier('accounts.visibility') . ' = ?', 'displayed') . ")";
         }
         $_select->where($where);
         $select = $_select instanceof Zend_Db_Select ? $_select : $_select->getSelect();
         $select = Tinebase_Backend_Sql_Abstract::traitGroup($db, $_backend->getTablePrefix(), $select);
         $_select instanceof Zend_Db_Select ? $_select = $select : $_select->setSelect($select);
         if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
             Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' contacts query ' . $_select->assemble());
         }
     }
 }
 /**
  * appends sql to given select statement
  *
  * @param Zend_Db_Select                $_select
  * @param Tinebase_Backend_Sql_Abstract $_backend
  */
 public function appendFilterSql($_select, $_backend)
 {
     if ($this->_value === self::VALUE_NOTSET) {
         return;
     }
     $action = $this->_opSqlMap[$this->_operator];
     $db = $_backend->getAdapter();
     // prepare value
     $value = $this->_value ? 1 : 0;
     if (!empty($this->_options['fields'])) {
         foreach ((array) $this->_options['fields'] as $fieldName) {
             $quotedField = $db->quoteIdentifier(strpos($fieldName, '.') === false ? $_backend->getTableName() . '.' . $fieldName : $fieldName);
             if ($value) {
                 $_select->where($quotedField . $action['sqlop'], $value);
             } else {
                 $_select->orwhere($quotedField . $action['sqlop'], $value);
             }
         }
     } else {
         if (!empty($this->_options['leftOperand'])) {
             $_select->where($this->_options['leftOperand'] . $action['sqlop'], $value);
         } else {
             $_select->where($this->_getQuotedFieldName($_backend) . $action['sqlop'], $value);
         }
     }
 }
 /**
  * appends sql to given select statement
  * 
  * @param  Zend_Db_Select                    $_select
  * @param  Tinebase_Backend_Sql_Abstract     $_backend
  * @throws Tinebase_Exception_NotFound
  */
 public function appendFilterSql2($_select, $_backend)
 {
     $this->_options['ignoreAcl'] = TRUE;
     $this->_resolve();
     $quotedDisplayContainerIdentifier = $_backend->getAdapter()->quoteIdentifier('attendee.displaycontainer_id');
     $_select->where($quotedDisplayContainerIdentifier . ' IN (?)', empty($this->_containerIds) ? " " : $this->_containerIds);
 }
 /**
  * appends sql to given select statement
  *
  * @param  Zend_Db_Select                    $_select
  * @param  Tinebase_Backend_Sql_Abstract     $_backend
  */
 public function appendFilterSql($_select, $_backend)
 {
     if ($this->_value === 'all') {
         $_select->where('1=1');
         return;
     }
     $gs = new Tinebase_Backend_Sql_Filter_GroupSelect($_select);
     $adapter = $_backend->getAdapter();
     $isExcept = strpos($this->_operator, 'Except') !== false;
     $sign = $isExcept ? '<>' : '=';
     //if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . ' (' . __LINE__ . ') value: ' . print_r($this->_value, true));
     foreach ($this->_value as $attenderValue) {
         if (in_array($attenderValue['user_type'], array(Calendar_Model_Attender::USERTYPE_USER, Calendar_Model_Attender::USERTYPE_GROUPMEMBER))) {
             // @todo user_id might contain filter in the future -> get userids from adressbook controller with contact filter
             // transform CURRENTCONTACT
             $attenderValue['user_id'] = $attenderValue['user_id'] == Addressbook_Model_Contact::CURRENTCONTACT ? Tinebase_Core::getUser()->contact_id : $attenderValue['user_id'];
             $attendee = array(array('user_type' => Calendar_Model_Attender::USERTYPE_USER, 'user_id' => $attenderValue['user_id']));
             if (!$isExcept) {
                 $attendee[] = array('user_type' => Calendar_Model_Attender::USERTYPE_GROUPMEMBER, 'user_id' => $attenderValue['user_id']);
             }
         } else {
             if ($attenderValue['user_type'] == self::USERTYPE_MEMBEROF) {
                 // resolve group members
                 $group = Tinebase_Group::getInstance()->getGroupById($attenderValue['user_id']);
                 $attendee = array();
                 // fetch list only if list_id is not NULL, otherwise we get back an empty list object
                 if (!empty($group->list_id)) {
                     $contactList = Addressbook_Controller_List::getInstance()->get($group->list_id);
                     foreach ($contactList->members as $member) {
                         $attendee[] = array('user_type' => Calendar_Model_Attender::USERTYPE_USER, 'user_id' => $member);
                         if (!$isExcept) {
                             $attendee[] = array('user_type' => Calendar_Model_Attender::USERTYPE_GROUPMEMBER, 'user_id' => $member);
                         }
                     }
                 }
             } else {
                 $attendee = array($attenderValue);
             }
         }
         foreach ($attendee as $attender) {
             $gs->orWhere(($isExcept ? '' : $adapter->quoteInto($adapter->quoteIdentifier('attendee.user_type') . ' = ?', $attender['user_type']) . ' AND ') . $adapter->quoteInto($adapter->quoteIdentifier('attendee.user_id') . ' ' . $sign . ' ?', $attender['user_id']));
         }
     }
     if (substr($this->_operator, 0, 3) === 'not') {
         // join attendee to be excluded as a new column. records having this column NULL don't have the attendee
         $dname = 'attendee-not-' . Tinebase_Record_Abstract::generateUID(5);
         $_select->joinLeft(array($dname => $_backend->getTablePrefix() . 'cal_attendee'), $adapter->quoteIdentifier($dname . '.cal_event_id') . ' = ' . $adapter->quoteIdentifier($_backend->getTableName() . '.id') . ' AND ' . $gs->getSQL(), array($dname => $_backend->getDbCommand()->getAggregate($dname . '.id')));
         $_select->having($_backend->getDbCommand()->getAggregate($dname . '.id') . ' IS NULL');
     } else {
         if ($isExcept) {
             $dname = 'attendee-hasSome-' . Tinebase_Record_Abstract::generateUID(5);
             $_select->joinLeft(array($dname => $_backend->getTablePrefix() . 'cal_attendee'), $adapter->quoteIdentifier($dname . '.cal_event_id') . ' = ' . $adapter->quoteIdentifier($_backend->getTableName() . '.id') . ' AND ' . $gs->getSQL(), array($dname => $_backend->getDbCommand()->getAggregate($dname . '.id')));
             $_select->having($_backend->getDbCommand()->getAggregate($dname . '.id') . ' IS NOT NULL');
         } else {
             $gs->appendWhere(Zend_Db_Select::SQL_OR);
         }
     }
 }
 /**
  * append grants acl filter
  * 
  * @param Zend_Db_Select $select
  * @param Tinebase_Backend_Sql_Abstract $backend
  * @param Tinebase_Model_User $user
  */
 protected function _appendGrantsFilter($select, $backend, $user)
 {
     $db = $backend->getAdapter();
     $select->join(array($this->_aclTableName => SQL_TABLE_PREFIX . $this->_aclTableName), "{$db->quoteIdentifier($this->_aclTableName . '.record_id')} = {$db->quoteIdentifier($backend->getTableName() . '.id')}", array());
     Tinebase_Container::addGrantsSql($select, $user, $this->_requiredGrants, $this->_aclTableName);
     if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) {
         Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' $select after appending grants sql: ' . $select);
     }
 }
Exemplo n.º 8
0
 /**
  * appends sql to given select statement
  *
  * @param  Zend_Db_Select                    $_select
  * @param  Tinebase_Backend_Sql_Abstract     $_backend
  * @throws Tinebase_Exception_NotFound
  */
 public function appendFilterSql($_select, $_backend)
 {
     $db = $_backend->getAdapter();
     $correlationName = Tinebase_Record_Abstract::generateUID() . $this->_value . 'owner';
     $_select->joinLeft(array($correlationName => SQL_TABLE_PREFIX . 'container_acl'), $db->quoteIdentifier("{$correlationName}.container_id") . " = container.id", array());
     // only personal containers have an owner!
     $_select->where("{$db->quoteIdentifier('container.type')} = ?", Tinebase_Model_Container::TYPE_PERSONAL);
     // assure admin grant
     $_select->where($db->quoteIdentifier("{$correlationName}.account_id") . " = " . $db->quote($this->_value) . ' AND ' . $db->quoteIdentifier("{$correlationName}.account_grant") . " = ?", Tinebase_Model_Grants::GRANT_ADMIN);
 }
 /**
  * appends sql to given select statement
  *
  * @param Zend_Db_Select                $_select
  * @param Tinebase_Backend_Sql_Abstract $_backend
  */
 public function appendFilterSql($_select, $_backend)
 {
     $db = $_backend->getAdapter();
     // prepare value
     $value = $this->_value ? 1 : 0;
     if ($value) {
         // nothing to filter
     } else {
         $_select->where($db->quoteIdentifier($_backend->getTableName() . '.is_open') . ' = 1');
     }
 }
Exemplo n.º 10
0
 /**
  * appends sql to given select statement
  *
  * @param Zend_Db_Select                $_select
  * @param Tinebase_Backend_Sql_Abstract $_backend
  */
 public function appendFilterSql($_select, $_backend)
 {
     if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
         Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' ' . 'Adding Relation filter: ' . $_backend->getModelName() . ' <-> ' . $this->_options['related_model']);
     }
     $this->_resolveForeignIds();
     $ownIds = $this->_getOwnIds($_backend->getModelName());
     $idField = array_key_exists('idProperty', $this->_options) ? $this->_options['idProperty'] : 'id';
     $db = $_backend->getAdapter();
     $qField = $db->quoteIdentifier($_backend->getTableName() . '.' . $idField);
     $_select->where($db->quoteInto("{$qField} IN (?)", empty($ownIds) ? ' ' : $ownIds));
 }
 /**
  * appends custom filters to a given select object
  * 
  * @param  Zend_Db_Select                    $_select
  * @param  Tinebase_Backend_Sql_Abstract     $_backend
  * @return void
  * 
  * @todo replace custom filter!
  */
 public function appendFilterSql($_select, $_backend)
 {
     $this->_appendAclSqlFilter($_select);
     $db = $_backend->getAdapter();
     foreach ($this->_customData as $customData) {
         $value = $customData['value'];
         if ($customData['field'] == 'is_cleared') {
             $_select->joinLeft(array('ta' => $db->table_prefix . 'timetracker_timeaccount'), $db->quoteIdentifier('ta.id') . ' = ' . $db->quoteIdentifier('timetracker_timesheet.timeaccount_id'), array());
             $opStatus = $value ? '=' : '<>';
             $op = $value ? ' OR ' : ' AND ';
             $_select->where($db->quoteInto($customData['field'] . ' = ?', $value) . $op . $db->quoteInto('ta.status' . $opStatus . ' ? ', 'billed'));
         }
     }
 }
 /**
  * appends sql to given select statement
  *
  * @param Zend_Db_Select                $_select
  * @param Tinebase_Backend_Sql_Abstract $_backend
  */
 public function appendFilterSql($_select, $_backend)
 {
     if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
         Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' ' . 'Adding Path filter for: ' . $_backend->getModelName());
     }
     $this->_resolvePathIds();
     $idField = isset($this->_options['idProperty']) || array_key_exists('idProperty', $this->_options) ? $this->_options['idProperty'] : 'id';
     $db = $_backend->getAdapter();
     $qField = $db->quoteIdentifier($_backend->getTableName() . '.' . $idField);
     if (empty($this->_pathRecordIds)) {
         $_select->where('1=0');
     } else {
         $_select->where($db->quoteInto("{$qField} IN (?)", $this->_pathRecordIds));
     }
 }
Exemplo n.º 13
0
 /**
  * appends sql to given select statement
  *
  * @param  Zend_Db_Select                    $_select
  * @param  Tinebase_Backend_Sql_Abstract     $_backend
  */
 public function appendFilterSql($_select, $_backend)
 {
     if ($this->_accountId === '0') {
         // get anyones preferences
         $field = $_backend->getAdapter()->quoteIdentifier($_backend->getTableName() . '.account_type');
         $_select->where(Tinebase_Core::getDb()->quoteInto($field . '= ?', Tinebase_Acl_Rights::ACCOUNT_TYPE_ANYONE));
     } else {
         $conditions = array(array('condition' => Tinebase_Model_Filter_FilterGroup::CONDITION_AND, 'filters' => array(array('field' => 'account_id', 'operator' => 'equals', 'value' => $this->_accountId), array('field' => 'account_type', 'operator' => 'equals', 'value' => $this->_accountType))), array('field' => 'account_type', 'operator' => 'equals', 'value' => Tinebase_Acl_Rights::ACCOUNT_TYPE_ANYONE));
         // add groups if accountType is user
         if ($this->_accountType === Tinebase_Acl_Rights::ACCOUNT_TYPE_USER) {
             $groups = Tinebase_Group::getInstance()->getGroupMemberships($this->_accountId);
             $conditions[] = array('condition' => Tinebase_Model_Filter_FilterGroup::CONDITION_AND, 'filters' => array(array('field' => 'account_id', 'operator' => 'in', 'value' => $groups), array('field' => 'account_type', 'operator' => 'equals', 'value' => Tinebase_Acl_Rights::ACCOUNT_TYPE_GROUP)));
         }
         $filter = new Tinebase_Model_PreferenceFilter($conditions, Tinebase_Model_Filter_FilterGroup::CONDITION_OR);
         Tinebase_Backend_Sql_Filter_FilterGroup::appendFilters($_select, $filter, $_backend);
     }
 }
 /**
  * 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');
     }
 }
Exemplo n.º 15
0
 /**
  * appends sql to given select statement
  * 
  * @param  Zend_Db_Select                    $_select
  * @param  Tinebase_Backend_Sql_Abstract     $_backend
  * @throws Tinebase_Exception_AccessDenied
  */
 public function appendFilterSql($_select, $_backend)
 {
     $db = $_backend->getAdapter();
     if ($this->_requiredGrants === NULL) {
         throw new Tinebase_Exception_AccessDenied('No grants have been defined.');
     }
     // it searches select columns
     // return an array where each element has 3 items: 0 - table, 1 - field/expression, 2 - alias
     $columns = $_select->getPart(Zend_Db_Select::COLUMNS);
     // it gets an array with expressions used in having clause
     $grants = array();
     foreach ($columns as $column) {
         if (!empty($column[2])) {
             $grants[$column[2]] = $column[1];
         }
     }
     // it uses into having clause expressions instead of aliases
     foreach ($this->_requiredGrants as $grant) {
         $_select->orHaving($db->quoteInto($db->quoteIdentifier($grants[$grant]) . ' = ?', 1, Zend_Db::INT_TYPE));
     }
 }
 /**
  * appends sql to given select statement
  *
  * @param Zend_Db_Select                $_select
  * @param Tinebase_Backend_Sql_Abstract $_backend
  */
 public function appendFilterSql($_select, $_backend)
 {
     $db = $_backend->getAdapter();
     // prepare value
     $value = $this->_value ? 1 : 0;
     if ($value) {
         // nothing to do -> show all contacts!
         if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
             Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Query all account contacts.');
         }
     } else {
         if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) {
             Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' Only query visible and enabled (expired, blocked) account contacts.');
         }
         if (Tinebase_Core::getUser() instanceof Tinebase_Model_FullUser) {
             $where = '/* is no user */ ' . $db->quoteIdentifier('accounts.id') . ' IS NULL OR /* is user */ ' . '(' . $db->quoteIdentifier('accounts.id') . ' IS NOT NULL AND ' . $db->quoteInto($db->quoteIdentifier('accounts.status') . ' != ?', 'disabled') . " AND " . '(' . $db->quoteInto($db->quoteIdentifier('accounts.visibility') . ' = ?', 'displayed') . ' OR ' . $db->quoteInto($db->quoteIdentifier('accounts.id') . ' = ?', Tinebase_Core::getUser()->getId()) . ')' . ")";
         } else {
             $where = '/* is no user */ ' . $db->quoteIdentifier('accounts.id') . ' IS NULL OR /* is user */ ' . '(' . $db->quoteIdentifier('accounts.id') . ' IS NOT NULL AND ' . $db->quoteInto($db->quoteIdentifier('accounts.status') . ' != ?', 'disabled') . " AND " . $db->quoteInto($db->quoteIdentifier('accounts.visibility') . ' = ?', 'displayed') . ")";
         }
         $_select->where($where);
     }
 }
Exemplo n.º 17
0
 /**
  * appends sql to given select statement
  *
  * @param  Zend_Db_Select                    $_select
  * @param  Tinebase_Backend_Sql_Abstract     $_backend
  */
 public function appendFilterSql($_select, $_backend)
 {
     if ($this->_value === 'all') {
         $_select->where('1=1');
         return;
     }
     $gs = new Tinebase_Backend_Sql_Filter_GroupSelect($_select);
     $adapter = $_backend->getAdapter();
     //if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . ' (' . __LINE__ . ') value: ' . print_r($this->_value, true));
     foreach ($this->_value as $attenderValue) {
         if (in_array($attenderValue['user_type'], array(Calendar_Model_Attender::USERTYPE_USER, Calendar_Model_Attender::USERTYPE_GROUPMEMBER))) {
             // @todo user_id might contain filter in the future -> get userids from adressbook controller with contact filter
             // transform CURRENTCONTACT
             $attenderValue['user_id'] = $attenderValue['user_id'] == Addressbook_Model_Contact::CURRENTCONTACT ? Tinebase_Core::getUser()->contact_id : $attenderValue['user_id'];
             $attendee = array(array('user_type' => Calendar_Model_Attender::USERTYPE_USER, 'user_id' => $attenderValue['user_id']), array('user_type' => Calendar_Model_Attender::USERTYPE_GROUPMEMBER, 'user_id' => $attenderValue['user_id']));
         } else {
             if ($attenderValue['user_type'] == self::USERTYPE_MEMBEROF) {
                 // resolve group members
                 $group = Tinebase_Group::getInstance()->getGroupById($attenderValue['user_id']);
                 $attendee = array();
                 // fetch list only if list_id is not NULL, otherwise we get back an empty list object
                 if (!empty($group->list_id)) {
                     $contactList = Addressbook_Controller_List::getInstance()->get($group->list_id);
                     foreach ($contactList->members as $member) {
                         $attendee[] = array('user_type' => Calendar_Model_Attender::USERTYPE_USER, 'user_id' => $member);
                         $attendee[] = array('user_type' => Calendar_Model_Attender::USERTYPE_GROUPMEMBER, 'user_id' => $member);
                     }
                 }
             } else {
                 $attendee = array($attenderValue);
             }
         }
         foreach ($attendee as $attender) {
             $gs->orWhere($adapter->quoteInto($adapter->quoteIdentifier('attendee.user_type') . ' = ?', $attender['user_type']) . ' AND ' . $adapter->quoteInto($adapter->quoteIdentifier('attendee.user_id') . ' = ?', $attender['user_id']));
         }
     }
     $gs->appendWhere(Zend_Db_Select::SQL_OR);
 }
 /**
  * appends sql to given select statement
  *
  * @param Zend_Db_Select                $_select
  * @param Tinebase_Backend_Sql_Abstract $_backend
  */
 function appendFilterSql($_select, $_backend)
 {
     $months = array();
     $db = $_backend->getAdapter();
     $date = new Tinebase_DateTime();
     $format = 'Y-m';
     $like = FALSE;
     if ($this->_operator == 'within') {
         switch ($this->_value) {
             case 'monthThis':
                 $months = array($date->format($format));
                 break;
             case 'monthLast':
                 $months = array($date->subMonth(1)->format($format));
                 break;
             case 'beforeLastMonth':
                 $months = array($date->subMonth(2)->format($format));
                 break;
             case 'quarterThis':
                 $month = ceil(intval($date->format('m')) / 3) * 3;
                 $date->setDate($date->format('Y'), $month, 15);
                 $months = array($date->format($format), $date->subMonth(1)->format($format), $date->subMonth(1)->format($format));
                 break;
             case 'quarterLast':
                 $date->subMonth(3);
                 $month = ceil(intval($date->format('m')) / 3) * 3;
                 $date->setDate($date->format('Y'), $month, 15);
                 $months = array($date->format($format), $date->subMonth(1)->format($format), $date->subMonth(1)->format($format));
                 break;
             case 'beforeLastQuarter':
                 $date->subMonth(6);
                 $month = ceil(intval($date->format('m')) / 3) * 3;
                 $date->setDate($date->format('Y'), $month, 15);
                 $months = array($date->format($format), $date->subMonth(1)->format($format), $date->subMonth(1)->format($format));
                 break;
             case 'yearThis':
                 $like = $date->format('Y') . '-%';
                 break;
             case 'yearLast':
                 $date->subYear(1);
                 $like = $date->format('Y') . '-%';
                 break;
             default:
                 throw new Tinebase_Exception_InvalidArgument('The value for the within operator is not supported: ' . $this->_value);
         }
         if ($like) {
             $_select->where($db->quoteInto($this->_getQuotedFieldName($_backend) . " LIKE (?)", $like));
         } else {
             $_select->where($db->quoteInto($this->_getQuotedFieldName($_backend) . " IN (?)", $months));
         }
     } elseif ($this->_operator == 'equals') {
         if ('' == $this->_value) {
             $_select->where($this->_getQuotedFieldName($_backend) . " = '0000-00-00 00:00:00' OR " . $this->_getQuotedFieldName($_backend) . ' IS NULL');
         } else {
             $split = explode('-', $this->_value);
             if (!(strlen($this->_value) == 7 && (int) $split[0] > 1900 && (int) $split[1] > 0 && (int) $split[1] < 13)) {
                 throw new Tinebase_Exception_MonthFormat();
             }
             $_select->where($db->quoteInto($this->_getQuotedFieldName($_backend) . " = (?)", $this->_value));
         }
     } else {
         $date = new Tinebase_DateTime($this->_value);
         $date->setTimezone(Tinebase_Core::getUserTimezone());
         $dateString = $date->format('Y-m');
         switch ($this->_operator) {
             case 'before':
                 $_select->where($db->quoteInto($this->_getQuotedFieldName($_backend) . " < (?)", $dateString));
                 break;
             case 'after':
                 $_select->where($db->quoteInto($this->_getQuotedFieldName($_backend) . " > (?)", $dateString));
                 break;
             case 'before_or_equals':
                 $_select->where($db->quoteInto($this->_getQuotedFieldName($_backend) . " <= (?)", $dateString));
                 break;
             case 'after_or_equals':
                 $_select->where($db->quoteInto($this->_getQuotedFieldName($_backend) . " >= (?)", $dateString));
                 break;
             default:
                 throw new Tinebase_Exception_InvalidArgument('The operator ' . $this->_operator . ' is not supported for this filter!');
         }
     }
 }
Exemplo n.º 19
0
 /**
  * appends sql to given select statement
  *
  * @param  Zend_Db_Select                    $_select
  * @param  Tinebase_Backend_Sql_Abstract     $_backend
  */
 public function appendFilterSql($_select, $_backend)
 {
     $db = $_backend->getAdapter();
     $_select->joinLeft(array('members' => $db->table_prefix . 'addressbook_list_members'), $db->quoteIdentifier('members.contact_id') . ' = ' . $db->quoteIdentifier('addressbook.id'), array());
     $_select->where($db->quoteIdentifier('members.list_id') . ' IN (?)', (array) $this->_value);
 }
 /**
  * append accountfilter
  * 
  * @param Zend_Db_Select $select
  * @param Tinebase_Backend_Sql_Abstract $backend
  * @param Tinebase_Model_User $user
  */
 protected function _appendAccountFilter($select, $backend, $user)
 {
     $accountIdFilter = $this->_findFilter('account_id');
     $userId = $user->getId();
     // set user account id as filter
     if ($accountIdFilter === null) {
         $accountIdFilter = $this->createFilter('account_id', 'equals', $userId);
         $this->addFilter($accountIdFilter);
     } else {
         $accountIdFilter->setValue($userId);
     }
     $groupSelect = new Tinebase_Backend_Sql_Filter_GroupSelect($select);
     $db = $backend->getAdapter();
     $accountIdFilter->appendFilterSql($groupSelect, $backend);
     $groupSelect->orWhere($db->quoteIdentifier('filter.account_id') . ' IS NULL');
     $groupSelect->appendWhere(Zend_Db_Select::SQL_AND);
     $this->removeFilter('account_id');
 }
 /**
  * appends sql to given select statement
  *
  * @param Zend_Db_Select $_select
  * @param Tinebase_Backend_Sql_Abstract $_backend
  * @throws Tinebase_Exception_InvalidArgument
  */
 public function appendFilterSql($_select, $_backend)
 {
     if (empty($this->_value)) {
         $_select->where('1=1/* empty query */');
         return;
     }
     $db = $_backend->getAdapter();
     $sqlCommand = Tinebase_Backend_Sql_Command::factory($db);
     if (0 === strpos($this->_operator, 'not')) {
         $not = true;
     } else {
         $not = false;
     }
     switch ($this->_operator) {
         case 'contains':
         case 'notcontains':
         case 'equals':
         case 'not':
         case 'startswith':
         case 'endswith':
             $queries = explode(' ', $this->_value);
             foreach ($queries as $query) {
                 $whereParts = array();
                 foreach ($this->_options['fields'] as $qField) {
                     // if field has . in name, then we already have tablename
                     if (strpos($qField, '.') !== FALSE) {
                         $whereParts[] = $sqlCommand->prepareForILike($sqlCommand->getUnaccent($db->quoteIdentifier($qField))) . ' ' . ($not ? 'NOT ' : '') . $sqlCommand->getLike() . $sqlCommand->prepareForILike($sqlCommand->getUnaccent('(?)'));
                     } else {
                         $whereParts[] = $sqlCommand->prepareForILike($sqlCommand->getUnaccent($db->quoteIdentifier($_backend->getTableName() . '.' . $qField))) . ' ' . ($not ? 'NOT ' : '') . $sqlCommand->getLike() . $sqlCommand->prepareForILike($sqlCommand->getUnaccent('(?)'));
                     }
                 }
                 $whereClause = '';
                 if (!empty($whereParts)) {
                     if ($not) {
                         $whereClause = implode(' AND ', $whereParts);
                     } else {
                         $whereClause = implode(' OR ', $whereParts);
                     }
                 }
                 if (!empty($whereClause)) {
                     $query = trim($query);
                     if ($this->_operator === 'startswith') {
                         $query .= '%';
                     } else {
                         if ($this->_operator === 'contains' || $this->_operator === 'notcontains') {
                             $query = '%' . $query . '%';
                         } else {
                             if ($this->_operator === 'endswith') {
                                 $query = '%' . $query;
                             }
                         }
                     }
                     $_select->where($db->quoteInto($whereClause, $query));
                 }
             }
             break;
         case 'notin':
         case 'in':
             foreach ($this->_options['fields'] as $qField) {
                 // if field has . in name, then we allready have tablename
                 if (strpos($qField, '.') !== FALSE) {
                     $whereParts[] = $db->quoteInto($db->quoteIdentifier($qField) . ($not ? ' NOT' : '') . ' IN (?)', (array) $this->_value);
                 } else {
                     $whereParts[] = $db->quoteInto($db->quoteIdentifier($_backend->getTableName() . '.' . $qField) . ($not ? ' NOT' : '') . ' IN (?)', (array) $this->_value);
                 }
             }
             if (!empty($whereParts)) {
                 if ($not) {
                     $whereClause = implode(' AND ', $whereParts);
                 } else {
                     $whereClause = implode(' OR ', $whereParts);
                 }
             }
             if (!empty($whereClause)) {
                 $_select->where($whereClause);
             }
             break;
         default:
             throw new Tinebase_Exception_InvalidArgument('Operator not defined: ' . $this->_operator);
     }
     // append advanced search filter if configured
     if (isset($this->_options['relatedModels']) && isset($this->_options['modelName'])) {
         $relationFilter = $this->_getAdvancedSearchFilter($this->_options['modelName'], $this->_options['relatedModels']);
         if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) {
             Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' Got relation filter: ' . ($relationFilter instanceof Tinebase_Model_Filter_Abstract ? print_r($relationFilter->toArray(), true) : ''));
         }
         if ($relationFilter) {
             $relationSelect = new Tinebase_Backend_Sql_Filter_GroupSelect($_select);
             $relationFilter->appendFilterSql($relationSelect, $_backend);
             $relationSelect->appendWhere($not ? Zend_Db_Select::SQL_AND : Zend_Db_Select::SQL_OR);
         }
     }
 }