get_row("SELECT *, DATE_FORMAT(date, '%Y') AS year FROM " . TEACHPRESS_PUB . " WHERE `pub_id` = '" . intval($id) . "'", $output_type); return $result; } /** * Returns a single publication selected by BibTeX key * @param int $key The BibTeX key * @param string $output_type OBJECT, ARRAY_N or ARRAY_A, default is OBJECT * @return mixed * @since 5.0.0 */ public static function get_publication_by_key($key, $output_type = OBJECT) { global $wpdb; // Check if bibtex key has no spaces if ( strpos($key, ' ') !== false ) { $key = str_replace(' ', '', $key); } $key = esc_sql(htmlspecialchars($key)); $result = $wpdb->get_row("SELECT *, DATE_FORMAT(date, '%Y') AS year FROM " . TEACHPRESS_PUB . " WHERE `bibtex` = '$key'", $output_type); return $result; } /** * Returns an array or object of publications * * @param array $args { * @type string user User IDs (separated by comma) * @type string type Type name (separated by comma) * @type string tag Tag IDs (separated by comma) * @type string key BibTex keys (separated by comma) * @type string author_id Author IDs (separated by comma) * @type string import_id Import IDs (separated by comma) * @type string year Years (separated by comma) * @type string years_between start/end year separated by comma, use 0 for unlimited * @type string author Author name (separated by comma) * @type string editor Editor name (separated by comma) * @type string exclude The ids of the publications you want to exclude (separated by comma) * @type string include The ids of the publications you want to include (separated by comma) * @type bool include_editor_as_author True or false * @type string exclude_tags Use it to exclude publications via tag IDs (separated by comma) * @type string order The order of the list * @type string limit The sql search limit, ie: 0,30 * @type string search The search string * @type array meta_key_search Array which contains the parameters for meta_keys as key=>value pair * Example for an checkbox: array( 'tp_meta_pub_custom_label' => '{Open Access}' ), * @type string output_type OBJECT, ARRAY_N or ARRAY_A, default is OBJECT * } * @param boolean $count set to true of you only need the number of rows * @since 5.0.0 * @return mixed array, object or int */ public static function get_publications($args = array(), $count = false) { $defaults = array( 'user' => '', 'type' => '', 'award' => '', 'tag' => '', 'tag_name' => '', 'key' => '', 'author_id' => '', 'import_id' => '', 'year' => '', 'years_between' => '', 'author' => '', 'editor' => '', 'include' => '', 'include_editor_as_author' => true, 'exclude' => '', 'exclude_tags' => '', 'exclude_types' => '', 'order' => 'date DESC', 'limit' => '', 'search' => '', 'meta_key_search' => array(), 'output_type' => OBJECT ); $atts = wp_parse_args( $args, $defaults ); global $wpdb; // define all things for meta data integration $joins = ''; $selects = ''; $meta_fields = $wpdb->get_results("SELECT variable FROM " . TEACHPRESS_SETTINGS . " WHERE `category` = 'teachpress_pub'", ARRAY_A); if ( !empty($meta_fields) ) { $i = 1; foreach ($meta_fields as $field) { $table_id = 'm' . $i; $selects .= ', ' . $table_id .'.meta_value AS ' . $field['variable']; $joins .= ' LEFT JOIN ' . TEACHPRESS_PUB_META . ' ' . $table_id . " ON ( " . $table_id . ".pub_id = p.pub_id AND " . $table_id . ".meta_key = '" . esc_sql($field['variable']) . "' ) "; $i++; } } // define basics $select = "SELECT DISTINCT p.pub_id, p.title, p.type, p.award, p.bibtex, p.author, p.editor, p.date, DATE_FORMAT(p.date, '%Y') AS year, p.urldate, p.isbn, p.url, p.booktitle, p.issuetitle, p.journal, p.issue, p.volume, p.number, p.pages, p.publisher, p.address, p.edition, p.chapter, p.institution, p.organization, p.school, p.series, p.crossref, p.abstract, p.howpublished, p.key, p.techtype, p.note, p.comment, p.is_isbn, p.image_url, p.image_target, p.image_ext, p.doi, p.rel_page, p.status, p.added, p.modified, p.import_id $selects FROM " . TEACHPRESS_PUB . " p $joins "; $select_for_count = "SELECT DISTINCT p.pub_id, DATE_FORMAT(p.date, '%Y') AS year FROM " . TEACHPRESS_PUB . " p $joins "; $join = ''; // exclude publications via tag_id if ( !empty($atts['exclude_tags']) ) { $extend = ''; $exclude_tags = TP_DB_Helpers::generate_where_clause($atts['exclude_tags'], "tag_id", "OR", "="); $exclude_publications = $wpdb->get_results("SELECT DISTINCT pub_id FROM " . TEACHPRESS_RELATION . " WHERE $exclude_tags ORDER BY pub_id ASC", ARRAY_A); foreach ($exclude_publications as $row) { $extend .= $row['pub_id'] . ','; } $atts['exclude'] = $extend . $atts['exclude']; } // additional joins if ( !empty($atts['user']) ) { $join .= "INNER JOIN " . TEACHPRESS_USER . " u ON u.pub_id = p.pub_id "; } if ( !empty($atts['tag']) || !empty($atts['tag_name']) ) { $join .= "INNER JOIN " . TEACHPRESS_RELATION . " b ON p.pub_id = b.pub_id INNER JOIN " . TEACHPRESS_TAGS . " t ON t.tag_id = b.tag_id "; } if ( !empty($atts['author_id'] ) ) { $join .= "INNER JOIN " . TEACHPRESS_REL_PUB_AUTH . " r ON p.pub_id = r.pub_id "; } // define order_by clause $order = ''; $array = explode(",", TP_DB_Helpers::validate_qualifier( $atts['order'], $defaults['order'] ) ); foreach($array as $element) { $element = trim($element); // order by year if ( strpos($element, 'year') !== false ) { $order = $order . $element . ', '; } // normal case if ( $element != '' && strpos($element, 'year') === false ) { $order = $order . 'p.' . $element . ', '; } } if ( $order != '' ) { $order = substr($order, 0, -2); } // define global search $search = esc_sql(htmlspecialchars(stripslashes($atts['search']))); if ( $search != '' ) { $search = "p.title LIKE '%$search%' OR p.author LIKE '%$search%' OR p.editor LIKE '%$search%' OR p.isbn LIKE '%$search%' OR p.booktitle LIKE '%$search%' OR p.issuetitle LIKE '%$search%' OR p.journal LIKE '%$search%' OR p.date LIKE '%$search%' OR p.abstract LIKE '%$search%' OR p.note LIKE '%$search%'"; } // WHERE clause $nwhere = array(); $nwhere[] = TP_DB_Helpers::generate_where_clause($atts['exclude'], "p.pub_id", "AND", "!="); $nwhere[] = TP_DB_Helpers::generate_where_clause($atts['exclude_types'], "p.type", "AND", "!="); $nwhere[] = TP_DB_Helpers::generate_where_clause($atts['include'], "p.pub_id", "OR", "="); $nwhere[] = TP_DB_Helpers::generate_where_clause($atts['type'], "p.type", "OR", "="); $nwhere[] = TP_DB_Helpers::generate_where_clause($atts['user'], "u.user", "OR", "="); $nwhere[] = TP_DB_Helpers::generate_where_clause($atts['tag'], "b.tag_id", "OR", "="); $nwhere[] = TP_DB_Helpers::generate_where_clause($atts['tag_name'], "t.name", "OR", "="); $nwhere[] = TP_DB_Helpers::generate_where_clause($atts['key'], "p.bibtex", "OR", "="); $nwhere[] = TP_DB_Helpers::compose_clause( array( TP_DB_Helpers::generate_where_clause($atts['author_id'], "r.author_id", "OR", "="), TP_DB_Helpers::generate_where_clause($atts['author'], "p.author", "OR", "LIKE", '%')), "OR", '' ); $nwhere[] = TP_DB_Helpers::generate_where_clause($atts['import_id'], "p.import_id", "OR", "="); $nwhere[] = TP_DB_Helpers::generate_where_clause($atts['editor'], "p.editor", "OR", "LIKE", '%'); $nwhere[] = ( $atts['author_id'] != '' && $atts['include_editor_as_author'] === false) ? " AND ( r.is_author = 1 ) " : null; $nwhere[] = ( $search != '') ? $search : null; // Where clause for meta fields if ( !empty( $meta_fields ) && !empty( $atts['meta_key_search'] ) ) { $meta_key_search = $atts['meta_key_search']; $i = 1; // Go throw each meta field and if there is a search value for it in meta_key_search[], add it to the WHERE clause foreach ($meta_fields as $field) { $key = $field['variable']; $column_id = 'm' . $i . '.meta_value'; if (array_key_exists($key, $meta_key_search) ) { $nwhere[] = TP_DB_Helpers::generate_where_clause($meta_key_search[$key], $column_id, "OR", "="); } $i++; } } $where = TP_DB_Helpers::compose_clause($nwhere); // HAVING clause $having = ''; if ( !empty($atts['year']) ) { $having = ' HAVING ' . TP_DB_Helpers::generate_where_clause($atts['year'], "year", "OR", "="); } if ( empty($atts['year']) && !empty($atts['years_between']) ) { $having = ' HAVING ' . TP_DB_Helpers::generate_between_clause($atts['years_between'], "year"); } // LIMIT clause $limit = ( !empty($atts['limit']) ) ? 'LIMIT ' . TP_DB_Helpers::validate_qualifier($atts['limit']) : ''; // End if ( $count !== true ) { $sql = $select . $join . $where . $having . " ORDER BY $order $limit"; // var_dump($args); // get_tp_message($sql,'red'); } else { $sql = "SELECT COUNT( pub_id ) AS `count` FROM ( $select_for_count $join $where $having) p "; } $sql = ( $count != true ) ? $wpdb->get_results($sql, $atts['output_type']): $wpdb->get_var($sql); return $sql; } /** * Returns course meta data * @param int $pub_id The publication ID * @param string $meta_key The name of the meta field * @return array * @since 5.0.0 */ public static function get_pub_meta($pub_id, $meta_key = ''){ global $wpdb; $where = ''; if ( $meta_key !== '' ) { $where = "AND `meta_key` = '" . esc_sql($meta_key) . "'"; } $sql = "SELECT * FROM " . TEACHPRESS_PUB_META . " WHERE `pub_id` = '" . intval($pub_id) . "' $where"; return $wpdb->get_results($sql, ARRAY_A); } /** * Returns an array or object of users who has a publication list * * Possible values for the array $args: * output type (STRING) OBJECT, ARRAY_A, ARRAY_N, default is OBJECT * * @param array $args * @return object|array * @since 5.0.0 */ public static function get_pub_users( $args = array() ) { $defaults = array( 'output_type' => OBJECT ); $atts = wp_parse_args( $args, $defaults ); global $wpdb; $result = $wpdb->get_results("SELECT DISTINCT user FROM " . TEACHPRESS_USER, $atts['output_type']); return $result; } /** * Returns an array or object of publication types which are used for existing publication entries * * Possible values for the array $args: * user (STRING) User IDs (separated by comma) * include (STRING) Publication types (separated by comma) * exclude (STRING) Publication types (separated by comma) * output type (STRING) OBJECT, ARRAY_A, ARRAY_N, default is ARRAY_A * * @param array $args * @return object|array * @since 5.0.0 */ public static function get_used_pubtypes( $args = array() ) { $defaults = array( 'user' => '', 'include' => '', 'exclude' => '', 'output_type' => ARRAY_A ); $atts = wp_parse_args( $args, $defaults ); global $wpdb; $output_type = $atts['output_type']; $include = TP_DB_Helpers::generate_where_clause($atts['include'], "type", "OR", "="); $exclude = TP_DB_Helpers::generate_where_clause($atts['exclude'], "type", "OR", "!="); $user = TP_DB_Helpers::generate_where_clause($atts['user'], "u.user", "OR", "="); $having = ( $include != '' || $exclude != '' ) ? " HAVING $include $exclude " : ""; if ( $user == '' ) { return $wpdb->get_results("SELECT DISTINCT p.type FROM " .TEACHPRESS_PUB . " p $having ORDER BY p.type ASC", $output_type); } return $wpdb->get_results("SELECT DISTINCT p.type AS type from " .TEACHPRESS_PUB . " p INNER JOIN " .TEACHPRESS_USER . " u ON u.pub_id=p.pub_id WHERE $user $having ORDER BY p.type ASC", $output_type); } /** * Returns an object or array with the years where publications are written * * Possible values for the array $args: * type (STRING) Publication types (separated by comma) * user (STRING) User IDs (separated by comma) * order (STRING) ASC or DESC; default is ASC * output type (STRING) OBJECT, ARRAY_A, ARRAY_N, default is OBJECT * * @param array $args * @return object|array * @since 5.0.0 */ public static function get_years( $args = array() ) { $defaults = array( 'type' => '', 'user' => '', 'include' => '', 'years_between' => '', 'order' => 'ASC', 'output_type' => OBJECT ); $atts = wp_parse_args( $args, $defaults ); global $wpdb; // JOIN $join = ( $atts['user']) ? "INNER JOIN " . TEACHPRESS_USER . " u ON u.pub_id = p.pub_id" : ''; // WHERE clause $nwhere = array(); $nwhere[] = TP_DB_Helpers::generate_where_clause($atts['type'], "p.type", "OR", "="); $nwhere[] = TP_DB_Helpers::generate_where_clause($atts['user'], "u.user", "OR", "="); $where = TP_DB_Helpers::compose_clause($nwhere); // HAVING clause $having = ''; if ( $atts['include'] != '' && $atts['include'] !== '0' ) { $having = ' HAVING ' . TP_DB_Helpers::generate_where_clause($atts['include'], "year", "OR", "="); } if ( $atts['include'] === '' && $atts['years_between'] != '' ) { $having = ' HAVING ' . TP_DB_Helpers::generate_between_clause($atts['years_between'], "year"); } // END $order = TP_DB_Helpers::validate_qualifier($atts['order']); $result = $wpdb->get_results("SELECT DISTINCT DATE_FORMAT(p.date, '%Y') AS year FROM " . TEACHPRESS_PUB . " p $join $where $having ORDER BY year $order", $atts['output_type']); return $result; } /** * Adds a publication * @param array $data An associative array of publication data (title, type, bibtex, author, editor,...) * @param string $tags An associative array of tags * @param array $bookmark An array of bookmark IDs * @return int The ID of the new publication * @since 5.0.0 */ public static function add_publication($data, $tags, $bookmark = array() ) { global $wpdb; $defaults = self::get_default_fields(); $post_time = current_time('mysql',0); $data = wp_parse_args( $data, $defaults ); // check last chars of author/editor fields if ( substr($data['author'], -5) === ' and ' ) { $data['author'] = substr($data['author'] ,0 , strlen($data['author']) - 5); } if ( substr($data['editor'], -5) === ' and ' ) { $data['editor'] = substr($data['editor'] ,0 , strlen($data['editor']) - 5); } // replace double spaces from author/editor fields $data['author'] = str_replace(' ', ' ', $data['author']); $data['editor'] = str_replace(' ', ' ', $data['editor']); $wpdb->insert( TEACHPRESS_PUB, array( 'bibtex' => stripslashes( TP_Publications::generate_unique_bibtex_key($data['bibtex']) ), 'type' => $data['type'], 'award' => $data['award'], 'title' => ( $data['title'] === '' ) ? '[' . esc_html__('No title','teachpress') . ']' : stripslashes($data['title']), 'author' => stripslashes($data['author']), 'editor' => stripslashes($data['editor']), 'isbn' => $data['isbn'], 'url' => $data['url'], 'date' => preg_match('/\d\d\d\d-\d\d-\d\d/', $data['date']) === 1 ? $data['date'] : '0000-00-00', 'urldate' => preg_match('/\d\d\d\d-\d\d-\d\d/', $data['urldate']) === 1 ? $data['urldate'] : '0000-00-00', 'booktitle' => stripslashes($data['booktitle']), 'issuetitle' => stripslashes($data['issuetitle']), 'journal' => stripslashes($data['journal']), 'issue' => stripslashes($data['issue']), 'volume' => stripslashes($data['volume']), 'number' => stripslashes($data['number']), 'pages' => stripslashes($data['pages']), 'publisher' => stripslashes($data['publisher']), 'address' => stripslashes($data['address']), 'edition' => stripslashes($data['edition']), 'chapter' => stripslashes($data['chapter']), 'institution' => stripslashes($data['institution']), 'organization' => stripslashes($data['organization']), 'school' => stripslashes($data['school']), 'series' => stripslashes($data['series']), 'crossref' => stripslashes($data['crossref']), 'abstract' => stripslashes($data['abstract']), 'howpublished' => stripslashes($data['howpublished']), 'key' => stripslashes($data['key']), 'techtype' => stripslashes($data['techtype']), 'comment' => stripslashes($data['comment']), 'note' => stripslashes($data['note']), 'image_url' => $data['image_url'], 'image_target' => $data['image_target'], 'image_ext' => $data['image_ext'], 'doi' => $data['doi'], 'is_isbn' => $data['is_isbn'], 'rel_page' => $data['rel_page'], 'status' => stripslashes($data['status']), 'added' => $post_time, 'modified' => $post_time, 'import_id' => $data['import_id'] ), array( '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%d', '%d', '%s', '%s', '%s', '%d' ) ); $pub_id = $wpdb->insert_id; // Error message for the user: if ( $pub_id === 0 && $wpdb->last_error !== '' ) { get_tp_message($data['title'] . ':