activity_model.php
3 months ago
agent_meta_model.php
3 months ago
agent_model.php
3 months ago
booking_meta_model.php
3 months ago
booking_model.php
14 hours ago
bundle_meta_model.php
3 months ago
bundle_model.php
1 week ago
cart_item_model.php
3 months ago
cart_meta_model.php
3 months ago
cart_model.php
2 weeks ago
connector_model.php
3 months ago
customer_meta_model.php
3 months ago
customer_model.php
1 month ago
invoice_model.php
2 weeks ago
join_bundles_services_model.php
3 months ago
location_category_model.php
3 months ago
location_model.php
3 months ago
meta_model.php
3 months ago
model.php
2 days ago
off_period_model.php
3 months ago
order_intent_meta_model.php
3 months ago
order_intent_model.php
1 week ago
order_item_model.php
3 months ago
order_meta_model.php
3 months ago
order_model.php
1 month ago
otp_model.php
3 months ago
payment_request_model.php
3 months ago
process_job_model.php
3 months ago
process_model.php
1 month ago
recurrence_model.php
3 months ago
service_category_model.php
3 months ago
service_meta_model.php
3 months ago
service_model.php
3 months ago
session_model.php
3 months ago
settings_model.php
3 months ago
step_settings_model.php
3 months ago
transaction_intent_model.php
3 months ago
transaction_model.php
3 months ago
transaction_refund_model.php
3 months ago
work_period_model.php
3 months ago
location_model.php
233 lines
| 1 | <?php |
| 2 | |
| 3 | class OsLocationModel extends OsModel { |
| 4 | public $id, |
| 5 | $name, |
| 6 | $full_address, |
| 7 | $selection_image_id, |
| 8 | $status, |
| 9 | $category_id, |
| 10 | $order_number, |
| 11 | $services_agents_table_name, |
| 12 | $updated_at, |
| 13 | $created_at; |
| 14 | |
| 15 | function __construct( $id = false ) { |
| 16 | parent::__construct(); |
| 17 | $this->table_name = LATEPOINT_TABLE_LOCATIONS; |
| 18 | $this->services_agents_table_name = LATEPOINT_TABLE_AGENTS_SERVICES; |
| 19 | $this->nice_names = array( |
| 20 | 'name' => __( 'Location Name', 'latepoint' ), |
| 21 | ); |
| 22 | |
| 23 | if ( $id ) { |
| 24 | $this->load_by_id( $id ); |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | public function generate_data_vars(): array { |
| 29 | $location_category = empty( $this->category_id ) ? false : new OsLocationCategoryModel( $this->category_id ); |
| 30 | return [ |
| 31 | 'id' => $this->id, |
| 32 | 'name' => $this->name, |
| 33 | 'full_address' => $this->full_address, |
| 34 | 'category' => $location_category ? $location_category->get_data_vars() : [], |
| 35 | ]; |
| 36 | } |
| 37 | |
| 38 | public function filter_allowed_records(): OsModel { |
| 39 | if ( ! OsRolesHelper::are_all_records_allowed( 'location' ) ) { |
| 40 | $this->filter_where_conditions( [ 'id' => OsRolesHelper::get_allowed_records( 'location' ) ] ); |
| 41 | } |
| 42 | return $this; |
| 43 | } |
| 44 | |
| 45 | protected function allowed_params( $role = 'admin' ) { |
| 46 | $allowed_params = array( |
| 47 | 'id', |
| 48 | 'name', |
| 49 | 'full_address', |
| 50 | 'status', |
| 51 | 'category_id', |
| 52 | 'order_number', |
| 53 | 'selection_image_id', |
| 54 | ); |
| 55 | return $allowed_params; |
| 56 | } |
| 57 | |
| 58 | protected function params_to_save( $role = 'admin' ) { |
| 59 | $params_to_save = array( |
| 60 | 'id', |
| 61 | 'name', |
| 62 | 'status', |
| 63 | 'category_id', |
| 64 | 'order_number', |
| 65 | 'full_address', |
| 66 | 'selection_image_id', |
| 67 | ); |
| 68 | return $params_to_save; |
| 69 | } |
| 70 | |
| 71 | |
| 72 | protected function before_create() { |
| 73 | } |
| 74 | |
| 75 | protected function set_defaults() { |
| 76 | if ( empty( $this->category_id ) ) { |
| 77 | $this->category_id = 0; |
| 78 | } |
| 79 | if ( empty( $this->status ) ) { |
| 80 | $this->status = LATEPOINT_LOCATION_STATUS_ACTIVE; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | public function save_custom_schedule( $work_periods ) { |
| 85 | foreach ( $work_periods as &$work_period ) { |
| 86 | $work_period['location_id'] = $this->id; |
| 87 | } |
| 88 | unset( $work_period ); |
| 89 | OsWorkPeriodsHelper::save_work_periods( $work_periods ); |
| 90 | } |
| 91 | |
| 92 | public function get_google_maps_link( $embed = false ) { |
| 93 | $extra = $embed ? '&output=embed' : ''; |
| 94 | return 'https://google.com/maps?q=' . urlencode( $this->full_address ) . $extra; |
| 95 | } |
| 96 | |
| 97 | public function get_google_maps_iframe( $height = '240' ) { |
| 98 | return '<iframe width="100%" height="' . $height . '" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="' . $this->get_google_maps_link( true ) . '"></iframe>'; |
| 99 | } |
| 100 | |
| 101 | public function delete_custom_schedule() { |
| 102 | $work_periods_model = new OsWorkPeriodModel(); |
| 103 | $work_periods = $work_periods_model->where( |
| 104 | array( |
| 105 | 'location_id' => $this->id, |
| 106 | 'agent_id' => 0, |
| 107 | 'service_id' => 0, |
| 108 | 'custom_date' => 'IS NULL', |
| 109 | ) |
| 110 | )->get_results_as_models(); |
| 111 | if ( is_array( $work_periods ) ) { |
| 112 | foreach ( $work_periods as $work_period ) { |
| 113 | $work_period->delete(); |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | public function count_number_of_connected_services( $agent_id = false ) { |
| 119 | if ( $this->is_new_record() ) { |
| 120 | return 0; |
| 121 | } |
| 122 | $args = [ 'location_id' => $this->id ]; |
| 123 | if ( $agent_id ) { |
| 124 | $args['agent_id'] = $agent_id; |
| 125 | } |
| 126 | return OsConnectorHelper::count_connections( $args, 'service_id' ); |
| 127 | } |
| 128 | |
| 129 | public function get_connected_agents() { |
| 130 | $connector = new OsConnectorModel(); |
| 131 | $agent_ids = $connector->select( 'agent_id' )->where( [ 'location_id' => $this->id ] )->group_by( 'agent_id' )->get_results(); |
| 132 | $agents = []; |
| 133 | if ( $agent_ids ) { |
| 134 | foreach ( $agent_ids as $connector_row ) { |
| 135 | $agents[] = new OsAgentModel( $connector_row->agent_id ); |
| 136 | } |
| 137 | } |
| 138 | return $agents; |
| 139 | } |
| 140 | |
| 141 | public function save_agents_and_services( $agents ) { |
| 142 | if ( ! $agents ) { |
| 143 | return true; |
| 144 | } |
| 145 | $connections_to_save = []; |
| 146 | $connections_to_remove = []; |
| 147 | foreach ( $agents as $agent_key => $services ) { |
| 148 | $agent_id = str_replace( 'agent_', '', $agent_key ); |
| 149 | foreach ( $services as $service_key => $service ) { |
| 150 | $service_id = str_replace( 'service_', '', $service_key ); |
| 151 | $connection = [ |
| 152 | 'location_id' => $this->id, |
| 153 | 'agent_id' => $agent_id, |
| 154 | 'service_id' => $service_id, |
| 155 | ]; |
| 156 | if ( $service['connected'] == 'yes' ) { |
| 157 | $connections_to_save[] = $connection; |
| 158 | } else { |
| 159 | $connections_to_remove[] = $connection; |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | if ( ! empty( $connections_to_save ) ) { |
| 164 | foreach ( $connections_to_save as $connection_to_save ) { |
| 165 | OsConnectorHelper::save_connection( $connection_to_save ); |
| 166 | } |
| 167 | } |
| 168 | if ( ! empty( $connections_to_remove ) ) { |
| 169 | foreach ( $connections_to_remove as $connection_to_remove ) { |
| 170 | OsConnectorHelper::remove_connection( $connection_to_remove ); |
| 171 | } |
| 172 | } |
| 173 | return true; |
| 174 | } |
| 175 | |
| 176 | public function delete( $id = false ) { |
| 177 | if ( ! $id && isset( $this->id ) ) { |
| 178 | $id = $this->id; |
| 179 | } |
| 180 | if ( $id && $this->db->delete( $this->table_name, array( 'id' => $id ), array( '%d' ) ) ) { |
| 181 | $this->db->delete( LATEPOINT_TABLE_AGENTS_SERVICES, array( 'location_id' => $id ), array( '%d' ) ); |
| 182 | $this->db->delete( LATEPOINT_TABLE_BOOKINGS, array( 'location_id' => $id ), array( '%d' ) ); |
| 183 | $this->db->delete( LATEPOINT_TABLE_WORK_PERIODS, array( 'location_id' => $id ), array( '%d' ) ); |
| 184 | return true; |
| 185 | } else { |
| 186 | return false; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | public function should_be_active() { |
| 191 | return $this->where( [ 'status' => LATEPOINT_LOCATION_STATUS_ACTIVE ] ); |
| 192 | } |
| 193 | |
| 194 | public function is_active() { |
| 195 | return ( $this->status == LATEPOINT_LOCATION_STATUS_ACTIVE ); |
| 196 | } |
| 197 | |
| 198 | |
| 199 | public function get_selection_image_url() { |
| 200 | $default_location_image_url = LATEPOINT_IMAGES_URL . 'location-image.png'; |
| 201 | return OsImageHelper::get_image_url_by_id( $this->selection_image_id, 'thumbnail', $default_location_image_url ); |
| 202 | } |
| 203 | |
| 204 | public function has_agent( $agent_id ) { |
| 205 | return OsConnectorHelper::has_connection( |
| 206 | [ |
| 207 | 'location_id' => $this->id, |
| 208 | 'agent_id' => $agent_id, |
| 209 | ] |
| 210 | ); |
| 211 | } |
| 212 | |
| 213 | public function has_agent_and_service( $agent_id, $service_id ) { |
| 214 | if ( $this->is_new_record() ) { |
| 215 | return false; |
| 216 | } |
| 217 | return OsConnectorHelper::has_connection( |
| 218 | [ |
| 219 | 'location_id' => $this->id, |
| 220 | 'agent_id' => $agent_id, |
| 221 | 'service_id' => $service_id, |
| 222 | ] |
| 223 | ); |
| 224 | } |
| 225 | |
| 226 | protected function properties_to_validate() { |
| 227 | $validations = array( |
| 228 | 'name' => array( 'presence', 'uniqueness' ), |
| 229 | ); |
| 230 | return $validations; |
| 231 | } |
| 232 | } |
| 233 |