Location: PHPKode > projects > SENAYAN Library Automation > senayan3-stable5/lib/biblio_list.inc.php
<?php
/**
 * biblio_list class
 * Class for generating list of bibliographic records
 *
 * Copyright (C) 2007,2008  Arie Nugraha (hide@address.com)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */


class biblio_list
{
    private $obj_db = false;
    private $resultset = false;
    private $num2show = 10;
    private $criteria = '';
    private $search_title = false;
    private $search_subject = false;
    private $search_author = false;
    private $search_location = false;
    private $biblio_ids = array();
    public $num_rows = 0;
    public $xml_detail = true;
    public $xml_result = true;


    /**
     * Method to set search criteria
     *
     * @param   string  $str_criteria
     * @param   string  $str_boolean_mode
     * @return  void
     */
    public function setSQLcriteria($str_criteria, $str_boolean_mode = 'OR')
    {
        $this->criteria = 'WHERE ';
        // $this->criteria = '';
        if (!preg_match('@OR|AND@', $str_boolean_mode)) {
            $str_boolean_mode = 'OR';
        }

        // search on title
        if (preg_match('@title\{[^\{]+\}@i', $str_criteria, $matches)) {
            $words = trim(str_replace(array('title{', '}'), '', $matches[0]));
            $keywords = '';
            foreach (explode(' ', $words) as $word) {
                $keywords .= '+'.$word.' ';
            }
            // trim the concatenated keywords
            $keywords = trim($keywords);
            $this->criteria .= " (MATCH (title) AGAINST ('$keywords' IN BOOLEAN MODE)) ";
            $this->search_title = true;
            // $this->criteria .= "(MATCH (title) AGAINST ('$keywords' IN BOOLEAN MODE)) ";
        }

        /*
        // search on title on string mode
        if (preg_match('@titlestring\{[^\{]+\}@i', $str_criteria, $matches)) {
            $words = trim(str_replace(array('titlestring{', '}'), '', $matches[0]));
            $str = '';
            foreach (explode(' ', $words) as $word) {
                $str .= " title LIKE '%$word%' OR";
            }
            // remove the last OR word
            $str = substr_replace(trim($str), '', -2);
            $this->criteria .= " $str_boolean_mode ($str) ";
        }
        */

        // search on authors
        if (preg_match('@authors\{[^\{]+\}@i', $str_criteria, $matches)) {
            $words = trim(str_replace(array('authors{', '}'), '', $matches[0]));
            $str = '';
            // IMPORTANT
            // if you want to broaden your search result on author, uncomment below code and replace
            // the boolean AND with OR
            /*
                        foreach (explode(' ', $words) as $word) {
                $str .= " a.author_name LIKE '%$word%' OR";
            }
            // remove the last AND word
            $str = substr_replace(trim($str), '', -2);
                        */
            // if the $words var is enclosed with double aphostrope
            if (preg_match('@".+"@', $words)) {
                $words = trim(str_replace(array('"', '\\'), '', $words));
                $str .= " a.author_name='".$words."' AND";
            } else {
                foreach (explode(' ', $words) as $word) {
                    $str .= " a.author_name LIKE '%$word%' AND";
                }
            }
            // remove the last AND word
            $str = substr_replace(trim($str), '', -3);
            $this->criteria .= "$str_boolean_mode ($str) ";
            $this->search_author = true;
        }

        // search on subject
        if (preg_match('@subject\{[^\{]+\}@i', $str_criteria, $matches)) {
            $words = trim(str_replace(array('subject{', '}'), '', $matches[0]));
            $str = '';
            if (preg_match('@".+"@', $words)) {
                $words = trim(str_replace(array('"', '\\'), '', $words));
                $str .= " t.topic='".$words."' OR";
            } else {
                foreach (explode(' ', $words) as $word) {
                    $str .= " t.topic LIKE '%$word%' OR";
                }
            }
            // remove the last OR word
            $str = substr_replace(trim($str), '', -2);
            $this->criteria .= "$str_boolean_mode ($str) ";
            $this->search_subject = true;
        }

        // search with location
        if (preg_match('@location\{[^\{]+\}@i', $str_criteria, $matches)) {
            $words = trim(str_replace(array('location{', '}'), '', $matches[0]));
            $str = 'loc.location_name=\''.$words.'\'';
            $this->criteria .= "AND ($str) ";
            $this->search_location = true;
        }
        // die($this->criteria);
    }


    /**
     * Method to print out document records
     *
     * @param   object  $obj_db
     * @param   integer $int_num2show
     * @param   boolean $bool_return_output
     * @return  string
     */
    public function getDocumentList($obj_db, $int_num2show = 10, $bool_return_output = true)
    {
        $this->obj_db = $obj_db;
        $this->num2show = $int_num2show;
        // get page number from http get var
        if (!isset($_GET['page']) OR $_GET['page'] < 1){
            $_page = 1;
        } else{
            $_page = (integer)$_GET['page'];
        }

        // count the row offset
        if ($_page <= 1) {
            $_offset = 0;
        } else {
            $_offset = ($_page*$this->num2show) - $this->num2show;
        }

        // remove last AND or OR syntax
        $this->criteria = preg_replace('@^(WHERE\sAND\s)|^(WHERE\sOR\s)@i', 'WHERE ', $this->criteria);

        // additional sql string
        $_add_sql_str = '';

        if ($this->search_author) {
            $_add_sql_str .= ' LEFT JOIN biblio_author AS ba ON b.biblio_id=ba.biblio_id LEFT JOIN mst_author AS a ON ba.author_id=a.author_id ';
        }

        if ($this->search_subject) {
            $_add_sql_str .= ' LEFT JOIN biblio_topic AS bt ON b.biblio_id=bt.biblio_id LEFT JOIN mst_topic AS t ON bt.topic_id=t.topic_id ';
        }

        if ($this->search_location) {
            $_add_sql_str .= ' LEFT JOIN item AS i ON b.biblio_id=i.biblio_id LEFT JOIN mst_location AS loc ON i.location_id=loc.location_id ';
        }

        $_add_sql_str .= $this->criteria;

        // sql string
        $_sql_str = "SELECT DISTINCT SQL_CALC_FOUND_ROWS
            b.biblio_id,
            b.title, b.image
            FROM biblio AS b
            $_add_sql_str
            ORDER BY b.last_update DESC LIMIT $_offset, ".$this->num2show;

        // for debugging purpose only
        // die($_sql_str);
        // query
        $this->resultset = $obj_db->query($_sql_str);
        // get total number of rows from query
        $_total_q = $obj_db->query("SELECT FOUND_ROWS()");
        if ($obj_db->error) {
            return '<div style="padding: 5px; border: 1px dotted #FF0000; color: #FF0000;">'
                .'Error on query bibliographic database. Please check your database</div>';
        }
        $_total_d = $_total_q->fetch_row();
        $this->num_rows = $_total_d[0];
        if ($bool_return_output) {
            // return the html result
            return $this->makeOutput();
        }
    }


    /**
     * Method to make an output of document records
     *
     * @return  string
     */
    protected function makeOutput()
    {
        global $is_in_safe_mode;
        // init the result buffer
        $_buffer = '';

        // loop data
        $_i = 0;
        while ($_biblio_d = $this->resultset->fetch_assoc()) {
            $_biblio_d['title'] = '<a href="index.php?p=show_detail&id='.$_biblio_d['biblio_id'].'" title="'.(defined('lang_opac_rec_detail')?lang_opac_rec_detail:'View Record Detail').'">'.$_biblio_d['title'].'</a>';
            $_biblio_d['detail_button'] = '<a href="index.php?p=show_detail&id='.$_biblio_d['biblio_id'].'" class="detailLink" title="'.(defined('lang_opac_rec_detail')?lang_opac_rec_detail:'View Record Detail').'">Record Detail</a>';
            if ($this->xml_detail) {
                $_biblio_d['xml_button'] = '<a href="index.php?p=show_detail&inXML=true&id='.$_biblio_d['biblio_id'].'" class="xmlDetailLink" title="View Detail in XML Format" target="_blank">XML Detail</a>';
            } else {
                $_biblio_d['xml_button'] = '';
            }
            // append the link to the end of array
            $_biblio_d = $_biblio_d;
            // cover images var
            $_image_cover = '';
            if (!empty($_biblio_d['image'])) {
                $_biblio_d['image'] = urlencode($_biblio_d['image']);
                $images_loc = 'images/docs/'.$_biblio_d['image'];
                if (!$is_in_safe_mode AND file_exists($images_loc)) {
                    $_image_cover = 'style="background-image: url(./lib/phpthumb/phpThumb.php?src=../../'.$images_loc.'&w=42);"';
                } else if ($is_in_safe_mode) {
                    $_image_cover = 'style="background-image: url(./lib/phpthumb/phpThumb.php?src=../../'.$images_loc.'&w=42);"';
                }
            }

            $_alt_list = ($_i%2 == 0)?'alterList':'alterList2';
            $_buffer .= '<div class="item '.$_alt_list.'" '.$_image_cover.'>'.$_biblio_d['title'].'<br />';
            // query the author
            $_author_q = $this->obj_db->query("SELECT a.author_name FROM biblio_author AS ba
                LEFT JOIN biblio AS b ON ba.biblio_id=b.biblio_id
                LEFT JOIN mst_author AS a ON ba.author_id=a.author_id WHERE ba.biblio_id=".$_biblio_d['biblio_id']);
            // concat author data
            $_authors = '';
            while ($_author_d = $_author_q->fetch_row()) {
                $_authors .= $_author_d[0].' - ';
            }
            if ($_authors) {
                // replace the last strip
                $_authors = substr_replace($_authors, '', -3);
                $_buffer .= '<div class="subItem"><span class="author">'.(defined('lang_mod_biblio_field_authors')?lang_mod_biblio_field_authors:'Authors').'</span> : '.$_authors.'</div>';
            }

            $_buffer .= '<div class="subItem">'.$_biblio_d['detail_button'].' '.$_biblio_d['xml_button'].'</div>';
            $_buffer .= "</div>\n";
            $_i++;
        }

        // free resultset memory
        $this->resultset->free_result();

        // paging
        if (($this->num_rows > $this->num2show)) {
            $_paging = '<hr width="97%" size="1" />'."\n";
            $_paging .= '<div style="text-align: center;">'.simbio_paging::paging($this->num_rows, $this->num2show, 5).'</div>';
        } else {
            $_paging = '';
        }

        return $_buffer.$_paging;
    }


    /**
     * Method to get list of document IDs of result
     *
     * @return  mixed
     */
    public function getDocumentIds()
    {
        $_temp_resultset = $this->resultset;
        while ($_biblio_d = $_temp_resultset->fetch_assoc()) {
            $this->biblio_ids[] = $_biblio_d['biblio_id'];
        }
        unset($_temp_resultset);
        return $this->biblio_ids;
    }
}
?>
Return current item: SENAYAN Library Automation