Merge pull request #12064 from seamuslee001/nfc_rename_groupby_function
[civicrm-core.git] / CRM / Core / ManagedEntities.php
index e47c7dfde2ea8133cf5283b5e4b1dd7d70863fa6..5021859b01c0ef9a2cbabd1fa397aa7f369d5a34 100644 (file)
@@ -7,6 +7,11 @@
  */
 class CRM_Core_ManagedEntities {
 
+  /**
+   * Get clean up options.
+   *
+   * @return array
+   */
   public static function getCleanupOptions() {
     return array(
       'always' => ts('Always'),
@@ -29,7 +34,7 @@ class CRM_Core_ManagedEntities {
   protected $declarations;
 
   /**
-   * Get an instance
+   * Get an instance.
    * @param bool $fresh
    * @return \CRM_Core_ManagedEntities
    */
@@ -329,17 +334,25 @@ class CRM_Core_ManagedEntities {
         'version' => 3,
         'id' => $dao->entity_id,
       );
-      $result = civicrm_api($dao->entity_type, 'delete', $params);
-      if ($result['is_error']) {
-        $this->onApiError($dao->entity_type, 'delete', $params, $result);
-      }
+      $check = civicrm_api3($dao->entity_type, 'get', $params);
+      if ((bool) $check['count']) {
+        $result = civicrm_api($dao->entity_type, 'delete', $params);
+        if ($result['is_error']) {
+          $this->onApiError($dao->entity_type, 'delete', $params, $result);
+        }
 
-      CRM_Core_DAO::executeQuery('DELETE FROM civicrm_managed WHERE id = %1', array(
-        1 => array($dao->id, 'Integer'),
-      ));
+        CRM_Core_DAO::executeQuery('DELETE FROM civicrm_managed WHERE id = %1', array(
+          1 => array($dao->id, 'Integer'),
+        ));
+      }
     }
   }
 
+  /**
+   * Get declarations.
+   *
+   * @return array|null
+   */
   public function getDeclarations() {
     if ($this->declarations === NULL) {
       $this->declarations = array();