/** * Get a list of Commendations matching the params, where each param key is: * 1. the key of a field in civicrm_activity, except for activity_type_id * 2. the key of a custom field on the activity (volunteer_project_id) * 3. the key of a field in civicrm_contact * * @param array $params * @return array of CRM_Volunteer_BAO_Project objects */ public static function retrieve(array $params) { $activity_fields = CRM_Activity_DAO_Activity::fields(); $contact_fields = CRM_Contact_DAO_Contact::fields(); $custom_fields = self::getCustomFields(); // This is the "real" id $activity_fields['id'] = $activity_fields['activity_id']; unset($activity_fields['activity_id']); // enforce restrictions on parameters $allowed_params = array_flip(array_merge(array_keys($activity_fields), array_keys($contact_fields), array_keys($custom_fields))); unset($allowed_params['activity_type_id']); $filtered_params = array_intersect_key($params, $allowed_params); $custom_group = self::getCustomGroup(); $customTableName = $custom_group['table_name']; foreach ($custom_fields as $name => $field) { $selectClause[] = "{$customTableName}.{$field['column_name']} AS {$name}"; } $customSelect = implode(', ', $selectClause); $activityContactTypes = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name'); $targetID = CRM_Utils_Array::key('Activity Targets', $activityContactTypes); $placeholders = array(1 => array($targetID, 'Integer'), 2 => array(self::getActivityTypeId(), 'Integer')); $i = count($placeholders) + 1; $where = array(); $whereClause = NULL; foreach ($filtered_params as $key => $value) { if (CRM_Utils_Array::value($key, $activity_fields)) { $dataType = CRM_Utils_Type::typeToString($activity_fields[$key]['type']); $fieldName = $activity_fields[$key]['name']; $tableName = CRM_Activity_DAO_Activity::$_tableName; } elseif (CRM_Utils_Array::value($key, $contact_fields)) { $dataType = CRM_Utils_Type::typeToString($contact_fields[$key]['type']); $fieldName = $contact_fields[$key]['name']; $tableName = CRM_Contact_DAO_Contact::$_tableName; } elseif (CRM_Utils_Array::value($key, $custom_fields)) { $dataType = $custom_fields[$key]['data_type']; $fieldName = $custom_fields[$key]['column_name']; $tableName = $customTableName; } $where[] = "{$tableName}.{$fieldName} = %{$i}"; $placeholders[$i] = array($value, $dataType); $i++; } if (count($where)) { $whereClause = 'AND ' . implode("\nAND ", $where); } $query = "\n SELECT\n civicrm_activity.*,\n {$customSelect},\n activityContact.contact_id AS volunteer_contact_id,\n volunteer_contact.sort_name AS volunteer_sort_name,\n volunteer_contact.display_name AS volunteer_display_name\n FROM civicrm_activity\n INNER JOIN civicrm_activity_contact activityContact\n ON (\n activityContact.activity_id = civicrm_activity.id\n AND activityContact.record_type_id = %1\n )\n INNER JOIN civicrm_contact volunteer_contact\n ON activityContact.contact_id = volunteer_contact.id\n INNER JOIN {$customTableName}\n ON ({$customTableName}.entity_id = civicrm_activity.id)\n WHERE civicrm_activity.activity_type_id = %2\n {$whereClause}\n "; $dao = CRM_Core_DAO::executeQuery($query, $placeholders); $rows = array(); while ($dao->fetch()) { $rows[$dao->id] = $dao->toArray(); } return $rows; }
/** * Get a list of Assignments matching the params, where each param key is: * 1. the key of a field in civicrm_activity * except for activity_type_id and activity_duration * 2. the key of a custom field on the activity * (volunteer_need_id, time_scheduled, time_completed) * 3. the key of a field in civicrm_contact * 4. project_id * * @param array $params * @return array of CRM_Volunteer_BAO_Project objects */ static function retrieve(array $params) { $activity_fields = CRM_Activity_DAO_Activity::fields(); $contact_fields = CRM_Contact_DAO_Contact::fields(); $custom_fields = self::getCustomFields(); $foreign_fields = array('project_id', 'target_contact_id'); // This is the "real" id $activity_fields['id'] = $activity_fields['activity_id']; unset($activity_fields['activity_id']); // enforce restrictions on parameters $allowed_params = array_flip(array_merge(array_keys($activity_fields), array_keys($contact_fields), array_keys($custom_fields), $foreign_fields)); unset($allowed_params['activity_type_id']); unset($allowed_params['activity_duration']); $filtered_params = array_intersect_key($params, $allowed_params); $custom_group = self::getCustomGroup(); $customTableName = $custom_group['table_name']; foreach ($custom_fields as $name => $field) { $selectClause[] = "{$customTableName}.{$field['column_name']} AS {$name}"; } $customSelect = implode(', ', $selectClause); $activityContactTypes = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name'); $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContactTypes); $targetID = CRM_Utils_Array::key('Activity Targets', $activityContactTypes); $volunteerStatus = CRM_Activity_BAO_Activity::buildOptions('status_id', 'validate'); $available = CRM_Utils_Array::key('Available', $volunteerStatus); $scheduled = CRM_Utils_Array::key('Scheduled', $volunteerStatus); $placeholders = array(1 => array($assigneeID, 'Integer'), 2 => array(self::volunteerActivityTypeId(), 'Integer'), 3 => array($scheduled, 'Integer'), 4 => array($available, 'Integer'), 5 => array($targetID, 'Integer')); $i = count($placeholders) + 1; $where = array(); $whereClause = NULL; foreach ($filtered_params as $key => $value) { if (CRM_Utils_Array::value($key, $activity_fields)) { $dataType = CRM_Utils_Type::typeToString($activity_fields[$key]['type']); $fieldName = $activity_fields[$key]['name']; $tableName = CRM_Activity_DAO_Activity::$_tableName; } elseif (CRM_Utils_Array::value($key, $contact_fields)) { $dataType = CRM_Utils_Type::typeToString($contact_fields[$key]['type']); $fieldName = $contact_fields[$key]['name']; $tableName = CRM_Contact_DAO_Contact::$_tableName; } elseif (CRM_Utils_Array::value($key, $custom_fields)) { $dataType = $custom_fields[$key]['data_type']; $fieldName = $custom_fields[$key]['column_name']; $tableName = $customTableName; } elseif ($key == 'project_id') { $dataType = 'Int'; $fieldName = 'id'; $tableName = CRM_Volunteer_DAO_Project::$_tableName; } elseif ($key == 'target_contact_id') { $dataType = 'Int'; $fieldName = 'contact_id'; $tableName = 'tgt'; // this is an alias for civicrm_activity_contact } $where[] = "{$tableName}.{$fieldName} = %{$i}"; $placeholders[$i] = array($value, $dataType); $i++; } if (count($where)) { $whereClause = 'AND ' . implode("\nAND ", $where); } $query = "\n SELECT\n civicrm_activity.*,\n assignee.contact_id AS assignee_contact_id,\n {$customSelect},\n civicrm_volunteer_need.start_time,\n civicrm_volunteer_need.is_flexible,\n civicrm_volunteer_need.role_id,\n assignee_contact.sort_name AS assignee_sort_name,\n assignee_contact.display_name AS assignee_display_name,\n assignee_phone.phone AS assignee_phone,\n assignee_phone.phone_ext AS assignee_phone_ext,\n assignee_email.email AS assignee_email,\n -- begin target contact fields\n tgt.contact_id AS target_contact_id,\n tgt_contact.sort_name AS target_sort_name,\n tgt_contact.display_name AS target_display_name,\n tgt_phone.phone AS target_phone,\n tgt_phone.phone_ext AS target_phone_ext,\n tgt_email.email AS target_email\n -- end target contact fields\n FROM civicrm_activity\n INNER JOIN civicrm_activity_contact assignee\n ON (\n assignee.activity_id = civicrm_activity.id\n AND assignee.record_type_id = %1\n )\n INNER JOIN civicrm_contact assignee_contact\n ON assignee.contact_id = assignee_contact.id\n LEFT JOIN civicrm_email assignee_email\n ON assignee_email.contact_id = assignee_contact.id AND assignee_email.is_primary = 1\n LEFT JOIN civicrm_phone assignee_phone\n ON assignee_phone.contact_id = assignee_contact.id AND assignee_phone.is_primary = 1\n -- begin target contact joins\n LEFT JOIN civicrm_activity_contact tgt\n ON (\n tgt.activity_id = civicrm_activity.id\n AND tgt.record_type_id = %5\n )\n LEFT JOIN civicrm_contact tgt_contact\n ON tgt.contact_id = tgt_contact.id\n LEFT JOIN civicrm_email tgt_email\n ON tgt_email.contact_id = tgt_contact.id AND tgt_email.is_primary = 1\n LEFT JOIN civicrm_phone tgt_phone\n ON tgt_phone.contact_id = tgt_contact.id AND tgt_phone.is_primary = 1\n -- end target contact joins\n INNER JOIN {$customTableName}\n ON ({$customTableName}.entity_id = civicrm_activity.id)\n INNER JOIN civicrm_volunteer_need\n ON (civicrm_volunteer_need.id = {$customTableName}.{$custom_fields['volunteer_need_id']['column_name']})\n INNER JOIN civicrm_volunteer_project\n ON (civicrm_volunteer_project.id = civicrm_volunteer_need.project_id)\n WHERE civicrm_activity.activity_type_id = %2\n AND civicrm_activity.status_id IN (%3, %4 )\n {$whereClause}\n "; $dao = CRM_Core_DAO::executeQuery($query, $placeholders); $rows = array(); while ($dao->fetch()) { $rows[$dao->id] = $dao->toArray(); } /* * For clarity we want the fields associated with each contact prefixed with * the contact type (e.g., target_phone). For backwards compatibility, * however, we want the fields associated with each assignee contact to be * accessible sans prefix. Eventually we should deprecate the non-prefixed * field names. */ foreach ($rows as $id => $fields) { foreach ($fields as $key => $value) { if (substr($key, 0, 9) == 'assignee_') { $rows[$id][substr($key, 9)] = $value; } } } return $rows; }
/** * take the input parameter list as specified in the data model and * convert it into the same format that we use in QF and BAO object * * @param array $params * Associative array of property name/value. * pairs to insert in new contact. * @param array $values * The reformatted properties that we can use internally. * * @param array|bool $create Is the formatted Values array going to * be used for CRM_Activity_BAO_Activity::create() * * @return array|CRM_Error */ function _civicrm_api3_deprecated_activity_formatted_param(&$params, &$values, $create = FALSE) { // copy all the activity fields as is $fields = CRM_Activity_DAO_Activity::fields(); _civicrm_api3_store_values($fields, $params, $values); require_once 'CRM/Core/OptionGroup.php'; $customFields = CRM_Core_BAO_CustomField::getFields('Activity'); foreach ($params as $key => $value) { // ignore empty values or empty arrays etc if (CRM_Utils_System::isNull($value)) { continue; } //Handling Custom Data if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) { $values[$key] = $value; $type = $customFields[$customFieldID]['html_type']; if ($type == 'CheckBox' || $type == 'Multi-Select') { $mulValues = explode(',', $value); $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, TRUE); $values[$key] = array(); foreach ($mulValues as $v1) { foreach ($customOption as $customValueID => $customLabel) { $customValue = $customLabel['value']; if (strtolower(trim($customLabel['label'])) == strtolower(trim($v1)) || strtolower(trim($customValue)) == strtolower(trim($v1))) { if ($type == 'CheckBox') { $values[$key][$customValue] = 1; } else { $values[$key][] = $customValue; } } } } } elseif ($type == 'Select' || $type == 'Radio') { $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, TRUE); foreach ($customOption as $customFldID => $customValue) { $val = CRM_Utils_Array::value('value', $customValue); $label = CRM_Utils_Array::value('label', $customValue); $label = strtolower($label); $value = strtolower(trim($value)); if ($value == $label || $value == strtolower($val)) { $values[$key] = $val; } } } } if ($key == 'target_contact_id') { if (!CRM_Utils_Rule::integer($value)) { return civicrm_api3_create_error("contact_id not valid: {$value}"); } $contactID = CRM_Core_DAO::singleValueQuery("SELECT id FROM civicrm_contact WHERE id = {$value}"); if (!$contactID) { return civicrm_api3_create_error("Invalid Contact ID: There is no contact record with contact_id = {$value}."); } } } return NULL; }
/** * take the input parameter list as specified in the data model and * convert it into the same format that we use in QF and BAO object * * @param array $params Associative array of property name/value * pairs to insert in new contact. * @param array $values The reformatted properties that we can use internally * * @param array $create Is the formatted Values array going to * be used for CRM_Activity_BAO_Activity::create() * * @return array|CRM_Error * @access public */ function _civicrm_activity_formatted_param(&$params, &$values, $create = false) { $fields =& CRM_Activity_DAO_Activity::fields(); _civicrm_store_values($fields, $params, $values); require_once 'CRM/Core/OptionGroup.php'; $customFields = CRM_Core_BAO_CustomField::getFields('Activity'); foreach ($params as $key => $value) { // ignore empty values or empty arrays etc if (CRM_Utils_System::isNull($value)) { continue; } //Handling Custom Data if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) { $values[$key] = $value; $type = $customFields[$customFieldID]['html_type']; if ($type == 'CheckBox' || $type == 'Multi-Select') { $mulValues = explode(',', $value); $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, true); $values[$key] = array(); foreach ($mulValues as $v1) { foreach ($customOption as $customValueID => $customLabel) { $customValue = $customLabel['value']; if (strtolower($customLabel['label']) == strtolower(trim($v1)) || strtolower($customValue) == strtolower(trim($v1))) { if ($type == 'CheckBox') { $values[$key][$customValue] = 1; } else { $values[$key][] = $customValue; } } } } } else { if ($type == 'Select' || $type == 'Radio') { $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, true); foreach ($customOption as $customValue => $customLabel) { if (strtolower($customLabel) == strtolower(trim($v1)) || strtolower($customValue) == strtolower(trim($v1))) { $values[$key] = $customValue; } } } } } } return null; }
/** * take the input parameter list as specified in the data model and * convert it into the same format that we use in QF and BAO object * * @param array $params Associative array of property name/value * pairs to insert in new contact. * @param array $values The reformatted properties that we can use internally * * @param array $create Is the formatted Values array going to * be used for CRM_Activity_BAO_Activity::create() * * @return array|CRM_Error * @access public */ function _civicrm_activity_formatted_param(&$params, &$values, $create = FALSE) { $fields = CRM_Activity_DAO_Activity::fields(); _civicrm_store_values($fields, $params, $values); require_once 'CRM/Core/OptionGroup.php'; $customFields = CRM_Core_BAO_CustomField::getFields('Activity'); foreach ($params as $key => $value) { // ignore empty values or empty arrays etc if (CRM_Utils_System::isNull($value)) { continue; } //Handling Custom Data _civicrm_generic_handle_custom_data($key, $value, $values, $customFields); if ($key == 'target_contact_id') { if (!CRM_Utils_Rule::integer($value)) { return civicrm_create_error("contact_id not valid: {$value}"); } $contactID = CRM_Core_DAO::singleValueQuery("SELECT id FROM civicrm_contact WHERE id = {$value}"); if (!$contactID) { return civicrm_create_error("Invalid Contact ID: There is no contact record with contact_id = {$value}."); } } } return NULL; }