Test fixes for All kind of Custom Values
[civicrm-core.git] / CRM / Core / BAO / RecurringEntity.php
index 0b74ca82d11c9263282863c50707eaf0f32c807e..dae4dd10d3860e414f07b86abb0927d68603561c 100644 (file)
@@ -3,7 +3,7 @@
  +--------------------------------------------------------------------+
  | CiviCRM version 4.6                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2014                                |
+ | Copyright CiviCRM LLC (c) 2004-2015                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
@@ -28,7 +28,7 @@
 /**
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2014
+ * @copyright CiviCRM LLC (c) 2004-2015
  * $Id$
  *
  */
@@ -128,25 +128,34 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity {
       ),
     );
 
+  /**
+   * Getter for status.
+   *
+   * @return string
+   */
   public static function getStatus() {
     return self::$status;
   }
 
+  /**
+   * Setter for status.
+   *
+   * @param string $status
+   */
   public static function setStatus($status) {
     self::$status = $status;
   }
 
   /**
-   * Save records in civicrm_recujrring_entity table
+   * Save records in civicrm_recurring_entity table.
    *
    * @param array $params
-   *   Reference array contains the values submitted by the form .
-   *
+   *   Reference array contains the values submitted by the form.
    *
    * @return object
    */
   public static function add(&$params) {
-    if (CRM_Utils_Array::value('id', $params)) {
+    if (!empty($params['id'])) {
       CRM_Utils_Hook::pre('edit', 'RecurringEntity', $params['id'], $params);
     }
     else {
@@ -158,7 +167,7 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity {
     $daoRecurringEntity->find(TRUE);
     $result = $daoRecurringEntity->save();
 
-    if (CRM_Utils_Array::value('id', $params)) {
+    if (!empty($params['id'])) {
       CRM_Utils_Hook::post('edit', 'RecurringEntity', $daoRecurringEntity->id, $daoRecurringEntity);
     }
     else {
@@ -191,7 +200,7 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity {
   }
 
   /**
-   * This function updates the mode column in the civicrm_recurring_entity table
+   * This function updates the mode column in the civicrm_recurring_entity table.
    *
    * @param int $mode
    *   Mode of the entity to cascade changes across parent/child relations eg 1 - only this entity, 2 - this and the following entities, 3 - All the entity .
@@ -213,7 +222,7 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity {
   }
 
   /**
-   * This function generates all new entities based on object vars
+   * This function generates all new entities based on object vars.
    *
    * @return array
    */
@@ -250,7 +259,7 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity {
   }
 
   /**
-   * Generate new DAOs and along with entries in civicrm_recurring_entity table
+   * Generate new DAOs and along with entries in civicrm_recurring_entity table.
    *
    * @return array
    */
@@ -352,8 +361,8 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity {
           $recursionDates[$count][$col] = CRM_Utils_Date::processDate($newDate->format('Y-m-d H:i:s'));
         }
         if ($exRangeStart) {
-          $exRangeStartDate = CRM_Utils_Date::processDate($recursionDates[$count][$exRangeStart], NULL, FALSE, 'Ymd');
-          $exRangeEndDate = CRM_Utils_Date::processDate($recursionDates[$count][$exRangeEnd], NULL, FALSE, 'Ymd');
+          $exRangeStartDate = CRM_Utils_Date::processDate(CRM_Utils_Array::value($exRangeStart, $recursionDates[$count]), NULL, FALSE, 'Ymd');
+          $exRangeEndDate = CRM_Utils_Date::processDate(CRM_Utils_Array::value($exRangeEnd, $recursionDates[$count]), NULL, FALSE, 'Ymd');
         }
 
         foreach ($this->excludeDates as $exDate) {
@@ -392,7 +401,7 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity {
   }
 
   /**
-   * This function gets all the children for a particular parent entity
+   * This function gets all the children for a particular parent entity.
    *
    * @param int $parentId
    *   Parent entity id .
@@ -488,7 +497,7 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity {
   }
 
   /**
-   * This function gets the parent for the entity id passed to it
+   * This function gets the parent for the entity id passed to it.
    *
    * @param int $entityId
    *   Entity ID .
@@ -524,7 +533,41 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity {
   }
 
   /**
-   * This function copies the information from parent entity and creates other entities with same information
+   * Finds the position of this entity as well as total count of the repeating set
+   *
+   * @param $entityId
+   * @param $entityTable
+   * @return array|null
+   */
+  static public function getPositionAndCount($entityId, $entityTable) {
+    $position = $count = 0;
+
+    $query = "
+      SELECT entity_id
+      FROM civicrm_recurring_entity
+      WHERE parent_id = (SELECT parent_id FROM civicrm_recurring_entity WHERE entity_id = %1 AND entity_table = %2) AND entity_table = %2";
+
+    $dao = CRM_Core_DAO::executeQuery($query,
+      array(
+        1 => array($entityId, 'Integer'),
+        2 => array($entityTable, 'String'),
+      )
+    );
+
+    while ($dao->fetch()) {
+      ++$count;
+      if ($dao->entity_id <= $entityId) {
+        ++$position;
+      }
+    }
+    if ($count) {
+      return array($position, $count);
+    }
+    return NULL;
+  }
+
+  /**
+   * This function copies the information from parent entity and creates other entities with same information.
    *
    * @param string $entityTable
    *   Entity table name .
@@ -825,7 +868,7 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity {
   }
 
   /**
-   * This function maps values posted from form to civicrm_action_schedule columns
+   * This function maps values posted from form to civicrm_action_schedule columns.
    *
    * @param array $formParams
    *   And array of form values posted .
@@ -834,39 +877,39 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity {
    */
   public function mapFormValuesToDB($formParams = array()) {
     $dbParams = array();
-    if (CRM_Utils_Array::value('used_for', $formParams)) {
+    if (!empty($formParams['used_for'])) {
       $dbParams['used_for'] = $formParams['used_for'];
     }
 
-    if (CRM_Utils_Array::value('entity_id', $formParams)) {
+    if (!empty($formParams['entity_id'])) {
       $dbParams['entity_value'] = $formParams['entity_id'];
     }
 
-    if (CRM_Utils_Array::value('repetition_start_date', $formParams)) {
-      if (CRM_Utils_Array::value('repetition_start_date_display', $formParams)) {
+    if (!empty($formParams['repetition_start_date'])) {
+      if (!empty($formParams['repetition_start_date_display'])) {
         $repetitionStartDate = $formParams['repetition_start_date_display'];
       }
       else {
         $repetitionStartDate = $formParams['repetition_start_date'];
       }
-      if (CRM_Utils_Array::value('repetition_start_date_time', $formParams)) {
+      if (!empty($formParams['repetition_start_date_time'])) {
         $repetitionStartDate = $repetitionStartDate . " " . $formParams['repetition_start_date_time'];
       }
       $repetition_start_date = new DateTime($repetitionStartDate);
       $dbParams['start_action_date'] = CRM_Utils_Date::processDate($repetition_start_date->format('Y-m-d H:i:s'));
     }
 
-    if (CRM_Utils_Array::value('repetition_frequency_unit', $formParams)) {
+    if (!empty($formParams['repetition_frequency_unit'])) {
       $dbParams['repetition_frequency_unit'] = $formParams['repetition_frequency_unit'];
     }
 
-    if (CRM_Utils_Array::value('repetition_frequency_interval', $formParams)) {
+    if (!empty($formParams['repetition_frequency_interval'])) {
       $dbParams['repetition_frequency_interval'] = $formParams['repetition_frequency_interval'];
     }
 
     //For Repeats on:(weekly case)
     if ($formParams['repetition_frequency_unit'] == 'week') {
-      if (CRM_Utils_Array::value('start_action_condition', $formParams)) {
+      if (!empty($formParams['start_action_condition'])) {
         $repeats_on = CRM_Utils_Array::value('start_action_condition', $formParams);
         $dbParams['start_action_condition'] = implode(",", array_keys($repeats_on));
       }
@@ -875,7 +918,7 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity {
     //For Repeats By:(monthly case)
     if ($formParams['repetition_frequency_unit'] == 'month') {
       if ($formParams['repeats_by'] == 1) {
-        if (CRM_Utils_Array::value('limit_to', $formParams)) {
+        if (!empty($formParams['limit_to'])) {
           $dbParams['limit_to'] = $formParams['limit_to'];
         }
       }
@@ -888,14 +931,14 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity {
 
     //For "Ends" - After:
     if ($formParams['ends'] == 1) {
-      if (CRM_Utils_Array::value('start_action_offset', $formParams)) {
+      if (!empty($formParams['start_action_offset'])) {
         $dbParams['start_action_offset'] = $formParams['start_action_offset'];
       }
     }
 
     //For "Ends" - On:
     if ($formParams['ends'] == 2) {
-      if (CRM_Utils_Array::value('repeat_absolute_date', $formParams)) {
+      if (!empty($formParams['repeat_absolute_date'])) {
         $dbParams['absolute_date'] = CRM_Utils_Date::processDate($formParams['repeat_absolute_date']);
       }
     }
@@ -928,7 +971,7 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity {
   }
 
   /**
-   * wrapper of getScheduleReminderDetailsById function
+   * wrapper of getScheduleReminderDetailsById function.
    *
    * @param int $scheduleReminderId
    *   Primary key of civicrm_action_schedule table .
@@ -1037,10 +1080,11 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity {
         $r->count($scheduleReminderDetails['start_action_offset']);
       }
 
-      if (CRM_Utils_Array::value('absolute_date', $scheduleReminderDetails)) {
+      if (!empty($scheduleReminderDetails['absolute_date'])) {
         $absoluteDate = CRM_Utils_Date::setDateDefaults($scheduleReminderDetails['absolute_date']);
-        $endDate = new DateTime($absoluteDate[0] . ' ' . $absoluteDate[1]);
-        $endDate->modify('+1 day');
+        // absolute_date column of scheduled-reminder table is of type date (and not datetime)
+        // and we always want the date to be included, and therefore appending 23:59
+        $endDate = new DateTime($absoluteDate[0] . ' ' . '23:59');
         $r->until($endDate);
       }
 
@@ -1056,7 +1100,7 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity {
 
 
   /**
-   * This function gets time difference between the two datetime object
+   * This function gets time difference between the two datetime object.
    *
    * @param DateTime $startDate
    *   Start Date .
@@ -1076,7 +1120,7 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity {
   }
 
   /**
-   * This function gets all columns from civicrm_action_schedule on the basis of event id
+   * This function gets all columns from civicrm_action_schedule on the basis of event id.
    *
    * @param int $entityId
    *   Entity ID .
@@ -1106,7 +1150,7 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity {
   }
 
   /**
-   * Update mode column in civicrm_recurring_entity table for event related tabs
+   * Update mode column in civicrm_recurring_entity table for event related tabs.
    *
    * @param int $entityId
    *   Event id .
@@ -1131,7 +1175,7 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity {
       );
       $defaults = array();
       CRM_Core_DAO::commonRetrieve($dao, $params, $defaults);
-      if (CRM_Utils_Array::value('id', $defaults)) {
+      if (!empty($defaults['id'])) {
         $result['entityId'] = $defaults['id'];
         $result['entityTable'] = $entityTable;
       }