Beispiel #1
0
 /**
  * Function to process the activities
  *
  * @param object $form         form object
  * @param array  $params       associated array of the submitted values
  * @param array  $ids          array of ids
  * @param string $activityType activity Type
  * @param boolean $record   true if it is Record Activity 
  * @access public
  * @return
  */
 public function create(&$params)
 {
     // check required params
     if (!self::dataExists($params)) {
         CRM_Core_Error::fatal('Not enough data to create activity object,');
     }
     $activity =& new CRM_Activity_DAO_Activity();
     if (!CRM_Utils_Array::value('status_id', $params)) {
         if (isset($params['activity_date_time']) && strcmp($params['activity_date_time'], CRM_Utils_Date::processDate(date('Ymd')) == -1)) {
             $params['status_id'] = 2;
         } else {
             $params['status_id'] = 1;
         }
     }
     //set priority to Normal for Auto-populated activities (for Cases)
     if (!CRM_Utils_Array::value('priority_id', $params)) {
         require_once 'CRM/Core/PseudoConstant.php';
         $priority = CRM_Core_PseudoConstant::priority();
         $params['priority_id'] = array_search('Normal', $priority);
     }
     if (empty($params['id'])) {
         unset($params['id']);
     }
     if (!empty($params['target_contact_id']) && is_array($params['target_contact_id'])) {
         $params['target_contact_id'] = array_unique($params['target_contact_id']);
     }
     if (!empty($params['assignee_contact_id']) && is_array($params['assignee_contact_id'])) {
         $params['assignee_contact_id'] = array_unique($params['assignee_contact_id']);
     }
     $activity->copyValues($params);
     // start transaction
     require_once 'CRM/Core/Transaction.php';
     $transaction = new CRM_Core_Transaction();
     $result = $activity->save();
     if (is_a($result, 'CRM_Core_Error')) {
         $transaction->rollback();
         return $result;
     }
     $activityId = $activity->id;
     // check and attach and files as needed
     require_once 'CRM/Core/BAO/File.php';
     CRM_Core_BAO_File::processAttachment($params, 'civicrm_activity', $activityId);
     // attempt to save activity assignment
     $resultAssignment = null;
     if (CRM_Utils_Array::value('assignee_contact_id', $params)) {
         require_once 'CRM/Activity/BAO/ActivityAssignment.php';
         $assignmentParams = array('activity_id' => $activityId);
         if (is_array($params['assignee_contact_id'])) {
             if (CRM_Utils_Array::value('deleteActivityAssignment', $params, true)) {
                 // first delete existing assignments if any
                 self::deleteActivityAssignment($activityId);
             }
             foreach ($params['assignee_contact_id'] as $acID) {
                 if ($acID) {
                     $assignmentParams['assignee_contact_id'] = $acID;
                     $resultAssignment = CRM_Activity_BAO_ActivityAssignment::create($assignmentParams);
                     if (is_a($resultAssignment, 'CRM_Core_Error')) {
                         $transaction->rollback();
                         return $resultAssignment;
                     }
                 }
             }
         } else {
             $assignmentParams['assignee_contact_id'] = $params['assignee_contact_id'];
             if (CRM_Utils_Array::value('id', $params)) {
                 $assignment =& new CRM_Activity_BAO_ActivityAssignment();
                 $assignment->activity_id = $activityId;
                 $assignment->find(true);
                 if ($assignment->assignee_contact_id != $params['assignee_contact_id']) {
                     $assignmentParams['id'] = $assignment->id;
                     $resultAssignment = CRM_Activity_BAO_ActivityAssignment::create($assignmentParams);
                 }
             } else {
                 $resultAssignment = CRM_Activity_BAO_ActivityAssignment::create($assignmentParams);
             }
         }
     } else {
         if (CRM_Utils_Array::value('deleteActivityAssignment', $params, true)) {
             self::deleteActivityAssignment($activityId);
         }
     }
     if (is_a($resultAssignment, 'CRM_Core_Error')) {
         $transaction->rollback();
         return $resultAssignment;
     }
     // attempt to save activity targets
     $resultTarget = null;
     if (CRM_Utils_Array::value('target_contact_id', $params)) {
         $targetParams = array('activity_id' => $activityId);
         $resultTarget = array();
         if (is_array($params['target_contact_id'])) {
             if (CRM_Utils_Array::value('deleteActivityTarget', $params, true)) {
                 // first delete existing targets if any
                 self::deleteActivityTarget($activityId);
             }
             foreach ($params['target_contact_id'] as $tid) {
                 if ($tid) {
                     $targetParams['target_contact_id'] = $tid;
                     $resultTarget = CRM_Activity_BAO_ActivityTarget::create($targetParams);
                     if (is_a($resultTarget, 'CRM_Core_Error')) {
                         $transaction->rollback();
                         return $resultTarget;
                     }
                 }
             }
         } else {
             $targetParams['target_contact_id'] = $params['target_contact_id'];
             if (CRM_Utils_Array::value('id', $params)) {
                 $target =& new CRM_Activity_BAO_ActivityTarget();
                 $target->activity_id = $activityId;
                 $target->find(true);
                 if ($target->target_contact_id != $params['target_contact_id']) {
                     $targetParams['id'] = $target->id;
                     $resultTarget = CRM_Activity_BAO_ActivityTarget::create($targetParams);
                 }
             } else {
                 $resultTarget = CRM_Activity_BAO_ActivityTarget::create($targetParams);
             }
         }
     } else {
         if (CRM_Utils_Array::value('deleteActivityTarget', $params, true)) {
             self::deleteActivityTarget($activityId);
         }
     }
     // write to changelog before transation is committed/rolled
     // back (and prepare status to display)
     if (CRM_Utils_Array::value('id', $params)) {
         $logMsg = "Activity (id: {$result->id} ) updated with ";
     } else {
         $logMsg = "Activity created for ";
     }
     $msgs = array();
     if (isset($params['source_contact_id'])) {
         $msgs[] = "source={$params['source_contact_id']}";
     }
     if (CRM_Utils_Array::value('target_contact_id', $params)) {
         if (is_array($params['target_contact_id']) && !CRM_Utils_array::crmIsEmptyArray($params['target_contact_id'])) {
             $msgs[] = "target=" . implode(',', $params['target_contact_id']);
             // take only first target
             // will be used for recently viewed display
             $t = array_slice($params['target_contact_id'], 0, 1);
             $recentContactId = $t[0];
         } else {
             if (isset($params['target_contact_id'])) {
                 $msgs[] = "target={$params['target_contact_id']}";
                 // will be used for recently viewed display
                 $recentContactId = $params['target_contact_id'];
             }
         }
     } else {
         // at worst, take source for recently viewed display
         $recentContactId = $params['source_contact_id'];
     }
     if (isset($params['assignee_contact_id'])) {
         if (is_array($params['assignee_contact_id'])) {
             $msgs[] = "assignee=" . implode(',', $params['assignee_contact_id']);
         } else {
             $msgs[] = "assignee={$params['assignee_contact_id']}";
         }
     }
     $logMsg .= implode(', ', $msgs);
     self::logActivityAction($result, $logMsg);
     if (CRM_Utils_Array::value('custom', $params) && is_array($params['custom'])) {
         require_once 'CRM/Core/BAO/CustomValueTable.php';
         CRM_Core_BAO_CustomValueTable::store($params['custom'], 'civicrm_activity', $result->id);
     }
     $transaction->commit();
     if (!CRM_Utils_Array::value('skipRecentView', $params)) {
         require_once 'CRM/Utils/Recent.php';
         if (CRM_Utils_Array::value('case_id', $params)) {
             $caseContactID = CRM_Core_DAO::getFieldValue('CRM_Case_DAO_CaseContact', $params['case_id'], 'contact_id', 'case_id');
             $url = CRM_Utils_System::url('civicrm/case/activity/view', "reset=1&aid={$activity->id}&cid={$caseContactID}&caseID={$params['case_id']}&context=home");
         } else {
             $q = "action=view&reset=1&id={$activity->id}&atype={$activity->activity_type_id}&cid={$activity->source_contact_id}&context=home";
             if ($activity->activity_type_id != CRM_Core_OptionGroup::getValue('activity_type', 'Email', 'name')) {
                 $url = CRM_Utils_System::url('civicrm/contact/view/activity', $q);
             } else {
                 $url = CRM_Utils_System::url('civicrm/activity', $q);
             }
         }
         if (!isset($activity->parent_id)) {
             require_once 'CRM/Contact/BAO/Contact.php';
             $recentContactDisplay = CRM_Contact_BAO_Contact::displayName($recentContactId);
             // add the recently created Activity
             $activityTypes = CRM_Core_Pseudoconstant::activityType(true, true);
             $activitySubject = CRM_Core_DAO::getFieldValue('CRM_Activity_DAO_Activity', $activity->id, 'subject');
             $title = "";
             if (isset($activitySubject)) {
                 $title = $activitySubject . ' - ';
             }
             $title = $title . $recentContactDisplay . ' (' . $activityTypes[$activity->activity_type_id] . ')';
             CRM_Utils_Recent::add($title, $url, $activity->id, 'Activity', $recentContactId, $recentContactDisplay);
         }
     }
     if (CRM_Utils_Array::value('id', $params)) {
         CRM_Utils_Hook::post('edit', 'Activity', $activity->id, $activity);
     } else {
         CRM_Utils_Hook::post('create', 'Activity', $activity->id, $activity);
     }
     return $result;
 }
 /**
  * Function to process the activities
  *
  * @param object $form         form object
  * @param array  $params       associated array of the submitted values
  * @param array  $ids          array of ids
  * @param string $activityType activity Type
  * @param boolean $record   true if it is Record Activity
  * @access public
  *
  * @return
  */
 public static function create(&$params)
 {
     // check required params
     if (!self::dataExists($params)) {
         CRM_Core_Error::fatal('Not enough data to create activity object,');
     }
     $activity = new CRM_Activity_DAO_Activity();
     if (isset($params['id']) && empty($params['id'])) {
         unset($params['id']);
     }
     if (!CRM_Utils_Array::value('status_id', $params) && !CRM_Utils_Array::value('activity_status_id', $params) && !CRM_Utils_Array::value('id', $params)) {
         if (isset($params['activity_date_time']) && strcmp($params['activity_date_time'], CRM_Utils_Date::processDate(date('Ymd')) == -1)) {
             $params['status_id'] = 2;
         } else {
             $params['status_id'] = 1;
         }
     }
     //set priority to Normal for Auto-populated activities (for Cases)
     if (CRM_Utils_Array::value('priority_id', $params) === NULL && !CRM_Utils_Array::value('id', $params)) {
         $priority = CRM_Core_PseudoConstant::priority();
         $params['priority_id'] = array_search('Normal', $priority);
     }
     if (!empty($params['target_contact_id']) && is_array($params['target_contact_id'])) {
         $params['target_contact_id'] = array_unique($params['target_contact_id']);
     }
     if (!empty($params['assignee_contact_id']) && is_array($params['assignee_contact_id'])) {
         $params['assignee_contact_id'] = array_unique($params['assignee_contact_id']);
     }
     // CRM-9137
     if (CRM_Utils_Array::value('id', $params)) {
         CRM_Utils_Hook::pre('edit', 'Activity', $activity->id, $params);
     } else {
         CRM_Utils_Hook::pre('create', 'Activity', NULL, $params);
     }
     $activity->copyValues($params);
     if (isset($params['case_id'])) {
         // CRM-8708, preserve case ID even though it's not part of the SQL model
         $activity->case_id = $params['case_id'];
     } elseif (is_numeric($activity->id)) {
         // CRM-8708, preserve case ID even though it's not part of the SQL model
         $activity->case_id = CRM_Case_BAO_Case::getCaseIdByActivityId($activity->id);
     }
     // start transaction
     $transaction = new CRM_Core_Transaction();
     $result = $activity->save();
     if (is_a($result, 'CRM_Core_Error')) {
         $transaction->rollback();
         return $result;
     }
     $activityId = $activity->id;
     // check and attach and files as needed
     CRM_Core_BAO_File::processAttachment($params, 'civicrm_activity', $activityId);
     // attempt to save activity assignment
     $resultAssignment = NULL;
     if (CRM_Utils_Array::value('assignee_contact_id', $params)) {
         $assignmentParams = array('activity_id' => $activityId);
         if (is_array($params['assignee_contact_id'])) {
             if (CRM_Utils_Array::value('deleteActivityAssignment', $params, TRUE)) {
                 // first delete existing assignments if any
                 self::deleteActivityAssignment($activityId);
             }
             $values = array();
             foreach ($params['assignee_contact_id'] as $acID) {
                 if ($acID) {
                     $values[] = "( {$activityId}, {$acID} )";
                 }
             }
             while (!empty($values)) {
                 $input = array_splice($values, 0, CRM_Core_DAO::BULK_INSERT_COUNT);
                 $str = implode(',', $input);
                 $sql = "INSERT INTO civicrm_activity_assignment ( activity_id, assignee_contact_id ) VALUES {$str};";
                 CRM_Core_DAO::executeQuery($sql);
             }
         } else {
             $assignmentParams['assignee_contact_id'] = $params['assignee_contact_id'];
             if (CRM_Utils_Array::value('id', $params)) {
                 $assignment = new CRM_Activity_BAO_ActivityAssignment();
                 $assignment->activity_id = $activityId;
                 $assignment->find(TRUE);
                 if ($assignment->assignee_contact_id != $params['assignee_contact_id']) {
                     $assignmentParams['id'] = $assignment->id;
                     $resultAssignment = CRM_Activity_BAO_ActivityAssignment::create($assignmentParams);
                 }
             } else {
                 $resultAssignment = CRM_Activity_BAO_ActivityAssignment::create($assignmentParams);
             }
         }
     } else {
         if (CRM_Utils_Array::value('deleteActivityAssignment', $params, TRUE)) {
             self::deleteActivityAssignment($activityId);
         }
     }
     if (is_a($resultAssignment, 'CRM_Core_Error')) {
         $transaction->rollback();
         return $resultAssignment;
     }
     // attempt to save activity targets
     $resultTarget = NULL;
     if (CRM_Utils_Array::value('target_contact_id', $params)) {
         $targetParams = array('activity_id' => $activityId);
         $resultTarget = array();
         if (is_array($params['target_contact_id'])) {
             if (CRM_Utils_Array::value('deleteActivityTarget', $params, TRUE)) {
                 // first delete existing targets if any
                 self::deleteActivityTarget($activityId);
             }
             $values = array();
             foreach ($params['target_contact_id'] as $tid) {
                 if ($tid) {
                     $values[] = "( {$activityId}, {$tid} )";
                 }
             }
             while (!empty($values)) {
                 $input = array_splice($values, 0, CRM_Core_DAO::BULK_INSERT_COUNT);
                 $str = implode(',', $input);
                 $sql = "INSERT INTO civicrm_activity_target ( activity_id, target_contact_id ) VALUES {$str};";
                 CRM_Core_DAO::executeQuery($sql);
             }
         } else {
             $targetParams['target_contact_id'] = $params['target_contact_id'];
             if (CRM_Utils_Array::value('id', $params)) {
                 $target = new CRM_Activity_BAO_ActivityTarget();
                 $target->activity_id = $activityId;
                 $target->find(TRUE);
                 if ($target->target_contact_id != $params['target_contact_id']) {
                     $targetParams['id'] = $target->id;
                     $resultTarget = CRM_Activity_BAO_ActivityTarget::create($targetParams);
                 }
             } else {
                 $resultTarget = CRM_Activity_BAO_ActivityTarget::create($targetParams);
             }
         }
     } else {
         if (CRM_Utils_Array::value('deleteActivityTarget', $params, TRUE)) {
             self::deleteActivityTarget($activityId);
         }
     }
     // write to changelog before transation is committed/rolled
     // back (and prepare status to display)
     if (CRM_Utils_Array::value('id', $params)) {
         $logMsg = "Activity (id: {$result->id} ) updated with ";
     } else {
         $logMsg = "Activity created for ";
     }
     $msgs = array();
     if (isset($params['source_contact_id'])) {
         $msgs[] = "source={$params['source_contact_id']}";
     }
     if (CRM_Utils_Array::value('target_contact_id', $params)) {
         if (is_array($params['target_contact_id']) && !CRM_Utils_array::crmIsEmptyArray($params['target_contact_id'])) {
             $msgs[] = "target=" . implode(',', $params['target_contact_id']);
             // take only first target
             // will be used for recently viewed display
             $t = array_slice($params['target_contact_id'], 0, 1);
             $recentContactId = $t[0];
         } elseif (isset($params['target_contact_id'])) {
             $msgs[] = "target={$params['target_contact_id']}";
             // will be used for recently viewed display
             $recentContactId = $params['target_contact_id'];
         }
     } else {
         // at worst, take source for recently viewed display
         $recentContactId = CRM_Utils_Array::value('source_contact_id', $params);
     }
     if (isset($params['assignee_contact_id'])) {
         if (is_array($params['assignee_contact_id'])) {
             $msgs[] = "assignee=" . implode(',', $params['assignee_contact_id']);
         } else {
             $msgs[] = "assignee={$params['assignee_contact_id']}";
         }
     }
     $logMsg .= implode(', ', $msgs);
     self::logActivityAction($result, $logMsg);
     if (CRM_Utils_Array::value('custom', $params) && is_array($params['custom'])) {
         CRM_Core_BAO_CustomValueTable::store($params['custom'], 'civicrm_activity', $result->id);
     }
     $transaction->commit();
     if (!CRM_Utils_Array::value('skipRecentView', $params)) {
         $recentOther = array();
         if (CRM_Utils_Array::value('case_id', $params)) {
             $caseContactID = CRM_Core_DAO::getFieldValue('CRM_Case_DAO_CaseContact', $params['case_id'], 'contact_id', 'case_id');
             $url = CRM_Utils_System::url('civicrm/case/activity/view', "reset=1&aid={$activity->id}&cid={$caseContactID}&caseID={$params['case_id']}&context=home");
         } else {
             $q = "action=view&reset=1&id={$activity->id}&atype={$activity->activity_type_id}&cid={$activity->source_contact_id}&context=home";
             if ($activity->activity_type_id != CRM_Core_OptionGroup::getValue('activity_type', 'Email', 'name')) {
                 $url = CRM_Utils_System::url('civicrm/activity', $q);
                 if ($activity->activity_type_id == CRM_Core_OptionGroup::getValue('activity_type', 'Print PDF Letter', 'name')) {
                     $recentOther['editUrl'] = CRM_Utils_System::url('civicrm/activity/pdf/add', "action=update&reset=1&id={$activity->id}&atype={$activity->activity_type_id}&cid={$activity->source_contact_id}&context=home");
                 } else {
                     $recentOther['editUrl'] = CRM_Utils_System::url('civicrm/activity/add', "action=update&reset=1&id={$activity->id}&atype={$activity->activity_type_id}&cid={$activity->source_contact_id}&context=home");
                 }
                 if (CRM_Core_Permission::check("delete activities")) {
                     $recentOther['deleteUrl'] = CRM_Utils_System::url('civicrm/activity', "action=delete&reset=1&id={$activity->id}&atype={$activity->activity_type_id}&cid={$activity->source_contact_id}&context=home");
                 }
             } else {
                 $url = CRM_Utils_System::url('civicrm/activity/view', $q);
                 if (CRM_Core_Permission::check('delete activities')) {
                     $recentOther['deleteUrl'] = CRM_Utils_System::url('civicrm/activity', "action=delete&reset=1&id={$activity->id}&atype={$activity->activity_type_id}&cid={$activity->source_contact_id}&context=home");
                 }
             }
         }
         if (!isset($activity->parent_id)) {
             $recentContactDisplay = CRM_Contact_BAO_Contact::displayName($recentContactId);
             // add the recently created Activity
             $activityTypes = CRM_Core_PseudoConstant::activityType(TRUE, TRUE);
             $activitySubject = CRM_Core_DAO::getFieldValue('CRM_Activity_DAO_Activity', $activity->id, 'subject');
             $title = "";
             if (isset($activitySubject)) {
                 $title = $activitySubject . ' - ';
             }
             $title = $title . $recentContactDisplay;
             if (CRM_Utils_Array::value($activity->activity_type_id, $activityTypes)) {
                 $title .= ' (' . $activityTypes[$activity->activity_type_id] . ')';
             }
             CRM_Utils_Recent::add($title, $url, $activity->id, 'Activity', $recentContactId, $recentContactDisplay, $recentOther);
         }
     }
     // reset the group contact cache since smart groups might be affected due to this
     CRM_Contact_BAO_GroupContactCache::remove();
     if (CRM_Utils_Array::value('id', $params)) {
         CRM_Utils_Hook::post('edit', 'Activity', $activity->id, $activity);
     } else {
         CRM_Utils_Hook::post('create', 'Activity', $activity->id, $activity);
     }
     // if the subject contains a ‘[case #…]’ string, file that activity on the related case (CRM-5916)
     $matches = array();
     if (preg_match('/\\[case #([0-9a-h]{7})\\]/', CRM_Utils_Array::value('subject', $params), $matches)) {
         $key = CRM_Core_DAO::escapeString(CIVICRM_SITE_KEY);
         $hash = $matches[1];
         $query = "SELECT id FROM civicrm_case WHERE SUBSTR(SHA1(CONCAT('{$key}', id)), 1, 7) = '{$hash}'";
         $caseParams = array('activity_id' => $activity->id, 'case_id' => CRM_Core_DAO::singleValueQuery($query));
         if ($caseParams['case_id']) {
             CRM_Case_BAO_Case::processCaseActivity($caseParams);
         } else {
             self::logActivityAction($activity, "unknown case hash encountered: {$hash}");
         }
     }
     return $result;
 }