some comment fixes
[civicrm-core.git] / CRM / Export / BAO / Export.php
index 564d95de0337e57589da96efe15242dd4e2c57d1..d991ee3b01603b6da9ffa4f287e717a0db2fee45 100644 (file)
@@ -23,7 +23,7 @@
  | GNU Affero General Public License or the licensing of CiviCRM,     |
  | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
  +--------------------------------------------------------------------+
-*/
+ */
 
 /**
  *
@@ -46,24 +46,35 @@ class CRM_Export_BAO_Export {
   /**
    * Get the list the export fields
    *
-   * @param int $selectAll user preference while export
-   * @param array $ids contact ids
-   * @param array $params associated array of fields
-   * @param string $order order by clause
-   * @param array $fields associated array of fields
-   * @param array $moreReturnProperties additional return fields
-   * @param int $exportMode export mode
-   * @param string $componentClause component clause
-   * @param string $componentTable component table
-   * @param bool $mergeSameAddress merge records if they have same address
-   * @param bool $mergeSameHousehold merge records if they belong to the same household
+   * @param int $selectAll
+   *   User preference while export.
+   * @param array $ids
+   *   Contact ids.
+   * @param array $params
+   *   Associated array of fields.
+   * @param string $order
+   *   Order by clause.
+   * @param array $fields
+   *   Associated array of fields.
+   * @param array $moreReturnProperties
+   *   Additional return fields.
+   * @param int $exportMode
+   *   Export mode.
+   * @param string $componentClause
+   *   Component clause.
+   * @param string $componentTable
+   *   Component table.
+   * @param bool $mergeSameAddress
+   *   Merge records if they have same address.
+   * @param bool $mergeSameHousehold
+   *   Merge records if they belong to the same household.
    *
    * @param array $exportParams
    * @param string $queryOperator
    *
-   * @static
    */
-  static function exportComponents($selectAll,
+  public static function exportComponents(
+    $selectAll,
     $ids,
     $params,
     $order = NULL,
@@ -78,7 +89,7 @@ class CRM_Export_BAO_Export {
     $queryOperator = 'AND'
   ) {
     $headerRows = $returnProperties = array();
-    $primary    = $paymentFields    = $selectedPaymentFields = FALSE;
+    $primary = $paymentFields = $selectedPaymentFields = FALSE;
     $origFields = $fields;
     $relationField = NULL;
 
@@ -223,13 +234,13 @@ class CRM_Export_BAO_Export {
           if ($fieldName == 'event_id') {
             $returnProperties['event_id'] = 1;
           }
-          else if (
+          elseif (
             $exportMode == CRM_Export_Form_Select::EVENT_EXPORT &&
-            array_key_exists( $fieldName, self::componentPaymentFields())
+            array_key_exists($fieldName, self::componentPaymentFields())
           ) {
-              $selectedPaymentFields = true;
-              $paymentTableId = 'participant_id';
-              $returnProperties[$fieldName] = 1;
+            $selectedPaymentFields = TRUE;
+            $paymentTableId = 'participant_id';
+            $returnProperties[$fieldName] = 1;
           }
           else {
             $returnProperties[$fieldName] = 1;
@@ -311,8 +322,7 @@ class CRM_Export_BAO_Export {
         $componentReturnProperties = CRM_Contact_BAO_Query::defaultReturnProperties($queryMode);
         if ($queryMode == CRM_Contact_BAO_Query::MODE_CONTRIBUTE) {
           // soft credit columns are not automatically populated, because contribution search doesn't require them by default
-          $componentReturnProperties =
-            array_merge(
+          $componentReturnProperties = array_merge(
               $componentReturnProperties,
               CRM_Contribute_BAO_Query::softCreditReturnProperties(TRUE));
         }
@@ -324,8 +334,12 @@ class CRM_Export_BAO_Export {
 
         // unset non exportable fields for components
         $nonExpoFields = array(
-          'groups', 'tags', 'notes', 'contribution_status_id',
-          'pledge_status_id', 'pledge_payment_status_id',
+          'groups',
+          'tags',
+          'notes',
+          'contribution_status_id',
+          'pledge_status_id',
+          'pledge_payment_status_id',
         );
         foreach ($nonExpoFields as $value) {
           unset($returnProperties[$value]);
@@ -503,11 +517,11 @@ INSERT INTO {$componentTable} SELECT distinct gc.contact_id FROM civicrm_group_c
                 {$relationshipJoin} ";
 
         //check for active relationship status only
-        $today               = date('Ymd');
-        $relationActive      = " AND (crel.is_active = 1 AND ( crel.end_date is NULL OR crel.end_date >= {$today} ) )";
-        $relationWhere       = " WHERE contact_a.is_deleted = 0 {$relationshipClause} {$relationActive}";
-        $relationGroupBy     = " GROUP BY crel.{$contactA}";
-        $relationSelect      = "{$relationSelect}, {$contactA} as refContact ";
+        $today = date('Ymd');
+        $relationActive = " AND (crel.is_active = 1 AND ( crel.end_date is NULL OR crel.end_date >= {$today} ) )";
+        $relationWhere = " WHERE contact_a.is_deleted = 0 {$relationshipClause} {$relationActive}";
+        $relationGroupBy = " GROUP BY crel.{$contactA}";
+        $relationSelect = "{$relationSelect}, {$contactA} as refContact ";
         $relationQueryString = "$relationSelect $relationFrom $relationWhere $relationHaving $relationGroupBy";
 
         $allRelContactDAO = CRM_Core_DAO::executeQuery($relationQueryString);
@@ -908,15 +922,19 @@ INSERT INTO {$componentTable} SELECT distinct gc.contact_id FROM civicrm_group_c
               }
               // CRM-13995
               elseif (is_object($relDAO) && in_array($relationField, array(
-                'email_greeting', 'postal_greeting', 'addressee'))) {
+                  'email_greeting',
+                  'postal_greeting',
+                  'addressee',
+                ))
+              ) {
                 //special case for greeting replacement
                 $fldValue = "{$relationField}_display";
                 $fieldValue = $relDAO->$fldValue;
               }
-              elseif ( is_object($relDAO) && $relationField == 'state_province' ) {
+              elseif (is_object($relDAO) && $relationField == 'state_province') {
                 $fieldValue = CRM_Core_PseudoConstant::stateProvince($relDAO->state_province_id);
               }
-              elseif ( is_object($relDAO) && $relationField == 'country' ) {
+              elseif (is_object($relDAO) && $relationField == 'country') {
                 $fieldValue = CRM_Core_PseudoConstant::country($relDAO->country_id);
               }
               else {
@@ -933,7 +951,7 @@ INSERT INTO {$componentTable} SELECT distinct gc.contact_id FROM civicrm_group_c
               if (is_object($relDAO) && $relationField == 'id') {
                 $row[$field . $relationField] = $relDAO->contact_id;
               }
-              elseif (is_array( $relationValue ) && $relationField == 'location') {
+              elseif (is_array($relationValue) && $relationField == 'location') {
                 foreach ($relationValue as $ltype => $val) {
                   foreach (array_keys($val) as $fld) {
                     $type = explode('-', $fld);
@@ -944,7 +962,7 @@ INSERT INTO {$componentTable} SELECT distinct gc.contact_id FROM civicrm_group_c
                     // CRM-3157: localise country, region (both have ‘country’ context)
                     // and state_province (‘province’ context)
                     switch (TRUE) {
-                    case (!is_object($relDAO)):
+                      case (!is_object($relDAO)):
                         $row[$field . '_' . $fldValue] = '';
                         break;
 
@@ -1025,7 +1043,10 @@ INSERT INTO {$componentTable} SELECT distinct gc.contact_id FROM civicrm_group_c
             }
 
             elseif (in_array($field, array(
-              'email_greeting', 'postal_greeting', 'addressee'))) {
+              'email_greeting',
+              'postal_greeting',
+              'addressee',
+            ))) {
               //special case for greeting replacement
               $fldValue = "{$field}_display";
               $row[$field] = $iterationDAO->$fldValue;
@@ -1058,11 +1079,11 @@ INSERT INTO {$componentTable} SELECT distinct gc.contact_id FROM civicrm_group_c
           elseif ($selectedPaymentFields && array_key_exists($field, self::componentPaymentFields())) {
             $paymentData = CRM_Utils_Array::value($iterationDAO->$paymentTableId, $paymentDetails);
             $payFieldMapper = array(
-              'componentPaymentField_total_amount'        => 'total_amount',
+              'componentPaymentField_total_amount' => 'total_amount',
               'componentPaymentField_contribution_status' => 'contribution_status',
-              'componentPaymentField_payment_instrument'  => 'pay_instru',
-              'componentPaymentField_transaction_id'      => 'trxn_id',
-              'componentPaymentField_received_date'       => 'receive_date',
+              'componentPaymentField_payment_instrument' => 'pay_instru',
+              'componentPaymentField_transaction_id' => 'trxn_id',
+              'componentPaymentField_received_date' => 'receive_date',
             );
             $row[$field] = CRM_Utils_Array::value($payFieldMapper[$field], $paymentData, '');
           }
@@ -1172,10 +1193,13 @@ INSERT INTO {$componentTable} SELECT distinct gc.contact_id FROM civicrm_group_c
   /**
    * Name of the export file based on mode
    *
-   * @param string  $output type of output
-   * @param int     $mode export mode
+   * @param string $output
+   *   Type of output.
+   * @param int $mode
+   *   Export mode.
    *
-   * @return string name of the file
+   * @return string
+   *   name of the file
    */
   public static function getExportFileName($output = 'csv', $mode = CRM_Export_Form_Select::CONTACT_EXPORT) {
     switch ($mode) {
@@ -1207,7 +1231,6 @@ INSERT INTO {$componentTable} SELECT distinct gc.contact_id FROM civicrm_group_c
 
   /**
    * Handle import error file creation.
-   *
    */
   public static function invoke() {
     $type = CRM_Utils_Request::retrieve('type', 'Positive', CRM_Core_DAO::$_nullObject);
@@ -1225,7 +1248,7 @@ INSERT INTO {$componentTable} SELECT distinct gc.contact_id FROM civicrm_group_c
     if ($parserClass[0] == 'CRM' &&
       count($parserClass) >= 3
     ) {
-      require_once (str_replace('_', DIRECTORY_SEPARATOR, $parserName) . ".php");
+      require_once str_replace('_', DIRECTORY_SEPARATOR, $parserName) . ".php";
       // ensure the functions exists
       if (method_exists($parserName, 'errorFileName') &&
         method_exists($parserName, 'saveFileName')
@@ -1254,10 +1277,10 @@ INSERT INTO {$componentTable} SELECT distinct gc.contact_id FROM civicrm_group_c
   public static function exportCustom($customSearchClass, $formValues, $order) {
     $ext = CRM_Extension_System::singleton()->getMapper();
     if (!$ext->isExtensionClass($customSearchClass)) {
-      require_once (str_replace('_', DIRECTORY_SEPARATOR, $customSearchClass) . '.php');
+      require_once str_replace('_', DIRECTORY_SEPARATOR, $customSearchClass) . '.php';
     }
     else {
-      require_once ($ext->classToPath($customSearchClass));
+      require_once $ext->classToPath($customSearchClass);
     }
     $search = new $customSearchClass($formValues);
 
@@ -1273,8 +1296,8 @@ INSERT INTO {$componentTable} SELECT distinct gc.contact_id FROM civicrm_group_c
     $header = array_keys($columns);
     $fields = array_values($columns);
 
-    $rows     = array();
-    $dao      = CRM_Core_DAO::executeQuery($sql);
+    $rows = array();
+    $dao = CRM_Core_DAO::executeQuery($sql);
     $alterRow = FALSE;
     if (method_exists($search, 'alterRow')) {
       $alterRow = TRUE;
@@ -1368,7 +1391,7 @@ INSERT INTO {$componentTable} SELECT distinct gc.contact_id FROM civicrm_group_c
         // to accomodate different systems - CRM-13739
         static $notRealIDFields = NULL;
         if ($notRealIDFields == NULL) {
-          $notRealIDFields = array( 'trxn_id', 'componentpaymentfield_transaction_id' );
+          $notRealIDFields = array('trxn_id', 'componentpaymentfield_transaction_id');
         }
 
         if (in_array($fieldName, $notRealIDFields)) {
@@ -1385,7 +1408,7 @@ INSERT INTO {$componentTable} SELECT distinct gc.contact_id FROM civicrm_group_c
         $changeFields = array(
           'groups',
           'tags',
-          'notes'
+          'notes',
         );
 
         if (in_array($fieldName, $changeFields)) {
@@ -1597,7 +1620,8 @@ UPDATE $tableName
 SET    addressee = %1, postal_greeting = %2, email_greeting = %3
 WHERE  id = %4
 ";
-      $params = array(1 => array($values['addressee'], 'String'),
+      $params = array(
+        1 => array($values['addressee'], 'String'),
         2 => array($values['postalGreeting'], 'String'),
         3 => array($values['emailGreeting'], 'String'),
         4 => array($masterID, 'Integer'),
@@ -1605,9 +1629,9 @@ WHERE  id = %4
       CRM_Core_DAO::executeQuery($sql, $params);
 
       // delete all copies
-      $deleteIDs      = array_keys($values['copy']);
+      $deleteIDs = array_keys($values['copy']);
       $deleteIDString = implode(',', $deleteIDs);
-      $sql            = "
+      $sql = "
 DELETE FROM $tableName
 WHERE  id IN ( $deleteIDString )
 ";
@@ -1666,9 +1690,9 @@ WHERE  id IN ( $deleteIDString )
    * The function unsets static part of the string, if token is the dynamic part.
    * Example: 'Hello {contact.first_name}' => converted to => '{contact.first_name}'
    * i.e 'Hello Alan' => converted to => 'Alan'
-   *
    */
-  static function _trimNonTokens(&$parsedString, $defaultGreeting,
+  public static function _trimNonTokens(
+    &$parsedString, $defaultGreeting,
     $addressMergeGreetings, $greetingType = 'postal_greeting'
   ) {
     if (!empty($addressMergeGreetings[$greetingType])) {
@@ -1794,16 +1818,20 @@ WHERE  id IN ( $deleteIDString )
    * Merge household record into the individual record
    * if exists
    *
-   * @param string $exportTempTable temporary temp table that stores the records
-   * @param array  $headerRows array of headers for the export file
-   * @param array  $sqlColumns array of names of the table columns of the temp table
-   * @param string $prefix name of the relationship type that is prefixed to the table columns
+   * @param string $exportTempTable
+   *   Temporary temp table that stores the records.
+   * @param array $headerRows
+   *   Array of headers for the export file.
+   * @param array $sqlColumns
+   *   Array of names of the table columns of the temp table.
+   * @param string $prefix
+   *   Name of the relationship type that is prefixed to the table columns.
    */
   public static function mergeSameHousehold($exportTempTable, &$headerRows, &$sqlColumns, $prefix) {
     $prefixColumn = $prefix . '_';
-    $allKeys      = array_keys($sqlColumns);
-    $replaced     = array();
-    $headerRows   = array_values($headerRows);
+    $allKeys = array_keys($sqlColumns);
+    $replaced = array();
+    $headerRows = array_values($headerRows);
 
     // name map of the non standard fields in header rows & sql columns
     $mappingFields = array(
@@ -1813,7 +1841,7 @@ WHERE  id IN ( $deleteIDString )
       'contact_is_deleted' => 'is_deleted',
       'name' => 'address_name',
       'provider_id' => 'im_service_provider',
-      'phone_type_id' => 'phone_type'
+      'phone_type_id' => 'phone_type',
     );
 
     //figure out which columns are to be replaced by which ones
@@ -1880,10 +1908,10 @@ GROUP BY civicrm_primary_id ";
    * @param null $saveFile
    * @param string $batchItems
    */
-  public static function writeCSVFromTable($exportTempTable, $headerRows, $sqlColumns, $exportMode, $saveFile = null, $batchItems = '') {
+  public static function writeCSVFromTable($exportTempTable, $headerRows, $sqlColumns, $exportMode, $saveFile = NULL, $batchItems = '') {
     $writeHeader = TRUE;
-    $offset      = 0;
-    $limit       = self::EXPORT_ROW_COUNT;
+    $offset = 0;
+    $limit = self::EXPORT_ROW_COUNT;
 
     $query = "SELECT * FROM $exportTempTable";
 
@@ -1906,31 +1934,30 @@ LIMIT $offset, $limit
         }
         $componentDetails[] = $row;
       }
-      if ( $exportMode == 'financial' ) {
+      if ($exportMode == 'financial') {
         $getExportFileName = 'CiviCRM Contribution Search';
       }
       else {
-        $getExportFileName =self::getExportFileName( 'csv', $exportMode );
+        $getExportFileName = self::getExportFileName('csv', $exportMode);
       }
-      $csvRows = CRM_Core_Report_Excel::writeCSVFile( $getExportFileName,
+      $csvRows = CRM_Core_Report_Excel::writeCSVFile($getExportFileName,
         $headerRows,
         $componentDetails,
-        null,
+        NULL,
         $writeHeader,
-        $saveFile );
+        $saveFile);
 
       if ($saveFile && !empty($csvRows)) {
         $batchItems .= $csvRows;
       }
 
-      $writeHeader = false;
+      $writeHeader = FALSE;
       $offset += $limit;
     }
   }
 
   /**
    * Manipulate header rows for relationship fields
-   *
    */
   public static function manipulateHeaderRows(&$headerRows, $contactRelationshipTypes) {
     foreach ($headerRows as & $header) {
@@ -1945,7 +1972,6 @@ LIMIT $offset, $limit
   /**
    * Exclude contacts who are deceased, have "Do not mail" privacy setting,
    * or have no street address
-   *
    */
   public static function postalMailingFormat($exportTempTable, &$headerRows, &$sqlColumns, $exportParams) {
     $whereClause = array();
@@ -2001,15 +2027,16 @@ WHERE  {$whereClause}";
    */
   public static function componentPaymentFields() {
     static $componentPaymentFields;
-    if (!isset( $componentPaymentFields)) {
+    if (!isset($componentPaymentFields)) {
       $componentPaymentFields = array(
-        'componentPaymentField_total_amount'        => ts('Total Amount'),
+        'componentPaymentField_total_amount' => ts('Total Amount'),
         'componentPaymentField_contribution_status' => ts('Contribution Status'),
-        'componentPaymentField_received_date'       => ts('Received Date'),
-        'componentPaymentField_payment_instrument'  => ts('Payment Instrument'),
-        'componentPaymentField_transaction_id'      => ts('Transaction ID'),
+        'componentPaymentField_received_date' => ts('Received Date'),
+        'componentPaymentField_payment_instrument' => ts('Payment Instrument'),
+        'componentPaymentField_transaction_id' => ts('Transaction ID'),
       );
     }
     return $componentPaymentFields;
   }
+
 }