| 1 |
<?php |
| 2 |
namespace WeDevs\ERP\HRM\Emails; |
| 3 |
|
| 4 |
use WeDevs\ERP\Email; |
| 5 |
use WeDevs\ERP\Framework\Traits\Hooker; |
| 6 |
|
| 7 |
/** |
| 8 |
* New Leave Request |
| 9 |
*/ |
| 10 |
class New_Leave_Request extends Email { |
| 11 |
|
| 12 |
use Hooker; |
| 13 |
|
| 14 |
function __construct() { |
| 15 |
$this->id = 'new-leave-request'; |
| 16 |
$this->title = __( 'New Leave Request', 'erp' ); |
| 17 |
$this->description = __( 'New leave request notification to HR Manager.', 'erp' ); |
| 18 |
|
| 19 |
$this->subject = __( 'New leave request received', 'erp'); |
| 20 |
$this->heading = __( 'New Leave Request', 'erp'); |
| 21 |
|
| 22 |
$this->find = [ |
| 23 |
'full-name' => '{employee_name}', |
| 24 |
'employee-url' => '{employee_url}', |
| 25 |
'leave_type' => '{leave_type}', |
| 26 |
'date_from' => '{date_from}', |
| 27 |
'date_to' => '{date_to}', |
| 28 |
'no_days' => '{no_days}', |
| 29 |
'reason' => '{reason}', |
| 30 |
'requests_url' => '{requests_url}', |
| 31 |
]; |
| 32 |
|
| 33 |
$this->action( 'erp_admin_field_' . $this->id . '_help_texts', 'replace_keys' ); |
| 34 |
|
| 35 |
parent::__construct(); |
| 36 |
} |
| 37 |
|
| 38 |
function get_args() { |
| 39 |
return [ |
| 40 |
'email_heading' => $this->heading, |
| 41 |
'email_body' => wpautop( $this->get_option( 'body' ) ), |
| 42 |
]; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Trigger sending email |
| 47 |
* |
| 48 |
* @since 1.0.0 |
| 49 |
* @since 1.2.0 Send single email to multiple recipients. |
| 50 |
* Add `erp_new_leave_request_notification_recipients` filter |
| 51 |
* |
| 52 |
* @param int $request_id |
| 53 |
* |
| 54 |
* @return boolean |
| 55 |
*/ |
| 56 |
public function trigger( $request_id = null ) { |
| 57 |
$request = erp_hr_get_leave_request( $request_id ); |
| 58 |
|
| 59 |
if ( ! $request ) { |
| 60 |
return; |
| 61 |
} |
| 62 |
|
| 63 |
$this->heading = $this->get_option( 'heading', $this->heading ); |
| 64 |
$this->subject = $this->get_option( 'subject', $this->subject ); |
| 65 |
|
| 66 |
$this->replace = [ |
| 67 |
'full-name' => $request->display_name, |
| 68 |
'employee-url' => sprintf( '<a href="%s">%s</a>', admin_url( 'admin.php?page=erp-hr-employee&action=view&id=' . $request->user_id ), $request->display_name ), |
| 69 |
'leave_type' => $request->policy_name, |
| 70 |
'date_from' => erp_format_date( $request->start_date ), |
| 71 |
'date_to' => erp_format_date( $request->end_date ), |
| 72 |
'no_days' => $request->days, |
| 73 |
'reason' => stripslashes($request->reason), |
| 74 |
'requests_url' => sprintf( '<a class="button green" href="%s">%s</a>', admin_url( 'admin.php?page=erp-leave' ), __( 'View Request', 'erp' ) ), |
| 75 |
]; |
| 76 |
|
| 77 |
$subject = $this->get_subject(); |
| 78 |
$content = $this->get_content(); |
| 79 |
$headers = $this->get_headers(); |
| 80 |
$attachments = $this->get_attachments(); |
| 81 |
$recipients = []; |
| 82 |
|
| 83 |
$managers = get_users( [ 'role' => erp_hr_get_manager_role() ] ); |
| 84 |
|
| 85 |
if ( ! $managers ) { |
| 86 |
return; |
| 87 |
} |
| 88 |
|
| 89 |
foreach ( $managers as $hr ) { |
| 90 |
$recipients[] = $hr->user_email; |
| 91 |
} |
| 92 |
|
| 93 |
$recipients = apply_filters( 'erp_new_leave_request_notification_recipients', $recipients, $request ); |
| 94 |
|
| 95 |
return $this->send( $recipients, $subject, $content, $headers, $attachments ); |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* get_content_html function. |
| 100 |
* |
| 101 |
* @access public |
| 102 |
* @return string |
| 103 |
*/ |
| 104 |
function get_content_html() { |
| 105 |
$message = $this->get_template_content( WPERP_INCLUDES . '/email/email-body.php', $this->get_args() ); |
| 106 |
|
| 107 |
return $this->format_string( $message ); |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* get_content_plain function. |
| 112 |
* |
| 113 |
* @access public |
| 114 |
* @return string |
| 115 |
*/ |
| 116 |
function get_content_plain() { |
| 117 |
$message = $this->get_template_content( WPERP_INCLUDES . '/email/email-body.php', $this->get_args() ); |
| 118 |
|
| 119 |
return $message; |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Initialise settings form fields. |
| 124 |
*/ |
| 125 |
public function init_form_fields() { |
| 126 |
$this->form_fields = [ |
| 127 |
[ |
| 128 |
'title' => __( 'Subject', 'erp' ), |
| 129 |
'id' => 'subject', |
| 130 |
'type' => 'text', |
| 131 |
'description' => sprintf( __( 'This controls the email subject line. Leave blank to use the default subject: <code>%s</code>.', 'erp' ), $this->subject ), |
| 132 |
'placeholder' => '', |
| 133 |
'default' => $this->subject, |
| 134 |
'desc_tip' => true |
| 135 |
], |
| 136 |
[ |
| 137 |
'title' => __( 'Email Heading', 'erp' ), |
| 138 |
'id' => 'heading', |
| 139 |
'type' => 'text', |
| 140 |
'description' => sprintf( __( 'This controls the main heading contained within the email notification. Leave blank to use the default heading: <code>%s</code>.', 'erp' ), $this->heading ), |
| 141 |
'placeholder' => '', |
| 142 |
'default' => $this->heading, |
| 143 |
'desc_tip' => true |
| 144 |
], |
| 145 |
[ |
| 146 |
'title' => __( 'Email Body', 'erp' ), |
| 147 |
'type' => 'wysiwyg', |
| 148 |
'id' => 'body', |
| 149 |
'description' => sprintf( __( 'This controls the main heading contained within the email notification. Leave blank to use the default heading: <code>%s</code>.', 'erp' ), $this->heading ), |
| 150 |
'placeholder' => '', |
| 151 |
'default' => '', |
| 152 |
'desc_tip' => true, |
| 153 |
'custom_attributes' => [ |
| 154 |
'rows' => 5, |
| 155 |
'cols' => 45 |
| 156 |
] |
| 157 |
], |
| 158 |
[ |
| 159 |
'type' => $this->id . '_help_texts' |
| 160 |
] |
| 161 |
]; |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Template tags |
| 166 |
* |
| 167 |
* @return void |
| 168 |
*/ |
| 169 |
function replace_keys() { |
| 170 |
?> |
| 171 |
<tr valign="top" class="single_select_page"> |
| 172 |
<th scope="row" class="titledesc"><?php _e( 'Template Tags', 'erp' ); ?></th> |
| 173 |
<td class="forminp"> |
| 174 |
<em><?php _e( 'You may use these template tags inside subject, heading, body and those will be replaced by original values', 'erp' ); ?></em>: |
| 175 |
<?php echo '<code>' . implode( '</code>, <code>', $this->find ) . '</code>'; ?> |
| 176 |
</td> |
| 177 |
</tr> |
| 178 |
<?php |
| 179 |
} |
| 180 |
} |
| 181 |
|