From 1dfe1ef521c87663dd2321bc4ba11902aa127796 Mon Sep 17 00:00:00 2001 From: avl Date: Sun, 25 Jan 2009 06:53:19 +0200 Subject: [PATCH] Tree Catalog Selector --- ipf/admin/model.php | 3 -- ipf/form.php | 6 ++-- ipf/form/field.php | 9 +++-- ipf/form/field/treemodelchoice.php | 54 +++++++++++++++++++++++++++++ ipf/form/model.php | 2 +- ipf/form/widget/fileinput.php | 6 +++- ipf/form/widget/treeselectinput.php | 42 ++++++++++++++++++++++ 7 files changed, 112 insertions(+), 10 deletions(-) create mode 100644 ipf/form/field/treemodelchoice.php create mode 100644 ipf/form/widget/treeselectinput.php diff --git a/ipf/admin/model.php b/ipf/admin/model.php index 4f6d4e1..a4b21b8 100644 --- a/ipf/admin/model.php +++ b/ipf/admin/model.php @@ -108,9 +108,6 @@ class IPF_Admin_Model{ } public function ListItemsHeader(){ - - - $this->header = array(); if (method_exists($this,'list_display')) $this->names = $this->list_display(); diff --git a/ipf/form.php b/ipf/form.php index 09b0c6d..517135d 100644 --- a/ipf/form.php +++ b/ipf/form.php @@ -60,8 +60,7 @@ class IPF_Form implements Iterator $this->errors = array(); $form_methods = get_class_methods($this); foreach ($this->fields as $name=>$field) { - $value = $field->widget->valueFromFormData($this->addPrefix($name), - $this->data); + $value = $field->widget->valueFromFormData($this->addPrefix($name), &$this->data); try { $value = $field->clean($value); $this->cleaned_data[$name] = $value; @@ -96,6 +95,9 @@ class IPF_Form implements Iterator public function clean() { + foreach ($this->fields as $name=>$field) { + $field->LateClean(&$this->data, &$this->cleaned_data); + } return $this->cleaned_data; } diff --git a/ipf/form/field.php b/ipf/form/field.php index fdd3319..f2e10d6 100644 --- a/ipf/form/field.php +++ b/ipf/form/field.php @@ -6,7 +6,7 @@ class IPF_Form_Field public $widget = 'IPF_Form_Widget_TextInput'; public $label = ''; - public $required = false; + public $required = false; public $help_text = ''; public $initial = ''; public $choices = null; @@ -21,7 +21,7 @@ class IPF_Form_Field foreach ($params as $key=>$in) { if ($key !== 'widget_attrs') if (isset($this->$key)) - $default[$key] = $this->$key; + $default[$key] = $this->$key; } $m = array_merge($default, $params); foreach ($params as $key=>$in) { @@ -49,7 +49,10 @@ class IPF_Form_Field } return $value; } - + + function LateClean($data, $cleaned_data){ + } + protected function getWidget(){ return $this->widget; } diff --git a/ipf/form/field/treemodelchoice.php b/ipf/form/field/treemodelchoice.php new file mode 100644 index 0000000..7c78e4b --- /dev/null +++ b/ipf/form/field/treemodelchoice.php @@ -0,0 +1,54 @@ +_models = $params['models']; + $choices = array('--------'=>''); + $levels = array(); + foreach($this->_models as &$m){ + $m['objects'] = IPF_ORM_Query::create()->from($m['model'])->orderby('ord')->execute(); + $levels[] = $m['field']; + } + $this->_collectTreeRecursive(&$choices); + $this->setChoices($choices); + $this->widget->setLevels($levels); + } + + protected function _collectTreeRecursive(&$choices,$level=0,$parent_id=null,$valname=''){ + foreach($this->_models[$level]['objects'] as $o){ + if ($parent_id){ + $foreign = $this->_models[$level]['foreign']; + if ($parent_id!=$o->$foreign) + continue; + } + $name = str_repeat("-", $level).$o['name']; + $choices[$name] = $valname.$o->id; + if ($level<(count($this->_models)-1)){ + $this->_collectTreeRecursive(&$choices,$level+1,$o->id,$valname.$o->id.'.'); + } + } + } + + function LateClean($data, $cleaned_data){ + foreach($this->_models as &$m){ + $cleaned_data[$m['field']] = $data[$m['field']]; + } + } + + + /* + public function clean($value){ + parent::clean($value); + if (in_array($value, $this->empty_values)) { + return null; + } + $o = $this->_model->getTable()->find($value); + return $o; + } + */ +} diff --git a/ipf/form/model.php b/ipf/form/model.php index 0e447b9..bbbdf63 100644 --- a/ipf/form/model.php +++ b/ipf/form/model.php @@ -145,6 +145,6 @@ class IPF_Form_Model extends IPF_Form } } } - throw new IPF_Exception_Form(__('Cannot save the model from an invalid form.')); + //throw new IPF_Exception_Form(__('Cannot save the model from an invalid form.')); } } diff --git a/ipf/form/widget/fileinput.php b/ipf/form/widget/fileinput.php index b9f0b84..5ae334d 100644 --- a/ipf/form/widget/fileinput.php +++ b/ipf/form/widget/fileinput.php @@ -4,6 +4,7 @@ class IPF_Form_Widget_FileInput extends IPF_Form_Widget_Input { public $input_type = 'file'; public $needs_multipart_form = true; + public $allow_extended = true; public function render($name, $value, $extra_attrs=array()) { @@ -11,7 +12,10 @@ class IPF_Form_Widget_FileInput extends IPF_Form_Widget_Input if (isset($value['data'])){ $value = $value['data']; if (is_string($value) && $value!=''){ - $sim = 'Currently: '.$value.' |  Change:'; + if ($this->allow_extended) + $sim = 'Currently: '.$value.' |  Change:'; + else + $sim = 'Currently: '.$value.'
Change: '; } } $value = ''; diff --git a/ipf/form/widget/treeselectinput.php b/ipf/form/widget/treeselectinput.php new file mode 100644 index 0000000..b39da24 --- /dev/null +++ b/ipf/form/widget/treeselectinput.php @@ -0,0 +1,42 @@ +_levels = $levels; + } + + public function valueToFormData($name, $data) + { + $val = null; + foreach($this->_levels as $l){ + if ( (!isset($data[$l])) || ($data[$l]=='')){ + return $val; + } + if ($val==null) + $val = ''; + else + $val .= '.'; + $val .= $data[$l]; + } + return $val; + } + + public function valueFromFormData($name, $data) + { + if (isset($data[$name])) { + $vals = split("\.",(string)$data[$name]); + for($i=0; $i_levels); $i++){ + if ( ($i_levels[$i]] = $vals[$i]; + } + else + $data[$this->_levels[$i]] = null; + } + return $data[$name]; + } + return null; + } +} \ No newline at end of file -- 2.49.0