Dotclear

Changeset 1053:96e69ef76fc9 for inc


Ignore:
Timestamp:
12/03/12 14:29:47 (13 years ago)
Author:
Dsls <dsls@…>
Branch:
twig
Message:
  • Added page errors & informations in context and default layout page.
  • Added submit actions and hidden fields.
  • cleaned up code in admin/post.php, all actions need to be recoded.
Location:
inc/admin
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • inc/admin/class.dc.admincontext.php

    r1014 r1053  
    66     protected $js_var; 
    77     protected $head; 
     8     protected $globals; 
    89      
    910     protected $core; 
     
    1617          $this->jsCommon(); 
    1718          $this->blogs = array(); 
     19          $this->globals = array(); 
    1820          if ($this->core->auth->blog_count > 1 && $this->core->auth->blog_count < 20) { 
    1921               $rs_blogs = $core->getBlogs(array('order'=>'LOWER(blog_name)','limit'=>20)); 
     
    110112     } 
    111113     public function getGlobals() { 
    112           return array(); 
     114          return $this->globals; 
    113115     } 
    114116     public function getName() { 
    115117          return 'AdminPage'; 
    116118     } 
     119 
     120     public function setMessage($message) { 
     121          $this->globals['page_message'] = $message; 
     122          return $this; 
     123     } 
     124      
     125     public function addError($error) { 
     126          if (!isset($this->globals['page_errors'])) 
     127               $this->globals['page_errors']=array(); 
     128          $this->globals['page_errors'][] = $error; 
     129          return $this; 
     130     } 
     131 
    117132     public function getFilters() 
    118133     { 
  • inc/admin/class.dc.form.php

    r1014 r1053  
    2020                $this->getAttribute('name')."');\n") 
    2121            ->subcompile($this->getNode('body')) 
     22            ->write("\$context['dc_form']->renderHiddenWidgets();\n") 
    2223            ->write("\$context['dc_form']->endForm();\n") 
    2324        ; 
     
    101102     public function renderWidget($name,$attributes=array()) { 
    102103          $field = $this->currentForm->$name; 
    103           if ($field) 
    104           echo $this->template->renderBlock( 
    105                $field->getWidgetBlock(), 
    106                array_merge($field->getAttributes(),array('attr'=>$attributes)), 
    107                $this->blocks); 
    108  
     104          if ($field) { 
     105               echo $this->template->renderBlock( 
     106                    $field->getWidgetBlock(), 
     107                    array_merge($field->getAttributes(),array('attr'=>$attributes)), 
     108                    $this->blocks); 
     109          } 
     110     } 
     111 
     112     public function renderHiddenWidgets() { 
     113          foreach ($this->currentForm->getHiddenFields() as $h) { 
     114               $this->renderWidget($h->getName()); 
     115          } 
    109116     } 
    110117 
     
    132139} 
    133140 
     141class InvalidFieldException extends Exception { 
     142} 
     143 
    134144 
    135145class dcForm { 
     
    138148     protected $action; 
    139149     protected $fields; 
    140  
     150     protected $method; 
     151     protected $submitfields; 
     152     protected $hiddenfields; 
     153     protected $errors; 
     154      
     155     private function addNonce() { 
     156          $this->addField(new dcFieldHidden(array('xd_check'), $this->core->getNonce())); 
     157     } 
     158      
    141159     public function __construct($core,$name,$action, $method='POST') { 
    142160          $this->core = $core; 
     
    146164          $this->fields = array(); 
    147165          $this->core->page->getExtension('dc_form')->addForm($this); 
     166          $this->submitfields = array(); 
     167          $this->hiddenfields = array(); 
     168          $this->errors = array(); 
     169          if ($method == 'POST') { 
     170               $this->addNonce(); 
     171          } 
    148172     } 
    149173 
     
    152176     } 
    153177 
     178     public function getErrors() { 
     179          return $this->errors; 
     180     } 
     181      
    154182     public function addField(dcField $f) { 
     183           
     184          if ($f instanceof dcFieldAction) { 
     185               $this->submitfields[$f->getName()]=$f; 
     186          } 
     187          if ($f instanceof dcFieldHidden) { 
     188               $this->hiddenfields[$f->getName()]=$f; 
     189          } 
     190 
    155191          $this->fields[$f->getName()]=$f; 
    156192          return $this; 
     
    184220    } 
    185221 
     222     public function isSubmitted() { 
     223          $from = ($this->method == 'POST')?$_POST:$_GET; 
     224          echo "form fields :\n"; 
     225     } 
     226      
     227     public function setup() { 
     228          $from = ($this->method=='POST')?$_POST:$_GET; 
     229          foreach ($this->fields as $f) { 
     230               $f->setup($from); 
     231          } 
     232          foreach ($this->submitfields as $f) { 
     233               if ($f->isDefined()) { 
     234                    $ret = call_user_func ($f->getAction(),$this); 
     235                    return; 
     236               } 
     237          } 
     238     } 
     239      
     240     public function check() { 
     241          foreach ($this->fields as $f) { 
     242               try { 
     243                    $f->check(); 
     244               } catch (InvalidFieldException $e) { 
     245                    $this->errors[]=$e->getMessage(); 
     246               } 
     247          } 
     248     } 
     249      
     250     public function getHiddenFields() { 
     251          return $this->hiddenfields; 
     252     } 
     253      
    186254} 
    187255 
     
    191259     protected $value; 
    192260     protected $id; 
     261     protected $defined; 
    193262      
    194263     protected function getNID($nid) { 
     
    208277          $this->attributes['id'] = $this->id; 
    209278          $this->attributes['value'] = $this->value; 
     279          $this->defined = false; 
    210280 
    211281     } 
     
    227297 
    228298     public function check() { 
    229  
    230      } 
     299          if (!$this->defined && $this->attributes['defined']) { 
     300               throw new InvalidFieldException(sprintf('Field "%s" is mandatory'),$this->attributes['label']); 
     301          } 
     302 
     303     } 
     304      
     305     public function setup($from) { 
     306          if (isset($from[$this->id])) { 
     307               $this->value = $from[$this->id]; 
     308               $this->defined = true; 
     309          } 
     310     } 
     311      
     312     public function isDefined() { 
     313          return $this->defined; 
     314     } 
     315      
    231316} 
    232317 
     
    264349} 
    265350 
    266 class dcFieldSubmit extends dcField { 
     351abstract class dcFieldAction extends dcField { 
     352     protected $action; 
     353     public function __construct($name, $value, $attributes = array()) { 
     354          parent::__construct($name, $value, $attributes); 
     355          if (isset($attributes['action'])) { 
     356               $this->action = $attributes['action']; 
     357          } 
     358     } 
     359     public function getAction() { 
     360          return $this->action; 
     361     } 
     362} 
     363 
     364class dcFieldSubmit extends dcFieldAction { 
    267365 
    268366     public function getWidgetBlock() { 
  • inc/admin/default-templates/layout.html.twig

    r993 r1053  
    4040                    {% block content %} 
    4141                    <h2>Mon premier blog &rsaquo; <span class="page-title"></span></h2> 
    42                     {% endblock %}                      
     42                    {% if page_message is not empty %} 
     43                         <p class="message">{{page_message}}</p> 
     44                    {% endif %} 
     45                    {% if page_errors is not empty %} 
     46                         <div class="error"> 
     47                              <p><strong>Errors :</strong></p> 
     48                              <ul> 
     49                              {% for error in page_errors %} 
     50                                   <li>{{error}}</li> 
     51                              {% endfor %} 
     52                              </ul> 
     53                         </div> 
     54                    {% endif %} 
     55                    {% endblock %} 
     56 
    4357               </div> 
    4458          </div> 
Note: See TracChangeset for help on using the changeset viewer.

Sites map