Exemplo n.º 1
0
 public function validate(CM_Form_Abstract $form)
 {
     if ($this->_extra_validation) {
         $values = array();
         foreach ($form->get_values() as $name => $value) {
             $values[$name] = $value->get_raw();
             $this->_extra_validation->label($name, $form->get_field($name)->get_label());
         }
         // Validation только read-only, поэтому создаем новый объект
         $this->_extra_validation = $this->_extra_validation->copy($values);
     }
     try {
         $this->get_model()->check($this->_extra_validation);
     } catch (ORM_Validation_Exception $e) {
         $errors = $e->errors('validation');
         if ($external = arr::get($errors, '_external')) {
             $errors = arr::merge($errors, $external);
             unset($errors['_external']);
         }
         foreach ($errors as $name => $error) {
             $form->get_field($name)->set_error($error);
         }
         return FALSE;
     }
     return TRUE;
 }
Exemplo n.º 2
0
 public function populate(CM_Form_Abstract $form)
 {
     $value = $form->get_field($this->get_field_name())->get_value()->get_raw();
     if ($value) {
         $this->get_model()->where($this->get_field_name(), 'LIKE', '%' . $value . '%');
     }
 }
Exemplo n.º 3
0
 public function after_submit(CM_Form_Abstract $form)
 {
     $values = unserialize($form->get_field($this->_field_name)->get_value()->get_raw());
     foreach ($this->get_db_values() as $db_value) {
         if (!isset($values[$db_value->index])) {
             $db_value->delete();
         } else {
             if (is_null($values[$db_value->index])) {
                 unset($values[$db_value->index]);
                 $db_value->delete();
             } else {
                 $db_value->value = $values[$db_value->index];
                 $db_value->save();
                 unset($values[$db_value->index]);
             }
         }
     }
     foreach ($values as $index => $value) {
         if (is_null($value)) {
             continue;
         }
         $db_value = clone $this->_value_model;
         $db_value->index = $index;
         $db_value->value = $value;
         $db_value->{$this->_parent_field} = $this->get_model();
         $db_value->save();
     }
 }
Exemplo n.º 4
0
 public function populate(CM_Form_Abstract $form)
 {
     $value = $form->get_field($this->get_field_name())->get_value()->get_raw();
     if ($value == '') {
         return;
     }
     if (empty($value) && $value != 0 && !$this->_allow_empty) {
         return;
     }
     if (!is_null($value) && $value or $this->_allow_null or $value == 0) {
         if (is_object($this->get_model()->{$this->get_field_name()})) {
             $ids = array();
             foreach (DB::select()->from($this->_relation_table)->where($this->_related_field, '=', $value)->execute()->as_array() as $model) {
                 $ids[] = $model[$this->_relation_field];
             }
             if ($ids) {
                 $this->get_model()->where('id', 'IN', $ids);
             } elseif ($value != '') {
                 $this->get_model()->where('id', '=', '-1');
             }
         } elseif ($value || $value == 0) {
             $this->get_model()->where($this->get_field_name(), is_null($value) ? 'IS' : '=', $value);
         }
     }
 }
Exemplo n.º 5
0
 public function populate(CM_Form_Abstract $form)
 {
     $values = array();
     foreach ($this->_item->get_fieldschema()->get_field_names() as $name) {
         $values[$name] = $form->get_field($name)->get_value();
     }
     $this->_item->set_values($values);
 }
Exemplo n.º 6
0
 public function validate(CM_Form_Abstract $form)
 {
     if (!$form->get_field($this->_field_name)->is_valid()) {
         $form->get_field($this->_field_name)->set_error('Неверный код');
         return FALSE;
     }
     return TRUE;
 }
Exemplo n.º 7
0
 public function construct_form(CM_Form_Abstract $form, $param)
 {
     foreach ($this->_labels as $name => $label) {
         if ($form->has_field($name)) {
             $form->get_field($name)->set_label($label);
         }
     }
 }
Exemplo n.º 8
0
 public function populate(CM_Form_Abstract $form)
 {
     $date = $form->get_field($this->get_field_name())->get_value();
     $date_value = $date->get_raw();
     if ($date->is_valid() && !empty($date_value)) {
         $this->get_model()->where($this->get_field_name(), '>=', $date->get_raw() . ' 00:00:00');
         $this->get_model()->where($this->get_field_name(), '<=', $date->get_raw() . ' 23:59:59');
     }
 }
Exemplo n.º 9
0
 public function construct_form(CM_Form_Abstract $form, $params)
 {
     $this->set_model($params);
     foreach ($form->get_field_names() as $name) {
         if ($label = arr::get($this->get_model()->labels(), $name)) {
             $form->get_field($name)->set_label($label);
         }
     }
 }
Exemplo n.º 10
0
 public function construct_form(CM_Form_Abstract $form, $user)
 {
     if ($user->id == Auth::instance()->get_user()->id) {
         $form->get_field('roles')->set_role_disabled('admin');
     }
     if (!Auth::instance()->logged_in('admin')) {
         $form->get_field('roles')->set_role_disabled('admin');
     }
 }
Exemplo n.º 11
0
 /**
  * @return array [string => bool]
  */
 public function getFieldList()
 {
     if (null === $this->_fieldList) {
         $this->_fieldList = array();
         foreach ($this->_form->getFields() as $fieldName => $field) {
             $this->_fieldList[$fieldName] = in_array($fieldName, $this->_getRequiredFields());
         }
     }
     return $this->_fieldList;
 }
Exemplo n.º 12
0
 public function populate(CM_Form_Abstract $form)
 {
     $value = $form->get_field($this->get_field_name())->get_value()->get_raw();
     if (!is_null($value)) {
         $value = preg_replace('#\\*{2,}#', '*', $value);
         $value = strtr($value, array('%' => '\\%', '_' => '\\_'));
         if (!empty($value)) {
             $this->get_model()->where($this->get_field_name(), 'LIKE', str_replace('*', '%', $value));
         }
     }
 }
Exemplo n.º 13
0
 public function populate(CM_Form_Abstract $form)
 {
     $date_from = $form->get_field($this->get_field_name())->get_value()->get_date_from();
     $date_to = $form->get_field($this->get_field_name())->get_value()->get_date_to();
     if ($date_from->is_valid() and !is_null($date_from->get_raw())) {
         $this->get_model()->where($this->get_field_name(), '>=', $date_from->get_raw() . ' 00:00:00');
     }
     if ($date_to->is_valid() and !is_null($date_to->get_raw())) {
         $this->get_model()->where($this->get_field_name(), '<=', $date_to->get_raw() . ' 23:59:59');
     }
 }
Exemplo n.º 14
0
 public function render(CM_Form_Abstract $form)
 {
     $fieldgroups = array();
     foreach ($this->_fieldgroups as $fieldgroup) {
         $fieldgroups[$fieldgroup] = array();
     }
     foreach ($form->get_field_names() as $name) {
         $fieldgroup = arr::get($this->_fields_fieldgroups, $name, 'Default');
         $fieldgroups[$fieldgroup][$name] = $form->get_field($name);
     }
     foreach ($fieldgroups as $fieldgroup_name => $fields) {
         if (empty($fields)) {
             unset($fieldgroups[$fieldgroup_name]);
         }
     }
     return View::factory('cm/form/fieldgroups', array('fieldgroups' => $fieldgroups, 'form' => $form));
 }
Exemplo n.º 15
0
 public function validate(CM_Form_Abstract $form)
 {
     $values = array();
     foreach ($form->get_values() as $name => $value) {
         $values[$name] = $value->get_raw();
         $this->_validation->label($name, $form->get_field($name)->get_label());
     }
     // Validation только read-only, поэтому создаем новый объект
     $this->_validation = $this->_validation->copy($values);
     if ($this->_validation->check()) {
         return TRUE;
     }
     foreach ($this->_validation->errors('validation') as $name => $error) {
         $form->set_error($name, $error);
     }
     return FALSE;
 }
Exemplo n.º 16
0
 public function after_submit(CM_Form_Abstract $form)
 {
     $this->_model->save();
     if (!trim($form->get_field('url')->get_value()->get_raw()) && $this->_model->name != 'Главная страница') {
         $this->_model->url = Extasy_Text::to_url($form->get_field('name')->get_value()->get_raw());
     }
     if (!trim($form->get_field('s_title')->get_value()->get_raw())) {
         $this->_model->s_title = $this->_model->name;
     }
     $number_of_dashes = substr_count($this->_model->url, '--');
     $replacement_string = '';
     for ($i = 0; $i < $number_of_dashes; $i++) {
         $replacement_string .= '--';
     }
     $this->_model->url = str_replace($replacement_string, '-', $this->_model->url);
     $this->_model->save();
     parent::after_submit($form);
 }
Exemplo n.º 17
0
 public function construct_form(CM_Form_Abstract $form, $param)
 {
     $renderer = new CM_Form_Renderer_Fieldgroups();
     foreach ($this->_fieldgroups as $group_name => $fields) {
         if (is_array($fields)) {
             foreach ($fields as $field_name) {
                 $renderer->set_field_fieldgroup($field_name, $group_name);
             }
         } else {
             foreach ($form->get_field_names() as $field_name) {
                 if (preg_match($fields, $field_name)) {
                     $renderer->set_field_fieldgroup($field_name, $group_name);
                 }
             }
         }
     }
     $form->set_renderer($renderer);
 }
Exemplo n.º 18
0
 public function construct_form($param)
 {
     parent::construct_form($param);
     $siteRoutes = $this->get_menu();
     if (array_key_exists($param->url, $siteRoutes)) {
         $this->set_field('url', new CM_Field_Change($siteRoutes), 20);
     } else {
         $this->set_field('url', new CM_Field_Change($siteRoutes, 'string'), 20);
     }
     $this->get_field('url')->set_raw_value($param->url);
 }
Exemplo n.º 19
0
function smarty_block_form($params, $content, Smarty_Internal_Template $template, $open)
{
    /** @var CM_Frontend_Render $render */
    $render = $template->smarty->getTemplateVars('render');
    $frontend = $render->getGlobalResponse();
    if ($open) {
        $form = CM_Form_Abstract::factory($params['name'], $params);
        $viewResponse = new CM_Frontend_ViewResponse($form);
        $form->prepare($render->getEnvironment(), $viewResponse);
        $frontend->treeExpand($viewResponse);
        return '';
    } else {
        $viewResponse = $frontend->getClosestViewResponse('CM_Form_Abstract');
        if (null === $viewResponse) {
            throw new CM_Exception_Invalid('Cannot find `CM_Form_Abstract` within frontend tree.');
        }
        /** @var CM_Form_Abstract $form */
        $form = $viewResponse->getView();
        $cssClasses = $viewResponse->getCssClasses();
        $cssClasses[] = $form->getName();
        $html = '<form id="' . $viewResponse->getAutoId() . '" class="' . implode(' ', $cssClasses) . '" method="post" action="" onsubmit="return false;" novalidate >';
        if ($form->getAvoidPasswordManager()) {
            $html .= '<input style="display:none" type="text" name="fakeusernameremembered">';
            $html .= '<input style="display:none" type="password" name="fakepasswordremembered">';
        }
        $html .= $content;
        foreach ($form->getFields() as $field) {
            if ($field instanceof CM_FormField_Hidden) {
                $renderAdapter = new CM_RenderAdapter_FormField($render, $field);
                $html .= $renderAdapter->fetch(CM_Params::factory());
            }
        }
        foreach ($form->getActions() as $actionName => $action) {
            $viewResponse->getJs()->append("this.registerAction('{$actionName}', {$action->js_presentation()});");
        }
        $html .= '</form>';
        $frontend->treeCollapse();
        return $html;
    }
}
Exemplo n.º 20
0
 public function construct_form(CM_Form_Abstract $form, $param)
 {
     foreach ($this->_fields as $name) {
         $form->get_field($name)->set_raw_value(html_entity_decode($form->get_field($name)->get_value()->get_raw(), ENT_COMPAT, 'UTF-8'));
     }
 }