forked from BeProSoftware/bepro-listings
/
bepro-listings-bp.php
202 lines (166 loc) · 8.17 KB
/
bepro-listings-bp.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<?php
function display_item_list() {
add_action( 'bp_template_content', 'display_item_content' );
bp_core_load_template( apply_filters( 'bp_core_template_plugin', 'members/single/plugins' ) );
}
function display_item_content(){
global $wpdb, $bp;
$types = listing_types();
$current_user = wp_get_current_user();
$user_id = $bp->displayed_user->id;
// get records
$items = $wpdb->get_results("SELECT geo.*, wp_posts.post_title, wp_posts.post_status FROM ".$wpdb->prefix.BEPRO_LISTINGS_TABLE_NAME." as geo
LEFT JOIN ".$wpdb->prefix."posts as wp_posts on wp_posts.ID = geo.post_id WHERE wp_posts.post_status != 'trash' AND wp_posts.post_author = ".$user_id);
$listing_url = $bp->loggedin_user->domain.$bp->current_component."/".BEPRO_LISTINGS_CREATE_SLUG."/";
require( dirname( __FILE__ ) . '/templates/list.php' );
}
function create_listings() {
global $wpdb, $bp;
$current_user = wp_get_current_user();
//categories
$user_ID = $bp->displayed_user->id;
if(isset($_POST["save_bepro_listing"]) && !empty($_POST["save_bepro_listing"])){
$success = false;
$success = bepro_listings_save();
if($success)
$message = urlencode("Success saving listing");
else
$message = urlencode("Error saving listing");
$current_user = wp_get_current_user();
$bp_profile_link = bp_core_get_user_domain( $bp->displayed_user->id);
wp_redirect($bp_profile_link . BEPRO_LISTINGS_SLUG ."?message=".$message);
exit;
}elseif(isset($bp->action_variables[0]) && ($bp->current_action == BEPRO_LISTINGS_CREATE_SLUG)){
add_action( 'bp_template_content', 'update_listing_content' );
}else{
add_action( 'bp_template_content', 'create_listing_content' );
}
bp_core_load_template( apply_filters( 'bp_core_template_plugin', 'members/single/plugins' ) );
}
function create_listing_content(){
global $bp;
//get settings
$data = get_option("bepro_listings");
$default_user_id = $data["default_user_id"];
$num_images = $data["num_images"];
$validate = $data["validate_form"];
$show_cost = $data["show_cost"];
$show_con = $data["show_con"];
$show_geo = $data["show_geo"];
$listing_url = $bp->loggedin_user->domain.$bp->current_component;
require( dirname( __FILE__ ) . '/templates/form.php' );
}
function update_listing_content(){
global $wpdb, $bp, $post;
$data = get_option("bepro_listings");
//get information
$item = $wpdb->get_row("SELECT * FROM ".$wpdb->prefix.BEPRO_LISTINGS_TABLE_NAME." WHERE id = ".$bp->action_variables[0]);
$post_data = get_post($item->post_id);
//get categories
$raw_categories = listing_types_by_post($item->post_id);
$categories = array();
if($raw_categories){
foreach($raw_categories as $category) {
$categories[$category->term_id] = $category->term_id;
}
}
$args = array(
'numberposts' => -1,
'post_parent' => $item->post_id,
'post_type' => 'attachment'
);
//get images
$attachments = get_posts($args);
$thunmbnails = array();
$file_icons = array("application/pdf" => "document.png", "text/plain" => "text.png", "text/csv" => "spreadsheet.png","application/rar" => "archive.png", "application/x-tar" => "archive.png", "application/zip" => "archive.png", "application/x-gzip" => "archive.png","application/x-7z-compressed" => "archive.png","application/msword" => "document.png","application/vnd.oasis.opendocument.text" => "document.png","application/vnd.oasis.opendocument.presentation" => "text.png","application/vnd.oasis.opendocument.spreadsheet" => "interactive.png","application/vnd.oasis.opendocument.graphics" => "interactive.png", "application/vnd.oasis.opendocument.chart" => "spreadsheet.png","application/wordperfect" => "document.png", "video/x-ms-asf" => "video.png", "video/x-ms-wmv" => "video.png", "video/x-ms-wmx" => "video.png", "video/x-ms-wm" => "video.png", "video/avi" => "video.png", "video/divx" => "video.png","video/x-flv" => "video.png", "video/quicktime" => "video.png", "video/mpeg" => "video.png", "video/mp4" => "video.png", "video/ogg" => "video.png", "video/webm" => "video.png", "video/x-matroska" => "video.png");
if($attachments){
foreach ($attachments as $attachment) {
$image = wp_get_attachment_image_src($attachment->ID,'thumbnail', false);
if(!$image){
$p_type = get_post_mime_type($attachment->ID);
$f_type = empty($file_icons[$p_type])? "text.png":$file_icons[$p_type];
$image[0] = get_bloginfo("wpurl")."/wp-includes/images/crystal/".$f_type;
}
$image[4] = $attachment->ID;
$image[5] = basename ( get_attached_file( $attachment->ID ) );
$thunmbnails[] = $image;
}
}
//get settings
$data = get_option("bepro_listings");
$default_user_id = $data["default_user_id"];
$num_images = $data["num_images"];
$validate = $data["validate_form"];
$show_cost = $data["show_cost"];
$show_con = $data["show_con"];
$show_geo = $data["show_geo"];
$cat_drop = $data["cat_drop"];
$siteNo = $bp->groups->current_group->id;
$listing_url = $bp->loggedin_user->domain.BEPRO_LISTINGS_SLUG."/";
require( dirname( __FILE__ ) . '/templates/form.php' );
}
/**
* Get the current view type when the item type is 'group'
*
* @package BuddyPress Docs
* @since 1.0-beta
*/
function get_current_view( ) {
global $bp;
if ( empty( $bp->action_variables[0] ) ) {
// An empty $bp->action_variables[0] means that you're looking at a list
$view = 'list';
} else if ( $bp->action_variables[0] == "create" ) {
// Create new doc
$view = 'create';
} else if ( !empty( $bp->action_variables[1] ) && $bp->action_variables[1] == "edit" ) {
// Create new doc
$view = 'edit';
}
return $view;
}
// Set up Cutsom BP navigation
function bepro_listings_nav() {
global $bp;
$settings_link = $bp->loggedin_user->domain . BEPRO_LISTINGS_SLUG. '/';
bp_core_new_nav_item( array(
'name' => __( BEPRO_LISTINGS_SLUG, 'buddypress' ),
'slug' => BEPRO_LISTINGS_SLUG,
'position' => 20,
'default_subnav_slug' => 'List',
'screen_function' => 'display_item_list'
) );
bp_core_new_subnav_item( array( 'name' => __( BEPRO_LISTINGS_LIST_SLUG, 'buddypress' ), 'slug' => BEPRO_LISTINGS_LIST_SLUG, 'parent_url' => $settings_link, 'parent_slug' => BEPRO_LISTINGS_SLUG, 'screen_function' => 'display_item_list', 'position' => 10) );
bp_core_new_subnav_item( array( 'name' => __( BEPRO_LISTINGS_CREATE_SLUG, 'buddypress' ), 'slug' => BEPRO_LISTINGS_CREATE_SLUG, 'parent_url' => $settings_link, 'parent_slug' => BEPRO_LISTINGS_SLUG, 'screen_function' => 'create_listings', 'position' => 90, 'user_has_access' => bp_is_my_profile() ) );
// Change the order of menu items
$bp->bp_nav[BEPRO_LISTINGS_SLUG]['position'] = 100;
bepro_listings_nav_count();
}
function bepro_listings_nav_count() {
global $bp, $wpdb;
$user_id = $bp->displayed_user->id;
// This will probably only work on BP 1.3+
if ( !empty( $bp->bp_nav[BEPRO_LISTINGS_SLUG]) ) {
$current_tab_name = $bp->bp_nav[BEPRO_LISTINGS_SLUG]['name'];
$item_count = $wpdb->get_row("SELECT count(*) as item_count FROM ".$wpdb->prefix.BEPRO_LISTINGS_TABLE_NAME." as geo
LEFT JOIN ".$wpdb->prefix."posts as wp_posts on wp_posts.ID = geo.post_id WHERE wp_posts.post_status != 'trash' AND wp_posts.post_author = ".$user_id);
if($item_count) $item_count = $item_count->item_count;
$bp->bp_nav[BEPRO_LISTINGS_SLUG]['name'] = sprintf( __( '%s <span>%d</span>', BEPRO_LISTINGS_SLUG ), $current_tab_name, $item_count );
}
}
bepro_listings_nav();
function bepro_listings_bp_activity_publish( $post_types ) {
$post_types[] = 'bepro_listings';
return $post_types;
}
function bepro_listings_bp_activity_action( $activity_action, $post, $post_permalink ) {
global $bp;
if( $post->post_type == 'bepro_listings' ) {
if ( is_multisite() )
$activity_action = sprintf( __( '%1$s created a new listing, %2$s, on the site %3$s', 'bepro_listings' ), bp_core_get_userlink( (int) $post->post_author ), '' . $post->post_title . '', '' . get_blog_option( $blog_id, 'blogname' ) . '' );
else
$activity_action = sprintf( __( '%1$s created a new listing, %2$s', 'bepro_listings' ), bp_core_get_userlink( (int) $post->post_author ), '' . $post->post_title . '' );
}
return $activity_action;
}
?>