]> git.andy128k.dev Git - ipf.git/commitdiff
inline models
authoravl <alex.litovchenko@gmail.com>
Thu, 28 Aug 2008 07:55:48 +0000 (10:55 +0300)
committeravl <alex.litovchenko@gmail.com>
Thu, 28 Aug 2008 07:55:48 +0000 (10:55 +0300)
12 files changed:
ipf.php
ipf/admin/media/css/changelists.css
ipf/admin/media/css/global.css
ipf/admin/model.php
ipf/admin/modelinline.php [new file with mode: 0644]
ipf/admin/templates/admin/change.html
ipf/admin/templates/admin/login.html
ipf/auth/models/User.php
ipf/form.php
ipf/form/model.php
ipf/http/request.php
ipf/orm.php

diff --git a/ipf.php b/ipf.php
index a50ac04d101ec75dd9c3f93b352ca7d8c3dd5c94..b1e186bd11b4e6f03a8cca5a5b830767c2bafda4 100644 (file)
--- a/ipf.php
+++ b/ipf.php
@@ -9,7 +9,7 @@ function __autoload( $class_name ){
             $s .= '/';                                                  
         $s .= strtolower( $folder );                                            
     }
-    require_once($s.'.php');                                 
+    require_once($s.'.php');
 }
 
 final class IPF{
@@ -67,6 +67,10 @@ final class IPF{
             IPF::$settings['app_base'] = '/index.php';
         }
 
+        if (!isset(IPF::$settings['append_slash'])){
+            IPF::$settings['append_slash'] = true;
+        }
+
         if (!isset(IPF::$settings['media_url'])){
             IPF::$settings['media_url'] = '/media/';
         }
@@ -115,7 +119,6 @@ final class IPF{
         array_pop($elts);
         $file = strtolower(implode(DIRECTORY_SEPARATOR, $elts)).'.php';
         @include $file;
-
         if (!function_exists($function))
             throw new IPF_Exception('Impossible to load the function: '.$function.' in '.$file);
     }
index f187ae323774fc0dfcb8e400961896a18a719781..e8e51faaeacd9041b334952451a6c9115f86ef27 100644 (file)
 .change-list ul.toplinks li { float: left; width: 9em; padding:3px 6px; font-weight: bold; list-style-type:none; }
 .change-list ul.toplinks .date-back a { color:#999; }
 .change-list ul.toplinks .date-back a:hover { color:#036; }
+
+/*  INLINE TABULAR  */
+fieldset.inlineTabular {margin-top:5px; padding-top:5px;}
+fieldset.inlineTabular legend{font-size:11pt; color:#888;}
+fieldset.inlineTabular table{width:auto;}
+fieldset.inlineTabular table th{font-weight:bold; color:#666; padding:2px 5px; font-size:11px; background:url(../img/hbg.gif) top left repeat-x; border:1px solid #ABB0BE;}
+fieldset.inlineTabular table td{border:1px solid #DDD;}
index c7e07ae4be3e949b9eb828076b000d647faf0e96..b19a226f7f7333f512826b601ca65b1ce0b15f01 100644 (file)
@@ -118,6 +118,9 @@ input[type=submit], input[type=button], .submit-row input { background:white url
 input[type=submit]:active, input[type=button]:active { background-image:url(../img/nav-bg.gif); background-position:bottom; }
 input[type=submit].default, .submit-row input.default { border:2px solid #888; background: url(../img/hbg.gif) center repeat-x; font-weight:bold; color:#888; }
 input[type=submit].default:active { background-image:url(../img/default-bg-reverse.gif); background-position:top; }
+
+a.button { background:white url(../img/nav-bg.gif) bottom repeat-x; padding:2px; color:black; }
+
 .submit-row { padding:5px 7px; text-align:right; background:url(../img/topbg.gif) top left repeat-x; border:1px solid #ABB0BE; margin:5px 0; }
 .submit-row input { margin:0 0 0 5px; }
 .submit-row .float-left { padding-top:.1em; }
index fce08dbc10e0895b5506fe21195021e04944a08b..e13306a84c80b2dfee86113d51445dbb6a4458a8 100644 (file)
@@ -2,6 +2,7 @@
 
 class IPF_Admin_Model{
     static $models = array();
+    
     public static function register($classModel, $classAdmin){
         IPF_Admin_Model::$models[$classModel] = new $classAdmin($classModel);
     }
@@ -13,30 +14,60 @@ class IPF_Admin_Model{
     }
 
     public static function getModelAdmin($classModel){
-        if (array_key_exists($classModel, IPF_Admin_Model::$models))
-            return IPF_Admin_Model::$models[$classModel];
+        if (array_key_exists($classModel, IPF_Admin_Model::$models)){
+            $ma = IPF_Admin_Model::$models[$classModel];
+            $ma->setUp();
+            return $ma;
+        }
         return null;
     }
     
     var $modelName = null;
-    
+    var $model = null;
+    var $inlineInstances = array();
+
     public function __construct($modelName){
         $this->modelName = $modelName;
     }
     
+    public function setUp(){
+        $this->model = new $this->modelName;
+    }
+    
+    protected function setInlines(&$instance=null){
+        $il = $this->inlines();
+        if (is_array($il)){
+            foreach($il as $inlineName=>$inlineClassName){
+                $this->inlineInstances[] = new $inlineClassName(&$this->model,&$instance);
+            }
+        }
+    }
+    
     protected function _setupEditForm(&$form){
         $this->_setupForm(&$form);
+        $this->setInlines($this->instance);
     }
 
     protected function _setupAddForm(&$form){
         $this->_setupForm(&$form);
+        $this->setInlines();
     }
 
     protected function _setupForm(&$form){
     }
     
-    public function fields(){
-        return null;
+    public function fields(){return null;}
+
+    public function inlines(){return null;}
+    
+    public function isValidInlines(){
+        if ($this->inlineInstances==null)
+            return true;
+        foreach($this->inlineInstances as &$il){
+            if ($il->isValid()===false)
+                return false;
+        }
+        return true;
     }
 
     public function ListItemsHeader(){
@@ -106,9 +137,8 @@ class IPF_Admin_Model{
     
     // Views Function
     public function AddItem($request, $lapp, $lmodel){
-        $model = new $this->modelName();
         if ($request->method == 'POST'){
-            $form = IPF_Shortcuts::GetFormForModel($model,$request->POST+$request->FILES,array('user_fields'=>$this->fields()));
+            $form = IPF_Shortcuts::GetFormForModel($this->model,$request->POST+$request->FILES,array('user_fields'=>$this->fields()));
             $this->_setupAddForm(&$form);
             if ($form->isValid()) {
                 $item = $form->save();
@@ -118,9 +148,8 @@ class IPF_Admin_Model{
             }
         }
         else{
-            $form = IPF_Shortcuts::GetFormForModel($model,null,array('user_fields'=>$this->fields()));
+            $form = IPF_Shortcuts::GetFormForModel($this->model,null,array('user_fields'=>$this->fields()));
             $this->_setupAddForm(&$form);
-            
         }
         $context = array(
             'page_title'=>'Add '.$this->modelName, 
@@ -136,7 +165,7 @@ class IPF_Admin_Model{
         if ($request->method == 'POST'){
             $form = IPF_Shortcuts::GetFormForModel($o,$request->POST+$request->FILES,array('user_fields'=>$this->fields()));
             $this->_setupEditForm(&$form);
-            if ($form->isValid()) {
+            if ( ($form->isValid()) && ($this->isValidInlines()) ) {
                 $item = $form->save();
                 AdminLog::logAction($request, $item, AdminLog::CHANGE);
                 $url = IPF_HTTP_URL_urlForView('IPF_Admin_Views_ListItems', array($lapp, $lmodel));
@@ -146,13 +175,14 @@ class IPF_Admin_Model{
         else{
             $form = IPF_Shortcuts::GetFormForModel($o,$o->getData(),array('user_fields'=>$this->fields()));
             $this->_setupEditForm(&$form);
-            $dd = $o->getData();
         }
+        
         $context = array(
             'page_title'=>'Edit '.$this->modelName, 
             'classname'=>$this->modelName,
             'object'=>$o,
             'form'=>$form,
+            'inlineInstances'=>$this->inlineInstances,
             'lapp'=>$lapp,
             'lmodel'=>$lmodel,
         );
@@ -170,7 +200,6 @@ class IPF_Admin_Model{
             'page_title'=>'Delete '.$this->modelName, 
             'classname'=>$this->modelName,
             'object'=>$o,
-            'form'=>$form,
             'lapp'=>$lapp,
             'lmodel'=>$lmodel,
             'affected'=>array(),
diff --git a/ipf/admin/modelinline.php b/ipf/admin/modelinline.php
new file mode 100644 (file)
index 0000000..7951b83
--- /dev/null
@@ -0,0 +1,66 @@
+<?php
+
+abstract class IPF_Admin_ModelInline{
+
+    var $model = null;
+    var $parentModel = null;
+    var $formset = null;
+    var $parentInstance = null;
+    
+    function __construct(&$parentModel,$parentInstance=null){
+        $this->parentModel = $parentModel;
+        $this->parentInstance = $parentInstance;
+        
+        $modelName = $this->getModelName();
+        $this->model = new $modelName();
+        
+        $this->createFormSet();
+    }
+
+    abstract function getModelName();
+
+    function getAddNum(){ return 2; }
+    
+    function getLegend(){
+        return get_class($this->model);
+    }
+    
+    function isValid(){
+        foreach($this->formset as &$form){
+            if ($form->isValid()==false){
+                return false;
+            }
+        }
+        return true;
+    }
+    
+    function getFkName(){
+        foreach($this->model->getTable()->getRelations() as $rel){
+            if ($rel->getClass()==get_class($this->parentModel))
+                return $rel->getAlias();
+        } 
+        throw new IPF_Exception('Cannot get fkName for '.$this->getModelName());
+    }
+
+    function getFkLocal(){
+        foreach($this->model->getTable()->getRelations() as $rel){
+            if ($rel->getClass()==get_class($this->parentModel))
+                return $rel->getLocal();
+        } 
+        throw new IPF_Exception('Cannot get fkLocal for '.$this->getModelName());
+    }
+
+    function createFormSet(){
+        $this->formset = array();
+        for($i=0; $i<$this->getAddNum(); $i++ ){
+            $form = IPF_Shortcuts::GetFormForModel($this->model, null, array('exclude'=>array($this->getFkName(),$this->getFkLocal())));
+            $form->prefix = "add-$i";
+            if ($i==0)
+                $form->isFirst = true;
+            else
+                $form->isFirst = false;
+            //print_r($form->fields);
+            $this->formset[] = $form;
+        }
+    }
+}
\ No newline at end of file
index 5e2d30a762b16f5365b5c9c5a23594cf414f1e9a..9442bbf32c8085e5915821c56acdf86e7cc7a249 100644 (file)
@@ -8,12 +8,33 @@
 <div id="ipfcontent">
     <h1>{$page_title}</h1>
     
-    <form method="post" {if $form.hasFileField() }enctype="multipart/form-data"{/if}>
+    <form method="post" {if $form.hasFileField()}enctype="multipart/form-data"{/if}>
     <fieldset>
     <table>
     {$form.render_table}
     </table>
     </fieldset>
+    {foreach $inlineInstances as $inline}
+    <fieldset class="inlineTabular">
+    <legend>{$inline->getLegend()}</legend>
+    <table>
+    {foreach $inline.formset as $formset}
+    {if $formset.isFirst}
+    <tr>
+    {foreach $formset.fields as $fieldname=>$field}
+    <th>{$formset.field($fieldname).labelTag()|safe}</th>
+    {/foreach}
+    </tr>
+    {/if}
+    <tr>
+    {foreach $formset.fields as $fieldname=>$field}
+    <td>{$formset.field($fieldname)|safe}</td>
+    {/foreach}
+    </tr>
+    {/foreach}
+    </table>
+    </fieldset>
+    {/foreach}
 
     <div class="submit-row">
         <p class="float-left"><a href="{url 'IPF_Admin_Views_DeleteItem', array($lapp, $lmodel, $object.id)}" class="deletelink">Delete</a></p>
index 40c793c75578362d4a95d7c46978d7ff7ba0654f..dbcdcae101ddf654ae352af3655944b811f095a1 100644 (file)
@@ -1,7 +1,6 @@
 {extends 'admin/base-simple.html'}
 
 {block body}
-
 <div style="text-align:center; margin-top:100px;">
 <div style="margin-left:auto;margin-right:auto; width:300px; text-align:left;">
     <h1>{$page_title}</h1>
index ce37531e0d6f2260ae815e4e458b690f5dfa9abf..1ec352a60b1de63d1cffc11b5a71743f375d0c89 100644 (file)
@@ -10,7 +10,6 @@ class AdminUser extends IPF_Admin_Model{
     }
 
     public function AddItem($request, $lapp, $lmodel){
-        $model = new $this->modelName();
         if ($request->method == 'POST'){
             $form = new IPF_Auth_Forms_UserCreation($request->POST);
             if ($form->isValid()) {
@@ -24,6 +23,7 @@ class AdminUser extends IPF_Admin_Model{
                     $form->cleaned_data['is_staff'],
                     $form->cleaned_data['is_superuser']
                 );
+                AdminLog::logAction($request, $user, AdminLog::ADDITION);
                 $url = IPF_HTTP_URL_urlForView('IPF_Admin_Views_ListItems', array($lapp, $lmodel));
                 return new IPF_HTTP_Response_Redirect($url);
             }
index cb5bd82403db48b7be3a7cccd36401d90e52f8a3..3ab6c0d9151bd048b04fa413c630162a6853c791 100644 (file)
@@ -222,7 +222,6 @@ class IPF_Form implements Iterator
     public function field($key)
     {
         return new IPF_Form_BoundField($this, $this->fields[$key], $key);
-
     }
 
        public function current()
index bd24198dbc99a4a84e4f980b40ccfdff556e9d05..3400c6f14579087384cfa16c6f2816d77f442c6e 100644 (file)
@@ -16,12 +16,21 @@ class IPF_Form_Model extends IPF_Form
         $db_columns = $this->model->getTable()->getColumns();
         $db_relations = $this->model->getTable()->getRelations();
         
-        
         if ($user_fields===null){
+
+            if (isset($extra['exclude'])) 
+                $exclude = $extra['exclude'];
+            else
+                $exclude = array();
+                
             foreach($db_columns as $name=>$col){
+                if (array_search($name,$exclude)!==false)
+                    continue;
                 $this->addDBField($name,$col);
             }
             foreach($db_relations as $name => $relation){
+                if (array_search($name,$exclude)!==false)
+                    continue;
                 $this->addDBRelation($name,$relation);
                    }
         }
@@ -69,8 +78,6 @@ class IPF_Form_Model extends IPF_Form
         //echo $name;
         //print_r($defaults);
 
-
-
         
         if (null !== ($form_field=$db_field->formField($defaults))) {
             $this->fields[$name] = $form_field;
index 22a9a83189e80e215f58d95737115ee9acaecb5d..a7ac0dabd44a1ed103a47ec269d76be9a76da2ad 100644 (file)
@@ -29,6 +29,23 @@ class IPF_HTTP_Request
         $this->uri = $_SERVER['REQUEST_URI'];
         $this->remote_addr = $_SERVER['REMOTE_ADDR'];
         $this->http_host = $_SERVER['HTTP_HOST'];
+        if (isset($_SERVER['PATH_INFO']))
+            $this->path_info = $_SERVER['PATH_INFO'];
+        else
+            $this->path_info = '/';
+        
         $this->SERVER =& $_SERVER;
     }
+    
+    function isSecure(){
+        return false; // # FIXME 
+    }
+    
+    function addUrlrotocol($uri){
+        if ($this->isSecure())
+            $proto = 'https';
+        else
+            $proto = 'http';
+        return $proto.'://'.$uri;
+    }
 }
index ecb2556514f69da16cee053fa267a226949a4b23..f93028221db1530fe68ce4056aa5fd2c67c0296e 100644 (file)
@@ -258,13 +258,11 @@ final class IPF_ORM {
                         $className = $e[0];
                         if ($modelLoading == IPF_ORM::MODEL_LOADING_CONSERVATIVE) {
                             self::loadModel($className, $file->getPathName());
-
                             $loadedModels[$className] = $className;
                         } else {
                             //$declaredBefore = get_declared_classes();
                             require_once($file->getPathName());
                             $loadedModels[$className] = $className; // !!!
-
                             /*
                             //$declaredAfter = get_declared_classes();
                             //$foundClasses = array_slice($declaredAfter, count($declaredBefore) - 1);