1. <?php
  2. /*
  3. +--------------------------------------------------------------------+
  4. | CiviCRM version 4.2 |
  5. +--------------------------------------------------------------------+
  6. | Copyright CiviCRM LLC (c) 2004-2012 |
  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. * @package CRM
  30. * @copyright CiviCRM LLC (c) 2004-2012
  31. *
  32. */
  33. /**
  34. * This class contains all the function that are called using AJAX (jQuery)
  35. */
  36. class CRM_Activity_Page_AJAX {
  37. static
  38. function getCaseActivity() {
  39. $caseID = CRM_Utils_Type::escape($_GET['caseID'], 'Integer');
  40. $contactID = CRM_Utils_Type::escape($_GET['cid'], 'Integer');
  41. $userID = CRM_Utils_Type::escape($_GET['userID'], 'Integer');
  42. $context = CRM_Utils_Type::escape(CRM_Utils_Array::value('context', $_GET), 'String');
  43. $sortMapper = array(
  44. 0 => 'display_date', 1 => 'ca.subject', 2 => 'ca.activity_type_id',
  45. 3 => 'acc.sort_name', 4 => 'cc.sort_name', 5 => 'ca.status_id', 7 => 'location',
  46. );
  47. $sEcho = CRM_Utils_Type::escape($_REQUEST['sEcho'], 'Integer');
  48. $offset = isset($_REQUEST['iDisplayStart']) ? CRM_Utils_Type::escape($_REQUEST['iDisplayStart'], 'Integer') : 0;
  49. $rowCount = isset($_REQUEST['iDisplayLength']) ? CRM_Utils_Type::escape($_REQUEST['iDisplayLength'], 'Integer') : 25;
  50. $sort = isset($_REQUEST['iSortCol_0']) ? CRM_Utils_Array::value(CRM_Utils_Type::escape($_REQUEST['iSortCol_0'], 'Integer'), $sortMapper) : NULL;
  51. $sortOrder = isset($_REQUEST['sSortDir_0']) ? CRM_Utils_Type::escape($_REQUEST['sSortDir_0'], 'String') : 'asc';
  52. $params = $_POST;
  53. if ($sort && $sortOrder) {
  54. $params['sortname'] = $sort;
  55. $params['sortorder'] = $sortOrder;
  56. }
  57. $params['page'] = ($offset / $rowCount) + 1;
  58. $params['rp'] = $rowCount;
  59. // get the activities related to given case
  60. $activities = CRM_Case_BAO_Case::getCaseActivity($caseID, $params, $contactID, $context, $userID);
  61. $iFilteredTotal = $iTotal = $params['total'];
  62. $selectorElements = array('display_date', 'subject', 'type', 'with_contacts', 'reporter', 'location', 'status', 'links', 'class');
  63. echo CRM_Utils_JSON::encodeDataTableSelector($activities, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
  64. CRM_Utils_System::civiExit();
  65. }
  66. static
  67. function convertToCaseActivity() {
  68. $params = array('caseID', 'activityID', 'contactID', 'newSubject', 'targetContactIds', 'mode');
  69. foreach ($params as $param) {
  70. $vals[$param] = CRM_Utils_Array::value($param, $_POST);
  71. }
  72. $retval = self::_convertToCaseActivity($vals);
  73. echo json_encode($retval);
  74. CRM_Utils_System::civiExit();
  75. }
  76. static
  77. function _convertToCaseActivity($params) {
  78. if (!$params['activityID'] || !$params['caseID']) {
  79. return (array('error_msg' => 'required params missing.'));
  80. }
  81. $otherActivity = new CRM_Activity_DAO_Activity();
  82. $otherActivity->id = $params['activityID'];
  83. if (!$otherActivity->find(TRUE)) {
  84. return (array('error_msg' => 'activity record is missing.'));
  85. }
  86. $actDateTime = CRM_Utils_Date::isoToMysql($otherActivity->activity_date_time);
  87. //create new activity record.
  88. $mainActivity = new CRM_Activity_DAO_Activity();
  89. $mainActVals = array();
  90. CRM_Core_DAO::storeValues($otherActivity, $mainActVals);
  91. //get new activity subject.
  92. if (!empty($params['newSubject'])) {
  93. $mainActVals['subject'] = $params['newSubject'];
  94. }
  95. $mainActivity->copyValues($mainActVals);
  96. $mainActivity->id = NULL;
  97. $mainActivity->activity_date_time = $actDateTime;
  98. //make sure this is current revision.
  99. $mainActivity->is_current_revision = TRUE;
  100. //drop all relations.
  101. $mainActivity->parent_id = $mainActivity->original_id = NULL;
  102. $mainActivity->save();
  103. $mainActivityId = $mainActivity->id;
  104. CRM_Activity_BAO_Activity::logActivityAction($mainActivity);
  105. $mainActivity->free();
  106. /* Mark previous activity as deleted. If it was a non-case activity
  107. * then just change the subject.
  108. */
  109. if (in_array($params['mode'], array(
  110. 'move', 'file'))) {
  111. $caseActivity = new CRM_Case_DAO_CaseActivity();
  112. $caseActivity->case_id = $params['caseID'];
  113. $caseActivity->activity_id = $otherActivity->id;
  114. if ($params['mode'] == 'move' || $caseActivity->find(TRUE)) {
  115. $otherActivity->is_deleted = 1;
  116. }
  117. else {
  118. $otherActivity->subject = ts('(Filed on case %1)', array(
  119. 1 => $params['caseID'])) . ' ' . $otherActivity->subject;
  120. }
  121. $otherActivity->activity_date_time = $actDateTime;
  122. $otherActivity->save();
  123. $caseActivity->free();
  124. }
  125. $otherActivity->free();
  126. $targetContacts = array();
  127. if (!empty($params['targetContactIds'])) {
  128. $targetContacts = array_unique(explode(',', $params['targetContactIds']));
  129. }
  130. foreach ($targetContacts as $key => $value) {
  131. $targ_params = array(
  132. 'activity_id' => $mainActivityId,
  133. 'target_contact_id' => $value,
  134. );
  135. CRM_Activity_BAO_Activity::createActivityTarget($targ_params);
  136. }
  137. // typically this will be empty, since assignees on another case may be completely different
  138. $assigneeContacts = array();
  139. if (!empty($params['assigneeContactIds'])) {
  140. $assigneeContacts = array_unique(explode(',', $params['assigneeContactIds']));
  141. }
  142. foreach ($assigneeContacts as $key => $value) {
  143. $assigneeParams = array(
  144. 'activity_id' => $mainActivityId,
  145. 'assignee_contact_id' => $value,
  146. );
  147. CRM_Activity_BAO_Activity::createActivityAssignment($assigneeParams);
  148. }
  149. //attach newly created activity to case.
  150. $caseActivity = new CRM_Case_DAO_CaseActivity();
  151. $caseActivity->case_id = $params['caseID'];
  152. $caseActivity->activity_id = $mainActivityId;
  153. $caseActivity->save();
  154. $error_msg = $caseActivity->_lastError;
  155. $caseActivity->free();
  156. $params['mainActivityId'] = $mainActivityId;
  157. CRM_Activity_BAO_Activity::copyExtendedActivityData($params);
  158. return (array('error_msg' => $error_msg, 'newId' => $mainActivity->id));
  159. }
  160. static
  161. function getContactActivity() {
  162. $contactID = CRM_Utils_Type::escape($_POST['contact_id'], 'Integer');
  163. $context = CRM_Utils_Type::escape(CRM_Utils_Array::value('context', $_GET), 'String');
  164. $sortMapper = array(
  165. 0 => 'activity_type', 1 => 'subject', 2 => 'source_contact_name',
  166. 3 => '', 4 => '', 5 => '', 6 => 'activity_date_time', 7 => 'status_id'
  167. );
  168. $sEcho = CRM_Utils_Type::escape($_REQUEST['sEcho'], 'Integer');
  169. $offset = isset($_REQUEST['iDisplayStart']) ? CRM_Utils_Type::escape($_REQUEST['iDisplayStart'], 'Integer') : 0;
  170. $rowCount = isset($_REQUEST['iDisplayLength']) ? CRM_Utils_Type::escape($_REQUEST['iDisplayLength'], 'Integer') : 25;
  171. $sort = isset($_REQUEST['iSortCol_0']) ? CRM_Utils_Array::value(CRM_Utils_Type::escape($_REQUEST['iSortCol_0'], 'Integer'), $sortMapper) : NULL;
  172. $sortOrder = isset($_REQUEST['sSortDir_0']) ? CRM_Utils_Type::escape($_REQUEST['sSortDir_0'], 'String') : 'asc';
  173. $params = $_POST;
  174. if ($sort && $sortOrder) {
  175. $params['sortBy'] = $sort . ' ' . $sortOrder;
  176. }
  177. $params['page'] = ($offset / $rowCount) + 1;
  178. $params['rp'] = $rowCount;
  179. $params['contact_id'] = $contactID;
  180. $params['context'] = $context;
  181. // get the contact activities
  182. $activities = CRM_Activity_BAO_Activity::getContactActivitySelector($params);
  183. $iFilteredTotal = $iTotal = $params['total'];
  184. $selectorElements = array(
  185. 'activity_type', 'subject', 'source_contact',
  186. 'target_contact', 'assignee_contact', 'location',
  187. 'activity_date', 'status', 'links', 'class',
  188. );
  189. echo CRM_Utils_JSON::encodeDataTableSelector($activities, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
  190. CRM_Utils_System::civiExit();
  191. }
  192. }