PageRenderTime 35ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/public_html/sites/all/modules/civicrm/CRM/Activity/Page/AJAX.php

https://github.com/timstephenson/NatureBridge
PHP | 223 lines | 144 code | 35 blank | 44 comment | 14 complexity | 211989b9015f39cafeb43c4ad8b616b8 MD5 | raw file
  1. <?php
  2. /*
  3. +--------------------------------------------------------------------+
  4. | CiviCRM version 4.0 |
  5. +--------------------------------------------------------------------+
  6. | Copyright CiviCRM LLC (c) 2004-2011 |
  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-2011
  31. *
  32. */
  33. /**
  34. * This class contains all the function that are called using AJAX (jQuery)
  35. */
  36. class CRM_Activity_Page_AJAX
  37. {
  38. static function getCaseActivity( )
  39. {
  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. $sortMapper = array( 0 => 'display_date', 1 => 'ca.subject', 2 => 'ca.activity_type_id',
  45. 3 => 'acc.sort_name', 4 => 'cc.sort_name', 5 => 'ca.status_id' );
  46. $sEcho = CRM_Utils_Type::escape($_REQUEST['sEcho'], 'Integer');
  47. $offset = isset($_REQUEST['iDisplayStart'])? CRM_Utils_Type::escape($_REQUEST['iDisplayStart'], 'Integer'):0;
  48. $rowCount = isset($_REQUEST['iDisplayLength'])? CRM_Utils_Type::escape($_REQUEST['iDisplayLength'], 'Integer'):25;
  49. $sort = isset($_REQUEST['iSortCol_0'] )? CRM_Utils_Array::value( CRM_Utils_Type::escape($_REQUEST['iSortCol_0'],'Integer'), $sortMapper ): null;
  50. $sortOrder = isset($_REQUEST['sSortDir_0'] )? CRM_Utils_Type::escape($_REQUEST['sSortDir_0'], 'String'):'asc';
  51. $params = $_POST;
  52. if ( $sort && $sortOrder ) {
  53. $params['sortname'] = $sort;
  54. $params['sortorder'] = $sortOrder;
  55. }
  56. $params['page'] = ($offset/$rowCount) + 1;
  57. $params['rp'] = $rowCount;
  58. // get the activities related to given case
  59. require_once "CRM/Case/BAO/Case.php";
  60. $activities = CRM_Case_BAO_Case::getCaseActivity( $caseID, $params, $contactID, $context, $userID );
  61. require_once "CRM/Utils/JSON.php";
  62. $iFilteredTotal = $iTotal = $params['total'];
  63. $selectorElements = array( 'display_date', 'subject', 'type', 'with_contacts', 'reporter', 'status', 'links', 'class' );
  64. echo CRM_Utils_JSON::encodeDataTableSelector( $activities, $sEcho, $iTotal, $iFilteredTotal, $selectorElements );
  65. CRM_Utils_System::civiExit( );
  66. }
  67. static function convertToCaseActivity()
  68. {
  69. $params = array( 'caseID', 'activityID', 'contactID', 'newSubject', 'targetContactIds', 'mode' );
  70. foreach ( $params as $param ) {
  71. $$param = CRM_Utils_Array::value( $param, $_POST );
  72. }
  73. if ( !$activityID || !$caseID ) {
  74. echo json_encode( array('error_msg' => 'required params missing.' ) );
  75. CRM_Utils_System::civiExit( );
  76. }
  77. require_once "CRM/Activity/DAO/Activity.php";
  78. $otherActivity = new CRM_Activity_DAO_Activity();
  79. $otherActivity->id = $activityID;
  80. if ( !$otherActivity->find( true ) ) {
  81. echo json_encode( array('error_msg' => 'activity record is missing.' ) );
  82. CRM_Utils_System::civiExit( );
  83. }
  84. $actDateTime = CRM_Utils_Date::isoToMysql( $otherActivity->activity_date_time );
  85. //create new activity record.
  86. $mainActivity = new CRM_Activity_DAO_Activity( );
  87. $mainActVals = array( );
  88. CRM_Core_DAO::storeValues( $otherActivity, $mainActVals );
  89. //get new activity subject.
  90. if ( !empty( $newSubject ) ) $mainActVals['subject'] = $newSubject;
  91. $mainActivity->copyValues( $mainActVals );
  92. $mainActivity->id = null;
  93. $mainActivity->activity_date_time = $actDateTime;
  94. //make sure this is current revision.
  95. $mainActivity->is_current_revision = true;
  96. //drop all relations.
  97. $mainActivity->parent_id = $mainActivity->original_id = null;
  98. $mainActivity->save( );
  99. $mainActivityId = $mainActivity->id;
  100. require_once 'CRM/Activity/BAO/Activity.php';
  101. CRM_Activity_BAO_Activity::logActivityAction( $mainActivity );
  102. $mainActivity->free( );
  103. //mark previous activity as deleted.
  104. if ( in_array( $mode, array( 'move', 'file' ) ) ) {
  105. $otherActivity->activity_date_time = $actDateTime;
  106. $otherActivity->is_deleted = 1;
  107. $otherActivity->save( );
  108. }
  109. $otherActivity->free( );
  110. require_once "CRM/Activity/BAO/Activity.php";
  111. $targetContacts = array( );
  112. if ( !empty( $targetContactIds ) ) {
  113. $targetContacts = array_unique( explode( ',', $targetContactIds ) );
  114. }
  115. foreach ( $targetContacts as $key => $value ) {
  116. $params = array( 'activity_id' => $mainActivityId,
  117. 'target_contact_id' => $value );
  118. CRM_Activity_BAO_Activity::createActivityTarget( $params );
  119. }
  120. //attach newly created activity to case.
  121. require_once "CRM/Case/DAO/CaseActivity.php";
  122. $caseActivity = new CRM_Case_DAO_CaseActivity( );
  123. $caseActivity->case_id = $caseID;
  124. $caseActivity->activity_id = $mainActivityId;
  125. $caseActivity->save( );
  126. $error_msg = $caseActivity->_lastError;
  127. $caseActivity->free( );
  128. // attach custom data to the new activity
  129. require_once 'CRM/Core/BAO/CustomValueTable.php';
  130. require_once 'CRM/Core/BAO/File.php';
  131. $customParams = $htmlType = array( );
  132. $customValues = CRM_Core_BAO_CustomValueTable::getEntityValues( $activityID, 'Activity' );
  133. $fieldIds = implode( ', ', array_keys( $customValues ) );
  134. $sql = "SELECT id FROM civicrm_custom_field WHERE html_type = 'File' AND id IN ( {$fieldIds} )";
  135. $result = CRM_Core_DAO::executeQuery( $sql );
  136. while ( $result->fetch( ) ) {
  137. $htmlType[] = $result->id;
  138. }
  139. foreach ( $customValues as $key => $value ) {
  140. if ( $value ) {
  141. if ( in_array( $key, $htmlType ) ) {
  142. $fileValues = CRM_Core_BAO_File::path( $value, $activityID );
  143. $customParams["custom_{$key}_-1"] = array( 'name' => $fileValues[0],
  144. 'path' => $fileValues[1] );
  145. } else {
  146. $customParams["custom_{$key}_-1"] = $value;
  147. }
  148. }
  149. }
  150. CRM_Core_BAO_CustomValueTable::postProcess( $customParams, CRM_Core_DAO::$_nullArray, 'civicrm_activity',
  151. $mainActivityId, 'Activity' );
  152. // copy activity attachments ( if any )
  153. require_once "CRM/Core/BAO/File.php";
  154. CRM_Core_BAO_File::copyEntityFile( 'civicrm_activity', $activityID, 'civicrm_activity', $mainActivityId );
  155. echo json_encode(array('error_msg' => $error_msg));
  156. CRM_Utils_System::civiExit( );
  157. }
  158. static function getContactActivity( )
  159. {
  160. $contactID = CRM_Utils_Type::escape( $_POST['contact_id'], 'Integer' );
  161. $context = CRM_Utils_Type::escape( CRM_Utils_Array::value( 'context', $_GET ), 'String' );
  162. $sortMapper = array( 0 => 'activity_type', 1 => 'subject', 2 => 'source_contact_name',
  163. 3 => '', 4 => '', 5 => 'activity_date_time', 6 => 'status_id' );
  164. $sEcho = CRM_Utils_Type::escape($_REQUEST['sEcho'], 'Integer');
  165. $offset = isset($_REQUEST['iDisplayStart'])? CRM_Utils_Type::escape($_REQUEST['iDisplayStart'], 'Integer'):0;
  166. $rowCount = isset($_REQUEST['iDisplayLength'])? CRM_Utils_Type::escape($_REQUEST['iDisplayLength'], 'Integer'):25;
  167. $sort = isset($_REQUEST['iSortCol_0'] )? CRM_Utils_Array::value( CRM_Utils_Type::escape($_REQUEST['iSortCol_0'],'Integer'), $sortMapper ): null;
  168. $sortOrder = isset($_REQUEST['sSortDir_0'] )? CRM_Utils_Type::escape($_REQUEST['sSortDir_0'], 'String'):'asc';
  169. $params = $_POST;
  170. if ( $sort && $sortOrder ) {
  171. $params['sortBy'] = $sort . ' '. $sortOrder;
  172. }
  173. $params['page'] = ($offset/$rowCount) + 1;
  174. $params['rp'] = $rowCount;
  175. $params['contact_id'] = $contactID;
  176. $params['context' ] = $context;
  177. // get the contact activities
  178. require_once 'CRM/Activity/BAO/Activity.php';
  179. $activities = CRM_Activity_BAO_Activity::getContactActivitySelector( $params );
  180. require_once "CRM/Utils/JSON.php";
  181. $iFilteredTotal = $iTotal = $params['total'];
  182. $selectorElements = array( 'activity_type', 'subject', 'source_contact',
  183. 'target_contact', 'assignee_contact',
  184. 'activity_date', 'status', 'links', 'class' );
  185. echo CRM_Utils_JSON::encodeDataTableSelector( $activities, $sEcho, $iTotal, $iFilteredTotal, $selectorElements );
  186. CRM_Utils_System::civiExit( );
  187. }
  188. }