PageRenderTime 41ms CodeModel.GetById 25ms app.highlight 11ms RepoModel.GetById 0ms 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 +--------------------------------------------------------------------+
  5 | CiviCRM version 4.0                                                |
  6 +--------------------------------------------------------------------+
  7 | Copyright CiviCRM LLC (c) 2004-2011                                |
  8 +--------------------------------------------------------------------+
  9 | This file is a part of CiviCRM.                                    |
 10 |                                                                    |
 11 | CiviCRM is free software; you can copy, modify, and distribute it  |
 12 | under the terms of the GNU Affero General Public License           |
 13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception.   |
 14 |                                                                    |
 15 | CiviCRM is distributed in the hope that it will be useful, but     |
 16 | WITHOUT ANY WARRANTY; without even the implied warranty of         |
 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               |
 18 | See the GNU Affero General Public License for more details.        |
 19 |                                                                    |
 20 | You should have received a copy of the GNU Affero General Public   |
 21 | License and the CiviCRM Licensing Exception along                  |
 22 | with this program; if not, contact CiviCRM LLC                     |
 23 | at info[AT]civicrm[DOT]org. If you have questions about the        |
 24 | GNU Affero General Public License or the licensing of CiviCRM,     |
 25 | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
 26 +--------------------------------------------------------------------+
 27*/
 28
 29/**
 30 *
 31 * @package CRM
 32 * @copyright CiviCRM LLC (c) 2004-2011
 33 *
 34 */
 35
 36/**
 37 * This class contains all the function that are called using AJAX (jQuery)
 38 */
 39class CRM_Activity_Page_AJAX
 40{
 41    static function getCaseActivity( ) 
 42    {
 43        $caseID    = CRM_Utils_Type::escape( $_GET['caseID'], 'Integer' );
 44        $contactID = CRM_Utils_Type::escape( $_GET['cid'], 'Integer' );
 45        $userID    = CRM_Utils_Type::escape( $_GET['userID'], 'Integer' );
 46        $context   = CRM_Utils_Type::escape( CRM_Utils_Array::value( 'context', $_GET ), 'String' );
 47        
 48        $sortMapper  = array( 0 => 'display_date', 1 => 'ca.subject', 2 => 'ca.activity_type_id', 
 49                              3 => 'acc.sort_name', 4 => 'cc.sort_name', 5 => 'ca.status_id'  );
 50
 51        $sEcho       = CRM_Utils_Type::escape($_REQUEST['sEcho'], 'Integer');
 52        $offset      = isset($_REQUEST['iDisplayStart'])? CRM_Utils_Type::escape($_REQUEST['iDisplayStart'], 'Integer'):0;
 53        $rowCount    = isset($_REQUEST['iDisplayLength'])? CRM_Utils_Type::escape($_REQUEST['iDisplayLength'], 'Integer'):25; 
 54        $sort        = isset($_REQUEST['iSortCol_0'] )? CRM_Utils_Array::value( CRM_Utils_Type::escape($_REQUEST['iSortCol_0'],'Integer'), $sortMapper ): null;
 55        $sortOrder   = isset($_REQUEST['sSortDir_0'] )? CRM_Utils_Type::escape($_REQUEST['sSortDir_0'], 'String'):'asc';
 56
 57        $params    = $_POST;
 58        if ( $sort && $sortOrder ) {
 59            $params['sortname']  = $sort;
 60            $params['sortorder'] = $sortOrder;
 61        }
 62        $params['page'] = ($offset/$rowCount) + 1;
 63        $params['rp']   = $rowCount;
 64
 65        // get the activities related to given case
 66        require_once "CRM/Case/BAO/Case.php";
 67        $activities = CRM_Case_BAO_Case::getCaseActivity( $caseID, $params, $contactID, $context, $userID );
 68
 69        require_once "CRM/Utils/JSON.php";
 70        $iFilteredTotal = $iTotal =  $params['total'];
 71        $selectorElements = array( 'display_date', 'subject', 'type', 'with_contacts', 'reporter', 'status', 'links', 'class' );
 72
 73        echo CRM_Utils_JSON::encodeDataTableSelector( $activities, $sEcho, $iTotal, $iFilteredTotal, $selectorElements );
 74        CRM_Utils_System::civiExit( );
 75
 76    }
 77    
 78    static function convertToCaseActivity()
 79    {
 80        $params = array( 'caseID', 'activityID', 'contactID', 'newSubject', 'targetContactIds', 'mode' );
 81        foreach ( $params as $param ) {
 82            $$param = CRM_Utils_Array::value( $param, $_POST );
 83        }
 84        
 85        if ( !$activityID || !$caseID ) {
 86            echo json_encode( array('error_msg' => 'required params missing.' ) );
 87            CRM_Utils_System::civiExit( );
 88        }
 89        
 90        require_once "CRM/Activity/DAO/Activity.php";
 91        $otherActivity = new CRM_Activity_DAO_Activity();
 92        $otherActivity->id = $activityID;
 93        if ( !$otherActivity->find( true ) ) {
 94            echo json_encode( array('error_msg' => 'activity record is missing.' ) );
 95            CRM_Utils_System::civiExit( );  
 96        }
 97        $actDateTime = CRM_Utils_Date::isoToMysql( $otherActivity->activity_date_time );
 98        
 99        //create new activity record.
100        $mainActivity = new CRM_Activity_DAO_Activity( );
101        $mainActVals  = array( );
102        CRM_Core_DAO::storeValues( $otherActivity, $mainActVals );
103        
104        //get new activity subject.
105        if ( !empty( $newSubject ) ) $mainActVals['subject'] = $newSubject;
106        
107        $mainActivity->copyValues( $mainActVals );
108        $mainActivity->id = null;
109        $mainActivity->activity_date_time = $actDateTime;
110        //make sure this is current revision.
111        $mainActivity->is_current_revision = true;
112        //drop all relations.
113        $mainActivity->parent_id = $mainActivity->original_id = null;
114        
115        $mainActivity->save( );
116        $mainActivityId = $mainActivity->id;
117        require_once 'CRM/Activity/BAO/Activity.php';
118        CRM_Activity_BAO_Activity::logActivityAction( $mainActivity );
119        $mainActivity->free( );
120        
121        //mark previous activity as deleted.
122        if ( in_array( $mode, array( 'move', 'file' ) ) ) {
123            $otherActivity->activity_date_time = $actDateTime;
124            $otherActivity->is_deleted = 1;
125            $otherActivity->save( );
126        }
127        $otherActivity->free( ); 
128        
129        require_once "CRM/Activity/BAO/Activity.php";
130        $targetContacts = array( );
131        if ( !empty( $targetContactIds ) ) {
132            $targetContacts = array_unique( explode( ',', $targetContactIds ) );
133        }
134        foreach ( $targetContacts as $key => $value ) { 
135            $params = array( 'activity_id' => $mainActivityId, 
136                             'target_contact_id' => $value );
137            CRM_Activity_BAO_Activity::createActivityTarget( $params );
138        }
139        
140        //attach newly created activity to case.
141        require_once "CRM/Case/DAO/CaseActivity.php";
142        $caseActivity = new CRM_Case_DAO_CaseActivity( );
143        $caseActivity->case_id     = $caseID;
144        $caseActivity->activity_id = $mainActivityId;
145        $caseActivity->save( );
146        $error_msg = $caseActivity->_lastError;
147        $caseActivity->free( ); 
148
149        // attach custom data to the new activity
150        require_once 'CRM/Core/BAO/CustomValueTable.php';
151        require_once 'CRM/Core/BAO/File.php';
152        $customParams = $htmlType = array( );
153        $customValues = CRM_Core_BAO_CustomValueTable::getEntityValues( $activityID, 'Activity' );
154
155        $fieldIds = implode( ', ', array_keys( $customValues ) );
156        $sql      = "SELECT id FROM civicrm_custom_field WHERE html_type = 'File' AND id IN ( {$fieldIds} )";
157        $result   = CRM_Core_DAO::executeQuery( $sql );
158        
159        while ( $result->fetch( ) ) {
160            $htmlType[] = $result->id;
161        }
162                
163        foreach ( $customValues as $key => $value ) {
164            if ( $value ) {
165                if ( in_array( $key, $htmlType ) ) {
166                    $fileValues = CRM_Core_BAO_File::path( $value, $activityID );
167                    $customParams["custom_{$key}_-1"] = array( 'name' => $fileValues[0],
168                                                               'path' => $fileValues[1] );
169                } else {
170                    $customParams["custom_{$key}_-1"] = $value;
171                }
172            }
173        }
174        CRM_Core_BAO_CustomValueTable::postProcess( $customParams, CRM_Core_DAO::$_nullArray, 'civicrm_activity',
175                                                    $mainActivityId, 'Activity' );
176        
177        // copy activity attachments ( if any )
178        require_once "CRM/Core/BAO/File.php";
179        CRM_Core_BAO_File::copyEntityFile( 'civicrm_activity', $activityID, 'civicrm_activity', $mainActivityId );
180            
181        echo json_encode(array('error_msg' => $error_msg));
182        CRM_Utils_System::civiExit( );
183    }
184    
185    static function getContactActivity( ) 
186    {
187        $contactID = CRM_Utils_Type::escape( $_POST['contact_id'], 'Integer' );
188        $context   = CRM_Utils_Type::escape( CRM_Utils_Array::value( 'context', $_GET ), 'String' );
189    
190        $sortMapper  = array( 0 => 'activity_type', 1 => 'subject', 2 => 'source_contact_name', 
191                              3 => '', 4 => '', 5 => 'activity_date_time', 6 => 'status_id'  );
192
193        $sEcho       = CRM_Utils_Type::escape($_REQUEST['sEcho'], 'Integer');
194        $offset      = isset($_REQUEST['iDisplayStart'])? CRM_Utils_Type::escape($_REQUEST['iDisplayStart'], 'Integer'):0;
195        $rowCount    = isset($_REQUEST['iDisplayLength'])? CRM_Utils_Type::escape($_REQUEST['iDisplayLength'], 'Integer'):25; 
196        $sort        = isset($_REQUEST['iSortCol_0'] )? CRM_Utils_Array::value( CRM_Utils_Type::escape($_REQUEST['iSortCol_0'],'Integer'), $sortMapper ): null;
197        $sortOrder   = isset($_REQUEST['sSortDir_0'] )? CRM_Utils_Type::escape($_REQUEST['sSortDir_0'], 'String'):'asc';
198
199        $params    = $_POST;
200        if ( $sort && $sortOrder ) {
201            $params['sortBy']  = $sort . ' '. $sortOrder;
202        }
203        
204        $params['page'] = ($offset/$rowCount) + 1;
205        $params['rp']   = $rowCount;
206
207        $params['contact_id'] = $contactID;
208        $params['context'   ] = $context;
209        
210        // get the contact activities
211        require_once 'CRM/Activity/BAO/Activity.php';
212        $activities = CRM_Activity_BAO_Activity::getContactActivitySelector( $params );
213
214        require_once "CRM/Utils/JSON.php";
215        $iFilteredTotal = $iTotal =  $params['total'];
216        $selectorElements = array( 'activity_type', 'subject', 'source_contact',
217                                   'target_contact', 'assignee_contact',
218                                   'activity_date', 'status', 'links', 'class' );
219
220        echo CRM_Utils_JSON::encodeDataTableSelector( $activities, $sEcho, $iTotal, $iFilteredTotal, $selectorElements );
221        CRM_Utils_System::civiExit( );
222    }
223}