PluginProbe
Property Hive / 2.2.3
Property Hive v2.2.3
2.3.1 2.3.0 2.2.6 2.2.5 2.2.4 2.2.3 2.2.2 1.4.46 1.4.47 1.4.48 1.4.49 1.4.5 1.4.50 1.4.51 1.4.52 1.4.53 1.4.54 1.4.55 1.4.56 1.4.57 1.4.58 1.4.59 1.4.6 1.4.60 1.4.61 All 261 releases
propertyhive / includes / admin / post-types / class-ph-admin-cpt-tenancy.php

class-ph-admin-cpt-tenancy.php in Property Hive 2.2.3, at includes/admin/post-types/class-ph-admin-cpt-tenancy.php

285 lines 6.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Admin functions for the tenancy post type
4 *
5 * @author PropertyHive
6 * @category Admin
7 * @package PropertyHive/Admin/Post Types
8 * @version 1.0.0
9 */
10
11 if ( ! defined( 'ABSPATH' ) ) {
12 exit; // Exit if accessed directly
13 }
14
15 if ( ! class_exists( 'PH_Admin_CPT' ) ) {
16 include( 'class-ph-admin-cpt.php' );
17 }
18
19 if ( ! class_exists( 'PH_Admin_CPT_Tenancy' ) ) :
20
21 /**
22 * PH_Admin_CPT_Tenancy Class
23 */
24 class PH_Admin_CPT_Tenancy extends PH_Admin_CPT {
25
26 /**
27 * Constructor
28 */
29 public function __construct() {
30 $this->type = 'tenancy';
31
32 // Before data updates
33 add_action( 'pre_post_update', array( $this, 'pre_post_update' ) );
34 add_filter( 'wp_insert_post_data', array( $this, 'wp_insert_post_data' ) );
35
36 // Admin Columns
37 add_filter( 'manage_edit-tenancy_columns', array( $this, 'edit_columns' ) );
38 add_action( 'manage_tenancy_posts_custom_column', array( $this, 'custom_columns' ), 2 );
39 add_filter( 'list_table_primary_column', array( $this, 'set_primary_column' ), 10, 2 );
40 add_filter( 'post_row_actions', array( $this, 'remove_actions' ), 10, 2 );
41 add_filter( 'manage_edit-tenancy_sortable_columns', array( $this, 'custom_columns_sort' ) );
42 add_filter( 'request', array( $this, 'custom_columns_orderby' ) );
43
44 // Bulk / quick edit
45 add_filter( 'bulk_actions-edit-tenancy', array( $this, 'remove_bulk_actions' ) );
46
47 // Call PH_Admin_CPT constructor
48 parent::__construct();
49 }
50
51 /**
52 * Check if we're editing or adding a tenancy
53 * @return boolean
54 */
55 private function is_editing_tenancy() {
56 if ( ! empty( $_GET['post_type'] ) && 'tenancy' == $_GET['post_type'] ) {
57 return true;
58 }
59 if ( ! empty( $_GET['post'] ) && 'tenancy' == get_post_type( (int) $_GET['post'] ) ) {
60 return true;
61 }
62 if ( ! empty( $_REQUEST['post_id'] ) && 'tenancy' == get_post_type( (int) $_REQUEST['post_id'] ) ) {
63 return true;
64 }
65
66 return false;
67 }
68
69 /**
70 * @param int $post_id
71 */
72 public function pre_post_update( $post_id ) {
73
74 }
75
76 /**
77 * @param array $data
78 *
79 * @return array
80 */
81 public function wp_insert_post_data( $data ) {
82
83
84 return $data;
85 }
86
87 /**
88 * Change the columns shown in admin.
89 */
90 public function edit_columns( $existing_columns ) {
91
92 if ( empty( $existing_columns ) && ! is_array( $existing_columns ) )
93 {
94 $existing_columns = array();
95 }
96
97 unset( $existing_columns['title'], $existing_columns['comments'], $existing_columns['date'] );
98
99 $columns = array();
100 $columns['cb'] = '<input type="checkbox" />';
101 $columns['property'] = __( 'Property', 'propertyhive' );
102 $columns['property_owner'] = __( 'Landlord', 'propertyhive' );
103 $columns['applicant'] = __( 'Tenants', 'propertyhive' );
104 $columns['start_date'] = __( 'Start Date', 'propertyhive' );
105 $columns['end_date'] = __( 'End Date', 'propertyhive' );
106 $columns['rent'] = __( 'Rent', 'propertyhive' );
107 $columns['status'] = __( 'Status', 'propertyhive' );
108
109 return array_merge( $columns, $existing_columns );
110 }
111
112 /**
113 * Define our custom columns shown in admin.
114 *
115 * @param string $column
116 */
117 public function custom_columns( $column ) {
118 global $post, $propertyhive, $the_tenancy;
119
120 if ( empty( $the_tenancy ) || $the_tenancy->ID != $post->ID )
121 {
122 $the_tenancy = new PH_Tenancy( $post->ID );
123 }
124
125 switch ( $column ) {
126
127 case 'property' :
128
129 $edit_link = get_edit_post_link( $post->ID );
130 $post_type_object = get_post_type_object( $post->post_type );
131 $can_edit_post = current_user_can( $post_type_object->cap->edit_post, $post->ID );
132
133 $property = new PH_Property( (int) $the_tenancy->property_id );
134 echo '<strong><a class="row-title" href="' . esc_url( $edit_link ) . '">' . esc_html($property->get_formatted_full_address()) . '</a></strong>';
135 break;
136 case 'property_owner' :
137
138 $the_property = new PH_Property( (int) $the_tenancy->property_id );
139 $owner_contact_ids = $the_property->_owner_contact_id;
140 if (
141 ( ! is_array( $owner_contact_ids ) && $owner_contact_ids != '' && $owner_contact_ids != 0 )
142 ||
143 ( is_array( $owner_contact_ids ) && ! empty( $owner_contact_ids ) )
144 ) {
145 if ( ! is_array( $owner_contact_ids ) )
146 {
147 $owner_contact_ids = array( $owner_contact_ids );
148 }
149
150 foreach ( $owner_contact_ids as $owner_contact_id )
151 {
152 echo esc_html(get_the_title( $owner_contact_id )) . '<br>';
153 if ( count( $owner_contact_ids ) == 1 )
154 {
155 echo '<div class="row-actions">';
156 echo 'T: ' . esc_html(get_post_meta( $owner_contact_id, '_telephone_number', true )) . '<br>';
157 echo 'E: ' . esc_html(get_post_meta( $owner_contact_id, '_email_address', true ));
158 echo '</div>';
159 }
160 }
161 }
162 else
163 {
164 echo '-';
165 }
166
167 break;
168 case 'applicant' :
169
170 echo $the_tenancy->get_tenants(false, true);
171
172 break;
173 case 'start_date' :
174 echo ( $the_tenancy->_start_date != '' ? esc_html(date( "d/m/Y", strtotime( $the_tenancy->_start_date ) )) : '-' );
175
176 break;
177 case 'end_date' :
178 echo ( $the_tenancy->_end_date != '' ? esc_html(date( "d/m/Y", strtotime( $the_tenancy->_end_date ) )) : '-' );
179
180 break;
181 case 'rent' :
182
183 echo $the_tenancy->get_formatted_rent();
184
185 break;
186 case 'status' :
187
188 echo $the_tenancy->get_status();
189
190 break;
191 default :
192 break;
193 }
194 }
195
196 public function set_primary_column( $default, $screen ) {
197
198 if ( 'edit-tenancy' === $screen )
199 {
200 $default = 'property';
201 }
202
203 return $default;
204 }
205
206 public function remove_actions($actions, $post) {
207
208 if ( $post->post_type !== 'tenancy' )
209 {
210 return $actions;
211 }
212
213 // Remove 'Quick Edit' link
214 if ( isset($actions['inline hide-if-no-js']) )
215 {
216 unset($actions['inline hide-if-no-js']);
217 }
218
219 if ( isset($actions['trash']) )
220 {
221 unset($actions['trash']);
222 }
223
224 return $actions;
225 }
226
227 /**
228 * Make property columns sortable
229 *
230 * https://gist.github.com/906872
231 *
232 * @access public
233 * @param mixed $columns
234 * @return array
235 */
236 public function custom_columns_sort( $columns ) {
237 $custom = array(
238 'start_date' => '_start_date',
239 'end_date' => '_end_date'
240 );
241 return wp_parse_args( $custom, $columns );
242 }
243
244 /**
245 * Property column orderby
246 *
247 * @access public
248 * @param mixed $vars
249 * @return array
250 */
251 public function custom_columns_orderby( $vars ) {
252 if ( isset( $vars['orderby'] ) ) {
253 if ( '_start_date' == $vars['orderby'] ) {
254 $vars = array_merge( $vars, array(
255 'meta_key' => '_start_date',
256 'orderby' => 'meta_value'
257 ) );
258 }
259 elseif ( '_end_date' == $vars['orderby'] ) {
260 $vars = array_merge( $vars, array(
261 'meta_key' => '_end_date',
262 'orderby' => 'meta_value'
263 ) );
264 }
265 }
266
267 return $vars;
268 }
269
270 /**
271 * Remove bulk edit option
272 *
273 * @param array $actions
274 */
275 public function remove_bulk_actions( $actions ) {
276 unset( $actions['edit'] );
277
278 return $actions;
279 }
280 }
281
282 endif;
283
284 return new PH_Admin_CPT_Tenancy();
285