/** * @inheritDoc */ public function addSelectWhereClause() { return array('case_id' => CRM_Utils_SQL::mergeSubquery('Case'), 'contact_id' => array()); // Don't call hook selectWhereClause, the case query already did }
/** * @inheritDoc */ public function addSelectWhereClause() { // We always return an array with these keys, even if they are empty, // because this tells the query builder that we have considered these fields for acls $clauses = array('id' => array(), 'is_deleted' => CRM_Core_Permission::check('administer CiviCase') ? array() : array("= 0")); // Ensure the user has permission to view the case client $contactClause = CRM_Utils_SQL::mergeSubquery('Contact'); if ($contactClause) { $contactClause = implode(' AND contact_id ', $contactClause); $clauses['id'][] = "IN (SELECT case_id FROM civicrm_case_contact WHERE contact_id {$contactClause})"; } // The api gatekeeper ensures the user has at least "access my cases and activities" // so if they do not have permission to see all cases we'll assume they can only access their own if (!CRM_Core_Permission::check('access all cases and activities')) { $user = (int) CRM_Core_Session::getLoggedInContactID(); $clauses['id'][] = "IN (\n SELECT r.case_id FROM civicrm_relationship r, civicrm_case_contact cc WHERE r.is_active = 1 AND cc.case_id = r.case_id AND (\n (r.contact_id_a = cc.contact_id AND r.contact_id_b = {$user}) OR (r.contact_id_b = cc.contact_id AND r.contact_id_a = {$user})\n )\n )"; } CRM_Utils_Hook::selectWhereClause($this, $clauses); return $clauses; }
/** * Generates acl clauses suitable for adding to WHERE or ON when doing an api.get for this entity * * Return format is in the form of fieldname => clauses starting with an operator. e.g.: * @code * array( * 'location_type_id' => array('IS NOT NULL', 'IN (1,2,3)') * ) * @endcode * * Note that all array keys must be actual field names in this entity. Use subqueries to filter on other tables e.g. custom values. * * @return array */ public function addSelectWhereClause() { // This is the default fallback, and works for contact-related entities like Email, Relationship, etc. $clauses = array(); foreach ($this->fields() as $fieldName => $field) { if (strpos($fieldName, 'contact_id') === 0 && CRM_Utils_Array::value('FKClassName', $field) == 'CRM_Contact_DAO_Contact') { $clauses[$fieldName] = CRM_Utils_SQL::mergeSubquery('Contact'); } } CRM_Utils_Hook::selectWhereClause($this, $clauses); return $clauses; }
/** * Generates acl clauses suitable for adding to WHERE or ON when doing an api.get for this entity * * Return format is in the form of fieldname => clauses starting with an operator. e.g.: * @code * array( * 'location_type_id' => array('IS NOT NULL', 'IN (1,2,3)') * ) * @endcode * * Note that all array keys must be actual field names in this entity. Use subqueries to filter on other tables e.g. custom values. * * @return array */ public function addSelectWhereClause() { $clauses = array(); $fields = $this->fields(); foreach ($fields as $fieldName => $field) { // Clause for contact-related entities like Email, Relationship, etc. if (strpos($fieldName, 'contact_id') === 0 && CRM_Utils_Array::value('FKClassName', $field) == 'CRM_Contact_DAO_Contact') { $clauses[$fieldName] = CRM_Utils_SQL::mergeSubquery('Contact'); } // Clause for an entity_table/entity_id combo if ($fieldName == 'entity_id' && isset($fields['entity_table'])) { $relatedClauses = array(); $relatedEntities = $this->buildOptions('entity_table', 'get'); foreach ((array) $relatedEntities as $table => $ent) { if (!empty($ent)) { $ent = CRM_Core_DAO_AllCoreTables::getBriefName(CRM_Core_DAO_AllCoreTables::getClassForTable($table)); $subquery = CRM_Utils_SQL::mergeSubquery($ent); if ($subquery) { $relatedClauses[] = "(entity_table = '{$table}' AND entity_id " . implode(' AND entity_id ', $subquery) . ")"; } else { $relatedClauses[] = "(entity_table = '{$table}')"; } } } if ($relatedClauses) { $clauses['id'] = 'IN (SELECT id FROM `' . $this->tableName() . '` WHERE (' . implode(') OR (', $relatedClauses) . '))'; } } } CRM_Utils_Hook::selectWhereClause($this, $clauses); return $clauses; }