]> git.andy128k.dev Git - ipf.git/commitdiff
Tree Catalog Selector
authoravl <alex.litovchenko@gmail.com>
Sun, 25 Jan 2009 04:53:19 +0000 (06:53 +0200)
committeravl <alex.litovchenko@gmail.com>
Sun, 25 Jan 2009 04:53:19 +0000 (06:53 +0200)
ipf/admin/model.php
ipf/form.php
ipf/form/field.php
ipf/form/field/treemodelchoice.php [new file with mode: 0644]
ipf/form/model.php
ipf/form/widget/fileinput.php
ipf/form/widget/treeselectinput.php [new file with mode: 0644]

index 4f6d4e1714c29a6ae50251afdc60de6b6b9891bc..a4b21b80e8c533b07430aee853cb025f850c0a29 100644 (file)
@@ -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();
index 09b0c6d2e4379725dacff4c76239f6ced46d4cd9..517135d37baf35da0f056dbea3e76e0230e704a1 100644 (file)
@@ -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;
     }
 
index fdd33193b10dadd8fc763b77cf2dcfb0b1f8b86f..f2e10d6871539e46f4c0c90c1621a3960404f080 100644 (file)
@@ -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 (file)
index 0000000..7c78e4b
--- /dev/null
@@ -0,0 +1,54 @@
+<?php
+
+class IPF_Form_Field_TreeModelChoice extends IPF_Form_Field_Choice{
+
+    public $widget = 'IPF_Form_Widget_TreeSelectInput';
+    protected $_models;
+
+    function __construct($params=array()){
+        parent::__construct($params);
+        $this->_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;
+    }
+    */
+}
index 0e447b9b8c50b19d2bf40d47ea4bccdb5b32fde3..bbbdf6326f4548b75a188ffa0de1202374307b13 100644 (file)
@@ -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.'));
     }
 }
index b9f0b84a28888e7299eca83bd2f25746c7542ea0..5ae334d3428098939c751a2c7583dc4fb5436571 100644 (file)
@@ -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 = '<nobr>Currently: <a target="_blank" href="'.IPF::get('upload_url').$value.'">'.$value.'</a>&nbsp;|&nbsp;<input name="'.$name.'_remove" value="1" id="id_'.$name.'_remove" type="checkbox" />&nbsp;<label class="file_remove" for="id_'.$name.'_remove">Remove</label></nobr>Change:';
+                               if ($this->allow_extended)
+                       $sim = '<nobr>Currently: <a target="_blank" href="'.IPF::get('upload_url').$value.'">'.$value.'</a>&nbsp;|&nbsp;<input name="'.$name.'_remove" value="1" id="id_'.$name.'_remove" type="checkbox" />&nbsp;<label class="file_remove" for="id_'.$name.'_remove">Remove</label></nobr>Change:';
+                               else
+                       $sim = '<nobr>Currently: <b>'.$value.'</b><br> Change: ';
             }
         }
         $value = '';
diff --git a/ipf/form/widget/treeselectinput.php b/ipf/form/widget/treeselectinput.php
new file mode 100644 (file)
index 0000000..b39da24
--- /dev/null
@@ -0,0 +1,42 @@
+<?php
+
+class IPF_Form_Widget_TreeSelectInput extends IPF_Form_Widget_SelectInput
+{
+       protected $_levels = null;
+
+       public function setLevels($levels){
+           $this->_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<count($this->_levels); $i++){
+                       if ( ($i<count($vals)) && ($data[$name]!='')){
+                               $data[$this->_levels[$i]] = $vals[$i];
+                       }
+                       else
+                               $data[$this->_levels[$i]] = null;
+               }
+            return $data[$name];
+        }
+        return null;
+    }
+}
\ No newline at end of file