From 0956138b3dae192c3bb8db2b769e7526d1ec489e Mon Sep 17 00:00:00 2001
From: Silvio <silvio@devlet.com.br>
Date: Fri, 20 Aug 2010 18:49:49 -0300
Subject: Adding simple search app

---
 apps/search.php        |  48 +++++++++++++++++++
 classes/IsisFinder.php | 122 +++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 170 insertions(+)
 create mode 100644 apps/search.php

diff --git a/apps/search.php b/apps/search.php
new file mode 100644
index 0000000..cd75339
--- /dev/null
+++ b/apps/search.php
@@ -0,0 +1,48 @@
+<?php
+/**
+ * Cinisis - Isis db reading tool.
+ *
+ * @todo
+ *   Checkbox for $match.
+ */
+
+// Import requisites.
+require_once '../index.php';
+
+// Get input data.
+$entry = CinisisHttpHelper::getNumericArg('entry');
+$fid   = CinisisHttpHelper::getNumericArg('fid');
+$sid   = CinisisHttpHelper::getTextualArg('sid');
+$text  = CinisisHttpHelper::getTextualArg('text');
+
+// Draw the document.
+$display = new CinisisDisplayHelper('Text finder');
+$form    = $display->formInputText('entry', $entry);
+$form   .= $display->formInputText('fid', $fid);
+$form   .= $display->formInputText('sid', $sid);
+$form   .= $display->formInputText('text', $text);
+$script  = basename(__FILE__);
+$display->form($form, $script);
+
+// Get a db instance.
+$isis = new IsisFinder();
+
+// Setup database and entry number.
+if ($isis) {
+  // Query database.
+  $field = $isis->getFieldArray($fid);
+  $item  = ($sid == 'main') ? 'main' : $isis->getSubfieldName($fid, $sid);
+  list($entry, $result) = $isis->nextResult($field, $item, $text, $entry);
+
+  // Navigation bar.
+  $display->navbar($entry, $isis->entries, $script, '&fid='. $fid . '&sid='. $sid . '&text='. $text);
+
+  // Format output.
+  $display->pre("Selected field: $fid: ". $field['name'] .".");
+  $display->pre("Selected item: $sid: $item.");
+  $display->pre("Showing entry ". $display->entryLink($entry) ." from ". $isis->entries ." total entries.");
+  $display->pre("Repetitions found: ". count($result[$field['name']]) .".");
+  $display->dump($result[$field['name']]);
+}
+
+$display->footer();
diff --git a/classes/IsisFinder.php b/classes/IsisFinder.php
index 7f7405d..a4da3a0 100644
--- a/classes/IsisFinder.php
+++ b/classes/IsisFinder.php
@@ -94,4 +94,126 @@ class IsisFinder extends IsisConnector {
 
     return FALSE;
   }
+
+  /**
+   * Search the next match inside a result.
+   *
+   * @param $field
+   *   Field data.
+   *
+   * @param $item
+   *   Item name (main field or subfield).
+   *
+   * @param $search
+   *   Search token.
+   *
+   * @param $entry
+   *   Start entry number to begin the search.
+   *
+   * @param $match
+   *   Set to false do find the next result where the
+   *   item has not the token specified in $search.
+   */
+  public function nextResult($field, $item, $search, $entry = 1, $match = TRUE) {
+    do {
+      // Get the next entry that has the field/subfield we'll look at.
+      if ($item = 'main') {
+        $next = $this->nextField($field, $entry);
+      }
+      else {
+        $next = $this->nextSubfield($field, $item, $entry);
+      }
+
+      // Check if there's a next matching field/subfield.
+      if ($next === FALSE) {
+        break;
+      }
+      else {
+        list($entry, $result) = $next;
+      }
+
+      // Search for any occurrence.
+      foreach (new IsisRowIterator($this, $field) as $row) {
+        if ($item = 'main') {
+          $verify = $this->matchMainItem($field, $row, $search, $match);
+        }
+        else {
+          $verify = $this->matchSubfield($field, $row, $search, $match);
+        }
+
+        if ($verify !== FALSE)
+        {
+          return array($entry, $result);
+        }
+      }
+    }
+    while ($entry++);
+  }
+
+  /**
+   * Check if a main item match a given value.
+   *
+   * @param $field
+   *   Field data.
+   *
+   * @param $row
+   *   Row number.
+   *
+   * @param $search
+   *   Search token.
+   *
+   * @param $match
+   *   Set to false do find the next result where the
+   *   item has not the token specified in $search.
+   *
+   * @return
+   *   True if match, false otherwise.
+   */
+  public function matchMainItem($field, $row, $search, $match) {
+    if ($this->getMainItem($field, $row) == $search) {
+      if ($match) {
+        return TRUE;
+      }
+    }
+    elseif (!$match) {
+      return TRUE;
+    }
+
+    return FALSE;
+  }
+
+  /**
+   * Check if a subfield match a given value.
+   *
+   * @param $field
+   *   Field data.
+   *
+   * @param $subfield
+   *   Subfield name.
+   *
+   * @param $row
+   *   Row number.
+   *
+   * @param $search
+   *   Search token.
+   *
+   * @param $match
+   *   Set to false do find the next result where the
+   *   item has not the token specified in $search.
+   *
+   * @return
+   *   True if match, false otherwise.
+   */
+  public function matchSubfield($field, $subfield, $row, $search, $match) {
+    if ($this->getSubfield($field, $subfield, $row) == $search) {
+      if ($match) {
+        return TRUE;
+      }
+      elseif (!$match) {
+        return TRUE;
+      }
+    }
+
+    return FALSE;
+  }
 }
-- 
cgit v1.2.3