Support adding fields on the fly
[disclosr.git] / getAgency.php
blob:a/getAgency.php -> blob:b/getAgency.php
--- a/getAgency.php
+++ b/getAgency.php
@@ -1,9 +1,10 @@
 <?php
 
-include_once('common.inc.php');
+include_once('include/common.inc.php');
 include_header();
 
 function displayValue($key, $value, $mode) {
+    global $db;
     if ($mode == "view") {
         if (is_array($value)) {
             echo "<tr><td>$key</td><td><ol>";
@@ -30,10 +31,25 @@
         } else {
             if (strpos($key, "_") === 0) {
                 echo"<input type='hidden' id='$key' name='$key' value='$value'/>";
-            } if (strpos($key, "has") === 0) {
-                echo "<label for='$key'><input type='checkbox' id='$key' name='$key' value='$value'> $key</label>";
+                
+            } else if ($key == "parentOrg") {
+                echo "<label for='$key'>$key</label><select  id='$key' name='$key'><option value=''> Select... </option>";
+                  $rows = $db->get_view("app", "byDeptStateName")->rows;
+    //print_r($rows);
+    foreach ($rows as $row) {
+        echo "<option value='{$row->value}'".(($row->value == $value) ? "SELECTED":"")." >".str_replace("Department of ","",$row->key)."</option>";
+    }
+    echo" </select>";
+            } else if (strpos($key, "has") === 0) {
+                echo "<label for='$key'><input type='checkbox' id='$key' name='$key' ".(($value=='on' || $value=='true')?"checked='$value'":"")."> $key</label>";
             } else {
-                echo "<label>$key</label><input  class='input-text' type='text' id='$key' name='$key' value='$value'/></tr>";
+                echo "<label>$key</label><input  class='input-text' type='text' id='$key' name='$key' value='$value'/>";
+                if ((strpos($key,"URL") > 0 || $key == 'website')&& $value != "") {
+                   echo "<a href='$value'>view</a>"; 
+                }
+                if ($key == 'abn') {
+                   echo "<a href='http://www.abr.business.gov.au/SearchByAbn.aspx?SearchText=$value'>view abn</a>";
+                }
             }
         }
     }
@@ -41,10 +57,24 @@
 }
 
 function addDefaultFields($row) {
-    $defaultFields = Array("name");
+    global $schemas;
+    $defaultFields = array_keys($schemas['agency']['properties']);
     foreach ($defaultFields as $defaultField) {
-        if (!isset($row[$defaultField]))
+        if (!isset($row[$defaultField])) {
+            if ($schemas['agency']['properties'][$defaultField]['type'] == "string") {
+                if (strpos($defaultField, "has") === 0) {
+            $row[$defaultField] = "false";
+                
+                } else {
             $row[$defaultField] = "";
+                }
+            
+            }
+             if ($schemas['agency']['properties'][$defaultField]['type'] == "array") {
+                
+            $row[$defaultField] = Array("");
+            }
+        }
     }
     return $row;
 }
@@ -60,11 +90,21 @@
     //print_r($row);
     if (sizeof($_POST) > 0) {
         //print_r($_POST);
+        foreach ($_POST as $postkey => $postvalue) {
+            if ($postvalue == "") {
+                unset($_POST[$postkey]);
+            }
+            if (is_array($postvalue) && count($postvalue) == 1 && $postvalue[0] == "") {
+                unset($_POST[$postkey]);
+            }
+        }
         if (isset($_POST['_id']) && $db->get_rev($_POST['_id']) == $_POST['_rev']) {
             echo "Edited version was latest version, continue saving";
-            $row = $db->save($_POST);
+            $newdoc = $_POST;
+            $newdoc['metadata']['lastModified'] = time();
+            $row = $db->save($newdoc);
         } else {
-            echo "ALERT doc revised by someone else while editing.";
+            echo "ALERT doc revised by someone else while editing. Document not saved.";
         }
     }
 
@@ -82,14 +122,14 @@
             window.onload = function() {
                 $(document).ready(function() {
                     // put all your jQuery goodness in here.
-// http://charlie.griefer.com/blog/2009/09/17/jquery-dynamically-adding-form-elements/
+                    // http://charlie.griefer.com/blog/2009/09/17/jquery-dynamically-adding-form-elements/
                     $('#addfield').click(function() {
-                        var field_name=window.prompt("What is your name?","");
+                        var field_name=window.prompt("fieldname?","");
                         if (field_name !="") {
                             $('#submitbutton').before($('<span></span>')
                             .append("<label>"+field_name+"</label>")
                             .append("<input  class='input-text' type='text' id='"+field_name+"' name='"+field_name+"'/>")
-                            );
+                        );
                         }
                     });
                 });
@@ -111,12 +151,20 @@
     } else {
 
         try {
-            $rows = $db->get_view("app", "showNamesABNs")->rows;
+            /*$rows = $db->get_view("app", "showNamesABNs")->rows;
             //print_r($rows);
             foreach ($rows as $row) {
                 //   print_r($row);
                 echo '<li><a href="getAgency.php?id=' . $row->key . '">' .
                 (isset($row->value->name) && $row->value->name != "" ? $row->value->name : "NO NAME " . $row->value->abn)
+                . '</a></li>';
+            }*/
+            $rows = $db->get_view("app", "byName")->rows;
+            //print_r($rows);
+            foreach ($rows as $row) {
+                //   print_r($row);
+                echo '<li><a href="getAgency.php?id=' . $row->value . '">' .
+                $row->key
                 . '</a></li>';
             }
         } catch (SetteeRestClientException $e) {
@@ -124,3 +172,4 @@
         }
     }
     include_footer();
+    ?>