Towards deleting and recreating activities and resave repeat configuration
authorunknown <priyanka.karan26@gmail.com>
Thu, 9 Oct 2014 16:31:57 +0000 (17:31 +0100)
committerunknown <priyanka.karan26@gmail.com>
Thu, 9 Oct 2014 16:31:57 +0000 (17:31 +0100)
CRM/Activity/Form/Activity.php
CRM/Core/BAO/RecurringEntity.php
CRM/Core/Form/RecurringEntity.php
CRM/Event/Form/ManageEvent/Repeat.php

index cedcdaf8207f8b8918dab9db12cd0a2f6f169340..4d3b90c42548df10d5f2c15cf382bcb619a90f63 100644 (file)
@@ -953,6 +953,7 @@ class CRM_Activity_Form_Activity extends CRM_Contact_Form_Task {
       $activity = $this->processActivity($params);
       $params['parent_entity_start_date'] = $this->_parentActivityStartDate;
       $params['start_date_column_name'] = 'activity_date_time';
+      $params['entity_table'] = 'civicrm_activity';
       //Unset activity id
       unset($params['id']);
       $linkedEntities = array(
index be7fff6abb214caa8b46444ebea8305f66f56587..e06e07f18dcd53ff02ba5147876d48a700f4ba46 100644 (file)
@@ -52,17 +52,19 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity {
 
   protected $recursion = NULL;
   
+  public static $_entitiesToBeDeleted = array();
+  
   static $_recurringEntityHelper = 
     array(
       'civicrm_event' => array(
-      'helper_class' => 'CRM_Event_BAO_Event',
-      'delete_func' => 'del',
-      'count_func' => ''
+      'helper_class' => 'CRM_Event_DAO_Event',
+      'delete_func' => 'CRM_Event_BAO_Event::del',
+      'pre_delete_func' => 'CRM_Event_Form_ManageEvent_Repeat::checkRegistrationForEvents'
       ),
       'civicrm_activity' => array(
-      'helper_class' => 'CRM_Activity_BAO_Activity',
-      'delete_func' => 'del',
-      'count_func' => ''
+      'helper_class' => 'CRM_Activity_DAO_Activity',
+      'delete_func' => 'CRM_Activity_BAO_Activity::deleteActivity',
+      'pre_delete_func' => ''
       ) 
     ); 
 
@@ -983,7 +985,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
    * 
-   * @param int $eventId Entity ID
+   * @param int $entityId Entity ID
    * @param string $used_for Specifies for which entity type it's used for
    * 
    * @access public
@@ -991,7 +993,7 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity {
    * 
    * @return object
    */
-  static public function getReminderDetailsByEntityId($entityId, $used_for) {
+  public static function getReminderDetailsByEntityId($entityId, $used_for) {
     if ($entityId) {
       $query = "
         SELECT *
index 340ff8426110579b9f143c809d8cbc6a7bdc2c79..2bb17e8875025b4cc1376ebc4ae5df9a6a6c349d 100644 (file)
@@ -298,7 +298,7 @@ class CRM_Core_Form_RecurringEntity {
    */
   static function postProcess($params = array(), $type, $linkedEntities = array()) {
     $params['entity_id'] = self::$_entityId;
-    if ($type && CRM_Utils_Array::value('entity_id', $params)) {
+    if (CRM_Utils_Array::value('entity_table', $params) && CRM_Utils_Array::value('entity_id', $params) && $type) {
       $params['used_for'] = $type;
       $params['parent_entity_id'] = self::$_parentEntityId;
       $params['id'] = self::$_scheduleReminderID;
@@ -359,18 +359,37 @@ class CRM_Core_Form_RecurringEntity {
 
       //Delete relations if any from recurring entity tables before inserting new relations for this entity id
       if ($params['entity_id']) {
-        $getRelatedEntities = CRM_Core_BAO_RecurringEntity::getEntitiesFor($params['entity_id'], 'civicrm_'.$type, TRUE);
-        
-        if ($type == 'event') {
-          $participantDetails = CRM_Event_Form_ManageEvent_Repeat::getParticipantCountforEvent($getRelatedEntities);
-          //Check if participants exists for events
-          foreach ($getRelatedEntities as $key => $value) {
-            if (!CRM_Utils_Array::value($value['id'], $participantDetails['countByID']) && $value['id'] != $params['entity_id']) {
-              CRM_Event_BAO_Event::del($value['id']);
+        //If entity has any pre delete function, consider that first
+        if (CRM_Utils_Array::value('pre_delete_func', CRM_Core_BAO_RecurringEntity::$_recurringEntityHelper[$params['entity_table']]) &&
+            CRM_Utils_Array::value('helper_class', CRM_Core_BAO_RecurringEntity::$_recurringEntityHelper[$params['entity_table']])) {
+            call_user_func(array(
+              CRM_Core_BAO_RecurringEntity::$_recurringEntityHelper[$params['entity_table']]['helper_class'], 
+              call_user_func_array(CRM_Core_BAO_RecurringEntity::$_recurringEntityHelper[$params['entity_table']]['pre_delete_func'], array($params['entity_id'])))
+            );
+        }
+        //Ready to execute delete on entities if it has delete function set
+        if (CRM_Utils_Array::value('delete_func', CRM_Core_BAO_RecurringEntity::$_recurringEntityHelper[$params['entity_table']]) &&
+            CRM_Utils_Array::value('helper_class', CRM_Core_BAO_RecurringEntity::$_recurringEntityHelper[$params['entity_table']])) {
+            //Check if pre delete function has some ids to be deleted
+            if (!empty(CRM_Core_BAO_RecurringEntity::$_entitiesToBeDeleted)) {
+              foreach (CRM_Core_BAO_RecurringEntity::$_entitiesToBeDeleted as $value) {
+                call_user_func(array(
+                CRM_Core_BAO_RecurringEntity::$_recurringEntityHelper[$params['entity_table']]['helper_class'], 
+                call_user_func_array(CRM_Core_BAO_RecurringEntity::$_recurringEntityHelper[$params['entity_table']]['delete_func'], array($value)))
+                );
+              }
+            }
+            else {
+              $getRelatedEntities = CRM_Core_BAO_RecurringEntity::getEntitiesFor($params['entity_id'], $params['entity_table'], FALSE);
+              foreach ($getRelatedEntities as $key => $value) {
+                call_user_func(array(
+                CRM_Core_BAO_RecurringEntity::$_recurringEntityHelper[$params['entity_table']]['helper_class'], 
+                call_user_func_array(CRM_Core_BAO_RecurringEntity::$_recurringEntityHelper[$params['entity_table']]['delete_func'], array(array('id' => $value['id']))))
+                );
+              }
             }
-          }
         }
-        CRM_Core_BAO_RecurringEntity::delEntityRelations($params['entity_id'], 'civicrm_'.$type);
+        CRM_Core_BAO_RecurringEntity::delEntityRelations($params['entity_id'], $params['entity_table']);
       }
 
       $recursion = new CRM_Core_BAO_RecurringEntity();
@@ -397,7 +416,7 @@ class CRM_Core_Form_RecurringEntity {
       }
 
       $recursion->entity_id = $params['entity_id'];
-      $recursion->entity_table = 'civicrm_'.$type;
+      $recursion->entity_table = $params['entity_table'];
       if (!empty($linkedEntities)) {
         $recursion->linkedEntities = $linkedEntities;
       }
index 26e41c11b36a45bf46b2cf0f6786cccc40c28f9e..7663575bdff39bac63ef37bbce9a2627b3a717e6 100644 (file)
@@ -110,6 +110,7 @@ class CRM_Event_Form_ManageEvent_Repeat extends CRM_Event_Form_ManageEvent {
       $params['parent_entity_end_date'] = $this->_parentEventEndDate;
       $params['start_date_column_name'] = 'start_date';
       $params['end_date_column_name'] = 'end_date';
+      $params['entity_table'] = 'civicrm_event';
       //Unset event id
       unset($params['id']);
 
@@ -195,13 +196,13 @@ class CRM_Event_Form_ManageEvent_Repeat extends CRM_Event_Form_ManageEvent {
     return $participantDetails;
   }
   
-   /**
-   * Update mode column in civicrm_recurring_entity table for event related tabs
-   
-   * @params int $entityId event id
-   * @params string $linkedEntityTable Linked entity table name for this event
-   * @return array
-   */
+  /**
+  * Update mode column in civicrm_recurring_entity table for event related tabs
+  * 
+  * @params int $entityId event id
+  * @params string $linkedEntityTable Linked entity table name for this event
+  * @return array
+  */
   public static function updateModeRecurringEntityForEvent($entityId, $linkedEntityTable) {
     $result = array();
     if ( $entityId && $linkedEntityTable ) {
@@ -238,5 +239,28 @@ class CRM_Event_Form_ManageEvent_Repeat extends CRM_Event_Form_ManageEvent {
         }
     } 
     return $result;
-  }  
+  } 
+  
+  /**
+   * This function checks if there was any registraion for related event ids,
+   * and returns array of ids with no regsitrations
+   * @param type $eventID Event ID
+   * @return type
+   */
+  public static function checkRegistrationForEvents($eventID) {
+    $eventIdsWithNoRegistration = array();
+    if ($eventID) {
+      $getRelatedEntities = CRM_Core_BAO_RecurringEntity::getEntitiesFor($eventID, 'civicrm_event', TRUE);
+      $participantDetails = CRM_Event_Form_ManageEvent_Repeat::getParticipantCountforEvent($getRelatedEntities);
+      //Check if participants exists for events
+      foreach ($getRelatedEntities as $key => $value) {
+        if (!CRM_Utils_Array::value($value['id'], $participantDetails['countByID']) && $value['id'] != $eventID) {
+          //CRM_Event_BAO_Event::del($value['id']);
+          $eventIdsWithNoRegistration[] = $value['id'];
+        }
+      }
+    }
+    CRM_Core_BAO_RecurringEntity::$_entitiesToBeDeleted = $eventIdsWithNoRegistration;
+    return CRM_Core_BAO_RecurringEntity::$_entitiesToBeDeleted;
+  }
 }