[ansel] Patch for date searching

Heath S. Hendrickson heath at outerspaceconsultants.com
Sun May 23 14:03:22 PDT 2004


Chuck Hagenbuch wrote:

>
> Please resubmit this without the old diffs, so that it's just these 
> changes
> against current (as of tonight) CVS, and also with an eye to the style 
> stuff I
> mentioned.
>
Attached is a new diff against the current version of search.php in 
CVS.  It should have the appropriate style as requested.

I cleaned it up a bit and discovered the Horde_Form_Type of 'date' which 
made it a lot easier to handle the dates.  I also inserted a couple of 
'spacer' form variables to break it apart into logical groupings.

I attempted to use the Horde_Form_Action_conditional_enable to make 
certain form fields visible/invisible based on the value of the select 
box for search type, but it wasn't working and I noticed that no other 
Horde application uses it, so I can only assume that either I'm not 
calling it correctly or it's not fully functional.  I think I was 
calling it correctly as it was adding to the output, but the JavaScript 
code didn't quite look right.

h


-------------- next part --------------
Index: search.php
===================================================================
RCS file: /usr/local/horde/cvs/ansel/search.php,v
retrieving revision 1.6
diff -u -r1.6 search.php
--- search.php	22 May 2004 03:37:28 -0000	1.6
+++ search.php	23 May 2004 20:57:16 -0000
@@ -1,6 +1,6 @@
 <?php
 /**
- * $Horde: ansel/search.php,v 1.5 2004/05/18 15:25:28 chuck Exp $
+ * $Horde: ansel/search.php,v 1.4 2004/05/18 11:37:25 jan Exp $
  *
  * Copyright 2001-2004 Chuck Hagenbuch <chuck at horde.org>
  *
@@ -13,6 +13,7 @@
 require_once ANSEL_BASE . '/lib/Exif.php';
 require_once 'Horde/Form.php';
 require_once 'Horde/Form/Renderer.php';
+require_once 'Horde/Form/Action.php';
 require_once 'Horde/Template.php';
 require_once 'Horde/Variables.php';
 
@@ -27,21 +28,24 @@
         $this->setButtons(_("Search"));
         $this->addHidden('', 'actionID', 'text', false);
 
-        // Get known Exif fields.
+        // This gets the known Exif fields.
         $fields = Ansel_ImageData::getFields();
 
-        // Add additional Ansel-specific fields.
-        $fields = array_merge($fields,
-                              array('description' => array('description' => _("Description"), 'type' => 'text'),
-                                    'date-uploaded' => array('description' => _("Date Uploaded"), 'type' => 'date'),
-                                    'filename' => array('description' => _("Filename"), 'type' => 'text'),
-                                    'gallery' => array('description' => _("Gallery Name"), 'type' => 'text')));
+        // Now add on any others that we may want.
+        $fields += array('description' => array('description' => _("Description"), 'type' => 'text'),
+                         'date-uploaded' => array('description' => _("Date Uploaded"), 'type' => 'date'),
+                         'filename' => array('description' => _("Filename"), 'type' => 'text'),
+                         'gallery' => array('description' => _("Gallery Name"), 'type' => 'text'));
 
-        // Build the enum for the fields to search.
+        // reformat the array so that the Form Renderer likes it
+        // amd grab only the non-date based fields
         $searchFields = array();
         foreach ($fields as $field => $data) {
-            $searchFields[$field] = $data['description'];
+            if ($data['type'] != 'date') {
+                $searchFields += array($field => $data['description']);
+            }
         }
+ 
         asort($searchFields);
 
         $ops = array('=' => _("Is"),
@@ -51,14 +55,42 @@
                      'LIKE' => _("Contains"),
                      'NOT LIKE' => _("Does Not Contain"));
 
-        $this->addVariable(_("Field"), 'field', 'enum', false, false, null, array($searchFields));
-        $this->addVariable(_("Operator"), 'op', 'enum', false, false, null, array($ops));
-        $this->addVariable(_("Value"), 'value', 'text', false, false, null);
-    }
+        // add the select box to tell us what type of search to perform
+        $this->addVariable(_("Search Type"), 'searchType', 'enum', false, false, null, 
+                           array(array('TEXT' => _("Text Fields"), 'DATE' => _("Date Fields"))));
+
+        $this->addVariable(null, null, 'spacer', false, false, null);
+
+        // add the text search form items
+        $this->addVariable(_("Text Field"), 'textField', 'enum', false, false, null, array($searchFields));
+        $this->addVariable(_("Text Operator"), 'textOp', 'enum', false, false, null, array($ops));
+        $this->addVariable(_("Text Value"), 'textValue', 'text', false, false, null);
+        $this->addVariable(null, null, 'spacer', false, false, null);
 
+        // reformat the array so that the Form Renderer likes it
+        // and grab only the date based fields.
+        $searchFields = array();
+        foreach ($fields as $field => $data) {
+            if ($data['type'] == 'date') {
+                $searchFields[$field] = $data['description'];
+            }
+        }
+ 
+        asort($searchFields);
+
+        $ops = array('BETWEEN' => _("Between"),
+                     '>' => _("After"),
+                     '<' => _("Before"));
+
+        $this->addVariable(_("Date Field"), 'dateField', 'enum', false, false, null, array($searchFields));
+        $this->addVariable(_("Date Operator"), 'dateOp', 'enum', false, false, null, array($ops));
+        $this->addVariable(_("Start Date"), 'startDate', 'monthDayYear', false, false, null);
+        $this->addVariable(_("End Date"), 'endDate', 'monthDayYear', false, false, null);
+    }
 }
 
 $actionID = Util::getFormData('actionID');
+$searchType = Util::getFormData('searchType');
 $vars = &Variables::getDefaultVariables();
 $title = _("Search for Images");
 require ANSEL_TEMPLATES . '/common-header.inc';
@@ -67,67 +99,143 @@
 $vars->set('actionID', 'search');
 $form = &Horde_Form::singleton('SearchForm', $vars, sprintf(_("Search for Images"), $title));
 
+// this should be automatically included by the setAction() calls above, but it's not, so let's include
+// it here manually.
+Horde::addScriptFile('form_helpers.js', 'horde');
+
 $renderer = &new Horde_Form_Renderer();
 $form->renderActive($renderer, $vars, 'search.php', 'post', 'multipart/form-data');
 
 // Now display any results, if a search was performed.
 switch ($actionID) {
 case 'search':
-    // Set up the search criteria to use.
-    $attr_name = Util::getFormData('field');
-    $attr_op = Util::getFormData('op');
-    $attr_value = Util::getFormData('value');
-
-    // Escape any SQL wildcards.
-    $attr_value = str_replace(array('%', '_'), array('\%', '\_'), $attr_value);
-
-    // For the CONTAINS and DOES NOT CONTAIN cases, we add the SQL
-    // regex wildcards to the front and back of the value.
-    if ($attr_op == 'LIKE' || $attr_op == 'NOT LIKE') {
-        $attr_value = '%' . $attr_value . '%';
-    } elseif ($attr_op == 'BEGIN') {
-        $attr_op = 'LIKE';
-        $attr_value = $attr_value . '%';
-    } elseif ($attr_op == 'END') {
-        $attr_op = 'LIKE';
-        $attr_value = '%' . $attr_value;
-    }
+    switch ($searchType) {
+    case 'TEXT':
+        // Set up the search criteria to use.
+        $attr_name = Util::getFormData('textField');
+        $attr_op = Util::getFormData('textOp');
+        $attr_value = Util::getFormData('textValue');
+
+        // Escape any SQL wildcards.
+        $attr_value = str_replace(array('%', '_'), array('\%', '\_'), $attr_value);
+
+        // For the CONTAINS and DOES NOT CONTAIN cases, we add the SQL
+        // regex wildcards to the front and back of the value.
+        if ($attr_op == 'LIKE' || $attr_op == 'NOT LIKE') {
+            $attr_value = '%' . $attr_value . '%';
+        } elseif ($attr_op == 'BEGIN') {
+            $attr_op = 'LIKE';
+            $attr_value = $attr_value . '%';
+        } elseif ($attr_op == 'END') {
+            $attr_op = 'LIKE';
+            $attr_value = '%' . $attr_value;
+        }
+    
+        $criteria['AND'] = array(array('field' => 'name',
+                                       'op' => '=',
+                                       'test' => $attr_name),
+                                 array('field' => 'value',
+                                       'op' => $attr_op,
+                                       'test' => $attr_value));
+    
+        $imagelist = $ansel_shares->searchAllImages($criteria);
+    
+        // Now display the results.
+        Horde::addScriptFile('popup.js', 'horde');
+    
+        $i = 0;
+        $images = array();
+        foreach ($imagelist as $id => $name) {
+            $image = &$ansel_shares->getImage($name);
+            $galleryName = $image->get('gallery');
+            $gallery = $ansel_shares->getShare($galleryName);
+            // check that the user has permission to view this image before displaying the link
+            if (is_a($gallery, 'PEAR_Error') || !$gallery->hasPermission(Auth::getAuth(), PERMS_READ)) {
+                continue;
+            }
+    
+            $img = Util::addParameter('image.php', array('gallery' => $image->get('gallery'),
+                                                        'image' => $image->getId(),
+                                                        'actionID' => 'imagethumb'));
+            $images[] = array('i' => ($i++ %2),
+                            'image' => Horde::link('', _("Thumbnail"), '', '', 'popup(\'' . Horde::applicationUrl($img) . '\',' . ($conf['thumbnail']['width'] + 10) . ', ' . ($conf['thumbnail']['height'] + 10) . '); return false;') . $image->get('filename') . '</a>',
+                            'gallery' => Horde::link(Horde::applicationUrl(Util::addParameter('view.php', array('gallery' => $galleryName, 'page' => '1')))) . $gallery->get('name') . '</a>',
+                            'description' => $image->get('description') ? htmlspecialchars($image->get('description')) : '&nbsp;');
+        }
 
-    $criteria['AND'] = array(array('field' => 'name',
-                                   'op' => '=',
-                                   'test' => $attr_name),
-                             array('field' => 'value',
-                                   'op' => $attr_op,
-                                   'test' => $attr_value));
-
-    $imagelist = $ansel_shares->searchAllImages($criteria);
-
-    // Now display the results.
-    Horde::addScriptFile('popup.js', 'horde');
-
-    $i = 0;
-    $images = array();
-    foreach ($imagelist as $id => $name) {
-        $image = &$ansel_shares->getImage($name);
-        $galleryName = $image->get('gallery');
-        $gallery = $ansel_shares->getShare($galleryName);
-        if (is_a($gallery, 'PEAR_Error') || !$gallery->hasPermission(Auth::getAuth(), PERMS_READ)) {
-            continue;
-        }
-
-        $img = Util::addParameter('image.php', array('gallery' => $image->get('gallery'),
-                                                     'image' => $image->getId(),
-                                                     'actionID' => 'imagethumb'));
-        $images[] = array('i' => ($i++ %2),
-                          'image' => Horde::link('', _("Thumbnail"), '', '', 'popup(\'' . Horde::applicationUrl($img) . '\',' . ($conf['thumbnail']['width'] + 10) . ', ' . ($conf['thumbnail']['height'] + 10) . '); return false;') . $image->get('filename') . '</a>',
-                          'gallery' => Horde::link(Horde::applicationUrl(Util::addParameter('view.php', array('gallery' => $galleryName, 'page' => '1')))) . $gallery->get('name') . '</a>',
-                          'description' => $image->get('description') ? htmlspecialchars($image->get('description')) : '&nbsp;');
+        $template = &new Horde_Template();
+        $template->set('images', $images);
+        $template->set('heading', count($images) == 1 ? _("Matched 1 image") : sprintf(_("Matched %s images"), count($images)));
+        echo $template->fetch(ANSEL_TEMPLATES . '/search/search.html');
+        break;
+
+    case 'DATE':
+        $attr_name = Util::getFormData('dateField');
+        $attr_op = Util::getFormData('dateOp');
+        $attr_startDate = Util::getFormData('startDate');
+        $attr_endDate = Util::getFormData('endDate');
+
+        // convert the date form fields to a timestamp
+        $startDate = strtotime(sprintf("%s/%s/%s 00:00:00", $attr_startDate['month'], $attr_startDate['day'], $attr_startDate['year']));
+        $endDate = strtotime(sprintf("%s/%s/%s 00:00:00", $attr_endDate['month'], $attr_endDate['day'], $attr_endDate['year']));
+
+        // set up the proper search criteria
+        $criteria = array();
+        if ($attr_op == "BETWEEN") {
+            $criteria['AND'] = array(array('AND' => array(array('field' => 'name',
+                                                                'op' => '=',
+                                                                'test' => $attr_name),
+                                                          array('field' => 'value',
+                                                                'op' => '>',
+                                                                'test' => $startDate))),
+                                    array('AND' => array(array('field' => 'name',
+                                                                'op' => '=',
+                                                                'test' => $attr_name),
+                                                         array('field' => 'value',
+                                                                'op' => '<',
+                                                                'test' => $endDate))));
+        } else {
+            $criteria['AND'] = array(array('field' => 'name',
+                                           'op' => '=',
+                                           'test' => $attr_name),
+                                     array('field' => 'value',
+                                           'op' => $attr_op,
+                                           'test' => $startDate));
+        }
+    
+        // actually do the search
+        $imagelist = $ansel_shares->searchAllImages($criteria);
+
+        // Now display the results.
+        Horde::addScriptFile('popup.js', 'horde');
+    
+        $i = 0;
+        $images = array();
+        foreach ($imagelist as $id => $name) {
+            $image = &$ansel_shares->getImage($name);
+            $galleryName = $image->get('gallery');
+            $gallery = $ansel_shares->getShare($galleryName);
+            // check that the user has permission to view this image before displaying the link
+            if (is_a($gallery, 'PEAR_Error') || !$gallery->hasPermission(Auth::getAuth(), PERMS_READ)) {
+                continue;
+            }
+    
+            $img = Util::addParameter('image.php', array('gallery' => $image->get('gallery'),
+                                                        'image' => $image->getId(),
+                                                        'actionID' => 'imagethumb'));
+            $images[] = array('i' => ($i++ %2),
+                            'image' => Horde::link('', _("Thumbnail"), '', '', 'popup(\'' . Horde::applicationUrl($img) . '\',' . ($conf['thumbnail']['width'] + 10) . ', ' . ($conf['thumbnail']['height'] + 10) . '); return false;') . $image->get('filename') . '</a>',
+                            'gallery' => Horde::link(Horde::applicationUrl(Util::addParameter('view.php', array('gallery' => $galleryName, 'page' => '1')))) . $gallery->get('name') . '</a>',
+                            'description' => $image->get('description') ? htmlspecialchars($image->get('description')) : '&nbsp;');
+        }
+    
+        $template = &new Horde_Template();
+        $template->set('images', $images);
+        $template->set('heading', count($images) == 1 ? _("Matched 1 image") : sprintf(_("Matched %s images"), count($images)));
+        echo $template->fetch(ANSEL_TEMPLATES . '/search/search.html');
+        break;
     }
 
-    $template = &new Horde_Template();
-    $template->set('images', $images);
-    $template->set('heading', count($images) == 1 ? _("Matched 1 image") : sprintf(_("Matched %s images"), count($images)));
-    echo $template->fetch(ANSEL_TEMPLATES . '/search/search.html');
 }
 
 require $registry->getParam('templates', 'horde') . '/common-footer.inc';


More information about the ansel mailing list