From 06185539370443dbad37270800bdc493e2d29c38 Mon Sep 17 00:00:00 2001 From: Andrey Kutejko Date: Sun, 11 Aug 2013 23:25:04 +0300 Subject: [PATCH] rework rendering on inline tables --- ipf/admin/modelinline.php | 40 +++++++++++++++++---------- ipf/admin/templates/admin/change.html | 22 +++++++++------ 2 files changed, 40 insertions(+), 22 deletions(-) diff --git a/ipf/admin/modelinline.php b/ipf/admin/modelinline.php index f49d666..8e1b9fe 100644 --- a/ipf/admin/modelinline.php +++ b/ipf/admin/modelinline.php @@ -34,6 +34,32 @@ abstract class IPF_Admin_ModelInline return get_class($this->model); } + public function listColumns() + { + if (!$this->formset || !count($this->formset)) + return array(); + + $header = array(); + foreach ($this->formset[0]->fields as $fieldname => $field) { + $header[] = array( + 'fieldname' => $fieldname, + 'label' => $field->label, + 'is_hidden' => $field->widget->is_hidden, + 'is_del' => $field->label == 'Del', + 'has_display_method' => method_exists($this, 'column_'.$fieldname), + ); + } + return $header; + } + + public function displayField($fieldname, $obj) + { + $method = 'column_'.$fieldname; + if (!method_exists($this, $method)) + throw new IPF_Exception('No method '.$method.' defined.'); + return $this->$method($obj); + } + function isValid() { foreach ($this->formset as &$form) { @@ -84,8 +110,6 @@ abstract class IPF_Admin_ModelInline if ($o) $form_extra['exclude'][] = $o; - $first = true; - if ($this->parentModel->exists()) { $query = IPF_ORM_Query::create() ->from(get_class($this->model)) @@ -119,12 +143,6 @@ abstract class IPF_Admin_ModelInline ), $form->fields); $form->isAdd = false; - if ($first) { - $form->isFirst = true; - $first = false; - } else { - $form->isFirst = false; - } $this->formset[] = $form; } } @@ -136,12 +154,6 @@ abstract class IPF_Admin_ModelInline $form->prefix = 'add_'.get_class($this->model).'_'.$i.'_'; $form->data = $data; $form->isAdd = true; - if ($first) { - $form->isFirst = true; - $first = false; - } else { - $form->isFirst = false; - } $this->formset[] = $form; } } diff --git a/ipf/admin/templates/admin/change.html b/ipf/admin/templates/admin/change.html index 4fe5de5..2984d46 100644 --- a/ipf/admin/templates/admin/change.html +++ b/ipf/admin/templates/admin/change.html @@ -32,20 +32,26 @@

{$inline->getLegend()}

_orderable()} class="orderable-inlne" data-url="{url 'IPF_Admin_Views_ListItems', array($inline->getApplication()->getSlug(), strtolower($inline->getModelName()))}"{/if}> - {foreach $inline.formset as $formset} - {if $formset.isFirst} - {foreach $formset.fields as $fieldname=>$field} - {if !$field.widget.is_hidden}{$formset.field($fieldname).label}{/if} + {foreach $inline->listColumns() as $column} + {$column['label']} {/foreach} - {/if} - - {foreach $formset.fields as $fieldname=>$field} - {$formset.field($fieldname).fieldErrors()}{$formset.field($fieldname)|safe} + {foreach $inline.formset as $form} + + {foreach $inline->listColumns() as $column} + + {assign $field = $form.field($column['fieldname'])} + {if $column['has_display_method']} + {$inline->displayField($column['fieldname'], $form->model) |safe} + {else} + {$field.fieldErrors()} + {$field |safe} + {/if} + {/foreach} {/foreach} -- 2.49.0