private function _load_record()
 {
     $marc_record = new MARCRecord();
     $marc_record->load_yaz_array($this->YAZ_ARRAY);
     return $marc_record;
 }
 function save_to_db()
 {
     global $projects_dir, $pguser;
     $postednum_str = $this->postednum == "" ? "NULL" : "'{$this->postednum}'";
     // Call addslashes() on any members of $this that might contain
     // single-quotes/apostrophes (because they are unescaped, and
     // would otherwise break the query).
     $common_project_settings = "\n            t_last_edit    = UNIX_TIMESTAMP(),\n            up_projectid   = '{$this->up_projectid}',\n            nameofwork     = '" . addslashes($this->nameofwork) . "',\n            authorsname    = '" . addslashes($this->authorsname) . "',\n            language       = '{$this->language}',\n            genre          = '{$this->genre}',\n            difficulty     = '{$this->difficulty_level}',\n            special_code   = '{$this->special_code}',\n            clearance      = '" . addslashes($this->clearance) . "',\n            comments       = '" . addslashes($this->comments) . "',\n            image_source   = '{$this->image_source}',\n            scannercredit  = '" . addslashes($this->scannercredit) . "',\n            checkedoutby   = '{$this->checkedoutby}',\n            postednum      = {$postednum_str},\n            image_preparer = '{$this->image_preparer}',\n            text_preparer  = '{$this->text_preparer}',\n            extra_credits  = '" . addslashes($this->extra_credits) . "',\n            deletion_reason= '" . addslashes($this->deletion_reason) . "'\n        ";
     $pm_setter = '';
     if (user_is_a_sitemanager()) {
         // can change PM
         $pm_setter = " username = '******',";
     } else {
         if (isset($this->clone_projectid)) {
             // cloning a project. The PM should be the same as
             // that of the project being cloned, if the user
             // isn't an SA
             $res = mysql_query("\n                SELECT username\n                FROM projects\n                WHERE projectid='{$this->clone_projectid}'\n            ") or die(mysql_error());
             list($projectmanager) = mysql_fetch_row($res);
             $pm_setter = " username = '******',";
         }
     }
     if (isset($this->projectid)) {
         // We are updating an already-existing project.
         // needn't change $pm_setter, as there is no change if the user
         // isn't an SA
         // find out what we are changing from
         $old_pih = new ProjectInfoHolder();
         $fatal_error = $old_pih->set_from_db(TRUE, $this->projectid);
         if ($fatal_error != '') {
             $fatal_error = _('site error') . ': ' . $fatal_error;
             echo "<br><center><font size='+1' color='#ff0000'><b>{$fatal_error}</b></font></center>";
             exit;
         }
         $changed_fields = get_changed_fields($this, $old_pih);
         // We're particularly interested in knowing
         // when the project comments change.
         if (!in_array('comments', $changed_fields)) {
             // no change
             $tlcc_setter = '';
         } else {
             // changed!
             $tlcc_setter = 't_last_change_comments = UNIX_TIMESTAMP(),';
         }
         // We also want to know if the edit is resulting in the project
         // effectively being checked out to a new PPer
         if ($old_pih->state == PROJ_POST_FIRST_CHECKED_OUT && in_array('checkedoutby', $changed_fields)) {
             $md_setter = 'modifieddate = UNIX_TIMESTAMP(),';
             $PPer_checkout = TRUE;
         } else {
             $md_setter = '';
             $PPer_checkout = FALSE;
         }
         // Update the projects database with the updated info
         mysql_query("\n                UPDATE projects SET\n                    {$pm_setter}\n                    {$tlcc_setter}\n                    {$md_setter}\n                    {$common_project_settings}\n                WHERE projectid='{$this->projectid}'\n            ") or die(mysql_error());
         $details1 = implode(' ', $changed_fields);
         if ($details1 == '') {
             // There are no changed fields.
             // Don't just save '' for the details1 column,
             // because then do_history() won't be able to distinguish
             // this case (no changed fields) from old cases
             // (edit occurred before we started recording changed fields).
             // Instead, use a special value.
             $details1 = 'NONE';
         }
         $e = log_project_event($this->projectid, $GLOBALS['pguser'], 'edit', $details1);
         if (!empty($e)) {
             die($e);
         }
         if ($PPer_checkout) {
             // we fake the project transition...
             $e = log_project_event($this->projectid, $GLOBALS['pguser'], 'transition', PROJ_POST_FIRST_CHECKED_OUT, PROJ_POST_FIRST_CHECKED_OUT, $this->checkedoutby);
             if (!empty($e)) {
                 die($e);
             }
         }
         // Update the MARC record with any info we've received.
         $project = new Project($this->projectid);
         $marc_record = $project->load_marc_record();
         $this->update_marc_record_from_post($marc_record);
         $project->save_marc_record($marc_record);
     } else {
         // We are creating a new project
         $this->projectid = uniqid("projectID");
         // The project ID
         if ('' == $pm_setter) {
             $pm_setter = "username = '******',";
         }
         // Insert a new row into the projects table
         mysql_query("\n                INSERT INTO projects\n                SET\n                    projectid    = '{$this->projectid}',\n                    {$pm_setter}\n                    state        = '" . PROJ_NEW . "',\n                    modifieddate = UNIX_TIMESTAMP(),\n                    t_last_change_comments = UNIX_TIMESTAMP(),\n                    {$common_project_settings}\n            ") or die(mysql_error());
         $e = log_project_event($this->projectid, $GLOBALS['pguser'], 'creation');
         if (!empty($e)) {
             die($e);
         }
         $e = project_allow_pages($this->projectid);
         if (!empty($e)) {
             die($e);
         }
         // Make a directory in the projects_dir for this project
         mkdir("{$projects_dir}/{$this->projectid}", 0777) or die("System error: unable to mkdir '{$projects_dir}/{$this->projectid}'");
         chmod("{$projects_dir}/{$this->projectid}", 0777);
         // Do MARC record manipulations
         $project = new Project($this->projectid);
         $marc_record = new MARCRecord();
         // Save original MARC record, if provided
         $yaz_array = unserialize(base64_decode($this->original_marc_array_encd));
         if ($yaz_array !== FALSE) {
             $marc_record->load_yaz_array($yaz_array);
             $project->init_marc_record($marc_record);
             // Update the MARC record with data from POST
             $this->update_marc_record_from_post($marc_record);
             $project->save_marc_record($marc_record);
         }
         // Create the project's 'good word list' and 'bad word list'.
         if (isset($this->clone_projectid)) {
             // We're creating a project via cloning.
             // Copy the original project's word-lists.
             $good_words = load_project_good_words($this->clone_projectid);
             if (is_string($good_words)) {
                 // It's an error message.
                 echo "{$good_words}<br>\n";
                 $good_words = array();
             }
             $bad_words = load_project_bad_words($this->clone_projectid);
             if (is_string($bad_words)) {
                 // It's an error message.
                 echo "{$bad_words}<br>\n";
                 $bad_words = array();
             }
         } else {
             // We're creating a project by means other than cloning
             // (from_nothing, from_marc_record, from_uberproject).
             // Initialize its GWL and BWL to empty.
             $good_words = array();
             $bad_words = array();
         }
         save_project_good_words($this->projectid, $good_words);
         save_project_bad_words($this->projectid, $bad_words);
     }
     // Create/update the Dublin Core file for the project.
     // When we get here, the project's database entry has been fully
     // updated, so we can create a Project object and allow it
     // to pull the relevant fields from the database.
     $project = new Project($this->projectid);
     $project->create_dc_xml_oai($marc_record);
     // If the project has been posted to PG, make the appropriate transition.
     if ($this->posted) {
         $err = project_transition($this->projectid, PROJ_SUBMIT_PG_POSTED, $pguser);
         if ($err != '') {
             echo "{$err}<br>\n";
             exit;
         }
     }
 }
function do_search_and_show_hits()
{
    output_header("Search Results");
    echo "<br>";
    if (empty($_GET['start'])) {
        $start = 1;
    } else {
        $start = $_GET['start'];
    }
    if (!empty($_GET['fq'])) {
        $fullquery = unserialize(base64_decode($_GET['fq']));
    } else {
        $fullquery = query_format();
    }
    global $external_catalog_locator;
    $id = yaz_connect($external_catalog_locator);
    yaz_syntax($id, "usmarc");
    yaz_element($id, "F");
    yaz_search($id, "rpn", trim($fullquery));
    $extra_options = array("timeout" => 60);
    yaz_wait($extra_options);
    $errorMsg = yaz_error($id);
    if (!empty($errorMsg)) {
        echo "<center>";
        echo _("The following error has occurred:");
        echo "<br><br>";
        echo "<b><i>{$errorMsg}</i></b>";
        echo "<p>";
        $url = "editproject.php?action=createnew";
        echo sprintf(_("Please try again. If the problem recurs, please create your project manually by following this <a href='%s'>link</a>."), $url);
        echo "</center>";
        exit;
    }
    echo "<center>";
    if (yaz_hits($id) == 0) {
        echo "<b>";
        echo _("There were no results returned.");
        echo "</b>";
        echo "<br>";
        echo _("Please search again or click 'No Matches' to create the project manually.");
        echo "<br>";
    } else {
        echo "<b>";
        echo sprintf(_("%d results returned. Note that some non-book results may not be displayed."), yaz_hits($id));
        echo "<br>";
        echo _("Please pick a result from below:");
        echo "</b>";
    }
    echo "</center>";
    echo "<br><form method='post' action='editproject.php'>";
    echo "<input type='hidden' name='action' value='create_from_marc_record'>";
    echo "<table border='0 width='100%' cellpadding='0' cellspacing='0'>";
    // -----------------------------------------------------
    $hits_per_page = 20;
    // Perhaps later this can be a PM preference or an option on the form.
    $i = 1;
    while ($start <= yaz_hits($id) && $i <= $hits_per_page) {
        $rec = yaz_record($id, $start, "array");
        //if it's not a book don't display it.  we might want to uncomment in the future if there are too many records being returned - if (substr(yaz_record($id, $start, "raw"), 6, 1) != "a") { $start++; continue; }
        $marc_record = new MARCRecord();
        $marc_record->load_yaz_array($rec);
        if ($i % 2 == 1) {
            echo "<tr>";
        }
        echo "<td width='5%' align='center' valign='top'>";
        echo "<input type='radio' name='rec' value='" . base64_encode(serialize($rec)) . "'>";
        echo "</td>";
        echo "<td width='45%' align='left' valign='top'>";
        echo "<table border='0' width='100%' cellpadding='0' cellspacing='0'>";
        foreach (array(array('label' => _("Title"), 'value' => $marc_record->title), array('label' => _("Author"), 'value' => $marc_record->author), array('label' => _("Publisher"), 'value' => $marc_record->publisher), array('label' => _("Language"), 'value' => $marc_record->language), array('label' => _("LCCN"), 'value' => $marc_record->lccn), array('label' => _("ISBN"), 'value' => $marc_record->isbn)) as $couple) {
            $label = $couple['label'];
            $value = $couple['value'];
            echo "<tr>";
            echo "<td width='20%' align='left' valign='top'><b>{$label}</b>:</td>";
            echo "<td align='left' valign='top'>{$value}</td>";
            echo "</tr>\n";
        }
        echo "</table><p></td>";
        if ($i % 2 != 1) {
            echo "</tr>\n";
        }
        $i++;
        $start++;
    }
    if ($i % 2 != 1) {
        echo "</tr>\n";
    }
    // -----------------------------------------------------
    $encoded_fullquery = base64_encode(serialize($fullquery));
    echo "<tr>";
    echo "<td colspan='2' width='50%' align='left' valign='top'>";
    if (isset($_GET['start']) && $_GET['start'] - $hits_per_page > 0) {
        $url = "external_catalog_search.php?action=do_search_and_show_hits&start=" . ($_GET['start'] - $hits_per_page) . "&fq={$encoded_fullquery}";
        echo "<a href='{$url}'>Previous</a>";
    } else {
        echo "&nbsp;";
    }
    echo "</td>";
    echo "<td colspan='2' width='50%' align='right' valign='top'>";
    if ($start + $hits_per_page <= yaz_hits($id)) {
        $url = "external_catalog_search.php?action=do_search_and_show_hits&start={$start}&fq={$encoded_fullquery}";
        echo "<a href='{$url}'>Next</a>";
    } else {
        echo "&nbsp;";
    }
    echo "</td>";
    echo "</tr>\n";
    // -----------------------------------------------------
    echo "</table><br><center>";
    if (yaz_hits($id) != 0) {
        echo "<input type='submit' value='", attr_safe(_("Create the Project")), "'>&nbsp;";
    }
    $label = attr_safe(_('Search Again'));
    $url = "external_catalog_search.php?action=show_query_form";
    echo "<input type='button' value='{$label}' onclick='javascript:location.href=\"{$url}\";'>";
    echo "&nbsp;";
    $label = attr_safe(_('No Matches'));
    $url = "editproject.php?action=createnew";
    echo "<input type='button' value='{$label}' onclick='javascript:location.href=\"{$url}\";'>";
    echo "&nbsp;";
    $label = attr_safe(_('Quit'));
    $url = "projectmgr.php";
    echo "<input type='button' value='{$label}' onclick='javascript:location.href=\"{$url}\";'>";
    echo "</form>";
    echo "</center>";
    yaz_close($id);
}