_formValues = &$formValues; } function count() { return CRM_Core_DAO::singleValueQuery($this->sql('count(distinct contact_a.id) as total')); } function summary() { return NULL; } function contactIDs($offset = 0, $rowcount = 0, $sort = NULL, $returnSQL = FALSE) { $sql = $this->sql( 'contact_a.id as contact_id', $offset, $rowcount, $sort ); $this->validateUserSQL($sql); if ($returnSQL) { return $sql; } return CRM_Core_DAO::composeQuery($sql, CRM_Core_DAO::$_nullArray); } function sql( $selectClause, $offset = 0, $rowcount = 0, $sort = NULL, $includeContactIDs = FALSE, $groupBy = NULL ) { $sql = "SELECT $selectClause " . $this->from(); $where = $this->where(); if (!empty($where)) { $sql .= " WHERE " . $where; } if ($includeContactIDs) { $this->includeContactIDs($sql, $this->_formValues ); } if ($groupBy) { $sql .= " $groupBy "; } $this->addSortOffset($sql, $offset, $rowcount, $sort); return $sql; } function templateFile() { return NULL; } function &columns() { return $this->_columns; } static function includeContactIDs(&$sql, &$formValues) { $contactIDs = array(); foreach ($formValues as $id => $value) { if ($value && substr($id, 0, CRM_Core_Form::CB_PREFIX_LEN) == CRM_Core_Form::CB_PREFIX ) { $contactIDs[] = substr($id, CRM_Core_Form::CB_PREFIX_LEN); } } if (!empty($contactIDs)) { $contactIDs = implode(', ', $contactIDs); $sql .= " AND contact_a.id IN ( $contactIDs )"; } } function addSortOffset(&$sql, $offset, $rowcount, $sort) { if (!empty($sort)) { if (is_string($sort)) { $sql .= " ORDER BY $sort "; } else { $sql .= " ORDER BY " . trim($sort->orderBy()); } } if ($rowcount > 0 && $offset >= 0) { $sql .= " LIMIT $offset, $rowcount "; } } function validateUserSQL(&$sql, $onlyWhere = FALSE) { $includeStrings = array('contact_a'); $excludeStrings = array('insert', 'delete', 'update'); if (!$onlyWhere) { $includeStrings += array('select', 'from', 'where', 'civicrm_contact'); } foreach ($includeStrings as $string) { if (stripos($sql, $string) === FALSE) { CRM_Core_Error::fatal(ts('Could not find \'%1\' string in SQL clause.', array(1 => $string) )); } } foreach ($excludeStrings as $string) { if (preg_match('/(\s' . $string . ')|(' . $string . '\s)/i', $sql)) { CRM_Core_Error::fatal(ts('Found illegal \'%1\' string in SQL clause.', array(1 => $string) )); } } } function whereClause(&$where, &$params) { return CRM_Core_DAO::composeQuery($where, $params, TRUE); } // override this method to define the contact query object // used for creating $sql function getQueryObj() { return NULL; } }