Beispiel #1
0
 /**
  * Add/edit/register contacts through profile.
  *
  * @param array $params
  *   Array of profile fields to be edited/added.
  * @param array $fields
  *   Array of fields from UFGroup.
  * @param int $contactID
  *   Id of the contact to be edited/added.
  * @param int $addToGroupID
  *   Specifies the default group to which contact is added.
  * @param int $ufGroupId
  *   Uf group id (profile id).
  * @param string $ctype
  * @param bool $visibility
  *   Basically lets us know where this request is coming from.
  *                                if via a profile from web, we restrict what groups are changed
  *
  * @return int
  *   contact id created/edited
  */
 public static function createProfileContact(&$params, &$fields, $contactID = NULL, $addToGroupID = NULL, $ufGroupId = NULL, $ctype = NULL, $visibility = FALSE)
 {
     // add ufGroupID to params array ( CRM-2012 )
     if ($ufGroupId) {
         $params['uf_group_id'] = $ufGroupId;
     }
     self::addBillingNameFieldsIfOtherwiseNotSet($params);
     // If a user has logged in, or accessed via a checksum
     // Then deliberately 'blanking' a value in the profile should remove it from their record
     $session = CRM_Core_Session::singleton();
     $params['updateBlankLocInfo'] = TRUE;
     if (($session->get('authSrc') & CRM_Core_Permission::AUTH_SRC_CHECKSUM + CRM_Core_Permission::AUTH_SRC_LOGIN) == 0) {
         $params['updateBlankLocInfo'] = FALSE;
     }
     if ($contactID) {
         $editHook = TRUE;
         CRM_Utils_Hook::pre('edit', 'Profile', $contactID, $params);
     } else {
         $editHook = FALSE;
         CRM_Utils_Hook::pre('create', 'Profile', NULL, $params);
     }
     list($data, $contactDetails) = self::formatProfileContactParams($params, $fields, $contactID, $ufGroupId, $ctype);
     // manage is_opt_out
     if (array_key_exists('is_opt_out', $fields) && array_key_exists('is_opt_out', $params)) {
         $wasOptOut = CRM_Utils_Array::value('is_opt_out', $contactDetails, FALSE);
         $isOptOut = CRM_Utils_Array::value('is_opt_out', $params, FALSE);
         $data['is_opt_out'] = $isOptOut;
         // on change, create new civicrm_subscription_history entry
         if ($wasOptOut != $isOptOut && !empty($contactDetails['contact_id'])) {
             $shParams = array('contact_id' => $contactDetails['contact_id'], 'status' => $isOptOut ? 'Removed' : 'Added', 'method' => 'Web');
             CRM_Contact_BAO_SubscriptionHistory::create($shParams);
         }
     }
     $contact = self::create($data);
     // contact is null if the profile does not have any contact fields
     if ($contact) {
         $contactID = $contact->id;
     }
     if (empty($contactID)) {
         CRM_Core_Error::fatal('Cannot proceed without a valid contact id');
     }
     // Process group and tag
     if (!empty($fields['group'])) {
         $method = 'Admin';
         // this for sure means we are coming in via profile since i added it to fix
         // removing contacts from user groups -- lobo
         if ($visibility) {
             $method = 'Web';
         }
         CRM_Contact_BAO_GroupContact::create($params['group'], $contactID, $visibility, $method);
     }
     if (!empty($fields['tag'])) {
         CRM_Core_BAO_EntityTag::create($params['tag'], 'civicrm_contact', $contactID);
     }
     //to add profile in default group
     if (is_array($addToGroupID)) {
         $contactIds = array($contactID);
         foreach ($addToGroupID as $groupId) {
             CRM_Contact_BAO_GroupContact::addContactsToGroup($contactIds, $groupId);
         }
     } elseif ($addToGroupID) {
         $contactIds = array($contactID);
         CRM_Contact_BAO_GroupContact::addContactsToGroup($contactIds, $addToGroupID);
     }
     // reset the group contact cache for this group
     CRM_Contact_BAO_GroupContactCache::remove();
     if ($editHook) {
         CRM_Utils_Hook::post('edit', 'Profile', $contactID, $params);
     } else {
         CRM_Utils_Hook::post('create', 'Profile', $contactID, $params);
     }
     return $contactID;
 }
Beispiel #2
0
 /**
  * Process the form submission.
  *
  *
  * @param array $params
  *
  * @return void
  */
 public function postProcess($params = NULL)
 {
     $transaction = new CRM_Core_Transaction();
     if ($this->_action & CRM_Core_Action::DELETE) {
         $statusMsg = NULL;
         //block deleting activities which affects
         //case attributes.CRM-4543
         $activityCondition = " AND v.name IN ('Open Case', 'Change Case Type', 'Change Case Status', 'Change Case Start Date')";
         $caseAttributeActivities = CRM_Core_OptionGroup::values('activity_type', FALSE, FALSE, FALSE, $activityCondition);
         if (!array_key_exists($this->_activityTypeId, $caseAttributeActivities)) {
             $params = array('id' => $this->_activityId);
             $activityDelete = CRM_Activity_BAO_Activity::deleteActivity($params, TRUE);
             if ($activityDelete) {
                 $statusMsg = ts('The selected activity has been moved to the Trash. You can view and / or restore deleted activities by checking "Deleted Activities" from the Case Activities search filter (under Manage Case).<br />');
             }
         } else {
             $statusMsg = ts("Selected Activity cannot be deleted.");
         }
         $tagParams = array('entity_table' => 'civicrm_activity', 'entity_id' => $this->_activityId);
         CRM_Core_BAO_EntityTag::del($tagParams);
         CRM_Core_Session::setStatus('', $statusMsg, 'info');
         return;
     }
     if ($this->_action & CRM_Core_Action::RENEW) {
         $statusMsg = NULL;
         $params = array('id' => $this->_activityId);
         $activityRestore = CRM_Activity_BAO_Activity::restoreActivity($params);
         if ($activityRestore) {
             $statusMsg = ts('The selected activity has been restored.<br />');
         }
         CRM_Core_Session::setStatus('', $statusMsg, 'info');
         return;
     }
     // store the submitted values in an array
     $params = $this->controller->exportValues($this->_name);
     //set parent id if its edit mode
     if ($parentId = CRM_Utils_Array::value('parent_id', $this->_defaults)) {
         $params['parent_id'] = $parentId;
     }
     // store the dates with proper format
     $params['activity_date_time'] = CRM_Utils_Date::processDate($params['activity_date_time'], $params['activity_date_time_time']);
     $params['activity_type_id'] = $this->_activityTypeId;
     // format with contact (target contact) values
     if (isset($params['target_contact_id'])) {
         $params['target_contact_id'] = explode(',', $params['target_contact_id']);
     } else {
         $params['target_contact_id'] = array();
     }
     // format activity custom data
     if (!empty($params['hidden_custom'])) {
         if ($this->_activityId) {
             // unset custom fields-id from params since we want custom
             // fields to be saved for new activity.
             foreach ($params as $key => $value) {
                 $match = array();
                 if (preg_match('/^(custom_\\d+_)(\\d+)$/', $key, $match)) {
                     $params[$match[1] . '-1'] = $params[$key];
                     // for autocomplete transfer hidden value instead of label
                     if ($params[$key] && isset($params[$key . '_id'])) {
                         $params[$match[1] . '-1_id'] = $params[$key . '_id'];
                         unset($params[$key . '_id']);
                     }
                     unset($params[$key]);
                 }
             }
         }
         // build custom data getFields array
         $customFields = CRM_Core_BAO_CustomField::getFields('Activity', FALSE, FALSE, $this->_activityTypeId);
         $customFields = CRM_Utils_Array::crmArrayMerge($customFields, CRM_Core_BAO_CustomField::getFields('Activity', FALSE, FALSE, NULL, NULL, TRUE));
         $params['custom'] = CRM_Core_BAO_CustomField::postProcess($params, $this->_activityId, 'Activity');
     }
     // assigning formatted value
     if (!empty($params['assignee_contact_id'])) {
         $params['assignee_contact_id'] = explode(',', $params['assignee_contact_id']);
     } else {
         $params['assignee_contact_id'] = array();
     }
     if (isset($this->_activityId)) {
         // activity which hasn't been modified by a user yet
         if ($this->_defaults['is_auto'] == 1) {
             $params['is_auto'] = 0;
         }
         // always create a revision of an case activity. CRM-4533
         $newActParams = $params;
         // add target contact values in update mode
         if (empty($params['target_contact_id']) && !empty($this->_defaults['target_contact'])) {
             $newActParams['target_contact_id'] = $this->_defaults['target_contact'];
         }
     }
     if (!isset($newActParams)) {
         // add more attachments if needed for old activity
         CRM_Core_BAO_File::formatAttachment($params, $params, 'civicrm_activity');
         // call begin post process, before the activity is created/updated.
         $this->beginPostProcess($params);
         foreach ($this->_caseId as $key => $val) {
             $params['case_id'] = $val;
             // activity create/update
             $activity = CRM_Activity_BAO_Activity::create($params);
             $vvalue[] = array('case_id' => $val, 'actId' => $activity->id);
             // call end post process, after the activity has been created/updated.
             $this->endPostProcess($params, $activity);
         }
     } else {
         // since the params we need to set are very few, and we don't want rest of the
         // work done by bao create method , lets use dao object to make the changes
         $params = array('id' => $this->_activityId);
         $params['is_current_revision'] = 0;
         $activity = new CRM_Activity_DAO_Activity();
         $activity->copyValues($params);
         $activity->save();
     }
     // create a new version of activity if activity was found to
     // have been modified/created by user
     if (isset($newActParams)) {
         // set proper original_id
         if (!empty($this->_defaults['original_id'])) {
             $newActParams['original_id'] = $this->_defaults['original_id'];
         } else {
             $newActParams['original_id'] = $activity->id;
         }
         //is_current_revision will be set to 1 by default.
         // add attachments if any
         CRM_Core_BAO_File::formatAttachment($newActParams, $newActParams, 'civicrm_activity');
         // call begin post process, before the activity is created/updated.
         $this->beginPostProcess($newActParams);
         foreach ($this->_caseId as $key => $val) {
             $newActParams['case_id'] = $val;
             $activity = CRM_Activity_BAO_Activity::create($newActParams);
             $vvalue[] = array('case_id' => $val, 'actId' => $activity->id);
             // call end post process, after the activity has been created/updated.
             $this->endPostProcess($newActParams, $activity);
         }
         // copy files attached to old activity if any, to new one,
         // as long as users have not selected the 'delete attachment' option.
         if (empty($newActParams['is_delete_attachment'])) {
             CRM_Core_BAO_File::copyEntityFile('civicrm_activity', $this->_activityId, 'civicrm_activity', $activity->id);
         }
         // copy back params to original var
         $params = $newActParams;
     }
     foreach ($vvalue as $vkey => $vval) {
         if ($vval['actId']) {
             // add tags if exists
             $tagParams = array();
             if (!empty($params['tag'])) {
                 foreach ($params['tag'] as $tag) {
                     $tagParams[$tag] = 1;
                 }
             }
             //save static tags
             CRM_Core_BAO_EntityTag::create($tagParams, 'civicrm_activity', $vval['actId']);
             //save free tags
             if (isset($params['taglist']) && !empty($params['taglist'])) {
                 CRM_Core_Form_Tag::postProcess($params['taglist'], $vval['actId'], 'civicrm_activity', $this);
             }
         }
         // update existing case record if needed
         $caseParams = $params;
         $caseParams['id'] = $vval['case_id'];
         if (!empty($caseParams['case_status_id'])) {
             $caseParams['status_id'] = $caseParams['case_status_id'];
         }
         // unset params intended for activities only
         unset($caseParams['subject'], $caseParams['details'], $caseParams['status_id'], $caseParams['custom']);
         $case = CRM_Case_BAO_Case::create($caseParams);
         // create case activity record
         $caseParams = array('activity_id' => $vval['actId'], 'case_id' => $vval['case_id']);
         CRM_Case_BAO_Case::processCaseActivity($caseParams);
     }
     // Insert civicrm_log record for the activity (e.g. store the
     // created / edited by contact id and date for the activity)
     // Note - civicrm_log is already created by CRM_Activity_BAO_Activity::create()
     // send copy to selected contacts.
     $mailStatus = '';
     $mailToContacts = array();
     //CRM-5695
     //check for notification settings for assignee contacts
     $selectedContacts = array('contact_check');
     $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
     $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts);
     if (CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'activity_assignee_notification')) {
         $selectedContacts[] = 'assignee_contact_id';
     }
     foreach ($vvalue as $vkey => $vval) {
         foreach ($selectedContacts as $dnt => $val) {
             if (array_key_exists($val, $params) && !CRM_Utils_array::crmIsEmptyArray($params[$val])) {
                 if ($val == 'contact_check') {
                     $mailStatus = ts("A copy of the activity has also been sent to selected contacts(s).");
                 } else {
                     $this->_relatedContacts = CRM_Activity_BAO_ActivityAssignment::getAssigneeNames(array($vval['actId']), TRUE, FALSE);
                     $mailStatus .= ' ' . ts("A copy of the activity has also been sent to assignee contacts(s).");
                 }
                 //build an associative array with unique email addresses.
                 foreach ($params[$val] as $key => $value) {
                     if ($val == 'contact_check') {
                         $id = $key;
                     } else {
                         $id = $value;
                     }
                     if (isset($id) && array_key_exists($id, $this->_relatedContacts) && isset($this->_relatedContacts[$id]['email'])) {
                         //if email already exists in array then append with ', ' another role only otherwise add it to array.
                         if ($contactDetails = CRM_Utils_Array::value($this->_relatedContacts[$id]['email'], $mailToContacts)) {
                             $caseRole = CRM_Utils_Array::value('role', $this->_relatedContacts[$id]);
                             $mailToContacts[$this->_relatedContacts[$id]['email']]['role'] = $contactDetails['role'] . ', ' . $caseRole;
                         } else {
                             $mailToContacts[$this->_relatedContacts[$id]['email']] = $this->_relatedContacts[$id];
                         }
                     }
                 }
             }
         }
         $extraParams = array('case_id' => $vval['case_id'], 'client_id' => $this->_currentlyViewedContactId);
         $result = CRM_Activity_BAO_Activity::sendToAssignee($activity, $mailToContacts, $extraParams);
         if (empty($result)) {
             $mailStatus = '';
         }
         // create follow up activity if needed
         $followupStatus = '';
         if (!empty($params['followup_activity_type_id'])) {
             $followupActivity = CRM_Activity_BAO_Activity::createFollowupActivity($vval['actId'], $params);
             if ($followupActivity) {
                 $caseParams = array('activity_id' => $followupActivity->id, 'case_id' => $vval['case_id']);
                 CRM_Case_BAO_Case::processCaseActivity($caseParams);
                 $followupStatus = ts("A followup activity has been scheduled.") . '<br /><br />';
             }
         }
         $title = ts("%1 Saved", array(1 => $this->_activityTypeName));
         CRM_Core_Session::setStatus($followupStatus . $mailStatus, $title, 'success');
     }
 }
Beispiel #3
0
 /**
  * function to add/edit/register contacts through profile.
  *
  * @params  array  $params        Array of profile fields to be edited/added.
  * @params  int    $contactID     contact_id of the contact to be edited/added.
  * @params  array  $fields        array of fields from UFGroup
  * @params  int    $addToGroupID  specifies the default group to which contact is added.
  * @params  int    $ufGroupId     uf group id (profile id)
  * @param   string $ctype         contact type
  *
  * @return  int                   contact id created/edited
  * @static
  * @access public
  */
 static function createProfileContact(&$params, &$fields, $contactID = null, $addToGroupID = null, $ufGroupId = null, $ctype = null, $visibility = false)
 {
     // add ufGroupID to params array ( CRM-2012 )
     if ($ufGroupId) {
         $params['uf_group_id'] = $ufGroupId;
     }
     require_once 'CRM/Utils/Hook.php';
     if ($contactID) {
         $editHook = true;
         CRM_Utils_Hook::pre('edit', 'Profile', $contactID, $params);
     } else {
         $editHook = false;
         CRM_Utils_Hook::pre('create', 'Profile', null, $params);
     }
     $data = $contactDetails = array();
     // get the contact details (hier)
     if ($contactID) {
         list($details, $options) = self::getHierContactDetails($contactID, $fields);
         $contactDetails = $details[$contactID];
         $data['contact_type'] = CRM_Utils_Array::value('contact_type', $contactDetails);
     } else {
         //we should get contact type only if contact
         if ($ufGroupId) {
             require_once "CRM/Core/BAO/UFField.php";
             $data['contact_type'] = CRM_Core_BAO_UFField::getProfileType($ufGroupId);
             //special case to handle profile with only contact fields
             if ($data['contact_type'] == 'Contact') {
                 $data['contact_type'] = 'Individual';
             } else {
                 if (CRM_Contact_BAO_ContactType::isaSubType($data['contact_type'])) {
                     $data['contact_type'] = CRM_Contact_BAO_ContactType::getBasicType($data['contact_type']);
                 }
             }
         } else {
             if ($ctype) {
                 $data['contact_type'] = $ctype;
             } else {
                 $data['contact_type'] = 'Individual';
             }
         }
     }
     //fix contact sub type CRM-5125
     if ($subType = CRM_Utils_Array::value('contact_sub_type', $params)) {
         $data['contact_sub_type'] = $subType;
     } else {
         if ($ufGroupId) {
             $data['contact_sub_type'] = CRM_Core_BAO_UFField::getProfileSubType($ufGroupId, $data['contact_type']);
         }
     }
     if ($ctype == "Organization") {
         $data["organization_name"] = $contactDetails["organization_name"];
     } else {
         if ($ctype == "Household") {
             $data["household_name"] = $contactDetails["household_name"];
         }
     }
     $locationType = array();
     $count = 1;
     if ($contactID) {
         //add contact id
         $data['contact_id'] = $contactID;
         $primaryLocationType = self::getPrimaryLocationType($contactID);
     } else {
         require_once "CRM/Core/BAO/LocationType.php";
         $defaultLocation =& CRM_Core_BAO_LocationType::getDefault();
         $defaultLocationId = $defaultLocation->id;
     }
     // get the billing location type
     $locationTypes =& CRM_Core_PseudoConstant::locationType();
     $billingLocationTypeId = array_search('Billing', $locationTypes);
     $blocks = array('email', 'phone', 'im', 'openid');
     // prevent overwritten of formatted array, reset all block from
     // params if it is not in valid format (since import pass valid format)
     foreach ($blocks as $blk) {
         if (array_key_exists($blk, $params) && !is_array($params[$blk])) {
             unset($params[$blk]);
         }
     }
     $primaryPhoneLoc = null;
     foreach ($params as $key => $value) {
         $fieldName = $locTypeId = $typeId = null;
         list($fieldName, $locTypeId, $typeId) = CRM_Utils_System::explode('-', $key, 3);
         //store original location type id
         $actualLocTypeId = $locTypeId;
         if ($locTypeId == 'Primary') {
             if ($contactID) {
                 $locTypeId = $primaryLocationType;
             } else {
                 $locTypeId = $defaultLocationId;
             }
         }
         if (is_numeric($locTypeId)) {
             $index = $locTypeId;
             if (is_numeric($typeId)) {
                 $index .= '-' . $typeId;
             }
             if (!in_array($index, $locationType)) {
                 $locationType[$count] = $index;
                 $count++;
             }
             require_once 'CRM/Utils/Array.php';
             $loc = CRM_Utils_Array::key($index, $locationType);
             $blockName = 'address';
             if (in_array($fieldName, $blocks)) {
                 $blockName = $fieldName;
             }
             $data[$blockName][$loc]['location_type_id'] = $locTypeId;
             //set is_billing true, for location type "Billing"
             if ($locTypeId == $billingLocationTypeId) {
                 $data[$blockName][$loc]['is_billing'] = 1;
             }
             if ($contactID) {
                 //get the primary location type
                 if ($locTypeId == $primaryLocationType) {
                     $data[$blockName][$loc]['is_primary'] = 1;
                 }
             } else {
                 if (($locTypeId == $defaultLocationId || $locTypeId == $billingLocationTypeId) && ($loc == 1 || !CRM_Utils_Array::retrieveValueRecursive($data['location'][$loc - 1], 'is_primary'))) {
                     $data[$blockName][$loc]['is_primary'] = 1;
                 }
             }
             if ($fieldName == 'phone') {
                 if ($typeId) {
                     $data['phone'][$loc]['phone_type_id'] = $typeId;
                 } else {
                     $data['phone'][$loc]['phone_type_id'] = '';
                 }
                 $data['phone'][$loc]['phone'] = $value;
                 //special case to handle primary phone with different phone types
                 // in this case we make first phone type as primary
                 if (isset($data['phone'][$loc]['is_primary']) && !$primaryPhoneLoc) {
                     $primaryPhoneLoc = $loc;
                 }
                 if ($loc != $primaryPhoneLoc) {
                     unset($data['phone'][$loc]['is_primary']);
                 }
             } else {
                 if ($fieldName == 'email') {
                     $data['email'][$loc]['email'] = $value;
                 } else {
                     if ($fieldName == 'im') {
                         if (isset($params[$key . '-provider_id'])) {
                             $data['im'][$loc]['provider_id'] = $params[$key . '-provider_id'];
                         }
                         $data['im'][$loc]['name'] = $value;
                     } else {
                         if ($fieldName == 'openid') {
                             # $value should be a hash of the OpenID fields
                             foreach ($value as $key => $val) {
                                 $data['openid'][$loc][$key] = $val;
                             }
                         } else {
                             if ($fieldName === 'state_province') {
                                 // CRM-3393
                                 if (is_numeric($value) && (int) $value >= 1000) {
                                     $data['address'][$loc]['state_province_id'] = $value;
                                 } else {
                                     $data['address'][$loc]['state_province'] = $value;
                                 }
                             } else {
                                 if ($fieldName === 'country') {
                                     // CRM-3393
                                     if (is_numeric($value) && (int) $value >= 1000) {
                                         $data['address'][$loc]['country_id'] = $value;
                                     } else {
                                         $data['address'][$loc]['country'] = $value;
                                     }
                                 } else {
                                     if ($fieldName === 'county') {
                                         $data['address'][$loc]['address']['county_id'] = $value;
                                     } else {
                                         if ($fieldName == 'address_name') {
                                             $data['address'][$loc]['name'] = $value;
                                         } else {
                                             $data['address'][$loc][$fieldName] = $value;
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         } else {
             if ($key === 'individual_suffix') {
                 $data['suffix_id'] = $value;
             } else {
                 if ($key === 'individual_prefix') {
                     $data['prefix_id'] = $value;
                 } else {
                     if ($key === 'gender') {
                         $data['gender_id'] = $value;
                     } else {
                         if ($key === 'email_greeting') {
                             //save email/postal greeting and addressee values if any, CRM-4575
                             $data['email_greeting_id'] = $value;
                         } else {
                             if ($key === 'postal_greeting') {
                                 $data['postal_greeting_id'] = $value;
                             } else {
                                 if ($key === 'addressee') {
                                     $data['addressee_id'] = $value;
                                 } else {
                                     if ($customFieldId = CRM_Core_BAO_CustomField::getKeyID($key)) {
                                         // for autocomplete transfer hidden value instead of label
                                         if (isset($params[$key . '_id'])) {
                                             $value = $params[$key . '_id'];
                                         }
                                         $type = CRM_Utils_Array::value('contact_sub_type', $data) ? $data['contact_sub_type'] : $data['contact_type'];
                                         CRM_Core_BAO_CustomField::formatCustomField($customFieldId, $data['custom'], $value, $type, null, $contactID);
                                     } else {
                                         if ($key == 'edit') {
                                             continue;
                                         } else {
                                             if ($key == 'location') {
                                                 foreach ($value as $locationTypeId => $field) {
                                                     foreach ($field as $block => $val) {
                                                         if ($block == 'address' && array_key_exists('address_name', $val)) {
                                                             $value[$locationTypeId][$block]['name'] = $value[$locationTypeId][$block]['address_name'];
                                                         }
                                                     }
                                                 }
                                             }
                                             $data[$key] = $value;
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     // FIX ME: need to check if we need this code
     // //make sure primary location is at first position in location array
     // if ( isset( $data['location'] ) && count( $data['location'] ) > 1 ) {
     //     // if first location is primary skip manipulation
     //     if ( !isset($data['location'][1]['is_primary']) ) {
     //         //find the key for primary location
     //         foreach ( $data['location'] as $primaryLocationKey => $value ) {
     //             if ( isset( $value['is_primary'] ) ) {
     //                 break;
     //             }
     //         }
     //
     //         // swap first location with primary location
     //         $tempLocation        = $data['location'][1];
     //         $data['location'][1] = $data['location'][$primaryLocationKey];
     //         $data['location'][$primaryLocationKey] = $tempLocation;
     //     }
     // }
     if (!isset($data['contact_type'])) {
         $data['contact_type'] = 'Individual';
     }
     if (CRM_Core_Permission::access('Quest')) {
         $studentFieldPresent = 0;
         foreach ($fields as $name => $field) {
             // check if student fields present
             require_once 'CRM/Quest/BAO/Student.php';
             if (!$studentFieldPresent && array_key_exists($name, CRM_Quest_BAO_Student::exportableFields())) {
                 $studentFieldPresent = 1;
             }
         }
     }
     //set the values for checkboxes (do_not_email, do_not_mail, do_not_trade, do_not_phone)
     $privacy = CRM_Core_SelectValues::privacy();
     foreach ($privacy as $key => $value) {
         if (array_key_exists($key, $fields)) {
             if ($params[$key]) {
                 $data[$key] = $params[$key];
             } else {
                 $data[$key] = 0;
             }
         }
     }
     // manage is_opt_out
     if (array_key_exists('is_opt_out', $fields)) {
         $wasOptOut = CRM_Utils_Array::value('is_opt_out', $contactDetails, false);
         $isOptOut = CRM_Utils_Array::value('is_opt_out', $params, false);
         $data['is_opt_out'] = $isOptOut;
         // on change, create new civicrm_subscription_history entry
         if ($wasOptOut != $isOptOut && CRM_Utils_Array::value('contact_id', $contactDetails)) {
             $shParams = array('contact_id' => $contactDetails['contact_id'], 'status' => $isOptOut ? 'Removed' : 'Added', 'method' => 'Web');
             CRM_Contact_BAO_SubscriptionHistory::create($shParams);
         }
     }
     require_once 'CRM/Contact/BAO/Contact.php';
     if ($data['contact_type'] != 'Student') {
         $contact =& self::create($data);
     }
     // contact is null if the profile does not have any contact fields
     if ($contact) {
         $contactID = $contact->id;
     }
     if (!$contactID) {
         CRM_Core_Error::fatal('Cannot proceed without a valid contact id');
     }
     // Process group and tag
     if (CRM_Utils_Array::value('group', $fields)) {
         $method = 'Admin';
         // this for sure means we are coming in via profile since i added it to fix
         // removing contacts from user groups -- lobo
         if ($visibility) {
             $method = 'Web';
         }
         CRM_Contact_BAO_GroupContact::create($params['group'], $contactID, $visibility, $method);
     }
     if (CRM_Utils_Array::value('tag', $fields)) {
         require_once 'CRM/Core/BAO/EntityTag.php';
         CRM_Core_BAO_EntityTag::create($params['tag'], $contactID);
     }
     //to add profile in default group
     if (is_array($addToGroupID)) {
         $contactIds = array($contactID);
         foreach ($addToGroupID as $groupId) {
             CRM_Contact_BAO_GroupContact::addContactsToGroup($contactIds, $groupId);
         }
     } else {
         if ($addToGroupID) {
             $contactIds = array($contactID);
             CRM_Contact_BAO_GroupContact::addContactsToGroup($contactIds, $addToGroupID);
         }
     }
     //to update student record
     if (CRM_Core_Permission::access('Quest') && $studentFieldPresent) {
         $ids = array();
         $dao =& new CRM_Quest_DAO_Student();
         $dao->contact_id = $contactID;
         if ($dao->find(true)) {
             $ids['id'] = $dao->id;
         }
         $ssids = array();
         $studentSummary =& new CRM_Quest_DAO_StudentSummary();
         $studentSummary->contact_id = $contactID;
         if ($studentSummary->find(true)) {
             $ssids['id'] = $studentSummary->id;
         }
         $params['contact_id'] = $contactID;
         //fixed for check boxes
         $specialFields = array('educational_interest', 'college_type', 'college_interest', 'test_tutoring');
         foreach ($specialFields as $field) {
             if ($params[$field]) {
                 $params[$field] = implode(CRM_Core_BAO_CustomOption::VALUE_SEPERATOR, array_keys($params[$field]));
             }
         }
         CRM_Quest_BAO_Student::create($params, $ids);
         CRM_Quest_BAO_Student::createStudentSummary($params, $ssids);
     }
     // reset the group contact cache for this group
     require_once 'CRM/Contact/BAO/GroupContactCache.php';
     CRM_Contact_BAO_GroupContactCache::remove();
     if ($editHook) {
         CRM_Utils_Hook::post('edit', 'Profile', $contactID, $params);
     } else {
         CRM_Utils_Hook::post('create', 'Profile', $contactID, $params);
     }
     return $contactID;
 }
Beispiel #4
0
 /**
  *
  * @return void
  */
 public function postProcess()
 {
     CRM_Utils_System::flushCache('CRM_Core_DAO_Tag');
     // array contains the posted values
     // exportvalues is not used because its give value 1 of the checkbox which were checked by default,
     // even after unchecking them before submitting them
     $entityTag = $_POST['tagList'];
     CRM_Core_BAO_EntityTag::create($entityTag, $this->_entityTable, $this->_entityID);
     CRM_Core_Session::setStatus(ts('Your update(s) have been saved.'), ts('Saved'), 'success');
 }
Beispiel #5
0
 /**
  * Form submission of new/edit contact is processed.
  *
  * @access public
  * @return None
  */
 public function postProcess()
 {
     // check if dedupe button, if so return.
     $buttonName = $this->controller->getButtonName();
     if ($buttonName == $this->_dedupeButtonName) {
         return;
     }
     //get the submitted values in an array
     $params = $this->controller->exportValues($this->_name);
     //get the related id for shared / current employer
     if (CRM_Utils_Array::value('shared_household_id', $params)) {
         $params['shared_household'] = $params['shared_household_id'];
     }
     if (is_numeric(CRM_Utils_Array::value('current_employer_id', $params)) && CRM_Utils_Array::value('current_employer', $params)) {
         $params['current_employer'] = $params['current_employer_id'];
     }
     // don't carry current_employer_id field,
     // since we don't want to directly update DAO object without
     // handling related business logic ( eg related membership )
     if (isset($params['current_employer_id'])) {
         unset($params['current_employer_id']);
     }
     $params['contact_type'] = $this->_contactType;
     if ($this->_contactId) {
         $params['contact_id'] = $this->_contactId;
     }
     //make deceased date null when is_deceased = false
     if ($this->_contactType == 'Individual' && CRM_Utils_Array::value('Demographics', $this->_editOptions) && !CRM_Utils_Array::value('is_deceased', $params)) {
         $params['is_deceased'] = false;
         $params['deceased_date'] = null;
     }
     if ($this->_contactSubType && $this->_action & CRM_Core_Action::ADD) {
         $params['contact_sub_type'] = $this->_contactSubType;
     }
     // action is taken depending upon the mode
     require_once 'CRM/Utils/Hook.php';
     if ($this->_action & CRM_Core_Action::UPDATE) {
         CRM_Utils_Hook::pre('edit', $params['contact_type'], $params['contact_id'], $params);
     } else {
         CRM_Utils_Hook::pre('create', $params['contact_type'], null, $params);
     }
     require_once 'CRM/Core/BAO/CustomField.php';
     $customFields = CRM_Core_BAO_CustomField::getFields($params['contact_type'], false, true);
     $params['custom'] = CRM_Core_BAO_CustomField::postProcess($params, $customFields, $this->_contactId, $params['contact_type'], true);
     if (array_key_exists('CommunicationPreferences', $this->_editOptions)) {
         // this is a chekbox, so mark false if we dont get a POST value
         $params['is_opt_out'] = CRM_Utils_Array::value('is_opt_out', $params, false);
     }
     // copy household address, if use_household_address option (for individual form) is checked
     if ($this->_contactType == 'Individual') {
         if (CRM_Utils_Array::value('use_household_address', $params) && CRM_Utils_Array::value('shared_household', $params)) {
             if (is_numeric($params['shared_household'])) {
                 CRM_Contact_Form_Edit_Individual::copyHouseholdAddress($params);
             }
             CRM_Contact_Form_Edit_Individual::createSharedHousehold($params);
         } else {
             $params['mail_to_household_id'] = 'null';
         }
     } else {
         $params['mail_to_household_id'] = 'null';
     }
     if (!array_key_exists('TagsAndGroups', $this->_editOptions)) {
         unset($params['group']);
     }
     if (CRM_Utils_Array::value('contact_id', $params) && $this->_action & CRM_Core_Action::UPDATE) {
         // cleanup unwanted location blocks
         require_once 'CRM/Core/BAO/Location.php';
         CRM_Core_BAO_Location::cleanupContactLocations($params);
         // figure out which all groups are intended to be removed
         if (!empty($params['group'])) {
             $contactGroupList =& CRM_Contact_BAO_GroupContact::getContactGroup($params['contact_id'], 'Added');
             if (is_array($contactGroupList)) {
                 foreach ($contactGroupList as $key) {
                     if ($params['group'][$key['group_id']] != 1) {
                         $params['group'][$key['group_id']] = -1;
                     }
                 }
             }
         }
     }
     require_once 'CRM/Contact/BAO/Contact.php';
     $contact =& CRM_Contact_BAO_Contact::create($params, true, false);
     if ($this->_contactType == 'Individual' && CRM_Utils_Array::value('use_household_address', $params) && CRM_Utils_Array::value('mail_to_household_id', $params)) {
         // add/edit/delete the relation of individual with household, if use-household-address option is checked/unchecked.
         CRM_Contact_Form_Edit_Individual::handleSharedRelation($contact->id, $params);
     }
     if ($this->_contactType == 'Household' && $this->_action & CRM_Core_Action::UPDATE) {
         //TO DO: commented because of schema changes
         require_once 'CRM/Contact/Form/Edit/Household.php';
         CRM_Contact_Form_Edit_Household::synchronizeIndividualAddresses($contact->id);
     }
     if (array_key_exists('TagsAndGroups', $this->_editOptions)) {
         //add contact to tags
         require_once 'CRM/Core/BAO/EntityTag.php';
         CRM_Core_BAO_EntityTag::create($params['tag'], $params['contact_id']);
     }
     // here we replace the user context with the url to view this contact
     $session =& CRM_Core_Session::singleton();
     CRM_Core_Session::setStatus(ts('Your %1 contact record has been saved.', array(1 => $contact->contact_type_display)));
     $buttonName = $this->controller->getButtonName();
     if ($buttonName == $this->getButtonName('next', 'new') || $buttonName == $this->getButtonName('upload', 'new')) {
         require_once 'CRM/Utils/Recent.php';
         // add the recently viewed contact
         $displayName = CRM_Contact_BAO_Contact::displayName($contact->id);
         CRM_Utils_Recent::add($displayName, CRM_Utils_System::url('civicrm/contact/view', 'reset=1&cid=' . $contact->id), $contact->id, $this->_contactType, $contact->id, $displayName);
         $resetStr = "reset=1&ct={$contact->contact_type}";
         $resetStr .= $this->_contactSubType ? "&cst={$this->_contactSubType}" : '';
         $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/add', $resetStr));
     } else {
         $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view', 'reset=1&cid=' . $contact->id));
     }
     // now invoke the post hook
     if ($this->_action & CRM_Core_Action::UPDATE) {
         CRM_Utils_Hook::post('edit', $params['contact_type'], $contact->id, $contact);
     } else {
         CRM_Utils_Hook::post('create', $params['contact_type'], $contact->id, $contact);
     }
 }
Beispiel #6
0
 /** 
  * takes an associative array and creates a contact object and all the associated 
  * derived objects (i.e. individual, location, email, phone etc) 
  * 
  * This function is invoked from within the web form layer and also from the api layer
  * primarily from the profile / contribute forms where we dont have a nice hierarchy
  * and are too lazy to create one. This function should be obsoleted at some time
  * 
  * @param array $params (reference ) an assoc array of name/value pairs 
  * @param array $ids    the array that holds all the db ids 
  * 
  * @return object CRM_Contact_BAO_Contact object  
  * @access public 
  * @static 
  */
 function &createFlat(&$params, &$ids)
 {
     require_once 'CRM/Utils/Hook.php';
     if (CRM_Utils_Array::value('contact', $ids)) {
         CRM_Utils_Hook::pre('edit', 'Individual', $ids['contact'], $params);
     } else {
         CRM_Utils_Hook::pre('create', 'Individual', null, $params);
     }
     CRM_Core_DAO::transaction('BEGIN');
     $params['contact_type'] = 'Individual';
     $contact = CRM_Contact_BAO_Contact::add($params, $ids);
     $params['contact_id'] = $contact->id;
     require_once 'CRM/Contact/BAO/Individual.php';
     CRM_Contact_BAO_Individual::add($params, $ids);
     require_once 'CRM/Core/BAO/LocationType.php';
     $locationType =& CRM_Core_BAO_LocationType::getDefault();
     $locationTypeId = $locationType->id;
     $locationIds = CRM_Utils_Array::value('location', $ids);
     // extract the first location id
     if ($locationIds) {
         foreach ($locationIds as $dontCare => $locationId) {
             $locationIds = $locationId;
             break;
         }
     }
     $location =& new CRM_Core_DAO_Location();
     $location->location_type_id = $locationTypeId;
     $location->entity_table = 'civicrm_contact';
     $location->entity_id = $contact->id;
     $location->id = CRM_Utils_Array::value('id', $locationIds);
     if ($location->find(true)) {
         if (!$location->is_primary) {
             $location->is_primary = true;
         }
     } else {
         $location->is_primary = true;
     }
     $location->save();
     $address =& new CRM_Core_BAO_Address();
     CRM_Core_BAO_Address::fixAddress($params);
     if (!$address->copyValues($params)) {
         $address->id = CRM_Utils_Array::value('address', $locationIds);
         $address->location_id = $location->id;
         $address->save();
     }
     $phone =& new CRM_Core_BAO_Phone();
     if (!$phone->copyValues($params)) {
         $blockIds = CRM_Utils_Array::value('phone', $locationIds);
         $phone->id = CRM_Utils_Array::value(1, $blockIds);
         $phone->location_id = $location->id;
         $phone->is_primary = true;
         $phone->save();
     }
     $email =& new CRM_Core_BAO_Email();
     if (!$email->copyValues($params)) {
         $blockIds = CRM_Utils_Array::value('email', $locationIds);
         $email->id = CRM_Utils_Array::value(1, $blockIds);
         $email->location_id = $location->id;
         $email->is_primary = true;
         $email->save();
     }
     /* Process custom field values and other values */
     foreach ($params as $key => $value) {
         if ($key == 'group') {
             CRM_Contact_BAO_GroupContact::create($params['group'], $contact->id);
         } else {
             if ($key == 'tag') {
                 require_once 'CRM/Core/BAO/EntityTag.php';
                 CRM_Core_BAO_EntityTag::create($params['tag'], $contact->id);
             } else {
                 if ($cfID = CRM_Core_BAO_CustomField::getKeyID($key)) {
                     $custom_field_id = $cfID;
                     $cf =& new CRM_Core_BAO_CustomField();
                     $cf->id = $custom_field_id;
                     if ($cf->find(true)) {
                         switch ($cf->html_type) {
                             case 'Select Date':
                                 $date = CRM_Utils_Date::format($value);
                                 if (!$date) {
                                     $date = '';
                                 }
                                 $customValue = $date;
                                 break;
                             case 'CheckBox':
                                 $customValue = implode(CRM_CORE_BAO_CUSTOMOPTION_VALUE_SEPERATOR, array_keys($value));
                                 break;
                                 //added a case for Multi-Select
                             //added a case for Multi-Select
                             case 'Multi-Select':
                                 $customValue = implode(CRM_CORE_BAO_CUSTOMOPTION_VALUE_SEPERATOR, array_keys($value));
                                 break;
                             default:
                                 $customValue = $value;
                         }
                     }
                     CRM_Core_BAO_CustomValue::updateValue($contact->id, $custom_field_id, $customValue);
                 }
             }
         }
     }
     CRM_Core_DAO::transaction('COMMIT');
     if (CRM_Utils_Array::value('contact', $ids)) {
         CRM_Utils_Hook::post('edit', 'Individual', $contact->id, $contact);
     } else {
         CRM_Utils_Hook::post('create', 'Individual', $contact->id, $contact);
     }
     return $contact;
 }
/**
 * Submit a set of fields against a profile.
 * Note choice of submit versus create is discussed CRM-13234 & related to the fact
 * 'profile' is being treated as a data-entry entity
 *
 * @param array $params
 *
 * @throws API_Exception
 * @return array API result array
 */
function civicrm_api3_profile_submit($params)
{
    $profileID = _civicrm_api3_profile_getProfileID($params['profile_id']);
    if (!CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $profileID, 'is_active')) {
        //@todo declare pseudoconstant & let api do this
        throw new API_Exception('Invalid value for profile_id');
    }
    $isContactActivityProfile = CRM_Core_BAO_UFField::checkContactActivityProfileType($profileID);
    if (!empty($params['id']) && CRM_Core_BAO_UFField::checkProfileType($profileID) && !$isContactActivityProfile) {
        throw new API_Exception('Update profiles including more than one entity not currently supported');
    }
    $contactParams = $activityParams = $missingParams = array();
    $profileFields = civicrm_api3('profile', 'getfields', array('action' => 'submit', 'profile_id' => $profileID));
    $profileFields = $profileFields['values'];
    if ($isContactActivityProfile) {
        civicrm_api3_verify_mandatory($params, NULL, array('activity_id'));
        $errors = CRM_Profile_Form::validateContactActivityProfile($params['activity_id'], $params['contact_id'], $profileID);
        if (!empty($errors)) {
            throw new API_Exception(array_pop($errors));
        }
    }
    foreach ($profileFields as $fieldName => $field) {
        if (!isset($params[$fieldName])) {
            continue;
        }
        $value = $params[$fieldName];
        if ($params[$fieldName] && isset($params[$fieldName . '_id'])) {
            $value = $params[$fieldName . '_id'];
        }
        $contactEntities = array('contact', 'individual', 'organization', 'household');
        $locationEntities = array('email', 'address', 'phone', 'website', 'im');
        $entity = strtolower(CRM_Utils_Array::value('entity', $field));
        if ($entity && !in_array($entity, array_merge($contactEntities, $locationEntities))) {
            $contactParams['api.' . $entity . '.create'][$fieldName] = $value;
            //@todo we are not currently declaring this option
            if (isset($params['batch_id']) && strtolower($entity) == 'contribution') {
                $contactParams['api.' . $entity . '.create']['batch_id'] = $params['batch_id'];
            }
            if (isset($params[$entity . '_id'])) {
                //todo possibly declare $entity_id in getfields ?
                $contactParams['api.' . $entity . '.create']['id'] = $params[$entity . '_id'];
            }
        } else {
            $contactParams[_civicrm_api3_profile_translate_fieldnames_for_bao($fieldName)] = $value;
        }
    }
    if (isset($contactParams['api.contribution.create']) && isset($contactParams['api.membership.create'])) {
        $contactParams['api.membership_payment.create'] = array('contribution_id' => '$value.api.contribution.create.id', 'membership_id' => '$value.api.membership.create.id');
    }
    if (isset($contactParams['api.contribution.create']) && isset($contactParams['api.participant.create'])) {
        $contactParams['api.participant_payment.create'] = array('contribution_id' => '$value.api.contribution.create.id', 'participant_id' => '$value.api.participant.create.id');
    }
    $contactParams['contact_id'] = CRM_Utils_Array::value('contact_id', $params);
    $contactParams['profile_id'] = $profileID;
    $contactParams['skip_custom'] = 1;
    $contactProfileParams = civicrm_api3_profile_apply($contactParams);
    // Contact profile fields
    $profileParams = $contactProfileParams['values'];
    // If profile having activity fields
    if ($isContactActivityProfile && !empty($activityParams)) {
        $activityParams['id'] = $params['activity_id'];
        $profileParams['api.activity.create'] = $activityParams;
    }
    $groups = $tags = array();
    if (isset($profileParams['group'])) {
        $groups = $profileParams['group'];
        unset($profileParams['group']);
    }
    if (isset($profileParams['tag'])) {
        $tags = $profileParams['tag'];
        unset($profileParams['tag']);
    }
    return civicrm_api3('contact', 'create', $profileParams);
    $ufGroupDetails = array();
    $ufGroupParams = array('id' => $profileID);
    CRM_Core_BAO_UFGroup::retrieve($ufGroupParams, $ufGroupDetails);
    if (isset($profileFields['group'])) {
        CRM_Contact_BAO_GroupContact::create($groups, $params['contact_id'], FALSE, 'Admin');
    }
    if (isset($profileFields['tag'])) {
        CRM_Core_BAO_EntityTag::create($tags, 'civicrm_contact', $params['contact_id']);
    }
    if (!empty($ufGroupDetails['add_to_group_id'])) {
        $contactIds = array($params['contact_id']);
        CRM_Contact_BAO_GroupContact::addContactsToGroup($contactIds, $ufGroupDetails['add_to_group_id']);
    }
    return $result;
}
Beispiel #8
0
 /**
  * Process activity creation.
  *
  * @param array $params
  *   Associated array of submitted values.
  *
  * @return self|null|object
  */
 protected function processActivity(&$params)
 {
     $activityAssigned = array();
     $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
     $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts);
     // format assignee params
     if (!CRM_Utils_Array::crmIsEmptyArray($params['assignee_contact_id'])) {
         //skip those assignee contacts which are already assigned
         //while sending a copy.CRM-4509.
         $activityAssigned = array_flip($params['assignee_contact_id']);
         if ($this->_activityId) {
             $assigneeContacts = CRM_Activity_BAO_ActivityContact::getNames($this->_activityId, $assigneeID);
             $activityAssigned = array_diff_key($activityAssigned, $assigneeContacts);
         }
     }
     // call begin post process. Idea is to let injecting file do
     // any processing before the activity is added/updated.
     $this->beginPostProcess($params);
     $activity = CRM_Activity_BAO_Activity::create($params);
     // add tags if exists
     $tagParams = array();
     if (!empty($params['tag'])) {
         foreach ($params['tag'] as $tag) {
             $tagParams[$tag] = 1;
         }
     }
     // Save static tags.
     CRM_Core_BAO_EntityTag::create($tagParams, 'civicrm_activity', $activity->id);
     // Save free tags.
     if (isset($params['activity_taglist']) && !empty($params['activity_taglist'])) {
         CRM_Core_Form_Tag::postProcess($params['activity_taglist'], $activity->id, 'civicrm_activity', $this);
     }
     // call end post process. Idea is to let injecting file do any
     // processing needed, after the activity has been added/updated.
     $this->endPostProcess($params, $activity);
     // CRM-9590
     if (!empty($params['is_multi_activity'])) {
         $this->_activityIds[] = $activity->id;
     } else {
         $this->_activityId = $activity->id;
     }
     // create follow up activity if needed
     $followupStatus = '';
     $followupActivity = NULL;
     if (!empty($params['followup_activity_type_id'])) {
         $followupActivity = CRM_Activity_BAO_Activity::createFollowupActivity($activity->id, $params);
         $followupStatus = ts('A followup activity has been scheduled.');
     }
     // send copy to assignee contacts.CRM-4509
     $mailStatus = '';
     if (Civi::settings()->get('activity_assignee_notification')) {
         $activityIDs = array($activity->id);
         if ($followupActivity) {
             $activityIDs = array_merge($activityIDs, array($followupActivity->id));
         }
         $assigneeContacts = CRM_Activity_BAO_ActivityAssignment::getAssigneeNames($activityIDs, TRUE, FALSE);
         if (!CRM_Utils_Array::crmIsEmptyArray($params['assignee_contact_id'])) {
             $mailToContacts = array();
             // Build an associative array with unique email addresses.
             foreach ($activityAssigned as $id => $dnc) {
                 if (isset($id) && array_key_exists($id, $assigneeContacts)) {
                     $mailToContacts[$assigneeContacts[$id]['email']] = $assigneeContacts[$id];
                 }
             }
             $sent = CRM_Activity_BAO_Activity::sendToAssignee($activity, $mailToContacts);
             if ($sent) {
                 $mailStatus .= ts("A copy of the activity has also been sent to assignee contacts(s).");
             }
         }
         // Also send email to follow-up activity assignees if set
         if ($followupActivity) {
             $mailToFollowupContacts = array();
             foreach ($assigneeContacts as $values) {
                 if ($values['activity_id'] == $followupActivity->id) {
                     $mailToFollowupContacts[$values['email']] = $values;
                 }
             }
             $sentFollowup = CRM_Activity_BAO_Activity::sendToAssignee($followupActivity, $mailToFollowupContacts);
             if ($sentFollowup) {
                 $mailStatus .= '<br />' . ts("A copy of the follow-up activity has also been sent to follow-up assignee contacts(s).");
             }
         }
     }
     // set status message
     $subject = '';
     if (!empty($params['subject'])) {
         $subject = "'" . $params['subject'] . "'";
     }
     CRM_Core_Session::setStatus(ts('Activity %1 has been saved. %2 %3', array(1 => $subject, 2 => $followupStatus, 3 => $mailStatus)), ts('Saved'), 'success');
     return $activity;
 }
/**
 * Update Profile field values.
 *
 * @param array  $params       Associative array of property name/value
 *                             pairs to update profile field values
 *
 * @return Updated Contact/ Activity object|CRM_Error
 *
 * @todo add example
 * @todo add test cases
 *
 */
function civicrm_api3_profile_set($params)
{
    civicrm_api3_verify_mandatory($params, NULL, array('profile_id'));
    if (!CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $params['profile_id'], 'is_active')) {
        return civicrm_api3_create_error('Invalid value for profile_id');
    }
    $isContactActivityProfile = CRM_Core_BAO_UFField::checkContactActivityProfileType($params['profile_id']);
    if (CRM_Core_BAO_UFField::checkProfileType($params['profile_id']) && !$isContactActivityProfile) {
        return civicrm_api3_create_error('Can not retrieve values for profiles include fields for more than one record type.');
    }
    $contactParams = $activityParams = $missingParams = array();
    $profileFields = CRM_Core_BAO_UFGroup::getFields($params['profile_id'], FALSE, NULL, NULL, NULL, FALSE, NULL, TRUE, NULL, CRM_Core_Permission::EDIT);
    if ($isContactActivityProfile) {
        civicrm_api3_verify_mandatory($params, NULL, array('activity_id'));
        require_once 'CRM/Profile/Form.php';
        $errors = CRM_Profile_Form::validateContactActivityProfile($params['activity_id'], $params['contact_id'], $params['profile_id']);
        if (!empty($errors)) {
            return civicrm_api3_create_error(array_pop($errors));
        }
    }
    foreach ($profileFields as $fieldName => $field) {
        if (CRM_Utils_Array::value('is_required', $field)) {
            if (!CRM_Utils_Array::value($fieldName, $params) || empty($params[$fieldName])) {
                $missingParams[] = $fieldName;
            }
        }
        if (!isset($params[$fieldName])) {
            continue;
        }
        $value = $params[$fieldName];
        if ($params[$fieldName] && isset($params[$fieldName . '_id'])) {
            $value = $params[$fieldName . '_id'];
        }
        if ($isContactActivityProfile && CRM_Utils_Array::value('field_type', $field) == 'Activity') {
            $activityParams[$fieldName] = $value;
        } else {
            $contactParams[$fieldName] = $value;
        }
    }
    if (!empty($missingParams)) {
        return civicrm_api3_create_error("Missing required parameters for profile id {$params['profile_id']}: " . implode(', ', $missingParams));
    }
    $contactParams['version'] = 3;
    $contactParams['contact_id'] = CRM_Utils_Array::value('contact_id', $params);
    $contactParams['profile_id'] = $params['profile_id'];
    $contactParams['skip_custom'] = 1;
    $contactProfileParams = civicrm_api3_profile_apply($contactParams);
    if (CRM_Utils_Array::value('is_error', $contactProfileParams)) {
        return $contactProfileParams;
    }
    // Contact profile fields
    $profileParams = $contactProfileParams['values'];
    // If profile having activity fields
    if ($isContactActivityProfile && !empty($activityParams)) {
        $activityParams['id'] = $params['activity_id'];
        $profileParams['api.activity.create'] = $activityParams;
    }
    $groups = $tags = array();
    if (isset($profileParams['group'])) {
        $groups = $profileParams['group'];
        unset($profileParams['group']);
    }
    if (isset($profileParams['tag'])) {
        $tags = $profileParams['tag'];
        unset($profileParams['tag']);
    }
    $result = civicrm_api('contact', 'create', $profileParams);
    if (CRM_Utils_Array::value('is_error', $result)) {
        return $result;
    }
    $ufGroupDetails = array();
    $ufGroupParams = array('id' => $params['profile_id']);
    CRM_Core_BAO_UFGroup::retrieve($ufGroupParams, $ufGroupDetails);
    if (isset($profileFields['group'])) {
        CRM_Contact_BAO_GroupContact::create($groups, $params['contact_id'], FALSE, 'Admin');
    }
    if (isset($profileFields['tag'])) {
        require_once 'CRM/Core/BAO/EntityTag.php';
        CRM_Core_BAO_EntityTag::create($tags, 'civicrm_contact', $params['contact_id']);
    }
    if (CRM_Utils_Array::value('add_to_group_id', $ufGroupDetails)) {
        $contactIds = array($params['contact_id']);
        CRM_Contact_BAO_GroupContact::addContactsToGroup($contactIds, $ufGroupDetails['add_to_group_id']);
    }
    return $result;
}
Beispiel #10
0
 /**
  * Form submission of new/edit contact is processed.
  *
  * @access public
  * @return None
  */
 function postProcess()
 {
     // check if dedupe button, if so return.
     $buttonName = $this->controller->getButtonName();
     if ($buttonName == $this->_dedupeButtonName) {
         return;
     }
     // store the submitted values in an array
     $params = $this->controller->exportValues($this->_name);
     // action is taken depending upon the mode
     $ids = array();
     if ($this->_action & CRM_CORE_ACTION_UPDATE) {
         // if update get all the valid database ids
         // from the session
         $ids = $this->get('ids');
     }
     $params['contact_type'] = $this->_contactType;
     $contact = CRM_Contact_BAO_Contact::create($params, $ids, CRM_CONTACT_FORM_EDIT_LOCATION_BLOCKS);
     //add contact to gruoup
     CRM_Contact_BAO_GroupContact::create($params['group'], $params['contact_id']);
     //add contact to tags
     CRM_Core_BAO_EntityTag::create($params['tag'], $params['contact_id']);
     // here we replace the user context with the url to view this contact
     $config =& CRM_Core_Config::singleton();
     $session =& CRM_Core_Session::singleton();
     CRM_Core_Session::setStatus(ts('Your %1 contact record has been saved.', array(1 => $contact->contact_type_display)));
     $buttonName = $this->controller->getButtonName();
     if ($buttonName == $this->getButtonName('next', 'new')) {
         // add the recently viewed contact
         list($displayName, $contactImage) = CRM_Contact_BAO_Contact::getDisplayAndImage($contact->id);
         CRM_Utils_Recent::add($displayName, CRM_Utils_System::url('civicrm/contact/view', 'reset=1&cid=' . $contact->id), $contactImage, $contact->id);
         $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/add' . $contact->contact_type[0], 'reset=1&c_type=' . $contact->contact_type));
     } else {
         $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view', 'reset=1&cid=' . $contact->id));
     }
     CRM_Core_BAO_CustomGroup::postProcess($this->_groupTree, $params);
     // do the updates/inserts
     CRM_Core_BAO_CustomGroup::updateCustomData($this->_groupTree, $this->_contactType, $contact->id);
 }
 /**
  * function to add/edit/register contacts through profile.
  *
  * @params  array  $params        Array of profile fields to be edited/added.
  * @params  int    $contactID     contact_id of the contact to be edited/added.
  * @params  array  $fields        array of fields from UFGroup
  * @params  int    $addToGroupID  specifies the default group to which contact is added.
  * @params  int    $ufGroupId     uf group id (profile id)
  * @param   string $ctype         contact type
  * @param   boolean $visibility   basically lets us know where this request is coming from
  *                                if via a profile from web, we restrict what groups are changed
  *
  * @return  int                   contact id created/edited
  * @static
  * @access public
  */
 static function createProfileContact(&$params, &$fields, $contactID = NULL, $addToGroupID = NULL, $ufGroupId = NULL, $ctype = NULL, $visibility = FALSE)
 {
     // add ufGroupID to params array ( CRM-2012 )
     if ($ufGroupId) {
         $params['uf_group_id'] = $ufGroupId;
     }
     if ($contactID) {
         $editHook = TRUE;
         CRM_Utils_Hook::pre('edit', 'Profile', $contactID, $params);
     } else {
         $editHook = FALSE;
         CRM_Utils_Hook::pre('create', 'Profile', NULL, $params);
     }
     list($data, $contactDetails) = self::formatProfileContactParams($params, $fields, $contactID, $ufGroupId, $ctype);
     // manage is_opt_out
     if (array_key_exists('is_opt_out', $fields) && array_key_exists('is_opt_out', $params)) {
         $wasOptOut = CRM_Utils_Array::value('is_opt_out', $contactDetails, FALSE);
         $isOptOut = CRM_Utils_Array::value('is_opt_out', $params, FALSE);
         $data['is_opt_out'] = $isOptOut;
         // on change, create new civicrm_subscription_history entry
         if ($wasOptOut != $isOptOut && CRM_Utils_Array::value('contact_id', $contactDetails)) {
             $shParams = array('contact_id' => $contactDetails['contact_id'], 'status' => $isOptOut ? 'Removed' : 'Added', 'method' => 'Web');
             CRM_Contact_BAO_SubscriptionHistory::create($shParams);
         }
     }
     if ($data['contact_type'] != 'Student') {
         $contact = self::create($data);
     }
     // contact is null if the profile does not have any contact fields
     if ($contact) {
         $contactID = $contact->id;
     }
     if (!$contactID) {
         CRM_Core_Error::fatal('Cannot proceed without a valid contact id');
     }
     // Process group and tag
     if (CRM_Utils_Array::value('group', $fields)) {
         $method = 'Admin';
         // this for sure means we are coming in via profile since i added it to fix
         // removing contacts from user groups -- lobo
         if ($visibility) {
             $method = 'Web';
         }
         CRM_Contact_BAO_GroupContact::create($params['group'], $contactID, $visibility, $method);
     }
     if (CRM_Utils_Array::value('tag', $fields)) {
         CRM_Core_BAO_EntityTag::create($params['tag'], 'civicrm_contact', $contactID);
     }
     //to add profile in default group
     if (is_array($addToGroupID)) {
         $contactIds = array($contactID);
         foreach ($addToGroupID as $groupId) {
             CRM_Contact_BAO_GroupContact::addContactsToGroup($contactIds, $groupId);
         }
     } elseif ($addToGroupID) {
         $contactIds = array($contactID);
         CRM_Contact_BAO_GroupContact::addContactsToGroup($contactIds, $addToGroupID);
     }
     //to update student record
     if (CRM_Core_Permission::access('Quest') && $studentFieldPresent) {
         $ids = array();
         $dao = new CRM_Quest_DAO_Student();
         $dao->contact_id = $contactID;
         if ($dao->find(TRUE)) {
             $ids['id'] = $dao->id;
         }
         $ssids = array();
         $studentSummary = new CRM_Quest_DAO_StudentSummary();
         $studentSummary->contact_id = $contactID;
         if ($studentSummary->find(TRUE)) {
             $ssids['id'] = $studentSummary->id;
         }
         $params['contact_id'] = $contactID;
         //fixed for check boxes
         $specialFields = array('educational_interest', 'college_type', 'college_interest', 'test_tutoring');
         foreach ($specialFields as $field) {
             if ($params[$field]) {
                 $params[$field] = implode(CRM_Core_DAO::VALUE_SEPARATOR, array_keys($params[$field]));
             }
         }
         CRM_Quest_BAO_Student::create($params, $ids);
         CRM_Quest_BAO_Student::createStudentSummary($params, $ssids);
     }
     // reset the group contact cache for this group
     CRM_Contact_BAO_GroupContactCache::remove();
     if ($editHook) {
         CRM_Utils_Hook::post('edit', 'Profile', $contactID, $params);
     } else {
         CRM_Utils_Hook::post('create', 'Profile', $contactID, $params);
     }
     return $contactID;
 }
Beispiel #12
0
 /**
  * Process the user submitted custom data values.
  *
  * @access public
  * @return void
  */
 function postProcess()
 {
     $params = $this->controller->exportValues($this->_name);
     $data = array();
     $data['contact_type'] = 'Individual';
     //get the custom fields for the contact
     $customFields = CRM_Core_BAO_CustomField::getFields($data['contact_type']);
     $locationType = array();
     $count = 1;
     if ($this->_id) {
         $primaryLocationType = CRM_Contact_BAO_Contact::getPrimaryLocationType($this->_id);
     }
     $phoneLoc = 0;
     foreach ($params as $key => $value) {
         $keyValue = explode('-', $key);
         if (is_numeric($keyValue[1])) {
             if (!in_array($keyValue[1], $locationType)) {
                 $locationType[$count] = $keyValue[1];
                 $count++;
             }
             require_once 'CRM/Utils/Array.php';
             $loc = CRM_Utils_Array::key($keyValue[1], $locationType);
             $data['location'][$loc]['location_type_id'] = $keyValue[1];
             if ($this->_id) {
                 //get the primary location type
                 if ($keyValue[1] == $primaryLocationType) {
                     $data['location'][$loc]['is_primary'] = 1;
                 }
             } else {
                 if ($loc == 1) {
                     $data['location'][$loc]['is_primary'] = 1;
                 }
             }
             if ($keyValue[0] == 'name') {
                 $data['location'][$loc]['name'] = $value;
             } else {
                 if ($keyValue[0] == 'phone') {
                     $phoneLoc++;
                     if ($keyValue[2]) {
                         $data['location'][$loc]['phone'][$phoneLoc]['phone_type'] = $keyValue[2];
                     } else {
                         $data['location'][$loc]['phone'][$phoneLoc]['phone_type'] = '';
                         $data['location'][$loc]['phone'][$phoneLoc]['is_primary'] = 1;
                     }
                     $data['location'][$loc]['phone'][$phoneLoc]['phone'] = $value;
                 } else {
                     if ($keyValue[0] == 'email') {
                         $data['location'][$loc]['email'][1]['email'] = $value;
                         $data['location'][$loc]['email'][1]['is_primary'] = 1;
                     } else {
                         if ($keyValue[0] == 'im') {
                             $data['location'][$loc]['im'][1]['name'] = $value;
                             $data['location'][$loc]['im'][1]['is_primary'] = 1;
                         } else {
                             if ($keyValue[0] === 'state_province') {
                                 $data['location'][$loc]['address']['state_province_id'] = $value;
                             } else {
                                 if ($keyValue[0] === 'country') {
                                     $data['location'][$loc]['address']['country_id'] = $value;
                                 } else {
                                     $data['location'][$loc]['address'][$keyValue[0]] = $value;
                                 }
                             }
                         }
                     }
                 }
             }
         } else {
             if ($key === 'individual_suffix') {
                 $data['suffix_id'] = $value;
             } else {
                 if ($key === 'individual_prefix') {
                     $data['prefix_id'] = $value;
                 } else {
                     if ($key === 'gender') {
                         $data['gender_id'] = $value;
                     } else {
                         if (substr($key, 0, 6) === 'custom') {
                             if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) {
                                 //fix checkbox
                                 if ($customFields[$customFieldID][3] == 'CheckBox') {
                                     $value = implode(CRM_CORE_BAO_CUSTOMOPTION_VALUE_SEPERATOR, array_keys($value));
                                 }
                                 if ($customFields[$customFieldID][3] == 'Multi-Select') {
                                     $value = implode(CRM_CORE_BAO_CUSTOMOPTION_VALUE_SEPERATOR, $value);
                                 }
                                 // fix the date field
                                 if ($customFields[$customFieldID][2] == 'Date') {
                                     $date = CRM_Utils_Date::format($value);
                                     if (!$date) {
                                         $date = '';
                                     }
                                     $value = $date;
                                 }
                                 //to add the id of custom value if exits
                                 //$this->_contact['custom_value_5_id'] = 123;
                                 $str = 'custom_value_' . $customFieldID . '_id';
                                 if ($this->_contact[$str]) {
                                     $id = $this->_contact[$str];
                                 }
                                 $data['custom'][$customFieldID] = array('id' => $id, 'value' => $value, 'extends' => $customFields[$customFieldID][3], 'type' => $customFields[$customFieldID][2], 'custom_field_id' => $customFieldID);
                             }
                         } else {
                             if ($key == 'edit') {
                                 continue;
                             } else {
                                 $data[$key] = $value;
                             }
                         }
                     }
                 }
             }
         }
     }
     // fix all the custom field checkboxes which are empty
     foreach ($this->_fields as $name => $field) {
         $cfID = CRM_Core_BAO_CustomField::getKeyID($name);
         // if there is a custom field of type checkbox and it has not been set
         // then set it to null, thanx to html protocol
         if ($cfID && $customFields[$cfID][3] == 'CheckBox' && CRM_Utils_Array::value('custom', $data) && !CRM_Utils_Array::value($cfID, $data['custom'])) {
             $str = 'custom_value_' . $cfID . '_id';
             if ($this->_contact[$str]) {
                 $id = $this->_contact[$str];
             }
             $data['custom'][$cfID] = array('id' => $id, 'value' => '', 'extends' => $customFields[$cfID][3], 'type' => $customFields[$cfID][2], 'custom_field_id' => $cfID);
         }
     }
     if ($this->_id) {
         $objects = array('contact_id', 'individual_id', 'location_id', 'address_id');
         $ids = array();
         $phoneLoc = 0;
         foreach ($this->_fields as $name => $field) {
             $nameValue = explode('-', $name);
             foreach ($this->_contact as $key => $value) {
                 if (in_array($key, $objects)) {
                     $ids[substr($key, 0, strlen($key) - 3)] = $value;
                 } else {
                     if (is_array($value)) {
                         //fixed for CRM-665
                         if ($nameValue[1] == $value['location_type_id']) {
                             $locations[$value['location_type_id']] = 1;
                             $loc_no = count($locations);
                             if ($nameValue[0] == 'phone') {
                                 $phoneLoc++;
                                 if (isset($nameValue[2])) {
                                     $ids['location'][$loc_no]['phone'][$phoneLoc] = $value['phone'][$nameValue[2] . '_id'];
                                 } else {
                                     $ids['location'][$loc_no]['phone'][$phoneLoc] = $value['phone']['1_id'];
                                 }
                             } else {
                                 if ($nameValue[0] == 'im') {
                                     $ids['location'][$loc_no]['im'][1] = $value['im']['1_id'];
                                 } else {
                                     if ($nameValue[0] == 'email') {
                                         $ids['location'][$loc_no]['email'][1] = $value['email']['1_id'];
                                     } else {
                                         $ids['location'][$loc_no]['address'] = $value['address_id'];
                                     }
                                 }
                             }
                             $ids['location'][$loc_no]['id'] = $value['location_id'];
                         }
                     }
                 }
             }
         }
     }
     //set the values for checkboxes (do_not_email, do_not_mail, do_not_trade, do_not_phone)
     $privacy = CRM_Core_SelectValues::privacy();
     foreach ($privacy as $key => $value) {
         if (array_key_exists($key, $this->_fields)) {
             if ($params[$key]) {
                 $data[$key] = $params[$key];
             } else {
                 $data[$key] = 0;
             }
         }
     }
     // manage is_opt_out
     if (array_key_exists('is_opt_out', $this->_fields)) {
         $wasOptOut = $this->_contact['is_opt_out'] ? true : false;
         $isOptOut = $params['is_opt_out'] ? true : false;
         $data['is_opt_out'] = $isOptOut;
         // on change, create new civicrm_subscription_history entry
         if ($wasOptOut != $isOptOut) {
             $shParams = array('contact_id' => $this->_contact['contact_id'], 'status' => $isOptOut ? 'Removed' : 'Added', 'method' => 'Web');
             CRM_Contact_BAO_SubscriptionHistory::create($shParams);
         }
     }
     if ($this->_mode == CRM_PROFILE_FORM_MODE_REGISTER) {
         require_once 'CRM/Core/BAO/Address.php';
         CRM_Core_BAO_Address::setOverwrite(false);
     }
     require_once 'CRM/Contact/BAO/Contact.php';
     $contact = CRM_Contact_BAO_Contact::create($data, $ids, count($data['location']));
     // Process group and tag
     if (CRM_Utils_Array::value('group', $this->_fields)) {
         CRM_Contact_BAO_GroupContact::create($params['group'], $contact->id);
     }
     if (CRM_Utils_Array::value('tag', $this->_fields)) {
         require_once 'CRM/Core/BAO/EntityTag.php';
         CRM_Core_BAO_EntityTag::create($params['tag'], $contact->id);
     }
 }
Beispiel #13
0
 /**
  * Function to process the form
  *
  * @access public
  * @return None
  */
 public function postProcess()
 {
     if ($this->_action & CRM_Core_Action::DELETE) {
         $statusMsg = null;
         //block deleting activities which affects
         //case attributes.CRM-4543
         $activityCondition = " AND v.name IN ('Open Case', 'Change Case Type', 'Change Case Status', 'Change Case Start Date')";
         $caseAttributeActivities = CRM_Core_OptionGroup::values('activity_type', false, false, false, $activityCondition);
         if (!array_key_exists($this->_activityTypeId, $caseAttributeActivities)) {
             $params = array('id' => $this->_activityId);
             $activityDelete = CRM_Activity_BAO_Activity::deleteActivity($params, true);
             if ($activityDelete) {
                 $statusMsg = ts('The selected activity has been moved to the Trash. You can view and / or restore deleted activities by checking "Deleted Activities" from the Case Activities search filter (under Manage Case).<br />');
             }
         } else {
             $statusMsg = ts("Selected Activity cannot be deleted.");
         }
         require_once 'CRM/Core/BAO/EntityTag.php';
         $tagParams = array('entity_table' => 'civicrm_activity', 'entity_id' => $this->_activityId);
         CRM_Core_BAO_EntityTag::del($tagParams);
         CRM_Core_Session::setStatus($statusMsg);
         return;
     }
     if ($this->_action & CRM_Core_Action::RENEW) {
         $statusMsg = null;
         $params = array('id' => $this->_activityId);
         $activityRestore = CRM_Activity_BAO_Activity::restoreActivity($params);
         if ($activityRestore) {
             $statusMsg = ts('The selected activity has been restored.<br />');
         }
         CRM_Core_Session::setStatus($statusMsg);
         return;
     }
     // store the submitted values in an array
     $params = $this->controller->exportValues($this->_name);
     if ($params['source_contact_id']) {
         $params['source_contact_id'] = $params['source_contact_qid'];
     }
     //set parent id if its edit mode
     if ($parentId = CRM_Utils_Array::value('parent_id', $this->_defaults)) {
         $params['parent_id'] = $parentId;
     }
     // required for status msg
     $recordStatus = 'created';
     // store the dates with proper format
     $params['activity_date_time'] = CRM_Utils_Date::processDate($params['activity_date_time'], $params['activity_date_time_time']);
     $params['activity_type_id'] = $this->_activityTypeId;
     require_once 'CRM/Case/XMLProcessor/Process.php';
     $xmlProcessorProcess = new CRM_Case_XMLProcessor_Process();
     $isMultiClient = $xmlProcessorProcess->getAllowMultipleCaseClients();
     $this->assign('multiClient', $isMultiClient);
     $targetContacts = array($this->_currentlyViewedContactId);
     if (CRM_Utils_Array::value('hidden_target_contact', $params) && CRM_Utils_Array::value('target_contact_id', $params)) {
         $targetContacts = array_unique(explode(',', $params['target_contact_id']));
     }
     $params['target_contact_id'] = $targetContacts;
     // format activity custom data
     if (CRM_Utils_Array::value('hidden_custom', $params)) {
         if ($this->_activityId) {
             // unset custom fields-id from params since we want custom
             // fields to be saved for new activity.
             foreach ($params as $key => $value) {
                 $match = array();
                 if (preg_match('/^(custom_\\d+_)(\\d+)$/', $key, $match)) {
                     $params[$match[1] . '-1'] = $params[$key];
                     // for autocomplete transfer hidden value instead of label
                     if ($params[$key] && isset($params[$key . '_id'])) {
                         $params[$match[1] . '-1_id'] = $params[$key . '_id'];
                         unset($params[$key . '_id']);
                     }
                     unset($params[$key]);
                 }
             }
         }
         // build custom data getFields array
         $customFields = CRM_Core_BAO_CustomField::getFields('Activity', false, false, $this->_activityTypeId);
         $customFields = CRM_Utils_Array::crmArrayMerge($customFields, CRM_Core_BAO_CustomField::getFields('Activity', false, false, null, null, true));
         $params['custom'] = CRM_Core_BAO_CustomField::postProcess($params, $customFields, $this->_activityId, 'Activity');
     }
     if (CRM_Utils_Array::value('assignee_contact_id', $params)) {
         $assineeContacts = explode(',', $params['assignee_contact_id']);
         $assineeContacts = array_unique($assineeContacts);
         unset($params['assignee_contact_id']);
     } else {
         $params['assignee_contact_id'] = $assineeContacts = array();
     }
     if (isset($this->_activityId)) {
         // activity which hasn't been modified by a user yet
         if ($this->_defaults['is_auto'] == 1) {
             $params['is_auto'] = 0;
         }
         // always create a revision of an case activity. CRM-4533
         $newActParams = $params;
         // add target contact values in update mode
         if (empty($params['target_contact_id']) && !empty($this->_defaults['target_contact'])) {
             $newActParams['target_contact_id'] = $this->_defaults['target_contact'];
         }
         // record status for status msg
         $recordStatus = 'updated';
     }
     if (!isset($newActParams)) {
         // add more attachments if needed for old activity
         CRM_Core_BAO_File::formatAttachment($params, $params, 'civicrm_activity');
         // call begin post process, before the activity is created/updated.
         $this->beginPostProcess($params);
         $params['case_id'] = $this->_caseId;
         // activity create/update
         $activity = CRM_Activity_BAO_Activity::create($params);
         // call end post process, after the activity has been created/updated.
         $this->endPostProcess($params, $activity);
     } else {
         // since the params we need to set are very few, and we don't want rest of the
         // work done by bao create method , lets use dao object to make the changes
         $params = array('id' => $this->_activityId);
         $params['is_current_revision'] = 0;
         $activity = new CRM_Activity_DAO_Activity();
         $activity->copyValues($params);
         $activity->save();
     }
     // create a new version of activity if activity was found to
     // have been modified/created by user
     if (isset($newActParams)) {
         // set proper original_id
         if (CRM_Utils_Array::value('original_id', $this->_defaults)) {
             $newActParams['original_id'] = $this->_defaults['original_id'];
         } else {
             $newActParams['original_id'] = $activity->id;
         }
         //is_current_revision will be set to 1 by default.
         // add attachments if any
         CRM_Core_BAO_File::formatAttachment($newActParams, $newActParams, 'civicrm_activity');
         // call begin post process, before the activity is created/updated.
         $this->beginPostProcess($newActParams);
         $newActParams['case_id'] = $this->_caseId;
         $activity = CRM_Activity_BAO_Activity::create($newActParams);
         // call end post process, after the activity has been created/updated.
         $this->endPostProcess($newActParams, $activity);
         // copy files attached to old activity if any, to new one,
         // as long as users have not selected the 'delete attachment' option.
         if (!CRM_Utils_Array::value('is_delete_attachment', $newActParams)) {
             CRM_Core_BAO_File::copyEntityFile('civicrm_activity', $this->_activityId, 'civicrm_activity', $activity->id);
         }
         // copy back params to original var
         $params = $newActParams;
     }
     if ($activity->id) {
         // add tags if exists
         $tagParams = array();
         if (!empty($params['tag'])) {
             foreach ($params['tag'] as $tag) {
                 $tagParams[$tag] = 1;
             }
         }
         //save static tags
         require_once 'CRM/Core/BAO/EntityTag.php';
         CRM_Core_BAO_EntityTag::create($tagParams, 'civicrm_activity', $activity->id);
         //save free tags
         if (isset($params['taglist']) && !empty($params['taglist'])) {
             require_once 'CRM/Core/Form/Tag.php';
             CRM_Core_Form_Tag::postProcess($params['taglist'], $activity->id, 'civicrm_activity', $this);
         }
     }
     $params['assignee_contact_id'] = $assineeContacts;
     // update existing case record if needed
     $caseParams = $params;
     $caseParams['id'] = $this->_caseId;
     if (CRM_Utils_Array::value('case_type_id', $caseParams)) {
         $caseParams['case_type_id'] = CRM_Case_BAO_Case::VALUE_SEPERATOR . $caseParams['case_type_id'] . CRM_Case_BAO_Case::VALUE_SEPERATOR;
     }
     if (CRM_Utils_Array::value('case_status_id', $caseParams)) {
         $caseParams['status_id'] = $caseParams['case_status_id'];
     }
     // unset params intended for activities only
     unset($caseParams['subject'], $caseParams['details'], $caseParams['status_id'], $caseParams['custom']);
     $case = CRM_Case_BAO_Case::create($caseParams);
     // create case activity record
     $caseParams = array('activity_id' => $activity->id, 'case_id' => $this->_caseId);
     CRM_Case_BAO_Case::processCaseActivity($caseParams);
     // create activity assignee records
     $assigneeParams = array('activity_id' => $activity->id);
     if (!CRM_Utils_Array::crmIsEmptyArray($params['assignee_contact_id'])) {
         //skip those assignee contacts which are already assigned
         //while sending a copy.CRM-4509.
         $activityAssigned = array_flip($params['assignee_contact_id']);
         $activityId = isset($this->_activityId) ? $this->_activityId : $activity->id;
         $assigneeContacts = CRM_Activity_BAO_ActivityAssignment::getAssigneeNames($activityId);
         $activityAssigned = array_diff_key($activityAssigned, $assigneeContacts);
         foreach ($params['assignee_contact_id'] as $key => $id) {
             $assigneeParams['assignee_contact_id'] = $id;
             CRM_Activity_BAO_Activity::createActivityAssignment($assigneeParams);
         }
         //modify assigne_contact as per newly assigned contact before sending copy. CRM-4509.
         $params['assignee_contact_id'] = $activityAssigned;
     }
     // Insert civicrm_log record for the activity (e.g. store the
     // created / edited by contact id and date for the activity)
     // Note - civicrm_log is already created by CRM_Activity_BAO_Activity::create()
     // send copy to selected contacts.
     $mailStatus = '';
     $mailToContacts = array();
     //CRM-5695
     //check for notification settings for assignee contacts
     $selectedContacts = array('contact_check');
     $config =& CRM_Core_Config::singleton();
     if ($config->activityAssigneeNotification) {
         $selectedContacts[] = 'assignee_contact_id';
     }
     foreach ($selectedContacts as $dnt => $val) {
         if (array_key_exists($val, $params) && !CRM_Utils_array::crmIsEmptyArray($params[$val])) {
             if ($val == 'contact_check') {
                 $mailStatus = ts("A copy of the activity has also been sent to selected contacts(s).");
             } else {
                 $this->_relatedContacts = CRM_Activity_BAO_ActivityAssignment::getAssigneeNames($activity->id, true, false);
                 $mailStatus .= ' ' . ts("A copy of the activity has also been sent to assignee contacts(s).");
             }
             //build an associative array with unique email addresses.
             foreach ($params[$val] as $id => $dnc) {
                 if (isset($id) && array_key_exists($id, $this->_relatedContacts)) {
                     //if email already exists in array then append with ', ' another role only otherwise add it to array.
                     if ($contactDetails = CRM_Utils_Array::value($this->_relatedContacts[$id]['email'], $mailToContacts)) {
                         $caseRole = CRM_Utils_Array::value('role', $this->_relatedContacts[$id]);
                         $mailToContacts[$this->_relatedContacts[$id]['email']]['role'] = $contactDetails['role'] . ', ' . $caseRole;
                     } else {
                         $mailToContacts[$this->_relatedContacts[$id]['email']] = $this->_relatedContacts[$id];
                     }
                 }
             }
         }
     }
     if (!CRM_Utils_array::crmIsEmptyArray($mailToContacts)) {
         //include attachments while sendig a copy of activity.
         $attachments =& CRM_Core_BAO_File::getEntityFile('civicrm_activity', $activity->id);
         $result = CRM_Case_BAO_Case::sendActivityCopy($this->_currentlyViewedContactId, $activity->id, $mailToContacts, $attachments, $this->_caseId);
         if (empty($result)) {
             $mailStatus = '';
         }
     } else {
         $mailStatus = '';
     }
     // create follow up activity if needed
     $followupStatus = '';
     if (CRM_Utils_Array::value('followup_activity_type_id', $params)) {
         $followupActivity = CRM_Activity_BAO_Activity::createFollowupActivity($activity->id, $params);
         if ($followupActivity) {
             $caseParams = array('activity_id' => $followupActivity->id, 'case_id' => $this->_caseId);
             CRM_Case_BAO_Case::processCaseActivity($caseParams);
             $followupStatus = ts("A followup activity has been scheduled.");
         }
     }
     CRM_Core_Session::setStatus(ts("'%1' activity has been %2. %3 %4", array(1 => $this->_activityTypeName, 2 => $recordStatus, 3 => $followupStatus, 4 => $mailStatus)));
 }
 /**
  * Add/edit/register contacts through profile.
  *
  * @param array $params
  *   Array of profile fields to be edited/added.
  * @param array $fields
  *   Array of fields from UFGroup.
  * @param int $contactID
  *   Id of the contact to be edited/added.
  * @param int $addToGroupID
  *   Specifies the default group to which contact is added.
  * @param int $ufGroupId
  *   Uf group id (profile id).
  * @param string $ctype
  * @param bool $visibility
  *   Basically lets us know where this request is coming from.
  *                                if via a profile from web, we restrict what groups are changed
  *
  * @return int
  *   contact id created/edited
  */
 public static function createProfileContact(&$params, &$fields, $contactID = NULL, $addToGroupID = NULL, $ufGroupId = NULL, $ctype = NULL, $visibility = FALSE)
 {
     // add ufGroupID to params array ( CRM-2012 )
     if ($ufGroupId) {
         $params['uf_group_id'] = $ufGroupId;
     }
     if ($contactID) {
         $editHook = TRUE;
         CRM_Utils_Hook::pre('edit', 'Profile', $contactID, $params);
     } else {
         $editHook = FALSE;
         CRM_Utils_Hook::pre('create', 'Profile', NULL, $params);
     }
     list($data, $contactDetails) = self::formatProfileContactParams($params, $fields, $contactID, $ufGroupId, $ctype);
     // manage is_opt_out
     if (array_key_exists('is_opt_out', $fields) && array_key_exists('is_opt_out', $params)) {
         $wasOptOut = CRM_Utils_Array::value('is_opt_out', $contactDetails, FALSE);
         $isOptOut = CRM_Utils_Array::value('is_opt_out', $params, FALSE);
         $data['is_opt_out'] = $isOptOut;
         // on change, create new civicrm_subscription_history entry
         if ($wasOptOut != $isOptOut && !empty($contactDetails['contact_id'])) {
             $shParams = array('contact_id' => $contactDetails['contact_id'], 'status' => $isOptOut ? 'Removed' : 'Added', 'method' => 'Web');
             CRM_Contact_BAO_SubscriptionHistory::create($shParams);
         }
     }
     $contact = self::create($data);
     // contact is null if the profile does not have any contact fields
     if ($contact) {
         $contactID = $contact->id;
     }
     if (empty($contactID)) {
         CRM_Core_Error::fatal('Cannot proceed without a valid contact id');
     }
     // Process group and tag
     if (!empty($fields['group'])) {
         $method = 'Admin';
         // this for sure means we are coming in via profile since i added it to fix
         // removing contacts from user groups -- lobo
         if ($visibility) {
             $method = 'Web';
         }
         CRM_Contact_BAO_GroupContact::create($params['group'], $contactID, $visibility, $method);
     }
     if (!empty($fields['tag'])) {
         CRM_Core_BAO_EntityTag::create($params['tag'], 'civicrm_contact', $contactID);
     }
     //to add profile in default group
     if (is_array($addToGroupID)) {
         $contactIds = array($contactID);
         foreach ($addToGroupID as $groupId) {
             CRM_Contact_BAO_GroupContact::addContactsToGroup($contactIds, $groupId);
         }
     } elseif ($addToGroupID) {
         $contactIds = array($contactID);
         CRM_Contact_BAO_GroupContact::addContactsToGroup($contactIds, $addToGroupID);
     }
     // reset the group contact cache for this group
     CRM_Contact_BAO_GroupContactCache::remove();
     if ($editHook) {
         CRM_Utils_Hook::post('edit', 'Profile', $contactID, $params);
     } else {
         CRM_Utils_Hook::post('create', 'Profile', $contactID, $params);
     }
     return $contactID;
 }
Beispiel #15
0
 /**
  *
  * @access public
  * @return None
  */
 function postProcess()
 {
     // array contains the posted values
     // exportvalues is not used because its give value 1 of the checkbox which were checked by default,
     // even after unchecking them before submitting them
     $contactTag = $_POST['tagList'];
     CRM_Core_BAO_EntityTag::create($contactTag, $this->_contactId);
     CRM_Core_Session::setStatus(ts('Your update(s) have been saved.'));
 }
 /**
  * Function to process the form
  *
  * @access public
  *
  * @return None
  */
 public function postProcess()
 {
     $tx = new CRM_Core_Transaction();
     // check if dedupe button, if so return.
     $buttonName = $this->controller->getButtonName();
     if (isset($this->_dedupeButtonName) && $buttonName == $this->_dedupeButtonName) {
         return;
     }
     if ($this->_action & CRM_Core_Action::DELETE) {
         $statusMsg = NULL;
         $caseDelete = CRM_Case_BAO_Case::deleteCase($this->_caseId, TRUE);
         if ($caseDelete) {
             $statusMsg = ts('The selected case has been moved to the Trash. You can view and / or restore deleted cases by checking the "Deleted Cases" option under Find Cases.<br />');
         }
         CRM_Core_Session::setStatus($statusMsg);
         return;
     }
     if ($this->_action & CRM_Core_Action::RENEW) {
         $statusMsg = NULL;
         $caseRestore = CRM_Case_BAO_Case::restoreCase($this->_caseId);
         if ($caseRestore) {
             $statusMsg = ts('The selected case has been restored.<br />');
         }
         CRM_Core_Session::setStatus($statusMsg);
         return;
     }
     // store the submitted values in an array
     $params = $this->controller->exportValues($this->_name);
     $params['now'] = date("Ymd");
     // 1. call begin post process
     if ($this->_activityTypeFile) {
         eval("CRM_Case_Form_Activity_{$this->_activityTypeFile}" . "::beginPostProcess( \$this, \$params );");
     }
     if (CRM_Utils_Array::value('hidden_custom', $params) && !isset($params['custom'])) {
         $customFields = array();
         $params['custom'] = CRM_Core_BAO_CustomField::postProcess($params, $customFields, NULL, 'Case');
     }
     // 2. create/edit case
     if (CRM_Utils_Array::value('case_type_id', $params)) {
         $caseType = CRM_Case_PseudoConstant::caseType('name');
         $params['case_type'] = $caseType[$params['case_type_id']];
         $params['subject'] = $params['activity_subject'];
         $params['case_type_id'] = CRM_Core_DAO::VALUE_SEPARATOR . $params['case_type_id'] . CRM_Core_DAO::VALUE_SEPARATOR;
     }
     $caseObj = CRM_Case_BAO_Case::create($params);
     $params['case_id'] = $caseObj->id;
     // unset any ids, custom data
     unset($params['id'], $params['custom']);
     // add tags if exists
     $tagParams = array();
     if (!empty($params['tag'])) {
         $tagParams = array();
         foreach ($params['tag'] as $tag) {
             $tagParams[$tag] = 1;
         }
     }
     CRM_Core_BAO_EntityTag::create($tagParams, 'civicrm_case', $caseObj->id);
     //save free tags
     if (isset($params['case_taglist']) && !empty($params['case_taglist'])) {
         CRM_Core_Form_Tag::postProcess($params['case_taglist'], $caseObj->id, 'civicrm_case', $this);
     }
     // user context
     $url = CRM_Utils_System::url('civicrm/contact/view/case', "reset=1&action=view&cid={$this->_currentlyViewedContactId}&id={$caseObj->id}");
     $session = CRM_Core_Session::singleton();
     $session->pushUserContext($url);
     // 3. format activity custom data
     if (CRM_Utils_Array::value('hidden_custom', $params)) {
         $customFields = CRM_Core_BAO_CustomField::getFields('Activity', FALSE, FALSE, $this->_activityTypeId);
         $customFields = CRM_Utils_Array::crmArrayMerge($customFields, CRM_Core_BAO_CustomField::getFields('Activity', FALSE, FALSE, NULL, NULL, TRUE));
         $params['custom'] = CRM_Core_BAO_CustomField::postProcess($params, $customFields, $this->_activityId, 'Activity');
     }
     // 4. call end post process
     if ($this->_activityTypeFile) {
         eval("CRM_Case_Form_Activity_{$this->_activityTypeFile}" . "::endPostProcess( \$this, \$params );");
     }
     // 5. auto populate activites
     // 6. set status
     CRM_Core_Session::setStatus("{$params['statusMsg']}");
 }
Beispiel #17
0
 /**
  * Process activity creation
  *
  * @param array $params associated array of submitted values
  * @access protected
  */
 protected function processActivity(&$params)
 {
     $activityAssigned = array();
     $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
     $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts);
     // format assignee params
     if (!CRM_Utils_Array::crmIsEmptyArray($params['assignee_contact_id'])) {
         //skip those assignee contacts which are already assigned
         //while sending a copy.CRM-4509.
         $activityAssigned = array_flip($params['assignee_contact_id']);
         if ($this->_activityId) {
             $assigneeContacts = CRM_Activity_BAO_ActivityContact::getNames($this->_activityId, $assigneeID);
             $activityAssigned = array_diff_key($activityAssigned, $assigneeContacts);
         }
     }
     // call begin post process. Idea is to let injecting file do
     // any processing before the activity is added/updated.
     $this->beginPostProcess($params);
     $activity = CRM_Activity_BAO_Activity::create($params);
     // add tags if exists
     $tagParams = array();
     if (!empty($params['tag'])) {
         foreach ($params['tag'] as $tag) {
             $tagParams[$tag] = 1;
         }
     }
     //save static tags
     CRM_Core_BAO_EntityTag::create($tagParams, 'civicrm_activity', $activity->id);
     //save free tags
     if (isset($params['activity_taglist']) && !empty($params['activity_taglist'])) {
         CRM_Core_Form_Tag::postProcess($params['activity_taglist'], $activity->id, 'civicrm_activity', $this);
     }
     // call end post process. Idea is to let injecting file do any
     // processing needed, after the activity has been added/updated.
     $this->endPostProcess($params, $activity);
     // CRM-9590
     if (CRM_Utils_Array::value('is_multi_activity', $params)) {
         $this->_activityIds[] = $activity->id;
     } else {
         $this->_activityId = $activity->id;
     }
     // create follow up activity if needed
     $followupStatus = '';
     if (CRM_Utils_Array::value('followup_activity_type_id', $params)) {
         CRM_Activity_BAO_Activity::createFollowupActivity($activity->id, $params);
         $followupStatus = ts('A followup activity has been scheduled.');
     }
     // send copy to assignee contacts.CRM-4509
     $mailStatus = '';
     if (!CRM_Utils_Array::crmIsEmptyArray($params['assignee_contact_id']) && CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'activity_assignee_notification')) {
         $mailToContacts = array();
         $assigneeContacts = CRM_Activity_BAO_ActivityAssignment::getAssigneeNames($activity->id, TRUE, FALSE);
         //build an associative array with unique email addresses.
         foreach ($activityAssigned as $id => $dnc) {
             if (isset($id) && array_key_exists($id, $assigneeContacts)) {
                 $mailToContacts[$assigneeContacts[$id]['email']] = $assigneeContacts[$id];
             }
         }
         if (!CRM_Utils_array::crmIsEmptyArray($mailToContacts)) {
             //include attachments while sending a copy of activity.
             $attachments = CRM_Core_BAO_File::getEntityFile('civicrm_activity', $activity->id);
             $ics = new CRM_Activity_BAO_ICalendar($activity);
             $ics->addAttachment($attachments, $mailToContacts);
             // CRM-8400 add param with _currentlyViewedContactId for URL link in mail
             CRM_Case_BAO_Case::sendActivityCopy(NULL, $activity->id, $mailToContacts, $attachments, NULL);
             $ics->cleanup();
             $mailStatus .= ts("A copy of the activity has also been sent to assignee contacts(s).");
         }
     }
     // set status message
     $subject = '';
     if (CRM_Utils_Array::value('subject', $params)) {
         $subject = "'" . $params['subject'] . "'";
     }
     CRM_Core_Session::setStatus(ts('Activity %1 has been saved. %2 %3', array(1 => $subject, 2 => $followupStatus, 3 => $mailStatus)), ts('Saved'), 'success');
     return $activity;
 }
Beispiel #18
0
 static function filePostProcess($data, $fileTypeID, $entityTable, $entityID, $entitySubtype, $overwrite = TRUE, $fileParams = NULL, $uploadName = 'uploadFile', $mimeType = null)
 {
     if (!$mimeType) {
         CRM_Core_Error::fatal(ts('Mime Type is now a required parameter'));
     }
     $config = CRM_Core_Config::singleton();
     $path = explode('/', $data);
     $filename = $path[count($path) - 1];
     // rename this file to go into the secure directory
     if ($entitySubtype) {
         $directoryName = $config->customFileUploadDir . $entitySubtype . DIRECTORY_SEPARATOR . $entityID;
     } else {
         $directoryName = $config->customFileUploadDir;
     }
     CRM_Utils_File::createDir($directoryName);
     if (!rename($data, $directoryName . DIRECTORY_SEPARATOR . $filename)) {
         CRM_Core_Error::fatal(ts('Could not move custom file to custom upload directory'));
         break;
     }
     // to get id's
     if ($overwrite && $fileTypeID) {
         list($sql, $params) = self::sql($entityTable, $entityID, $fileTypeID);
     } else {
         list($sql, $params) = self::sql($entityTable, $entityID, 0);
     }
     $dao = CRM_Core_DAO::executeQuery($sql, $params);
     $dao->fetch();
     $fileDAO = new CRM_Core_DAO_File();
     $op = 'create';
     if (isset($dao->cfID) && $dao->cfID) {
         $op = 'edit';
         $fileDAO->id = $dao->cfID;
         unlink($directoryName . DIRECTORY_SEPARATOR . $dao->uri);
     }
     if (!empty($fileParams)) {
         $fileDAO->copyValues($fileParams);
     }
     $fileDAO->uri = $filename;
     $fileDAO->mime_type = $mimeType;
     $fileDAO->file_type_id = $fileTypeID;
     $fileDAO->upload_date = date('Ymdhis');
     $fileDAO->save();
     // need to add/update civicrm_entity_file
     $entityFileDAO = new CRM_Core_DAO_EntityFile();
     if (isset($dao->cefID) && $dao->cefID) {
         $entityFileDAO->id = $dao->cefID;
     }
     $entityFileDAO->entity_table = $entityTable;
     $entityFileDAO->entity_id = $entityID;
     $entityFileDAO->file_id = $fileDAO->id;
     $entityFileDAO->save();
     //save static tags
     if (!empty($fileParams['tag'])) {
         CRM_Core_BAO_EntityTag::create($fileParams['tag'], 'civicrm_file', $entityFileDAO->id);
     }
     //save free tags
     if (isset($fileParams['attachment_taglist']) && !empty($fileParams['attachment_taglist'])) {
         CRM_Core_Form_Tag::postProcess($fileParams['attachment_taglist'], $entityFileDAO->id, 'civicrm_file', CRM_Core_DAO::$_nullObject);
     }
     // lets call the post hook here so attachments code can do the right stuff
     CRM_Utils_Hook::post($op, 'File', $fileDAO->id, $fileDAO);
 }
Beispiel #19
0
 /**
  * Function to process the form
  *
  * @access public
  * @return None
  */
 public function postProcess($params = null)
 {
     if ($this->_action & CRM_Core_Action::DELETE) {
         $deleteParams = array('id' => $this->_activityId);
         require_once 'CRM/Case/BAO/Case.php';
         $moveToTrash = CRM_Case_BAO_Case::isCaseActivity($this->_activityId);
         CRM_Activity_BAO_Activity::deleteActivity($deleteParams, $moveToTrash);
         // delete tags for the entity
         require_once 'CRM/Core/BAO/EntityTag.php';
         $tagParams = array('entity_table' => 'civicrm_activity', 'entity_id' => $this->_activityId);
         CRM_Core_BAO_EntityTag::del($tagParams);
         CRM_Core_Session::setStatus(ts("Selected Activity has been deleted sucessfully."));
         return;
     }
     // store the submitted values in an array
     if (!$params) {
         $params = $this->controller->exportValues($this->_name);
     }
     //set activity type id
     if (!CRM_Utils_Array::value('activity_type_id', $params)) {
         $params['activity_type_id'] = $this->_activityTypeId;
     }
     if (CRM_Utils_Array::value('hidden_custom', $params) && !isset($params['custom'])) {
         $customFields = CRM_Core_BAO_CustomField::getFields('Activity', false, false, $this->_activityTypeId);
         $customFields = CRM_Utils_Array::crmArrayMerge($customFields, CRM_Core_BAO_CustomField::getFields('Activity', false, false, null, null, true));
         $params['custom'] = CRM_Core_BAO_CustomField::postProcess($params, $customFields, $this->_activityId, 'Activity');
     }
     // store the date with proper format
     $params['activity_date_time'] = CRM_Utils_Date::processDate($params['activity_date_time'], $params['activity_date_time_time']);
     // assigning formated value to related variable
     if (CRM_Utils_Array::value('target_contact_id', $params)) {
         $params['target_contact_id'] = explode(',', $params['target_contact_id']);
     } else {
         $params['target_contact_id'] = array();
     }
     if (CRM_Utils_Array::value('assignee_contact_id', $params)) {
         $params['assignee_contact_id'] = explode(',', $params['assignee_contact_id']);
     } else {
         $params['assignee_contact_id'] = array();
     }
     // get ids for associated contacts
     if (!$params['source_contact_id']) {
         $params['source_contact_id'] = $this->_currentUserId;
     } else {
         $params['source_contact_id'] = $this->_submitValues['source_contact_qid'];
     }
     if (isset($this->_activityId)) {
         $params['id'] = $this->_activityId;
     }
     // add attachments as needed
     CRM_Core_BAO_File::formatAttachment($params, $params, 'civicrm_activity', $this->_activityId);
     // format target params
     if (!$this->_single) {
         $params['target_contact_id'] = $this->_contactIds;
     }
     $activityAssigned = array();
     // format assignee params
     if (!CRM_Utils_Array::crmIsEmptyArray($params['assignee_contact_id'])) {
         //skip those assignee contacts which are already assigned
         //while sending a copy.CRM-4509.
         $activityAssigned = array_flip($params['assignee_contact_id']);
         if ($this->_activityId) {
             $assigneeContacts = CRM_Activity_BAO_ActivityAssignment::getAssigneeNames($this->_activityId);
             $activityAssigned = array_diff_key($activityAssigned, $assigneeContacts);
         }
     }
     // call begin post process. Idea is to let injecting file do
     // any processing before the activity is added/updated.
     $this->beginPostProcess($params);
     $activity = CRM_Activity_BAO_Activity::create($params);
     // add tags if exists
     $tagParams = array();
     if (!empty($params['tag'])) {
         foreach ($params['tag'] as $tag) {
             $tagParams[$tag] = 1;
         }
     }
     //save static tags
     require_once 'CRM/Core/BAO/EntityTag.php';
     CRM_Core_BAO_EntityTag::create($tagParams, 'civicrm_activity', $activity->id);
     //save free tags
     if (isset($params['taglist']) && !empty($params['taglist'])) {
         require_once 'CRM/Core/Form/Tag.php';
         CRM_Core_Form_Tag::postProcess($params['taglist'], $activity->id, 'civicrm_activity', $this);
     }
     // call end post process. Idea is to let injecting file do any
     // processing needed, after the activity has been added/updated.
     $this->endPostProcess($params, $activity);
     // create follow up activity if needed
     $followupStatus = '';
     if (CRM_Utils_Array::value('followup_activity_type_id', $params)) {
         $followupActivity = CRM_Activity_BAO_Activity::createFollowupActivity($activity->id, $params);
         $followupStatus = "A followup activity has been scheduled.";
     }
     // send copy to assignee contacts.CRM-4509
     $mailStatus = '';
     $config =& CRM_Core_Config::singleton();
     if (!CRM_Utils_Array::crmIsEmptyArray($params['assignee_contact_id']) && $config->activityAssigneeNotification) {
         $mailToContacts = array();
         $assigneeContacts = CRM_Activity_BAO_ActivityAssignment::getAssigneeNames($activity->id, true, false);
         //build an associative array with unique email addresses.
         foreach ($activityAssigned as $id => $dnc) {
             if (isset($id) && array_key_exists($id, $assigneeContacts)) {
                 $mailToContacts[$assigneeContacts[$id]['email']] = $assigneeContacts[$id];
             }
         }
         if (!CRM_Utils_array::crmIsEmptyArray($mailToContacts)) {
             //include attachments while sendig a copy of activity.
             $attachments =& CRM_Core_BAO_File::getEntityFile('civicrm_activity', $activity->id);
             require_once "CRM/Case/BAO/Case.php";
             $result = CRM_Case_BAO_Case::sendActivityCopy(null, $activity->id, $mailToContacts, $attachments, null);
             $mailStatus .= ts("A copy of the activity has also been sent to assignee contacts(s).");
         }
     }
     // set status message
     if (CRM_Utils_Array::value('subject', $params)) {
         $params['subject'] = "'" . $params['subject'] . "'";
     }
     CRM_Core_Session::setStatus(ts('Activity %1 has been saved. %2. %3', array(1 => $params['subject'], 2 => $followupStatus, 3 => $mailStatus)));
     return array('activity' => $activity);
 }
Beispiel #20
0
 /**
  * Form submission of new/edit contact is processed.
  */
 public function postProcess()
 {
     // check if dedupe button, if so return.
     $buttonName = $this->controller->getButtonName();
     if ($buttonName == $this->_dedupeButtonName) {
         return;
     }
     //get the submitted values in an array
     $params = $this->controller->exportValues($this->_name);
     $group = CRM_Utils_Array::value('group', $params);
     if (!empty($group) && is_array($group)) {
         unset($params['group']);
         foreach ($group as $key => $value) {
             $params['group'][$value] = 1;
         }
     }
     CRM_Contact_BAO_Contact_Optimizer::edit($params, $this->_preEditValues);
     if (!empty($params['image_URL'])) {
         CRM_Contact_BAO_Contact::processImageParams($params);
     }
     if (is_numeric(CRM_Utils_Array::value('current_employer_id', $params)) && !empty($params['current_employer'])) {
         $params['current_employer'] = $params['current_employer_id'];
     }
     // don't carry current_employer_id field,
     // since we don't want to directly update DAO object without
     // handling related business logic ( eg related membership )
     if (isset($params['current_employer_id'])) {
         unset($params['current_employer_id']);
     }
     $params['contact_type'] = $this->_contactType;
     if (empty($params['contact_sub_type']) && $this->_isContactSubType) {
         $params['contact_sub_type'] = array($this->_contactSubType);
     }
     if ($this->_contactId) {
         $params['contact_id'] = $this->_contactId;
     }
     //make deceased date null when is_deceased = false
     if ($this->_contactType == 'Individual' && !empty($this->_editOptions['Demographics']) && empty($params['is_deceased'])) {
         $params['is_deceased'] = FALSE;
         $params['deceased_date'] = NULL;
     }
     if (isset($params['contact_id'])) {
         // process membership status for deceased contact
         $deceasedParams = array('contact_id' => CRM_Utils_Array::value('contact_id', $params), 'is_deceased' => CRM_Utils_Array::value('is_deceased', $params, FALSE), 'deceased_date' => CRM_Utils_Array::value('deceased_date', $params, NULL));
         $updateMembershipMsg = $this->updateMembershipStatus($deceasedParams);
     }
     // action is taken depending upon the mode
     if ($this->_action & CRM_Core_Action::UPDATE) {
         CRM_Utils_Hook::pre('edit', $params['contact_type'], $params['contact_id'], $params);
     } else {
         CRM_Utils_Hook::pre('create', $params['contact_type'], NULL, $params);
     }
     $customFields = CRM_Core_BAO_CustomField::getFields($params['contact_type'], FALSE, TRUE);
     //CRM-5143
     //if subtype is set, send subtype as extend to validate subtype customfield
     $customFieldExtends = CRM_Utils_Array::value('contact_sub_type', $params) ? $params['contact_sub_type'] : $params['contact_type'];
     $params['custom'] = CRM_Core_BAO_CustomField::postProcess($params, $this->_contactId, $customFieldExtends, TRUE);
     if ($this->_contactId && !empty($this->_oldSubtypes)) {
         CRM_Contact_BAO_ContactType::deleteCustomSetForSubtypeMigration($this->_contactId, $params['contact_type'], $this->_oldSubtypes, $params['contact_sub_type']);
     }
     if (array_key_exists('CommunicationPreferences', $this->_editOptions)) {
         // this is a chekbox, so mark false if we dont get a POST value
         $params['is_opt_out'] = CRM_Utils_Array::value('is_opt_out', $params, FALSE);
     }
     // process shared contact address.
     CRM_Contact_BAO_Contact_Utils::processSharedAddress($params['address']);
     if (!array_key_exists('TagsAndGroups', $this->_editOptions) && !empty($params['group'])) {
         unset($params['group']);
     }
     if (!empty($params['contact_id']) && $this->_action & CRM_Core_Action::UPDATE && !empty($params['group'])) {
         // figure out which all groups are intended to be removed
         $contactGroupList = CRM_Contact_BAO_GroupContact::getContactGroup($params['contact_id'], 'Added');
         if (is_array($contactGroupList)) {
             foreach ($contactGroupList as $key) {
                 if ((!array_key_exists($key['group_id'], $params['group']) || $params['group'][$key['group_id']] != 1) && empty($key['is_hidden'])) {
                     $params['group'][$key['group_id']] = -1;
                 }
             }
         }
     }
     // parse street address, CRM-5450
     $parseStatusMsg = NULL;
     if ($this->_parseStreetAddress) {
         $parseResult = self::parseAddress($params);
         $parseStatusMsg = self::parseAddressStatusMsg($parseResult);
     }
     // Allow un-setting of location info, CRM-5969
     $params['updateBlankLocInfo'] = TRUE;
     $contact = CRM_Contact_BAO_Contact::create($params, TRUE, FALSE, TRUE);
     // status message
     if ($this->_contactId) {
         $message = ts('%1 has been updated.', array(1 => $contact->display_name));
     } else {
         $message = ts('%1 has been created.', array(1 => $contact->display_name));
     }
     // set the contact ID
     $this->_contactId = $contact->id;
     if (array_key_exists('TagsAndGroups', $this->_editOptions)) {
         //add contact to tags
         CRM_Core_BAO_EntityTag::create($params['tag'], 'civicrm_contact', $params['contact_id']);
         //save free tags
         if (isset($params['contact_taglist']) && !empty($params['contact_taglist'])) {
             CRM_Core_Form_Tag::postProcess($params['contact_taglist'], $params['contact_id'], 'civicrm_contact', $this);
         }
     }
     if (!empty($parseStatusMsg)) {
         $message .= "<br />{$parseStatusMsg}";
     }
     if (!empty($updateMembershipMsg)) {
         $message .= "<br />{$updateMembershipMsg}";
     }
     $session = CRM_Core_Session::singleton();
     $session->setStatus($message, ts('Contact Saved'), 'success');
     // add the recently viewed contact
     $recentOther = array();
     if ($session->get('userID') == $contact->id || CRM_Contact_BAO_Contact_Permission::allow($contact->id, CRM_Core_Permission::EDIT)) {
         $recentOther['editUrl'] = CRM_Utils_System::url('civicrm/contact/add', 'reset=1&action=update&cid=' . $contact->id);
     }
     if ($session->get('userID') != $this->_contactId && CRM_Core_Permission::check('delete contacts')) {
         $recentOther['deleteUrl'] = CRM_Utils_System::url('civicrm/contact/view/delete', 'reset=1&delete=1&cid=' . $contact->id);
     }
     CRM_Utils_Recent::add($contact->display_name, CRM_Utils_System::url('civicrm/contact/view', 'reset=1&cid=' . $contact->id), $contact->id, $this->_contactType, $contact->id, $contact->display_name, $recentOther);
     // here we replace the user context with the url to view this contact
     $buttonName = $this->controller->getButtonName();
     if ($buttonName == $this->getButtonName('upload', 'new')) {
         $contactSubTypes = array_filter(explode(CRM_Core_DAO::VALUE_SEPARATOR, $this->_contactSubType));
         $resetStr = "reset=1&ct={$contact->contact_type}";
         $resetStr .= count($contactSubTypes) == 1 ? "&cst=" . array_pop($contactSubTypes) : '';
         $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/add', $resetStr));
     } else {
         $context = CRM_Utils_Request::retrieve('context', 'String', $this);
         $qfKey = CRM_Utils_Request::retrieve('key', 'String', $this);
         //validate the qfKey
         $urlParams = 'reset=1&cid=' . $contact->id;
         if ($context) {
             $urlParams .= "&context={$context}";
         }
         if (CRM_Utils_Rule::qfKey($qfKey)) {
             $urlParams .= "&key={$qfKey}";
         }
         $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view', $urlParams));
     }
     // now invoke the post hook
     if ($this->_action & CRM_Core_Action::UPDATE) {
         CRM_Utils_Hook::post('edit', $params['contact_type'], $contact->id, $contact);
     } else {
         CRM_Utils_Hook::post('create', $params['contact_type'], $contact->id, $contact);
     }
 }
Beispiel #21
0
 /**
  * Form submission of new/edit contact is processed.
  *
  * @access public
  * @return None
  */
 public function postProcess()
 {
     // check if dedupe button, if so return.
     $buttonName = $this->controller->getButtonName();
     if ($buttonName == $this->_dedupeButtonName) {
         return;
     }
     //get the submitted values in an array
     $params = $this->controller->exportValues($this->_name);
     if (CRM_Utils_Array::value('image_URL', $params)) {
         CRM_Contact_BAO_Contact::processImageParams($params);
     }
     if (is_numeric(CRM_Utils_Array::value('current_employer_id', $params)) && CRM_Utils_Array::value('current_employer', $params)) {
         $params['current_employer'] = $params['current_employer_id'];
     }
     // don't carry current_employer_id field,
     // since we don't want to directly update DAO object without
     // handling related business logic ( eg related membership )
     if (isset($params['current_employer_id'])) {
         unset($params['current_employer_id']);
     }
     $params['contact_type'] = $this->_contactType;
     if ($this->_contactSubType && !CRM_Utils_Array::value('contact_sub_type', $params)) {
         $params['contact_sub_type'] = $this->_contactSubType;
     }
     if ($this->_contactId) {
         $params['contact_id'] = $this->_contactId;
     }
     //make deceased date null when is_deceased = false
     if ($this->_contactType == 'Individual' && CRM_Utils_Array::value('Demographics', $this->_editOptions) && !CRM_Utils_Array::value('is_deceased', $params)) {
         $params['is_deceased'] = false;
         $params['deceased_date'] = null;
     }
     // process membership status for deceased contact
     $deceasedParams = array('contact_id' => $params['contact_id'], 'is_deceased' => $params['is_deceased'], 'deceased_date' => $params['deceased_date']);
     $updateMembershipMsg = $this->updateMembershipStatus($deceasedParams);
     // action is taken depending upon the mode
     require_once 'CRM/Utils/Hook.php';
     if ($this->_action & CRM_Core_Action::UPDATE) {
         CRM_Utils_Hook::pre('edit', $params['contact_type'], $params['contact_id'], $params);
     } else {
         CRM_Utils_Hook::pre('create', $params['contact_type'], null, $params);
     }
     require_once 'CRM/Core/BAO/CustomField.php';
     $customFields = CRM_Core_BAO_CustomField::getFields($params['contact_type'], false, true);
     //CRM-5143
     //if subtype is set, send subtype as extend to validate subtype customfield
     $customFieldExtends = CRM_Utils_Array::value('contact_sub_type', $params) ? $params['contact_sub_type'] : $params['contact_type'];
     $params['custom'] = CRM_Core_BAO_CustomField::postProcess($params, $customFields, $this->_contactId, $customFieldExtends, true);
     if (array_key_exists('CommunicationPreferences', $this->_editOptions)) {
         // this is a chekbox, so mark false if we dont get a POST value
         $params['is_opt_out'] = CRM_Utils_Array::value('is_opt_out', $params, false);
     }
     // process shared contact address.
     require_once 'CRM/Contact/BAO/Contact/Utils.php';
     CRM_Contact_BAO_Contact_Utils::processSharedAddress($params['address']);
     if (!array_key_exists('TagsAndGroups', $this->_editOptions)) {
         unset($params['group']);
     }
     if (CRM_Utils_Array::value('contact_id', $params) && $this->_action & CRM_Core_Action::UPDATE) {
         // figure out which all groups are intended to be removed
         if (!empty($params['group'])) {
             $contactGroupList =& CRM_Contact_BAO_GroupContact::getContactGroup($params['contact_id'], 'Added');
             if (is_array($contactGroupList)) {
                 foreach ($contactGroupList as $key) {
                     if ($params['group'][$key['group_id']] != 1) {
                         $params['group'][$key['group_id']] = -1;
                     }
                 }
             }
         }
     }
     // parse street address, CRM-5450
     $parseStatusMsg = null;
     if ($this->_parseStreetAddress) {
         $parseResult = $this->parseAddress($params);
         $parseStatusMsg = $this->parseAddressStatusMsg($parseResult);
     }
     // Allow un-setting of location info, CRM-5969
     $params['updateBlankLocInfo'] = true;
     require_once 'CRM/Contact/BAO/Contact.php';
     $contact =& CRM_Contact_BAO_Contact::create($params, true, false, true);
     // set the contact ID
     $this->_contactId = $contact->id;
     if (array_key_exists('TagsAndGroups', $this->_editOptions)) {
         //add contact to tags
         require_once 'CRM/Core/BAO/EntityTag.php';
         CRM_Core_BAO_EntityTag::create($params['tag'], 'civicrm_contact', $params['contact_id']);
         //save free tags
         if (isset($params['taglist']) && !empty($params['taglist'])) {
             require_once 'CRM/Core/Form/Tag.php';
             CRM_Core_Form_Tag::postProcess($params['taglist'], $params['contact_id'], 'civicrm_contact', $this);
         }
     }
     $statusMsg = ts('Your %1 contact record has been saved.', array(1 => $contact->contact_type_display));
     if ($parseStatusMsg) {
         $statusMsg = "{$statusMsg} <br > {$parseStatusMsg}";
     }
     if ($uploadFailMsg) {
         $statusMsg = "{$statusMsg} <br > {$uploadFailMsg}";
     }
     if ($updateMembershipMsg) {
         $statusMsg = "{$statusMsg} <br > {$updateMembershipMsg}";
     }
     $session = CRM_Core_Session::singleton();
     CRM_Core_Session::setStatus($statusMsg);
     require_once 'CRM/Utils/Recent.php';
     // add the recently viewed contact
     $displayName = CRM_Contact_BAO_Contact::displayName($contact->id);
     require_once 'CRM/Contact/BAO/Contact/Permission.php';
     $recentOther = array();
     if ($session->get('userID') == $contact->id || CRM_Contact_BAO_Contact_Permission::allow($contact->id, CRM_Core_Permission::EDIT)) {
         $recentOther['editUrl'] = CRM_Utils_System::url('civicrm/contact/add', 'reset=1&action=update&cid=' . $contact->id);
     }
     if ($session->get('userID') != $this->_contactId && CRM_Core_Permission::check('delete contacts')) {
         $recentOther['deleteUrl'] = CRM_Utils_System::url('civicrm/contact/view/delete', 'reset=1&delete=1&cid=' . $contact->id);
     }
     CRM_Utils_Recent::add($displayName, CRM_Utils_System::url('civicrm/contact/view', 'reset=1&cid=' . $contact->id), $contact->id, $this->_contactType, $contact->id, $displayName, $recentOther);
     // here we replace the user context with the url to view this contact
     $buttonName = $this->controller->getButtonName();
     if ($buttonName == $this->getButtonName('upload', 'new')) {
         $resetStr = "reset=1&ct={$contact->contact_type}";
         $resetStr .= $this->_contactSubType ? "&cst={$this->_contactSubType}" : '';
         $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/add', $resetStr));
     } else {
         $context = CRM_Utils_Request::retrieve('context', 'String', $this);
         $qfKey = CRM_Utils_Request::retrieve('key', 'String', $this);
         //validate the qfKey
         require_once 'CRM/Utils/Rule.php';
         $urlParams = 'reset=1&cid=' . $contact->id;
         if ($context) {
             $urlParams .= "&context={$context}";
         }
         if (CRM_Utils_Rule::qfKey($qfKey)) {
             $urlParams .= "&key={$qfKey}";
         }
         $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view', $urlParams));
     }
     // now invoke the post hook
     if ($this->_action & CRM_Core_Action::UPDATE) {
         CRM_Utils_Hook::post('edit', $params['contact_type'], $contact->id, $contact);
     } else {
         CRM_Utils_Hook::post('create', $params['contact_type'], $contact->id, $contact);
     }
 }