}
    $copyright_holders[] = sprintf("<a href='%s'>%s</a>", $org_url, $org_name);
}
$year = date('Y');
$legal_buf = preg_replace('/<!-- YEAR -->/', $year, $legal_buf);
$legal_buf = preg_replace('/<!-- COPYRIGHT_HOLDER -->/', implode(', ', $copyright_holders), $legal_buf);
if (!$callback) {
    ?>
<html>
 <head>
  <link rel="stylesheet" href="css/pinform.css"/>
  <style>
    body {
        font: 15px Helvetica, Helvetica Neue, Arial, 'sans serif';
    }
  </style>
 </head>
 <body>
 <?php 
    echo $legal_buf;
    ?>
 </body>
</html>
<?php 
} else {
    $legal_json = Encoding::json_encode_utf8(array('legal' => $legal_buf));
    header("Content-Type: application/json");
    echo "{$callback}(";
    echo $legal_json;
    echo ")";
}
                }
            },
            user_ethnicity : {
                label : AIR2.Fixtures.FieldLabels['user_ethnicity'],
                fact_identifier : 'ethnicity'
            },
            birth_year : {
                label : 'Age'
            },
            tag : {
                label : 'Tag'
            },
            prj_uuid : {
                label : 'Project',
                itemLabels : <?php 
echo Encoding::json_encode_utf8($prj_names);
?>
            }


        };

        var app = new AIR2.UI.App({
            items: AIR2.SearchPanel({
                title           : '<?php 
echo $search_label;
?>
',
                searchUrl       : '<?php 
echo $search_url;
?>
 /**
  *
  *
  * @return unknown
  */
 public function write_file()
 {
     $path = $this->get_file_path();
     air2_mkdir(dirname($path));
     $params = $this->srs;
     $params['meta'] = $this->meta;
     // handle file uploads first, because we need to alter $params
     // to reflect target file name
     if ($this->has_files) {
         foreach ($params as $ques_uuid => $param_value) {
             if (is_array($param_value) && isset($param_value['orig_name'])) {
                 $upload_dir = sprintf("%s/%s.uploads", dirname($path), $this->uuid);
                 $target_file = sprintf("%s/%s.%s", $upload_dir, $ques_uuid, $param_value['file_ext']);
                 air2_mkdir($upload_dir);
                 if (move_uploaded_file($param_value['tmp_name'], $target_file)) {
                     chmod($target_file, 0664);
                 }
                 $params[$ques_uuid]['tmp_name'] = $target_file;
                 // for reaper
             }
         }
     }
     $json = Encoding::json_encode_utf8($params);
     $bytes = file_put_contents($path, $json);
     return $bytes;
 }
              '</tpl>',
              '</li>',
              '<li class="air2-search-created">Created {[AIR2.Format.dateHuman(values.prj_cre_dtim)]} by ',
               '{[AIR2.Format.createLink(values.author_fl, "/user/"+values.author_uuid, true)]}',
              '</li>',
             '</ul>',
            '</div>',
           '</tpl>'
        );

        var facetDefs = {

            org_uuid : {
                label : 'Organization',
                itemLabels : <?php 
echo Encoding::json_encode_utf8($org_uuids);
?>
            },

            tag : {
                label : 'Tag'
            }


        };

        var app = new AIR2.UI.App({
            items: AIR2.SearchPanel({
                title       : 'Projects',
                searchUrl   : '<?php 
echo $search_url;
        header('X-PIN: success', false, 202);
    } else {
        // problem writing the temp file (bad news)
        header('X-PIN: internal server error with storing submission. Try again later.', false, 500);
    }
    // send response
    if ($response_content_type == 'text/html') {
        print '<textarea>';
        print Encoding::json_encode_utf8($response);
        print '</textarea>';
    } elseif ($response_content_type == 'application/json') {
        $needs_jsonp = isset($_GET['callback']) ? $_GET['callback'] : false;
        if ($needs_jsonp) {
            print "{$needs_jsonp}(";
        }
        print Encoding::json_encode_utf8($response);
        if ($needs_jsonp) {
            print ")";
        }
    }
} else {
    // TODO handle HTML response
}
/**
 * Returns referer value from either X-PIN-referer POST param or HTTP header.
 *
 * @return unknown
 */
function get_referer()
{
    if (isset($_POST['X-PIN-referer'])) {
 /**
  * Returns object as JSON string. Only immediate columns
  * (no related objects) are encoded.
  *
  * @return $json
  */
 public function asJSON()
 {
     $cols = $this->toArray(false);
     return Encoding::json_encode_utf8($cols);
 }