From 68e67b6da1999d913ec0c39ab265e59b9d418a08 Mon Sep 17 00:00:00 2001
From: Erik Hommel <hommel@ee-atwork.nl>
Date: Thu, 15 Sep 2016 11:00:26 +0200
Subject: [PATCH] added api CiviRuleRule delete

---
 api/v3/CiviRuleRule/Delete.php |  31 +++++++++
 civirules.civix.php            | 115 ++++++++++++++++++++++++++-------
 2 files changed, 123 insertions(+), 23 deletions(-)
 create mode 100644 api/v3/CiviRuleRule/Delete.php

diff --git a/api/v3/CiviRuleRule/Delete.php b/api/v3/CiviRuleRule/Delete.php
new file mode 100644
index 0000000..0ad80a2
--- /dev/null
+++ b/api/v3/CiviRuleRule/Delete.php
@@ -0,0 +1,31 @@
+<?php
+
+/**
+ * CiviRuleRule.Delete API specification (optional)
+ * This is used for documentation and validation.
+ *
+ * @param array $spec description of fields supported by this API call
+ * @return void
+ * @see http://wiki.civicrm.org/confluence/display/CRMDOC/API+Architecture+Standards
+ */
+function _civicrm_api3_civi_rule_rule_Delete_spec(&$spec) {
+  $spec['id']['api_required'] = 1;
+}
+
+/**
+ * CiviRuleRule.Delete API
+ *
+ * @param array $params
+ * @return array API result descriptor
+ * @see civicrm_api3_create_success
+ * @see civicrm_api3_create_error
+ * @throws API_Exception
+ */
+function civicrm_api3_civi_rule_rule_Delete($params) {
+  if (!array_key_exists('id', $params) || empty($params['id'])) {
+    throw new API_Exception('Parameter id is mandatory and can not be empty in ' . __METHOD__, 0010);
+  } else {
+    return civicrm_api3_create_success(CRM_Civirules_BAO_Rule::deleteWithId($params['id']), $params, 'CiviRuleRule', 'Delete');
+  }
+}
+
diff --git a/civirules.civix.php b/civirules.civix.php
index 0168988..22ebf62 100755
--- a/civirules.civix.php
+++ b/civirules.civix.php
@@ -9,28 +9,32 @@
  */
 function _civirules_civix_civicrm_config(&$config = NULL) {
   static $configured = FALSE;
-  if ($configured) return;
+  if ($configured) {
+    return;
+  }
   $configured = TRUE;
 
   $template =& CRM_Core_Smarty::singleton();
 
-  $extRoot = dirname( __FILE__ ) . DIRECTORY_SEPARATOR;
+  $extRoot = dirname(__FILE__) . DIRECTORY_SEPARATOR;
   $extDir = $extRoot . 'templates';
 
   if ( is_array( $template->template_dir ) ) {
       array_unshift( $template->template_dir, $extDir );
-  } else {
+  }
+  else {
       $template->template_dir = array( $extDir, $template->template_dir );
   }
 
   $include_path = $extRoot . PATH_SEPARATOR . get_include_path( );
-  set_include_path( $include_path );
+  set_include_path($include_path);
 }
 
 /**
  * (Delegated) Implements hook_civicrm_xmlMenu().
  *
  * @param $files array(string)
+ *
  * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_xmlMenu
  */
 function _civirules_civix_civicrm_xmlMenu(&$files) {
@@ -115,7 +119,8 @@ function _civirules_civix_civicrm_upgrade($op, CRM_Queue_Queue $queue = NULL) {
 function _civirules_civix_upgrader() {
   if (!file_exists(__DIR__.'/CRM/Civirules/Upgrader.php')) {
     return NULL;
-  } else {
+  }
+  else {
     return CRM_Civirules_Upgrader_Base::instance();
   }
 }
@@ -206,6 +211,31 @@ function _civirules_civix_civicrm_caseTypes(&$caseTypes) {
   }
 }
 
+/**
+ * (Delegated) Implements hook_civicrm_angularModules().
+ *
+ * Find any and return any files matching "ang/*.ang.php"
+ *
+ * Note: This hook only runs in CiviCRM 4.5+.
+ *
+ * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_angularModules
+ */
+function _civirules_civix_civicrm_angularModules(&$angularModules) {
+  if (!is_dir(__DIR__ . '/ang')) {
+    return;
+  }
+
+  $files = _civirules_civix_glob(__DIR__ . '/ang/*.ang.php');
+  foreach ($files as $file) {
+    $name = preg_replace(':\.ang\.php$:', '', basename($file));
+    $module = include $file;
+    if (empty($module['ext'])) {
+      $module['ext'] = 'org.civicoop.civirules';
+    }
+    $angularModules[$name] = $module;
+  }
+}
+
 /**
  * Glob wrapper which is guaranteed to return an array.
  *
@@ -224,30 +254,24 @@ function _civirules_civix_glob($pattern) {
 }
 
 /**
- * Inserts a navigation menu item at a given place in the hierarchy
+ * Inserts a navigation menu item at a given place in the hierarchy.
  *
- * $menu - menu hierarchy
- * $path - path where insertion should happen (ie. Administer/System Settings)
- * $item - menu you need to insert (parent/child attributes will be filled for you)
- * $parentId - used internally to recurse in the menu structure
+ * @param array $menu - menu hierarchy
+ * @param string $path - path where insertion should happen (ie. Administer/System Settings)
+ * @param array $item - menu you need to insert (parent/child attributes will be filled for you)
  */
-function _civirules_civix_insert_navigation_menu(&$menu, $path, $item, $parentId = NULL) {
-  static $navId;
-
+function _civirules_civix_insert_navigation_menu(&$menu, $path, $item) {
   // If we are done going down the path, insert menu
   if (empty($path)) {
-    if (!$navId) $navId = CRM_Core_DAO::singleValueQuery("SELECT max(id) FROM civicrm_navigation");
-    $navId ++;
-    $menu[$navId] = array (
-      'attributes' => array_merge($item, array(
+    $menu[] = array(
+      'attributes' => array_merge(array(
         'label'      => CRM_Utils_Array::value('name', $item),
         'active'     => 1,
-        'parentID'   => $parentId,
-        'navID'      => $navId,
-      ))
+      ), $item),
     );
-    return true;
-  } else {
+    return TRUE;
+  }
+  else {
     // Find an recurse into the next level down
     $found = false;
     $path = explode('/', $path);
@@ -262,6 +286,49 @@ function _civirules_civix_insert_navigation_menu(&$menu, $path, $item, $parentId
   }
 }
 
+/**
+ * (Delegated) Implements hook_civicrm_navigationMenu().
+ */
+function _civirules_civix_navigationMenu(&$nodes) {
+  if (!is_callable(array('CRM_Core_BAO_Navigation', 'fixNavigationMenu'))) {
+    _civirules_civix_fixNavigationMenu($nodes);
+  }
+}
+
+/**
+ * Given a navigation menu, generate navIDs for any items which are
+ * missing them.
+ */
+function _civirules_civix_fixNavigationMenu(&$nodes) {
+  $maxNavID = 1;
+  array_walk_recursive($nodes, function($item, $key) use (&$maxNavID) {
+    if ($key === 'navID') {
+      $maxNavID = max($maxNavID, $item);
+    }
+    });
+  _civirules_civix_fixNavigationMenuItems($nodes, $maxNavID, NULL);
+}
+
+function _civirules_civix_fixNavigationMenuItems(&$nodes, &$maxNavID, $parentID) {
+  $origKeys = array_keys($nodes);
+  foreach ($origKeys as $origKey) {
+    if (!isset($nodes[$origKey]['attributes']['parentID']) && $parentID !== NULL) {
+      $nodes[$origKey]['attributes']['parentID'] = $parentID;
+    }
+    // If no navID, then assign navID and fix key.
+    if (!isset($nodes[$origKey]['attributes']['navID'])) {
+      $newKey = ++$maxNavID;
+      $nodes[$origKey]['attributes']['navID'] = $newKey;
+      $nodes[$newKey] = $nodes[$origKey];
+      unset($nodes[$origKey]);
+      $origKey = $newKey;
+    }
+    if (isset($nodes[$origKey]['child']) && is_array($nodes[$origKey]['child'])) {
+      _civirules_civix_fixNavigationMenuItems($nodes[$origKey]['child'], $maxNavID, $nodes[$origKey]['attributes']['navID']);
+    }
+  }
+}
+
 /**
  * (Delegated) Implements hook_civicrm_alterSettingsFolders().
  *
@@ -269,7 +336,9 @@ function _civirules_civix_insert_navigation_menu(&$menu, $path, $item, $parentId
  */
 function _civirules_civix_civicrm_alterSettingsFolders(&$metaDataFolders = NULL) {
   static $configured = FALSE;
-  if ($configured) return;
+  if ($configured) {
+    return;
+  }
   $configured = TRUE;
 
   $settingsDir = __DIR__ . DIRECTORY_SEPARATOR . 'settings';
-- 
GitLab