Use writeRecords for Navigations
authorlarssandergreen <lars@wildsight.ca>
Sat, 14 Oct 2023 20:35:32 +0000 (14:35 -0600)
committerlarssandergreen <lars@wildsight.ca>
Sat, 14 Oct 2023 20:35:32 +0000 (14:35 -0600)
CRM/Core/BAO/Navigation.php
CRM/Report/BAO/ReportInstance.php
CRM/Report/Form/Instance.php

index 5357b6f7d4a9a4097965a08497af72638f178f62..c9a4c61202aab3bb294891f2107ae435bba7f04a 100644 (file)
@@ -74,7 +74,6 @@ class CRM_Core_BAO_Navigation extends CRM_Core_DAO_Navigation {
    * @return CRM_Core_DAO_Navigation
    */
   public static function add(&$params) {
-    $navigation = new CRM_Core_DAO_Navigation();
     if (empty($params['id'])) {
       $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE);
       $params['has_separator'] = CRM_Utils_Array::value('has_separator', $params, FALSE);
@@ -102,10 +101,7 @@ class CRM_Core_BAO_Navigation extends CRM_Core_DAO_Navigation {
       $params['permission'] = implode(',', $params['permission']);
     }
 
-    $navigation->copyValues($params);
-
-    $navigation->save();
-    return $navigation;
+    return self::writeRecord($params);
   }
 
   /**
@@ -513,11 +509,11 @@ ORDER BY weight";
         break;
 
       case "rename":
-        self::processRename($nodeID, $label);
+        self::writeRecord(['id' => $nodeID, 'label' => $label]);
         break;
 
       case "delete":
-        self::processDelete($nodeID);
+        self::deleteRecord(['id' => $nodeID]);
         break;
     }
 
@@ -578,8 +574,6 @@ ORDER BY weight";
       $incrementOtherNodes = FALSE;
     }
 
-    $transaction = new CRM_Core_Transaction();
-
     // now update the existing nodes to weight + 1, if required.
     if ($incrementOtherNodes) {
       $query = "UPDATE civicrm_navigation SET weight = weight + 1
@@ -588,11 +582,8 @@ ORDER BY weight";
       CRM_Core_DAO::executeQuery($query);
     }
 
-    // finally set the weight of current node
-    $query = "UPDATE civicrm_navigation SET weight = {$newWeight}, parent_id = {$newParentID} WHERE id = {$nodeID}";
-    CRM_Core_DAO::executeQuery($query);
-
-    $transaction->commit();
+    // finally set the weight and parent of current node
+    self::writeRecord(['id' => $nodeID, 'weight' => $newWeight, 'parent_id' => $newParentID]);
   }
 
   /**
@@ -600,19 +591,20 @@ ORDER BY weight";
    *
    * @param int $nodeID
    * @param $label
+   * @deprecated  - use API
    */
   public static function processRename($nodeID, $label) {
-    CRM_Core_DAO::setFieldValue('CRM_Core_DAO_Navigation', $nodeID, 'label', $label);
+    self::writeRecord(['id' => $nodeID, 'label' => $label]);
   }
 
   /**
    * Process delete action for tree.
    *
    * @param int $nodeID
+   * @deprecated - use API
    */
   public static function processDelete($nodeID) {
-    $query = "DELETE FROM civicrm_navigation WHERE id = {$nodeID}";
-    CRM_Core_DAO::executeQuery($query);
+    self::deleteRecord(['id' => $nodeID]);
   }
 
   /**
index eb7ff8fd6fb0702511eff4561c1686318a9a3ba5..1e183b19a9cbbc2a580c0625512c9852d93c0aa6 100644 (file)
@@ -240,7 +240,7 @@ class CRM_Report_BAO_ReportInstance extends CRM_Report_DAO_ReportInstance implem
       // When deleting a report, also delete from navigation menu
       $navId = CRM_Core_DAO::getFieldValue('CRM_Report_DAO_ReportInstance', $event->id, 'navigation_id');
       if ($navId) {
-        CRM_Core_BAO_Navigation::processDelete($navId);
+        CRM_Core_BAO_Navigation::deleteRecord(['id' => $navId]);
         CRM_Core_BAO_Navigation::resetNavigation();
       }
     }
index ecd39427156b44c8c8cff1e6bcbfc89005f11ae8..fe77273cfcaec7065931086df4a87d880e13141c 100644 (file)
@@ -318,7 +318,7 @@ class CRM_Report_Form_Instance {
       // Delete navigation if exists.
       $navId = CRM_Core_DAO::getFieldValue('CRM_Report_DAO_ReportInstance', $instanceID, 'navigation_id', 'id');
       if ($navId) {
-        CRM_Core_BAO_Navigation::processDelete($navId);
+        CRM_Core_BAO_Navigation::deleteRecord(['id' => $navId]);
         CRM_Core_BAO_Navigation::resetNavigation();
       }
     }