Esempio n. 1
0
 public function filter($value)
 {
     $ext = strrpos($value, '.');
     if ($ext !== false) {
         $t = substr($value, $ext + 1);
         $value = substr($value, 0, $ext);
         $ext = $t;
     }
     $stitle = Zkernel_Common::stitle($value, $this->_length);
     $p = '';
     while (file_exists($this->_directory . '/' . $stitle . $p . ($ext ? '.' . $ext : ''))) {
         $p = $p ? $p + 1 : 1;
     }
     return $stitle . $p . ($ext ? '.' . $ext : '');
 }
Esempio n. 2
0
 public function routeForm()
 {
     $id = $id_old = $this->config->param['id'];
     if ($this->config->type == 'edit' && !$id) {
         $this->config->info[] = $this->config->control_lang_current['no_sel'];
         $this->config->stop_frame = true;
     } else {
         $this->config->form = $this->buildForm();
         $this->config->data = array();
         if (@(int) $this->config->post['cposted'] || @(int) $this->config->post['sposted']) {
             if ($this->config->form->isValid($this->config->post->toArray())) {
                 if ($this->config->type == 'add') {
                     $id = $this->config->use_db ? method_exists($this->config->model, 'fetchNextId') ? $this->config->model->fetchNextId() : 0 : 0;
                 }
                 $this->config->data = $this->config->form->getValues();
                 if ($this->config->static_field && !@$this->config->data->{$this->config->static_field->field_dst} && $this->config->type == 'add') {
                     $stitle = Zkernel_Common::stitle($this->config->data[$this->config->static_field->field_src], $this->config->static_field->length);
                     $stitle = $stitle ? $stitle : '_';
                     $stitle_n = $stitle;
                     if ($this->config->static_field->unique && $this->config->use_db) {
                         $stitle_p = -1;
                         do {
                             $stitle_p++;
                             $stitle_n = $stitle . ($stitle_p == 0 ? '' : $stitle_p);
                             $w = array('`' . $this->config->static_field->field_dst . '` = ?' => $stitle_n);
                             if ($this->config->type == 'edit') {
                                 $w['`id` != ?'] = $id;
                             }
                             $stitle_c = (int) $this->config->model->fetchCount($w);
                         } while ($stitle_c > 0);
                     }
                     $this->config->data[$this->config->static_field->field_dst] = $stitle_n;
                 }
                 if (count($this->config->post_field_unset)) {
                     foreach ($this->config->post_field_unset as $k) {
                         unset($this->config->data[$k]);
                     }
                 }
                 if (count($this->config->post_field_extend)) {
                     $this->config->data->set($this->config->post_field_extend);
                 }
                 $m2m_changed = false;
                 foreach ($this->config->data as $k => $v) {
                     if (@$this->config->field->{$k}->m2m) {
                         $m2m_new = isset($this->config->data->{$k}) ? $this->config->data->{$k}->toArray() : array();
                         $m2m_orderid = (int) $this->config->field->{$k}->m2m->orderid;
                         $m2m_model = $this->config->field->{$k}->m2m->model;
                         $m2m_model = new $m2m_model();
                         $m2m_self = $this->config->field->{$k}->m2m->self;
                         $m2m_foreign = $this->config->field->{$k}->m2m->foreign;
                         $m2m_old = $m2m_model->fetchAll(array('`' . $m2m_self . '` = ?' => $id));
                         if ($m2m_old) {
                             $m2m_ids = array();
                             // Удаляем несуществующие связи
                             foreach ($m2m_old as $m2m_el) {
                                 if (!$m2m_new || !in_array($m2m_el->{$m2m_foreign}, $m2m_new)) {
                                     $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)) {
                                         $m2m_changed = true;
                                         $m2md = array($m2m_self => $id, $m2m_foreign => $m2m_el);
                                         if ($m2m_orderid) {
                                             $nid = $m2m_model->fetchOne('MAX(`orderid`)');
                                             $m2md['orderid'] = $nid + 1;
                                         }
                                         $yyy = $m2m_model->insert($m2md);
                                     }
                                 }
                             }
                         }
                         unset($this->config->data->{$k});
                     }
                 }
                 if ($this->config->zk_meta) {
                     $empty = !$this->config->data->meta_title && !$this->config->data->meta_keywords && !$this->config->data->meta_description;
                     $md = array('title' => $this->config->data->meta_title, 'keywords' => $this->config->data->meta_keywords, 'description' => $this->config->data->meta_description, 'show_title' => $this->config->data->meta_show_title);
                     $mm = new Default_Model_Meta();
                     $me = (int) $mm->fetchOne('id', array('`oid` = "' . $this->config->controller . '_' . $id . '"'));
                     if ($me) {
                         if ($empty) {
                             $mm->delete(array('`id` = ?' => $me));
                         } else {
                             $mm->updateControl($md, array('`id` = ?' => $me));
                         }
                     } else {
                         $md['oid'] = $this->config->controller . '_' . $id;
                         $mm->insertControl($md);
                     }
                 }
                 $ok = false;
                 $this->config->func_override;
                 $this->config->func_check;
                 if ($this->config->use_db && count($this->config->info) == 0) {
                     $data_db = $this->config->data->toArray();
                     foreach ($data_db as $k => $v) {
                         if (!array_key_exists($k, method_exists($this->config->model, 'info') ? $this->config->model->info('metadata') : array())) {
                             unset($data_db[$k]);
                         }
                     }
                     if ($this->config->type == 'edit') {
                         $where = $this->config->where ? $this->config->where->toArray() : array();
                         $where['`id` = ?'] = $id;
                         $ok = $data_db ? $this->config->model->updateControl($data_db, $where) : false;
                         if ($ok) {
                             $keys = array_keys($data_db);
                             $dd = array();
                             foreach ($keys as $key) {
                                 if (@(int) $this->config->field->{$key}->single) {
                                     $dd[$key] = '';
                                 }
                             }
                             if ($dd) {
                                 unset($where['`id` = ?']);
                                 $where['`id` != ?'] = $id;
                                 $this->config->model->updateControl($dd, $where);
                             }
                         }
                     } else {
                         $ok = $this->config->data->id = $this->config->model->insertControl($data_db);
                     }
                 }
                 if ($ok || $m2m_changed) {
                     $this->config->info[] = $this->config->control_lang_current['data_saved'];
                     $this->config->func_success;
                     if ($this->config->oac_add_apply && @$this->config->post['is_apply'] && $this->config->type == 'add') {
                         $this->view->inlineScript('script', 'c.go("' . $this->config->controller . '", "ctledit", ' . Zend_Json::encode(array('id' => $ok)) . ');');
                     }
                 } else {
                     //$this->config->info[] = 'Изменений данных не было';
                 }
                 if (!@$this->config->post['is_apply'] && !@$this->config->post['sposted']) {
                     $this->view->inlineScript('script', 'c.go("' . $this->config->request_ok->controller . '", "' . $this->config->request_ok->action . '", ' . Zend_Json::encode(Zkernel_Common::url2array($this->config->request_ok->param)) . ');');
                 }
             } else {
                 $this->config->info_type = 'e';
                 foreach ($this->config->form->getErrors() as $k => $el) {
                     if ($el) {
                         $this->config->info[] = $this->config->form->getElement($k)->getLabel() . ': ' . implode(', ', $el);
                     }
                 }
             }
             if ($this->config->scroll_top) {
                 $this->view->inlineScript('script', 'window.scroll(0, 0);');
             }
             $this->config->stop_frame = true;
         } else {
             if ($this->config->type == 'edit') {
                 $where = $this->config->where ? $this->config->where->toArray() : array();
                 $where['`id` = ?'] = $id;
                 $data = $this->config->model->fetchControlCard($where);
                 $data = $this->view->override()->overrideSingle($data, $this->config->controller, array('multilang_nofall' => true, 'module_nofall' => true));
                 $this->config->data->set($data->toArray());
                 if ($this->config->field && isset($this->config->field->{$this->config->field_title}) && $this->config->data->{$this->config->field_title}) {
                     $this->config->navpane->finish[] = array('t' => $this->config->data->{$this->config->field_title}, 'c' => $this->config->controller, 'a' => $this->config->action, 'p' => '');
                 }
                 //$data = $data ? $data->toArray() : array();
                 $els = $this->config->form->getElements();
                 if ($els) {
                     foreach ($els as $el) {
                         $k = $el->getName();
                         if (@$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->fetchCol($m2m_foreign, array('`' . $m2m_self . '` = ?' => $id));
                         }
                     }
                 }
                 if ($this->config->zk_meta) {
                     $mm = new Default_Model_Meta();
                     $md = $mm->fetchRow(array('`oid` = "' . $this->config->controller . '_' . $id . '"'));
                     $md = $this->view->override()->overrideSingle($md);
                     if ($md) {
                         foreach ($md as $k => $v) {
                             $this->config->data['meta_' . $k] = $v;
                         }
                     }
                 }
             }
             $this->config->func_preset;
             $this->config->form->setDefaults($this->config->data->toArray());
         }
         if ($this->config->tree && $id) {
             $s = new Zend_Session_Namespace();
             $s->control['history'][$this->config->controller]['oid'] = $id;
         }
     }
     echo $this->view->render($this->config->view);
     return $this;
 }