Merge pull request #4962 from totten/master-angular-ts
[civicrm-core.git] / CRM / Core / BAO / RecurringEntity.php
index 3c52ea7676d38c1a8bcc32ef78e8d3f1985fc230..f214e164eed49e8b311a91399d526a9a33068eb2 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        |
  +--------------------------------------------------------------------+
-*/
+ */
 
 /**
  *
@@ -58,70 +58,70 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity {
 
   public static $status = NULL;
 
-  static $_recurringEntityHelper =
-    array(
+  static $_recurringEntityHelper
+    array(
       'civicrm_event' => array(
         'helper_class' => 'CRM_Event_DAO_Event',
         'delete_func' => 'delete',
-        'pre_delete_func' => 'CRM_Event_Form_ManageEvent_Repeat::checkRegistrationForEvents'
+        'pre_delete_func' => 'CRM_Event_Form_ManageEvent_Repeat::checkRegistrationForEvents',
       ),
       'civicrm_activity' => array(
         'helper_class' => 'CRM_Activity_DAO_Activity',
         'delete_func' => 'delete',
-        'pre_delete_func' => ''
-      )
+        'pre_delete_func' => '',
+      ),
     );
 
-  static $_dateColumns =
-    array(
+  static $_dateColumns
+    array(
       'civicrm_event' => array(
         'dateColumns' => array('start_date'),
         'excludeDateRangeColumns' => array('start_date', 'end_date'),
-        'intervalDateColumns' => array('end_date')
+        'intervalDateColumns' => array('end_date'),
       ),
       'civicrm_activity' => array(
         'dateColumns' => array('activity_date_time'),
-      )
+      ),
     );
 
-  static $_tableDAOMapper =
-    array(
-      'civicrm_event'       => 'CRM_Event_DAO_Event',
+  static $_tableDAOMapper
+    array(
+      'civicrm_event' => 'CRM_Event_DAO_Event',
       'civicrm_price_set_entity' => 'CRM_Price_DAO_PriceSetEntity',
-      'civicrm_uf_join'     => 'CRM_Core_DAO_UFJoin',
+      'civicrm_uf_join' => 'CRM_Core_DAO_UFJoin',
       'civicrm_tell_friend' => 'CRM_Friend_DAO_Friend',
-      'civicrm_pcp_block'   => 'CRM_PCP_DAO_PCPBlock',
-      'civicrm_activity'    => 'CRM_Activity_DAO_Activity',
-      'civicrm_activity_contact'  => 'CRM_Activity_DAO_ActivityContact',
+      'civicrm_pcp_block' => 'CRM_PCP_DAO_PCPBlock',
+      'civicrm_activity' => 'CRM_Activity_DAO_Activity',
+      'civicrm_activity_contact' => 'CRM_Activity_DAO_ActivityContact',
     );
 
-  static $_updateSkipFields =
-    array(
-      'civicrm_event'       => array('start_date', 'end_date'),
+  static $_updateSkipFields
+    array(
+      'civicrm_event' => array('start_date', 'end_date'),
       'civicrm_tell_friend' => array('entity_id'),
-      'civicrm_pcp_block'   => array('entity_id'),
-      'civicrm_activity'    => array('activity_date_time'),
+      'civicrm_pcp_block' => array('entity_id'),
+      'civicrm_activity' => array('activity_date_time'),
     );
 
-  static $_linkedEntitiesInfo =
-    array(
+  static $_linkedEntitiesInfo
+    array(
       'civicrm_tell_friend' => array(
-        'entity_id_col'    => 'entity_id',
-        'entity_table_col' => 'entity_table'
+        'entity_id_col' => 'entity_id',
+        'entity_table_col' => 'entity_table',
       ),
       'civicrm_price_set_entity' => array(
-        'entity_id_col'    => 'entity_id',
+        'entity_id_col' => 'entity_id',
         'entity_table_col' => 'entity_table',
-        'is_multirecord'   => TRUE,
+        'is_multirecord' => TRUE,
       ),
       'civicrm_uf_join' => array(
-        'entity_id_col'    => 'entity_id',
+        'entity_id_col' => 'entity_id',
         'entity_table_col' => 'entity_table',
-        'is_multirecord'   => TRUE,
+        'is_multirecord' => TRUE,
       ),
       'civicrm_pcp_block' => array(
-        'entity_id_col'    => 'entity_id',
-        'entity_table_col' => 'entity_table'
+        'entity_id_col' => 'entity_id',
+        'entity_table_col' => 'entity_table',
       ),
     );
 
@@ -132,13 +132,13 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity {
   public static function setStatus($status) {
     self::$status = $status;
   }
+
   /**
    * Save records in civicrm_recujrring_entity table
    *
    * @param array $params
    *   Reference array contains the values submitted by the form .
    *
-   * @static
    *
    * @return object
    */
@@ -174,16 +174,15 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity {
    * @param string $entityTable
    *   Name of the entity table .
    *
-   * @static
    *
    * @return object
    */
   public static function quickAdd($parentId, $entityId, $entityTable) {
-    $params =
-      array(
-        'parent_id'    => $parentId,
-        'entity_id'    => $entityId,
-        'entity_table' => $entityTable
+    $params
+      array(
+        'parent_id' => $parentId,
+        'entity_id' => $entityId,
+        'entity_table' => $entityTable,
       );
     return self::add($params);
   }
@@ -224,7 +223,8 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity {
   /**
    * This function builds a "When" object based on schedule/reminder params
    *
-   * @return object When object
+   * @return object
+   *   When object
    */
   public function generateRecursion() {
     // return if already generated
@@ -254,7 +254,7 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity {
   public function generateEntities() {
     self::setStatus(self::RUNNING);
 
-    $newEntities  = array();
+    $newEntities = array();
     $findCriteria = array();
     if (!empty($this->recursionDates)) {
       if ($this->entity_id) {
@@ -315,7 +315,8 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity {
    * This function iterates through when object criterias and
    * generates recursive dates based on that
    *
-   * @return array array of dates
+   * @return array
+   *   array of dates
    */
   public function generateRecursiveDates() {
     $this->generateRecursion();
@@ -327,7 +328,7 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity {
       $exRangeStart = $exRangeEnd = NULL;
       if (!empty($this->excludeDateRangeColumns)) {
         $exRangeStart = $this->excludeDateRangeColumns[0];
-        $exRangeEnd   = $this->excludeDateRangeColumns[1];
+        $exRangeEnd = $this->excludeDateRangeColumns[1];
       }
 
       $count = 1;
@@ -349,7 +350,7 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity {
         }
         if ($exRangeStart) {
           $exRangeStartDate = CRM_Utils_Date::processDate($recursionDates[$count][$exRangeStart], NULL, FALSE, 'Ymd');
-          $exRangeEndDate   = CRM_Utils_Date::processDate($recursionDates[$count][$exRangeEnd], NULL, FALSE, 'Ymd');
+          $exRangeEndDate = CRM_Utils_Date::processDate($recursionDates[$count][$exRangeEnd], NULL, FALSE, 'Ymd');
         }
 
         foreach ($this->excludeDates as $exDate) {
@@ -401,9 +402,9 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity {
    * @param int $initiatorId
    *   The instance where this function is invoked from .
    *
-   * @static
    *
-   * @return array an array of child ids
+   * @return array
+   *   an array of child ids
    */
   static public function getEntitiesForParent($parentId, $entityTable, $includeParent = TRUE, $mode = 3, $initiatorId = NULL) {
     $entities = array();
@@ -416,7 +417,7 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity {
     }
 
     $queryParams = array(
-      1 => array($parentId,    'Integer'),
+      1 => array($parentId, 'Integer'),
       2 => array($entityTable, 'String'),
       3 => array($initiatorId, 'Integer'),
     );
@@ -432,10 +433,12 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity {
       $query .= " AND entity_id != " . ($initiatorId ? "%3" : "%1");
     }
 
-    if ($mode == '1') { // MODE = SINGLE
+    // MODE = SINGLE
+    if ($mode == '1') {
       $query .= " AND entity_id = %3";
     }
-    elseif ($mode == '2') { // MODE = FUTURE
+    // MODE = FUTURE
+    elseif ($mode == '2') {
       $recurringEntityID = CRM_Core_DAO::singleValueQuery("SELECT id FROM civicrm_recurring_entity WHERE entity_id = %3 AND entity_table = %2", $queryParams);
       if ($recurringEntityID) {
         $query .= $includeParent ? " AND id >= %4" : " AND id > %4";
@@ -469,9 +472,9 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity {
    * @param int $mode
    *   1. retrieve only one entity. 2. retrieve all future entities in the repeating set. 3. all entities in the repeating set. .
    *
-   * @static
    *
-   * @return array array of connected ids
+   * @return array
+   *   array of connected ids
    */
   static public function getEntitiesFor($entityId, $entityTable, $includeParent = TRUE, $mode = 3) {
     $parentId = self::getParentFor($entityId, $entityTable);
@@ -491,9 +494,9 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity {
    * @param bool $includeParent
    *   Include parent in result set .
    *
-   * @static
    *
-   * @return int unsigned $parentId Parent ID
+   * @return int
+   *   unsigned $parentId Parent ID
    */
   static public function getParentFor($entityId, $entityTable, $includeParent = TRUE) {
     if (empty($entityId) || empty($entityTable)) {
@@ -507,8 +510,8 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity {
     if (!$includeParent) {
       $query .= " AND parent_id != %1";
     }
-    $parentId =
-      CRM_Core_DAO::singleValueQuery($query,
+    $parentId
+      CRM_Core_DAO::singleValueQuery($query,
         array(
           1 => array($entityId, 'Integer'),
           2 => array($entityTable, 'String'),
@@ -529,7 +532,6 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity {
    * @param bool $createRecurringEntity
    *   If to create a record in recurring_entity table .
    *
-   * @static
    *
    * @return object
    */
@@ -541,7 +543,7 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity {
     $newObject = CRM_Core_DAO::copyGeneric($daoName, $fromCriteria, $newParams);
 
     if (is_a($newObject, 'CRM_Core_DAO') && $newObject->id && $createRecurringEntity) {
-      $object = new $daoName( );
+      $object = new $daoName();
       foreach ($fromCriteria as $key => $value) {
         $object->$key = $value;
       }
@@ -559,7 +561,6 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity {
    * @param object $event
    *   An object of /Civi/Core/DAO/Event/PostUpdate containing dao object that was just updated .
    *
-   * @static
    *
    * @return void
    */
@@ -604,7 +605,7 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity {
       $processedEntities[$key] = 1;
 
       if (array_key_exists($entityTable, self::$_tableDAOMapper)) {
-        $daoName  = self::$_tableDAOMapper[$entityTable];
+        $daoName = self::$_tableDAOMapper[$entityTable];
 
         $skipData = array();
         if (array_key_exists($entityTable, self::$_updateSkipFields)) {
@@ -632,7 +633,6 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity {
    * @param object $event
    *   An object of /Civi/Core/DAO/Event/PostUpdate containing dao object that was just inserted .
    *
-   * @static
    *
    * @return void
    */
@@ -676,14 +676,15 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity {
     if (empty($hasaRecurringRecord)) {
       // check if its a linked entity
       if (array_key_exists($obj->__table, self::$_linkedEntitiesInfo) &&
-        !CRM_Utils_Array::value('is_multirecord', self::$_linkedEntitiesInfo[$obj->__table])) {
+        !CRM_Utils_Array::value('is_multirecord', self::$_linkedEntitiesInfo[$obj->__table])
+      ) {
         $linkedDAO = new self::$_tableDAOMapper[$obj->__table]();
         $linkedDAO->id = $obj->id;
         if ($linkedDAO->find(TRUE)) {
           $idCol = self::$_linkedEntitiesInfo[$obj->__table]['entity_id_col'];
           $tableCol = self::$_linkedEntitiesInfo[$obj->__table]['entity_table_col'];
 
-          $pEntityID    = $linkedDAO->$idCol;
+          $pEntityID = $linkedDAO->$idCol;
           $pEntityTable = $linkedDAO->$tableCol;
 
           // find all parent recurring entity set
@@ -714,8 +715,8 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity {
               else {
                 // linked entity doesn't exist. lets create them
                 $newCriteria = array(
-                  $idCol    => $val['id'],
-                  $tableCol => $val['table']
+                  $idCol => $val['id'],
+                  $tableCol => $val['table'],
                 );
                 $linkedObj = CRM_Core_BAO_RecurringEntity::copyCreateEntity($obj->__table,
                   array('id' => $obj->id),
@@ -742,7 +743,6 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity {
    * @param object $event
    *   An object of /Civi/Core/DAO/Event/PostUpdate containing dao object that was just deleted .
    *
-   * @static
    *
    * @return void
    */
@@ -782,13 +782,12 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity {
    * This function deletes main entity and related linked entities from recurring-entity table
    *
    * @param int $entityId
-   *   Entity id .
+   *   Entity id
    * @param string $entityTable
-   *   Name of the entity table .
+   *   Name of the entity table
    *
-   * @static
    *
-   * @return boolean|object Returns either boolean value or CRM_Core_DAO_RecurringEntity object
+   * @return bool|CRM_Core_DAO_RecurringEntity
    */
   static public function delEntity($entityId, $entityTable, $isDelLinkedEntities = FALSE) {
     if (empty($entityId) || empty($entityTable)) {
@@ -879,7 +878,7 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity {
       }
       if ($formParams['repeats_by'] == 2) {
         if (CRM_Utils_Array::value('entity_status_1', $formParams) && CRM_Utils_Array::value('entity_status_2', $formParams)) {
-          $dbParams['entity_status'] = $formParams['entity_status_1']." ".$formParams['entity_status_2'];
+          $dbParams['entity_status'] = $formParams['entity_status_1'] . " " . $formParams['entity_status_2'];
         }
       }
     }
@@ -906,7 +905,6 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity {
    * @param int $scheduleReminderId
    *   Primary key of civicrm_action_schedule table .
    *
-   * @static
    *
    * @return object
    */
@@ -919,7 +917,7 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity {
     }
     $dao = CRM_Core_DAO::executeQuery($query,
       array(
-        1 => array($scheduleReminderId, 'Integer')
+        1 => array($scheduleReminderId, 'Integer'),
       )
     );
     $dao->fetch();
@@ -927,7 +925,7 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity {
   }
 
   /**
-   * This function is a wrapper of getScheduleReminderDetailsById function
+   * wrapper of getScheduleReminderDetailsById function
    *
    * @param int $scheduleReminderId
    *   Primary key of civicrm_action_schedule table .
@@ -951,7 +949,8 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity {
    * @param array $scheduleReminderDetails
    *   Array of repeat criterias saved in civicrm_action_schedule table .
    *
-   * @return object When object
+   * @return object
+   *   When object
    */
   public function getRecursionFromSchedule($scheduleReminderDetails = array()) {
     $r = new When();
@@ -970,7 +969,7 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity {
         if ($repetition_frequency_unit == "day") {
           $repetition_frequency_unit = "dai";
         }
-        $repetition_frequency_unit = $repetition_frequency_unit.'ly';
+        $repetition_frequency_unit = $repetition_frequency_unit . 'ly';
         $r->recur($start, $repetition_frequency_unit);
       }
 
@@ -999,23 +998,27 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity {
         if ($scheduleReminderDetails['entity_status']) {
           $startActionDate = explode(" ", $scheduleReminderDetails['entity_status']);
           switch ($startActionDate[0]) {
-          case 'first':
-            $startActionDate1 = 1;
-            break;
-          case 'second':
-            $startActionDate1 = 2;
-            break;
-          case 'third':
-            $startActionDate1 = 3;
-            break;
-          case 'fourth':
-            $startActionDate1 = 4;
-            break;
-          case 'last':
-            $startActionDate1 = -1;
-            break;
+            case 'first':
+              $startActionDate1 = 1;
+              break;
+
+            case 'second':
+              $startActionDate1 = 2;
+              break;
+
+            case 'third':
+              $startActionDate1 = 3;
+              break;
+
+            case 'fourth':
+              $startActionDate1 = 4;
+              break;
+
+            case 'last':
+              $startActionDate1 = -1;
+              break;
           }
-          $concatStartActionDateBits = $startActionDate1.strtoupper(substr($startActionDate[1], 0, 2));
+          $concatStartActionDateBits = $startActionDate1 . strtoupper(substr($startActionDate[1], 0, 2));
           $r->byday(array($concatStartActionDateBits));
         }
         elseif ($scheduleReminderDetails['limit_to']) {
@@ -1033,7 +1036,7 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity {
 
       if (CRM_Utils_Array::value('absolute_date', $scheduleReminderDetails)) {
         $absoluteDate = CRM_Utils_Date::setDateDefaults($scheduleReminderDetails['absolute_date']);
-        $endDate = new DateTime($absoluteDate[0].' '.$absoluteDate[1]);
+        $endDate = new DateTime($absoluteDate[0] . ' ' . $absoluteDate[1]);
         $endDate->modify('+1 day');
         $r->until($endDate);
       }
@@ -1057,14 +1060,14 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity {
    * @param DateTime $endDate
    *   End Date .
    *
-   * @static
    *
-   * @return object DateTime object which contain time difference
+   * @return object
+   *   DateTime object which contain time difference
    */
   static public function getInterval($startDate, $endDate) {
     if ($startDate && $endDate) {
       $startDate = new DateTime($startDate);
-      $endDate   = new DateTime($endDate);
+      $endDate = new DateTime($endDate);
       return $startDate->diff($endDate);
     }
   }
@@ -1077,7 +1080,6 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity {
    * @param string $used_for
    *   Specifies for which entity type it's used for .
    *
-   * @static
    *
    * @return object
    */
@@ -1092,7 +1094,7 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity {
       }
       $params = array(
         1 => array($entityId, 'Integer'),
-        2 => array($used_for, 'String')
+        2 => array($used_for, 'String'),
       );
       $dao = CRM_Core_DAO::executeQuery($query, $params);
       $dao->fetch();
@@ -1111,19 +1113,19 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity {
    */
   public static function updateModeLinkedEntity($entityId, $linkedEntityTable, $mainEntityTable) {
     $result = array();
-    if ( $entityId && $linkedEntityTable && $mainEntityTable ) {
+    if ($entityId && $linkedEntityTable && $mainEntityTable) {
       if (CRM_Utils_Array::value($linkedEntityTable, self::$_tableDAOMapper)) {
         $dao = self::$_tableDAOMapper[$linkedEntityTable];
       }
       else {
         CRM_Core_Session::setStatus('Could not update mode for linked entities');
-        return;
+        return NULL;
       }
       $entityTable = $linkedEntityTable;
       $params = array(
-                      'entity_id' => $entityId,
-                      'entity_table' => $mainEntityTable
-                    );
+        'entity_id' => $entityId,
+        'entity_table' => $mainEntityTable,
+      );
       $defaults = array();
       CRM_Core_DAO::commonRetrieve($dao, $params, $defaults);
       if (CRM_Utils_Array::value('id', $defaults)) {
@@ -1133,4 +1135,5 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity {
     }
     return $result;
   }
+
 }