| 1 |
<?php |
| 2 |
|
| 3 |
class OsActivityModel extends OsModel { |
| 4 |
public $id, |
| 5 |
$agent_id, |
| 6 |
$order_id, |
| 7 |
$order_item_id, |
| 8 |
$booking_id, |
| 9 |
$service_id, |
| 10 |
$customer_id, |
| 11 |
$coupon_id, |
| 12 |
$code, |
| 13 |
$description, |
| 14 |
$initiated_by, |
| 15 |
$initiated_by_id, |
| 16 |
$updated_at, |
| 17 |
$created_at; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
protected $codes; |
| 24 |
|
| 25 |
function __construct( $id = false ) { |
| 26 |
parent::__construct(); |
| 27 |
$this->table_name = LATEPOINT_TABLE_ACTIVITIES; |
| 28 |
$this->nice_names = array(); |
| 29 |
|
| 30 |
$this->codes = $this->get_codes(); |
| 31 |
|
| 32 |
if ( $id ) { |
| 33 |
$this->load_by_id( $id ); |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
protected function get_codes() { |
| 38 |
return OsActivitiesHelper::get_codes(); |
| 39 |
} |
| 40 |
|
| 41 |
public function get_link_to_object( $label = false ) { |
| 42 |
$label = ( $label ) ? $label : esc_html__( 'View', 'latepoint' ); |
| 43 |
$href = '#'; |
| 44 |
$attrs = ''; |
| 45 |
switch ( $this->code ) { |
| 46 |
case 'agent_updated': |
| 47 |
case 'agent_created': |
| 48 |
$href = OsRouterHelper::build_link( OsRouterHelper::build_route_name( 'agents', 'edit_form' ), array( 'id' => $this->agent_id ) ); |
| 49 |
break; |
| 50 |
case 'service_updated': |
| 51 |
case 'service_created': |
| 52 |
$href = OsRouterHelper::build_link( OsRouterHelper::build_route_name( 'services', 'edit_form' ), array( 'id' => $this->service_id ) ); |
| 53 |
break; |
| 54 |
default: |
| 55 |
$attrs = 'data-os-params="' . esc_attr( http_build_query( [ 'id' => $this->id ] ) ) . '" |
| 56 |
data-os-action="' . esc_attr( OsRouterHelper::build_route_name( 'activities', 'view' ) ) . '" |
| 57 |
data-os-lightbox-classes="width-800" |
| 58 |
data-os-after-call="latepoint_init_json_view" |
| 59 |
data-os-output-target="side-panel"'; |
| 60 |
break; |
| 61 |
} |
| 62 |
$link = '<a class="view-activity-link" href="' . esc_url( $href ) . '" ' . $attrs . '>' . $label . '</a>'; |
| 63 |
$link = apply_filters( 'latepoint_activity_link_to_object', $link, $this, $label ); |
| 64 |
return $link; |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
public function get_user_link_with_avatar() { |
| 69 |
return $this->get_user_link( true ); |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
public function get_user_link( $show_avatar = false ) { |
| 74 |
$link = '#'; |
| 75 |
$name = 'n/a'; |
| 76 |
$attrs = ''; |
| 77 |
$avatar_url = LATEPOINT_DEFAULT_AVATAR_URL; |
| 78 |
|
| 79 |
switch ( $this->initiated_by ) { |
| 80 |
case 'wp_user': |
| 81 |
case LATEPOINT_USER_TYPE_ADMIN: |
| 82 |
case LATEPOINT_USER_TYPE_CUSTOM: |
| 83 |
$link = get_edit_user_link( $this->initiated_by_id ); |
| 84 |
$userdata = get_userdata( $this->initiated_by_id ); |
| 85 |
$name = $userdata->display_name; |
| 86 |
$avatar_url = get_avatar_url( $this->initiated_by_id, array( 'size' => 200 ) ); |
| 87 |
break; |
| 88 |
case LATEPOINT_USER_TYPE_AGENT: |
| 89 |
$agent = new OsAgentModel( $this->initiated_by_id ); |
| 90 |
$link = OsRouterHelper::build_link( OsRouterHelper::build_route_name( 'agents', 'edit_form' ), array( 'id' => $this->initiated_by_id ) ); |
| 91 |
$name = $agent->full_name; |
| 92 |
$avatar_url = $agent->get_avatar_url(); |
| 93 |
break; |
| 94 |
case LATEPOINT_USER_TYPE_CUSTOMER: |
| 95 |
$customer = new OsCustomerModel( $this->initiated_by_id ); |
| 96 |
$attrs = OsCustomerHelper::quick_customer_btn_html( $this->initiated_by_id ); |
| 97 |
$name = $customer->full_name; |
| 98 |
$avatar_url = $customer->get_avatar_url(); |
| 99 |
break; |
| 100 |
default: |
| 101 |
return esc_html( $this->initiated_by ?? 'n/a' ); |
| 102 |
} |
| 103 |
$avatar_url = esc_url( $avatar_url ); |
| 104 |
$name = esc_html( $name ); |
| 105 |
$link = esc_url( $link ); |
| 106 |
$avatar = $show_avatar ? "<span class='ula-avatar' style='background-image: url({$avatar_url})'></span>" : ''; |
| 107 |
|
| 108 |
return "<a class='user-link-with-avatar' target='_blank' href='{$link}' {$attrs}>{$avatar}<span class='ula-name'>{$name}</span><span class='latepoint-icon latepoint-icon-external-link'></span></a>"; |
| 109 |
} |
| 110 |
|
| 111 |
public function get_description() { |
| 112 |
if ( $this->code == 'sms_sent' ) { |
| 113 |
$this->description = json_decode( $this->description, true ); |
| 114 |
} |
| 115 |
|
| 116 |
return $this->description; |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
protected function get_nice_created_at( $include_time = true ) { |
| 121 |
$format = $include_time ? OsSettingsHelper::get_readable_date_format() . ' ' . OsSettingsHelper::get_readable_time_format() : OsSettingsHelper::get_readable_date_format(); |
| 122 |
$utc_date = date_create_from_format( LATEPOINT_DATETIME_DB_FORMAT, $this->created_at ); |
| 123 |
$wp_timezone_date = $utc_date->setTimezone( OsTimeHelper::get_wp_timezone() ); |
| 124 |
|
| 125 |
return date_format( $wp_timezone_date, $format ); |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
protected function get_name() { |
| 130 |
if ( $this->code && isset( $this->codes[ $this->code ] ) ) { |
| 131 |
return $this->codes[ $this->code ]; |
| 132 |
} else { |
| 133 |
return $this->code; |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
protected function params_to_save( $role = 'admin' ) { |
| 138 |
$params_to_save = array( |
| 139 |
'id', |
| 140 |
'agent_id', |
| 141 |
'order_id', |
| 142 |
'order_item_id', |
| 143 |
'booking_id', |
| 144 |
'service_id', |
| 145 |
'customer_id', |
| 146 |
'coupon_id', |
| 147 |
'code', |
| 148 |
'description', |
| 149 |
'initiated_by', |
| 150 |
'initiated_by_id', |
| 151 |
); |
| 152 |
return $params_to_save; |
| 153 |
} |
| 154 |
|
| 155 |
protected function allowed_params( $role = 'admin' ) { |
| 156 |
$allowed_params = array( |
| 157 |
'id', |
| 158 |
'agent_id', |
| 159 |
'booking_id', |
| 160 |
'order_id', |
| 161 |
'order_item_id', |
| 162 |
'service_id', |
| 163 |
'customer_id', |
| 164 |
'coupon_id', |
| 165 |
'code', |
| 166 |
'description', |
| 167 |
'initiated_by', |
| 168 |
'initiated_by_id', |
| 169 |
); |
| 170 |
return $allowed_params; |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
protected function properties_to_validate() { |
| 175 |
$validations = array( |
| 176 |
'code' => array( 'presence' ), |
| 177 |
); |
| 178 |
return $validations; |
| 179 |
} |
| 180 |
} |
| 181 |
|