PluginProbe ʕ •ᴥ•ʔ
LatePoint – Calendar Booking Plugin for Appointments and Events / 5.1.3
LatePoint – Calendar Booking Plugin for Appointments and Events v5.1.3
5.6.6 5.6.5 5.6.4 5.6.3 5.6.2 5.6.1 5.6.0 5.5.2 5.5.1 5.5.0 5.4.2 trunk 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.1.8 5.1.9 5.1.91 5.1.92 5.1.93 5.1.94 5.2.0 5.2.1 5.2.10 5.2.11 5.2.2 5.2.3 5.2.4 5.2.5 5.2.6 5.2.7 5.2.8 5.2.9 5.3.0 5.3.1 5.3.2 5.4.0 5.4.1
latepoint / lib / models / location_model.php
latepoint / lib / models Last commit date
activity_model.php 1 year ago agent_meta_model.php 1 year ago agent_model.php 1 year ago booking_meta_model.php 1 year ago booking_model.php 1 year ago bundle_model.php 1 year ago cart_item_model.php 1 year ago cart_meta_model.php 1 year ago cart_model.php 1 year ago connector_model.php 1 year ago customer_meta_model.php 1 year ago customer_model.php 1 year ago invoice_model.php 1 year ago join_bundles_services_model.php 1 year ago location_category_model.php 1 year ago location_model.php 1 year ago meta_model.php 1 year ago model.php 1 year ago order_intent_meta_model.php 1 year ago order_intent_model.php 1 year ago order_item_model.php 1 year ago order_meta_model.php 1 year ago order_model.php 1 year ago payment_request_model.php 1 year ago process_job_model.php 1 year ago process_model.php 1 year ago service_category_model.php 1 year ago service_meta_model.php 1 year ago service_model.php 1 year ago session_model.php 1 year ago settings_model.php 1 year ago step_settings_model.php 1 year ago transaction_intent_model.php 1 year ago transaction_model.php 1 year ago transaction_refund_model.php 1 year ago work_period_model.php 1 year ago
location_model.php
194 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 if($id){
23 $this->load_by_id($id);
24 }
25 }
26
27 public function generate_data_vars(): array {
28 $location_category = empty($this->category_id) ? false : new OsLocationCategoryModel($this->category_id);
29 return [
30 'id' => $this->id,
31 'name' => $this->name,
32 'full_address' => $this->full_address,
33 'category' => $location_category ? $location_category->get_data_vars() : [],
34 ];
35 }
36
37 public function filter_allowed_records(): OsModel{
38 if(!OsRolesHelper::are_all_records_allowed('location')){
39 $this->filter_where_conditions(['id' => OsRolesHelper::get_allowed_records('location')]);
40 }
41 return $this;
42 }
43
44 protected function allowed_params($role = 'admin'){
45 $allowed_params = array('id',
46 'name',
47 'full_address',
48 'status',
49 'category_id',
50 'order_number',
51 'selection_image_id',
52 );
53 return $allowed_params;
54 }
55
56 protected function params_to_save($role = 'admin'){
57 $params_to_save = array('id',
58 'name',
59 'status',
60 'category_id',
61 'order_number',
62 'full_address',
63 'selection_image_id');
64 return $params_to_save;
65 }
66
67
68 protected function before_create(){
69 }
70
71 protected function set_defaults(){
72 if(empty($this->category_id)) $this->category_id = 0;
73 if(empty($this->status)) $this->status = LATEPOINT_LOCATION_STATUS_ACTIVE;
74 }
75
76 public function save_custom_schedule($work_periods){
77 foreach($work_periods as &$work_period){
78 $work_period['location_id'] = $this->id;
79 }
80 unset($work_period);
81 OsWorkPeriodsHelper::save_work_periods($work_periods);
82 }
83
84 public function get_google_maps_link($embed = false){
85 $extra = $embed ? '&output=embed' : '';
86 return 'https://google.com/maps?q='.urlencode($this->full_address).$extra;
87 }
88
89 public function get_google_maps_iframe($height = '240'){
90 return '<iframe width="100%" height="'.$height.'" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="'.$this->get_google_maps_link(true).'"></iframe>';
91 }
92
93 public function delete_custom_schedule(){
94 $work_periods_model = new OsWorkPeriodModel();
95 $work_periods = $work_periods_model->where(array('location_id' => $this->id, 'agent_id' => 0, 'service_id' => 0, 'custom_date' => 'IS NULL'))->get_results_as_models();
96 if(is_array($work_periods)){
97 foreach($work_periods as $work_period){
98 $work_period->delete();
99 }
100 }
101 }
102
103 public function count_number_of_connected_services($agent_id = false){
104 if($this->is_new_record()) return 0;
105 $args = ['location_id' => $this->id];
106 if($agent_id) $args['agent_id'] = $agent_id;
107 return OsConnectorHelper::count_connections($args, 'service_id');
108 }
109
110 public function get_connected_agents(){
111 $connector = new OsConnectorModel();
112 $agent_ids = $connector->select('agent_id')->where(['location_id' => $this->id])->group_by('agent_id')->get_results();
113 $agents = [];
114 if($agent_ids){
115 foreach($agent_ids as $connector_row){
116 $agents[] = new OsAgentModel($connector_row->agent_id);
117 }
118 }
119 return $agents;
120 }
121
122 public function save_agents_and_services($agents){
123 if(!$agents) return true;
124 $connections_to_save = [];
125 $connections_to_remove = [];
126 foreach($agents as $agent_key => $services){
127 $agent_id = str_replace('agent_', '', $agent_key);
128 foreach($services as $service_key => $service){
129 $service_id = str_replace('service_', '', $service_key);
130 $connection = ['location_id' => $this->id, 'agent_id' => $agent_id, 'service_id' => $service_id];
131 if($service['connected'] == 'yes'){
132 $connections_to_save[] = $connection;
133 }else{
134 $connections_to_remove[] = $connection;
135 }
136 }
137 }
138 if(!empty($connections_to_save)){
139 foreach($connections_to_save as $connection_to_save){
140 OsConnectorHelper::save_connection($connection_to_save);
141 }
142 }
143 if(!empty($connections_to_remove)){
144 foreach($connections_to_remove as $connection_to_remove){
145 OsConnectorHelper::remove_connection($connection_to_remove);
146 }
147 }
148 return true;
149 }
150
151 public function delete($id = false){
152 if(!$id && isset($this->id)){
153 $id = $this->id;
154 }
155 if($id && $this->db->delete( $this->table_name, array('id' => $id), array( '%d' ))){
156 $this->db->delete(LATEPOINT_TABLE_AGENTS_SERVICES, array('location_id' => $id), array( '%d' ) );
157 $this->db->delete(LATEPOINT_TABLE_BOOKINGS, array('location_id' => $id), array( '%d' ) );
158 $this->db->delete(LATEPOINT_TABLE_WORK_PERIODS, array('location_id' => $id), array( '%d' ) );
159 return true;
160 }else{
161 return false;
162 }
163 }
164
165 public function should_be_active(){
166 return $this->where(['status' => LATEPOINT_LOCATION_STATUS_ACTIVE]);
167 }
168
169 public function is_active(){
170 return ($this->status == LATEPOINT_LOCATION_STATUS_ACTIVE);
171 }
172
173
174 public function get_selection_image_url(){
175 $default_location_image_url = LATEPOINT_IMAGES_URL . 'location-image.png';
176 return OsImageHelper::get_image_url_by_id($this->selection_image_id, 'thumbnail', $default_location_image_url);
177 }
178
179 public function has_agent($agent_id){
180 return OsConnectorHelper::has_connection(['location_id' => $this->id, 'agent_id' => $agent_id]);
181 }
182
183 public function has_agent_and_service($agent_id, $service_id){
184 if($this->is_new_record()) return false;
185 return OsConnectorHelper::has_connection(['location_id' => $this->id, 'agent_id' => $agent_id, 'service_id' => $service_id]);
186 }
187
188 protected function properties_to_validate(){
189 $validations = array(
190 'name' => array('presence', 'uniqueness'),
191 );
192 return $validations;
193 }
194 }