Merge branch '4.5' into 4.6
[civicrm-core.git] / CRM / Batch / Form / Entry.php
index 395185feca59e4337528fbee1854c2000cb62321..ec48e66409a10a2228663069eb7c70eba07078c5 100755 (executable)
 class CRM_Batch_Form_Entry extends CRM_Core_Form {
 
   /**
-   * Maximum profile fields that will be displayed
-   *
+   * Maximum profile fields that will be displayed.
    */
   protected $_rowCount = 1;
 
   /**
-   * Batch id
+   * Batch id.
    */
   protected $_batchId;
 
   /**
-   * Batch information
+   * Batch information.
    */
   protected $_batchInfo = array();
 
   /**
-   * Store the profile id associated with the batch type
+   * Store the profile id associated with the batch type.
    */
   protected $_profileId;
 
@@ -65,19 +64,19 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form {
 
   public $_params;
 
-  public $_membershipId = null;
+  public $_membershipId = NULL;
   /**
-   * When not to reset sort_name
+   * When not to reset sort_name.
    */
   protected $_preserveDefault = TRUE;
 
   /**
-   * Contact fields
+   * Contact fields.
    */
   protected $_contactFields = array();
 
   /**
-   * Fields array of fields in the batch profile
+   * Fields array of fields in the batch profile.
    * (based on the uf_field table data)
    * (this can't be protected as it is passed into the CRM_Contact_Form_Task_Batch::parseStreetAddress function
    * (although a future refactoring might hopefully change that so it uses the api & the function is not
@@ -85,8 +84,9 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form {
    * @var array
    */
   public $_fields = array();
+
   /**
-   * Build all the data structures needed to build the form
+   * Build all the data structures needed to build the form.
    *
    * @return void
    */
@@ -106,15 +106,15 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form {
       $this->_profileId = CRM_Batch_BAO_Batch::getProfileId($this->_batchInfo['type_id']);
     }
     CRM_Core_Resources::singleton()
-    ->addScriptFile('civicrm', 'templates/CRM/Batch/Form/Entry.js', 1, 'html-header')
-    ->addSetting(array('batch' => array('type_id' => $this->_batchInfo['type_id'])))
-    ->addSetting(array('setting' => array('monetaryThousandSeparator' => CRM_Core_Config::singleton()->monetaryThousandSeparator)))
-    ->addSetting(array('setting' => array('monetaryDecimalPoint' => CRM_Core_Config::singleton()->monetaryDecimalPoint)));
+      ->addScriptFile('civicrm', 'templates/CRM/Batch/Form/Entry.js', 1, 'html-header')
+      ->addSetting(array('batch' => array('type_id' => $this->_batchInfo['type_id'])))
+      ->addSetting(array('setting' => array('monetaryThousandSeparator' => CRM_Core_Config::singleton()->monetaryThousandSeparator)))
+      ->addSetting(array('setting' => array('monetaryDecimalPoint' => CRM_Core_Config::singleton()->monetaryDecimalPoint)));
 
   }
 
   /**
-   * Build the form object
+   * Build the form object.
    *
    *
    * @return void
@@ -126,20 +126,20 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form {
 
     $this->addElement('hidden', 'batch_id', $this->_batchId);
 
-    $batchTypes = CRM_Core_Pseudoconstant::get('CRM_Batch_DAO_Batch', 'type_id',array('flip' => 1), 'validate');
+    $batchTypes = CRM_Core_Pseudoconstant::get('CRM_Batch_DAO_Batch', 'type_id', array('flip' => 1), 'validate');
     // get the profile information
     if ($this->_batchInfo['type_id'] == $batchTypes['Contribution']) {
       CRM_Utils_System::setTitle(ts('Batch Data Entry for Contributions'));
       $customFields = CRM_Core_BAO_CustomField::getFields('Contribution');
     }
-   elseif ($this->_batchInfo['type_id'] == $batchTypes['Membership']) {
+    elseif ($this->_batchInfo['type_id'] == $batchTypes['Membership']) {
       CRM_Utils_System::setTitle(ts('Batch Data Entry for Memberships'));
       $customFields = CRM_Core_BAO_CustomField::getFields('Membership');
     }
-   elseif ($this->_batchInfo['type_id'] == $batchTypes['Pledge Payment']) {
-     CRM_Utils_System::setTitle(ts('Batch Data Entry for Pledge Payments'));
-     $customFields = CRM_Core_BAO_CustomField::getFields('Contribution');
-   }
+    elseif ($this->_batchInfo['type_id'] == $batchTypes['Pledge Payment']) {
+      CRM_Utils_System::setTitle(ts('Batch Data Entry for Pledge Payments'));
+      $customFields = CRM_Core_BAO_CustomField::getFields('Contribution');
+    }
     $this->_fields = array();
     $this->_fields = CRM_Core_BAO_UFGroup::getFields($this->_profileId, FALSE, CRM_Core_Action::VIEW);
 
@@ -175,12 +175,12 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form {
         array(
           'type' => 'upload',
           'name' => ts('Validate & Process the Batch'),
-          'isDefault' => TRUE
+          'isDefault' => TRUE,
         ),
         array(
           'type' => 'cancel',
           'name' => ts('Save & Continue Later'),
-        )
+        ),
       )
     );
 
@@ -188,7 +188,9 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form {
 
     $fileFieldExists = FALSE;
     $preserveDefaultsArray = array(
-      'first_name', 'last_name', 'middle_name',
+      'first_name',
+      'last_name',
+      'middle_name',
       'organization_name',
       'household_name',
     );
@@ -198,7 +200,10 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form {
     $config = CRM_Core_Config::singleton();
 
     for ($rowNumber = 1; $rowNumber <= $this->_batchInfo['item_count']; $rowNumber++) {
-      $this->addEntityRef("primary_contact_id[{$rowNumber}]", '', array('create' => TRUE, 'placeholder' => ts('- select -')));
+      $this->addEntityRef("primary_contact_id[{$rowNumber}]", '', array(
+          'create' => TRUE,
+          'placeholder' => ts('- select -'),
+        ));
 
       // special field specific to membership batch udpate
       if ($this->_batchInfo['type_id'] == 2) {
@@ -209,7 +214,7 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form {
         $this->add('select', "member_option[$rowNumber]", '', $options);
       }
       if ($this->_batchInfo['type_id'] == $batchTypes['Pledge Payment']) {
-        $options =  array('' => '-select-');
+        $options = array('' => '-select-');
         $optionTypes = array(
           '1' => ts('Adjust Pledge Payment Schedule?'),
           '2' => ts('Adjust Total Pledge Amount?'),
@@ -227,7 +232,7 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form {
         }
 
         $this->add('select', "open_pledges[$rowNumber]", '', $options);
-     }
+      }
 
       foreach ($this->_fields as $name => $field) {
         if (in_array($field['field_type'], $contactTypes)) {
@@ -244,10 +249,12 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form {
 
     $this->assign('fields', $this->_fields);
     CRM_Core_Resources::singleton()
-    ->addSetting(array('contact' => array(
-      'return' => implode(',', $contactReturnProperties),
-      'fieldmap' => array_flip($contactReturnProperties),
-    )));
+      ->addSetting(array(
+        'contact' => array(
+          'return' => implode(',', $contactReturnProperties),
+          'fieldmap' => array_flip($contactReturnProperties),
+        ),
+      ));
 
     // don't set the status message when form is submitted.
     $buttonName = $this->controller->getButtonName('submit');
@@ -258,7 +265,7 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form {
   }
 
   /**
-   * Form validations
+   * Form validations.
    *
    * @param array $params
    *   Posted values of the form.
@@ -267,8 +274,8 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form {
    * @param array $self
    *   Form object.
    *
-   * @return array list of errors to be posted back to the form
-   * @static
+   * @return array
+   *   list of errors to be posted back to the form
    */
   public static function formRule($params, $files, $self) {
     $errors = array();
@@ -321,7 +328,7 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form {
   }
 
   /**
-   * Override default cancel action
+   * Override default cancel action.
    */
   public function cancelAction() {
     // redirect to batch listing
@@ -341,21 +348,21 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form {
     }
 
     // for add mode set smart defaults
-    if ( $this->_action & CRM_Core_Action::ADD ) {
-      list( $currentDate, $currentTime ) = CRM_Utils_Date::setDateDefaults( NULL, 'activityDateTime' );
+    if ($this->_action & CRM_Core_Action::ADD) {
+      list($currentDate, $currentTime) = CRM_Utils_Date::setDateDefaults(NULL, 'activityDateTime');
 
       //get all status
       $allStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
-      $completeStatus = array_search( 'Completed', $allStatus );
+      $completeStatus = array_search('Completed', $allStatus);
       $specialFields = array(
         'join_date' => $currentDate,
         'receive_date' => $currentDate,
         'receive_date_time' => $currentTime,
-        'contribution_status_id' => $completeStatus
+        'contribution_status_id' => $completeStatus,
       );
 
       for ($rowNumber = 1; $rowNumber <= $this->_batchInfo['item_count']; $rowNumber++) {
-        foreach ($specialFields as $key => $value ) {
+        foreach ($specialFields as $key => $value) {
           $defaults['field'][$rowNumber][$key] = $value;
         }
       }
@@ -371,7 +378,7 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form {
   }
 
   /**
-   * Process the form after the input has been submitted and validated
+   * Process the form after the input has been submitted and validated.
    *
    *
    * @return void
@@ -406,7 +413,7 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form {
   }
 
   /**
-   * Process contribution records
+   * Process contribution records.
    *
    * @param array $params
    *   Associated array of submitted values.
@@ -463,7 +470,7 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form {
 
         foreach ($dates as $val) {
           if (!empty($value[$val])) {
-            $value[$val] = CRM_Utils_Date::processDate( $value[$val], $value[$val . '_time'], TRUE );
+            $value[$val] = CRM_Utils_Date::processDate($value[$val], $value[$val . '_time'], TRUE);
           }
         }
 
@@ -490,8 +497,8 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form {
         $value['skipRecentView'] = TRUE;
 
         // build line item params
-        $this->_priceSet['fields'][$priceFieldID]['options'][$priceFieldValueID ]['amount'] =  $value['total_amount'];
-        $value['price_'. $priceFieldID] = 1;
+        $this->_priceSet['fields'][$priceFieldID]['options'][$priceFieldValueID]['amount'] = $value['total_amount'];
+        $value['price_' . $priceFieldID] = 1;
 
         $lineItem = array();
         CRM_Price_BAO_PriceSet::processAmount($this->_priceSet['fields'], $value, $lineItem[$priceSetId]);
@@ -522,7 +529,7 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form {
           if (is_numeric($pledgeId)) {
             $result = CRM_Pledge_BAO_PledgePayment::getPledgePayments($pledgeId);
             $pledgePaymentId = 0;
-            foreach ($result as $key => $values ) {
+            foreach ($result as $key => $values) {
               if ($values['status'] != 'Completed') {
                 $pledgePaymentId = $values['id'];
                 break;
@@ -563,12 +570,12 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form {
 
         //send receipt mail.
         if ($contribution->id && !empty($value['send_receipt'])) {
-            // add the domain email id
-            $domainEmail = CRM_Core_BAO_Domain::getNameAndEmail();
-            $domainEmail = "$domainEmail[0] <$domainEmail[1]>";
-            $value['from_email_address'] = $domainEmail;
-            $value['contribution_id'] = $contribution->id;
-            CRM_Contribute_Form_AdditionalInfo::emailReceipt( $this, $value );
+          // add the domain email id
+          $domainEmail = CRM_Core_BAO_Domain::getNameAndEmail();
+          $domainEmail = "$domainEmail[0] <$domainEmail[1]>";
+          $value['from_email_address'] = $domainEmail;
+          $value['contribution_id'] = $contribution->id;
+          CRM_Contribute_Form_AdditionalInfo::emailReceipt($this, $value);
         }
       }
     }
@@ -576,7 +583,7 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form {
   }
 
   /**
-   * Process membership records
+   * Process membership records.
    *
    * @param array $params
    *   Associated array of submitted values.
@@ -588,14 +595,14 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form {
     $dateTypes = array(
       'join_date' => 'joinDate',
       'membership_start_date' => 'startDate',
-      'membership_end_date' => 'endDate'
+      'membership_end_date' => 'endDate',
     );
 
     $dates = array(
       'join_date',
       'start_date',
       'end_date',
-      'reminder_date'
+      'reminder_date',
     );
 
     // get the price set associated with offline memebership
@@ -697,7 +704,7 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form {
         }
 
         if (!empty($value['receive_date'])) {
-          $value['receive_date'] = CRM_Utils_Date::processDate( $value['receive_date'], $value['receive_date_time'] , TRUE );
+          $value['receive_date'] = CRM_Utils_Date::processDate($value['receive_date'], $value['receive_date_time'], TRUE);
         }
 
         $params['actualBatchTotal'] += $value['total_amount'];
@@ -712,7 +719,7 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form {
 
         $editedFieldParams = array(
           'price_set_id' => $priceSetId,
-          'name' => $value['membership_type'][0]
+          'name' => $value['membership_type'][0],
         );
 
         $editedResults = array();
@@ -725,7 +732,7 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form {
           $fid = $editedResults['id'];
           $editedFieldParams = array(
             'price_field_id' => $editedResults['id'],
-            'membership_type_id' => $value['membership_type_id']
+            'membership_type_id' => $value['membership_type_id'],
           );
 
           $editedResults = array();
@@ -761,10 +768,10 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form {
         unset($value['membership_start_date']);
         unset($value['membership_end_date']);
 
-        $value['is_renew'] = false;
-        if (!empty($params['member_option']) && CRM_Utils_Array::value( $key, $params['member_option'] ) == 2 ) {
+        $value['is_renew'] = FALSE;
+        if (!empty($params['member_option']) && CRM_Utils_Array::value($key, $params['member_option']) == 2) {
           $this->_params = $params;
-          $value['is_renew'] = true;
+          $value['is_renew'] = TRUE;
           $membership = CRM_Member_BAO_Membership::renewMembershipFormWrapper(
             $value['contact_id'],
             $value['membership_type_id'],
@@ -773,7 +780,8 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form {
           );
 
           // make contribution entry
-          CRM_Member_BAO_Membership::recordMembershipContribution( array_merge($value, array('membership_id' => $membership->id)));
+          $contrbutionParams = array_merge($value, array('membership_id' => $membership->id));
+          CRM_Member_BAO_Membership::recordMembershipContribution($contrbutionParams);
         }
         else {
           $membership = CRM_Member_BAO_Membership::create($value, CRM_Core_DAO::$_nullArray);
@@ -802,16 +810,16 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form {
         // end of premium
 
         //send receipt mail.
-        if ( $membership->id && !empty($value['send_receipt'])) {
+        if ($membership->id && !empty($value['send_receipt'])) {
 
-            // add the domain email id
-            $domainEmail = CRM_Core_BAO_Domain::getNameAndEmail();
-            $domainEmail = "$domainEmail[0] <$domainEmail[1]>";
+          // add the domain email id
+          $domainEmail = CRM_Core_BAO_Domain::getNameAndEmail();
+          $domainEmail = "$domainEmail[0] <$domainEmail[1]>";
 
-            $value['from_email_address'] = $domainEmail;
-            $value['membership_id']      = $membership->id;
-            $value['contribution_id'] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipPayment', $membership->id, 'contribution_id', 'membership_id');
-            CRM_Member_Form_Membership::emailReceipt( $this, $value, $membership );
+          $value['from_email_address'] = $domainEmail;
+          $value['membership_id'] = $membership->id;
+          $value['contribution_id'] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipPayment', $membership->id, 'contribution_id', 'membership_id');
+          CRM_Member_Form_Membership::emailReceipt($this, $value, $membership);
         }
       }
     }
@@ -819,7 +827,7 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form {
   }
 
   /**
-   * Update contact information
+   * Update contact information.
    *
    * @param array $value
    *   Associated array of submitted values.
@@ -863,4 +871,5 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form {
   public function testProcessContribution($params) {
     return $this->processContribution($params);
   }
+
 }