]> git.andy128k.dev Git - ipf.git/commitdiff
TupleInput Widget (Textarea to EditableGrid)
authoravl <alex.litovchenko@gmail.com>
Sat, 13 Sep 2008 22:42:41 +0000 (01:42 +0300)
committeravl <alex.litovchenko@gmail.com>
Sat, 13 Sep 2008 22:42:41 +0000 (01:42 +0300)
ipf/admin/modelinline.php
ipf/form/boundfield.php
ipf/form/widget.php
ipf/form/widget/fileinput.php
ipf/form/widget/tupleinput.php [new file with mode: 0644]

index 4d43f8e4d2d9577fa639094be5bdc5029d48e110..efa78e06cd85c7f8d850cf51f21f2a9b09e82931 100644 (file)
@@ -93,6 +93,7 @@ abstract class IPF_Admin_ModelInline{
             }
         }
         
+        /*
         for($i=0; $i<$this->getAddNum(); $i++ ){
             $form = IPF_Shortcuts::GetFormForModel($this->model->copy(), null, array('exclude'=>array($this->getFkName(),$this->getFkLocal())));
             $form->fields = array_merge(array(new IPF_Form_Field_Boolean(array('label'=>'Del','name'=>'delete_', 'widget_attrs'=>array('disabled'=>'disabled')))),$form->fields);
@@ -107,6 +108,7 @@ abstract class IPF_Admin_ModelInline{
                 $form->isFirst = false;
             $this->formset[] = $form;
         }
+        */
     }
     
     function save(){
index bc97152f156255f67232d931af712ec4c6869a54..d16f01d0f7dbf0028b37b855c00f278d8f92d22e 100644 (file)
@@ -40,7 +40,7 @@ class IPF_Form_BoundField
         if (!$this->form->is_bound) {
             $data = $this->form->initial($this->name);
         } else {
-            $data = $this->field->widget->valueFromFormData($this->html_name, $this->form->data);
+            $data = $this->field->widget->valueToFormData($this->html_name, $this->form->data);
         }
         return $widget->render($this->html_name, $data, $attrs);
     }
index 551fd4165f77c54ba0d7620a3352e647d0fa0528..aff7a07ae5870977f831030dda32687d70d0d39a 100644 (file)
@@ -30,6 +30,14 @@ class IPF_Form_Widget
         return null;
     }
 
+    public function valueToFormData($name, $data)
+    {
+        if (isset($data[$name])) {
+            return $data[$name];
+        }
+        return null;
+    }
+
     public function idForLabel($id)
     {
         return $id;
index 08f6f75444e027d5c89de577b847a6af18ac7162..6d80ef9c67927f6a83402506fbf71b55a491101a 100644 (file)
@@ -11,7 +11,7 @@ class IPF_Form_Widget_FileInput extends IPF_Form_Widget_Input
         if (isset($value['data'])){
             $value = $value['data'];
             if (is_string($value) && $value!=''){
-                $sim = 'Currently: <a target="_blank" href="'.IPF::get('upload_url').$value.'">'.$value.'</a> | <input name="'.$name.'_remove" value="1" id="id_'.$name.'_remove" type="checkbox" /> <label for="id_'.$name.'_remove">Remove</label><br />Change:';
+                $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 for="id_'.$name.'_remove">Remove</label></nobr><br />Change:';
             }
         }
         $value = '';
@@ -30,6 +30,20 @@ class IPF_Form_Widget_FileInput extends IPF_Form_Widget_Input
         }
         return null;
     }
+
+    public function valueToFormData($name, $data)
+    {
+        if (isset($data[$name])) {
+            $remove = false;
+            if (isset($data[$name.'_remove']))
+                if ($data[$name.'_remove']==1)
+                    $remove = true;
+            $res = array('data'=>$data[$name], 'remove'=>$remove);
+            return $res;
+        }
+        return null;
+    }
+
     
 
 }
\ No newline at end of file
diff --git a/ipf/form/widget/tupleinput.php b/ipf/form/widget/tupleinput.php
new file mode 100644 (file)
index 0000000..e82e484
--- /dev/null
@@ -0,0 +1,68 @@
+<?php
+
+class IPF_Form_Widget_TupleInput extends IPF_Form_Widget
+{
+    var $headers = array('');
+    var $rows = 3;
+
+    public function __construct($attrs=array()){
+        
+        parent::__construct($attrs);
+
+        if (isset($attrs['headers']))
+            $this->headers = $attrs['headers'];
+
+        if (isset($attrs['rows']))
+            $this->rows = $attrs['rows'];
+    }
+    
+    protected function isHeadLabels(){
+        foreach ($this->headers as &$h){
+            if ($h=='')
+                return false;
+        }
+        return true;
+    }
+
+    public function render($name, $value, $extra_attrs=array())
+    {
+        $data = array();
+        $lines = explode("\n",$value);
+        foreach ($lines as $line){
+            $data[] = explode('|', $line);
+        }
+        if ($value === null) $value = '';
+        $s = '<table class="tuplegrid">';
+        if ($this->isHeadLabels()){
+            $s .= '<tr>';
+            foreach ($this->headers as &$h){
+                $s .= '<th>'.$h.'</th>';
+            }
+            $s .= '</tr>';
+        }
+        for ($i=0; $i<$this->rows; $i++){
+            $s .= '<tr>';
+            for ($j=0; $j<count($this->headers); $j++){
+                $v = @$data[$i][$j];
+                $s .= '<td><input name="'.$name.'_'.$i.'_'.$j.'" value="'.$v.'"></td>';
+            }
+            $s .= '<tr>';
+        }
+        $s .= '</table>';
+        return new IPF_Template_SafeString($s,true);
+    }
+
+    public function valueFromFormData($name, $data)
+    {
+        $s = '';
+        for ($i=0; $i<$this->rows; $i++){
+            if ($i>0) $s .= "\n";
+            for ($j=0; $j<count($this->headers); $j++){
+                if ($j>0) $s.='|';
+                $s .= @$data[$name.'_'.$i.'_'.$j];
+            }
+        }
+        return $s;
+    }
+}
+