get($id); if ($rec) $this->populate($rec); } } function populate($rec){ $this->id = $rec->id; $this->date_created = strtotime($rec->date_created); $this->date_updated = strtotime($rec->date_updated); $this->item_type = $rec->item_type; $this->item_id = $rec->item_id; $this->item_source = $rec->item_source; $this->item_status = $rec->item_status; } // requires cat names, can't use cat ids here because the cats can come from wp and do not exist here function update_categories($cats, $taxonomy = ''){ if (!is_array($cats)) $cats = array($cats); $listeners = oci_get_listeners($this->item_type); if (!empty($this->item_source)) array_merge($listeners, (array)oci_get_listeners($this->item_type, $this->item_source)); if (!empty($taxonomy)) array_merge($listeners, (array)$taxonomy); foreach ($listeners as $tax){ if (is_taxonomy_hierarchical($tax)) oci_set_object_terms($this->id, $cats, $tax); } } function update_tags($terms, $taxonomy = ''){ global $bp; if ( empty($terms) ) $terms = array(); $terms = is_array($terms) ? $terms : explode( ',', trim($terms, " \n\t\r\0\x0B,") ); $listeners = oci_get_listeners($this->item_type); if (!empty($this->item_source)) array_merge($listeners, (array)oci_get_listeners($this->item_type, $this->item_source)); if (!empty($taxonomy)) array_merge($listeners, (array)$taxonomy); foreach ($listeners as $tax){ if (!is_taxonomy_hierarchical($tax)) oci_set_object_terms($this->id, $terms, $tax); } } /** * get_by_source_item_id() * * Gets an item by it's original source item id and item type. * * @param string $item_id original item id, serialized * @param string $item_type original content type * @return mixed false for error or no item | object record from items table */ function get_by_source_item_id($item_id, $item_type, $item_source = ''){ global $wpdb, $bp; $sql = $wpdb->prepare( "SELECT * FROM {$bp->contents->items->table_name} WHERE item_type = %s AND item_id = %s AND item_source = %s", $item_type, $item_id, $item_source); $result = $wpdb->get_row($sql); if (!$result) return false; return $result; } function get($id){ global $wpdb, $bp; $sql = $wpdb->prepare( "SELECT * FROM {$bp->contents->items->table_name} WHERE id = %d", $id ); $result = $wpdb->get_row($sql); if (!$result) return false; return $result; } // get the item recs for all $terms in all $taxonomies, paginated function get_items_in_terms($terms, $taxonomies, $type, $limit = null, $page = 1){ global $wpdb, $bp; if ( !is_array( $terms) ) $terms = array($terms); foreach ($terms as $slug){ $term = oci_get_term_by('slug', $slug, $taxonomies); if ($term) $_terms[] = $term->term_id; } $ids = oci_get_objects_in_term( $_terms, $taxonomies); $ids = implode(', ', $ids); if ( $limit && $page ) $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) ); if ('all' == $type) $type_sql = ''; else $type_sql = $wpdb->prepare(" AND item_type = %s", $type); $total_sql = $wpdb->prepare("SELECT DISTINCT count(id) FROM {$bp->contents->items->table_name} WHERE id IN ({$ids}) {$type_sql} ORDER BY date_created DESC"); $total_items = $wpdb->get_var($total_sql); $items_sql = $wpdb->prepare("SELECT * FROM {$bp->contents->items->table_name} WHERE id IN ({$ids}) {$type_sql} ORDER BY date_created DESC{$pag_sql}", $pag_sql); $paged_items = $wpdb->get_results($items_sql); if (empty($paged_items)) return false; return array('items' => $paged_items, 'total' => $total_items); } /** * save() * * Inserts new or updates item in items table * * $this->id should be null to cause an insert, else an update is done * @return boolean success or failure */ function save(){ global $wpdb, $bp; if ( $this->id ) { $sql = $wpdb->prepare( "UPDATE {$bp->contents->items->table_name} SET date_created = FROM_UNIXTIME(%d), date_updated %d, item_type = %s, item_id = %s, item_source = %s, item_version = %s, item_status = %s WHERE id = %d ", $this->date_created, $this->date_updated = time(), $this->item_type, $this->item_id, $this->item_source, $this->item_version, $this->item_status, $this->id ); } else { $sql = $wpdb->prepare( "INSERT INTO {$bp->contents->items->table_name} ( date_created, date_updated, item_type, item_id, item_source, item_version, item_status ) VALUES (FROM_UNIXTIME(%d), FROM_UNIXTIME(%d), %s, %s, %s, %s, %s)", $this->date_created = time(), $this->date_updated = time(), $this->item_type, $this->item_id, $this->item_source, $this->item_version, $this->item_status ); } if ( false === $wpdb->query($sql) ) return false; if ( !$this->id ) { $this->id = $wpdb->insert_id; } return true; } /** * Deletes this item from the bpc_items table. Also deletes all taxonomy/term/item relationships * */ function delete($id = false){ global $wpdb, $bp; if (!$id) $id = $this->id; // get the tax that are associated with items $obj->post_type = OCI_ITEM; $taxs = get_object_taxonomies($obj); // delete the item from the taxonomies oci_delete_object_term_relationships($id, $taxs); // delete the item return $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->contents->items->table_name} WHERE id = %d", $id)); } // returns a live instance of the original, derived item, fully populated function get_source_object($item_id = false, $type = false){ if (!$item_id && !$type){ $item_id = $this->item_id; $type = $this->item_type; } $source_class = "OCI_Item_" . $type; if (class_exists($source_class)){ $source_obj = new $source_class($item_id); } return $source_obj; } } // class OCI_Item function oci_screen_items_directory() { global $bp; if ( $bp->current_component == $bp->contents->slug && empty( $bp->current_action ) ) { $bp->is_directory = true; bp_core_load_template( apply_filters( 'oci_screen_items_directory', 'bpcontents/items/index' ) ); } if ( $bp->current_component == $bp->contents->slug ) { if ( isset( $_POST['submit_taxonomy_select_go'] )) { if ( !check_admin_referer( 'oci_taxonomy_select' ) ) return false; $taxonomy = $_POST['taxonomy_select']; $type = $_POST['content_type_select']; $path = oci_get_the_items_action(); $path .= '/?taxonomy=' . $taxonomy; $path .= '&type=' . $type; bp_core_redirect($path); } } } add_action( 'wp', 'oci_screen_items_directory', 2 ); // item id, taxonomy function oci_get_item_tags_edit($id, $taxonomy){ $terms = oci_get_the_terms($id, $taxonomy); if (!$terms) return false; $term_names = oci_objects_get_properties($terms, 'name'); $term_names = join(', ', $term_names); return $term_names; } function oci_update_item_categories($item_id, $terms, $taxonomy = ''){ $item = new OCI_Item($item_id); if (!$item->id) return false; $item->update_categories($terms, $taxonomy); } function oci_update_item_tags($item_id, $terms, $taxonomy = ''){ global $bp; $item = new OCI_Item($item_id); if (!$item->id) return false; $item->update_tags($terms, $taxonomy); } // returns a live instance of the original, derived item, fully populated function oci_source_get_item($id){ $item = new OCI_Item($id); if ($item->id){ $src_item = $item->get_source_object(); } // instance of the source item derived class return $src_item; } function oci_get_item($id){ $item = new OCI_Item($id); } // returns an arbitrary source item property from a fully populated derived item function oci_get_item_property($property, $id = false){ global $oci_items_template; if ($id) $src_item = oci_source_get_item($id); else $src_item = $oci_items_template->item->item; return $src_item->item->{$property}; } function oci_get_item_tags( $id, $taxonomy, $args = array() ) { $id = (int) $id; $defaults = array('fields' => 'ids'); $args = wp_parse_args( $args, $defaults ); $tags = oci_get_object_terms($id, $taxonomy, $args); return $tags; } function oci_update_item_term_count( $terms ) { global $wpdb, $bp; foreach ( (array) $terms as $term ) { $count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$bp->contents->term_relationships->table_name}, {$bp->contents->items->table_name} WHERE {$bp->contents->items->table_name}.id = {$bp->contents->term_relationships->table_name}.object_id AND term_taxonomy_id = %d", $term ) ); $wpdb->update( $bp->contents->term_taxonomy->table_name, compact( 'count' ), array( 'term_taxonomy_id' => $term ) ); } } function oci_get_item_categories( $id, $taxonomy, $args = array() ) { $id = (int) $id; $defaults = array('fields' => 'ids'); $args = wp_parse_args( $args, $defaults ); $cats = oci_get_object_terms($id, $taxonomy, $args); return $cats; } ?>