function controller($name, $id) { $m = new model_meta(); $data = $m->fetch_by_controller($name, $id); if ($data) { $this->auto($data, false); } }
public function route_form() { $this->config->form = new form(array('class' => 'row-fluid c-form', 'class_element_frame' => 'form-group col-md-8', 'class_element_text' => 'form-control c-input', 'class_element_select' => 'form-control c-select', 'class_element_textarea' => 'form-control c-textarea', 'error_view_script' => 'control/error', 'enctype' => 'application/x-www-form-urlencoded')); if ($this->config->field) { $n = 0; foreach ($this->config->field as $k => $v) { if (!$v->active || $v->hidden) { continue; } $p = clone $v; $p->label = $p->title; $this->config->form->add($p->type, $k, $p); $n++; } } if ($this->config->callback->after_form) { $f = $this->config->callback->after_form; $f($this); } if ($this->config->meta) { $meta = $this->config_include('meta', array('return_only' => true)); if ($meta) { $meta = new data($meta); $field = $meta->field; unset($field->url); unset($field->controller); unset($field->data); $meta_array = array(); foreach ($field as $k => $v) { if (!isset($v->type)) { $v->type = 'text'; } $v->label = $v->title; $this->config->form->add($v->type, 'meta_' . $k, $v->to_array()); $meta_array[] = 'meta_' . $k; } $this->config->form->add_display_group($meta_array, 'meta', array('legend' => 'Дополнительно', 'class' => 'c-meta c-invisible')); } } if (count($this->config->oac)) { $oac_array = array(); foreach ($this->config->oac as $k => $v) { $this->config->form->add($v->type, 'oac_' . $k, $v->to_array()); $oac_array[] = 'oac_' . $k; } $this->config->form->add_display_group($oac_array, 'oac', array('class' => 'c-oac')); } if ($this->config->type == 'edit' && $this->config->model && $this->config->use_db) { $this->config->data = $this->config->model->fetch_control_card($this->config->where->to_array()); if ($this->config->data) { $d = $this->config->model->entity($this->config->data); $map = array('title_control', 'login_control', 'date_control', 'id_control'); $title = ''; foreach ($map as $el) { $title = $d->{$el}; if ($title) { break; } } if ($title) { $bread = $this->config->bread->to_array(); $bread[] = new navigation(array('title' => $title, 'route' => 'control', 'controller' => $this->config->controller, 'action' => $this->config->action, 'param' => $this->config->param)); $this->config->bread = $bread; } } } if ($this->config->type == 'edit' && ($this->config->model && $this->config->use_db && !count($this->config->data) || (!$this->config->model || !$this->config->use_db) && !$this->config->param->id)) { $this->config->notify[] = array('title' => $this->view->translate('control_notify_' . $this->config->type . '_noel'), 'style' => 'warning'); $this->config->request->current = $this->config->request->cancel; return; } $this->config->id = $this->config->data->id; if ($this->config->type == 'add' && !$this->config->id && $this->config->model && $this->config->use_db) { $this->config->id = method_exists($this->config->model, 'fetch_next_id') ? $this->config->model->fetch_next_id() : null; } if (count($this->config->post)) { $this->config->form_valid = true; if ($this->config->callback->check) { $f = $this->config->callback->check; $f($this); } if ($this->config->form->validate($this->config->post) && $this->config->form_valid) { $this->config->data_old = clone $this->config->data; unset($this->config->data); $this->config->data = $this->config->form->get(); if (count($this->config->post_field_extend)) { $this->config->data = $this->config->post_field_extend; } if (count($this->config->post_field_unset)) { foreach ($this->config->post_field_unset as $el) { unset($this->config->data->{$el}); } } if ($this->config->static_field && !@$this->config->data->{$this->config->static_field->field_dst} && $this->config->type == 'add') { $stitle = common::stitle($this->config->data[$this->config->static_field->field_src], $this->config->static_field->length); $this->config->data[$this->config->static_field->field_dst] = common::stitle_unique($this->config->model, $stitle ? $stitle : '_', $this->config->static_field->field_dst); } if (!isset($this->config->ok)) { $this->config->ok = true; } if ($this->config->callback->before) { $f = $this->config->callback->before; $f($this); } if ($this->config->ok) { $this->config->m2m_changed = false; $this->config->meta_changed = false; foreach ($this->config->data as $k => $v) { if (@$this->config->field->{$k}->m2m) { $m2m_new = $this->config->data->{$k} ? $this->config->data->{$k}->to_array() : array(); $m2m_orderid = (int) $this->config->field->{$k}->m2m->orderid; $m2m_model_class = $this->config->field->{$k}->m2m->model; $m2m_model = new $m2m_model_class(); $m2m_self = $this->config->field->{$k}->m2m->self; $m2m_foreign = $this->config->field->{$k}->m2m->foreign; $m2m_old = $m2m_model->fetch_all(array($m2m_self => $this->config->id)); $m2m_ids = array(); if ($m2m_old) { // Удаляем несуществующие связи foreach ($m2m_old as $m2m_el) { if (!$m2m_new || !in_array($m2m_el->{$m2m_foreign}, $m2m_new)) { $this->config->m2m_changed = true; $m2m_model->delete(array('id' => $m2m_el->id)); } else { $m2m_ids[] = $m2m_el->{$m2m_foreign}; } } } // Добавляем if ($m2m_new) { foreach ($m2m_new as $m2m_el) { if (!in_array($m2m_el, $m2m_ids)) { $this->config->m2m_changed = true; $m2md = array($m2m_self => $this->config->id, $m2m_foreign => $m2m_el); if ($m2m_orderid) { $nid = $m2m_model->fetch_max('orderid'); $m2md['orderid'] = $nid + 1; } $m2m_model->insert($m2md); } } } unset($this->config->data->{$k}); } } if ($this->config->model && $this->config->use_db) { if ($this->config->meta) { $model_meta = new model_meta(); $meta_data = array(); $this->config->form->group->meta->validate($this->config->post); $meta_post = $this->config->form->group->meta->get(); if (count($meta_post)) { foreach ($meta_post as $k => $v) { if (substr($k, 0, 5) != 'meta_') { continue; } $v = trim($v); if ($v) { $meta_data[substr($k, 5)] = $v; } } } $use_meta = $ex = $model_meta->fetch_row(array('controller' => $this->config->controller, 'parentid' => $this->config->id)); if (!$ex && $meta_data) { $use_meta = true; } if ($use_meta) { $meta_d = json_encode($meta_data); if ($ex) { if (!$meta_data) { $this->config->meta_changed = $model_meta->delete(array('id' => $ex->id)); } else { $this->config->meta_changed = $model_meta->update(array('data' => $meta_d), array('id' => $ex->id)); } } else { $this->config->meta_changed = $model_meta->insert(array('data' => $meta_d, 'controller' => $this->config->controller, 'parentid' => $this->config->id)); } } } $data = $data_db = $this->config->data->to_array(); $where = $this->config->where->to_array(); if ($data) { $meta = $this->config->model->metadata(); foreach ($data as $k => $v) { if (!array_key_exists($k, $meta)) { unset($data[$k]); } } } if ($this->config->type == 'add') { $this->config->ok = $this->config->model->insert_control($data); } else { $this->config->ok = $this->config->model->update_control($data, $where); } if ($this->config->ok) { if (!$this->config->id) { $this->config->id = $this->config->ok; } $this->update_single(); $this->update_clink(); } } } if (!$this->config->ok) { $this->config->ok = $this->config->m2m_changed || $this->config->meta_changed; } if ($this->config->callback->after) { $f = $this->config->callback->after; $f($this); } if ($this->config->ok) { if (!$this->config->stop_info) { $this->config->notify[] = array('title' => $this->view->translate('control_notify_' . $this->config->type . '_success'), 'style' => 'success'); } $this->config->request->current = $this->config->request->success; if ($this->config->callback->success) { $f = $this->config->callback->success; $f($this); } } else { $this->config->request->current = $this->config->request->fail; if ($this->config->callback->fail) { $f = $this->config->callback->fail; $f($this); } } return; } } else { if (count($this->config->form->element) && $this->config->type == 'edit') { foreach ($this->config->form->element as $el) { $k = $el->name; if ($this->config->field->{$k} && $this->config->field->{$k}->m2m) { $m2m_model = $this->config->field->{$k}->m2m->model; $m2m_self = $this->config->field->{$k}->m2m->self; $m2m_foreign = $this->config->field->{$k}->m2m->foreign; $this->config->data->{$k} = $m2m_model->fetch_col($m2m_foreign, array($m2m_self => $this->config->id)); } } } if ($this->config->meta) { $model_meta = new model_meta(); $meta_data_raw = $model_meta->fetch_by_controller($this->config->controller, $this->config->id); if ($meta_data_raw) { $data_entity = new entity($meta_data_raw); $meta_data = array(); if ($data_entity) { foreach ($data_entity as $k => $v) { $meta_data['meta_' . $k] = $data_entity->{$k . '_lang'}; } } $this->config->form->group->meta->populate($meta_data); } } } if ($this->config->callback->preset) { $f = $this->config->callback->preset; $f($this); } if (!count($this->config->post)) { $data_entity = new entity($this->config->data); $rd = array(); if ($data_entity) { foreach ($data_entity as $k => $v) { $rd[$k] = $data_entity->{$k . '_lang'}; } } $this->config->form->populate($rd); } }