Convert search templates to Horde_View.
authorJan Schneider <jan@horde.org>
Wed, 9 Jun 2010 22:48:17 +0000 (00:48 +0200)
committerJan Schneider <jan@horde.org>
Mon, 5 Jul 2010 10:34:43 +0000 (12:34 +0200)
turba/search.php
turba/templates/search/advanced.html.php
turba/templates/search/basic.html.php [new file with mode: 0644]
turba/templates/search/basic.inc [deleted file]
turba/templates/search/header.html.php [new file with mode: 0644]
turba/templates/search/header.inc [deleted file]
turba/templates/search/vbook.html.php [new file with mode: 0644]
turba/templates/search/vbook.inc [deleted file]

index ba6f414..1c2bd41 100644 (file)
@@ -109,11 +109,11 @@ if (is_a($driver, 'PEAR_Error')) {
     $map = $driver->getCriteria();
     if ($_SESSION['turba']['search_mode'] == 'advanced') {
         $criteria = array();
-        foreach ($map as $key => $value) {
+        foreach (array_keys($map) as $key) {
             if ($key != '__key') {
-                $val = Horde_Util::getFormData($key);
-                if (strlen($val)) {
-                    $criteria[$key] = $val;
+                $value = Horde_Util::getFormData($key);
+                if (strlen($value)) {
+                    $criteria[$key] = $value;
                 }
             }
         }
@@ -177,12 +177,59 @@ if (is_a($driver, 'PEAR_Error')) {
     }
 }
 
-if ($_SESSION['turba']['search_mode'] == 'basic') {
+/* Build all available search criteria. */
+$addressBooks = Turba::getAddressBooks();
+$allCriteria = $shareSources = array();
+foreach ($addressBooks as $key => $entry) {
+    $allCriteria[$key] = array();
+    foreach ($entry['search'] as $field) {
+        $allCriteria[$key][$field] = $GLOBALS['attributes'][$field]['label'];
+    }
+
+    /* Remember vbooks and sources that are using shares. */
+    $shareSources[$key] = $entry['type'] != 'vbook';
+}
+
+/* Build search mode tabs. */
+$sUrl = Horde::applicationUrl('search.php');
+$vars = Horde_Variables::getDefaultVariables();
+$tabs = new Horde_Ui_Tabs('search_mode', $vars);
+$tabs->addTab(_("Basic Search"), $sUrl, 'basic');
+$tabs->addTab(_("Advanced Search"), $sUrl, 'advanced');
+
+/* The form header. */
+$headerView = new Horde_View(array('templatePath' => TURBA_TEMPLATES . '/search'));
+if (count($addressBooks) == 1) {
+    $headerView->uniqueSource = key($addressBooks);
+}
+
+/* The search forms. */
+$searchView = new Horde_View(array('templatePath' => TURBA_TEMPLATES . '/search'));
+new Horde_View_Helper_Text($searchView);
+$searchView->addressBooks = $addressBooks;
+$searchView->shareSources = $shareSources;
+$searchView->attributes = $GLOBALS['attributes'];
+$searchView->allCriteria = $allCriteria;
+$searchView->map = $map;
+$searchView->source = $source;
+$searchView->criteria = $criteria;
+$searchView->value = $val;
+
+/* The form footer and vbook section. */
+$vbookView = new Horde_View(array('templatePath' => TURBA_TEMPLATES . '/search'));
+$vbookView->hasShare = !empty($_SESSION['turba']['has_share']);
+$vbookView->shareSources = $shareSources;
+$vbookView->source = $source;
+
+switch ($_SESSION['turba']['search_mode']) {
+case 'basic':
     $title = _("Basic Search");
     $notification->push('document.directory_search.val.focus();', 'javascript');
-} else {
+    break;
+case 'advanced':
     $title = _("Advanced Search");
     $notification->push('document.directory_search.name.focus();', 'javascript');
+    break;
 }
 
 Horde::addScriptFile('quickfinder.js', 'horde');
@@ -191,9 +238,10 @@ Horde::addScriptFile('redbox.js', 'horde');
 Horde::addScriptFile('search.js', 'turba');
 require TURBA_TEMPLATES . '/common-header.inc';
 require TURBA_TEMPLATES . '/menu.inc';
-require TURBA_TEMPLATES . '/search/header.inc';
-require TURBA_TEMPLATES . '/search/' . $_SESSION['turba']['search_mode'] . '.inc';
-require TURBA_TEMPLATES . '/search/vbook.inc';
+echo $tabs->render($_SESSION['turba']['search_mode']);
+echo $headerView->render('header');
+echo $searchView->render($_SESSION['turba']['search_mode']);
+echo $vbookView->render('vbook');
 if (isset($view) && is_object($view)) {
     require TURBA_TEMPLATES . '/browse/javascript.inc';
     require TURBA_TEMPLATES . '/browse/header.inc';
index b173e33..c3538f1 100644 (file)
@@ -2,30 +2,28 @@
  <tr>
   <td>&nbsp;</td>
   <td>
-   <input type="submit" class="button" name="search" value="<?php echo _("Search") ?>" />
-   <input type="reset" class="button" name="reset" value="<?php echo _("Reset to Defaults") ?>" />
+   <input type="submit" class="button" name="search" value="<?= _("Search") ?>" />
+   <input type="reset" class="button" name="reset" value="<?= _("Reset to Defaults") ?>" />
   </td>
  </tr>
- <?php if ($source_count > 1): ?>
+ <? if (count($this->addressBooks) > 1): ?>
  <tr>
-  <td class="rightAlign"><strong><?php echo Horde::label('source', _("Address Book")) ?></strong></td>
+  <td class="rightAlign"><strong><label for="source"><?= _("Address Book") ?></label></strong></td>
   <td class="leftAlign">
    <select id="source" name="source" onchange="directory_search.submit()">
-    <?php echo $source_options ?>
+    <? foreach ($this->addressBooks as $key => $entry): ?>
+    <option<?= $key == $this->source ? ' selected="selected"' : '' ?> value="<?= $key ?>"><?= $this->h($entry['title']) ?></option>
+    <? endforeach; ?>
    </select>
   </td>
  </tr>
-<?php endif; ?>
-<?php
-foreach ($map as $name => $v) {
-    if (substr($name, 0, 2) != '__') {
-?>
+<? endif; ?>
+<? foreach ($this->map as $name => $v): ?>
+<? if (substr($name, 0, 2) != '__'): ?>
  <tr>
-  <td width="1%" class="nowrap rightAlign" ><strong><?php echo Horde::label($name, $GLOBALS['attributes'][$name]['label']) ?></strong></td>
-  <td class="leftAlign"><input type="text" size="30" id="<?php echo $name ?>" name="<?php echo $name ?>" value="<?php echo isset($criteria[$name]) ? htmlspecialchars($criteria[$name]) : '' ?>" /></td>
+  <td width="1%" class="nowrap rightAlign" ><strong><label for="<?= $name ?>"><?= $this->h($this->attributes[$name]['label']) ?></label></strong></td>
+  <td class="leftAlign"><input type="text" size="30" id="<?= $name ?>" name="<?= $name ?>" value="<?= isset($this->criteria[$name]) ? $this->h($this->criteria[$name]) : '' ?>" /></td>
  </tr>
-<?php
-    }
-}
-?>
+<? endif; ?>
+<? endforeach; ?>
 </table>
diff --git a/turba/templates/search/basic.html.php b/turba/templates/search/basic.html.php
new file mode 100644 (file)
index 0000000..f4e87f4
--- /dev/null
@@ -0,0 +1,25 @@
+<script type="text/javascript">
+TurbaSearch.criteria = <?= json_encode($this->allCriteria) ?>;
+TurbaSearch.shareSources = <?= json_encode($this->shareSources) ?>;
+</script>
+
+<? if (count($this->addressBooks) > 1): ?>
+<strong><label for="source"><?= _("From") ?></label></strong>
+<select id="turbaSearchSource" name="source" onchange="TurbaSearch.updateCriteria();">
+  <? foreach ($this->addressBooks as $key => $entry): ?>
+  <option<?= $key == $this->source ? ' selected="selected"' : '' ?> value="<?= $key ?>"><?= $this->h($entry['title']) ?></option>
+  <? endforeach; ?>
+</select>
+<? endif; ?>
+
+<strong><label for="criteria"><?= _("Find") ?></label></strong>
+<select id="turbaSearchCriteria" name="criteria">
+  <? foreach ($this->addressBooks[$this->source]['search'] as $field): ?>
+  <option<?= $field == $this->criteria ? ' selected="selected"' : '' ?> value="<?= $field ?>"><?= $this->h($this->attributes[$field]['label'])
+ ?></option>
+  <? endforeach; ?>
+</select>
+
+<strong><label for="val"><?= _("Matching") ?></label></strong>
+<input type="text" size="30" id="val" name="val" value="<?php echo $this->h($this->val) ?>" />
+<input class="button" type="submit" name="search" value="<?= _("Search") ?>" />
diff --git a/turba/templates/search/basic.inc b/turba/templates/search/basic.inc
deleted file mode 100644 (file)
index f39e68c..0000000
+++ /dev/null
@@ -1,20 +0,0 @@
-<script type="text/javascript">
-TurbaSearch.criteria = <?php echo json_encode($criteria) ?>;
-TurbaSearch.shareSources = <?php echo json_encode($shareSources) ?>;
-</script>
-
-<?php if ($source_count > 1): ?>
- <strong><?php echo Horde::label('source', _("From")) ?></strong>
- <select id="turbaSearchSource" name="source" onchange="TurbaSearch.updateCriteria();">
-  <?php echo $source_options ?>
- </select>
-<?php endif; ?>
-
- <strong><?php echo Horde::label('criteria', _("Find")) ?></strong>
- <select id="turbaSearchCriteria" name="criteria">
-  <?php echo $criteria_options ?>
- </select>
-
- <strong><?php echo Horde::label('val', _("Matching")) ?></strong>
- <input type="text" size="30" id="val" name="val" value="<?php echo htmlspecialchars($val) ?>" />
- <input class="button" type="submit" name="search" value="<?php echo _("Search") ?>" />
diff --git a/turba/templates/search/header.html.php b/turba/templates/search/header.html.php
new file mode 100644 (file)
index 0000000..709118a
--- /dev/null
@@ -0,0 +1,6 @@
+<div class="text" style="padding:1em">
+<form name="directory_search" action="search.php" method="get" onsubmit="RedBox.loading(); return true;">
+<?= Horde_Util::formInput() ?>
+<? if ($this->uniqueSource): ?>
+<input type="hidden" id="turbaSearchSource" name="source" value="<?= $this->uniqueSource ?>" />
+<? endif; ?>
diff --git a/turba/templates/search/header.inc b/turba/templates/search/header.inc
deleted file mode 100644 (file)
index 7862d84..0000000
+++ /dev/null
@@ -1,50 +0,0 @@
-<?php
-/* Build the directory sources select widget. */
-$source_options = '';
-$criteria_options = '';
-$js_criteria = "var criteriaOptions = [];\n"
-    . "var shareSources = [];\n";
-
-$source_count = 0;
-$criteria = $shareSources = array();
-foreach (Turba::getAddressBooks() as $key => $entry) {
-    $criteria[$key] = array();
-
-    /* Build the criteria select widget. */
-    $field_count = 1;
-    foreach ($entry['search'] as $field) {
-        if ($key == $source) {
-            $selected = ($criteria == $field) ? ' selected="selected"' : '';
-            $criteria_options .= "<option value=\"$field\"$selected>" .
-                htmlspecialchars($GLOBALS['attributes'][$field]['label']) . "</option>\n";
-        }
-        $field_count++;
-        $criteria[$key][$field] = $GLOBALS['attributes'][$field]['label'];
-    }
-
-    $selected = ($key == $source) ? ' selected="selected"' : '';
-    $source_options .= "<option value=\"$key\"$selected>"
-        . htmlspecialchars($entry['title']) . "</option>\n";
-
-    $unique_source = $key;
-    $source_count++;
-
-    /* Remember vbooks and sources that are using shares. */
-    $shareSources[$key] = $entry['type'] != 'vbook';
-}
-
-/* Build search mode tabs. */
-$sUrl = Horde::applicationUrl('search.php');
-$vars = Horde_Variables::getDefaultVariables();
-$tabs = new Horde_Ui_Tabs('search_mode', $vars);
-$tabs->addTab(_("Basic Search"), $sUrl, 'basic');
-$tabs->addTab(_("Advanced Search"), $sUrl, 'advanced');
-echo $tabs->render($_SESSION['turba']['search_mode']);
-
-?>
-<div class="text" style="padding:1em">
-<form name="directory_search" action="search.php" method="get" onsubmit="RedBox.loading(); return true;">
-<?php echo Horde_Util::formInput() ?>
-<?php if ($source_count == 1): ?>
- <input type="hidden" id="turbaSearchSource" name="source" value="<?php echo $unique_source ?>" />
-<?php endif; ?>
diff --git a/turba/templates/search/vbook.html.php b/turba/templates/search/vbook.html.php
new file mode 100644 (file)
index 0000000..44b2fc8
--- /dev/null
@@ -0,0 +1,17 @@
+<? if ($this->hasShare): ?>
+<div id="vbook-form"<?= $this->shareSources[$this->source] ? '' : ' style="display:none"' ?>>
+  <input type="checkbox" id="save-vbook" name="save_vbook" />
+  <strong><label for="save-vbook"><?= _("Save search as a virtual address book?") ?></label></strong>
+
+  <label for="vbook_name"><?= _("Name:") ?></label>
+  <input type="text" id="vbook_name" name="vbook_name" />
+  <script type="text/javascript">
+  $('vbook_name').observe('keyup', function() {
+      $('save-vbook').checked = !!$F('vbook_name');
+  });
+  </script>
+</div>
+<? endif; ?>
+</form>
+</div>
+<br />
diff --git a/turba/templates/search/vbook.inc b/turba/templates/search/vbook.inc
deleted file mode 100644 (file)
index 087fdcb..0000000
+++ /dev/null
@@ -1,17 +0,0 @@
-<?php if (!empty($_SESSION['turba']['has_share'])): ?>
- <div id="vbook-form"<?php if (is_a($driver, 'Turba_Driver_vbook')) echo ' style="display:none"' ?>>
-  <input type="checkbox" id="save-vbook" name="save_vbook" />
-  <strong><?php echo Horde::label('save-vbook', _("Save search as a virtual address book?")) ?></strong>
-
-  <?php echo Horde::label('vbook_name', _("Name:")) ?>
-  <input type="text" id="vbook_name" name="vbook_name" />
-  <script type="text/javascript">
-  Event.observe($('vbook_name'), 'keyup', function() {
-      $('save-vbook').checked = $F('vbook_name') ? true : false;
-  });
-  </script>
- </div>
-<?php endif; ?>
-</form>
-</div>
-<br />