| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly; |
| 3 |
|
| 4 |
class Wdk_searchform extends Winter_MVC_Controller { |
| 5 |
|
| 6 |
public function __construct(){ |
| 7 |
parent::__construct(); |
| 8 |
} |
| 9 |
|
| 10 |
public function index() |
| 11 |
{ |
| 12 |
$this->load->model('searchform_m'); |
| 13 |
$this->load->model('field_m'); |
| 14 |
|
| 15 |
$id = 1; |
| 16 |
wdk_access_check('searchform_m', $id); |
| 17 |
$this->data['db_data'] = NULL; |
| 18 |
|
| 19 |
$this->data['form'] = &$this->form; |
| 20 |
|
| 21 |
$this->data['fields'] = $this->field_m->get(); |
| 22 |
|
| 23 |
//exit($this->db->last_query()); |
| 24 |
|
| 25 |
$rules = array( |
| 26 |
array( |
| 27 |
'field' => 'searchform_name', |
| 28 |
'label' => __('Name', 'wpdirectorykit'), |
| 29 |
'rules' => 'required' |
| 30 |
), |
| 31 |
array( |
| 32 |
'field' => 'searchform_json', |
| 33 |
'label' => __('Search Form Json/Structure', 'wpdirectorykit'), |
| 34 |
'rules' => '' |
| 35 |
), |
| 36 |
); |
| 37 |
|
| 38 |
if($this->form->run($rules)) |
| 39 |
{ |
| 40 |
|
| 41 |
// Check _wpnonce |
| 42 |
check_admin_referer( 'wdk-searchform-edit_'.$id, '_wpnonce' ); |
| 43 |
|
| 44 |
// Save procedure for basic data |
| 45 |
$data = $this->searchform_m->prepare_data(wdk_get_post(), $rules); |
| 46 |
|
| 47 |
// Save standard wp post |
| 48 |
|
| 49 |
if($this->searchform_m->total() == 0) |
| 50 |
{ |
| 51 |
$id = NULL; |
| 52 |
$data['idsearchform'] = 1; |
| 53 |
} |
| 54 |
|
| 55 |
$insert_id = $this->searchform_m->insert($data, $id); |
| 56 |
|
| 57 |
//exit($this->db->last_error().$insert_id); |
| 58 |
|
| 59 |
// redirect |
| 60 |
|
| 61 |
if(!empty($insert_id) && empty($id)) |
| 62 |
{ |
| 63 |
wp_redirect(admin_url("admin.php?page=wdk_searchform&id=$insert_id&is_updated=true")); |
| 64 |
exit; |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
if(!empty($id)) |
| 69 |
{ |
| 70 |
$this->data['db_data'] = $this->searchform_m->get($id, TRUE); |
| 71 |
|
| 72 |
// generate/decode used fields |
| 73 |
$used_fields = NULL; |
| 74 |
|
| 75 |
if(is_object($this->data['db_data'])) |
| 76 |
$used_fields = json_decode($this->data['db_data']->searchform_json); |
| 77 |
$this->data['used_fields'] = array(); |
| 78 |
|
| 79 |
if(is_array($used_fields)) |
| 80 |
foreach($used_fields as $used_field) |
| 81 |
{ |
| 82 |
$this->data['used_fields'][$used_field->field_id] = $used_field; |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
$this->load->view('wdk_searchform/searchform_edit', $this->data); |
| 87 |
} |
| 88 |
|
| 89 |
} |
| 90 |
|