| 1 |
<?php |
| 2 |
|
| 3 |
// If this file is called directly, abort. |
| 4 |
if ( ! defined( 'WPINC' ) ) { |
| 5 |
die; |
| 6 |
} |
| 7 |
|
| 8 |
class Field_m extends Winter_MVC_Model { |
| 9 |
|
| 10 |
public $_table_name = 'wdk_fields'; |
| 11 |
public $_order_by = 'order_index,idfield'; |
| 12 |
public $_primary_key = 'idfield'; |
| 13 |
public $_own_columns = array(); |
| 14 |
public $_timestamps = TRUE; |
| 15 |
protected $_primary_filter = 'intval'; |
| 16 |
public $form_admin = array(); |
| 17 |
public $fields_list = NULL; |
| 18 |
|
| 19 |
public $fields_validations = array(); |
| 20 |
public $fields_autosuggestions = array(); |
| 21 |
|
| 22 |
public function __construct(){ |
| 23 |
$this->fields_validations = array( |
| 24 |
'is_numerical'=> __('Numerical', 'wpdirectorykit'), |
| 25 |
'is_phone'=> __('Phone', 'wpdirectorykit'), |
| 26 |
'is_phone|wdk_viber'=> __('Phone(viber)', 'wpdirectorykit'), |
| 27 |
'is_phone|wdk_whatsapp'=> __('Phone(whatsApp)', 'wpdirectorykit'), |
| 28 |
'is_email'=> __('Email', 'wpdirectorykit'), |
| 29 |
); |
| 30 |
|
| 31 |
$this->fields_autosuggestions = array( |
| 32 |
'google_api_cities'=> __('Google Api Cities', 'wpdirectorykit'), |
| 33 |
'google_api_countries'=> __('Google Api Countries', 'wpdirectorykit'), |
| 34 |
'rapid_api_cities'=> __('Rapid Api Cities', 'wpdirectorykit'), |
| 35 |
'rapid_api_countries'=> __('Rapid Api Countries', 'wpdirectorykit'), |
| 36 |
'countries'=> __('Countries', 'wpdirectorykit'), |
| 37 |
'db_locations'=> __('Db Locations', 'wpdirectorykit'), |
| 38 |
'db_categories'=> __('Db Categories', 'wpdirectorykit'), |
| 39 |
'db_selft_field'=> __('Current Field Values', 'wpdirectorykit'), |
| 40 |
); |
| 41 |
|
| 42 |
parent::__construct(); |
| 43 |
} |
| 44 |
|
| 45 |
/* [START] For dynamic data table */ |
| 46 |
|
| 47 |
public function get_available_fields() |
| 48 |
{ |
| 49 |
$fields = $this->db->list_fields($this->_table_name); |
| 50 |
|
| 51 |
return $fields; |
| 52 |
} |
| 53 |
|
| 54 |
public function total_lang($where = array()) |
| 55 |
{ |
| 56 |
$this->db->select('COUNT(*) as total_count'); |
| 57 |
$this->db->from($this->_table_name); |
| 58 |
$this->db->where($where); |
| 59 |
$this->db->order_by($this->_order_by); |
| 60 |
$query = $this->db->get(); |
| 61 |
$res = $this->db->results(); |
| 62 |
|
| 63 |
if(isset($res[0]->total_count)) |
| 64 |
return $res[0]->total_count; |
| 65 |
|
| 66 |
return 0; |
| 67 |
} |
| 68 |
|
| 69 |
public function get_pagination_lang($limit, $offset, $where = array()) |
| 70 |
{ |
| 71 |
$this->db->select('*'); |
| 72 |
$this->db->from($this->_table_name); |
| 73 |
$this->db->where($where); |
| 74 |
$this->db->limit($limit); |
| 75 |
$this->db->offset($offset); |
| 76 |
$this->db->order_by($this->_order_by); |
| 77 |
$query = $this->db->get(); |
| 78 |
|
| 79 |
if ($this->db->num_rows() > 0) |
| 80 |
return $this->db->results(); |
| 81 |
|
| 82 |
return array(); |
| 83 |
} |
| 84 |
|
| 85 |
public function check_deletable($id) |
| 86 |
{ |
| 87 |
if(wmvc_user_in_role('administrator') || current_user_can('wdk_listings_manage')) return true; |
| 88 |
|
| 89 |
return false; |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
/* [END] For dynamic data table */ |
| 94 |
|
| 95 |
public function get_fields_data($field_id = NULL) { |
| 96 |
static $fields_data = array(); |
| 97 |
if(empty($fields_data)){ |
| 98 |
$query = $this->get(); |
| 99 |
if ($this->db->num_rows() > 0) { |
| 100 |
foreach($this->db->results() as $field) { |
| 101 |
$fields_data[$field->idfield] = $field; |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
if(!empty($field_id)) |
| 107 |
if(isset($fields_data[(int)$field_id])){ |
| 108 |
return $fields_data[(int)$field_id]; |
| 109 |
} else { |
| 110 |
return NULL; |
| 111 |
} |
| 112 |
|
| 113 |
return $fields_data; |
| 114 |
|
| 115 |
} |
| 116 |
|
| 117 |
public function get_sections() { |
| 118 |
static $sections = array(); |
| 119 |
/* hard section, for uncategory */ |
| 120 |
|
| 121 |
foreach ($this->field_m->get() as $field) { |
| 122 |
if(wmvc_show_data('field_type',$field) == 'SECTION'){ |
| 123 |
$sections[wmvc_show_data('idfield',$field)] = wmvc_show_data('field_label',$field); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
return $sections; |
| 128 |
} |
| 129 |
|
| 130 |
public function get_fields_section() { |
| 131 |
static $fields_section = array(); |
| 132 |
|
| 133 |
$section_id = '0'; |
| 134 |
/* hard section, for uncategory */ |
| 135 |
$this->data['fields_sections'][$section_id] = array( |
| 136 |
"idfield" => $section_id, |
| 137 |
"field_type" => "SECTION", |
| 138 |
"is_locked" =>0, |
| 139 |
"is_table_visible" =>0, |
| 140 |
"is_visible_frontend" => 1, |
| 141 |
"is_visible_dashboard" => 1, |
| 142 |
"is_hardlocked" => 0, |
| 143 |
"is_required" => 0, |
| 144 |
"is_price_format" => 0, |
| 145 |
"columns_number" => "12", |
| 146 |
"field_label" => "Unsection", |
| 147 |
"prefix" => '', |
| 148 |
"suffix" => '', |
| 149 |
"values_list" => '', |
| 150 |
"placeholder" => '', |
| 151 |
"hint" =>'' |
| 152 |
); |
| 153 |
|
| 154 |
foreach ($this->field_m->get() as $field) { |
| 155 |
if(wmvc_show_data('field_type',$field) == 'SECTION'){ |
| 156 |
$section_id = wmvc_show_data('idfield', $field); |
| 157 |
$fields_section[$section_id] = array( |
| 158 |
"idfield" => $section_id, |
| 159 |
"field_type" => wmvc_show_data('field_type',$field), |
| 160 |
"is_locked" =>wmvc_show_data('is_locked',$field), |
| 161 |
"is_table_visible" =>wmvc_show_data('is_table_visible',$field), |
| 162 |
"is_visible_frontend" => wmvc_show_data('is_visible_frontend',$field), |
| 163 |
"is_visible_dashboard" => wmvc_show_data('is_visible_dashboard',$field), |
| 164 |
"is_hardlocked" => wmvc_show_data('is_hardlocked',$field), |
| 165 |
"is_required" => wmvc_show_data('is_required',$field), |
| 166 |
"is_price_format" => wmvc_show_data('is_required',$field), |
| 167 |
"columns_number" => wmvc_show_data('columns_number',$field), |
| 168 |
"field_label" => wmvc_show_data('field_label',$field), |
| 169 |
"prefix" => wmvc_show_data('prefix',$field), |
| 170 |
"suffix" => wmvc_show_data('suffix',$field), |
| 171 |
"values_list" => wmvc_show_data('values_list',$field), |
| 172 |
"placeholder" => wmvc_show_data('placeholder',$field), |
| 173 |
"hint" =>wmvc_show_data('hint',$field), |
| 174 |
"fields" => array() |
| 175 |
); |
| 176 |
} else { |
| 177 |
$fields_section[$section_id]["fields"][] = $field; |
| 178 |
} |
| 179 |
} |
| 180 |
return $fields_section; |
| 181 |
} |
| 182 |
|
| 183 |
/* only admin can edit */ |
| 184 |
public function is_related($item_id, $user_id, $method = 'edit') |
| 185 |
{ |
| 186 |
return false; |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
public function delete($field_id, $user_id=NULL) { |
| 192 |
|
| 193 |
if(!$this->check_deletable($field_id, $user_id)) return false; |
| 194 |
|
| 195 |
$this->load->model('listingfield_m'); |
| 196 |
|
| 197 |
/* remove from listing fields */ |
| 198 |
$field_data = $this->get($field_id, TRUE); |
| 199 |
|
| 200 |
if($field_data) |
| 201 |
$this->listingfield_m->delete_table_column($field_data, $field_id); |
| 202 |
|
| 203 |
|
| 204 |
parent::delete($field_id); |
| 205 |
|
| 206 |
do_action('wpdirectorykit/model/field/delete', $field_id); |
| 207 |
|
| 208 |
return true; |
| 209 |
} |
| 210 |
} |
| 211 |
?> |