Merge pull request #6378 from johanv/CRM-15991-permission_delete_relationship
[civicrm-core.git] / api / v3 / utils.php
index bb57d3aba0fcf5ea54d67af691fd8971eab0adad..e2a204c2c1b4ed60427f091e4667b8c01dc6270f 100644 (file)
@@ -2284,3 +2284,61 @@ function _civicrm_api3_field_value_check(&$params, $fieldName, $type = NULL) {
   }
   return array($fieldValue, $op);
 }
+
+/**
+ * A generic "get" API based on simple array data. This is comparable to
+ * _civicrm_api3_basic_get but does not use DAO/BAO. This is useful for
+ * small/mid-size data loaded from external JSON or XML documents.
+ *
+ * @param array $params
+ *   API parameters.
+ * @param array $records
+ *   List of all records.
+ * @param string $idCol
+ *   The property which defines the ID of a record
+ * @param array $fields
+ *   List of filterable fields.
+ * @return array
+ */
+function _civicrm_api3_basic_array_get($entity, $params, $records, $idCol, $fields) {
+  $options = _civicrm_api3_get_options_from_params($params, TRUE, $entity, 'get');
+  // TODO // $sort = CRM_Utils_Array::value('sort', $options, NULL);
+  $offset = CRM_Utils_Array::value('offset', $options);
+  $limit = CRM_Utils_Array::value('limit', $options);
+
+  $matches = array();
+
+  $currentOffset = 0;
+  foreach ($records as $record) {
+    if ($idCol != 'id') {
+      $record['id'] = $record[$idCol];
+    }
+    $match = TRUE;
+    foreach ($params as $k => $v) {
+      if ($k == 'id') {
+        $k = $idCol;
+      }
+      if (in_array($k, $fields) && $record[$k] != $v) {
+        $match = FALSE;
+        break;
+      }
+    }
+    if ($match) {
+      if ($currentOffset >= $offset) {
+        $matches[$record[$idCol]] = $record;
+      }
+      if ($limit && count($matches) >= $limit) {
+        break;
+      }
+      $currentOffset++;
+    }
+  }
+
+  $return = CRM_Utils_Array::value('return', $options, array());
+  if (!empty($return)) {
+    $return['id'] = 1;
+    $matches = CRM_Utils_Array::filterColumns($matches, array_keys($return));
+  }
+
+  return civicrm_api3_create_success($matches, $params);
+}