| 1 |
<?php |
| 2 |
namespace WeDevs\ERP\HRM; |
| 3 |
|
| 4 |
use WeDevs\ERP\Framework\Traits\Hooker; |
| 5 |
|
| 6 |
/** |
| 7 |
* Announcement class HR |
| 8 |
* |
| 9 |
* Announcement for employees |
| 10 |
* |
| 11 |
* @since 0.1 |
| 12 |
* |
| 13 |
* @author weDevs <[email protected]> |
| 14 |
*/ |
| 15 |
class Announcement { |
| 16 |
|
| 17 |
use Hooker; |
| 18 |
|
| 19 |
private $post_type = 'erp_hr_announcement'; |
| 20 |
private $assign_type = array(); |
| 21 |
|
| 22 |
/** |
| 23 |
* Load autometically all actions |
| 24 |
*/ |
| 25 |
function __construct() { |
| 26 |
$this->assign_type = array( |
| 27 |
'' => __( '-- Select --', 'erp' ), |
| 28 |
'all_employee' => __( 'All Employees', 'erp' ), |
| 29 |
'selected_employee' => __( 'Selected Employee', 'erp' ), |
| 30 |
'by_department' => __( 'By Department', 'erp' ), |
| 31 |
'by_designation' => __( 'By Designation', 'erp' ) |
| 32 |
); |
| 33 |
|
| 34 |
$this->action( 'init', 'post_types' ) ; |
| 35 |
$this->action( 'do_meta_boxes', 'do_metaboxes' ); |
| 36 |
$this->action( 'save_post', 'save_announcement_meta', 10, 2 ); |
| 37 |
|
| 38 |
$this->filter( 'manage_edit-erp_hr_announcement_columns', 'add_type_columns' ); |
| 39 |
$this->filter( 'manage_erp_hr_announcement_posts_custom_column', 'assign_type_edit_columns', 10, 2 ); |
| 40 |
|
| 41 |
// $this->filter( 'parent_file', 'fix_parent_file', 999 ); |
| 42 |
// $this->filter( 'submenu_file', 'submenu_file', 999 ); |
| 43 |
|
| 44 |
$this->action( 'admin_head', 'filter_admin_sidebar_menu_items' ); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Filter admin sidebar menu items |
| 49 |
* |
| 50 |
* Remove HR Announcement items from sidebar generated by register_post_type function. |
| 51 |
* Highlight Parent menu as active item when we are in questionnaire menu page and sub pages. |
| 52 |
* |
| 53 |
* @since 1.1.6 |
| 54 |
* |
| 55 |
* @return void |
| 56 |
*/ |
| 57 |
function filter_admin_sidebar_menu_items() { |
| 58 |
global $menu, $submenu_file, $typenow; |
| 59 |
|
| 60 |
$hr_menu = array_filter( $menu, function ( $item ) { |
| 61 |
return __( 'HR Announcement', 'erp' ) === $item[0]; |
| 62 |
} ); |
| 63 |
|
| 64 |
$announcement_pages = [ |
| 65 |
'post-new.php?post_type=erp_hr_announcement', |
| 66 |
'edit.php?post_type=erp_hr_announcement' |
| 67 |
]; |
| 68 |
|
| 69 |
if ( in_array( $submenu_file , $announcement_pages ) ) { |
| 70 |
$submenu_file = 'edit.php?post_type=erp_hr_announcement'; |
| 71 |
$typenow = null; |
| 72 |
$_SERVER['PHP_SELF'] = 'erp-hr'; |
| 73 |
|
| 74 |
add_filter( 'parent_file', function () { |
| 75 |
return 'edit.php?post_type=erp_hr_announcement'; |
| 76 |
} ); |
| 77 |
} |
| 78 |
|
| 79 |
$hr_menu_position = key( $hr_menu ); |
| 80 |
|
| 81 |
unset( $menu[ $hr_menu_position ] ); |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Fix parent file |
| 86 |
* |
| 87 |
* @param string $parent_file |
| 88 |
* |
| 89 |
* @return string |
| 90 |
*/ |
| 91 |
function fix_parent_file( $parent_file ) { |
| 92 |
global $current_screen; |
| 93 |
|
| 94 |
if ( $current_screen->post_type == $this->post_type ) { |
| 95 |
$parent_file = 'erp-hr'; |
| 96 |
} |
| 97 |
|
| 98 |
return $parent_file; |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Set submenu file |
| 103 |
* |
| 104 |
* @param string $submenu_file |
| 105 |
* |
| 106 |
* @return string |
| 107 |
*/ |
| 108 |
function submenu_file( $submenu_file ) { |
| 109 |
global $current_screen; |
| 110 |
|
| 111 |
if ( $current_screen->post_type == $this->post_type ) { |
| 112 |
$submenu_file = 'edit.php?post_type=erp_hr_announcement'; |
| 113 |
} |
| 114 |
|
| 115 |
return $submenu_file; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Register Announcement post type |
| 120 |
* |
| 121 |
* @since 0.1 |
| 122 |
* |
| 123 |
* @return void |
| 124 |
*/ |
| 125 |
function post_types() { |
| 126 |
$capability = 'erp_hr_manager'; |
| 127 |
|
| 128 |
register_post_type( $this->post_type, array( |
| 129 |
'label' => __( 'Announcement', 'erp' ), |
| 130 |
'description' => '', |
| 131 |
'public' => true, |
| 132 |
'show_ui' => true, |
| 133 |
'show_in_menu' => true, |
| 134 |
'show_in_nav_menus' => true, |
| 135 |
'exclude_from_search' => false, |
| 136 |
'publicly_queryable' => false, |
| 137 |
'capability_type' => 'post', |
| 138 |
'hierarchical' => false, |
| 139 |
'rewrite' => array( 'slug' => '' ), |
| 140 |
'query_var' => false, |
| 141 |
'supports' => array( 'title', 'editor' ), |
| 142 |
'capabilities' => array( |
| 143 |
'edit_post' => $capability, |
| 144 |
'read_post' => $capability, |
| 145 |
'delete_posts' => $capability, |
| 146 |
'edit_posts' => $capability, |
| 147 |
'edit_others_posts' => $capability, |
| 148 |
'publish_posts' => $capability, |
| 149 |
'read_private_posts' => $capability, |
| 150 |
'create_posts' => $capability, |
| 151 |
'delete_post' => $capability, |
| 152 |
), |
| 153 |
'labels' => array( |
| 154 |
'name' => __( 'Announcement', 'erp' ), |
| 155 |
'singular_name' => __( 'Announcement', 'erp' ), |
| 156 |
'menu_name' => __( 'HR Announcement', 'erp' ), |
| 157 |
'add_new' => __( 'Add Announcement', 'erp' ), |
| 158 |
'add_new_item' => __( 'Add New Announcement', 'erp' ), |
| 159 |
'edit' => __( 'Edit', 'erp' ), |
| 160 |
'edit_item' => __( 'Edit Announcement', 'erp' ), |
| 161 |
'new_item' => __( 'New Announcement', 'erp' ), |
| 162 |
'view' => __( 'View Announcement', 'erp' ), |
| 163 |
'view_item' => __( 'View Announcement', 'erp' ), |
| 164 |
'search_items' => __( 'Search Announcement', 'erp' ), |
| 165 |
'not_found' => __( 'No Announcement Found', 'erp' ), |
| 166 |
'not_found_in_trash' => __( 'No Announcement found in trash', 'erp' ), |
| 167 |
'parent' => __( 'Parent Announcement', 'erp' ) |
| 168 |
), |
| 169 |
) ); |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Initialize metabox for ERP HR announcement post type |
| 174 |
* |
| 175 |
* @since 0.1 |
| 176 |
* |
| 177 |
* @return void |
| 178 |
*/ |
| 179 |
function do_metaboxes() { |
| 180 |
add_meta_box( 'erp-hr-announcement-meta-box', __('Announcement Settings', 'erp'), array( $this, 'meta_boxes_cb' ), $this->post_type, 'advanced', 'high' ); |
| 181 |
} |
| 182 |
|
| 183 |
/** |
| 184 |
* Announcement metabox callback function |
| 185 |
* |
| 186 |
* @param integer $post_id |
| 187 |
* |
| 188 |
* @return void |
| 189 |
*/ |
| 190 |
function meta_boxes_cb( $post_id ) { |
| 191 |
global $post; |
| 192 |
|
| 193 |
$employees = erp_hr_get_employees( [ 'number' => -1, 'no_object' => true ] ); |
| 194 |
$departments = erp_hr_get_departments( [ 'number' => -1, 'no_object' => true ] ); |
| 195 |
$designations = erp_hr_get_designations( [ 'number' => -1, 'no_object' => true ] ); |
| 196 |
|
| 197 |
$announcement_type = get_post_meta( $post->ID, '_announcement_type', true ); |
| 198 |
$announcement_users = get_post_meta( $post->ID, '_announcement_selected_user', true ); |
| 199 |
$announce_departments = get_post_meta( $post->ID, '_announcement_department', true ); |
| 200 |
$announce_designations = get_post_meta( $post->ID, '_announcement_designation', true ); |
| 201 |
|
| 202 |
$announcement_employee = ( $announcement_users ) ? $announcement_users : array(); |
| 203 |
$announcement_department = ( $announce_departments ) ? $announce_departments : array(); |
| 204 |
$announcement_designation = ( $announce_designations ) ? $announce_designations : array(); |
| 205 |
|
| 206 |
?> |
| 207 |
<table class="form-table erp-hr-announcement-meta-wrap-table"> |
| 208 |
<tr> |
| 209 |
<th><?php _e( 'Send Announcement To', 'erp' ); ?></th> |
| 210 |
<td> |
| 211 |
<select name="hr_announcement_assign_type" id="hr_announcement_assign_type" style="width:60%"> |
| 212 |
<?php foreach ( $this->assign_type as $key => $type ): ?> |
| 213 |
<option value="<?php echo $key; ?>" <?php selected( $announcement_type, $key ); ?>><?php echo $type; ?></option> |
| 214 |
<?php endforeach ?> |
| 215 |
</select> |
| 216 |
</td> |
| 217 |
</tr> |
| 218 |
|
| 219 |
<tr class="selected_employee_field"> |
| 220 |
<th><?php _e( 'Select Employees', 'erp' ); ?></th> |
| 221 |
<td> |
| 222 |
<select name="hr_announcement_assign_employee[]" data-placeholder= '<?php echo __( 'Select Employees...', 'erp' ); ?>' id="hr_announcement_assign_employee" class="erp-select2" multiple="multiple"> |
| 223 |
<?php |
| 224 |
foreach ( $employees as $user ) { |
| 225 |
if ( $user->user_id == get_current_user_id() ) { |
| 226 |
continue; |
| 227 |
} |
| 228 |
|
| 229 |
?> |
| 230 |
<option <?php echo in_array( $user->user_id, $announcement_employee ) ? 'selected="selected"' : ''; ?> value='<?php echo $user->user_id ?>'><?php echo $user->display_name; ?></option> |
| 231 |
<?php |
| 232 |
} |
| 233 |
?> |
| 234 |
</select> |
| 235 |
</td> |
| 236 |
</tr> |
| 237 |
|
| 238 |
<tr class="by_department_field"> |
| 239 |
<th><?php _e( 'Select Departments', 'erp' ); ?></th> |
| 240 |
<td> |
| 241 |
<select name="hr_announcement_assign_department[]" data-placeholder= '<?php echo __( 'Select Departments...', 'erp' ); ?>' id="hr_announcement_assign_department" class="erp-select2" multiple="multiple"> |
| 242 |
<?php |
| 243 |
foreach ( $departments as $department ) { |
| 244 |
?> |
| 245 |
<option <?php echo in_array( $department->id, $announcement_department ) ? 'selected="selected"' : ''; ?> value='<?php echo $department->id; ?>'><?php echo $department->title; ?></option> |
| 246 |
<?php |
| 247 |
} |
| 248 |
?> |
| 249 |
</select> |
| 250 |
</td> |
| 251 |
</tr> |
| 252 |
|
| 253 |
<tr class="by_designation_field"> |
| 254 |
<th><?php _e( 'Select Designations', 'erp' ); ?></th> |
| 255 |
<td> |
| 256 |
<select name="hr_announcement_assign_designation[]" data-placeholder= '<?php echo __( 'Select Designations...', 'erp' ); ?>' id="hr_announcement_assign_designation" class="erp-select2" multiple="multiple"> |
| 257 |
<?php |
| 258 |
foreach ( $designations as $designation ) { |
| 259 |
?> |
| 260 |
<option <?php echo in_array( $designation->id, $announcement_designation ) ? 'selected="selected"' : ''; ?> value='<?php echo $designation->id; ?>'><?php echo $designation->title; ?></option> |
| 261 |
<?php |
| 262 |
} |
| 263 |
?> |
| 264 |
</select> |
| 265 |
</td> |
| 266 |
</tr> |
| 267 |
|
| 268 |
<?php do_action( 'hr_announcement_table_last', $post ); ?> |
| 269 |
|
| 270 |
</table> |
| 271 |
<?php wp_nonce_field( 'hr_announcement_meta_action', 'hr_announcement_meta_action_nonce' ); ?> |
| 272 |
|
| 273 |
<script> |
| 274 |
(function( $ ){ |
| 275 |
$( document ).ready( function() { |
| 276 |
|
| 277 |
// Remove selected value other than the currently active one. |
| 278 |
switch ( $('select#hr_announcement_assign_type').val() ) { |
| 279 |
case 'selected_employee': |
| 280 |
$( 'tr.by_department_field select' ).val(null).trigger('change'); |
| 281 |
$( 'tr.by_designation_field select' ).val(null).trigger('change'); |
| 282 |
break; |
| 283 |
|
| 284 |
case 'by_department': |
| 285 |
$( 'tr.selected_employee_field select' ).val(null).trigger('change'); |
| 286 |
$( 'tr.by_designation_field select' ).val(null).trigger('change'); |
| 287 |
break; |
| 288 |
|
| 289 |
case 'by_designation': |
| 290 |
$( 'tr.selected_employee_field select' ).val(null).trigger('change'); |
| 291 |
$( 'tr.by_department_field select' ).val(null).trigger('change'); |
| 292 |
} |
| 293 |
|
| 294 |
$('table.erp-hr-announcement-meta-wrap-table').on( 'change', 'select#hr_announcement_assign_type', function() { |
| 295 |
var self = $( this ); |
| 296 |
|
| 297 |
switch ( self.val() ) { |
| 298 |
case 'all_employee': |
| 299 |
$( 'tr.selected_employee_field' ).hide(); |
| 300 |
$( 'tr.by_department_field' ).hide(); |
| 301 |
$( 'tr.by_designation_field' ).hide(); |
| 302 |
break; |
| 303 |
|
| 304 |
case 'selected_employee': |
| 305 |
$( 'tr.by_department_field' ).hide(); |
| 306 |
$( 'tr.by_designation_field' ).hide(); |
| 307 |
$( 'tr.selected_employee_field' ).show(); |
| 308 |
break; |
| 309 |
|
| 310 |
case 'by_department': |
| 311 |
$( 'tr.selected_employee_field' ).hide(); |
| 312 |
$( 'tr.by_department_field' ).show(); |
| 313 |
$( 'tr.by_designation_field' ).hide(); |
| 314 |
break; |
| 315 |
|
| 316 |
case 'by_designation': |
| 317 |
$( 'tr.selected_employee_field' ).hide(); |
| 318 |
$( 'tr.by_department_field' ).hide(); |
| 319 |
$( 'tr.by_designation_field' ).show(); |
| 320 |
} |
| 321 |
}); |
| 322 |
|
| 323 |
$( 'select#hr_announcement_assign_type' ).trigger( 'change' ) |
| 324 |
}); |
| 325 |
})( jQuery ); |
| 326 |
</script> |
| 327 |
<style> |
| 328 |
#hr_announcement_assign_employee, |
| 329 |
#hr_announcement_assign_department, |
| 330 |
#hr_announcement_assign_designation { |
| 331 |
width: 315px; |
| 332 |
} |
| 333 |
|
| 334 |
tr.selected_employee_field, |
| 335 |
tr.by_department_field, |
| 336 |
tr.by_designation_field { |
| 337 |
display: none; |
| 338 |
} |
| 339 |
|
| 340 |
.erp-hr-announcement-meta-wrap-table .select2-search__field { |
| 341 |
width: 100% !important; |
| 342 |
} |
| 343 |
</style> |
| 344 |
<?php |
| 345 |
} |
| 346 |
|
| 347 |
/** |
| 348 |
* Add custom column label |
| 349 |
* |
| 350 |
* @since 0.1 |
| 351 |
* |
| 352 |
* @param array $columns |
| 353 |
*/ |
| 354 |
function add_type_columns( $columns ) { |
| 355 |
unset( $columns['date'] ); |
| 356 |
|
| 357 |
$columns['assign_type'] = __( 'Sent To', 'erp' ); |
| 358 |
$columns['send_type'] = __( 'Type', 'erp' ); |
| 359 |
$columns['date'] = __( 'Date', 'erp' ); |
| 360 |
|
| 361 |
return $columns; |
| 362 |
} |
| 363 |
|
| 364 |
/** |
| 365 |
* Render custom column content |
| 366 |
* |
| 367 |
* @since 0.1 |
| 368 |
* |
| 369 |
* @param string $column |
| 370 |
* @param integer $post_id |
| 371 |
* |
| 372 |
* @return void |
| 373 |
*/ |
| 374 |
function assign_type_edit_columns( $column, $post_id ) { |
| 375 |
global $post; |
| 376 |
|
| 377 |
if ( $column == 'assign_type' ) { |
| 378 |
$assign_type = get_post_meta( $post_id, '_announcement_type', true ); |
| 379 |
|
| 380 |
if ( $assign_type ) { |
| 381 |
echo $this->assign_type[$assign_type]; |
| 382 |
} else { |
| 383 |
_e( 'No employee assigned!', 'erp' ); |
| 384 |
} |
| 385 |
} |
| 386 |
|
| 387 |
if ( 'send_type' == $column ) { |
| 388 |
echo '<i class="fa fa-envelope-o fa-lg"></i>'; |
| 389 |
|
| 390 |
do_action( 'hr_announcement_send_type', $column, $post_id ); |
| 391 |
} |
| 392 |
} |
| 393 |
|
| 394 |
/** |
| 395 |
* Save Announcement post meta |
| 396 |
* |
| 397 |
* @since 0.1 |
| 398 |
* |
| 399 |
* @param integer $post_id |
| 400 |
* @param object $post |
| 401 |
* |
| 402 |
* @return void |
| 403 |
*/ |
| 404 |
function save_announcement_meta( $post_id, $post ) { |
| 405 |
|
| 406 |
if ( ! isset( $_POST['hr_announcement_meta_action_nonce'] ) ) { |
| 407 |
return $post_id; |
| 408 |
} |
| 409 |
|
| 410 |
if ( ! wp_verify_nonce( $_POST['hr_announcement_meta_action_nonce'], 'hr_announcement_meta_action' ) ) { |
| 411 |
return $post_id; |
| 412 |
} |
| 413 |
|
| 414 |
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { |
| 415 |
return $post_id; |
| 416 |
} |
| 417 |
|
| 418 |
$post_type = get_post_type_object( $post->post_type ); |
| 419 |
|
| 420 |
if ( !current_user_can( $post_type->cap->edit_post, $post_id ) ) { |
| 421 |
return $post_id; |
| 422 |
} |
| 423 |
|
| 424 |
if ( !current_user_can( 'erp_manage_announcement' ) ) { |
| 425 |
return $post_id; |
| 426 |
} |
| 427 |
|
| 428 |
$type = ( isset( $_POST['hr_announcement_assign_type'] ) ) ? $_POST['hr_announcement_assign_type']: ''; |
| 429 |
$employees = ( isset( $_POST['hr_announcement_assign_employee'] ) ) ? $_POST['hr_announcement_assign_employee']: array(); |
| 430 |
$departments = ( isset( $_POST['hr_announcement_assign_department'] ) ) ? $_POST['hr_announcement_assign_department']: array(); |
| 431 |
$designations = ( isset( $_POST['hr_announcement_assign_designation'] ) ) ? $_POST['hr_announcement_assign_designation']: array(); |
| 432 |
|
| 433 |
if ( $type == 'by_department' ) { |
| 434 |
$selected = $departments; |
| 435 |
} elseif ( $type == 'by_designation' ) { |
| 436 |
$selected = $designations; |
| 437 |
} else { |
| 438 |
$selected = $employees; |
| 439 |
} |
| 440 |
|
| 441 |
// Assign / Send announcements to the selected group |
| 442 |
erp_hr_assign_announcements_to_employees( $post_id, $type, $selected ); |
| 443 |
|
| 444 |
do_action( 'hr_annoucement_save', $post_id ); |
| 445 |
} |
| 446 |
|
| 447 |
} |
| 448 |
|