3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2013
36 * This class contains all the function that are called using AJAX (jQuery)
38 class CRM_Activity_Page_AJAX
{
39 static function getCaseActivity() {
40 $caseID = CRM_Utils_Type
::escape($_GET['caseID'], 'Integer');
41 $contactID = CRM_Utils_Type
::escape($_GET['cid'], 'Integer');
42 $userID = CRM_Utils_Type
::escape($_GET['userID'], 'Integer');
43 $context = CRM_Utils_Type
::escape(CRM_Utils_Array
::value('context', $_GET), 'String');
46 0 => 'display_date', 1 => 'ca.subject', 2 => 'ca.activity_type_id',
47 3 => 'acc.sort_name', 4 => 'cc.sort_name', 5 => 'ca.status_id',
50 $sEcho = CRM_Utils_Type
::escape($_REQUEST['sEcho'], 'Integer');
51 $offset = isset($_REQUEST['iDisplayStart']) ? CRM_Utils_Type
::escape($_REQUEST['iDisplayStart'], 'Integer') : 0;
52 $rowCount = isset($_REQUEST['iDisplayLength']) ? CRM_Utils_Type
::escape($_REQUEST['iDisplayLength'], 'Integer') : 25;
53 $sort = isset($_REQUEST['iSortCol_0']) ? CRM_Utils_Array
::value(CRM_Utils_Type
::escape($_REQUEST['iSortCol_0'], 'Integer'), $sortMapper) : NULL;
54 $sortOrder = isset($_REQUEST['sSortDir_0']) ? CRM_Utils_Type
::escape($_REQUEST['sSortDir_0'], 'String') : 'asc';
57 if ($sort && $sortOrder) {
58 $params['sortname'] = $sort;
59 $params['sortorder'] = $sortOrder;
61 $params['page'] = ($offset / $rowCount) +
1;
62 $params['rp'] = $rowCount;
64 // get the activities related to given case
65 $activities = CRM_Case_BAO_Case
::getCaseActivity($caseID, $params, $contactID, $context, $userID);
67 $iFilteredTotal = $iTotal = $params['total'];
68 $selectorElements = array('display_date', 'subject', 'type', 'with_contacts', 'reporter', 'status', 'links', 'class');
70 echo CRM_Utils_JSON
::encodeDataTableSelector($activities, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
71 CRM_Utils_System
::civiExit();
74 static function getCaseGlobalRelationships() {
76 0 => 'sort_name', 1 => 'phone', 2 => 'email',
79 $sEcho = CRM_Utils_Type
::escape($_REQUEST['sEcho'], 'Integer');
80 $offset = isset($_REQUEST['iDisplayStart']) ? CRM_Utils_Type
::escape($_REQUEST['iDisplayStart'], 'Integer') : 0;
81 $rowCount = isset($_REQUEST['iDisplayLength']) ? CRM_Utils_Type
::escape($_REQUEST['iDisplayLength'], 'Integer') : 25;
82 $sort = isset($_REQUEST['iSortCol_0']) ? CRM_Utils_Array
::value(CRM_Utils_Type
::escape($_REQUEST['iSortCol_0'], 'Integer'), $sortMapper) : NULL;
83 $sortOrder = isset($_REQUEST['sSortDir_0']) ? CRM_Utils_Type
::escape($_REQUEST['sSortDir_0'], 'String') : 'asc';
86 if ($sort && $sortOrder) {
87 $sortSQL = $sort .' '.$sortOrder;
90 // get the activities related to given case
91 $globalGroupInfo = array();
93 // get the total row count
94 $relGlobalTotalCount = CRM_Case_BAO_Case
::getGlobalContacts($globalGroupInfo, NULL, FALSE, TRUE, NULL, NULL);
96 $relGlobal = CRM_Case_BAO_Case
::getGlobalContacts($globalGroupInfo, $sortSQL, $showLinks = TRUE, FALSE, $offset, $rowCount);
98 $iFilteredTotal = $iTotal = $relGlobalTotalCount;
99 $selectorElements = array('sort_name', 'phone', 'email');
101 echo CRM_Utils_JSON
::encodeDataTableSelector($relGlobal, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
102 CRM_Utils_System
::civiExit();
105 static function getCaseClientRelationships() {
106 $caseID = CRM_Utils_Type
::escape($_GET['caseID'], 'Integer');
107 $contactID = CRM_Utils_Type
::escape($_GET['cid'], 'Integer');
110 0 => 'relation', 1 => 'name', 2 => 'phone', 3 => 'email'
113 $sEcho = CRM_Utils_Type
::escape($_REQUEST['sEcho'], 'Integer');
114 $offset = isset($_REQUEST['iDisplayStart']) ? CRM_Utils_Type
::escape($_REQUEST['iDisplayStart'], 'Integer') : 0;
115 $rowCount = isset($_REQUEST['iDisplayLength']) ? CRM_Utils_Type
::escape($_REQUEST['iDisplayLength'], 'Integer') : 25;
116 $sort = isset($_REQUEST['iSortCol_0']) ? CRM_Utils_Array
::value(CRM_Utils_Type
::escape($_REQUEST['iSortCol_0'], 'Integer'), $sortMapper) : 'relation';
117 $sortOrder = isset($_REQUEST['sSortDir_0']) ? CRM_Utils_Type
::escape($_REQUEST['sSortDir_0'], 'String') : 'asc';
120 if ($sort && $sortOrder) {
121 $sortSQL = $sort .' '.$sortOrder;
124 // Retrieve ALL client relationships
125 $relClient = CRM_Contact_BAO_Relationship
::getRelationship($contactID,
126 CRM_Contact_BAO_Relationship
::CURRENT
,
127 0, 0, 0, NULL, NULL, FALSE
130 $caseRelationships = CRM_Case_BAO_Case
::getCaseRoles($contactID, $caseID);
132 // Now build 'Other Relationships' array by removing relationships that are already listed under Case Roles
133 // so they don't show up twice.
134 $clientRelationships = array();
135 foreach ($relClient as $r) {
136 if (!array_key_exists($r['id'], $caseRelationships)) {
137 $clientRelationships[] = $r;
141 // sort clientRelationships array using jquery call params
142 foreach ($clientRelationships as $key => $row) {
143 $sortArray[$key] = $row[$sort];
145 $sort_type = "SORT_" . strtoupper($sortOrder);
146 array_multisort($sortArray, constant($sort_type), $clientRelationships);
149 $allClientRelationships = $clientRelationships;
150 $clientRelationships = array_slice($allClientRelationships, $offset, $rowCount, TRUE);
152 // after sort we can update username fields to be a url
153 foreach($clientRelationships as $key => $value) {
154 $clientRelationships[$key]['name'] = '<a href='.CRM_Utils_System
::url('civicrm/contact/view',
155 'action=view&reset=1&cid='.$clientRelationships[$key]['cid']).'>'.$clientRelationships[$key]['name'].'</a>';
158 $iFilteredTotal = $iTotal = $params['total'] = count($allClientRelationships);
159 $selectorElements = array('relation', 'name', 'phone', 'email');
161 echo CRM_Utils_JSON
::encodeDataTableSelector($clientRelationships, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
162 CRM_Utils_System
::civiExit();
166 static function getCaseRoles() {
167 $caseID = CRM_Utils_Type
::escape($_GET['caseID'], 'Integer');
168 $contactID = CRM_Utils_Type
::escape($_GET['cid'], 'Integer');
171 0 => 'relation', 1 => 'name', 2 => 'phone', 3 => 'email', 4 => 'actions'
174 $sEcho = CRM_Utils_Type
::escape($_REQUEST['sEcho'], 'Integer');
175 $offset = isset($_REQUEST['iDisplayStart']) ? CRM_Utils_Type
::escape($_REQUEST['iDisplayStart'], 'Integer') : 0;
176 $rowCount = isset($_REQUEST['iDisplayLength']) ? CRM_Utils_Type
::escape($_REQUEST['iDisplayLength'], 'Integer') : 25;
177 $sort = isset($_REQUEST['iSortCol_0']) ? CRM_Utils_Array
::value(CRM_Utils_Type
::escape($_REQUEST['iSortCol_0'], 'Integer'), $sortMapper) : 'relation';
178 $sortOrder = isset($_REQUEST['sSortDir_0']) ? CRM_Utils_Type
::escape($_REQUEST['sSortDir_0'], 'String') : 'asc';
181 if ($sort && $sortOrder) {
182 $sortSQL = $sort .' '.$sortOrder;
185 $caseRelationships = CRM_Case_BAO_Case
::getCaseRoles($contactID, $caseID);
186 $caseTypeName = CRM_Case_BAO_Case
::getCaseType($caseID, 'name');
187 $xmlProcessor = new CRM_Case_XMLProcessor_Process();
188 $caseRoles = $xmlProcessor->get($caseTypeName, 'CaseRoles');
190 $hasAccessToAllCases = CRM_Core_Permission
::check('access all cases and activities');
192 $managerRoleId = $xmlProcessor->getCaseManagerRoleId($caseTypeName);
193 if (!empty($managerRoleId)) {
194 $caseRoles[$managerRoleId] = $caseRoles[$managerRoleId] . '<br />' . '(' . ts('Case Manager') . ')';
197 foreach ($caseRelationships as $key => $value) {
198 //calculate roles that don't have relationships
199 if (CRM_Utils_Array
::value($value['relation_type'], $caseRoles)) {
200 //keep naming from careRoles array
201 $caseRelationships[$key]['relation'] = $caseRoles[$value['relation_type']];
202 unset($caseRoles[$value['relation_type']]);
204 // mark orginal case relationships record to use on setting edit links below
205 $caseRelationships[$key]['source'] = 'caseRel';
208 $caseRoles['client'] = CRM_Case_BAO_Case
::getContactNames($caseID);
210 // move/transform caseRoles array data to caseRelationships
211 // for sorting and display
212 foreach($caseRoles as $id => $value) {
213 if ($id != "client") {
215 $rel['relation'] = $value;
216 $rel['relation_type'] = $id;
217 $rel['name'] = '(not assigned)';
220 $rel['source'] = 'caseRoles';
221 $caseRelationships[] = $rel;
223 foreach($value as $clientRole) {
224 $relClient = array();
225 $relClient['relation'] = 'Client';
226 $relClient['name'] = $clientRole['sort_name'];
227 $relClient['phone'] = $clientRole['phone'];
228 $relClient['email'] = $clientRole['email'];
229 $relClient['cid'] = $clientRole['contact_id'];
230 $relClient['source'] = 'contact';
231 $caseRelationships[] = $relClient;
236 // sort clientRelationships array using jquery call params
237 foreach ($caseRelationships as $key => $row) {
238 $sortArray[$key] = $row[$sort];
241 $sort_type = "SORT_" . strtoupper($sortOrder);
242 array_multisort($sortArray, constant($sort_type), $caseRelationships);
245 $allCaseRelationships = $caseRelationships;
246 $caseRelationships = array_slice($allCaseRelationships, $offset, $rowCount, TRUE);
248 // set user name, email and edit columns links
249 // idx will count number of current row / needed by edit links
251 foreach ($caseRelationships as $key => $row) {
253 if ($caseRelationships[$key]['cid']) {
254 $caseRelationships[$key]['name'] = '<a href='.CRM_Utils_System
::url('civicrm/contact/view',
255 'action=view&reset=1&cid='.$caseRelationships[$key]['cid']).'>'.$caseRelationships[$key]['name'].'</a>';
257 // email column links/icon
258 if ($caseRelationships[$key]['email']) {
259 $caseRelationships[$key]['email'] = '<a href="'.CRM_Utils_System
::url('civicrm/contact/view/activity', 'action=reset=1&action=add&atype=3&cid='.$caseRelationships[$key]['cid']).'" title="compose and send an email"><div class="icon email-icon" title="compose and send an email"></div>
263 if ($hasAccessToAllCases) {
264 switch($caseRelationships[$key]['source']){
266 $caseRelationships[$key]['actions'] =
267 '<a href="#" title="edit case role" onclick="createRelationship( '.$caseRelationships[$key]['relation_type'].', '.$caseRelationships[$key]['cid'].', '.$caseRelationships[$key]['rel_id'].', '.$idx.', \''.$caseRelationships[$key]['relation'].'\' );return false;"><div class="icon edit-icon" ></div></a> <a href="#" class="case-role-delete" case_id="'.$caseID.'" rel_type="'.$caseRelationships[$key]['relation_type'].'"><div class="icon delete-icon" title="remove contact from case role"></div></a>';
271 $caseRelationships[$key]['actions'] =
272 '<a href="#" title="edit case role" onclick="createRelationship('.$caseRelationships[$key]['relation_type'].', null, null, '.$idx.', \''.$caseRelationships[$key]['relation'].'\');return false;"><div class="icon edit-icon"></div></a>';
276 $caseRelationships[$key]['actions'] = '';
280 $iFilteredTotal = $iTotal = $params['total'] = count($allCaseRelationships);
281 $selectorElements = array('relation', 'name', 'phone', 'email', 'actions');
283 echo CRM_Utils_JSON
::encodeDataTableSelector($caseRelationships, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
284 CRM_Utils_System
::civiExit();
287 static function convertToCaseActivity() {
288 $params = array('caseID', 'activityID', 'contactID', 'newSubject', 'targetContactIds', 'mode');
289 foreach ($params as $param) {
290 $vals[$param] = CRM_Utils_Array
::value($param, $_POST);
293 $retval = self
::_convertToCaseActivity($vals);
295 echo json_encode($retval);
296 CRM_Utils_System
::civiExit();
299 static function _convertToCaseActivity($params) {
300 if (!$params['activityID'] ||
!$params['caseID']) {
301 return (array('error_msg' => 'required params missing.'));
304 $otherActivity = new CRM_Activity_DAO_Activity();
305 $otherActivity->id
= $params['activityID'];
306 if (!$otherActivity->find(TRUE)) {
307 return (array('error_msg' => 'activity record is missing.'));
309 $actDateTime = CRM_Utils_Date
::isoToMysql($otherActivity->activity_date_time
);
311 //create new activity record.
312 $mainActivity = new CRM_Activity_DAO_Activity();
313 $mainActVals = array();
314 CRM_Core_DAO
::storeValues($otherActivity, $mainActVals);
316 //get new activity subject.
317 if (!empty($params['newSubject'])) {
318 $mainActVals['subject'] = $params['newSubject'];
321 $mainActivity->copyValues($mainActVals);
322 $mainActivity->id
= NULL;
323 $mainActivity->activity_date_time
= $actDateTime;
324 //make sure this is current revision.
325 $mainActivity->is_current_revision
= TRUE;
326 //drop all relations.
327 $mainActivity->parent_id
= $mainActivity->original_id
= NULL;
329 $mainActivity->save();
330 $mainActivityId = $mainActivity->id
;
331 CRM_Activity_BAO_Activity
::logActivityAction($mainActivity);
332 $mainActivity->free();
334 /* Mark previous activity as deleted. If it was a non-case activity
335 * then just change the subject.
338 if (in_array($params['mode'], array(
340 $caseActivity = new CRM_Case_DAO_CaseActivity();
341 $caseActivity->case_id
= $params['caseID'];
342 $caseActivity->activity_id
= $otherActivity->id
;
343 if ($params['mode'] == 'move' ||
$caseActivity->find(TRUE)) {
344 $otherActivity->is_deleted
= 1;
347 $otherActivity->subject
= ts('(Filed on case %1)', array(
348 1 => $params['caseID'])) . ' ' . $otherActivity->subject
;
350 $otherActivity->activity_date_time
= $actDateTime;
351 $otherActivity->save();
353 $caseActivity->free();
355 $otherActivity->free();
357 $targetContacts = array();
358 if (!empty($params['targetContactIds'])) {
359 $targetContacts = array_unique(explode(',', $params['targetContactIds']));
362 $activityContacts = CRM_Core_OptionGroup
::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
363 $sourceID = CRM_Utils_Array
::key('Activity Source', $activityContacts);
364 $assigneeID = CRM_Utils_Array
::key('Activity Assignees', $activityContacts);
365 $targetID = CRM_Utils_Array
::key('Activity Targets', $activityContacts);
367 foreach ($targetContacts as $key => $value) {
368 $targ_params = array(
369 'activity_id' => $mainActivityId,
370 'contact_id' => $value,
371 'record_type_id' => $targetID
373 CRM_Activity_BAO_ActivityContact
::create($targ_params);
376 // typically this will be empty, since assignees on another case may be completely different
377 $assigneeContacts = array();
378 if (!empty($params['assigneeContactIds'])) {
379 $assigneeContacts = array_unique(explode(',', $params['assigneeContactIds']));
381 foreach ($assigneeContacts as $key => $value) {
382 $assigneeParams = array(
383 'activity_id' => $mainActivityId,
384 'contact_id' => $value,
385 'record_type_id' => $assigneeID
387 CRM_Activity_BAO_ActivityContact
::create($assigneeParams);
390 //attach newly created activity to case.
391 $caseActivity = new CRM_Case_DAO_CaseActivity();
392 $caseActivity->case_id
= $params['caseID'];
393 $caseActivity->activity_id
= $mainActivityId;
394 $caseActivity->save();
395 $error_msg = $caseActivity->_lastError
;
396 $caseActivity->free();
398 $params['mainActivityId'] = $mainActivityId;
399 CRM_Activity_BAO_Activity
::copyExtendedActivityData($params);
401 return (array('error_msg' => $error_msg, 'newId' => $mainActivity->id
));
404 static function getContactActivity() {
405 $contactID = CRM_Utils_Type
::escape($_POST['contact_id'], 'Integer');
406 $context = CRM_Utils_Type
::escape(CRM_Utils_Array
::value('context', $_GET), 'String');
409 0 => 'activity_type', 1 => 'subject', 2 => 'source_contact_name',
410 3 => '', 4 => 'activity_date_time', 5 => 'status_id',
413 $sEcho = CRM_Utils_Type
::escape($_REQUEST['sEcho'], 'Integer');
414 $offset = isset($_REQUEST['iDisplayStart']) ? CRM_Utils_Type
::escape($_REQUEST['iDisplayStart'], 'Integer') : 0;
415 $rowCount = isset($_REQUEST['iDisplayLength']) ? CRM_Utils_Type
::escape($_REQUEST['iDisplayLength'], 'Integer') : 25;
416 $sort = isset($_REQUEST['iSortCol_0']) ? CRM_Utils_Array
::value(CRM_Utils_Type
::escape($_REQUEST['iSortCol_0'], 'Integer'), $sortMapper) : NULL;
417 $sortOrder = isset($_REQUEST['sSortDir_0']) ? CRM_Utils_Type
::escape($_REQUEST['sSortDir_0'], 'String') : 'asc';
420 if ($sort && $sortOrder) {
421 $params['sortBy'] = $sort . ' ' . $sortOrder;
424 $params['page'] = ($offset / $rowCount) +
1;
425 $params['rp'] = $rowCount;
427 $params['contact_id'] = $contactID;
428 $params['context'] = $context;
430 // get the contact activities
431 $activities = CRM_Activity_BAO_Activity
::getContactActivitySelector($params);
433 // store the activity filter preference CRM-11761
434 $session = CRM_Core_Session
::singleton();
435 $userID = $session->get('userID');
438 array('activity_type_filter_id' => CRM_Utils_Array
::value('activity_type_id', $params),
439 'activity_type_exclude_filter_id' => CRM_Utils_Array
::value('activity_type_exclude_id', $params));
440 CRM_Core_BAO_Setting
::setItem($activityFilter,
441 CRM_Core_BAO_Setting
::PERSONAL_PREFERENCES_NAME
,
442 'activity_tab_filter',
449 $iFilteredTotal = $iTotal = $params['total'];
450 $selectorElements = array(
451 'activity_type', 'subject', 'source_contact',
452 'target_contact', 'assignee_contact',
453 'activity_date', 'status','links', 'class',
456 echo CRM_Utils_JSON
::encodeDataTableSelector($activities, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
457 CRM_Utils_System
::civiExit();