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
3 months ago
bundle_meta_model.php
3 months ago
bundle_model.php
3 months ago
cart_item_model.php
3 months ago
cart_meta_model.php
3 months ago
cart_model.php
3 months ago
connector_model.php
3 months ago
customer_meta_model.php
3 months ago
customer_model.php
1 month ago
invoice_model.php
3 months 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
3 months ago
off_period_model.php
3 months ago
order_intent_meta_model.php
3 months ago
order_intent_model.php
3 months ago
order_item_model.php
3 months ago
order_meta_model.php
3 months ago
order_model.php
3 months ago
otp_model.php
3 months ago
payment_request_model.php
3 months ago
process_job_model.php
3 months ago
process_model.php
3 months 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_category_model.php
153 lines
| 1 | <?php |
| 2 | |
| 3 | class OsLocationCategoryModel extends OsModel { |
| 4 | public $id, |
| 5 | $name, |
| 6 | $parent_id, |
| 7 | $selection_image_id, |
| 8 | $order_number, |
| 9 | $short_description; |
| 10 | |
| 11 | function __construct( $id = false ) { |
| 12 | parent::__construct(); |
| 13 | $this->table_name = LATEPOINT_TABLE_LOCATION_CATEGORIES; |
| 14 | $this->nice_names = array( |
| 15 | 'name' => __( 'Location Category Name', 'latepoint' ), |
| 16 | 'short_description' => __( 'Location Category Short Description', 'latepoint' ), |
| 17 | 'selection_image_id' => __( 'Location Category Selection Image', 'latepoint' ), |
| 18 | ); |
| 19 | |
| 20 | if ( $id ) { |
| 21 | $this->load_by_id( $id ); |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | public function generate_data_vars(): array { |
| 26 | return [ |
| 27 | 'id' => $this->id, |
| 28 | 'name' => $this->name, |
| 29 | ]; |
| 30 | } |
| 31 | |
| 32 | public function count_locations() { |
| 33 | $location = new OsLocationModel(); |
| 34 | $total_location = $location->filter_allowed_records()->where( [ 'category_id' => $this->id ] )->should_be_active()->count(); |
| 35 | $child_categories = new OsLocationCategoryModel(); |
| 36 | $child_categories = $child_categories->where( [ 'parent_id' => $this->id ] )->get_results_as_models(); |
| 37 | if ( $child_categories ) { |
| 38 | foreach ( $child_categories as $child_category ) { |
| 39 | $total_location = $total_location + $child_category->count_locations(); |
| 40 | } |
| 41 | } |
| 42 | return $total_location; |
| 43 | } |
| 44 | |
| 45 | public function delete( $id = false ) { |
| 46 | if ( ! $id && isset( $this->id ) ) { |
| 47 | $id = $this->id; |
| 48 | } |
| 49 | if ( $id && $this->db->delete( $this->table_name, array( 'id' => $id ), array( '%d' ) ) ) { |
| 50 | $this->db->update( LATEPOINT_TABLE_SERVICES, array( 'category_id' => 0 ), array( 'category_id' => $id ), array( '%d' ) ); |
| 51 | $this->db->update( $this->table_name, array( 'parent_id' => null ), array( 'parent_id' => $id ), array( '%d' ) ); |
| 52 | return true; |
| 53 | } else { |
| 54 | return false; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | public function get_selection_image_url() { |
| 59 | $default_location_image_url = LATEPOINT_IMAGES_URL . 'location-image.png'; |
| 60 | return OsImageHelper::get_image_url_by_id( $this->selection_image_id, 'thumbnail', $default_location_image_url ); |
| 61 | } |
| 62 | |
| 63 | protected function properties_to_validate() { |
| 64 | $validations = array( |
| 65 | 'name' => array( 'presence', 'uniqueness' ), |
| 66 | ); |
| 67 | return $validations; |
| 68 | } |
| 69 | |
| 70 | public function index_for_select() { |
| 71 | $categories = $this->db->get_results( 'SELECT id, name FROM ' . $this->table_name ); |
| 72 | $categories_for_select = array(); |
| 73 | |
| 74 | $categories_for_select[] = array( |
| 75 | 'value' => 0, |
| 76 | 'label' => __( 'Not categorized', 'latepoint' ), |
| 77 | ); |
| 78 | foreach ( $categories as $category ) { |
| 79 | $categories_for_select[] = array( |
| 80 | 'value' => $category->id, |
| 81 | 'label' => $category->name, |
| 82 | ); |
| 83 | } |
| 84 | |
| 85 | return $categories_for_select; |
| 86 | } |
| 87 | |
| 88 | protected function allowed_params( $role = 'admin' ) { |
| 89 | $allowed_params = array( |
| 90 | 'id', |
| 91 | 'name', |
| 92 | 'short_description', |
| 93 | 'selection_image_id', |
| 94 | 'parent_id', |
| 95 | 'order_number', |
| 96 | ); |
| 97 | return $allowed_params; |
| 98 | } |
| 99 | |
| 100 | |
| 101 | protected function params_to_save( $role = 'admin' ) { |
| 102 | $params_to_save = array( |
| 103 | 'id', |
| 104 | 'name', |
| 105 | 'short_description', |
| 106 | 'selection_image_id', |
| 107 | 'parent_id', |
| 108 | 'order_number', |
| 109 | ); |
| 110 | return $params_to_save; |
| 111 | } |
| 112 | |
| 113 | public function get_active_locations( $filter_allowed_records = false ) { |
| 114 | if ( ! isset( $this->active_locations ) ) { |
| 115 | $location = new OsLocationModel(); |
| 116 | if ( $filter_allowed_records ) { |
| 117 | $location->filter_allowed_records(); |
| 118 | } |
| 119 | $location->should_be_active()->where( array( 'category_id' => $this->id ) )->order_by( 'order_number asc' ); |
| 120 | $this->active_locations = $location->get_results_as_models(); |
| 121 | } |
| 122 | return $this->active_locations; |
| 123 | } |
| 124 | |
| 125 | public function get_disabled_locations( $filter_allowed_records = false ) { |
| 126 | if ( ! isset( $this->disabled_locations ) ) { |
| 127 | $location = new OsLocationModel(); |
| 128 | if ( $filter_allowed_records ) { |
| 129 | $location->filter_allowed_records(); |
| 130 | } |
| 131 | $location->where( |
| 132 | array( |
| 133 | 'category_id' => $this->id, |
| 134 | 'status' => LATEPOINT_LOCATION_STATUS_DISABLED, |
| 135 | ) |
| 136 | )->order_by( 'order_number asc' ); |
| 137 | $this->disabled_locations = $location->get_results_as_models(); |
| 138 | } |
| 139 | return $this->disabled_locations; |
| 140 | } |
| 141 | |
| 142 | protected function get_locations( $filter_allowed_records = false ) { |
| 143 | if ( ! isset( $this->locations ) ) { |
| 144 | $location = new OsLocationModel(); |
| 145 | if ( $filter_allowed_records ) { |
| 146 | $location->filter_allowed_records(); |
| 147 | } |
| 148 | $this->locations = $location->where( array( 'category_id' => $this->id ) )->order_by( 'order_number asc' )->get_results_as_models(); |
| 149 | } |
| 150 | return $this->locations; |
| 151 | } |
| 152 | } |
| 153 |