Exemplo n.º 1
0
function populate_mbdb_book_columns($column, $post_id)
{
    // get the book for the current row
    $book = MBDB()->books->get($post_id);
    // if the book is invalid, exit
    if ($book == null) {
        return;
    }
    // if this is the genre or series column (taxonomies)
    // just get the term list and display that delimited by commas
    // and then exit. No further processing necessary
    if ($column == 'mbdb_genre' || $column == 'mbdb_series') {
        do_action('mbdb_book_pre_' . $column . '_column');
        echo apply_filters('mbdb_book_' . $column . '_column', get_the_term_list($post_id, $column, '', ', '));
        do_action('mbdb_book_post_' . $column . '_column');
        return;
    }
    // if the column is a property of book, grab that data
    if (property_exists($book, $column)) {
        $data = $book->{$column};
    } else {
        $data = '';
    }
    switch ($column) {
        // book cover: display as an image
        case 'cover':
            do_action('mbdb_book_pre_mbdb_cover_column', $column, $data, $book);
            if ($data != '') {
                $alt = mbdb_get_alt_text($book->cover_id, __('Book Cover:', 'mooberry-book-manager') . ' ' . get_the_title($post_id));
                echo apply_filters('mbdb_book_mbdb_cover_column', '<IMG SRC="' . esc_url($data) . '" width="100" ' . $alt . ' />', $column, $data, $book);
            }
            do_action('mbdb_book_post_mbdb_cover_column', $column, $data, $book);
            break;
            // release date: use short format
        // release date: use short format
        case 'release_date':
            do_action('mbdb_book_pre_mbdb_published_column', $column, $data, $book);
            if (!empty($data)) {
                // TO DO validate data to be a date??
                /* translators: short date format. see http://php.net/date */
                echo apply_filters('mbdb_book_mbdb_published_column', date(__('m/d/Y'), strtotime($data)), $column, $data, $book);
            }
            do_action('mbdb_book_post_mbdb_published_column', $column, $data, $book);
            break;
            // publisher: display publisher name
            // does not come from book object
        // publisher: display publisher name
        // does not come from book object
        case 'publisher_id':
            $publisher = mbdb_get_publisher_info($data);
            do_action('mbdb_book_pre_mbdb_publisher_column', $column, $publisher, $book);
            echo apply_filters('mbdb_book_mbdb_publisher_column', $publisher['name'], $column, $publisher, $book);
            do_action('mbdb_book_post_mbdb_publisher_column', $column, $publisher, $book);
            break;
        default:
            do_action('mbdb_book_pre_mbdb_' . $column . '_column', $column, $data, $book);
            echo apply_filters('mbdb_book_mbdb_' . $column . '_column', $data, $book, $post_id, $column);
            do_action('mbdb_book_post_mbdb_' . $column . '_column', $column, $data, $book);
    }
}
    public function get_ordered_selection($selection, $selection_ids, $sort, $book_ids = null, $taxonomy = null)
    {
        // selection_ids = book_ids if selection = "custom"
        // 				 = tax_ids if selection is a taxonomy
        //				 = publisher_ids if selection = "publisher"
        //				 should be null otherwise
        // book_ids is an optional, additional filtering of book ids
        // taxonmy:  array of taxonomy and id(s) to filter on. Also includes publisher.
        // 		Examples:
        //		{ series => 24 }
        //		{	genre => { 20, 21 } }
        //		{	publisher => 15 }  // publisher is expected to be a single value. If it's an array, only the 1st is selected
        // $sort = titleA, titleD, pubdateA, pubdateD, series_order
        //			OR { field, direction } ie { release_date, DESC }
        // validate inputs
        // SORT VARIABLES
        //if an array is passed in, separate into field, direction
        if (is_array($sort)) {
            list($sort, $order) = $sort;
        } else {
            // otherwise, set field, direction based on value
            list($sort, $order) = $this->get_sort_fields($sort);
        }
        // if getting books by series, the sort should be by series aascending
        if ($taxonomy == null && $selection == 'series') {
            $sort = 'series_order';
            $order = 'ASC';
        } else {
            if ($taxonomy != null && array_key_exists('series', $taxonomy)) {
                $sort = 'series_order';
                $order = 'ASC';
            }
        }
        // ensure that the sort field is a column in the table
        // and that the direction is either ASC or DESC
        $sort = $this->validate_orderby($sort);
        $order = $this->validate_order($order);
        // SELECTION VARIABLES
        // default to all books
        $book_selection_options = mbdb_book_grid_selection_options();
        if (!array_key_exists($selection, $book_selection_options)) {
            $selection = 'all';
        }
        $taxonomies = array('genre', 'series', 'tag', 'illustrator', 'editor', 'cover_artist');
        // if custom, genre, series, tag, illustrator, editor, cover artist, or publisher and no selection ids are passed, default to all books
        // otherwise if selection ids is not an array, make it an array
        if (in_array($selection, array_merge(array('custom', 'publisher'), $taxonomies))) {
            if ($selection_ids == null || $selection_ids == '') {
                $selection = 'all';
            } else {
                if (!is_array($selection_ids)) {
                    $selection_ids = array($selection_ids);
                }
            }
        }
        // TAXONOMY ARRAY
        // if taxonomy is supplied, the keys must be one of the options
        if ($taxonomy) {
            $tax_options = array_keys(mbdb_book_grid_group_by_options());
            foreach ($taxonomy as $tax => $tax_ids) {
                if (!in_array($tax, $tax_options)) {
                    unset($taxonomy[$tax]);
                }
            }
            if (count($taxonomy) == 0) {
                $taxonomy = null;
            }
        }
        $select = 'SELECT DISTINCT ';
        $join = ' JOIN ' . $this->prefix() . 'posts p ON p.id = b.book_id ';
        $where = ' WHERE p.post_status = "publish" ';
        $orderby = ' ORDER BY ';
        // if book_ids are sent, filter by them
        if ($book_ids != null) {
            if (!is_array($book_ids)) {
                $book_ids = array($book_ids);
            }
            $book_ids = array_map('absint', $book_ids);
            $where .= ' AND (book_id in (' . implode(', ', $book_ids) . ') ) ';
        }
        // set the where clause
        switch ($selection) {
            case 'all':
                // no change
                // this is included only so it doesn't fall into the "default"
                $where .= '';
                break;
            case 'published':
                $where .= ' AND ( release_date <= CURRENT_DATE() ) ';
                break;
            case 'unpublished':
                $where .= ' AND ( release_date > CURRENT_DATE() OR release_date IS NULL ) ';
                break;
            case 'custom':
                $selection_ids = array_map('absint', $selection_ids);
                $where .= ' AND (book_id in (' . implode(', ', $selection_ids) . ') ) ';
                break;
            case 'publisher':
                $selection_ids = array_map('esc_sql', $selection_ids);
                $where .= ' AND ( b.publisher_id in ( "' . implode('", "', $selection_ids) . '" ) ) ';
                break;
            default:
                // anything else is a taxonomy, a type handled by another add-on, or an
                // invalid input
                // if it's a taxonomy, add where and join
                if (in_array($selection, $taxonomies)) {
                    $selection_ids = array_map('absint', $selection_ids);
                    $where .= ' AND ( tt.taxonomy = "mbdb_' . $selection . '" 
									AND tt.term_id in ( ' . implode(', ', $selection_ids) . ' ) 
									AND p.post_type = "mbdb_book" ) ';
                    $join .= ' JOIN ' . $this->prefix() . 'term_relationships AS tr ON tr.object_id = b.book_id 
								JOIN ' . $this->prefix() . 'term_taxonomy AS tt  ON tt.term_taxonomy_id = tr.term_taxonomy_id ';
                }
                break;
        }
        // add in taxonomy filtering
        if ($taxonomy != null) {
            $tax_level = 2;
            foreach ($taxonomy as $tax => $tax_ids) {
                switch ($tax) {
                    case 'none':
                        // no additional filtering needed, this is the innermost level
                        break 2;
                    case 'publisher':
                        //if -1 then get books that don't have a publisher
                        if ($tax_ids == -1) {
                            $select .= '"" AS name' . $tax_level . ', ';
                            $where .= ' AND (b.publisher_id IS NULL) ';
                        } else {
                            if (is_array($tax_ids)) {
                                $tax_ids = $tax_ids[0];
                            }
                            $publisher = mbdb_get_publisher_info($tax_ids);
                            if ($publisher != null && array_key_exists('name', $publisher)) {
                                $select .= '"' . $publisher['name'] . '" as name' . $tax_level . ', ';
                                $where .= ' AND (b.publisher_id ="' . esc_sql($tax_ids) . '") ';
                            }
                        }
                        break;
                        // anything left is a taxonomy
                    // anything left is a taxonomy
                    default:
                        if (in_array($tax, $taxonomies)) {
                            // if -1 then get books that are NOT in any of this taxonomny
                            if ($tax_ids == -1) {
                                $select .= ' "" AS name' . $tax_level . ', ';
                                $where .= ' and b.book_id not in (select book_id from ' . $this->table_name . ' as b 
																		join ' . $this->prefix() . 'term_relationships as tr3 on tr3.object_id = b.book_id 
																		join ' . $this->prefix() . 'term_taxonomy tt3 on tt3.term_taxonomy_id = tr3.term_taxonomy_id 
																		where tt3.taxonomy = "mbdb_' . $tax . '" ) ';
                            } else {
                                if (!is_array($tax_ids)) {
                                    $tax_ids = array($tax_ids);
                                }
                                $tax_ids = array_map('absint', $tax_ids);
                                $select .= 't' . $tax_level . '.name AS name' . $tax_level . ', ';
                                $where .= ' AND (tt' . $tax_level . '.taxonomy = "mbdb_' . $tax . '" AND tt' . $tax_level . '.term_id in (' . implode(',', $tax_ids) . ') ) ';
                                $join .= ' JOIN ' . $this->prefix() . 'term_relationships AS tr' . $tax_level . ' ON tr' . $tax_level . '.object_id = b.book_id 
												JOIN ' . $this->prefix() . 'term_taxonomy AS tt' . $tax_level . '  ON tt' . $tax_level . '.term_taxonomy_id = tr' . $tax_level . '.term_taxonomy_id 
												JOIN ' . $this->prefix() . 'terms AS t' . $tax_level . ' ON t' . $tax_level . '.term_id = tt' . $tax_level . '.term_id';
                            }
                        }
                        break;
                }
                $tax_level++;
            }
        }
        // set the order
        switch ($sort) {
            case 'release_date':
                // sort null dates last
                $orderby .= ' CASE WHEN release_date IS NULL THEN 1 ELSE 0 END, release_date ';
                break;
            case 'series_order':
                // sort null orders last
                $orderby .= ' CASE WHEN series_order IS NULL THEN 999 ELSE 0 END, series_order ';
                break;
            default:
                $orderby .= ' post_title ';
                break;
        }
        $select = apply_filters('mbdb_book_get_ordered_selection_select', $select);
        $join = apply_filters('mbdb_book_get_ordered_selection_join', $join);
        $where = apply_filters('mbdb_book_get_ordered_selection_where', $where, $selection_ids, $selection, $book_ids);
        $orderby = apply_filters('mbdb_book_get_ordered_selection_orderby', $orderby, $sort, $order);
        $sql = "{$select} b.book_id, p.post_title, b.cover, b.release_date, b.cover_id FROM  {$this->table_name}  as b  {$join} {$where} {$orderby} {$order} ";
        $books = $this->run_sql($sql);
        return apply_filters('mbdb_book_get_ordered_selection', $books, $selection, $selection_ids, $sort, $order, $taxonomy, $book_ids);
    }
/**
 *  
 *  Get the book's publisher
 *  
 *  
 *  @since 2.0
 *  @since 3.0 use custom table column name instead of post_meta
 *  
 *  @return mixed data or false if book couldn't be found or if data was blank
 *  				( make sure to test with === false when calling this function )
 *  
 *  @access public
 *  
 */
function mbdb_get_publisher_data($book = '')
{
    $publisherID = mbdb_get_book_data('publisher_id', $book);
    if ($publisherID === false) {
        return false;
    }
    $publisher = mbdb_get_publisher_info($publisherID);
    if ($publisher == null) {
        return false;
    } else {
        return $publisher;
    }
}