/** * Build all the data structures needed to build the form. */ public function preProcess() { // Initialize the task and row fields. parent::preProcess(); // Get the contact read only fields to display. $readOnlyFields = array_merge(array('sort_name' => ts('Added By'), 'target_sort_name' => ts('With Contact')), CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'contact_autocomplete_options', TRUE, NULL, FALSE, 'name', TRUE)); // Get the read only field data. $returnProperties = array_fill_keys(array_keys($readOnlyFields), 1); $contactDetails = CRM_Contact_BAO_Contact_Utils::contactDetails($this->_activityHolderIds, 'Activity', $returnProperties); $readOnlyFields['assignee_display_name'] = ts('Assigned to'); if (!empty($contactDetails)) { foreach ($contactDetails as $key => $value) { $assignee = CRM_Activity_BAO_ActivityAssignment::retrieveAssigneeIdsByActivityId($key); foreach ($assignee as $keys => $values) { $assigneeContact[] = CRM_Contact_BAO_Contact::displayname($values); } $contactDetails[$key]['assignee_display_name'] = !empty($assigneeContact) ? implode(';', $assigneeContact) : NULL; } } $this->assign('contactDetails', $contactDetails); $this->assign('readOnlyFields', $readOnlyFields); }
/** * Given a list of activities, append any extra data requested about the activities. * * @note Called by civicrm-core and CiviHR * * @param array $params * API request parameters. * @param array $activities * * @return array * new activities list */ function _civicrm_api3_activity_get_formatResult($params, $activities) { $returns = CRM_Utils_Array::value('return', $params, array()); if (!is_array($returns)) { $returns = str_replace(' ', '', $returns); $returns = explode(',', $returns); } $returns = array_fill_keys($returns, 1); foreach ($params as $n => $v) { if (substr($n, 0, 7) == 'return.') { $returnkey = substr($n, 7); $returns[$returnkey] = $v; } } $returns['source_contact_id'] = 1; foreach ($returns as $n => $v) { switch ($n) { case 'assignee_contact_id': foreach ($activities as $key => $activityArray) { $activities[$key]['assignee_contact_id'] = CRM_Activity_BAO_ActivityAssignment::retrieveAssigneeIdsByActivityId($activityArray['id']); } break; case 'target_contact_id': foreach ($activities as $key => $activityArray) { $activities[$key]['target_contact_id'] = CRM_Activity_BAO_ActivityTarget::retrieveTargetIdsByActivityId($activityArray['id']); } break; case 'source_contact_id': foreach ($activities as $key => $activityArray) { $activities[$key]['source_contact_id'] = CRM_Activity_BAO_Activity::getSourceContactID($activityArray['id']); } break; default: if (substr($n, 0, 6) == 'custom') { $returnProperties[$n] = $v; } } } if (!empty($activities) && (!empty($returnProperties) || !empty($params['contact_id']))) { foreach ($activities as $activityId => $values) { //@todo - should possibly load activity type id if not loaded (update with id) _civicrm_api3_custom_data_get($activities[$activityId], CRM_Utils_Array::value('check_permissions', $params), 'Activity', $activityId, NULL, CRM_Utils_Array::value('activity_type_id', $values)); } } return $activities; }
/** * Pass a known activity id as an id and make sure 1 Assignees is retrieved */ public function testRetrieveAssigneeIdsByActivityIdOneId() { $activity = $this->activityCreate(); $activityId = CRM_Activity_BAO_ActivityAssignment::retrieveAssigneeIdsByActivityId($activity['id']); $this->assertEquals(count($activityId), 1, 'One record retrieved'); }
/** * Takes a bunch of params that are needed to match certain criteria and * retrieves the relevant objects. Typically the valid params are only * contact_id. We'll tweak this function to be more full featured over a period * of time. This is the inverse function of create. It also stores all the retrieved * values in the default array * * @param array $params (reference ) an assoc array of name/value pairs * @param array $defaults (reference ) an assoc array to hold the flattened values * @param string $activityType activity type * * @return object CRM_Core_BAO_Meeting object * @access public */ public function retrieve(&$params, &$defaults) { $activity =& new CRM_Activity_DAO_Activity(); $activity->copyValues($params); if ($activity->find(true)) { require_once "CRM/Contact/BAO/Contact.php"; // TODO: at some stage we'll have to deal // TODO: with multiple values for assignees and targets, but // TODO: for now, let's just fetch first row $defaults['assignee_contact'] = CRM_Activity_BAO_ActivityAssignment::retrieveAssigneeIdsByActivityId($activity->id); $assignee_contact_names = CRM_Activity_BAO_ActivityAssignment::getAssigneeNames($activity->id); $defaults['assignee_contact_value'] = null; foreach ($assignee_contact_names as $key => $name) { $defaults['assignee_contact_value'] .= $defaults['assignee_contact_value'] ? "; {$name}" : "{$name}"; } if ($activity->activity_type_id != CRM_Core_OptionGroup::getValue('activity_type', 'Bulk Email', 'name')) { require_once 'CRM/Activity/BAO/ActivityTarget.php'; $defaults['target_contact'] = CRM_Activity_BAO_ActivityTarget::retrieveTargetIdsByActivityId($activity->id); $target_contact_names = CRM_Activity_BAO_ActivityTarget::getTargetNames($activity->id); $defaults['target_contact_value'] = null; foreach ($target_contact_names as $key => $name) { $defaults['target_contact_value'] .= $defaults['target_contact_value'] ? "; {$name}" : "{$name}"; } } else { if (CRM_Core_Permission::check('access CiviMail')) { $defaults['mailingId'] = CRM_Utils_System::url('civicrm/mailing/report', "mid={$activity->source_record_id}&reset=1&atype={$activity->activity_type_id}&aid={$activity->id}&cid={$activity->source_contact_id}&context=activity"); } else { $defaults['target_contact_value'] = ts('(recipients)'); } } if ($activity->source_contact_id) { $defaults['source_contact'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $activity->source_contact_id, 'sort_name'); } //get case subject require_once "CRM/Case/BAO/Case.php"; $defaults['case_subject'] = CRM_Case_BAO_Case::getCaseSubject($activity->id); CRM_Core_DAO::storeValues($activity, $defaults); return $activity; } return null; }
/** * Gets a CiviCRM activity according to parameters * * @param array $params Associative array of property name/value * pairs for the activity. * * @return array * * {@getfields activity_get} * @example ActivityGet.php Basic example * @example Activity/DateTimeHigh.php Example get with date filtering * {@example ActivityGet.php 0} */ function civicrm_api3_activity_get($params) { if (!empty($params['contact_id'])) { $activities = CRM_Activity_BAO_Activity::getContactActivity($params['contact_id']); //BAO function doesn't actually return a contact ID - hack api for now & add to test so when api re-write happens it won't get missed foreach ($activities as $key => $activityArray) { $activities[$key]['id'] = $key; } } else { $activities = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params, FALSE); } $returns = CRM_Utils_Array::value('return', $params, array()); if (!is_array($returns)) { $returns = str_replace(' ', '', $returns); $returns = explode(',', $returns); } $returns = array_fill_keys($returns, 1); foreach ($params as $n => $v) { if (substr($n, 0, 7) == 'return.') { $returnkey = substr($n, 7); $returns[$returnkey] = $v; } } $returns['source_contact_id'] = 1; foreach ($returns as $n => $v) { switch ($n) { case 'assignee_contact_id': foreach ($activities as $key => $activityArray) { $activities[$key]['assignee_contact_id'] = CRM_Activity_BAO_ActivityAssignment::retrieveAssigneeIdsByActivityId($activityArray['id']); } break; case 'target_contact_id': foreach ($activities as $key => $activityArray) { $activities[$key]['target_contact_id'] = CRM_Activity_BAO_ActivityTarget::retrieveTargetIdsByActivityId($activityArray['id']); } break; case 'source_contact_id': foreach ($activities as $key => $activityArray) { $activities[$key]['source_contact_id'] = CRM_Activity_BAO_Activity::getSourceContactID($activityArray['id']); } break; default: if (substr($n, 0, 6) == 'custom') { $returnProperties[$n] = $v; } } } if (!empty($activities) && (!empty($returnProperties) || !empty($params['contact_id']))) { foreach ($activities as $activityId => $values) { _civicrm_api3_custom_data_get($activities[$activityId], 'Activity', $activityId, NULL, $values['activity_type_id']); } } //legacy custom data get - so previous formatted response is still returned too return civicrm_api3_create_success($activities, $params, 'activity', 'get'); }
/** * Gets a CiviCRM activity according to parameters * * @param array $params Associative array of property name/value * pairs for the activity. * * @return array * * {@getfields activity_get} * @example ActivityGet.php Basic example * @example Activity/DateTimeHigh.php Example get with date filtering * {@example ActivityGet.php 0} */ function civicrm_api3_activity_get($params) { if (!empty($params['contact_id'])) { $activities = CRM_Activity_BAO_Activity::getContactActivity($params['contact_id']); //BAO function doesn't actually return a contact ID - hack api for now & add to test so when api re-write happens it won't get missed foreach ($activities as $key => $activityArray) { $activities[$key]['id'] = $key; } } else { $activities = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params, FALSE); } if (CRM_Utils_Array::value('return.assignee_contact_id', $params)) { foreach ($activities as $key => $activityArray) { $activities[$key]['assignee_contact_id'] = CRM_Activity_BAO_ActivityAssignment::retrieveAssigneeIdsByActivityId($activityArray['id']); } } if (CRM_Utils_Array::value('return.target_contact_id', $params)) { foreach ($activities as $key => $activityArray) { $activities[$key]['target_contact_id'] = CRM_Activity_BAO_ActivityTarget::retrieveTargetIdsByActivityId($activityArray['id']); } } foreach ($params as $n => $v) { // handle the format return.sort_name=1,return.display_name=1 if (substr($n, 0, 13) == 'return.custom') { $returnProperties[substr($n, 7)] = $v; } } if (!empty($activities) && (!empty($returnProperties) || !empty($params['contact_id']))) { foreach ($activities as $activityId => $values) { _civicrm_api3_custom_data_get($activities[$activityId], 'Activity', $activityId, NULL, $values['activity_type_id']); } } //legacy custom data get - so previous formatted response is still returned too return civicrm_api3_create_success($activities, $params, 'activity', 'get'); }
/** * Does user has sufficient permission for view/edit activity record. * * @param int $activityId activity record id. * @param int $action edit/view * * @return boolean $allow true/false * @access public */ public static function checkPermission($activityId, $action) { $allow = FALSE; if (!$activityId || !in_array($action, array(CRM_Core_Action::UPDATE, CRM_Core_Action::VIEW))) { return $allow; } $activity = new CRM_Activity_DAO_Activity(); $activity->id = $activityId; if (!$activity->find(TRUE)) { return $allow; } //component related permissions. $compPermissions = array('CiviCase' => array('administer CiviCase', 'access my cases and activities', 'access all cases and activities'), 'CiviMail' => array('access CiviMail'), 'CiviEvent' => array('access CiviEvent'), 'CiviGrant' => array('access CiviGrant'), 'CiviPledge' => array('access CiviPledge'), 'CiviMember' => array('access CiviMember'), 'CiviReport' => array('access CiviReport'), 'CiviContribute' => array('access CiviContribute'), 'CiviCampaign' => array('administer CiviCampaign')); //return early when it is case activity. $isCaseActivity = CRM_Case_BAO_Case::isCaseActivity($activityId); //check for civicase related permission. if ($isCaseActivity) { $allow = FALSE; foreach ($compPermissions['CiviCase'] as $per) { if (CRM_Core_Permission::check($per)) { $allow = TRUE; break; } } //check for case specific permissions. if ($allow) { $oper = 'view'; if ($action == CRM_Core_Action::UPDATE) { $oper = 'edit'; } $allow = CRM_Case_BAO_Case::checkPermission($activityId, $oper, $activity->activity_type_id); } return $allow; } //first check the component permission. $sql = "\n SELECT component_id\n FROM civicrm_option_value val\nINNER JOIN civicrm_option_group grp ON ( grp.id = val.option_group_id AND grp.name = %1 )\n WHERE val.value = %2"; $params = array(1 => array('activity_type', 'String'), 2 => array($activity->activity_type_id, 'Integer')); $componentId = CRM_Core_DAO::singleValueQuery($sql, $params); if ($componentId) { $componentName = CRM_Core_Component::getComponentName($componentId); $compPermission = CRM_Utils_Array::value($componentName, $compPermissions); //here we are interesting in any single permission. if (is_array($compPermission)) { foreach ($compPermission as $per) { if (CRM_Core_Permission::check($per)) { $allow = TRUE; break; } } } } //check for this permission related to contact. $permission = CRM_Core_Permission::VIEW; if ($action == CRM_Core_Action::UPDATE) { $permission = CRM_Core_Permission::EDIT; } //check for source contact. if (!$componentId || $allow) { $allow = CRM_Contact_BAO_Contact_Permission::allow($activity->source_contact_id, $permission); } //check for target and assignee contacts. if ($allow) { //first check for supper permission. $supPermission = 'view all contacts'; if ($action == CRM_Core_Action::UPDATE) { $supPermission = 'edit all contacts'; } $allow = CRM_Core_Permission::check($supPermission); //user might have sufficient permission, through acls. if (!$allow) { $allow = TRUE; //get the target contacts. $targetContacts = CRM_Activity_BAO_ActivityTarget::retrieveTargetIdsByActivityId($activity->id); foreach ($targetContacts as $cnt => $contactId) { if (!CRM_Contact_BAO_Contact_Permission::allow($contactId, $permission)) { $allow = FALSE; break; } } //get the assignee contacts. if ($allow) { $assigneeContacts = CRM_Activity_BAO_ActivityAssignment::retrieveAssigneeIdsByActivityId($activity->id); foreach ($assigneeContacts as $cnt => $contactId) { if (!CRM_Contact_BAO_Contact_Permission::allow($contactId, $permission)) { $allow = FALSE; break; } } } } } return $allow; }