Dotclear

Changeset 1053:96e69ef76fc9


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.
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • admin/post.php

    r1014 r1053  
    1414 
    1515dcPage::check('usage,contentadmin'); 
     16 
     17function savePost($form) { 
     18     global $core; 
     19     $core->page->getContext()->setMessage('save'); 
     20 
     21} 
     22 
     23function deletePost($form) { 
     24     print_r($form); exit; 
     25} 
     26 
    1627$page_title = __('New entry'); 
    1728 
     
    3647     $categories = $core->blog->getCategories(array('post_type'=>'post')); 
    3748     while ($categories->fetch()) { 
    38           $categories_combo[] = new formSelectOption( 
    39                str_repeat('&nbsp;&nbsp;',$categories->level-1).($categories->level-1 == 0 ? '' : '&bull; ').html::escapeHTML($categories->cat_title), 
    40                $categories->cat_id 
    41           ); 
     49          $categories_combo[$categories->cat_id] =  
     50               str_repeat('&nbsp;&nbsp;',$categories->level-1). 
     51               ($categories->level-1 == 0 ? '' : '&bull; '). 
     52               html::escapeHTML($categories->cat_title); 
    4253     } 
    4354} catch (Exception $e) { } 
     
    6879unset($rs); 
    6980 
    70  
    71  
    7281$form = new dcForm($core,'post','post.php'); 
    7382$form 
     
    9099               'label'        => __("Notes")))) 
    91100     ->addField( 
    92           new dcFieldSubmit('save',__('Save'),array())) 
     101          new dcFieldSubmit('save',__('Save'),array( 
     102               'action' => 'savePost'))) 
    93103     ->addField( 
    94           new dcFieldSubmit('delete',__('Delete'),array())) 
     104          new dcFieldSubmit('delete',__('Delete'),array( 
     105               'action' => 'deletePost'))) 
    95106     ->addField( 
    96107          new dcFieldCombo('post_status',$core->auth->getInfo('user_post_status'),$status_combo,array( 
     
    118129          new dcFieldCombo ('post_lang',$core->auth->getInfo('user_lang'),$lang_combo, array( 
    119130               "label" => __('Entry lang:')))) 
     131     ->addField( 
     132          new dcFieldHidden ('id','')) 
    120133; 
    121  
    122  
    123134# Get entry informations 
    124135if (!empty($_REQUEST['id'])) 
     
    135146     else 
    136147     { 
    137           $form->post_id = $post->post_id; 
     148          $form->id = $post->post_id; 
    138149          $form->cat_id = $post->cat_id; 
    139150          $form->post_dt = date('Y-m-d H:i',strtotime($post->post_dt)); 
     
    144155          $form->post_title = $post->post_title; 
    145156          $form->post_excerpt = $post->post_excerpt; 
    146           $post_excerpt_xhtml = $post->post_excerpt_xhtml; 
     157          $form->post_excerpt_xhtml = $post->post_excerpt_xhtml; 
    147158          $form->post_content = $post->post_content; 
    148           $post_content_xhtml = $post->post_content_xhtml; 
     159          $form->post_content_xhtml = $post->post_content_xhtml; 
    149160          $form->post_notes = $post->post_notes; 
    150161          $form->post_status = $post->post_status; 
     
    152163          $form->post_open_comment = (boolean) $post->post_open_comment; 
    153164          $form->post_open_tb = (boolean) $post->post_open_tb; 
     165          $form->can_edit_post = $post->isEditable(); 
     166          $form->can_delete= $post->isDeletable(); 
    154167           
    155           $page_title = __('Edit entry'); 
    156            
    157           $can_edit_post = $post->isEditable(); 
    158           $can_delete= $post->isDeletable(); 
    159            
    160           $next_rs = $core->blog->getNextPost($post,1); 
    161           $prev_rs = $core->blog->getNextPost($post,-1); 
    162            
    163           if ($next_rs !== null) { 
    164                $next_link = sprintf($post_link,$next_rs->post_id, 
    165                     html::escapeHTML($next_rs->post_title),__('next entry').'&nbsp;&#187;'); 
    166                $next_headlink = sprintf($post_headlink,'next', 
    167                     html::escapeHTML($next_rs->post_title),$next_rs->post_id); 
    168           } 
    169            
    170           if ($prev_rs !== null) { 
    171                $prev_link = sprintf($post_link,$prev_rs->post_id, 
    172                     html::escapeHTML($prev_rs->post_title),'&#171;&nbsp;'.__('previous entry')); 
    173                $prev_headlink = sprintf($post_headlink,'previous', 
    174                     html::escapeHTML($prev_rs->post_title),$prev_rs->post_id); 
    175           } 
    176            
    177           try { 
    178                $core->media = new dcMedia($core); 
    179           } catch (Exception $e) {} 
    180168     } 
    181169} 
    182170 
    183 # Format excerpt and content 
    184 if (!empty($_POST) && $can_edit_post) 
    185 { 
    186      $post_format = $_POST['post_format']; 
    187      $post_excerpt = $_POST['post_excerpt']; 
    188      $post_content = $_POST['post_content']; 
    189       
    190      $post_title = $_POST['post_title']; 
    191       
    192      $cat_id = (integer) $_POST['cat_id']; 
    193       
    194      if (isset($_POST['post_status'])) { 
    195           $post_status = (integer) $_POST['post_status']; 
    196      } 
    197       
    198      if (empty($_POST['post_dt'])) { 
    199           $post_dt = ''; 
    200      } else { 
    201           $post_dt = strtotime($_POST['post_dt']); 
    202           $post_dt = date('Y-m-d H:i',$post_dt); 
    203      } 
    204       
    205      $post_open_comment = !empty($_POST['post_open_comment']); 
    206      $post_open_tb = !empty($_POST['post_open_tb']); 
    207      $post_selected = !empty($_POST['post_selected']); 
    208      $post_lang = $_POST['post_lang']; 
    209      $post_password = !empty($_POST['post_password']) ? $_POST['post_password'] : null; 
    210       
    211      $post_notes = $_POST['post_notes']; 
    212       
    213      if (isset($_POST['post_url'])) { 
    214           $post_url = $_POST['post_url']; 
    215      } 
    216       
    217      $core->blog->setPostContent( 
    218           $post_id,$post_format,$post_lang, 
    219           $post_excerpt,$post_excerpt_xhtml,$post_content,$post_content_xhtml 
    220      ); 
    221 } 
     171$form->setup(); 
    222172 
    223 # Create or update post 
    224 if (!empty($_POST) && !empty($_POST['save']) && $can_edit_post) 
    225 { 
    226      $cur = $core->con->openCursor($core->prefix.'post'); 
    227       
    228      $cur->post_title = $post_title; 
    229      $cur->cat_id = ($cat_id ? $cat_id : null); 
    230      $cur->post_dt = $post_dt ? date('Y-m-d H:i:00',strtotime($post_dt)) : ''; 
    231      $cur->post_format = $post_format; 
    232      $cur->post_password = $post_password; 
    233      $cur->post_lang = $post_lang; 
    234      $cur->post_title = $post_title; 
    235      $cur->post_excerpt = $post_excerpt; 
    236      $cur->post_excerpt_xhtml = $post_excerpt_xhtml; 
    237      $cur->post_content = $post_content; 
    238      $cur->post_content_xhtml = $post_content_xhtml; 
    239      $cur->post_notes = $post_notes; 
    240      $cur->post_status = $post_status; 
    241      $cur->post_selected = (integer) $post_selected; 
    242      $cur->post_open_comment = (integer) $post_open_comment; 
    243      $cur->post_open_tb = (integer) $post_open_tb; 
    244       
    245      if (isset($_POST['post_url'])) { 
    246           $cur->post_url = $post_url; 
    247      } 
    248       
    249      # Update post 
    250      if ($post_id) 
    251      { 
    252           try 
    253           { 
    254                # --BEHAVIOR-- adminBeforePostUpdate 
    255                $core->callBehavior('adminBeforePostUpdate',$cur,$post_id); 
    256                 
    257                $core->blog->updPost($post_id,$cur); 
    258                 
    259                # --BEHAVIOR-- adminAfterPostUpdate 
    260                $core->callBehavior('adminAfterPostUpdate',$cur,$post_id); 
    261                 
    262                http::redirect('post.php?id='.$post_id.'&upd=1'); 
    263           } 
    264           catch (Exception $e) 
    265           { 
    266                $core->error->add($e->getMessage()); 
    267           } 
    268      } 
    269      else 
    270      { 
    271           $cur->user_id = $core->auth->userID(); 
    272            
    273           try 
    274           { 
    275                # --BEHAVIOR-- adminBeforePostCreate 
    276                $core->callBehavior('adminBeforePostCreate',$cur); 
    277                 
    278                $return_id = $core->blog->addPost($cur); 
    279                 
    280                # --BEHAVIOR-- adminAfterPostCreate 
    281                $core->callBehavior('adminAfterPostCreate',$cur,$return_id); 
    282                 
    283                http::redirect('post.php?id='.$return_id.'&crea=1'); 
    284           } 
    285           catch (Exception $e) 
    286           { 
    287                $core->error->add($e->getMessage()); 
    288           } 
    289      } 
    290 } 
    291  
    292 if (!empty($_POST['delete']) && $can_delete) 
    293 { 
    294      try { 
    295           # --BEHAVIOR-- adminBeforePostDelete 
    296           $core->callBehavior('adminBeforePostDelete',$post_id); 
    297           $core->blog->delPost($post_id); 
    298           http::redirect('posts.php'); 
    299      } catch (Exception $e) { 
    300           $core->error->add($e->getMessage()); 
    301      } 
    302 } 
    303173 
    304174/* DISPLAY 
     
    323193echo $core->page->render('post.html.twig',array( 
    324194     'edit_size'=> $core->auth->getOption('edit_size'))); 
     195      
     196 
    325197?> 
  • 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