Exemple #1
0
 /**
  * Do AlterDisplay processing on Address Fields.
  *
  * @param array $row
  * @param array $rows
  * @param int $rowNum
  * @param string $baseUrl
  * @param string $linkText
  *
  * @return bool
  */
 public function alterDisplayContactFields(&$row, &$rows, &$rowNum, $baseUrl, $linkText)
 {
     $entryFound = FALSE;
     // There is no reason not to add links for all fields but it seems a bit odd to be able to click on
     // 'Mrs'. Also, we don't have metadata about the title. So, add selectively to addLinks.
     $addLinks = array('gender_id' => 'Gender');
     foreach (array('prefix_id', 'suffix_id', 'gender_id') as $fieldName) {
         if (array_key_exists('civicrm_contact_' . $fieldName, $row)) {
             if (($value = $row['civicrm_contact_' . $fieldName]) != FALSE) {
                 $rows[$rowNum]['civicrm_contact_' . $fieldName] = CRM_Core_Pseudoconstant::getLabel('CRM_Contact_BAO_Contact', $fieldName, $value);
                 if ($baseUrl && ($title = CRM_Utils_Array::value($fieldName, $addLinks)) != FALSE) {
                     $this->addLinkToRow($rows[$rowNum], $baseUrl, $linkText, $value, $fieldName, 'civicrm_contact', $title);
                 }
             }
             $entryFound = TRUE;
         }
     }
     $yesNoFields = array('do_not_email', 'is_deceased', 'do_not_phone', 'do_not_sms', 'do_not_mail', 'is_opt_out');
     foreach ($yesNoFields as $fieldName) {
         if (array_key_exists('civicrm_contact_' . $fieldName, $row)) {
             // Since these are essentially 'negative fields' it feels like it
             // makes sense to only highlight the exceptions hence no 'No'.
             $rows[$rowNum]['civicrm_contact_' . $fieldName] = !empty($rows[$rowNum]['civicrm_contact_' . $fieldName]) ? ts('Yes') : '';
             $entryFound = TRUE;
         }
     }
     return $entryFound;
 }
 /** 
  * Function to delete membership.
  * 
  * @param int $membershipId membership id that needs to be deleted 
  *
  * @static
  * @return $results   no of deleted Membership on success, false otherwise
  * @access public
  */
 static function deleteMembership($membershipId)
 {
     require_once 'CRM/Core/Transaction.php';
     $transaction = new CRM_Core_Transaction();
     $results = null;
     //delete activity record
     $activityTypes = CRM_Core_Pseudoconstant::activityType(true, false, false, 'name');
     require_once "CRM/Activity/BAO/Activity.php";
     $params = array('source_record_id' => $membershipId, 'activity_type_id' => array(array_search('Membership Signup', $activityTypes), array_search('Membership Renewal', $activityTypes)));
     CRM_Activity_BAO_Activity::deleteActivity($params);
     self::deleteMembershipPayment($membershipId);
     require_once 'CRM/Member/DAO/Membership.php';
     $membership = new CRM_Member_DAO_Membership();
     $membership->id = $membershipId;
     $results = $membership->delete();
     $transaction->commit();
     // delete the recently created Membership
     require_once 'CRM/Utils/Recent.php';
     $membershipRecent = array('id' => $membershipId, 'type' => 'Membership');
     CRM_Utils_Recent::del($membershipRecent);
     return $results;
 }
Exemple #3
0
 /**
  * Process contribution records.
  *
  * @param array $params
  *   Associated array of submitted values.
  *
  *
  * @return void
  */
 private function processContribution(&$params)
 {
     $dates = array('receive_date', 'receipt_date', 'thankyou_date', 'cancel_date');
     // get the price set associated with offline contribution record.
     $priceSetId = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', 'default_contribution_amount', 'id', 'name');
     $this->_priceSet = current(CRM_Price_BAO_PriceSet::getSetDetail($priceSetId));
     $priceFieldID = CRM_Price_BAO_PriceSet::getOnlyPriceFieldID($this->_priceSet);
     $priceFieldValueID = CRM_Price_BAO_PriceSet::getOnlyPriceFieldValueID($this->_priceSet);
     if (isset($params['field'])) {
         foreach ($params['field'] as $key => $value) {
             // if contact is not selected we should skip the row
             if (empty($params['primary_contact_id'][$key])) {
                 continue;
             }
             $value['contact_id'] = CRM_Utils_Array::value($key, $params['primary_contact_id']);
             // update contact information
             $this->updateContactInfo($value);
             //build soft credit params
             if (!empty($params['soft_credit_contact_id'][$key]) && !empty($params['soft_credit_amount'][$key])) {
                 $value['soft_credit'][$key]['contact_id'] = $params['soft_credit_contact_id'][$key];
                 $value['soft_credit'][$key]['amount'] = CRM_Utils_Rule::cleanMoney($params['soft_credit_amount'][$key]);
                 //CRM-15350: if soft-credit-type profile field is disabled or removed then
                 //we choose configured SCT default value
                 if (!empty($params['soft_credit_type'][$key])) {
                     $value['soft_credit'][$key]['soft_credit_type_id'] = $params['soft_credit_type'][$key];
                 } else {
                     $value['soft_credit'][$key]['soft_credit_type_id'] = CRM_Core_OptionGroup::getDefaultValue("soft_credit_type");
                 }
             }
             $value['custom'] = CRM_Core_BAO_CustomField::postProcess($value, NULL, 'Contribution');
             foreach ($dates as $val) {
                 if (!empty($value[$val])) {
                     $value[$val] = CRM_Utils_Date::processDate($value[$val], $value[$val . '_time'], TRUE);
                 }
             }
             if (!empty($value['send_receipt'])) {
                 $value['receipt_date'] = date('Y-m-d His');
             }
             // these translations & date handling are required because we are calling BAO directly rather than the api
             $fieldTranslations = array('financial_type' => 'financial_type_id', 'payment_instrument' => 'payment_instrument_id', 'contribution_source' => 'source', 'contribution_note' => 'note');
             foreach ($fieldTranslations as $formField => $baoField) {
                 if (isset($value[$formField])) {
                     $value[$baoField] = $value[$formField];
                 }
                 unset($value[$formField]);
             }
             $params['actualBatchTotal'] += $value['total_amount'];
             $value['batch_id'] = $this->_batchId;
             $value['skipRecentView'] = TRUE;
             // build line item params
             $this->_priceSet['fields'][$priceFieldID]['options'][$priceFieldValueID]['amount'] = $value['total_amount'];
             $value['price_' . $priceFieldID] = 1;
             $lineItem = array();
             CRM_Price_BAO_PriceSet::processAmount($this->_priceSet['fields'], $value, $lineItem[$priceSetId]);
             //unset amount level since we always use quick config price set
             unset($value['amount_level']);
             //CRM-11529 for back office transactions
             //when financial_type_id is passed in form, update the
             //line items with the financial type selected in form
             if (!empty($value['financial_type_id']) && !empty($lineItem[$priceSetId])) {
                 foreach ($lineItem[$priceSetId] as &$values) {
                     $values['financial_type_id'] = $value['financial_type_id'];
                 }
             }
             $value['line_item'] = $lineItem;
             //finally call contribution create for all the magic
             $contribution = CRM_Contribute_BAO_Contribution::create($value, CRM_Core_DAO::$_nullArray);
             $batchTypes = CRM_Core_Pseudoconstant::get('CRM_Batch_DAO_Batch', 'type_id', array('flip' => 1), 'validate');
             if (!empty($this->_batchInfo['type_id']) && $this->_batchInfo['type_id'] == $batchTypes['Pledge Payment']) {
                 $adjustTotalAmount = FALSE;
                 if (isset($params['option_type'][$key])) {
                     if ($params['option_type'][$key] == 2) {
                         $adjustTotalAmount = TRUE;
                     }
                 }
                 $pledgeId = $params['open_pledges'][$key];
                 if (is_numeric($pledgeId)) {
                     $result = CRM_Pledge_BAO_PledgePayment::getPledgePayments($pledgeId);
                     $pledgePaymentId = 0;
                     foreach ($result as $key => $values) {
                         if ($values['status'] != 'Completed') {
                             $pledgePaymentId = $values['id'];
                             break;
                         }
                     }
                     CRM_Core_DAO::setFieldValue('CRM_Pledge_DAO_PledgePayment', $pledgePaymentId, 'contribution_id', $contribution->id);
                     CRM_Pledge_BAO_PledgePayment::updatePledgePaymentStatus($pledgeId, array($pledgePaymentId), $contribution->contribution_status_id, NULL, $contribution->total_amount, $adjustTotalAmount);
                 }
             }
             //process premiums
             if (!empty($value['product_name'])) {
                 if ($value['product_name'][0] > 0) {
                     list($products, $options) = CRM_Contribute_BAO_Premium::getPremiumProductInfo();
                     $value['hidden_Premium'] = 1;
                     $value['product_option'] = CRM_Utils_Array::value($value['product_name'][1], $options[$value['product_name'][0]]);
                     $premiumParams = array('product_id' => $value['product_name'][0], 'contribution_id' => $contribution->id, 'product_option' => $value['product_option'], 'quantity' => 1);
                     CRM_Contribute_BAO_Contribution::addPremium($premiumParams);
                 }
             }
             // end of premium
             //send receipt mail.
             if ($contribution->id && !empty($value['send_receipt'])) {
                 // add the domain email id
                 $domainEmail = CRM_Core_BAO_Domain::getNameAndEmail();
                 $domainEmail = "{$domainEmail['0']} <{$domainEmail['1']}>";
                 $value['from_email_address'] = $domainEmail;
                 $value['contribution_id'] = $contribution->id;
                 CRM_Contribute_Form_AdditionalInfo::emailReceipt($this, $value);
             }
         }
     }
     return TRUE;
 }
 /**
  * @param string $context
  * @return array|bool
  */
 public function getOptions($context = NULL)
 {
     CRM_Core_DAO::buildOptionsContext($context);
     if (!$this->id) {
         return FALSE;
     }
     if (!$this->data_type || !$this->custom_group_id) {
         $this->find(TRUE);
     }
     if (!empty($this->option_group_id)) {
         $options = CRM_Core_OptionGroup::valuesByID($this->option_group_id, FALSE, FALSE, FALSE, 'label', !($context == 'validate' || $context == 'get'));
     } elseif ($this->data_type === 'StateProvince') {
         $options = CRM_Core_Pseudoconstant::stateProvince();
     } elseif ($this->data_type === 'Country') {
         $options = $context == 'validate' ? CRM_Core_Pseudoconstant::countryIsoCode() : CRM_Core_Pseudoconstant::country();
     } elseif ($this->data_type === 'Boolean') {
         $options = $context == 'validate' ? array(0, 1) : CRM_Core_SelectValues::boolean();
     } else {
         return FALSE;
     }
     CRM_Utils_Hook::customFieldOptions($this->id, $options, FALSE);
     CRM_Utils_Hook::fieldOptions($this->getEntity(), "custom_{$this->id}", $options, array('context' => $context));
     return $options;
 }
Exemple #5
0
 /**
  * Do AlterDisplay processing on Address Fields.
  *
  * @param array $row
  * @param array $rows
  * @param int $rowNum
  * @param string $baseUrl
  * @param string $linkText
  *
  * @return bool
  */
 public function alterDisplayContactFields(&$row, &$rows, &$rowNum, $baseUrl, $linkText)
 {
     $entryFound = FALSE;
     // There is no reason not to add links for all fields but it seems a bit odd to be able to click on
     // 'Mrs'. Also, we don't have metadata about the title. So, add selectively to addLinks.
     $addLinks = array('gender_id' => 'Gender');
     foreach (array('prefix_id', 'suffix_id', 'gender_id') as $fieldName) {
         if (array_key_exists('civicrm_contact_' . $fieldName, $row)) {
             if (($value = $row['civicrm_contact_' . $fieldName]) != FALSE) {
                 $rows[$rowNum]['civicrm_contact_' . $fieldName] = CRM_Core_Pseudoconstant::getLabel('CRM_Contact_BAO_Contact', $fieldName, $value);
                 if (($title = CRM_Utils_Array::value($fieldName, $addLinks)) != FALSE) {
                     $this->addLinkToRow($rows[$rowNum], $baseUrl, $linkText, $value, $fieldName, 'civicrm_contact', $title);
                 }
             }
             $entryFound = TRUE;
         }
     }
     return $entryFound;
 }
Exemple #6
0
/**
 * Get output for activity list.
 *
 * @see _civicrm_api3_generic_getlist_output
 *
 * @param array $result
 * @param array $request
 *
 * @return array
 */
function _civicrm_api3_activity_getlist_output($result, $request)
{
    $output = array();
    if (!empty($result['values'])) {
        foreach ($result['values'] as $row) {
            $data = array('id' => $row[$request['id_field']], 'label' => $row[$request['label_field']] ? $row[$request['label_field']] : ts('(no subject)'), 'description' => array(CRM_Core_Pseudoconstant::getLabel('CRM_Activity_BAO_Activity', 'activity_type_id', $row['activity_type_id'])));
            if (!empty($row['activity_date_time'])) {
                $data['description'][0] .= ': ' . CRM_Utils_Date::customFormat($row['activity_date_time']);
            }
            if (!empty($row['source_contact_id'])) {
                $data['description'][] = ts('By %1', array(1 => CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $row['source_contact_id'], 'display_name')));
            }
            // Add repeating info
            $repeat = CRM_Core_BAO_RecurringEntity::getPositionAndCount($row['id'], 'civicrm_activity');
            $data['extra']['is_recur'] = FALSE;
            if ($repeat) {
                $data['suffix'] = ts('(%1 of %2)', array(1 => $repeat[0], 2 => $repeat[1]));
                $data['extra']['is_recur'] = TRUE;
            }
            $output[] = $data;
        }
    }
    return $output;
}
Exemple #7
0
 /**
  *  Test civicrm_constant_get( 'locationType' ) with all=true
  */
 public function testLocationTypeAll()
 {
     // needed to get rid of cached values from previous tests
     CRM_Core_Pseudoconstant::flush('locationType');
     //  Testing on default set provided by civicrm_data.mysql
     $result = civicrm_constant_get('locationType', array('all' => true));
     $this->assertEquals(5, count($result), "In line " . __LINE__);
     $this->assertContains('Home', $result, "In line " . __LINE__);
     $this->assertContains('Work', $result, "In line " . __LINE__);
     $this->assertContains('Main', $result, "In line " . __LINE__);
     $this->assertContains('Other', $result, "In line " . __LINE__);
     $this->assertContains('Billing', $result, "In line " . __LINE__);
     $this->assertTrue(empty($result['is_error']), "In line " . __LINE__);
 }
Exemple #8
0
/**
 * Get event list output.
 *
 * @see _civicrm_api3_generic_getlist_output
 *
 * @param array $result
 * @param array $request
 *
 * @return array
 */
function _civicrm_api3_event_getlist_output($result, $request)
{
    $output = array();
    if (!empty($result['values'])) {
        foreach ($result['values'] as $row) {
            $data = array('id' => $row[$request['id_field']], 'label' => $row[$request['label_field']], 'description' => array(CRM_Core_Pseudoconstant::getLabel('CRM_Event_BAO_Event', 'event_type_id', $row['event_type_id'])));
            if (!empty($row['start_date'])) {
                $data['description'][0] .= ': ' . CRM_Utils_Date::customFormat($row['start_date']);
            }
            if (!empty($row['summary'])) {
                $data['description'][] = $row['summary'];
            }
            // Add repeating info
            $repeat = CRM_Core_BAO_RecurringEntity::getPositionAndCount($row['id'], 'civicrm_event');
            $data['extra']['is_recur'] = FALSE;
            if ($repeat) {
                $data['suffix'] = ts('(%1 of %2)', array(1 => $repeat[0], 2 => $repeat[1]));
                $data['extra']['is_recur'] = TRUE;
            }
            $output[] = $data;
        }
    }
    return $output;
}
Exemple #9
0
 /**
  * Function to process the activities
  *
  * @param object $form         form object
  * @param array  $params       associated array of the submitted values
  * @param array  $ids          array of ids
  * @param string $activityType activity Type
  * @param boolean $record   true if it is Record Activity 
  * @access public
  * @return
  */
 public function create(&$params)
 {
     // check required params
     if (!self::dataExists($params)) {
         CRM_Core_Error::fatal('Not enough data to create activity object,');
     }
     $activity =& new CRM_Activity_DAO_Activity();
     if (!CRM_Utils_Array::value('status_id', $params)) {
         if (isset($params['activity_date_time']) && strcmp($params['activity_date_time'], CRM_Utils_Date::processDate(date('Ymd')) == -1)) {
             $params['status_id'] = 2;
         } else {
             $params['status_id'] = 1;
         }
     }
     //set priority to Normal for Auto-populated activities (for Cases)
     if (!CRM_Utils_Array::value('priority_id', $params)) {
         require_once 'CRM/Core/PseudoConstant.php';
         $priority = CRM_Core_PseudoConstant::priority();
         $params['priority_id'] = array_search('Normal', $priority);
     }
     if (empty($params['id'])) {
         unset($params['id']);
     }
     if (!empty($params['target_contact_id']) && is_array($params['target_contact_id'])) {
         $params['target_contact_id'] = array_unique($params['target_contact_id']);
     }
     if (!empty($params['assignee_contact_id']) && is_array($params['assignee_contact_id'])) {
         $params['assignee_contact_id'] = array_unique($params['assignee_contact_id']);
     }
     $activity->copyValues($params);
     // start transaction
     require_once 'CRM/Core/Transaction.php';
     $transaction = new CRM_Core_Transaction();
     $result = $activity->save();
     if (is_a($result, 'CRM_Core_Error')) {
         $transaction->rollback();
         return $result;
     }
     $activityId = $activity->id;
     // check and attach and files as needed
     require_once 'CRM/Core/BAO/File.php';
     CRM_Core_BAO_File::processAttachment($params, 'civicrm_activity', $activityId);
     // attempt to save activity assignment
     $resultAssignment = null;
     if (CRM_Utils_Array::value('assignee_contact_id', $params)) {
         require_once 'CRM/Activity/BAO/ActivityAssignment.php';
         $assignmentParams = array('activity_id' => $activityId);
         if (is_array($params['assignee_contact_id'])) {
             if (CRM_Utils_Array::value('deleteActivityAssignment', $params, true)) {
                 // first delete existing assignments if any
                 self::deleteActivityAssignment($activityId);
             }
             foreach ($params['assignee_contact_id'] as $acID) {
                 if ($acID) {
                     $assignmentParams['assignee_contact_id'] = $acID;
                     $resultAssignment = CRM_Activity_BAO_ActivityAssignment::create($assignmentParams);
                     if (is_a($resultAssignment, 'CRM_Core_Error')) {
                         $transaction->rollback();
                         return $resultAssignment;
                     }
                 }
             }
         } else {
             $assignmentParams['assignee_contact_id'] = $params['assignee_contact_id'];
             if (CRM_Utils_Array::value('id', $params)) {
                 $assignment =& new CRM_Activity_BAO_ActivityAssignment();
                 $assignment->activity_id = $activityId;
                 $assignment->find(true);
                 if ($assignment->assignee_contact_id != $params['assignee_contact_id']) {
                     $assignmentParams['id'] = $assignment->id;
                     $resultAssignment = CRM_Activity_BAO_ActivityAssignment::create($assignmentParams);
                 }
             } else {
                 $resultAssignment = CRM_Activity_BAO_ActivityAssignment::create($assignmentParams);
             }
         }
     } else {
         if (CRM_Utils_Array::value('deleteActivityAssignment', $params, true)) {
             self::deleteActivityAssignment($activityId);
         }
     }
     if (is_a($resultAssignment, 'CRM_Core_Error')) {
         $transaction->rollback();
         return $resultAssignment;
     }
     // attempt to save activity targets
     $resultTarget = null;
     if (CRM_Utils_Array::value('target_contact_id', $params)) {
         $targetParams = array('activity_id' => $activityId);
         $resultTarget = array();
         if (is_array($params['target_contact_id'])) {
             if (CRM_Utils_Array::value('deleteActivityTarget', $params, true)) {
                 // first delete existing targets if any
                 self::deleteActivityTarget($activityId);
             }
             foreach ($params['target_contact_id'] as $tid) {
                 if ($tid) {
                     $targetParams['target_contact_id'] = $tid;
                     $resultTarget = CRM_Activity_BAO_ActivityTarget::create($targetParams);
                     if (is_a($resultTarget, 'CRM_Core_Error')) {
                         $transaction->rollback();
                         return $resultTarget;
                     }
                 }
             }
         } else {
             $targetParams['target_contact_id'] = $params['target_contact_id'];
             if (CRM_Utils_Array::value('id', $params)) {
                 $target =& new CRM_Activity_BAO_ActivityTarget();
                 $target->activity_id = $activityId;
                 $target->find(true);
                 if ($target->target_contact_id != $params['target_contact_id']) {
                     $targetParams['id'] = $target->id;
                     $resultTarget = CRM_Activity_BAO_ActivityTarget::create($targetParams);
                 }
             } else {
                 $resultTarget = CRM_Activity_BAO_ActivityTarget::create($targetParams);
             }
         }
     } else {
         if (CRM_Utils_Array::value('deleteActivityTarget', $params, true)) {
             self::deleteActivityTarget($activityId);
         }
     }
     // write to changelog before transation is committed/rolled
     // back (and prepare status to display)
     if (CRM_Utils_Array::value('id', $params)) {
         $logMsg = "Activity (id: {$result->id} ) updated with ";
     } else {
         $logMsg = "Activity created for ";
     }
     $msgs = array();
     if (isset($params['source_contact_id'])) {
         $msgs[] = "source={$params['source_contact_id']}";
     }
     if (CRM_Utils_Array::value('target_contact_id', $params)) {
         if (is_array($params['target_contact_id']) && !CRM_Utils_array::crmIsEmptyArray($params['target_contact_id'])) {
             $msgs[] = "target=" . implode(',', $params['target_contact_id']);
             // take only first target
             // will be used for recently viewed display
             $t = array_slice($params['target_contact_id'], 0, 1);
             $recentContactId = $t[0];
         } else {
             if (isset($params['target_contact_id'])) {
                 $msgs[] = "target={$params['target_contact_id']}";
                 // will be used for recently viewed display
                 $recentContactId = $params['target_contact_id'];
             }
         }
     } else {
         // at worst, take source for recently viewed display
         $recentContactId = $params['source_contact_id'];
     }
     if (isset($params['assignee_contact_id'])) {
         if (is_array($params['assignee_contact_id'])) {
             $msgs[] = "assignee=" . implode(',', $params['assignee_contact_id']);
         } else {
             $msgs[] = "assignee={$params['assignee_contact_id']}";
         }
     }
     $logMsg .= implode(', ', $msgs);
     self::logActivityAction($result, $logMsg);
     if (CRM_Utils_Array::value('custom', $params) && is_array($params['custom'])) {
         require_once 'CRM/Core/BAO/CustomValueTable.php';
         CRM_Core_BAO_CustomValueTable::store($params['custom'], 'civicrm_activity', $result->id);
     }
     $transaction->commit();
     if (!CRM_Utils_Array::value('skipRecentView', $params)) {
         require_once 'CRM/Utils/Recent.php';
         if (CRM_Utils_Array::value('case_id', $params)) {
             $caseContactID = CRM_Core_DAO::getFieldValue('CRM_Case_DAO_CaseContact', $params['case_id'], 'contact_id', 'case_id');
             $url = CRM_Utils_System::url('civicrm/case/activity/view', "reset=1&aid={$activity->id}&cid={$caseContactID}&caseID={$params['case_id']}&context=home");
         } else {
             $q = "action=view&reset=1&id={$activity->id}&atype={$activity->activity_type_id}&cid={$activity->source_contact_id}&context=home";
             if ($activity->activity_type_id != CRM_Core_OptionGroup::getValue('activity_type', 'Email', 'name')) {
                 $url = CRM_Utils_System::url('civicrm/contact/view/activity', $q);
             } else {
                 $url = CRM_Utils_System::url('civicrm/activity', $q);
             }
         }
         if (!isset($activity->parent_id)) {
             require_once 'CRM/Contact/BAO/Contact.php';
             $recentContactDisplay = CRM_Contact_BAO_Contact::displayName($recentContactId);
             // add the recently created Activity
             $activityTypes = CRM_Core_Pseudoconstant::activityType(true, true);
             $activitySubject = CRM_Core_DAO::getFieldValue('CRM_Activity_DAO_Activity', $activity->id, 'subject');
             $title = "";
             if (isset($activitySubject)) {
                 $title = $activitySubject . ' - ';
             }
             $title = $title . $recentContactDisplay . ' (' . $activityTypes[$activity->activity_type_id] . ')';
             CRM_Utils_Recent::add($title, $url, $activity->id, 'Activity', $recentContactId, $recentContactDisplay);
         }
     }
     if (CRM_Utils_Array::value('id', $params)) {
         CRM_Utils_Hook::post('edit', 'Activity', $activity->id, $activity);
     } else {
         CRM_Utils_Hook::post('create', 'Activity', $activity->id, $activity);
     }
     return $result;
 }
Exemple #10
0
 /**
  * Function to process the view
  *
  * @access public
  * @return None
  */
 public function preProcess()
 {
     $contactID = CRM_Utils_Request::retrieve('cid', 'Integer', $this, true);
     $activityID = CRM_Utils_Request::retrieve('aid', 'Integer', $this, true);
     $revs = CRM_Utils_Request::retrieve('revs', 'Boolean', CRM_Core_DAO::$_nullObject);
     $caseID = CRM_Utils_Request::retrieve('caseID', 'Boolean', CRM_Core_DAO::$_nullObject);
     $activitySubject = CRM_Core_DAO::getFieldValue('CRM_Activity_DAO_Activity', $activityID, 'subject');
     $this->assign('contactID', $contactID);
     $this->assign('caseID', $caseID);
     require_once 'CRM/Case/XMLProcessor/Report.php';
     $xmlProcessor = new CRM_Case_XMLProcessor_Report();
     $report = $xmlProcessor->getActivityInfo($contactID, $activityID, true);
     require_once 'CRM/Core/BAO/File.php';
     $attachmentUrl = CRM_Core_BAO_File::attachmentInfo('civicrm_activity', $activityID);
     if ($attachmentUrl) {
         $report['fields'][] = array('label' => 'Attachment(s)', 'value' => $attachmentUrl, 'type' => 'Link');
     }
     $this->assign('report', $report);
     $latestRevisionID = CRM_Activity_BAO_Activity::getLatestActivityId($activityID);
     if ($revs) {
         $this->assign('revs', $revs);
         $priorActivities = CRM_Activity_BAO_Activity::getPriorAcitivities($activityID);
         $this->assign('result', $priorActivities);
         $this->assign('subject', $activitySubject);
         $this->assign('latestRevisionID', $latestRevisionID);
     } else {
         $countPriorActivities = CRM_Activity_BAO_Activity::getPriorCount($activityID);
         if ($countPriorActivities >= 1) {
             $this->assign('activityID', $activityID);
         }
         if ($latestRevisionID != $activityID) {
             $this->assign('latestRevisionID', $latestRevisionID);
         }
     }
     $parentID = CRM_Activity_BAO_Activity::getParentActivity($activityID);
     if ($parentID) {
         $this->assign('parentID', $parentID);
     }
     //viewing activity should get diplayed in recent list.CRM-4670
     $activityTypeID = CRM_Core_DAO::getFieldValue('CRM_Activity_DAO_Activity', $activityID, 'activity_type_id');
     $activityTargetContacts = CRM_Activity_BAO_ActivityTarget::retrieveTargetIdsByActivityId($activityID);
     if (!empty($activityTargetContacts)) {
         $recentContactId = $activityTargetContacts[1];
     } else {
         $recentContactId = $contactID;
     }
     if (!isset($caseID)) {
         $caseID = CRM_Core_DAO::getFieldValue('CRM_Case_DAO_CaseActivity', $activityID, 'case_id', 'activity_id');
     }
     require_once 'CRM/Utils/Recent.php';
     $url = CRM_Utils_System::url('civicrm/case/activity/view', "reset=1&aid={$activityID}&cid={$recentContactId}&caseID={$caseID}");
     require_once 'CRM/Contact/BAO/Contact.php';
     $recentContactDisplay = CRM_Contact_BAO_Contact::displayName($recentContactId);
     // add the recently created Activity
     $activityTypes = CRM_Core_Pseudoconstant::activityType(true, true);
     $title = "";
     if (isset($activitySubject)) {
         $title = $activitySubject . ' - ';
     }
     $title = $title . $recentContactDisplay . ' (' . $activityTypes[$activityTypeID] . ')';
     CRM_Utils_Recent::add($title, $url, $activityID, 'Activity', $recentContactId, $recentContactDisplay);
 }
Exemple #11
0
/**
 * @see _civicrm_api3_generic_getlist_output
 *
 * @param $result array
 * @param $request array
 *
 * @return array
 */
function _civicrm_api3_event_getlist_output($result, $request)
{
    $output = array();
    if (!empty($result['values'])) {
        foreach ($result['values'] as $row) {
            $data = array('id' => $row[$request['id_field']], 'label' => $row[$request['label_field']], 'description' => array(CRM_Core_Pseudoconstant::getLabel('CRM_Event_BAO_Event', 'event_type_id', $row['event_type_id'])));
            if (!empty($row['start_date'])) {
                $data['description'][0] .= ': ' . CRM_Utils_Date::customFormat($row['start_date']);
            }
            if (!empty($row['summary'])) {
                $data['description'][] = $row['summary'];
            }
            foreach ($request['extra'] as $field) {
                $data['extra'][$field] = isset($row[$field]) ? $row[$field] : NULL;
            }
            $output[] = $data;
        }
    }
    return $output;
}
Exemple #12
0
 /**
  * Add the formatted address to $this-> display
  *
  * @param NULL
  * 
  * @return void
  *
  * @access public
  *
  */
 function addDisplay($microformat = false)
 {
     require_once 'CRM/Utils/Address.php';
     $fields = array('address_id' => $this->id, 'address_name' => str_replace('', ' ', $this->name), 'street_address' => $this->street_address, 'supplemental_address_1' => $this->supplemental_address_1, 'supplemental_address_2' => $this->supplemental_address_2, 'city' => $this->city, 'state_province_name' => isset($this->state_name) ? $this->state_name : "", 'state_province' => isset($this->state) ? $this->state : "", 'postal_code' => isset($this->postal_code) ? $this->postal_code : "", 'postal_code_suffix' => isset($this->postal_code_suffix) ? $this->postal_code_suffix : "", 'country' => isset($this->country) ? $this->country : "", 'world_region' => isset($this->world_region) ? $this->world_region : "");
     if (isset($this->county_id) && $this->county_id) {
         $fields['county'] = CRM_Core_Pseudoconstant::county($this->county_id);
     } else {
         $fields['county'] = null;
     }
     $this->display = CRM_Utils_Address::format($fields, null, $microformat);
     $this->display_text = CRM_Utils_Address::format($fields);
 }
Exemple #13
0
 /**
  * Function to build tag widget if correct parent is passed
  * 
  * @param object  $form form object
  * @param string  $parentName parent name ( tag name)
  * @param string  $entityTable entitytable 'eg: civicrm_contact'
  * @param int     $entityId    entityid  'eg: contact id'
  *
  * @return void
  * @access public
  * @static
  */
 static function buildQuickForm(&$form, $parentNames, $entityTable, $entityId = null, $skipTagCreate = false, $skipEntityAction = false)
 {
     $tagset = $form->_entityTagValues = array();
     foreach ($parentNames as &$parentNameItem) {
         // get the parent id for tag list input for keyword
         $parentId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Tag', $parentNameItem, 'id', 'name');
         // check if parent exists
         $entityTags = array();
         if ($parentId) {
             $tagsetItem = 'parentId_' . $parentId;
             $tagset[$tagsetItem]['parentName'] = $parentNameItem;
             $tagset[$tagsetItem]['parentID'] = $parentId;
             //tokeninput url
             $tagUrl = CRM_Utils_System::url('civicrm/ajax/taglist', "parentId={$parentId}", false, null, false);
             $tagset[$tagsetItem]['tagUrl'] = $tagUrl;
             $tagset[$tagsetItem]['entityTable'] = $entityTable;
             $tagset[$tagsetItem]['skipTagCreate'] = $skipTagCreate;
             $tagset[$tagsetItem]['skipEntityAction'] = $skipEntityAction;
             $tagset[$tagsetItem]['tagElementName'] = "taglist[{$parentId}]";
             $form->add('text', "taglist[{$parentId}]", null);
             if ($entityId) {
                 $tagset[$tagsetItem]['entityId'] = $entityId;
                 require_once 'CRM/Core/BAO/EntityTag.php';
                 $entityTags = CRM_Core_BAO_EntityTag::getChildEntityTags($parentId, $entityId, $entityTable);
             } elseif (!empty($form->_submitValues['taglist']) && CRM_Utils_Array::value($parentId, $form->_submitValues['taglist'])) {
                 $allTags = CRM_Core_Pseudoconstant::tag();
                 $tagIds = explode(',', $form->_submitValues['taglist'][$parentId]);
                 foreach ($tagIds as $tagId) {
                     if (is_numeric($tagId)) {
                         $tagName = $allTags[$tagId];
                     } else {
                         $tagName = $tagId;
                     }
                     $entityTags[$tagId] = array('id' => $tagId, 'name' => $tagName);
                 }
             } elseif (!empty($form->_formValues['contact_tags'])) {
                 require_once 'CRM/Core/BAO/Tag.php';
                 $contactTags = CRM_Core_BAO_Tag::getTagsUsedFor('civicrm_contact', true, false, $parentId);
                 foreach (array_keys($form->_formValues['contact_tags']) as $tagId) {
                     if (CRM_Utils_Array::value($tagId, $contactTags)) {
                         $tagName = $tagId;
                         if (is_numeric($tagId)) {
                             $tagName = $contactTags[$tagId];
                         }
                         $entityTags[$tagId] = array('id' => $tagId, 'name' => $tagName);
                     }
                 }
             }
             if (!empty($entityTags)) {
                 // assign as simple array for display in smarty
                 $tagset[$tagsetItem]['entityTagsArray'] = $entityTags;
                 // assign as json for js widget
                 $tagset[$tagsetItem]['entityTags'] = json_encode(array_values($entityTags));
                 if (!empty($form->_entityTagValues)) {
                     $form->_entityTagValues = CRM_Utils_Array::crmArrayMerge($entityTags, $form->_entityTagValues);
                 } else {
                     $form->_entityTagValues = $entityTags;
                 }
             }
         }
     }
     $form->assign('tagset', $tagset);
 }
 /**
  * Function to process the view
  *
  * @access public
  * @return None
  */
 public function preProcess()
 {
     $contactID = CRM_Utils_Request::retrieve('cid', 'Integer', $this, true);
     $activityID = CRM_Utils_Request::retrieve('aid', 'Integer', $this, true);
     $revs = CRM_Utils_Request::retrieve('revs', 'Boolean', CRM_Core_DAO::$_nullObject);
     $caseID = CRM_Utils_Request::retrieve('caseID', 'Boolean', CRM_Core_DAO::$_nullObject);
     $activitySubject = CRM_Core_DAO::getFieldValue('CRM_Activity_DAO_Activity', $activityID, 'subject');
     $type = CRM_Utils_Request::retrieve('type', 'String', CRM_Core_DAO::$_nullObject);
     //check for required permissions, CRM-6264
     if ($activityID && !CRM_Activity_BAO_Activity::checkPermission($activityID, CRM_Core_Action::VIEW)) {
         CRM_Core_Error::fatal(ts('You do not have permission to access this page.'));
     }
     $this->assign('contactID', $contactID);
     $this->assign('caseID', $caseID);
     $this->assign('type', $type);
     require_once 'CRM/Case/XMLProcessor/Report.php';
     $xmlProcessor = new CRM_Case_XMLProcessor_Report();
     $report = $xmlProcessor->getActivityInfo($contactID, $activityID, true);
     require_once 'CRM/Core/BAO/File.php';
     $attachmentUrl = CRM_Core_BAO_File::attachmentInfo('civicrm_activity', $activityID);
     if ($attachmentUrl) {
         $report['fields'][] = array('label' => 'Attachment(s)', 'value' => $attachmentUrl, 'type' => 'Link');
     }
     require_once 'CRM/Core/BAO/EntityTag.php';
     $tags = CRM_Core_BAO_EntityTag::getTag($activityID, 'civicrm_activity');
     if (!empty($tags)) {
         $allTag = CRM_Core_PseudoConstant::tag();
         foreach ($tags as $tid) {
             $tags[$tid] = $allTag[$tid];
         }
         $report['fields'][] = array('label' => 'Tags', 'value' => implode('<br />', $tags), 'type' => 'String');
     }
     $this->assign('report', $report);
     $latestRevisionID = CRM_Activity_BAO_Activity::getLatestActivityId($activityID);
     $viewPriorActivities = array();
     $priorActivities = CRM_Activity_BAO_Activity::getPriorAcitivities($activityID);
     foreach ($priorActivities as $activityId => $activityValues) {
         if (CRM_Case_BAO_Case::checkPermission($activityId, 'view', null, $contactID)) {
             $viewPriorActivities[$activityId] = $activityValues;
         }
     }
     if ($revs) {
         $this->assign('revs', $revs);
         $this->assign('result', $viewPriorActivities);
         $this->assign('subject', $activitySubject);
         $this->assign('latestRevisionID', $latestRevisionID);
     } else {
         if (count($viewPriorActivities) > 1) {
             $this->assign('activityID', $activityID);
         }
         if ($latestRevisionID != $activityID) {
             $this->assign('latestRevisionID', $latestRevisionID);
         }
     }
     $parentID = CRM_Activity_BAO_Activity::getParentActivity($activityID);
     if ($parentID) {
         $this->assign('parentID', $parentID);
     }
     //viewing activity should get diplayed in recent list.CRM-4670
     $activityTypeID = CRM_Core_DAO::getFieldValue('CRM_Activity_DAO_Activity', $activityID, 'activity_type_id');
     $activityTargetContacts = CRM_Activity_BAO_ActivityTarget::retrieveTargetIdsByActivityId($activityID);
     if (!empty($activityTargetContacts)) {
         $recentContactId = $activityTargetContacts[0];
     } else {
         $recentContactId = $contactID;
     }
     if (!isset($caseID)) {
         $caseID = CRM_Core_DAO::getFieldValue('CRM_Case_DAO_CaseActivity', $activityID, 'case_id', 'activity_id');
     }
     require_once 'CRM/Utils/Recent.php';
     $url = CRM_Utils_System::url('civicrm/case/activity/view', "reset=1&aid={$activityID}&cid={$recentContactId}&caseID={$caseID}&context=home");
     require_once 'CRM/Contact/BAO/Contact.php';
     $recentContactDisplay = CRM_Contact_BAO_Contact::displayName($recentContactId);
     // add the recently created Activity
     $activityTypes = CRM_Core_Pseudoconstant::activityType(true, true);
     $title = "";
     if (isset($activitySubject)) {
         $title = $activitySubject . ' - ';
     }
     $title = $title . $recentContactDisplay . ' (' . $activityTypes[$activityTypeID] . ')';
     require_once 'CRM/Case/BAO/Case.php';
     $recentOther = array();
     if (CRM_Case_BAO_Case::checkPermission($activityID, 'edit')) {
         $recentOther['editUrl'] = CRM_Utils_System::url('civicrm/case/activity', "reset=1&action=update&id={$activityID}&cid={$recentContactId}&caseid={$caseID}&context=home");
     }
     if (CRM_Case_BAO_Case::checkPermission($activityID, 'delete')) {
         $recentOther['deleteUrl'] = CRM_Utils_System::url('civicrm/case/activity', "reset=1&action=delete&id={$activityID}&cid={$recentContactId}&caseid={$caseID}&context=home");
     }
     CRM_Utils_Recent::add($title, $url, $activityID, 'Activity', $recentContactId, $recentContactDisplay, $recentOther);
 }
Exemple #15
0
 /**
  * @param $rows
  */
 public function alterDisplay(&$rows)
 {
     // Custom code to alter rows.
     $entryFound = FALSE;
     foreach ($rows as $rowNum => $row) {
         // Handle ID to label conversion for contact fields
         $entryFound = $this->alterDisplayContactFields($row, $rows, $rowNum, 'contact/relationship', 'View Relationships') ? TRUE : $entryFound;
         // Handle contact subtype A
         // @todo refactor into separate function
         if (array_key_exists('civicrm_contact_contact_sub_type_a', $row)) {
             if ($value = $row['civicrm_contact_contact_sub_type_a']) {
                 $rowValues = explode(CRM_Core_DAO::VALUE_SEPARATOR, $value);
                 $rowLabels = array();
                 foreach ($rowValues as $rowValue) {
                     if ($rowValue) {
                         $rowLabels[] = CRM_Core_Pseudoconstant::getLabel('CRM_Contact_BAO_Contact', 'contact_sub_type', $rowValue);
                     }
                 }
                 $rows[$rowNum]['civicrm_contact_contact_sub_type_a'] = implode(', ', $rowLabels);
             }
             $entryFound = TRUE;
         }
         // Handle contact subtype B
         // @todo refactor into separate function
         if (array_key_exists('civicrm_contact_b_contact_sub_type_b', $row)) {
             if ($value = $row['civicrm_contact_b_contact_sub_type_b']) {
                 $rowValues = explode(CRM_Core_DAO::VALUE_SEPARATOR, $value);
                 $rowLabels = array();
                 foreach ($rowValues as $rowValue) {
                     if ($rowValue) {
                         $rowLabels[] = CRM_Core_Pseudoconstant::getLabel('CRM_Contact_BAO_Contact', 'contact_sub_type', $rowValue);
                     }
                 }
                 $rows[$rowNum]['civicrm_contact_b_contact_sub_type_b'] = implode(', ', $rowLabels);
             }
             $entryFound = TRUE;
         }
         // Handle country
         // @todo use alterDisplayAddressFields function
         if (array_key_exists('civicrm_address_country_id', $row)) {
             if ($value = $row['civicrm_address_country_id']) {
                 $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant::country($value, FALSE);
             }
             $entryFound = TRUE;
         }
         // Handle state/province
         // @todo use alterDisplayAddressFields function
         if (array_key_exists('civicrm_address_state_province_id', $row)) {
             if ($value = $row['civicrm_address_state_province_id']) {
                 $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant::stateProvince($value, FALSE);
             }
             $entryFound = TRUE;
         }
         // Handle contact name A
         // @todo refactor into separate function
         if (array_key_exists('civicrm_contact_sort_name_a', $row) && array_key_exists('civicrm_contact_id', $row)) {
             $url = CRM_Report_Utils_Report::getNextUrl('contact/detail', 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id'], $this->_absoluteUrl, $this->_id, $this->_drilldownReport);
             $rows[$rowNum]['civicrm_contact_sort_name_a'] = $rows[$rowNum]['civicrm_contact_sort_name_a'] . ' (' . $rows[$rowNum]['civicrm_contact_id'] . ')';
             $rows[$rowNum]['civicrm_contact_sort_name_a_link'] = $url;
             $rows[$rowNum]['civicrm_contact_sort_name_a_hover'] = ts('View Contact Detail Report for this contact');
             $entryFound = TRUE;
         }
         // Handle contact name B
         // @todo refactor into separate function
         if (array_key_exists('civicrm_contact_b_sort_name_b', $row) && array_key_exists('civicrm_contact_b_id', $row)) {
             $url = CRM_Report_Utils_Report::getNextUrl('contact/detail', 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_b_id'], $this->_absoluteUrl, $this->_id, $this->_drilldownReport);
             $rows[$rowNum]['civicrm_contact_b_sort_name_b'] = $rows[$rowNum]['civicrm_contact_b_sort_name_b'] . ' (' . $rows[$rowNum]['civicrm_contact_b_id'] . ')';
             $rows[$rowNum]['civicrm_contact_b_sort_name_b_link'] = $url;
             $rows[$rowNum]['civicrm_contact_b_sort_name_b_hover'] = ts('View Contact Detail Report for this contact');
             $entryFound = TRUE;
         }
         // Handle relationship
         if (array_key_exists('civicrm_relationship_relationship_id', $row) && array_key_exists('civicrm_contact_id', $row)) {
             $url = "/civicrm/contact/view/rel?reset=1&action=update&rtype=a_b&cid=" . $row['civicrm_contact_id'] . "&id=" . $row['civicrm_relationship_relationship_id'];
             $rows[$rowNum]['civicrm_relationship_relationship_id_link'] = $url;
             $rows[$rowNum]['civicrm_relationship_relationship_id_hover'] = ts("Edit this relationship.");
             $entryFound = TRUE;
         }
         // skip looking further in rows, if first row itself doesn't
         // have the column we need
         if (!$entryFound) {
             break;
         }
     }
 }
 /**
  * Function to process the activities
  *
  * @param object $form         form object
  * @param array  $params       associated array of the submitted values
  * @param array  $ids          array of ids
  * @param string $activityType activity Type
  * @param boolean $record   true if it is Record Activity 
  * @access public
  * @return
  */
 public function create(&$params)
 {
     // check required params
     if (!self::dataExists($params)) {
         CRM_Core_Error::fatal('Not enough data to create activity object,');
     }
     $activity = new CRM_Activity_DAO_Activity();
     if (!CRM_Utils_Array::value('status_id', $params)) {
         if (isset($params['activity_date_time']) && strcmp($params['activity_date_time'], CRM_Utils_Date::processDate(date('Ymd')) == -1)) {
             $params['status_id'] = 2;
         } else {
             $params['status_id'] = 1;
         }
     }
     //set priority to Normal for Auto-populated activities (for Cases)
     if (!CRM_Utils_Array::value('priority_id', $params)) {
         require_once 'CRM/Core/PseudoConstant.php';
         $priority = CRM_Core_PseudoConstant::priority();
         $params['priority_id'] = array_search('Normal', $priority);
     }
     if (empty($params['id'])) {
         unset($params['id']);
     }
     if (!empty($params['target_contact_id']) && is_array($params['target_contact_id'])) {
         $params['target_contact_id'] = array_unique($params['target_contact_id']);
     }
     if (!empty($params['assignee_contact_id']) && is_array($params['assignee_contact_id'])) {
         $params['assignee_contact_id'] = array_unique($params['assignee_contact_id']);
     }
     $activity->copyValues($params);
     // start transaction
     require_once 'CRM/Core/Transaction.php';
     $transaction = new CRM_Core_Transaction();
     $result = $activity->save();
     if (is_a($result, 'CRM_Core_Error')) {
         $transaction->rollback();
         return $result;
     }
     $activityId = $activity->id;
     // check and attach and files as needed
     require_once 'CRM/Core/BAO/File.php';
     CRM_Core_BAO_File::processAttachment($params, 'civicrm_activity', $activityId);
     // attempt to save activity assignment
     $resultAssignment = null;
     if (CRM_Utils_Array::value('assignee_contact_id', $params)) {
         require_once 'CRM/Activity/BAO/ActivityAssignment.php';
         $assignmentParams = array('activity_id' => $activityId);
         if (is_array($params['assignee_contact_id'])) {
             if (CRM_Utils_Array::value('deleteActivityAssignment', $params, true)) {
                 // first delete existing assignments if any
                 self::deleteActivityAssignment($activityId);
             }
             foreach ($params['assignee_contact_id'] as $acID) {
                 if ($acID) {
                     $assignmentParams['assignee_contact_id'] = $acID;
                     $resultAssignment = CRM_Activity_BAO_ActivityAssignment::create($assignmentParams);
                     if (is_a($resultAssignment, 'CRM_Core_Error')) {
                         $transaction->rollback();
                         return $resultAssignment;
                     }
                 }
             }
         } else {
             $assignmentParams['assignee_contact_id'] = $params['assignee_contact_id'];
             if (CRM_Utils_Array::value('id', $params)) {
                 $assignment = new CRM_Activity_BAO_ActivityAssignment();
                 $assignment->activity_id = $activityId;
                 $assignment->find(true);
                 if ($assignment->assignee_contact_id != $params['assignee_contact_id']) {
                     $assignmentParams['id'] = $assignment->id;
                     $resultAssignment = CRM_Activity_BAO_ActivityAssignment::create($assignmentParams);
                 }
             } else {
                 $resultAssignment = CRM_Activity_BAO_ActivityAssignment::create($assignmentParams);
             }
         }
     } else {
         if (CRM_Utils_Array::value('deleteActivityAssignment', $params, true)) {
             self::deleteActivityAssignment($activityId);
         }
     }
     if (is_a($resultAssignment, 'CRM_Core_Error')) {
         $transaction->rollback();
         return $resultAssignment;
     }
     // attempt to save activity targets
     $resultTarget = null;
     if (CRM_Utils_Array::value('target_contact_id', $params)) {
         $targetParams = array('activity_id' => $activityId);
         $resultTarget = array();
         if (is_array($params['target_contact_id'])) {
             if (CRM_Utils_Array::value('deleteActivityTarget', $params, true)) {
                 // first delete existing targets if any
                 self::deleteActivityTarget($activityId);
             }
             foreach ($params['target_contact_id'] as $tid) {
                 if ($tid) {
                     $targetParams['target_contact_id'] = $tid;
                     $resultTarget = CRM_Activity_BAO_ActivityTarget::create($targetParams);
                     if (is_a($resultTarget, 'CRM_Core_Error')) {
                         $transaction->rollback();
                         return $resultTarget;
                     }
                 }
             }
         } else {
             $targetParams['target_contact_id'] = $params['target_contact_id'];
             if (CRM_Utils_Array::value('id', $params)) {
                 $target = new CRM_Activity_BAO_ActivityTarget();
                 $target->activity_id = $activityId;
                 $target->find(true);
                 if ($target->target_contact_id != $params['target_contact_id']) {
                     $targetParams['id'] = $target->id;
                     $resultTarget = CRM_Activity_BAO_ActivityTarget::create($targetParams);
                 }
             } else {
                 $resultTarget = CRM_Activity_BAO_ActivityTarget::create($targetParams);
             }
         }
     } else {
         if (CRM_Utils_Array::value('deleteActivityTarget', $params, true)) {
             self::deleteActivityTarget($activityId);
         }
     }
     // write to changelog before transation is committed/rolled
     // back (and prepare status to display)
     if (CRM_Utils_Array::value('id', $params)) {
         $logMsg = "Activity (id: {$result->id} ) updated with ";
     } else {
         $logMsg = "Activity created for ";
     }
     $msgs = array();
     if (isset($params['source_contact_id'])) {
         $msgs[] = "source={$params['source_contact_id']}";
     }
     if (CRM_Utils_Array::value('target_contact_id', $params)) {
         if (is_array($params['target_contact_id']) && !CRM_Utils_array::crmIsEmptyArray($params['target_contact_id'])) {
             $msgs[] = "target=" . implode(',', $params['target_contact_id']);
             // take only first target
             // will be used for recently viewed display
             $t = array_slice($params['target_contact_id'], 0, 1);
             $recentContactId = $t[0];
         } else {
             if (isset($params['target_contact_id'])) {
                 $msgs[] = "target={$params['target_contact_id']}";
                 // will be used for recently viewed display
                 $recentContactId = $params['target_contact_id'];
             }
         }
     } else {
         // at worst, take source for recently viewed display
         $recentContactId = $params['source_contact_id'];
     }
     if (isset($params['assignee_contact_id'])) {
         if (is_array($params['assignee_contact_id'])) {
             $msgs[] = "assignee=" . implode(',', $params['assignee_contact_id']);
         } else {
             $msgs[] = "assignee={$params['assignee_contact_id']}";
         }
     }
     $logMsg .= implode(', ', $msgs);
     self::logActivityAction($result, $logMsg);
     if (CRM_Utils_Array::value('custom', $params) && is_array($params['custom'])) {
         require_once 'CRM/Core/BAO/CustomValueTable.php';
         CRM_Core_BAO_CustomValueTable::store($params['custom'], 'civicrm_activity', $result->id);
     }
     $transaction->commit();
     if (!CRM_Utils_Array::value('skipRecentView', $params)) {
         $recentOther = array();
         require_once 'CRM/Utils/Recent.php';
         if (CRM_Utils_Array::value('case_id', $params)) {
             $caseContactID = CRM_Core_DAO::getFieldValue('CRM_Case_DAO_CaseContact', $params['case_id'], 'contact_id', 'case_id');
             $url = CRM_Utils_System::url('civicrm/case/activity/view', "reset=1&aid={$activity->id}&cid={$caseContactID}&caseID={$params['case_id']}&context=home");
         } else {
             $q = "action=view&reset=1&id={$activity->id}&atype={$activity->activity_type_id}&cid={$activity->source_contact_id}&context=home";
             if ($activity->activity_type_id != CRM_Core_OptionGroup::getValue('activity_type', 'Email', 'name')) {
                 $url = CRM_Utils_System::url('civicrm/contact/view/activity', $q);
                 $recentOther['editUrl'] = CRM_Utils_System::url('civicrm/contact/view/activity', "action=update&reset=1&id={$activity->id}&atype={$activity->activity_type_id}&cid={$activity->source_contact_id}&context=home");
                 require_once 'CRM/Core/Permission.php';
                 if (CRM_Core_Permission::check("delete activities")) {
                     $recentOther['deleteUrl'] = CRM_Utils_System::url('civicrm/contact/view/activity', "action=delete&reset=1&id={$activity->id}&atype={$activity->activity_type_id}&cid={$activity->source_contact_id}&context=home");
                 }
             } else {
                 $url = CRM_Utils_System::url('civicrm/activity', $q);
                 if (CRM_Core_Permission::check("delete activities")) {
                     $recentOther['deleteUrl'] = CRM_Utils_System::url('civicrm/activity', "action=delete&reset=1&id={$activity->id}&atype={$activity->activity_type_id}&cid={$activity->source_contact_id}&context=home");
                 }
             }
         }
         if (!isset($activity->parent_id)) {
             require_once 'CRM/Contact/BAO/Contact.php';
             $recentContactDisplay = CRM_Contact_BAO_Contact::displayName($recentContactId);
             // add the recently created Activity
             $activityTypes = CRM_Core_Pseudoconstant::activityType(true, true);
             $activitySubject = CRM_Core_DAO::getFieldValue('CRM_Activity_DAO_Activity', $activity->id, 'subject');
             $title = "";
             if (isset($activitySubject)) {
                 $title = $activitySubject . ' - ';
             }
             $title = $title . $recentContactDisplay . ' (' . $activityTypes[$activity->activity_type_id] . ')';
             CRM_Utils_Recent::add($title, $url, $activity->id, 'Activity', $recentContactId, $recentContactDisplay, $recentOther);
         }
     }
     // reset the group contact cache since smart groups might be affected due to this
     require_once 'CRM/Contact/BAO/GroupContactCache.php';
     CRM_Contact_BAO_GroupContactCache::remove();
     if (CRM_Utils_Array::value('id', $params)) {
         CRM_Utils_Hook::post('edit', 'Activity', $activity->id, $activity);
     } else {
         CRM_Utils_Hook::post('create', 'Activity', $activity->id, $activity);
     }
     // if the subject contains a ‘[case #…]’ string, file that activity on the related case (CRM-5916)
     $matches = array();
     if (preg_match('/\\[case #([0-9a-h]{7})\\]/', $params['subject'], $matches)) {
         $key = CRM_Core_DAO::escapeString(CIVICRM_SITE_KEY);
         $hash = $matches[1];
         $query = "SELECT id FROM civicrm_case WHERE SUBSTR(SHA1(CONCAT('{$key}', id)), 1, 7) = '{$hash}'";
         $caseParams = array('activity_id' => $activity->id, 'case_id' => CRM_Core_DAO::singleValueQuery($query));
         if ($caseParams['case_id']) {
             require_once 'CRM/Case/BAO/Case.php';
             CRM_Case_BAO_Case::processCaseActivity($caseParams);
         } else {
             self::logActivityAction($activity, "unknown case hash encountered: {$hash}");
         }
     }
     return $result;
 }