502d1232d24aed7d2270c31da545894808e9961e
[civicrm-core.git] / CRM / Activity / Page / AJAX.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2015
32 *
33 */
34
35 /**
36 * This class contains all the function that are called using AJAX (jQuery)
37 */
38 class CRM_Activity_Page_AJAX {
39 public 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');
44
45 $sortMapper = array();
46 foreach ($_GET['columns'] as $key => $value) {
47 $sortMapper[$key] = $value['data'];
48 };
49
50 $offset = isset($_GET['start']) ? CRM_Utils_Type::escape($_GET['start'], 'Integer') : 0;
51 $rowCount = isset($_GET['length']) ? CRM_Utils_Type::escape($_GET['length'], 'Integer') : 25;
52 $sort = isset($_GET['order'][0]['column']) ? CRM_Utils_Array::value(CRM_Utils_Type::escape($_GET['order'][0]['column'], 'Integer'), $sortMapper) : NULL;
53 $sortOrder = isset($_GET['order'][0]['dir']) ? CRM_Utils_Type::escape($_GET['order'][0]['dir'], 'String') : 'asc';
54
55 $params = $_GET;
56 if ($sort && $sortOrder) {
57 $params['sortBy'] = $sort . ' ' . $sortOrder;
58 }
59 $params['page'] = ($offset / $rowCount) + 1;
60 $params['rp'] = $rowCount;
61
62 // get the activities related to given case
63 $activities = CRM_Case_BAO_Case::getCaseActivity($caseID, $params, $contactID, $context, $userID);
64
65 CRM_Utils_JSON::output($activities);
66 }
67
68 public static function getCaseGlobalRelationships() {
69 $sortMapper = array();
70 foreach ($_GET['columns'] as $key => $value) {
71 $sortMapper[$key] = $value['data'];
72 };
73
74 $offset = isset($_GET['start']) ? CRM_Utils_Type::escape($_GET['start'], 'Integer') : 0;
75 $rowCount = isset($_GET['length']) ? CRM_Utils_Type::escape($_GET['length'], 'Integer') : 25;
76 $sort = isset($_GET['order'][0]['column']) ? CRM_Utils_Array::value(CRM_Utils_Type::escape($_GET['order'][0]['column'], 'Integer'), $sortMapper) : NULL;
77 $sortOrder = isset($_GET['order'][0]['dir']) ? CRM_Utils_Type::escape($_GET['order'][0]['dir'], 'String') : 'asc';
78
79 $params = $_GET;
80
81 // CRM-14466 initialize variable to avoid php notice.
82 $sortSQL = "";
83 if ($sort && $sortOrder) {
84 $sortSQL = $sort . ' ' . $sortOrder;
85 }
86
87 // get the activities related to given case
88 $globalGroupInfo = array();
89
90 // get the total row count
91 $relGlobalTotalCount = CRM_Case_BAO_Case::getGlobalContacts($globalGroupInfo, NULL, FALSE, TRUE, NULL, NULL);
92 // limit the rows
93 $relGlobal = CRM_Case_BAO_Case::getGlobalContacts($globalGroupInfo, $sortSQL, $showLinks = TRUE, FALSE, $offset, $rowCount);
94
95 $relationships = array();
96 // after sort we can update username fields to be a url
97 foreach ($relGlobal as $key => $value) {
98 $relationship = array();
99 $relationship['sort_name'] = $value['sort_name'];
100 $relationship['phone'] = $value['phone'];
101 $relationship['email'] = $value['email'];
102
103 array_push($relationships, $relationship);
104 }
105
106 $params['total'] = count($relationships);
107
108 $globalRelationshipsDT = array();
109 $globalRelationshipsDT['data'] = $relationships;
110 $globalRelationshipsDT['recordsTotal'] = $params['total'];
111 $globalRelationshipsDT['recordsFiltered'] = $params['total'];
112
113 CRM_Utils_JSON::output($globalRelationshipsDT);
114 }
115
116 public static function getCaseClientRelationships() {
117 $caseID = CRM_Utils_Type::escape($_GET['caseID'], 'Integer');
118 $contactID = CRM_Utils_Type::escape($_GET['cid'], 'Integer');
119
120 $sortMapper = array();
121 foreach ($_GET['columns'] as $key => $value) {
122 $sortMapper[$key] = $value['data'];
123 };
124
125 $offset = isset($_GET['start']) ? CRM_Utils_Type::escape($_GET['start'], 'Integer') : 0;
126 $rowCount = isset($_GET['length']) ? CRM_Utils_Type::escape($_GET['length'], 'Integer') : 25;
127 $sort = isset($_GET['order'][0]['column']) ? CRM_Utils_Array::value(CRM_Utils_Type::escape($_GET['order'][0]['column'], 'Integer'), $sortMapper) : NULL;
128 $sortOrder = isset($_GET['order'][0]['dir']) ? CRM_Utils_Type::escape($_GET['order'][0]['dir'], 'String') : 'asc';
129
130 $params = $_GET;
131
132 // Retrieve ALL client relationships
133 $relClient = CRM_Contact_BAO_Relationship::getRelationship($contactID,
134 CRM_Contact_BAO_Relationship::CURRENT,
135 0, 0, 0, NULL, NULL, FALSE
136 );
137
138 $caseRelationships = CRM_Case_BAO_Case::getCaseRoles($contactID, $caseID);
139
140 // Now build 'Other Relationships' array by removing relationships that are already listed under Case Roles
141 // so they don't show up twice.
142 $clientRelationships = array();
143 foreach ($relClient as $r) {
144 if (!array_key_exists($r['id'], $caseRelationships)) {
145 $clientRelationships[] = $r;
146 }
147 }
148
149 // sort clientRelationships array using jquery call params
150 foreach ($clientRelationships as $key => $row) {
151 $sortArray[$key] = $row[$sort];
152 }
153 $sort_type = "SORT_" . strtoupper($sortOrder);
154 array_multisort($sortArray, constant($sort_type), $clientRelationships);
155
156 $relationships = array();
157 // after sort we can update username fields to be a url
158 foreach ($clientRelationships as $key => $value) {
159 $relationship = array();
160 $relationship['relation'] = $value['relation'];
161 $relationship['name'] = '<a href=' . CRM_Utils_System::url('civicrm/contact/view',
162 'action=view&reset=1&cid=' . $clientRelationships[$key]['cid']) . '>' . $clientRelationships[$key]['name'] . '</a>';
163 $relationship['phone'] = $value['phone'];
164 $relationship['email'] = $value['email'];
165
166 array_push($relationships, $relationship);
167 }
168
169 $params['total'] = count($relationships);
170
171 $clientRelationshipsDT = array();
172 $clientRelationshipsDT['data'] = $relationships;
173 $clientRelationshipsDT['recordsTotal'] = $params['total'];
174 $clientRelationshipsDT['recordsFiltered'] = $params['total'];
175
176 CRM_Utils_JSON::output($clientRelationshipsDT);
177 }
178
179
180 public static function getCaseRoles() {
181 $caseID = CRM_Utils_Type::escape($_GET['caseID'], 'Integer');
182 $contactID = CRM_Utils_Type::escape($_GET['cid'], 'Integer');
183
184 $sortMapper = array();
185 foreach ($_GET['columns'] as $key => $value) {
186 $sortMapper[$key] = $value['data'];
187 };
188
189 $offset = isset($_GET['start']) ? CRM_Utils_Type::escape($_GET['start'], 'Integer') : 0;
190 $rowCount = isset($_GET['length']) ? CRM_Utils_Type::escape($_GET['length'], 'Integer') : 25;
191 $sort = isset($_GET['order'][0]['column']) ? CRM_Utils_Array::value(CRM_Utils_Type::escape($_GET['order'][0]['column'], 'Integer'), $sortMapper) : NULL;
192 $sortOrder = isset($_GET['order'][0]['dir']) ? CRM_Utils_Type::escape($_GET['order'][0]['dir'], 'String') : 'asc';
193
194 $params = $_GET;
195
196 $caseRelationships = CRM_Case_BAO_Case::getCaseRoles($contactID, $caseID);
197 $caseTypeName = CRM_Case_BAO_Case::getCaseType($caseID, 'name');
198 $xmlProcessor = new CRM_Case_XMLProcessor_Process();
199 $caseRoles = $xmlProcessor->get($caseTypeName, 'CaseRoles');
200
201 $hasAccessToAllCases = CRM_Core_Permission::check('access all cases and activities');
202
203 $managerRoleId = $xmlProcessor->getCaseManagerRoleId($caseTypeName);
204
205 foreach ($caseRelationships as $key => $value) {
206 // This role has been filled
207 unset($caseRoles[$value['relation_type']]);
208 // mark original case relationships record to use on setting edit links below
209 $caseRelationships[$key]['source'] = 'caseRel';
210 }
211
212 $caseRoles['client'] = CRM_Case_BAO_Case::getContactNames($caseID);
213
214 // move/transform caseRoles array data to caseRelationships
215 // for sorting and display
216 // CRM-14466 added cid to the non-client array to avoid php notice
217 foreach ($caseRoles as $id => $value) {
218 if ($id != "client") {
219 $rel = array();
220 $rel['relation'] = $value;
221 $rel['relation_type'] = $id;
222 $rel['name'] = '(not assigned)';
223 $rel['phone'] = '';
224 $rel['email'] = '';
225 $rel['source'] = 'caseRoles';
226 $caseRelationships[] = $rel;
227 }
228 else {
229 foreach ($value as $clientRole) {
230 $relClient = array();
231 $relClient['relation'] = 'Client';
232 $relClient['name'] = $clientRole['sort_name'];
233 $relClient['phone'] = $clientRole['phone'];
234 $relClient['email'] = $clientRole['email'];
235 $relClient['cid'] = $clientRole['contact_id'];
236 $relClient['source'] = 'contact';
237 $caseRelationships[] = $relClient;
238 }
239 }
240 }
241
242 // sort clientRelationships array using jquery call params
243 foreach ($caseRelationships as $key => $row) {
244 $sortArray[$key] = $row[$sort];
245 }
246 $sort_type = "SORT_" . strtoupper($sortOrder);
247 array_multisort($sortArray, constant($sort_type), $caseRelationships);
248
249 $relationships = array();
250
251 // set user name, email and edit columns links
252 foreach ($caseRelationships as $key => &$row) {
253 $typeLabel = $row['relation'];
254 // Add "<br />(Case Manager)" to label
255 if ($row['relation_type'] == $managerRoleId) {
256 $row['relation'] .= '<br />' . '(' . ts('Case Manager') . ')';
257 }
258 // view user links
259 if (!empty($row['cid'])) {
260 $row['name'] = '<a class="view-contact" title="' . ts('View Contact') . '" href=' . CRM_Utils_System::url('civicrm/contact/view',
261 'action=view&reset=1&cid=' . $row['cid']) . '>' . $row['name'] . '</a>';
262 }
263 // email column links/icon
264 if ($row['email']) {
265 $row['email'] = '<a class="crm-hover-button crm-popup" href="' . CRM_Utils_System::url('civicrm/activity/email/add', 'reset=1&action=add&atype=3&cid=' . $row['cid']) . '&caseid=' . $caseID . '" title="' . ts('Send an Email') . '"><i class="crm-i fa-envelope"></i></a>';
266 }
267 // edit links
268 $row['actions'] = '';
269 if ($hasAccessToAllCases) {
270 $contactType = empty($row['relation_type']) ? '' : (string) CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_RelationshipType', $row['relation_type'], 'contact_type_b');
271 $contactType = $contactType == 'Contact' ? '' : $contactType;
272 switch ($row['source']) {
273 case 'caseRel':
274 $row['actions'] = '<a href="#editCaseRoleDialog" title="' . ts('Reassign %1', array(1 => $typeLabel)) . '" class="crm-hover-button case-miniform" data-contact_type="' . $contactType . '" data-rel_type="' . $row['relation_type'] . '_' . $row['relationship_direction'] . '" data-cid="' . '" data-rel_id="' . $row['rel_id'] . '"data-key="' . CRM_Core_Key::get('civicrm/ajax/relation') . '">' .
275 '<i class="crm-i fa-pencil"></i>' .
276 '</a>' .
277 '<a href="#deleteCaseRoleDialog" title="' . ts('Remove %1', array(1 => $typeLabel)) . '" class="crm-hover-button case-miniform" data-contact_type="' . $contactType . '" data-rel_type="' . $row['relation_type'] . '_' . $row['relationship_direction'] . '" data-cid="' . $row['cid'] . '" data-key="' . CRM_Core_Key::get('civicrm/ajax/delcaserole') . '">' .
278 '<span class="icon delete-icon"></span>' .
279 '</a>';
280 break;
281
282 case 'caseRoles':
283 $row['actions'] = '<a href="#editCaseRoleDialog" title="' . ts('Assign %1', array(1 => $typeLabel)) . '" class="crm-hover-button case-miniform" data-contact_type="' . $contactType . '" data-rel_type="' . $row['relation_type'] . '_b_a" data-key="' . CRM_Core_Key::get('civicrm/ajax/relation') . '">' .
284 '<i class="crm-i fa-pencil"></i>' .
285 '</a>';
286 break;
287 }
288 }
289 unset($row['cid']);
290 unset($row['relation_type']);
291 unset($row['rel_id']);
292 unset($row['client_id']);
293 unset($row['source']);
294 array_push($relationships, $row);
295 }
296 $params['total'] = count($relationships);
297
298 $caseRelationshipsDT = array();
299 $caseRelationshipsDT['data'] = $relationships;
300 $caseRelationshipsDT['recordsTotal'] = $params['total'];
301 $caseRelationshipsDT['recordsFiltered'] = $params['total'];
302
303 CRM_Utils_JSON::output($caseRelationshipsDT);
304
305 }
306
307 public static function convertToCaseActivity() {
308 $params = array('caseID', 'activityID', 'contactID', 'newSubject', 'targetContactIds', 'mode');
309 $vals = array();
310 foreach ($params as $param) {
311 $vals[$param] = CRM_Utils_Array::value($param, $_POST);
312 }
313
314 CRM_Utils_JSON::output(self::_convertToCaseActivity($vals));
315 }
316
317 /**
318 * @param array $params
319 *
320 * @return array
321 */
322 public static function _convertToCaseActivity($params) {
323 if (!$params['activityID'] || !$params['caseID']) {
324 return (array('error_msg' => 'required params missing.'));
325 }
326
327 $otherActivity = new CRM_Activity_DAO_Activity();
328 $otherActivity->id = $params['activityID'];
329 if (!$otherActivity->find(TRUE)) {
330 return (array('error_msg' => 'activity record is missing.'));
331 }
332 $actDateTime = CRM_Utils_Date::isoToMysql($otherActivity->activity_date_time);
333
334 // Create new activity record.
335 $mainActivity = new CRM_Activity_DAO_Activity();
336 $mainActVals = array();
337 CRM_Core_DAO::storeValues($otherActivity, $mainActVals);
338
339 // Get new activity subject.
340 if (!empty($params['newSubject'])) {
341 $mainActVals['subject'] = $params['newSubject'];
342 }
343
344 $mainActivity->copyValues($mainActVals);
345 $mainActivity->id = NULL;
346 $mainActivity->activity_date_time = $actDateTime;
347 // Make sure this is current revision.
348 $mainActivity->is_current_revision = TRUE;
349 // Drop all relations.
350 $mainActivity->parent_id = $mainActivity->original_id = NULL;
351
352 $mainActivity->save();
353 $mainActivityId = $mainActivity->id;
354 CRM_Activity_BAO_Activity::logActivityAction($mainActivity);
355 $mainActivity->free();
356
357 // Mark previous activity as deleted. If it was a non-case activity
358 // then just change the subject.
359 if (in_array($params['mode'], array(
360 'move',
361 'file',
362 ))) {
363 $caseActivity = new CRM_Case_DAO_CaseActivity();
364 $caseActivity->case_id = $params['caseID'];
365 $caseActivity->activity_id = $otherActivity->id;
366 if ($params['mode'] == 'move' || $caseActivity->find(TRUE)) {
367 $otherActivity->is_deleted = 1;
368 }
369 else {
370 $otherActivity->subject = ts('(Filed on case %1)', array(
371 1 => $params['caseID'],
372 )) . ' ' . $otherActivity->subject;
373 }
374 $otherActivity->activity_date_time = $actDateTime;
375 $otherActivity->save();
376
377 $caseActivity->free();
378 }
379 $otherActivity->free();
380
381 $targetContacts = array();
382 if (!empty($params['targetContactIds'])) {
383 $targetContacts = array_unique(explode(',', $params['targetContactIds']));
384 }
385
386 $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
387 $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts);
388 $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts);
389 $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts);
390
391 $sourceContactID = CRM_Activity_BAO_Activity::getSourceContactID($params['activityID']);
392 $src_params = array(
393 'activity_id' => $mainActivityId,
394 'contact_id' => $sourceContactID,
395 'record_type_id' => $sourceID,
396 );
397 CRM_Activity_BAO_ActivityContact::create($src_params);
398
399 foreach ($targetContacts as $key => $value) {
400 $targ_params = array(
401 'activity_id' => $mainActivityId,
402 'contact_id' => $value,
403 'record_type_id' => $targetID,
404 );
405 CRM_Activity_BAO_ActivityContact::create($targ_params);
406 }
407
408 // typically this will be empty, since assignees on another case may be completely different
409 $assigneeContacts = array();
410 if (!empty($params['assigneeContactIds'])) {
411 $assigneeContacts = array_unique(explode(',', $params['assigneeContactIds']));
412 }
413 foreach ($assigneeContacts as $key => $value) {
414 $assigneeParams = array(
415 'activity_id' => $mainActivityId,
416 'contact_id' => $value,
417 'record_type_id' => $assigneeID,
418 );
419 CRM_Activity_BAO_ActivityContact::create($assigneeParams);
420 }
421
422 // Attach newly created activity to case.
423 $caseActivity = new CRM_Case_DAO_CaseActivity();
424 $caseActivity->case_id = $params['caseID'];
425 $caseActivity->activity_id = $mainActivityId;
426 $caseActivity->save();
427 $error_msg = $caseActivity->_lastError;
428 $caseActivity->free();
429
430 $params['mainActivityId'] = $mainActivityId;
431 CRM_Activity_BAO_Activity::copyExtendedActivityData($params);
432
433 return (array('error_msg' => $error_msg, 'newId' => $mainActivity->id));
434 }
435
436 public static function getContactActivity() {
437 $contactID = CRM_Utils_Type::escape($_GET['cid'], 'Integer');
438 $context = CRM_Utils_Type::escape(CRM_Utils_Array::value('context', $_GET), 'String');
439
440 $sortMapper = array();
441 foreach ($_GET['columns'] as $key => $value) {
442 $sortMapper[$key] = $value['data'];
443 };
444
445 $offset = isset($_GET['start']) ? CRM_Utils_Type::escape($_GET['start'], 'Integer') : 0;
446 $rowCount = isset($_GET['length']) ? CRM_Utils_Type::escape($_GET['length'], 'Integer') : 25;
447 $sort = isset($_GET['order'][0]['column']) ? CRM_Utils_Array::value(CRM_Utils_Type::escape($_GET['order'][0]['column'], 'Integer'), $sortMapper) : NULL;
448 $sortOrder = isset($_GET['order'][0]['dir']) ? CRM_Utils_Type::escape($_GET['order'][0]['dir'], 'String') : 'asc';
449
450 $params = $_GET;
451 if ($sort && $sortOrder) {
452 $params['sortBy'] = $sort . ' ' . $sortOrder;
453 }
454
455 $params['page'] = ($offset / $rowCount) + 1;
456 $params['rp'] = $rowCount;
457
458 $params['contact_id'] = $contactID;
459 $params['context'] = $context;
460
461 // get the contact activities
462 $activities = CRM_Activity_BAO_Activity::getContactActivitySelector($params);
463
464 foreach ($activities as $key => $value) {
465 // Check if recurring activity.
466 if (!empty($value['is_recurring_activity'])) {
467 $repeat = $value['is_recurring_activity'];
468 $activities[$key]['activity_type'] .= '<br/><span class="bold">' . ts('Repeating (%1 of %2)', array(1 => $repeat[0], 2 => $repeat[1])) . '</span>';
469 }
470 }
471
472 // store the activity filter preference CRM-11761
473 $session = CRM_Core_Session::singleton();
474 $userID = $session->get('userID');
475 if ($userID) {
476 $activityFilter = array(
477 'activity_type_filter_id' => empty($params['activity_type_id']) ? '' : CRM_Utils_Type::escape($params['activity_type_id'], 'Integer'),
478 'activity_type_exclude_filter_id' => empty($params['activity_type_exclude_id']) ? '' : CRM_Utils_Type::escape($params['activity_type_exclude_id'], 'Integer'),
479 );
480
481 /**
482 * @var \Civi\Core\SettingsBag $cSettings
483 */
484 $cSettings = Civi::service('settings_manager')->getBagByContact(CRM_Core_Config::domainID(), $userID);
485 $cSettings->set('activity_tab_filter', $activityFilter);
486 }
487
488 CRM_Utils_JSON::output($activities);
489 }
490
491 }