CRM-14765 - CRM_Core_FileSearchInterface - Allow modules to define file-search mechanism
authorTim Otten <totten@civicrm.org>
Tue, 10 Jun 2014 01:58:32 +0000 (18:58 -0700)
committerTim Otten <totten@civicrm.org>
Tue, 10 Jun 2014 02:52:16 +0000 (19:52 -0700)
CRM/Core/BAO/File.php
CRM/Core/FileSearchInterface.php [new file with mode: 0644]
CRM/Utils/Hook.php

index 78047e9e9f6d25905c1d968f3c1bc428e475ede0..3d0d59ab6856b9c637bfc1cd03237622cdddf527 100644 (file)
@@ -677,4 +677,21 @@ AND       CEF.entity_id    = %2";
     }
     return $results;
   }
+
+  /**
+   * Get a reference to the file-search service (if one is available).
+   *
+   * @return CRM_Core_FileSearchInterface|NULL
+   */
+  static function getSearchService() {
+    $fileSearches = array();
+    CRM_Utils_Hook::fileSearches($fileSearches);
+
+    // use the first available search
+    foreach ($fileSearches as $fileSearch) {
+      /** @var $fileSearch CRM_Core_FileSearchInterface */
+      return $fileSearch;
+    }
+    return NULL;
+  }
 }
diff --git a/CRM/Core/FileSearchInterface.php b/CRM/Core/FileSearchInterface.php
new file mode 100644 (file)
index 0000000..8ee91ca
--- /dev/null
@@ -0,0 +1,23 @@
+<?php
+interface CRM_Core_FileSearchInterface {
+  const DEFAULT_SEARCH_LIMIT = 20;
+  const DEFAULT_SEARCH_OFFSET = 0;
+
+  /**
+   * @param array $query any of the following:
+   *  - text: string, plain text to search for
+   *  - parent_table: string - entity to which file is directly attached
+   *  - parent_id: int - entity to which file is directly attached
+   *  - xparent_table: string - business-entity to which file is attached (directly or indirectly)
+   *  - xparent_id: int - business-entity to which file is attached (directly or indirectly)
+   * @param int $limit
+   * @param int $offset
+   * @return array each item has keys:
+   *  - file_id: int
+   *  - parent_table: string - entity to which file is directly attached
+   *  - parent_id: int - entity to which file is directly attached
+   *  - xparent_table: string - business-entity to which file is attached (directly or indirectly)
+   *  - xparent_id: int - business-entity to which file is attached (directly or indirectly)
+   */
+  function search($query, $limit = self::DEFAULT_SEARCH_LIMIT, $offset = self::DEFAULT_SEARCH_OFFSET);
+}
\ No newline at end of file
index 43420ad9bead8473bdfb867c02dd2edee1f55c31..bf0b47f4b7297e2b2d57e77fe0758d3e00785da2 100644 (file)
@@ -1717,4 +1717,15 @@ abstract class CRM_Utils_Hook {
       'civicrm_crudLink'
     );
   }
+
+  /**
+   * @param array<CRM_Core_FileSearchInterface> $fileSearches
+   * @return mixed
+   */
+  static function fileSearches(&$fileSearches) {
+    return self::singleton()->invoke(1, $fileSearches,
+      self::$_nullObject, self::$_nullObject, self::$_nullObject, self::$_nullObject, self::$_nullObject,
+      'civicrm_fileSearches'
+    );
+  }
 }